c++ - SDL Mouse Click -
so, i'm working on options menu game, have button when pressed changes it's text next resolution in array, user presses button change resolution next string in array.
my problem getting click event.
right now, when user presses button, returns true while mouse down, instead of when mouse pressed. want return true in mouse event when mouse pressed.
i've looked around, , i've found seems similar i've done or, said, returning true while mouse down, instead of initial click.
my events handled in eventmanager singleton, , here functions see necessary:
my update function, event polled, worth noting i'm using private sdl_event named "e".
void eventmanager::update(){ while(sdl_pollevent(&e)){ sdl_getmousestate(&mousex, &mousey); switch(e.type){ case sdl_quit: running = false; } } }
my mousepress function, want mouse press returned.
int eventmanager::mousepress(){ if(e.type == sdl_mousebuttondown){ return e.button.button; } return 0; }
instead of using sdl_getmousestate()
, gets actual state of mouse (thats name comes ;) ), use event polling. sdl should give sdl_mousebuttonevent
contains informations need , should queued once.
see https://wiki.libsdl.org/sdl_mousebuttonevent
edit clarify mean:
you use this:
void eventmanager::update(){ sdl_event e; while(sdl_pollevent(&e)){ switch(e.type){ case sdl_quit: running = false; break; case sdl_mousebuttondown: //do whatever want after mouse button pressed, // e.g.: mousepress(e.button); break; } } }
inside mousepress-function can test, of mouse buttons has been pressed:
void eventmanager::mousepress(sdl_mousebuttonevent& b){ if(b.button == sdl_button_left){ //handle left-click } }
this works, because sdl_pollevent return once every event. if theres no new event, return empty event. 1 click = 1 times sdl_pollevent() e being of type sdl_mousebuttondown afterwards , 1 times sdl_pollevent() e being of type sdl_mousebuttonup afterwards. if call sdl_pollevent() in between or afterwards, return 0 , leave e empty event, not calling switch
@ all. if respond mousebuttondown or mousebuttonup or both you...
i've declared sdl_event local variable update()
. why? idea behind event is, theres event-object whenever event has occured. react event , forget it. theres no need have global variable. if want prevent constant construction/destruction, can declare static. thats hint, not related original question.
Comments
Post a Comment