I am trying to load a simple sound effect using SDL_mixer on xcode but it is not loading. Here is the code:
#include <SDL/SDL.h>
#include "SDL_image.h"
#include "SDL_mixer.h"
#include "SDLMain.h"
#include <iostream>
bool isRunning = true;
bool moving = false;
//surfaces
SDL_Surface* screen = NULL;
SDL_Surface* hello = NULL;
SDL_Surface* background= NULL;
//sound effects
Mix_Chunk *gunshot = NULL;
const int SCREEN_WIDTH = 600;
const int SCREEN_HEIGHT = 400;
SDL_Rect offset;
int main(int argc, char * argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
//surfaces
SDL_Event event;
screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT, 32, SDL_DOUBLEBUF);
hello = IMG_Load("stickman.png");
background = IMG_Load("background.png");
gunshot = Mix_LoadWAV("gunshot.wav");
if (gunshot == NULL)
{
std::cout<< "GUN NOT LOADED" << std::endl;
}
if (hello == NULL)
{
std::cout<< "Could not load image" << std::endl;
return 1;
}
offset.x = (SCREEN_WIDTH - (hello->w))/2;
offset.y = SCREEN_HEIGHT - (hello->h + 42);
SDL_BlitSurface(background, NULL, screen, NULL);
SDL_BlitSurface(hello, NULL, screen, &offset);
while(isRunning)
{
while(SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
isRunning = false;
}
if (event.type == SDL_KEYDOWN)
{
if (event.key.keysym.sym == SDLK_SPACE)
{
if (Mix_PlayChannel(-1, gunshot, 0)==-1)
{
return 1;
}
}
moving = true;
}
if (event.type == SDL_KEYUP)
{
moving = false;
}
}
//update game after this comment
if (moving == true)
{
switch (event.key.keysym.sym) {
case SDLK_LEFT:
hello = IMG_Load("stickmanleft.png");
offset.x-=5;
break;
case SDLK_RIGHT:
hello = IMG_Load("stickman.png");
offset.x +=5;
break;
default:
break;
}
}
SDL_BlitSurface(background, NULL, screen, NULL);
SDL_BlitSurface(hello, NULL, screen, &offset);
SDL_Flip(screen);
}
SDL_FreeSurface(hello);
SDL_Quit();
return 0;
}
I have been using SDL for a little bit now and am still a beginner but I have just switched from windows to Mac. I had no problem loading the sounds on my windows computer but now I can't seem to figure out how this works in mac... where should I put the sound file? It is currently in the debug folder.
Any help or pointers are appreciated. thanks!