Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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!

share|improve this question
Try adding the sound file to the xcode project. – Tetrad Feb 11 at 20:54
Voting to close for "debug my code for me." This would be better asked in our chat or in a discussion forum which is designed for personal assistance. We're aiming for more universal and concrete Q&A, here. :) – Trevor Powell Feb 13 at 0:07

closed as too localized by Sean Middleditch, bummzack, Byte56, Josh Petrie, Trevor Powell Feb 13 at 0:07

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

Browse other questions tagged or ask your own question.