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

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
pspfan.
Posts: 4
Joined: Mon Feb 06, 2006 9:36 am

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

Post by pspfan. »

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();
pspfan.
Posts: 4
Joined: Mon Feb 06, 2006 9:36 am

Post by pspfan. »

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: Select all

// 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&#40;"Moving Image With Collision", 0, 1, 1&#41;;

#define printf pspDebugScreenPrintf
#define MAX&#40;X, Y&#41; &#40;&#40;X&#41; > &#40;Y&#41; ? &#40;X&#41; &#58; &#40;Y&#41;&#41;
#define RGB&#40;r, g, b&#41; &#40;&#40;r&#41;|&#40;&#40;g&#41;<<8&#41;|&#40;&#40;b&#41;<<16&#41;&#41;

#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&#40;int arg1, int arg2, void *common&#41; &#123;
          WAV_End&#40;&#41;; //DE-INITIALISE WAV LOADER
          sceKernelExitGame&#40;&#41;;
          return 0;
&#125;

/* Callback thread */
int CallbackThread&#40;SceSize args, void *argp&#41; &#123;
          int cbid;

          cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
          sceKernelRegisterExitCallback&#40;cbid&#41;;

          sceKernelSleepThreadCB&#40;&#41;;

          return 0;
&#125;

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks&#40;void&#41; &#123;
          int thid = 0;

          thid = sceKernelCreateThread&#40;"update_thread", CallbackThread, 0x11, 0xFA0, 0, 0&#41;;
          if&#40;thid >= 0&#41; &#123;
                    sceKernelStartThread&#40;thid, 0, 0&#41;;
          &#125;

          return thid;
&#125;

void drawTrees&#40;Image * tree&#41; //Function for drawing the Trees to the screen...
&#123;

	if&#40;!tree&#41; printf&#40;"Image load failed\n"&#41;;
	else
	&#123;
		blitAlphaImageToScreen&#40;0, 0, tree->imageWidth, tree->imageHeight, tree, T1X, T1Y&#41;;
		blitAlphaImageToScreen&#40;0, 0, tree->imageWidth, tree->imageHeight, tree, T2X, T2Y&#41;;
		blitAlphaImageToScreen&#40;0, 0, tree->imageWidth, tree->imageHeight, tree, T3X, T3Y&#41;;
	&#125;
&#125;

int main&#40;&#41;
&#123;
	scePowerSetClockFrequency&#40;333, 333, 166&#41;; //I don't need this, but I don't want to change things until switching from MP3s to WAVs is successful

	SetupCallbacks&#40;&#41;;
	pspDebugScreenInit&#40;&#41;;
	initGraphics&#40;&#41;;

//	pspAudioInit&#40;&#41;;   Don't think I need this, so I commented it out, but it didn't help.

	WAV_Init&#40;&#41;; //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&#40;"./oops.wav"&#41;;
	SceCtrlData pad;
	int i;

	int x, y;
	char collision = FALSE;

	char buffer&#91;200&#93;; //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&#40;0,0,0&#41;;
	int leftX, rightX, topY, bottomY;
	
	sprintf&#40;buffer, "tree.png"&#41;;
	tree = loadImage&#40;buffer&#41;;

	sprintf&#40;buffer, "neal.png"&#41;;
	neal = loadImage&#40;buffer&#41;;

	x = &#40;WIDTH / 2&#41; - &#40;neal->imageWidth / 2&#41;; //Placing the guy to start in the middle of the screen
	y = &#40;HEIGHT / 2&#41; - &#40;neal->imageHeight / 2&#41;;

	while&#40;1&#41;
	&#123;
		if&#40;!collision&#41;
		&#123;
			sceGuClearColor&#40;black&#41;;
			clearScreen&#40;black&#41;; //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&#40;tree&#41;; //Drawing the trees
			blitAlphaImageToScreen&#40;0, 0, neal->imageWidth, neal->imageHeight, neal, x, y&#41;; //Drawing the guy
			flipScreen&#40;&#41;;
		&#125;
		else
		&#123;
			WAV_Play&#40;ourWav&#41;; //Play the sound
			collision = FALSE; //No more collision...
		&#125;

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

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

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

//---------------------------------
// COLLISION DETECTION &#40;START&#41;&#58;
//---------------------------------

		for&#40;i = topY; i <= bottomY; i++&#41;
		&#123;
			if&#40;&#40;leftX >= T1X && leftX <= T1X + tree->imageWidth&#41; && &#40;i >= T1Y && i <= T1Y + tree->imageHeight&#41;&#41;
			&#123;
				x += INCR;
				collision = TRUE;
				break;
			&#125;
			if&#40;&#40;leftX >= T2X && leftX <= T2X + tree->imageWidth&#41; && &#40;i >= T2Y && i <= T2Y + tree->imageHeight&#41;&#41;
			&#123;
				x += INCR;
				collision = TRUE;
				break;
			&#125;
			if&#40;&#40;leftX >= T3X && leftX <= T3X + tree->imageWidth&#41; && &#40;i >= T3Y && i <= T3Y + tree->imageHeight&#41;&#41;
			&#123;
				x += INCR;
				collision = TRUE;
				break;
			&#125;
		&#125;

		for&#40;i = topY; i <= bottomY; i++&#41;
		&#123;
			if&#40;&#40;rightX >= T1X && rightX <= T1X + tree->imageWidth&#41; && &#40;i >= T1Y && i <= T1Y + tree->imageHeight&#41;&#41;
			&#123;
				x -= INCR;
				collision = TRUE;
				break;
			&#125;
			if&#40;&#40;rightX >= T2X && rightX <= T2X + tree->imageWidth&#41; && &#40;i >= T2Y && i <= T2Y + tree->imageHeight&#41;&#41;
			&#123;
				x-= INCR;
				collision = TRUE;
				break;
			&#125;
			if&#40;&#40;rightX >= T3X && rightX <= T3X + tree->imageWidth&#41; && &#40;i >= T3Y && i <= T3Y + tree->imageHeight&#41;&#41;
			&#123;
				x-= INCR;
				collision = TRUE;
				break;
			&#125;
		&#125;

		for&#40;i = leftX; i <= rightX; i++&#41;
		&#123;
			if&#40;&#40;topY >= T1Y && topY <= T1Y + tree->imageHeight&#41; && &#40;i >= T1X && i <= T1X + tree->imageWidth&#41;&#41;
			&#123;
				y += INCR;
				collision = TRUE;
				break;
			&#125;
			if&#40;&#40;topY >= T2Y && topY <= T2Y + tree->imageHeight&#41; && &#40;i >= T2X && i <= T2X + tree->imageWidth&#41;&#41;
			&#123;
				y += INCR;
				collision = TRUE;
				break;
			&#125;
			if&#40;&#40;topY >= T3Y && topY <= T3Y + tree->imageHeight&#41; && &#40;i >= T3X && i <= T3X + tree->imageWidth&#41;&#41;
                        &#123;
				y += INCR;
				collision = TRUE;
				break;
				&#125;
		&#125;

		for&#40;i = leftX; i <= rightX; i++&#41;
		&#123;
			if&#40;&#40;bottomY >= T1Y && topY <= T1Y + tree->imageHeight&#41; && &#40;i >= T1X && i <= T1X + tree->imageWidth&#41;&#41;
			&#123;
				y-= INCR;
				collision = TRUE;
				break;
			&#125;
			if&#40;&#40;bottomY >= T2Y && topY <= T2Y + tree->imageHeight&#41; && &#40;i >= T2X && i <= T2X + tree->imageWidth&#41;&#41;
			&#123;
				y-= INCR;
				collision = TRUE;
				break;
				&#125;
				if&#40;&#40;bottomY >= T3Y && topY <= T3Y + tree->imageHeight&#41; && &#40;i >= T3X && i <= T3X + tree->imageWidth&#41;&#41;
				&#123;
				y-= INCR;
				collision = TRUE;
				break;
			&#125;
		&#125;

//------------------------------
// COLLISION DETECTION &#40;END&#41;
//------------------------------

		for&#40;i=0; i<10; i++&#41; sceDisplayWaitVblankStart&#40;&#41;;
	&#125;

	WAV_Unload&#40;ourWav&#41;;

	sceKernelSleepThread&#40;&#41;;

	return 0;
&#125;

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!
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

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
Post Reply