App bugfix: event handler can change while handling events

This commit is contained in:
Nikita Lisitsa 2021-02-19 10:43:07 +03:00
parent db2fb3095d
commit 0c45409dfe

View file

@ -125,7 +125,7 @@ namespace psemek::app
void app::poll_events() void app::poll_events()
{ {
auto handler = impl().get_scene(); auto handler = [this]{ return impl().get_scene(); };
for (SDL_Event e; SDL_PollEvent(&e);) switch (e.type) for (SDL_Event e; SDL_PollEvent(&e);) switch (e.type)
{ {
@ -136,33 +136,33 @@ namespace psemek::app
{ {
case SDL_WINDOWEVENT_RESIZED: case SDL_WINDOWEVENT_RESIZED:
impl().had_initial_resize = true; impl().had_initial_resize = true;
handler->on_resize(e.window.data1, e.window.data2); handler()->on_resize(e.window.data1, e.window.data2);
break; break;
case SDL_WINDOWEVENT_FOCUS_GAINED: case SDL_WINDOWEVENT_FOCUS_GAINED:
handler->on_focus_gained(); handler()->on_focus_gained();
break; break;
case SDL_WINDOWEVENT_FOCUS_LOST: case SDL_WINDOWEVENT_FOCUS_LOST:
handler->on_focus_lost(); handler()->on_focus_lost();
break; break;
} }
break; break;
case SDL_MOUSEMOTION: case SDL_MOUSEMOTION:
handler->on_mouse_move(e.motion.x, e.motion.y, e.motion.xrel, e.motion.yrel); handler()->on_mouse_move(e.motion.x, e.motion.y, e.motion.xrel, e.motion.yrel);
break; break;
case SDL_MOUSEWHEEL: case SDL_MOUSEWHEEL:
handler->on_mouse_wheel(e.wheel.y); handler()->on_mouse_wheel(e.wheel.y);
break; break;
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONDOWN:
switch (e.button.button) switch (e.button.button)
{ {
case SDL_BUTTON_LEFT: case SDL_BUTTON_LEFT:
handler->on_left_button_down(); handler()->on_left_button_down();
break; break;
case SDL_BUTTON_MIDDLE: case SDL_BUTTON_MIDDLE:
handler->on_middle_button_down(); handler()->on_middle_button_down();
break; break;
case SDL_BUTTON_RIGHT: case SDL_BUTTON_RIGHT:
handler->on_right_button_down(); handler()->on_right_button_down();
break; break;
} }
break; break;
@ -170,21 +170,21 @@ namespace psemek::app
switch (e.button.button) switch (e.button.button)
{ {
case SDL_BUTTON_LEFT: case SDL_BUTTON_LEFT:
handler->on_left_button_up(); handler()->on_left_button_up();
break; break;
case SDL_BUTTON_MIDDLE: case SDL_BUTTON_MIDDLE:
handler->on_middle_button_up(); handler()->on_middle_button_up();
break; break;
case SDL_BUTTON_RIGHT: case SDL_BUTTON_RIGHT:
handler->on_right_button_up(); handler()->on_right_button_up();
break; break;
} }
break; break;
case SDL_KEYDOWN: case SDL_KEYDOWN:
handler->on_key_down(e.key.keysym.sym); handler()->on_key_down(e.key.keysym.sym);
break; break;
case SDL_KEYUP: case SDL_KEYUP:
handler->on_key_up(e.key.keysym.sym); handler()->on_key_up(e.key.keysym.sym);
break; break;
} }
} }