Introduce app::scene & support changing scenes

This commit is contained in:
Nikita Lisitsa 2020-08-30 10:14:35 +03:00
parent 91de56753b
commit da8d9550a7
3 changed files with 72 additions and 62 deletions

View file

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <psemek/app/scene.hpp>
#include <SDL2/SDL_keycode.h> #include <SDL2/SDL_keycode.h>
#include <memory> #include <memory>
@ -8,38 +10,24 @@ namespace psemek::app
{ {
struct app struct app
: scene
{ {
app(std::string const & name); app(std::string const & name);
virtual ~app(); ~app() override;
virtual bool running() const; virtual bool running() const;
virtual void on_resize(int width, int height); void on_resize(int width, int height) override;
virtual void on_focus_gained(); void on_quit() override;
virtual void on_focus_lost();
virtual void on_quit();
virtual void on_mouse_move(int x, int y); void draw() override;
virtual void on_mouse_wheel(int delta);
virtual void on_left_button_down();
virtual void on_left_button_up();
virtual void on_middle_button_down();
virtual void on_middle_button_up();
virtual void on_right_button_down();
virtual void on_right_button_up();
virtual void on_key_down(SDL_Keycode key);
virtual void on_key_up(SDL_Keycode key);
void poll_events(); void poll_events();
virtual void update();
virtual void draw();
void run(); void run();
void show_cursor(bool show); scene * set_scene(scene * s);
void show_cursor(bool show);
float time() const; float time() const;
private: private:

View file

@ -0,0 +1,35 @@
#pragma once
#include <SDL2/SDL_keycode.h>
namespace psemek::app
{
struct scene
{
virtual ~scene() = 0;
virtual void on_resize(int width, int height) {}
virtual void on_focus_gained() {}
virtual void on_focus_lost() {}
virtual void on_quit() {}
virtual void on_mouse_move(int x, int y) {}
virtual void on_mouse_wheel(int delta) {}
virtual void on_left_button_down() {}
virtual void on_left_button_up() {}
virtual void on_middle_button_down() {}
virtual void on_middle_button_up() {}
virtual void on_right_button_down() {}
virtual void on_right_button_up() {}
virtual void on_key_down(SDL_Keycode key) {}
virtual void on_key_up(SDL_Keycode key) {}
virtual void update() {}
virtual void draw() {}
};
inline scene::~scene() = default;
}

View file

@ -39,6 +39,8 @@ namespace psemek::app
SDL_Window * window = nullptr; SDL_Window * window = nullptr;
SDL_GLContext gl_context = nullptr; SDL_GLContext gl_context = nullptr;
scene * current_scene = nullptr;
bool running = false; bool running = false;
bool had_initial_resize = false; bool had_initial_resize = false;
@ -97,73 +99,53 @@ namespace psemek::app
gl::Viewport(0, 0, width, height); gl::Viewport(0, 0, width, height);
} }
void app::on_focus_gained() {}
void app::on_focus_lost() {}
void app::on_quit() void app::on_quit()
{ {
impl().running = false; impl().running = false;
} }
void app::on_mouse_move(int, int) {}
void app::on_mouse_wheel(int) {}
void app::on_left_button_down() {}
void app::on_left_button_up() {}
void app::on_middle_button_down() {}
void app::on_middle_button_up() {}
void app::on_right_button_down() {}
void app::on_right_button_up() {}
void app::on_key_down(SDL_Keycode) {}
void app::on_key_up(SDL_Keycode) {}
void app::poll_events() void app::poll_events()
{ {
scene * handler = this;
if (impl().current_scene)
handler = impl().current_scene;
for (SDL_Event e; SDL_PollEvent(&e);) switch (e.type) for (SDL_Event e; SDL_PollEvent(&e);) switch (e.type)
{ {
case SDL_QUIT: case SDL_QUIT:
on_quit(); handler->on_quit();
break; break;
case SDL_WINDOWEVENT: switch (e.window.event) case SDL_WINDOWEVENT: switch (e.window.event)
{ {
case SDL_WINDOWEVENT_RESIZED: case SDL_WINDOWEVENT_RESIZED:
impl().had_initial_resize = true; impl().had_initial_resize = true;
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:
on_focus_gained(); handler->on_focus_gained();
break; break;
case SDL_WINDOWEVENT_FOCUS_LOST: case SDL_WINDOWEVENT_FOCUS_LOST:
on_focus_lost(); handler->on_focus_lost();
break; break;
} }
break; break;
case SDL_MOUSEMOTION: case SDL_MOUSEMOTION:
on_mouse_move(e.motion.x, e.motion.y); handler->on_mouse_move(e.motion.x, e.motion.y);
break; break;
case SDL_MOUSEWHEEL: case SDL_MOUSEWHEEL:
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:
on_left_button_down(); handler->on_left_button_down();
break; break;
case SDL_BUTTON_MIDDLE: case SDL_BUTTON_MIDDLE:
on_middle_button_down(); handler->on_middle_button_down();
break; break;
case SDL_BUTTON_RIGHT: case SDL_BUTTON_RIGHT:
on_right_button_down(); handler->on_right_button_down();
break; break;
} }
break; break;
@ -171,28 +153,25 @@ namespace psemek::app
switch (e.button.button) switch (e.button.button)
{ {
case SDL_BUTTON_LEFT: case SDL_BUTTON_LEFT:
on_left_button_up(); handler->on_left_button_up();
break; break;
case SDL_BUTTON_MIDDLE: case SDL_BUTTON_MIDDLE:
on_middle_button_up(); handler->on_middle_button_up();
break; break;
case SDL_BUTTON_RIGHT: case SDL_BUTTON_RIGHT:
on_right_button_up(); handler->on_right_button_up();
break; break;
} }
break; break;
case SDL_KEYDOWN: case SDL_KEYDOWN:
on_key_down(e.key.keysym.sym); handler->on_key_down(e.key.keysym.sym);
break; break;
case SDL_KEYUP: case SDL_KEYUP:
on_key_up(e.key.keysym.sym); handler->on_key_up(e.key.keysym.sym);
break; break;
} }
} }
void app::update()
{}
void app::draw() void app::draw()
{ {
gl::ClearColor(0.7f, 0.7f, 1.f, 1.f); gl::ClearColor(0.7f, 0.7f, 1.f, 1.f);
@ -220,6 +199,14 @@ namespace psemek::app
} }
} }
scene * app::set_scene(scene * s)
{
impl().had_initial_resize = false;
std::swap(s, impl().current_scene);
return s;
}
void app::show_cursor(bool show) void app::show_cursor(bool show)
{ {
SDL_ShowCursor(show ? SDL_TRUE : SDL_FALSE); SDL_ShowCursor(show ? SDL_TRUE : SDL_FALSE);