forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to Play an MP3 Every Time There is a Collision?

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
pspfan.



Joined: 06 Feb 2006
Posts: 4

PostPosted: Tue Aug 22, 2006 5:16 pm    Post subject: How to Play an MP3 Every Time There is a Collision? Reply with quote

I'm trying to play an MP3 every time there is a collision in my program.

Should I load the MP3 in the collision function or before (I have tried both, but not sure which is better, both do the same things). Also, what about MP3_Init()?

More importantly, how do I stop the MP3 after one time, but play it again when there is another collision.

I've been playing around with it trying different MP3 functions to try to what I just mentioned, but I either can't stop the MP3 after it plays once, or I can't get it to play again when there's another collision, or both.

How do I make it work (what functions do I use) the way I want it to? Thanks in advance.

P.S. Most of what I've been changing is this function (after the if statement): if(MP3_EndOfStream() == 1) MP3_Stop();
Back to top
View user's profile Send private message
pspfan.



Joined: 06 Feb 2006
Posts: 4

PostPosted: Sat Jul 21, 2007 10:16 am    Post subject: Reply with quote

Sorry to bump my old thread, but it's a year later and I decided to pick up my project from last year. I never got it working with an MP3, so I tried using Insomniac's/Insert_Witty_Name's Wavlib. Unfortunately, something I did ended up giving me a blackscreen that eventually turns off the PSP (Home button doesn't work). So I'm asking someone with greater knowledge to help me see why it's turning off on me. Here's my source:

Code:

// Moving Image with Collision Detection and Sound
// (.Wav Version)

#include <pspkernel.h>
#include <pspctrl.h>
#include <pspdebug.h>
#include <pspaudio.h>
#include <pspaudiolib.h>
#include <psppower.h>
#include <pspdisplay.h>
#include <pspgu.h>
#include <png.h>
#include <stdio.h>
#include "graphics.h"
#include "wavloader.h"

PSP_MODULE_INFO("Moving Image With Collision", 0, 1, 1);

#define printf pspDebugScreenPrintf
#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
#define RGB(r, g, b) ((r)|((g)<<8)|((b)<<16))

#define WIDTH 480 //Yep, I later realized later that I could have used SCREEN_WIDTH, but oh well
#define HEIGHT 272
#define INCR 5 //How much one button push on the D-pad will move the guy
#define TRUE 1
#define FALSE 0
#define T1X 75 //Tree placement coordinates
#define T1Y 100
#define T2X 250
#define T2Y 15
#define T3X 425
#define T3Y 230

/* Exit callback */
int exit_callback(int arg1, int arg2, void *common) {
          WAV_End(); //DE-INITIALISE WAV LOADER
          sceKernelExitGame();
          return 0;
}

/* Callback thread */
int CallbackThread(SceSize args, void *argp) {
          int cbid;

          cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
          sceKernelRegisterExitCallback(cbid);

          sceKernelSleepThreadCB();

          return 0;
}

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks(void) {
          int thid = 0;

          thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
          if(thid >= 0) {
                    sceKernelStartThread(thid, 0, 0);
          }

          return thid;
}

void drawTrees(Image * tree) //Function for drawing the Trees to the screen...
{

   if(!tree) printf("Image load failed\n");
   else
   {
      blitAlphaImageToScreen(0, 0, tree->imageWidth, tree->imageHeight, tree, T1X, T1Y);
      blitAlphaImageToScreen(0, 0, tree->imageWidth, tree->imageHeight, tree, T2X, T2Y);
      blitAlphaImageToScreen(0, 0, tree->imageWidth, tree->imageHeight, tree, T3X, T3Y);
   }
}

int main()
{
   scePowerSetClockFrequency(333, 333, 166); //I don't need this, but I don't want to change things until switching from MP3s to WAVs is successful

   SetupCallbacks();
   pspDebugScreenInit();
   initGraphics();

//   pspAudioInit();   Don't think I need this, so I commented it out, but it didn't help.

   WAV_Init(); //I tried putting this and the line below directly before where I play the WAV, but it didn't help. I think this is this the better spot, right?
   WAV* ourWav = WAV_Load("./oops.wav");
   SceCtrlData pad;
   int i;

   int x, y;
   char collision = FALSE;

   char buffer[200]; //I also realized I don't need to do this to load an image, but I don't want to change things until switching from MP3s to WAVs is successful
   Image* tree, * neal;
   Color black=RGB(0,0,0);
   int leftX, rightX, topY, bottomY;
   
   sprintf(buffer, "tree.png");
   tree = loadImage(buffer);

   sprintf(buffer, "neal.png");
   neal = loadImage(buffer);

   x = (WIDTH / 2) - (neal->imageWidth / 2); //Placing the guy to start in the middle of the screen
   y = (HEIGHT / 2) - (neal->imageHeight / 2);

   while(1)
   {
      if(!collision)
      {
         sceGuClearColor(black);
         clearScreen(black); //Do I need this? It was in here from when I worked on it last year, but shouldn't the last line take care of it?
         drawTrees(tree); //Drawing the trees
         blitAlphaImageToScreen(0, 0, neal->imageWidth, neal->imageHeight, neal, x, y); //Drawing the guy
         flipScreen();
      }
      else
      {
         WAV_Play(ourWav); //Play the sound
         collision = FALSE; //No more collision...
      }

      sceCtrlReadBufferPositive(&pad, 1);
      //------------------------------------------------
      // Increment x and y according to the direction
      //------------------------------------------------
      if(pad.Buttons & PSP_CTRL_UP)      y -= INCR;
      else if(pad.Buttons & PSP_CTRL_RIGHT)   x += INCR;
      else if(pad.Buttons & PSP_CTRL_DOWN)   y += INCR;
      else if(pad.Buttons & PSP_CTRL_LEFT)   x -= INCR;

      //--------------------------------------------------------
      // Make sure we stay in the bounds of the screen
      //--------------------------------------------------------
      if(x + neal->imageWidth > WIDTH)   x = WIDTH - neal->imageWidth;
      if(x < 0)            x = 0;
      if(y + neal->imageHeight > HEIGHT)   y = HEIGHT - neal->imageHeight;
      if(y < 0)            y = 0;

      leftX   = x;
      rightX   = x + neal->imageWidth;
      topY   = y;
      bottomY   = y + neal->imageHeight;

//---------------------------------
// COLLISION DETECTION (START):
//---------------------------------

      for(i = topY; i <= bottomY; i++)
      {
         if((leftX >= T1X && leftX <= T1X + tree->imageWidth) && (i >= T1Y && i <= T1Y + tree->imageHeight))
         {
            x += INCR;
            collision = TRUE;
            break;
         }
         if((leftX >= T2X && leftX <= T2X + tree->imageWidth) && (i >= T2Y && i <= T2Y + tree->imageHeight))
         {
            x += INCR;
            collision = TRUE;
            break;
         }
         if((leftX >= T3X && leftX <= T3X + tree->imageWidth) && (i >= T3Y && i <= T3Y + tree->imageHeight))
         {
            x += INCR;
            collision = TRUE;
            break;
         }
      }

      for(i = topY; i <= bottomY; i++)
      {
         if((rightX >= T1X && rightX <= T1X + tree->imageWidth) && (i >= T1Y && i <= T1Y + tree->imageHeight))
         {
            x -= INCR;
            collision = TRUE;
            break;
         }
         if((rightX >= T2X && rightX <= T2X + tree->imageWidth) && (i >= T2Y && i <= T2Y + tree->imageHeight))
         {
            x-= INCR;
            collision = TRUE;
            break;
         }
         if((rightX >= T3X && rightX <= T3X + tree->imageWidth) && (i >= T3Y && i <= T3Y + tree->imageHeight))
         {
            x-= INCR;
            collision = TRUE;
            break;
         }
      }

      for(i = leftX; i <= rightX; i++)
      {
         if((topY >= T1Y && topY <= T1Y + tree->imageHeight) && (i >= T1X && i <= T1X + tree->imageWidth))
         {
            y += INCR;
            collision = TRUE;
            break;
         }
         if((topY >= T2Y && topY <= T2Y + tree->imageHeight) && (i >= T2X && i <= T2X + tree->imageWidth))
         {
            y += INCR;
            collision = TRUE;
            break;
         }
         if((topY >= T3Y && topY <= T3Y + tree->imageHeight) && (i >= T3X && i <= T3X + tree->imageWidth))
                        {
            y += INCR;
            collision = TRUE;
            break;
            }
      }

      for(i = leftX; i <= rightX; i++)
      {
         if((bottomY >= T1Y && topY <= T1Y + tree->imageHeight) && (i >= T1X && i <= T1X + tree->imageWidth))
         {
            y-= INCR;
            collision = TRUE;
            break;
         }
         if((bottomY >= T2Y && topY <= T2Y + tree->imageHeight) && (i >= T2X && i <= T2X + tree->imageWidth))
         {
            y-= INCR;
            collision = TRUE;
            break;
            }
            if((bottomY >= T3Y && topY <= T3Y + tree->imageHeight) && (i >= T3X && i <= T3X + tree->imageWidth))
            {
            y-= INCR;
            collision = TRUE;
            break;
         }
      }

//------------------------------
// COLLISION DETECTION (END)
//------------------------------

      for(i=0; i<10; i++) sceDisplayWaitVblankStart();
   }

   WAV_Unload(ourWav);

   sceKernelSleepThread();

   return 0;
}



I've got no errors (obviously) or warnings making it, wavloader.h is in the folder so it's included fine, and I've got my two .pngs and my .wav to go along with the EBOOT in the same folder on my PSP. Also, using the simple sample from the wavlib download I checked to see if my .wav file works, and it played just fine.

So, what's my really stupid mistake? Thanks in advance for any help, it's greatly appreciated!
Back to top
View user's profile Send private message
Jim



Joined: 02 Jul 2005
Posts: 487
Location: Sydney

PostPosted: Sun Jul 22, 2007 11:03 pm    Post subject: Reply with quote

What lib are you using for sound? If it's mikmod, make sure you're using an 8bit mono wav.

Much better to load all data before your collisions.
a) you don't want memory stick access in your collision function.
b) you don't want to load the wav for every collision.

Jim
_________________
http://www.dbfinteractive.com
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group