The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
SDL_Keycode, mouse ? C++
macrofeet


Joined: 29 Apr 2012
Posts: 20
Code:


   SDL_Keycode GetUserInput;

   if (event.type == SDL_KEYDOWN) GetUserInput = event.key.keysym.sym;
   else if (event.type == SDL_MOUSEBUTTONDOWN) GetUserInput = event.button.button;
   else GetUserInput = NULL;

   if  (GetUserInput == SDLK_F1) {//do something here} //f1
   else if  (GetUserInput == SDLK_F2){{//do something here} //f2
   else if  (GetUserInput == SDLK_F3){{//do something here} //f3
   else if  (GetUserInput == SDL_BUTTON_LEFT) {{//do something here} // left mouse  click

 


Questions

The code above works as expected will it cause any unforeseen problems using SDL_Keycode to store event.button.button

or should it really be

if (event.button.button == SDL_BUTTON_LEFT)
AntTheAlchemist


Joined: 13 Feb 2015
Posts: 60
Location: UK
Technically it works, but it's purely by chance that it does work.

This is very coding bad practice, specifically because SDL_BUTTON_LEFT could have the same value as SDLK_F2 and result in your else ifs not working.

SDL_BUTTON_X1 and SDL_SCANCODE_A both have the value of 4. So, using your code logic, if you were to press one of the extra mouse buttons, your code would think it was the A key on the keyboard. These are the kind of bugs that are a headache to locate.
macrofeet


Joined: 29 Apr 2012
Posts: 20
Thanks for the reply AntTheAlchemist, I expected this would be the case any thing like SDL_Keycode for both ?
AntTheAlchemist


Joined: 13 Feb 2015
Posts: 60
Location: UK
No, they can't be mixed. Store and process the inputs separately is the only way to do it.

Are you wanting to be clean or efficient by merging the checks into one GetUserInput variable? It's a bad idea, trust me. Keep them separate.

Just in case you didn't already know, look at using the switch / case statements instead of stacking if / else. The compiler builds up an efficient jump table for long case lists.
macrofeet


Joined: 29 Apr 2012
Posts: 20
Quote:
Are you wanting to be clean or efficient by merging the checks into one GetUserInput variable? It's a bad idea, trust me. Keep them separate.


Yea above was exactly what I was trying to accomplish, decided to ditch the mouse as it was only duplicating key presses, started of with a working case statement, currently combining keys for Game menu & get keys for main game, case statement gave me brain ache working out logic.

Code:


GameMenu()
{
if (SDL_PollEvent(&User)) GetUserInput();
// etc
}

GameMain()
{
if (SDL_PollEvent(&User)) GetUserInput();
//etc
}

GetUserInput()
{
if (User.type == SDL_KEYDOWN) // if key pressed
  {
    KeyPressed=User.key.keysym.sym; // get the key pressed
    if  (KeyPressed == SDLK_ESCAPE)  // Quit
    {
      Player.Hit_Points = 0;
      GameEnding.IsAlive=false;
    }
    else if  (KeyPressed == SDLK_F1)  SDL_ToggleFS(screen, 800, 600); // window

    if (MenuRun) // game menu controls
    {
      if ((KeyPressed == SDLK_SPACE) || (KeyPressed == SDLK_RETURN))
      {
        MenuRun=0; //exit menu
        GoGame(); // Do Main loop
      }
      else if (KeyPressed == SDLK_ESCAPE) MenuRun=0;
    }

    if  (Player.Hit_Points >0) // Main Game Player Controls allowed if Alive
    {
      if  (KeyPressed ==  SDLK_LEFT) Player.SetMovement('L');
      else if  (KeyPressed == SDLK_RIGHT) Player.SetMovement('R');
      else if  (KeyPressed == SDLK_DOWN) Player.SetMovement('B');
      else if  (KeyPressed == SDLK_UP) Player.SetMovement('T');
      else if  (KeyPressed == SDLK_SPACE) Fire();
    }
  }
  else if (User.type == SDL_KEYUP) Player.SetMovement('N');
  else if (User.type == SDL_QUIT) Player.Hit_Points = 0;
  if (User.type == SDL_WINDOWEVENT)
  {
    if  (User.window.event == SDL_WINDOWEVENT_RESIZED) Winresize(User.window.data1,User.window.data2);
  }
}