1 #include "Application.hpp"
6 #include "../graphics/Viewport.hpp"
14 Application::Application(Window &win, graphics::Viewport &vp)
20 Application::~Application() {
24 void Application::PushState(State *s) {
26 if (!states.empty()) {
27 states.top()->OnPause();
31 if (s->ref_count == 1) {
37 State *Application::PopState() {
38 State *s = states.top();
42 if (!states.empty()) {
43 states.top()->OnResume();
48 State *Application::SwitchState(State *s_new) {
50 State *s_old = states.top();
55 if (s_old->ref_count == 0) {
58 if (s_new->ref_count == 1) {
65 State &Application::GetState() {
69 bool Application::HasState() const noexcept {
70 return !states.empty();
74 void Application::Run() {
75 Uint32 last = SDL_GetTicks();
77 Uint32 now = SDL_GetTicks();
78 int delta = now - last;
84 void Application::Loop(int dt) {
86 if (!HasState()) return;
87 GetState().Update(dt);
88 if (!HasState()) return;
90 GetState().Render(viewport);
94 void Application::HandleEvents() {
96 while (HasState() && SDL_PollEvent(&event)) {
97 if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_RESIZED) {
98 viewport.Resize(event.window.data1, event.window.data2);
100 GetState().Handle(event);
104 void State::Handle(const SDL_Event &event) {
105 switch (event.type) {
107 OnKeyDown(event.key);
112 case SDL_MOUSEBUTTONDOWN:
113 OnMouseDown(event.button);
115 case SDL_MOUSEBUTTONUP:
116 OnMouseUp(event.button);
118 case SDL_MOUSEMOTION:
119 OnMouseMotion(event.motion);
122 OnMouseWheel(event.wheel);
127 case SDL_WINDOWEVENT:
128 Handle(event.window);
136 void State::Handle(const SDL_WindowEvent &event) {
137 switch (event.event) {
138 case SDL_WINDOWEVENT_FOCUS_GAINED:
141 case SDL_WINDOWEVENT_FOCUS_LOST:
144 case SDL_WINDOWEVENT_RESIZED:
145 OnResize(event.data1, event.data2);
152 void State::Update(int dt) {
156 void State::Render(graphics::Viewport &viewport) {
160 void State::OnQuit() {
161 while (App().HasState()) {
168 graphics::Format format;
169 textures.tiles.Bind();
170 textures.tiles.Reserve(1, 1, 3, format);
171 std::uint8_t texdata[] = {
172 0xFF, 0x00, 0x00, 0xFF,
173 0x00, 0xFF, 0x00, 0xFF,
174 0x00, 0x00, 0xFF, 0xFF,
176 textures.tiles.Data(0, format, texdata);
177 textures.tiles.Data(1, format, texdata + 4);
178 textures.tiles.Data(2, format, texdata + 8);