My Sound header file
#include
<SDL_mixer.h>
#include
<string>
using
namespace std;
class
Sound
{
public
:
Sound();
~Sound();
bool initSound();
bool loadFile();
Mix_Chunk* loadSound(string filename);
void playSound(int playWhich);
void pauseSound();
void stopSound();
void shutDown();
private
:
Mix_Music* music;
Mix_Chunk* scratch;
Mix_Chunk* high;
Mix_Chunk* med;
Mix_Chunk* low;
};
Sound Source Code
#include
"Sound.h"
Sound::Sound()
{
music = NULL;
scratch = NULL;
high = NULL;
med = NULL;
low = NULL;
}
Sound::~Sound()
{
}
bool
Sound::initSound()
{
int freq = 0;
Uint16 format = 0;
int channels = 0;
Mix_QuerySpec(&freq, &format, &channels);
if(Mix_OpenAudio(freq, format, channels, 4096) == -1)
return false;
return true;
}
bool
Sound::loadFile()
{
music = Mix_LoadMUS(
"beat.wav");
if(music == NULL)
{
return false;
}
scratch = loadSound(
"scratch.wav");
high = loadSound(
"high.wav");
med = loadSound(
"medium.wav");
low = loadSound(
"low.wav");
if((scratch == NULL) || (high == NULL) ||
(med == NULL) || (low == NULL))
{
return false;
}
return true;
}
Mix_Chunk* Sound::loadSound(string filename)
{
Mix_Chunk* sound = NULL;
sound = Mix_LoadWAV(filename.c_str());
if(sound == NULL)
{
return NULL;
}
return sound;
}
void
Sound::playSound(int playWhich)
{
switch(playWhich)
{
case 1:
if(Mix_PlayChannel(-1, scratch, 0) == -1)
break;
case 2:
if(Mix_PlayChannel(-1, high, 0) == -1)
break;
case 3:
if(Mix_PlayChannel(-1, med, 0) == -1)
break;
case 4:
if(Mix_PlayChannel(-1, low, 0) == -1)
break;
default:
break;
}
}
void
Sound::pauseSound()
{
if(Mix_PlayingMusic() == 0)
{
if(Mix_PlayMusic(music, -1) == -1)
return;
}
else
{
if(Mix_PausedMusic() == 1)
{
Mix_ResumeMusic();
}
else
{
Mix_PauseMusic();
}
}
}
void
Sound::stopSound()
{
Mix_HaltMusic();
}
void
Sound::shutDown()
{
Mix_FreeChunk(scratch);
Mix_FreeChunk(high);
Mix_FreeChunk(med);
Mix_FreeChunk(low);
Mix_FreeMusic(music);
Mix_CloseAudio();
}
My Main Source File
#include
<SDL.h>
#include
<SDL_ttf.h>
#include
"Sound.h"
#include
<string>
using
namespace std;
const
int SCRN_WIDTH = 640;
const
int SCRN_HEIGHT = 480;
const
int SCRN_BPP = 32;
SDL_Surface* videoSurface = NULL;
SDL_Surface* background = NULL;
SDL_Surface* instruction1 = NULL;
SDL_Surface* instruction2 = NULL;
SDL_Surface* instruction3 = NULL;
SDL_Event
event;
TTF_Font* font = NULL;
SDL_Color fontColor = {255, 255, 255};
Sound* soundMgr = NULL;
bool
initSDL();
bool
initTTF();
bool
loadFiles();
SDL_Surface* loadBMP(string filename);
void
applySurface(int x, int y,
SDL_Surface* src,
SDL_Surface* dest,
SDL_Rect* clip = NULL);
void
setText();
void
renderText();
void
shutDown();
int
main(int argc, char** argv)
{
bool quit = false;
if(!initSDL())
return -1;
if(!loadFiles())
return -1;
if(!soundMgr->initSound())
return -1;
setText();
applySurface(0, 0, background, videoSurface, 0);
renderText();
while(!quit)
{
if(SDL_PollEvent(&event))
{
if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_1)
{
soundMgr->playSound(1);
}
if(event.key.keysym.sym == SDLK_2)
{
soundMgr->playSound(2);
}
if(event.key.keysym.sym == SDLK_3)
{
soundMgr->playSound(3);
}
if(event.key.keysym.sym == SDLK_4)
{
soundMgr->playSound(4);
}
if(event.key.keysym.sym == SDLK_9)
{
soundMgr->pauseSound();
}
if(event.key.keysym.sym == SDLK_0)
{
soundMgr->stopSound();
}
}
else if(event.type == SDL_QUIT)
{
quit =
true;
}
}
if(SDL_Flip(videoSurface) == -1)
{
return -1;
}
}
shutDown();
return 0;
}
bool
initSDL()
{
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
return false;
videoSurface = SDL_SetVideoMode(SCRN_WIDTH, SCRN_HEIGHT, SCRN_BPP,
SDL_SWSURFACE);
if(videoSurface == NULL)
return false;
if(!initTTF())
return false;
SDL_WM_SetCaption(
"Playing Sound", NULL);
return true;
}
bool
loadFiles()
{
background = loadBMP(
"background.bmp");
if(background == NULL)
return false;
if(!soundMgr->loadFile())
return false;
font = TTF_OpenFont(
"robotic.ttf", 24);
if(font == NULL)
return false;
return true;
}
bool
initTTF()
{
if(TTF_Init() == -1)
return false;
return true;
}
SDL_Surface* loadBMP(string filename)
{
SDL_Surface* tempSurface = NULL;
SDL_Surface* optimizedImage = NULL;
tempSurface = SDL_LoadBMP(filename.c_str());
if(tempSurface != NULL)
{
optimizedImage = SDL_DisplayFormat(tempSurface);
SDL_FreeSurface(tempSurface);
if(optimizedImage != NULL)
{
Uint32 color = SDL_MapRGB(optimizedImage->format,
0, 0xFF, 0xFF);
SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, color);
}
}
return optimizedImage;
}
void
applySurface(int x, int y, SDL_Surface* src, SDL_Surface* dest,
SDL_Rect* clip)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(src, clip, dest, &offset);
}
void
setText()
{
instruction1 = TTF_RenderText_Solid(font,
"Press 1, 2, 3, or 4 to play a sound effect", fontColor);
instruction2 = TTF_RenderText_Solid(font,
"Press 9 to play or pause the music", fontColor);
instruction3 = TTF_RenderText_Solid(font,
"Press 0 to stop the music", fontColor);
}
void
renderText()
{
applySurface((SCRN_WIDTH/2) - (instruction1->w/2),
(SCRN_HEIGHT/2)/2,
instruction1, videoSurface);
applySurface((SCRN_WIDTH/2) - (instruction2->w/2),
(SCRN_HEIGHT/2),
instruction2, videoSurface);
applySurface((SCRN_WIDTH/2) - (instruction2->w/2),
(SCRN_HEIGHT/2)/2 + (SCRN_HEIGHT/2),
instruction3, videoSurface);
}
void
shutDown()
{
SDL_FreeSurface(background);
SDL_FreeSurface(instruction1);
SDL_FreeSurface(instruction2);
SDL_FreeSurface(instruction3);
soundMgr->shutDown();
TTF_CloseFont(font);
TTF_Quit();
SDL_Quit();
}
It still comes out quite terrible even using the [ code ] brackets.