I'm having some issues with SDL_Event

Currently the screen opens and then shuts down,but when it did open if I
pressed any key it would shut down.Can anyone help me?
#include “SDL.h”

#include “stdio.h”
#include

bool k = true;
int main( int argc, char* argv[] )
{
atexit(SDL_Quit);
SDL_Init ( SDL_INIT_VIDEO );
SDL_Surface* screen;
screen = SDL_SetVideoMode(1024, 768, 32, SDL_DOUBLEBUF| SDL_ANYFORMAT);
SDL_Surface* hero;
SDL_Surface* enemy;
SDL_Surface* helaser;
SDL_Surface* enlaser;
hero = SDL_LoadBMP(“ship.bmp”);
enemy = SDL_LoadBMP(“enemyship.bmp”);
helaser = SDL_LoadBMP(“helaser.bmp”);
enlaser = SDL_LoadBMP(“enlaser.bmp”);
SDL_Rect ship;
ship.x = 500;
ship.y = 579;
SDL_Event global;
enum state { UP = 2,LEFT,RIGHT,DOWN,STOP };
state image;
image = STOP;
while (k == true)
{

SDL_BlitSurface(hero,NULL,screen,&ship);
if ( global.type == SDL_QUIT )
{
k = false;
SDL_Quit();
}
while(SDL_PollEvent(&global))
{
if ( global.type == SDL_KEYDOWN )
{
if ( global.key.keysym.sym == SDLK_ESCAPE )
{
k = false;
SDL_Quit();
}
}

    if ( global.key.keysym.sym == SDLK_DOWN ) 
	{ 
              image = UP;
	}
  
  if(image == UP)
  {
	ship.x += 1;

SDL_BlitSurface(hero,NULL,screen,&ship);
SDL_Flip(screen);
}
}
}

return (0);
}

this worked on my machine with “ship.bmp” in the same directory
i moved some stuff around

i think it does what you want

main problems were your tests for the SDL_Event global or as i renamed it
"event"
dunno, try it…

#include “SDL.h”
#include “stdio.h”
#include

int main( int argc, char* argv[] )
{
atexit(SDL_Quit);
SDL_Init( SDL_INIT_VIDEO );

SDL_Surface *screen;

screen = SDL_SetVideoMode( 800, 600, 32, SDL_DOUBLEBUF| SDL_ANYFORMAT );

SDL_Surface *hero,*enemy, *helaser, *enlaser;

hero = SDL_LoadBMP( “ship.bmp” );
/*
enemy = SDL_LoadBMP( “enemyship.bmp” );
helaser = SDL_LoadBMP( “helaser.bmp” );
enlaser = SDL_LoadBMP( “enlaser.bmp” );
*/
SDL_Rect ship;
ship.x = 300;
ship.y = 200;

enum State{UP = 2,LEFT,RIGHT, DOWN, STOP};

State image;
image = STOP;
SDL_Event event;
bool running = true; // this doesnt have to be global

while( running ){
SDL_FillRect( screen, NULL, 0 );
SDL_BlitSurface(hero,NULL,screen,&ship);

while(SDL_PollEvent(&event)){

if ( event.type == SDL_QUIT ){ // inside pollevent
running = false;
}

if( event.type == SDL_KEYDOWN ){

if( event.key.keysym.sym == SDLK_ESCAPE ){
running = false;
}
if( event.key.keysym.sym == SDLK_DOWN ){
image = UP;
}

}

}

if( image == UP ){
ship.x += 1;
}

SDL_Flip(screen);
}

SDL_Quit();
return 0;
}

I wouldn’t check for global’s .type until AFTER having called SDL_PollEvent.
No knowing what sort of garbage will be sitting in .type, which might be
causing your crash.
As a side note, it’s kind of weird to have your ship go sideways as a result
of pressing the down arrow(not to mention that the down arrow sets image to
UP!)
Also make sure that you actually have a valid surface stored where hero
points . If your bitmap loading failed(perhaps the file is missing…) then
it will segfault when you run it. Hope this helps!
-Dave> ----- Original Message -----

From: rspjk@yahoo.com (Randy)
To:
Sent: Thursday, September 01, 2005 10:05 AM
Subject: [SDL] I’m having some issues with SDL_Event.

Currently the screen opens and then shuts down,but when it did open if I
pressed any key it would shut down.Can anyone help me?
#include “SDL.h”

#include “stdio.h”
#include

bool k = true;
int main( int argc, char* argv[] )
{
atexit(SDL_Quit);
SDL_Init ( SDL_INIT_VIDEO );
SDL_Surface* screen;
screen = SDL_SetVideoMode(1024, 768, 32, SDL_DOUBLEBUF| SDL_ANYFORMAT);
SDL_Surface* hero;
SDL_Surface* enemy;
SDL_Surface* helaser;
SDL_Surface* enlaser;
hero = SDL_LoadBMP(“ship.bmp”);
enemy = SDL_LoadBMP(“enemyship.bmp”);
helaser = SDL_LoadBMP(“helaser.bmp”);
enlaser = SDL_LoadBMP(“enlaser.bmp”);
SDL_Rect ship;
ship.x = 500;
ship.y = 579;
SDL_Event global;
enum state { UP = 2,LEFT,RIGHT,DOWN,STOP };
state image;
image = STOP;
while (k == true)
{

SDL_BlitSurface(hero,NULL,screen,&ship);
if ( global.type == SDL_QUIT )
{
k = false;
SDL_Quit();
}
while(SDL_PollEvent(&global))
{
if ( global.type == SDL_KEYDOWN )
{
if ( global.key.keysym.sym == SDLK_ESCAPE )
{
k = false;
SDL_Quit();
}
}

   if ( global.key.keysym.sym == SDLK_DOWN )

{
image = UP;
}

if(image == UP)
{
ship.x += 1;
SDL_BlitSurface(hero,NULL,screen,&ship);
SDL_Flip(screen);
}
}
}

return (0);
}


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl