🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

EVENT handling

Started by
5 comments, last by Shaarigan 5 years, 3 months ago

i am confused what i am doing is right or wrong?

currently storing the state of the each key and checking it in the game loop like this 

eHandler->getKey(EVENT_KEYS::KEY_LEFT_CTRL);

if it returns true than do something.

//this function is called by the function, which is passed as callback to the glfw library 
void eventHandler::keyBoardHandler(int key , int scancode, int action, int mods)
 {
    if(action!=GLFW_KEY_UNKNOWN)
    switch (key)
     {

        case GLFW_KEY_A:
                {
                  if(action==GLFW_PRESS||action==GLFW_REPEAT)
                   {
                        my_keys.KEY_A=true;
                        if(currentCamera->TP_PERSPECTIVE)
                        character_controller->moveLeft(currentCamera);
                   }
                  break;
                }
        case GLFW_KEY_B:
                {
                  if(action==GLFW_PRESS)
                   {
                        my_keys.KEY_B=true;
                   }
                  break;
                }
        case GLFW_KEY_C:
                {
                  if(action==GLFW_PRESS)
                   {
                        my_keys.KEY_C=true;
                   }
                  break;
                }
        case GLFW_KEY_D:
                {
                  if(action==GLFW_PRESS||action==GLFW_REPEAT)
                   {
                        my_keys.KEY_D=true;
                        if(currentCamera->TP_PERSPECTIVE)
                        character_controller->moveRight(currentCamera);
                   }
                  break;
                }
        case GLFW_KEY_E:
                {
                  if(action==GLFW_PRESS)
                   {
                        my_keys.KEY_E=true;
                   }
                  break;
                }
        case GLFW_KEY_F:
                {
                  if(action==GLFW_PRESS||action==GLFW_REPEAT)
                   {
                        my_keys.KEY_F=true;
                   }
                  break;
                }
        case GLFW_KEY_G:
                {
                  if(action==GLFW_PRESS)
                   {
                        my_keys.KEY_G=true;
                   }
                  break;
                }
        case GLFW_KEY_H:
                {
                  if(action==GLFW_PRESS)
                   {
                        my_keys.KEY_H=true;
                   }
                  break;
                }
        case GLFW_KEY_I:
                {
                  if(action==GLFW_PRESS)
                   {
                        my_keys.KEY_I=true;
                   }
                  break;
                }
        case GLFW_KEY_J:
                {
                  if(action==GLFW_PRESS)
                   {
                        my_keys.KEY_J=true;
                   }
                  break;
                }
        case GLFW_KEY_K:
                {
                  if(action==GLFW_PRESS)
                   {
                        my_keys.KEY_K=true;
                   }
                  break;
                }
        case GLFW_KEY_L:
                {
                  if(action==GLFW_PRESS)
                   {
                        my_keys.KEY_L=true;
                   }
                  break;
                }
        case GLFW_KEY_M:
                {
                  if(action==GLFW_PRESS)
                   {
                        my_keys.KEY_M=true;
                   }
                  break;
                }
        case GLFW_KEY_N:
                {
                  if(action==GLFW_PRESS)
                   {
                        my_keys.KEY_N=true;
                   }
                  break;
                }
        case GLFW_KEY_O:
                {
                  if(action==GLFW_PRESS)
                   {
                        my_keys.KEY_O=true;
                   }
                  break;
                }
        case GLFW_KEY_P:
                {
                  if(action==GLFW_PRESS)
                   {
                        my_keys.KEY_P=true;
                   }
                  break;
                }
        case GLFW_KEY_Q:
                {
                  if(action==GLFW_PRESS)
                   {
                        my_keys.KEY_Q=true;
                   }
                  break;
                }
        case GLFW_KEY_R:
                {
                  if(action==GLFW_PRESS||action==GLFW_REPEAT)
                   {
                        my_keys.KEY_R=true;
                   }
                  break;
                }
        case GLFW_KEY_S:
                {
                  if(action==GLFW_PRESS||action==GLFW_REPEAT)
                   {
                        my_keys.KEY_S=true;
                        if(currentCamera->TP_PERSPECTIVE)
                        character_controller->moveBackward(currentCamera);
                   }
                  break;
                }
        case GLFW_KEY_T:
                {
                  if(action==GLFW_PRESS)
                   {
                        my_keys.KEY_T=true;
                   }
                  break;
                }
        case GLFW_KEY_U:
                {
                  if(action==GLFW_PRESS)
                   {
                        my_keys.KEY_U=true;
                   }
                  break;
                }
        case GLFW_KEY_V:
                {
                  if(action==GLFW_PRESS)
                   {
                        my_keys.KEY_V=true;
                   }
                  break;
                }
        case GLFW_KEY_W:
                {
                  if(action==GLFW_PRESS||action==GLFW_REPEAT)
                   {
                        my_keys.KEY_W=true;
                        if(currentCamera->TP_PERSPECTIVE)
                        character_controller->moveForward(currentCamera);
                   }
                  break;
                }
        case GLFW_KEY_X:
                {
                  if(action==GLFW_PRESS)
                   {
                        my_keys.KEY_X=true;
                   }
                  break;
                }
        case GLFW_KEY_Y:
                {
                  if(action==GLFW_PRESS)
                   {
                        my_keys.KEY_Y=true;
                   }
                  break;
                }
        case GLFW_KEY_Z:
                {
                  if(action==GLFW_PRESS)
                   {
                        my_keys.KEY_Z=true;
                   }
                  break;
                }
        case GLFW_KEY_UP :
                {
                  my_keys.KEY_UP = true;
                  currentCamera->panUp();
                  break;
                }
        case GLFW_KEY_DOWN :
                {
                  my_keys.KEY_DOWN = true;
                  currentCamera->panDown();
                  break;
                }
        case GLFW_KEY_LEFT :
                {
                  my_keys.KEY_LEFT = true;
                  currentCamera->panLeft();
                  break;
                }
        case GLFW_KEY_RIGHT :
                {
                  my_keys.KEY_RIGHT = true;
                  currentCamera->panRight();
                  break;
                }
        case GLFW_KEY_ESCAPE:
                {
                  my_keys.KEY_ESCAPE = true;
                  glfwSetWindowShouldClose(window, GLFW_TRUE);
                  break;
                }
        case GLFW_KEY_SPACE:
                {
                  if(action==GLFW_PRESS||action==GLFW_REPEAT)
                   {
                     my_keys.KEY_SPACE = true;
                     if(currentCamera->TP_PERSPECTIVE)
                     character_controller->jump(currentCamera);
                   }
                  break;
                }
        case GLFW_KEY_ENTER:
                {
                  if(action==GLFW_PRESS)
                   {
                     my_keys.KEY_ENTER = true;
                   }
                  break;
                }
        case GLFW_KEY_TAB:
                {
                  if(action==GLFW_PRESS)
                   {
                     my_keys.KEY_TAB = true;
                   }
                  break;
                }
        case GLFW_KEY_LEFT_CONTROL:
                {
                  if(action==GLFW_PRESS)
                   {
                     my_keys.KEY_LEFT_CTRL = true;
                   }
                  break;
                }
        default:{}
     }
 }

sorry that i have pasted this thing here but this is what i am really doing.

i have done this like above because i don't want to pass other pointers to event handler class example

camera,object_manger,game_world_physics and that all

so my question is 

what is the correct way of doing event handling? what you people do for optimization?

????????????

Advertisement

Why dont you just set the glfw window user pointer to the address of your selfmade event dispatcher object?

This object should not know about all your game's systems though, only convert key presses to actions your game has to perform in response to them.

will you please elaborate things.

6 minutes ago, wintertime said:

Why dont you just set the glfw window user pointer to the address of your selfmade event dispatcher object?

i don't know how to do this.

thank you.

a little bit of code will be more helpfull

https://www.glfw.org/docs/latest/window_guide.html#window_events

- create event dispatcher

- set user pointer

- in callback get back the user pointer, cast it back to the right type

- call the corresponding method in your dispatcher

- translate (for example, space pressed -> action for firing weapons)

- handle the actions you created

 

Ok thank you very much, it will be lot more helpful from what i have been doing.

In more complex games or those running in multithreaded environment, you are keeping a key-to-action table of events that are subscribed to your event system. This is what you see in those key bindings menus when navigating to the settings in any game that allows of reconfiguration of the key-to-action table.

Those actions best are other events to fire even because your message handling code lifes in parallel to your game loop. Those events are then received by your game modules like the PlayerController and should enqueue an update into your game loop so the PC can then handle any adaptions to be made on the game in the next frame

This topic is closed to new replies.

Advertisement