tot mes ben estructuraet i ara amb gestió d'events, context i subsystems. arreglos en el main loop

This commit is contained in:
JailDesigner committed 2026-11-06 13:58:59 +01:00
1 parent 1837844b03
commit 4e0b819b96
12 files changed
+94 -49

No files matched your search

+27
View File
@@ -0,0 +1,27 @@
// eventmanager.hpp
#include <SDL3/SDL.h>
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};
};
+3 -3
View File
@@ -30,7 +30,7 @@ auto Video::initSDL() -> int {
return 1; 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) { if (window_ == nullptr) {
SDL_Log("No se pudo crear la ventana: %s", SDL_GetError()); SDL_Log("No se pudo crear la ventana: %s", SDL_GetError());
cleanup(); cleanup();
@@ -46,8 +46,8 @@ auto Video::initSDL() -> int {
return 1; return 1;
} }
// SDL_SetRenderVSync(renderer_, config_.vsync ? 1 : 0); SDL_SetRenderVSync(renderer_, config_.vsync ? 1 : 0);
// SDL_SetRenderLogicalPresentation(renderer_, GameCanvas::WIDTH, GameCanvas::HEIGHT, toSdlPresentation(config_.scaling_mode)); 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); game_texture_ = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, config_.width, config_.height);
if (game_texture_ == nullptr) { if (game_texture_ == nullptr) {
+4 -1
View File
@@ -3,14 +3,17 @@
#pragma once #pragma once
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <string>
class Video { class Video {
public: public:
struct Config { struct Config {
bool fullscreen{false}; bool fullscreen{false};
float zoom{1.0F}; float zoom{2.0F};
float width{320.0F}; float width{320.0F};
float height{240.0F}; float height{240.0F};
std::string window_caption{};
bool vsync{true};
}; };
Video(Config config); Video(Config config);
+10
View File
@@ -0,0 +1,10 @@
// context.hpp
#pragma once
enum class Scenes;
struct Context {
Scenes current_scene;
bool want_to_quit{false};
};
+8 -1
View File
@@ -2,9 +2,16 @@
#pragma once #pragma once
#include <string_view>
namespace Defaults::Video { namespace Defaults::Video {
constexpr int ZOOM = 3; constexpr int ZOOM = 3;
constexpr bool FULLSCREEN = false; constexpr bool FULLSCREEN = false;
constexpr float WIDTH = 320.0F; constexpr float WIDTH = 320.0F;
constexpr float HEIGHT = 240.0F; constexpr float HEIGHT = 240.0F;
} // namespace Defaults::Video constexpr bool VSYNC = true;
} // namespace Defaults::Video
namespace Defaults::App {
constexpr std::string_view NAME = "Spectrum Engine";
} // namespace Defaults::App
+17 -23
View File
@@ -16,17 +16,23 @@ Director::Director() {
// Inicializa los subsistemas // Inicializa los subsistemas
video_ = std::make_unique<Video>( video_ = std::make_unique<Video>(
Video::Config{ Video::Config{
Defaults::Video::FULLSCREEN, .fullscreen = Defaults::Video::FULLSCREEN,
Defaults::Video::ZOOM, .zoom = Defaults::Video::ZOOM,
Defaults::Video::WIDTH, .width = Defaults::Video::WIDTH,
Defaults::Video::HEIGHT}); .height = Defaults::Video::HEIGHT,
.window_caption = Defaults::App::NAME.data(),
.vsync = Defaults::Video::VSYNC});
timer_ = std::make_unique<Timer>(); timer_ = std::make_unique<Timer>();
event_manager_ = std::make_unique<EventManager>();
subsystems_.video = video_.get();
// Inicializa el contexto // Inicializa el contexto
context_.video = video_.get(); context_.current_scene = Scenes::TEST;
context_.want_to_quit = false;
// Establece la escena inicial // Establece la escena inicial
scene_ = std::make_unique<Test>(context_); scene_ = std::make_unique<Test>(subsystems_, context_);
} }
Director::~Director() { Director::~Director() {
@@ -36,24 +42,12 @@ Director::~Director() {
} }
auto Director::iterate() -> int { auto Director::iterate() -> int {
while (scene_) { while (!context_.want_to_quit) {
scene_->render(); scene_->render();
switch (scene_->update(timer_->getDelta())) { scene_->update(timer_->getDelta());
case Scenes::ACTION_CONTINUE:
break; event_manager_->update();
case Scenes::LOGO: context_.want_to_quit = event_manager_->wantToQuit();
break;
case Scenes::INTRO:
break;
case Scenes::GAME:
break;
case Scenes::TEST:
scene_ = std::make_unique<Test>(context_);
break;
case Scenes::ACTION_QUIT:
scene_.reset();
break;
}
} }
return 0; return 0;
+6 -2
View File
@@ -6,8 +6,10 @@
#include "core/rendering/video.hpp" #include "core/rendering/video.hpp"
#include "core/timer/timer.hpp" #include "core/timer/timer.hpp"
#include "core/eventmanager.hpp"
#include "game/scenes/scene.hpp" #include "game/scenes/scene.hpp"
#include "gamecontext.hpp" #include "context.hpp"
#include "subsystems.hpp"
class Director { class Director {
public: public:
@@ -17,11 +19,13 @@ class Director {
auto iterate() -> int; auto iterate() -> int;
private: private:
GameContext context_; Context context_;
Subsystems subsystems_;
// Core // Core
std::unique_ptr<Video> video_; std::unique_ptr<Video> video_;
std::unique_ptr<Timer> timer_; std::unique_ptr<Timer> timer_;
std::unique_ptr<EventManager> event_manager_;
// Game // Game
std::unique_ptr<Scene> scene_; std::unique_ptr<Scene> scene_;
+1 -1
View File
@@ -8,6 +8,6 @@ class Scene {
public: public:
virtual ~Scene() = default; virtual ~Scene() = default;
virtual auto update(int delta_ms) -> Scenes = 0; virtual void update(int delta_ms) = 0;
virtual void render() = 0; virtual void render() = 0;
}; };
+1 -3
View File
@@ -6,7 +6,5 @@ enum class Scenes {
INTRO, INTRO,
LOGO, LOGO,
GAME, GAME,
TEST, TEST
ACTION_CONTINUE,
ACTION_QUIT
}; };
+8 -8
View File
@@ -5,18 +5,18 @@
#include <iostream> #include <iostream>
#include "core/rendering/video.hpp" #include "core/rendering/video.hpp"
#include "game/gamecontext.hpp" #include "game/context.hpp"
#include "game/subsystems.hpp"
Test::Test(GameContext& context) Test::Test(Subsystems& subsystems, Context& context)
: context_(context) {} : subsystems_(subsystems),
context_(context) {}
auto Test::update(int delta_ms) -> Scenes { void Test::update(int delta_ms) {
time_elapsed_ += delta_ms; time_elapsed_ += delta_ms;
if (time_elapsed_ >= 5000) { return Scenes::ACTION_QUIT; }
return Scenes::ACTION_CONTINUE;
} }
void Test::render() { void Test::render() {
context_.video->clean(time_elapsed_ / 10); subsystems_.video->clean(time_elapsed_ / 10);
context_.video->present(); subsystems_.video->present();
} }
+6 -4
View File
@@ -6,16 +6,18 @@
#include "scene.hpp" #include "scene.hpp"
struct GameContext; struct Subsystems;
struct Context;
class Test final : public Scene { class Test final : public Scene {
public: public:
explicit Test(GameContext& context); explicit Test(Subsystems& subsystems, Context& context);
auto update(int delta_ms) -> Scenes override; void update(int delta_ms) override;
void render() override; void render() override;
private: private:
GameContext& context_; Subsystems& subsystems_;
Context& context_;
int color_{0}; int color_{0};
Uint64 time_elapsed_{0}; Uint64 time_elapsed_{0};
}; };
@@ -1,9 +1,9 @@
// GameContext.h // subsystems.hpp
#pragma once #pragma once
class Video; class Video;
struct GameContext { struct Subsystems {
Video* video{nullptr}; Video* video{nullptr};
}; };