diff options
Diffstat (limited to 'src/crepe/facade/SDLContext.cpp')
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 55 |
1 files changed, 28 insertions, 27 deletions
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 7e19ede..d6e3159 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -19,7 +19,7 @@ #include "../api/Color.h" #include "../api/Config.h" #include "../api/Sprite.h" -#include "../util/Log.h" +#include "../util/dbg.h" #include "manager/Mediator.h" #include "SDLContext.h" @@ -35,10 +35,10 @@ SDLContext::SDLContext(Mediator & mediator) { throw runtime_error(format("SDLContext: SDL_Init error: {}", SDL_GetError())); } - auto & cfg = Config::get_instance().window_settings; + auto & cfg = Config::get_instance().window; SDL_Window * tmp_window - = SDL_CreateWindow(cfg.window_title.c_str(), SDL_WINDOWPOS_CENTERED, - SDL_WINDOWPOS_CENTERED, cfg.default_size.x, cfg.default_size.y, 0); + = SDL_CreateWindow(cfg.title.c_str(), SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, cfg.size.x, cfg.size.y, 0); if (!tmp_window) { throw runtime_error(format("SDLContext: SDL_Window error: {}", SDL_GetError())); } @@ -76,12 +76,10 @@ SDLContext::~SDLContext() { } Keycode SDLContext::sdl_to_keycode(SDL_Scancode sdl_key) { - auto it = LOOKUP_TABLE.find(sdl_key); - if (it != LOOKUP_TABLE.end()) { - return it->second; - } + if (!LOOKUP_TABLE.contains(sdl_key)) + return Keycode::NONE; - return Keycode::NONE; + return LOOKUP_TABLE.at(sdl_key); } void SDLContext::update_keyboard_state() { // Array to hold the key states (true if pressed, false if not) @@ -90,10 +88,8 @@ void SDLContext::update_keyboard_state() { for (int i = 0; i < SDL_NUM_SCANCODES; ++i) { Keycode key = sdl_to_keycode(static_cast<SDL_Scancode>(i)); - - if (key != Keycode::NONE) { - this->keyboard_state[key] = current_state[i] != 0; - } + if (key == Keycode::NONE) continue; + this->keyboard_state[key] = current_state[i] != 0; } } @@ -187,13 +183,13 @@ void SDLContext::draw(const RenderContext & ctx) { } void SDLContext::update_camera_view(const Camera & cam, const vec2 & new_pos) { - const Camera::Data & cam_data = cam.data; + const Config & config = Config::get_instance(); // resize window int w, h; SDL_GetWindowSize(this->game_window.get(), &w, &h); - if (w != cam.screen.x || h != cam.screen.y) { - SDL_SetWindowSize(this->game_window.get(), cam.screen.x, cam.screen.y); + if (w != config.window.size.x || h != config.window.size.y) { + SDL_SetWindowSize(this->game_window.get(), config.window.size.x, config.window.size.y); } vec2 & zoomed_viewport = this->cam_aux_data.zoomed_viewport; @@ -202,28 +198,28 @@ void SDLContext::update_camera_view(const Camera & cam, const vec2 & new_pos) { this->cam_aux_data.cam_pos = new_pos; zoomed_viewport = cam.viewport_size * cam_data.zoom; - float screen_aspect = static_cast<float>(cam.screen.x) / cam.screen.y; + float screen_aspect = static_cast<float>(config.window.size.x) / config.window.size.y; float viewport_aspect = zoomed_viewport.x / zoomed_viewport.y; // calculate black bars if (screen_aspect > viewport_aspect) { // pillarboxing - float scale = cam.screen.y / zoomed_viewport.y; + float scale = config.window.size.y / zoomed_viewport.y; float adj_width = zoomed_viewport.x * scale; - float bar_width = (cam.screen.x - adj_width) / 2; - this->black_bars[0] = {0, 0, bar_width, (float) cam.screen.y}; - this->black_bars[1] = {(cam.screen.x - bar_width), 0, bar_width, (float) cam.screen.y}; + float bar_width = (config.window.size.x - adj_width) / 2; + this->black_bars[0] = {0, 0, bar_width, (float) config.window.size.y}; + this->black_bars[1] = {(config.window.size.x - bar_width), 0, bar_width, (float) config.window.size.y}; bar_size = {bar_width, 0}; render_scale.x = render_scale.y = scale; } else { // letterboxing - float scale = cam.screen.x / (cam.viewport_size.x * cam_data.zoom); + float scale = config.window.size.x / (cam.viewport_size.x * cam_data.zoom); float adj_height = cam.viewport_size.y * scale; - float bar_height = (cam.screen.y - adj_height) / 2; - this->black_bars[0] = {0, 0, (float) cam.screen.x, bar_height}; + float bar_height = (config.window.size.y - adj_height) / 2; + this->black_bars[0] = {0, 0, (float) config.window.size.x, bar_height}; this->black_bars[1] - = {0, (cam.screen.y - bar_height), (float) cam.screen.x, bar_height}; + = {0, (config.window.size.y - bar_height), (float) config.window.size.x, bar_height}; bar_size = {0, bar_height}; render_scale.x = render_scale.y = scale; @@ -235,8 +231,8 @@ void SDLContext::update_camera_view(const Camera & cam, const vec2 & new_pos) { SDL_Rect bg = { .x = 0, .y = 0, - .w = cam.screen.x, - .h = cam.screen.y, + .w = config.window.size.x, + .h = config.window.size.y, }; // fill bg color @@ -402,3 +398,8 @@ void SDLContext::set_color_texture(const Texture & texture, const Color & color) SDL_SetTextureColorMod(texture.get_img(), color.r, color.g, color.b); SDL_SetTextureAlphaMod(texture.get_img(), color.a); } + +const keyboard_state_t & SDLContext::get_keyboard_state() const { + return this->keyboard_state; +} + |