Esquelet inicial: finestra negra i primera escena

This commit is contained in:
JailDesigner committed 2026-04-18 19:56:59 +02:00
1 parent 959a3871db
commit 7bb1edd08a
20 files changed
+818

No files matched your search

+108
View File
@@ -0,0 +1,108 @@
#include "core/rendering/screen.hpp"
#include <SDL3/SDL.h>
#include <cstdio>
#include <cstdlib>
#include "utils/defaults.hpp"
#include "utils/defines.hpp"
namespace {
auto toSdlPresentation(Defaults::ScalingMode mode) -> SDL_RendererLogicalPresentation {
switch (mode) {
case Defaults::ScalingMode::DISABLED:
return SDL_LOGICAL_PRESENTATION_DISABLED;
case Defaults::ScalingMode::STRETCH:
return SDL_LOGICAL_PRESENTATION_STRETCH;
case Defaults::ScalingMode::LETTERBOX:
return SDL_LOGICAL_PRESENTATION_LETTERBOX;
case Defaults::ScalingMode::OVERSCAN:
return SDL_LOGICAL_PRESENTATION_OVERSCAN;
case Defaults::ScalingMode::INTEGER:
return SDL_LOGICAL_PRESENTATION_INTEGER_SCALE;
}
return SDL_LOGICAL_PRESENTATION_LETTERBOX;
}
auto toSdlScaleMode(Defaults::TextureFilter filter) -> SDL_ScaleMode {
return filter == Defaults::TextureFilter::LINEAR ? SDL_SCALEMODE_LINEAR : SDL_SCALEMODE_NEAREST;
}
} // namespace
Screen* Screen::instance = nullptr;
void Screen::init() {
if (instance == nullptr) {
instance = new Screen();
}
}
void Screen::destroy() {
delete instance;
instance = nullptr;
}
auto Screen::get() -> Screen* {
return instance;
}
Screen::Screen() {
if (!SDL_InitSubSystem(SDL_INIT_VIDEO)) {
std::fprintf(stderr, "SDL_InitSubSystem(VIDEO) failed: %s\n", SDL_GetError());
std::exit(EXIT_FAILURE);
}
const int WIN_W = GameCanvas::WIDTH * Defaults::Window::ZOOM;
const int WIN_H = GameCanvas::HEIGHT * Defaults::Window::ZOOM;
Uint32 win_flags = SDL_WINDOW_RESIZABLE;
if (Defaults::Window::FULLSCREEN) { win_flags |= SDL_WINDOW_FULLSCREEN; }
window_ = SDL_CreateWindow(Texts::WINDOW_TITLE, WIN_W, WIN_H, win_flags);
if (window_ == nullptr) {
std::fprintf(stderr, "SDL_CreateWindow failed: %s\n", SDL_GetError());
std::exit(EXIT_FAILURE);
}
renderer_ = SDL_CreateRenderer(window_, nullptr);
if (renderer_ == nullptr) {
std::fprintf(stderr, "SDL_CreateRenderer failed: %s\n", SDL_GetError());
std::exit(EXIT_FAILURE);
}
SDL_SetRenderVSync(renderer_, Defaults::Video::VSYNC ? 1 : 0);
SDL_SetRenderLogicalPresentation(renderer_, GameCanvas::WIDTH, GameCanvas::HEIGHT, toSdlPresentation(Defaults::Video::SCALING_MODE));
// Destination ARGB texture: paletted surfaces will be blitted onto this
// (each surface contributes its own palette, so the final image is full ARGB).
game_texture_ = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, GameCanvas::WIDTH, GameCanvas::HEIGHT);
if (game_texture_ == nullptr) {
std::fprintf(stderr, "SDL_CreateTexture failed: %s\n", SDL_GetError());
std::exit(EXIT_FAILURE);
}
SDL_SetTextureScaleMode(game_texture_, toSdlScaleMode(Defaults::Video::TEXTURE_FILTER));
}
Screen::~Screen() {
if (game_texture_ != nullptr) { SDL_DestroyTexture(game_texture_); }
if (renderer_ != nullptr) { SDL_DestroyRenderer(renderer_); }
if (window_ != nullptr) { SDL_DestroyWindow(window_); }
SDL_QuitSubSystem(SDL_INIT_VIDEO);
}
void Screen::clear(std::uint8_t r, std::uint8_t g, std::uint8_t b) {
SDL_SetRenderTarget(renderer_, game_texture_);
SDL_SetRenderDrawColor(renderer_, r, g, b, 0xFF);
SDL_RenderClear(renderer_);
SDL_SetRenderTarget(renderer_, nullptr);
}
void Screen::present() {
SDL_SetRenderDrawColor(renderer_, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderClear(renderer_);
SDL_RenderTexture(renderer_, game_texture_, nullptr, nullptr);
SDL_RenderPresent(renderer_);
}
+28
View File
@@ -0,0 +1,28 @@
#pragma once
#include <SDL3/SDL.h>
#include <cstdint>
class Screen {
public:
static void init();
static void destroy();
static auto get() -> Screen*;
void clear(std::uint8_t r = 0, std::uint8_t g = 0, std::uint8_t b = 0);
void present();
[[nodiscard]] auto getRenderer() const -> SDL_Renderer* { return renderer_; }
[[nodiscard]] auto getTarget() const -> SDL_Texture* { return game_texture_; }
private:
Screen();
~Screen();
static Screen* instance;
SDL_Window* window_{nullptr};
SDL_Renderer* renderer_{nullptr};
SDL_Texture* game_texture_{nullptr};
};
+18
View File
@@ -0,0 +1,18 @@
#pragma once
#include <memory>
class Scene {
public:
virtual ~Scene() = default;
virtual void update(int delta_ms) = 0;
virtual void render() = 0;
[[nodiscard]] auto wantsQuit() const -> bool { return wants_quit_; }
[[nodiscard]] auto nextScene() -> std::unique_ptr<Scene> { return std::move(next_scene_); }
protected:
bool wants_quit_{false};
std::unique_ptr<Scene> next_scene_;
};
+71
View File
@@ -0,0 +1,71 @@
#include "core/system/director.hpp"
#include <SDL3/SDL.h>
#include <algorithm>
#include <cstdio>
#include <memory>
#include <utility>
#include "core/rendering/screen.hpp"
#include "core/scene/scene.hpp"
#include "core/system/event_buffer.hpp"
#include "game/scenes/black_scene.hpp"
Director::Director() {
if (!SDL_Init(0)) {
std::fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
}
Screen::init();
current_scene_ = std::make_unique<BlackScene>();
last_ticks_ms_ = SDL_GetTicks();
}
Director::~Director() {
current_scene_.reset();
Screen::destroy();
SDL_Quit();
}
void Director::processEvents() {
const bool QUIT_REQUESTED = std::ranges::any_of(
EventBuffer::events,
[](const SDL_Event& event) { return event.type == SDL_EVENT_QUIT; });
if (QUIT_REQUESTED) { current_scene_.reset(); }
}
void Director::switchSceneIfRequested() {
if (!current_scene_) { return; }
if (auto next = current_scene_->nextScene()) {
current_scene_ = std::move(next);
}
}
auto Director::iterate() -> SDL_AppResult {
processEvents();
if (!current_scene_) {
EventBuffer::events.clear();
return SDL_APP_SUCCESS;
}
const std::uint64_t NOW_MS = SDL_GetTicks();
int delta_ms = static_cast<int>(NOW_MS - last_ticks_ms_);
delta_ms = std::max(delta_ms, 0);
last_ticks_ms_ = NOW_MS;
current_scene_->update(delta_ms);
current_scene_->render();
if (current_scene_->wantsQuit()) {
EventBuffer::events.clear();
return SDL_APP_SUCCESS;
}
switchSceneIfRequested();
EventBuffer::events.clear();
return SDL_APP_CONTINUE;
}
+23
View File
@@ -0,0 +1,23 @@
#pragma once
#include <SDL3/SDL.h>
#include <cstdint>
#include <memory>
class Scene;
class Director {
public:
Director();
~Director();
auto iterate() -> SDL_AppResult;
private:
std::unique_ptr<Scene> current_scene_;
std::uint64_t last_ticks_ms_{0};
void processEvents();
void switchSceneIfRequested();
};
+9
View File
@@ -0,0 +1,9 @@
#pragma once
#include <SDL3/SDL.h>
#include <vector>
namespace EventBuffer {
inline std::vector<SDL_Event> events;
}