Original Post
I tried to learn a little about the SDL_mixer library. Here's a small code in which you can play effects with 1,2,3,4. Music with 9, and pausing the Music also with 9. But the pause doesn't work, and I don't know why. Can you help me? I marked the critical block. Thanks Stromberg
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include "SDL/SDL_mixer.h"
#include <iostream>
#include <string>
using namespace std;
//Variables
const short SCREEN_WIDTH = 640;
const short SCREEN_HEIGHT = 480;
const short SCREEN_BPP = 32;
SDL_Surface *screen = NULL;
SDL_Surface *background = NULL;
SDL_Event event;
TTF_Font *font = NULL;
bool quit = false;
Mix_Music *music = NULL;
Mix_Chunk *scratch = NULL;
Mix_Chunk *high = NULL;
Mix_Chunk *med = NULL;
Mix_Chunk *low = NULL;
//Functions
bool Init();
bool loadFiles();
void cleanUp();
int main(int argc, char *args[])
{
if(Init() == false)
{
return 1;
}
if(loadFiles() == false)
{
return 2;
}
while(quit == false)
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
quit = true;
}
if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_1)
{
if(Mix_PlayChannel(-1, scratch, 0) == -1)
{
return 1;
}
}
else if(event.key.keysym.sym == SDLK_2)
{
if(Mix_PlayChannel(-1, high, 0) == -1)
{
return 1;
}
}
else if(event.key.keysym.sym == SDLK_3)
{
if(Mix_PlayChannel(-1, med, 0) == -1)
{
return 1;
}
}
else if(event.key.keysym.sym == SDLK_4)
{
if(Mix_PlayChannel(-1, low, 0) == -1)
{
return 1;
}
}
else if(event.key.keysym.sym == SDLK_9)
{
if(Mix_PlayingMusic() == 0)
{
if(Mix_PlayMusic(music, -1) == -1)
{
return 1;
}
}
else // HERE
{ // HERE
if(Mix_PausedMusic() == 1) // HERE
{ // HERE
Mix_ResumeMusic(); // HERE
} // HERE
else // HERE
{ // HERE
Mix_PauseMusic(); // HERE
} // HERE
} // HERE
}
else if(event.key.keysym.sym == SDLK_0)
{
Mix_HaltMusic();
}
}
}
}
cleanUp();
return 0;
}
bool Init()
{
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
return false;
}
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
if(screen == NULL)
{
return false;
}
if(TTF_Init() == -1)
{
return false;
}
if(Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096) == -1 )
{
return false;
}
return true;
}
bool loadFiles()
{
background = IMG_Load("back.bmp");
if(background == NULL)
{
return false;
}
font = TTF_OpenFont("augie.ttf",26);
if(font == NULL)
{
return false;
}
music = Mix_LoadMUS("starwars.mid");
if(music == NULL)
{
return false;
}
scratch = Mix_LoadWAV("beep.wav");
high = Mix_LoadWAV("beeplow.wav");
med = Mix_LoadWAV("Kling.wav");
low = Mix_LoadWAV("schuss3.wav");
if((scratch == NULL) || (high == NULL) || (med == NULL) || (low == NULL))
{
return false;
}
return true;
}
void cleanUp()
{
SDL_FreeSurface(background);
Mix_FreeChunk(scratch);
Mix_FreeChunk(high);
Mix_FreeChunk(med);
Mix_FreeChunk(low);
Mix_FreeMusic(music);
Mix_CloseAudio();
TTF_Quit();
SDL_Quit();
}