tot mes ben estructuraet i ara amb gestió d'events, context i subsystems. arreglos en el main loop
This commit is contained in:
1 parent
1837844b03
commit
4e0b819b96
12 files changed
+94
-49
No files matched your search
@@ -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};
|
||||
};
|
||||
@@ -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) {
|
||||
|
||||
@@ -3,14 +3,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <string>
|
||||
|
||||
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);
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
// context.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
enum class Scenes;
|
||||
|
||||
struct Context {
|
||||
Scenes current_scene;
|
||||
bool want_to_quit{false};
|
||||
};
|
||||
@@ -2,9 +2,16 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string_view>
|
||||
|
||||
namespace Defaults::Video {
|
||||
constexpr int ZOOM = 3;
|
||||
constexpr bool FULLSCREEN = false;
|
||||
constexpr float WIDTH = 320.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
@@ -16,17 +16,23 @@ Director::Director() {
|
||||
// Inicializa los subsistemas
|
||||
video_ = std::make_unique<Video>(
|
||||
Video::Config{
|
||||
Defaults::Video::FULLSCREEN,
|
||||
Defaults::Video::ZOOM,
|
||||
Defaults::Video::WIDTH,
|
||||
Defaults::Video::HEIGHT});
|
||||
.fullscreen = Defaults::Video::FULLSCREEN,
|
||||
.zoom = Defaults::Video::ZOOM,
|
||||
.width = Defaults::Video::WIDTH,
|
||||
.height = Defaults::Video::HEIGHT,
|
||||
.window_caption = Defaults::App::NAME.data(),
|
||||
.vsync = Defaults::Video::VSYNC});
|
||||
timer_ = std::make_unique<Timer>();
|
||||
event_manager_ = std::make_unique<EventManager>();
|
||||
|
||||
subsystems_.video = video_.get();
|
||||
|
||||
// Inicializa el contexto
|
||||
context_.video = video_.get();
|
||||
context_.current_scene = Scenes::TEST;
|
||||
context_.want_to_quit = false;
|
||||
|
||||
// Establece la escena inicial
|
||||
scene_ = std::make_unique<Test>(context_);
|
||||
scene_ = std::make_unique<Test>(subsystems_, context_);
|
||||
}
|
||||
|
||||
Director::~Director() {
|
||||
@@ -36,24 +42,12 @@ Director::~Director() {
|
||||
}
|
||||
|
||||
auto Director::iterate() -> int {
|
||||
while (scene_) {
|
||||
while (!context_.want_to_quit) {
|
||||
scene_->render();
|
||||
switch (scene_->update(timer_->getDelta())) {
|
||||
case Scenes::ACTION_CONTINUE:
|
||||
break;
|
||||
case Scenes::LOGO:
|
||||
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;
|
||||
}
|
||||
scene_->update(timer_->getDelta());
|
||||
|
||||
event_manager_->update();
|
||||
context_.want_to_quit = event_manager_->wantToQuit();
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -6,8 +6,10 @@
|
||||
|
||||
#include "core/rendering/video.hpp"
|
||||
#include "core/timer/timer.hpp"
|
||||
#include "core/eventmanager.hpp"
|
||||
#include "game/scenes/scene.hpp"
|
||||
#include "gamecontext.hpp"
|
||||
#include "context.hpp"
|
||||
#include "subsystems.hpp"
|
||||
|
||||
class Director {
|
||||
public:
|
||||
@@ -17,11 +19,13 @@ class Director {
|
||||
auto iterate() -> int;
|
||||
|
||||
private:
|
||||
GameContext context_;
|
||||
Context context_;
|
||||
Subsystems subsystems_;
|
||||
|
||||
// Core
|
||||
std::unique_ptr<Video> video_;
|
||||
std::unique_ptr<Timer> timer_;
|
||||
std::unique_ptr<EventManager> event_manager_;
|
||||
|
||||
// Game
|
||||
std::unique_ptr<Scene> scene_;
|
||||
|
||||
@@ -8,6 +8,6 @@ class Scene {
|
||||
public:
|
||||
virtual ~Scene() = default;
|
||||
|
||||
virtual auto update(int delta_ms) -> Scenes = 0;
|
||||
virtual void update(int delta_ms) = 0;
|
||||
virtual void render() = 0;
|
||||
};
|
||||
@@ -6,7 +6,5 @@ enum class Scenes {
|
||||
INTRO,
|
||||
LOGO,
|
||||
GAME,
|
||||
TEST,
|
||||
ACTION_CONTINUE,
|
||||
ACTION_QUIT
|
||||
TEST
|
||||
};
|
||||
@@ -5,18 +5,18 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "core/rendering/video.hpp"
|
||||
#include "game/gamecontext.hpp"
|
||||
#include "game/context.hpp"
|
||||
#include "game/subsystems.hpp"
|
||||
|
||||
Test::Test(GameContext& context)
|
||||
: context_(context) {}
|
||||
Test::Test(Subsystems& subsystems, Context& context)
|
||||
: subsystems_(subsystems),
|
||||
context_(context) {}
|
||||
|
||||
auto Test::update(int delta_ms) -> Scenes {
|
||||
void Test::update(int delta_ms) {
|
||||
time_elapsed_ += delta_ms;
|
||||
if (time_elapsed_ >= 5000) { return Scenes::ACTION_QUIT; }
|
||||
return Scenes::ACTION_CONTINUE;
|
||||
}
|
||||
|
||||
void Test::render() {
|
||||
context_.video->clean(time_elapsed_ / 10);
|
||||
context_.video->present();
|
||||
subsystems_.video->clean(time_elapsed_ / 10);
|
||||
subsystems_.video->present();
|
||||
}
|
||||
@@ -6,16 +6,18 @@
|
||||
|
||||
#include "scene.hpp"
|
||||
|
||||
struct GameContext;
|
||||
struct Subsystems;
|
||||
struct Context;
|
||||
|
||||
class Test final : public Scene {
|
||||
public:
|
||||
explicit Test(GameContext& context);
|
||||
auto update(int delta_ms) -> Scenes override;
|
||||
explicit Test(Subsystems& subsystems, Context& context);
|
||||
void update(int delta_ms) override;
|
||||
void render() override;
|
||||
|
||||
private:
|
||||
GameContext& context_;
|
||||
Subsystems& subsystems_;
|
||||
Context& context_;
|
||||
int color_{0};
|
||||
Uint64 time_elapsed_{0};
|
||||
};
|
||||
@@ -1,9 +1,9 @@
|
||||
// GameContext.h
|
||||
// subsystems.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
class Video;
|
||||
|
||||
struct GameContext {
|
||||
struct Subsystems {
|
||||
Video* video{nullptr};
|
||||
};
|
||||
};
|
||||
Reference in new issue
Block a user