Jan 5, 2013 at 4:50am UTC
I'm trying my first event driven program but it keeps hitting a breakpoint.
#include "SDL.h"
using namespace std;
int main( int argc, char* args[] )
{
SDL_Surface *home;
SDL_Surface *up;
SDL_Surface *down;
SDL_Surface *left;
SDL_Surface *right;
SDL_Surface *screen;
bool quit=false;
SDL_Init (SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode (1000, 1200, 32, SDL_SWSURFACE);
up = SDL_LoadBMP ("up.bmp");
down =SDL_LoadBMP ("down.bmp");
left= SDL_LoadBMP ("left.bmp");
right =SDL_LoadBMP ("right.bmp");
home =SDL_LoadBMP ("home.bmp");
SDL_Event event;
SDL_BlitSurface (home, NULL, screen, NULL);
SDL_Flip (screen);
while (quit ==false)
{
if (SDL_PollEvent(&event))
{
if (event.type==SDL_KEYDOWN)
{
switch (event.key.keysym.sym)
{
case SDLK_UP:
SDL_FreeSurface (home);
SDL_BlitSurface (screen, NULL, up, NULL);
SDL_Delay (2000);
SDL_FreeSurface (up);
case SDLK_DOWN:
SDL_FreeSurface (home);
SDL_BlitSurface (screen, NULL, down, NULL);
SDL_Delay (2000);
SDL_FreeSurface (up);
case SDLK_LEFT:
SDL_FreeSurface (home);
SDL_BlitSurface (screen, NULL, left, NULL);
SDL_Delay (2000);
SDL_FreeSurface (left);
case SDLK_RIGHT:
SDL_FreeSurface (home);
SDL_BlitSurface (screen, NULL, right, NULL);
SDL_Delay (2000);
SDL_FreeSurface (right);
}
}
if (event.type==SDL_QUIT)
{
quit= true;
}
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
}
}
return 0;
}
Jan 5, 2013 at 5:41am UTC
It keeps hitting a breakpoint? Then remove the breakpoint.
Jan 5, 2013 at 8:20am UTC
Don't free the surface after you blit them. Because when you try to blit them the second time, the surface no longer exists.