Hello all SDL users, Im new to this message group, yet not an absolute
beginner in SDL. I have no syntax errors from my compile, but i get an run
time error of my sdl window closing as soon as it opens up. I also get the
files stderr and stdout(which i know because for input and output)
#include
#include “SDL/SDL.h”
#include “SDL/SDL_image.h”
#include “SDL/SDL_mixer.h”
#define WIDTH 640
#define HEIGHT 480
#define BPP 32
SDL_Surface * Load_Image(std::string File)
{
SDL_Surface *screen;
SDL_Surface * Optimal;
screen = IMG_Load(File.c_str());
Optimal = SDL_DisplayFormat(screen);
SDL_FreeSurface(screen);
return(Optimal);
}
class player
{
Mix_Music *Song;
bool Music_paused;
bool Musicok;
SDL_Event event;
public:
void Musicplayer();
player();
~player();
};
player::player()
{
Mix_OpenAudio(22050,MIX_DEFAULT_FORMAT ,2,4096);
Music_paused= false;
Musicok= true;
Song = NULL;
}
player::~player()
{
Mix_CloseAudio();
Musicok=true;
Music_paused=false;
Mix_FreeMusic(Song);
Song = NULL;
}
void player::Musicplayer()
{
int quit= 0;
Uint8 *Keystate = SDL_GetKeyState(NULL);
while(quit != 1)
{
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_KEYDOWN :
if(Keystate[SDLK_1])
{ if(Musicok == true)
Song = Mix_LoadMUS("TEST.mp3");
else
{ Mix_FreeMusic(Song);
Song = Mix_LoadMUS("TEST.mp3");
}
Musicok = false;
}
if(Keystate[SDLK_2])
{ if(Musicok == true)
Song = Mix_LoadMUS("TEST2.mp3");
else
{ Mix_FreeMusic(Song);
Song = Mix_LoadMUS("TEST2.mp3");
}
Musicok = false;
}
if(Keystate[SDLK_SPACE])
{ if(Musicok == true)
Mix_PlayMusic(Song, -1);
else
Mix_RewindMusic();
}
if(Keystate[SDLK_RETURN])
{ if(Music_paused == false)
{Mix_PauseMusic();
Music_paused = true;}
else
Mix_ResumeMusic();
}
break;
case SDL_QUIT :
quit = 1;
break;
}
}
}
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface*
destination )
{
//Make a temporary rectangle to hold the offsets
SDL_Rect offset;
//Give the offsets to the rectangle
offset.x = x;
offset.y = y;
//Blit the surface
SDL_BlitSurface( source, NULL, destination, &offset );
}
int main( int argc, char* args[] )
{
SDL_Surface *screen;
SDL_Surface *image;
if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
return 1;
SDL_SetVideoMode(WIDTH,HEIGHT,BPP,SDL_HWSURFACE);
image = Load_Image(“sdl_test”);
apply_surface(0,0,image,screen);
player Test;
SDL_Event event;
int i =1;
while( i != 0)
{
while(SDL_PollEvent(&event))
{
Test.Musicplayer();
switch(event.type)
{
case SDL_QUIT :
i = 0;
break;
}
}
}
SDL_FreeSurface(screen);
SDL_FreeSurface(image);
SDL_Quit();
return 0;
}