Jump to content
  • Advertisement
Sign in to follow this  
JonathanO'Brien

Issue with changing game states [SDL]

This topic is 2779 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Howdy, I have a programming error which won't allow my program to quit using the [x] button or change states when I click the screen. I need help changing my code so that states can change and the program can quit.

My main loop:


stateID = STATE_INTRO;

currentState = new Intro();

while(stateID != STATE_EXIT)
{
fps.start();

currentState->handle_events();

currentState->logic();

currentState->render();

if(SDL_Flip(screen) == -1)
{
return 1;
}

if(fps.get_ticks() < 1000 / FRAME_RATE)
{
SDL_Delay((1000 / FRAME_RATE) - fps.get_ticks());
}
}

clean_up();

SDL_QUIT;

return 0;
}


This opens my opening state STATE_INTRO, but for whatever reason, STATE_INTRO will not, on mouse click, change to STATE_INTRO_2. Below is the code for Intro::handle_events()


void Intro::handle_events()
{
//While there's events to handle
while( SDL_PollEvent( &event ) )
{
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
set_next_state( STATE_EXIT );
}
//If the user pressed enter
else if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_RETURN)
{
//Move to the title screen
set_next_state( STATE_INTRO_2 );
}
}
}
}


The functions used to change state are shown below:


int stateID = STATE_NULL;
int nextState = STATE_NULL;

void set_next_state( int newState )
{
//If the user doesn't want to exit
if( nextState != STATE_EXIT )
{
//Set the next state
nextState = newState;
}
}

void change_state()
{
//If the state needs to be changed
if( nextState != STATE_NULL )
{
//Delete the current state
if( nextState != STATE_EXIT )
{
delete currentState;
}

//Change the state
switch( nextState )
{
case STATE_INTRO:
currentState = new Intro();
break;

case STATE_INTRO_2:
currentState = new Intro2();
break;

case STATE_TITLE:
currentState = new Title();
break;

case STATE_LEVEL_1:
currentState = new Level1();
break;

/*case STATE_LEVEL_2:
currentState = new Level2();
break;

case STATE_LEVEL_3:
currentState = new Level3();
break;*/
}

//Change the current state ID
stateID = nextState;

//NULL the next state ID
nextState = STATE_NULL;
}
}


The program renders the desired background image fine as coded in Image::render(), but no events are processed.

I'm probably just overlooking something, but nonetheless, this website has been great for programming consultations.

Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!