Jump to content

  • Log In with Google      Sign In   
  • Create Account

14 years ago on June 15th Gamedev.net was first launched! We want to thank all of you for being part of our community and hope the best years are ahead of us. Happy birthday Gamedev.net!

#Actualspooderw

Posted 02 April 2012 - 09:01 PM

if(SDL_PollEvent(&event)){

   if(event.type == SDL_QUIT){running = false; SDL_Quit(); break;}
   if(event.type == SDL_KEYDOWN){
		if(event.key.keysym.sym == SDLK_ESCAPE){running = false; SDL_Quit(); break;}
   }
   if(ExitButton.CheckEvents()){
		running = false;
		SDL_Quit();
		break;
   }


If I am correct, this should be contained in a while loop, because if more than one event occurs in a single iteration of your game loop, you will only catch one of the events, which will not only result in missed input, but I believe it leaks memory and causes major slowdown.


Also, you should not have another event handler inside your button, you should pass in a bool that checks if the mouse is pressed.

It should be something like this:

main.cpp:
bool leftClick = false;
while(SDL_PollEvent(&event)){

   if(event.type == SDL_QUIT){running = false; SDL_Quit(); break;}
   if(event.type == SDL_KEYDOWN){
		if(event.key.keysym.sym == SDLK_ESCAPE){running = false; SDL_Quit(); break;}
	if(event.type == SDL_MOUSEBUTTONDOWN){
		 if(event.button.button == SDL_BUTTON_LEFT){
		  leftClick = true;}
	  if(event.type == SDL_MOUSEBUTTONUP){
		 if(event.button.button == SDL_BUTTON_LEFT){
		  leftClick = false;}
}
   if(ExitButton.CheckEvents(leftClick)){
		running = false;
		SDL_Quit();
		break;
   }


newbutton.cpp:
bool NewButton::CheckEvents(bool leftMousePressed){
	 if(leftMousePressed)
	 {
		if((MouseX > ButtonRect.x) && (MouseY > ButtonRect.y) && (MouseX < (ButtonRect.x + ButtonRect.w)) && (MouseY < (ButtonRect.y + ButtonRect.h))){
		 return true;
		}
	 }
   }
  }
}

#1spooderw

Posted 02 April 2012 - 09:00 PM

if(SDL_PollEvent(&event)){
 
   if(event.type == SDL_QUIT){running = false; SDL_Quit(); break;}
   if(event.type == SDL_KEYDOWN){
        if(event.key.keysym.sym == SDLK_ESCAPE){running = false; SDL_Quit(); break;}
   }
   if(ExitButton.CheckEvents()){
        running = false;
        SDL_Quit();
        break;
   }


If I am correct, this should be contained in a while loop, because if more than one event occurs in a single iteration of your game loop, you will only catch one of the events, which will not only result in missed input, but I believe it leaks memory and causes major slowdown.


Also, you should not have another event handler inside your button, you should pass in a bool that checks if the mouse is pressed.

It should be something like this:

main.cpp:
bool leftClick = false;
while(SDL_PollEvent(&event)){
 
   if(event.type == SDL_QUIT){running = false; SDL_Quit(); break;}
   if(event.type == SDL_KEYDOWN){
        if(event.key.keysym.sym == SDLK_ESCAPE){running = false; SDL_Quit(); break;}
    if(event.type == SDL_MOUSEBUTTONDOWN){
         if(event.button.button == SDL_BUTTON_LEFT){
          leftClick = true;
      if(event.type == SDL_MOUSEBUTTONUP){
         if(event.button.button == SDL_BUTTON_LEFT){
          leftClick = false;
 }
   if(ExitButton.CheckEvents(leftClick)){
        running = false;
        SDL_Quit();
        break;
   }


newbutton.cpp:
bool NewButton::CheckEvents(bool leftMousePressed){
     if(leftMousePressed)
	 {
	    if((MouseX > ButtonRect.x) && (MouseY > ButtonRect.y) && (MouseX < (ButtonRect.x + ButtonRect.w)) && (MouseY < (ButtonRect.y + ButtonRect.h))){
		 return true;
	    }
	 }
   }
  }
}

PARTNERS