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;
}
+2
View File
@@ -0,0 +1,2 @@
DisableFormat: true
SortIncludes: Never
+4
View File
@@ -0,0 +1,4 @@
# source/external/.clang-tidy
Checks: '-*'
WarningsAsErrors: ''
HeaderFilterRegex: ''
+23
View File
@@ -0,0 +1,23 @@
#include "game/scenes/black_scene.hpp"
#include <SDL3/SDL.h>
#include "core/rendering/screen.hpp"
#include "core/system/event_buffer.hpp"
BlackScene::BlackScene() = default;
void BlackScene::update(int delta_ms) {
elapsed_ms_ += delta_ms;
for (const auto& event : EventBuffer::events) {
if (event.type == SDL_EVENT_KEY_DOWN && event.key.scancode == SDL_SCANCODE_ESCAPE) {
wants_quit_ = true;
}
}
}
void BlackScene::render() {
Screen::get()->clear(0x00, 0x00, 0x00);
Screen::get()->present();
}
+14
View File
@@ -0,0 +1,14 @@
#pragma once
#include "core/scene/scene.hpp"
class BlackScene : public Scene {
public:
BlackScene();
void update(int delta_ms) override;
void render() override;
private:
int elapsed_ms_{0};
};
+33
View File
@@ -0,0 +1,33 @@
#define SDL_MAIN_USE_CALLBACKS 1
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include "core/system/director.hpp"
#include "core/system/event_buffer.hpp"
auto SDL_AppInit(void** appstate, int argc, char* argv[]) -> SDL_AppResult {
(void)argc;
(void)argv;
auto* director = new Director();
*appstate = director;
return SDL_APP_CONTINUE;
}
auto SDL_AppEvent(void* appstate, SDL_Event* event) -> SDL_AppResult {
(void)appstate;
EventBuffer::events.push_back(*event);
return SDL_APP_CONTINUE;
}
auto SDL_AppIterate(void* appstate) -> SDL_AppResult {
auto* director = static_cast<Director*>(appstate);
return director->iterate();
}
void SDL_AppQuit(void* appstate, SDL_AppResult result) {
(void)result;
auto* director = static_cast<Director*>(appstate);
delete director;
}
+54
View File
@@ -0,0 +1,54 @@
#pragma once
#include <SDL3/SDL.h>
#include <cstdint>
namespace Defaults {
// Mode de presentació lògica (escala finestra). Mapeja als valors de
// SDL_RendererLogicalPresentation des de Screen via una funció helper,
// perquè aquest header no depengui de detalls de SDL3 més enllà del scancode.
enum class ScalingMode : std::uint8_t {
DISABLED = 0,
STRETCH = 1,
LETTERBOX = 2,
OVERSCAN = 3,
INTEGER = 4,
};
// Filtre de textura per a l'upscale final.
enum class TextureFilter : std::uint8_t {
NEAREST = 0,
LINEAR = 1,
};
namespace Window {
constexpr int ZOOM = 3;
constexpr bool FULLSCREEN = false;
} // namespace Window
namespace Video {
constexpr bool VSYNC = true;
constexpr ScalingMode SCALING_MODE = ScalingMode::LETTERBOX;
constexpr TextureFilter TEXTURE_FILTER = TextureFilter::NEAREST;
} // namespace Video
namespace Audio {
constexpr bool ENABLED = true;
constexpr float VOLUME = 1.0F;
constexpr bool MUSIC_ENABLED = true;
constexpr float MUSIC_VOLUME = 0.8F;
constexpr bool SOUND_ENABLED = true;
constexpr float SOUND_VOLUME = 1.0F;
} // namespace Audio
namespace KeysGame {
constexpr SDL_Scancode UP = SDL_SCANCODE_UP;
constexpr SDL_Scancode DOWN = SDL_SCANCODE_DOWN;
constexpr SDL_Scancode LEFT = SDL_SCANCODE_LEFT;
constexpr SDL_Scancode RIGHT = SDL_SCANCODE_RIGHT;
constexpr SDL_Scancode EXIT = SDL_SCANCODE_ESCAPE;
} // namespace KeysGame
} // namespace Defaults
+19
View File
@@ -0,0 +1,19 @@
#pragma once
namespace Texts {
constexpr const char* WINDOW_TITLE = "© 2026 Aventures en Egipte — JailDesigner";
constexpr const char* COPYRIGHT = "© 2026 JailDesigner";
constexpr const char* VERSION = "0.1.0"; // Single source of truth — Makefile el grep
} // namespace Texts
namespace GameCanvas {
constexpr int WIDTH = 320;
constexpr int HEIGHT = 240;
constexpr int CENTER_X = WIDTH / 2;
constexpr int CENTER_Y = HEIGHT / 2;
} // namespace GameCanvas
namespace Tile {
constexpr int SIZE = 8;
constexpr int HALF_SIZE = SIZE / 2;
} // namespace Tile
+6
View File
@@ -0,0 +1,6 @@
#pragma once
namespace Version {
constexpr const char* GIT_HASH = "@GIT_HASH@";
constexpr const char* APP_NAME = "Aventures en Egipte";
} // namespace Version