-
SDL run time error
Code:
#include<string>
#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;
SDL_Init(SDL_INIT_EVERYTHING);
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;
}
well my program when run, blinks off and on
-
Add SDL_DOUBLEBUF to SDL_SetVideoMode(), move the call to apply_surface() after the switch and then add a call to SDL_Flip(screen).
-
Code:
#include<string>
#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;
SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode(WIDTH,HEIGHT,BPP,SDL_DOUBLEBUF);
image = Load_Image("sdl_test");
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;
}
}
}
apply_surface(0,0,image,screen);
SDL_Flip(screen);
SDL_FreeSurface(screen);
SDL_FreeSurface(image);
SDL_Quit();
return 0;
}
this is what i alternated soo far
-
You need to put the rendering code (apply_surface() and SDL_Flip() ) in the message loop (i.e. the outer while loop) ..
-
i know in the first loop (outer) but ssame result
-
Are you sure your code isnt inside of Test.Musicplayer(), and therefore not rendering? Repost what has changed... (use a debugger to see if it ever passes Musicplayer() when you think it should be rendering...)
-
Code:
#include<string>
#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 *screen;
SDL_Surface *image;
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 );
}
SDL_Surface * Load_Image(std::string File)
{
SDL_Surface *Picture;
SDL_Surface * Optimal;
Picture = IMG_Load(File.c_str());
Optimal = SDL_DisplayFormat(Picture);
SDL_FreeSurface(Picture);
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);
apply_surface(0,0,image,screen);
SDL_Flip( screen );
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;
}
}
}
}
int main( int argc, char* args[] )
{
SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode(WIDTH,HEIGHT,BPP,SDL_DOUBLEBUF);
image = Load_Image("sdl_test");
player Test;
SDL_Event event;
int i =1;
Test.Musicplayer();
while( i != 0)
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
i=0;
}
}
SDL_FreeSurface(screen);
SDL_FreeSurface(image);
SDL_Quit();
return 0;
}
-
-
Re-read cactus_hugger's last post. The answer is just so obvious..
-
First off, try to indent your code a bit better. Spending an extra second a line will save you 5 seconds a line when you run into problems like this.
Code:
image = Load_Image("sdl_test");
What is the name of your image file name? Probably that should be changed to "sdl_test.bmp", no?
Also, attempting
Code:
SDL_FreeSurface(screen);
. . . will crash some systems (Namely mine . . .)
Code:
while( i != 0)
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
i=0;
}
What is the purpose of this loop?
-
thanks and the other loop XD i put it their for main and yeaa stupid me for the image.....