Motor de sprites/animacions/surfaces i jugador centrat (v0.2.0)

This commit is contained in:
JailDesigner committed 2026-04-18 21:02:55 +02:00
1 parent 7bb1edd08a
commit de5a670fdb
34 files changed
+16137 -81

No files matched your search

+17 -23
View File
@@ -2,8 +2,11 @@
#include <SDL3/SDL.h>
#include <algorithm>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <ranges>
#include "utils/defaults.hpp"
#include "utils/defines.hpp"
@@ -32,24 +35,15 @@ namespace {
} // namespace
Screen* Screen::instance = nullptr;
void Screen::init() {
if (instance == nullptr) {
instance = new Screen();
}
auto Screen::get() -> Screen& {
static Screen instance_;
return instance_;
}
void Screen::destroy() {
delete instance;
instance = nullptr;
}
auto Screen::get() -> Screen* {
return instance;
}
Screen::Screen() {
Screen::Screen()
: canvas_width_(GameCanvas::WIDTH),
canvas_height_(GameCanvas::HEIGHT),
canvas_(static_cast<std::size_t>(GameCanvas::WIDTH) * static_cast<std::size_t>(GameCanvas::HEIGHT), 0xFF000000U) {
if (!SDL_InitSubSystem(SDL_INIT_VIDEO)) {
std::fprintf(stderr, "SDL_InitSubSystem(VIDEO) failed: %s\n", SDL_GetError());
std::exit(EXIT_FAILURE);
@@ -76,9 +70,9 @@ Screen::Screen() {
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);
// STREAMING: el framebuffer ARGB del joc s'actualitza per CPU cada frame
// i s'envia a la GPU via SDL_UpdateTexture en present().
game_texture_ = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, GameCanvas::WIDTH, GameCanvas::HEIGHT);
if (game_texture_ == nullptr) {
std::fprintf(stderr, "SDL_CreateTexture failed: %s\n", SDL_GetError());
std::exit(EXIT_FAILURE);
@@ -94,13 +88,13 @@ Screen::~Screen() {
}
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);
const std::uint32_t COLOR = (0xFFU << 24) | (static_cast<std::uint32_t>(r) << 16) | (static_cast<std::uint32_t>(g) << 8) | static_cast<std::uint32_t>(b);
std::ranges::fill(canvas_, COLOR);
}
void Screen::present() {
SDL_UpdateTexture(game_texture_, nullptr, canvas_.data(), canvas_width_ * static_cast<int>(sizeof(std::uint32_t)));
SDL_SetRenderDrawColor(renderer_, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderClear(renderer_);
SDL_RenderTexture(renderer_, game_texture_, nullptr, nullptr);