From 4e0b819b9625aec03c9e1ddbfe117401e6ae2a41 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Fri, 6 Nov 2026 13:58:59 +0100 Subject: [PATCH] =?UTF-8?q?tot=20mes=20ben=20estructuraet=20i=20ara=20amb?= =?UTF-8?q?=20gesti=C3=B3=20d'events,=20context=20i=20subsystems.=20arregl?= =?UTF-8?q?os=20en=20el=20main=20loop?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/core/eventmanager.hpp | 27 +++++++++++++ source/core/rendering/video.cpp | 6 +-- source/core/rendering/video.hpp | 5 ++- source/game/context.hpp | 10 +++++ source/game/defaults.hpp | 9 ++++- source/game/director.cpp | 40 ++++++++----------- source/game/director.hpp | 8 +++- source/game/scenes/scene.hpp | 2 +- source/game/scenes/scenes.hpp | 4 +- source/game/scenes/test.cpp | 16 ++++---- source/game/scenes/test.hpp | 10 +++-- .../game/{gamecontext.hpp => subsystems.hpp} | 6 +-- 12 files changed, 94 insertions(+), 49 deletions(-) create mode 100644 source/core/eventmanager.hpp create mode 100644 source/game/context.hpp rename source/game/{gamecontext.hpp => subsystems.hpp} (57%) diff --git a/source/core/eventmanager.hpp b/source/core/eventmanager.hpp new file mode 100644 index 0000000..9516537 --- /dev/null +++ b/source/core/eventmanager.hpp @@ -0,0 +1,27 @@ +// eventmanager.hpp + +#include + +class EventManager { + public: + void update() { + SDL_Event event; + + while (SDL_PollEvent(&event)) { + if (event.type == SDL_EVENT_QUIT || + event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED) { + if (!quit) { + quit = true; + SDL_LogInfo( + SDL_LOG_CATEGORY_APPLICATION, + "Someone wants to quit!"); + } + } + } + } + + auto wantToQuit() -> bool { return quit; } + + private: + bool quit{false}; +}; \ No newline at end of file diff --git a/source/core/rendering/video.cpp b/source/core/rendering/video.cpp index 6e45a84..22d2ada 100644 --- a/source/core/rendering/video.cpp +++ b/source/core/rendering/video.cpp @@ -30,7 +30,7 @@ auto Video::initSDL() -> int { return 1; } - window_ = SDL_CreateWindow("Mi ventana", config_.width, config_.height, 0); + window_ = SDL_CreateWindow(config_.window_caption.c_str(), config_.width, config_.height, 0); if (window_ == nullptr) { SDL_Log("No se pudo crear la ventana: %s", SDL_GetError()); cleanup(); @@ -46,8 +46,8 @@ auto Video::initSDL() -> int { return 1; } - // SDL_SetRenderVSync(renderer_, config_.vsync ? 1 : 0); - // SDL_SetRenderLogicalPresentation(renderer_, GameCanvas::WIDTH, GameCanvas::HEIGHT, toSdlPresentation(config_.scaling_mode)); + SDL_SetRenderVSync(renderer_, config_.vsync ? 1 : 0); + SDL_SetRenderLogicalPresentation(renderer_, config_.width * config_.zoom, config_.height * config_.zoom, SDL_LOGICAL_PRESENTATION_INTEGER_SCALE); game_texture_ = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, config_.width, config_.height); if (game_texture_ == nullptr) { diff --git a/source/core/rendering/video.hpp b/source/core/rendering/video.hpp index cfbe439..00b429d 100644 --- a/source/core/rendering/video.hpp +++ b/source/core/rendering/video.hpp @@ -3,14 +3,17 @@ #pragma once #include +#include class Video { public: struct Config { bool fullscreen{false}; - float zoom{1.0F}; + float zoom{2.0F}; float width{320.0F}; float height{240.0F}; + std::string window_caption{}; + bool vsync{true}; }; Video(Config config); diff --git a/source/game/context.hpp b/source/game/context.hpp new file mode 100644 index 0000000..8f723f7 --- /dev/null +++ b/source/game/context.hpp @@ -0,0 +1,10 @@ +// context.hpp + +#pragma once + +enum class Scenes; + +struct Context { + Scenes current_scene; + bool want_to_quit{false}; +}; diff --git a/source/game/defaults.hpp b/source/game/defaults.hpp index 9d18006..7ca8414 100644 --- a/source/game/defaults.hpp +++ b/source/game/defaults.hpp @@ -2,9 +2,16 @@ #pragma once +#include + namespace Defaults::Video { constexpr int ZOOM = 3; constexpr bool FULLSCREEN = false; constexpr float WIDTH = 320.0F; constexpr float HEIGHT = 240.0F; -} // namespace Defaults::Video \ No newline at end of file + constexpr bool VSYNC = true; +} // namespace Defaults::Video + +namespace Defaults::App { + constexpr std::string_view NAME = "Spectrum Engine"; +} // namespace Defaults::App \ No newline at end of file diff --git a/source/game/director.cpp b/source/game/director.cpp index 69e2e90..c578975 100644 --- a/source/game/director.cpp +++ b/source/game/director.cpp @@ -16,17 +16,23 @@ Director::Director() { // Inicializa los subsistemas video_ = std::make_unique