Redistribuits els .cpp en carpetes

Actualitzat cmake
Modificats els include de SDL2 a SDL3
This commit is contained in:
2025-10-15 08:28:57 +02:00
parent c3415fd106
commit 78c5333144
86 changed files with 6757 additions and 7610 deletions

View File

@@ -1,51 +1,50 @@
#include "screen.h"
#include <SDL2/SDL_error.h> // Para SDL_GetError
#include <SDL2/SDL_events.h> // Para SDL_DISABLE, SDL_ENABLE
#include <SDL2/SDL_mouse.h> // Para SDL_ShowCursor
#include <SDL2/SDL_pixels.h> // Para SDL_PIXELFORMAT_ARGB8888, SDL_PIXELFORM...
#include <SDL2/SDL_timer.h> // Para SDL_GetTicks
#include <ctype.h> // Para toupper
#include <algorithm> // Para max, min, transform
#include <fstream> // Para basic_ostream, operator<<, endl, basic_...
#include <iostream> // Para cerr
#include <iterator> // Para istreambuf_iterator, operator==
#include <string> // Para char_traits, string, operator+, operator==
#include "asset.h" // Para Asset, AssetType
#include "jail_shader.h" // Para init, render
#include "mouse.h" // Para updateCursorVisibility
#include "notifier.h" // Para Notifier
#include "options.h" // Para Options, options, OptionsVideo, Border
#include "resource.h" // Para Resource
#include "surface.h" // Para Surface, readPalFile
#include "text.h" // Para Text
#include <SDL3/SDL_error.h> // Para SDL_GetError
#include <SDL3/SDL_events.h> // Para SDL_DISABLE, SDL_ENABLE
#include <SDL3/SDL_mouse.h> // Para SDL_ShowCursor
#include <SDL3/SDL_pixels.h> // Para SDL_PIXELFORMAT_ARGB8888, SDL_PIXELFORM...
#include <SDL3/SDL_timer.h> // Para SDL_GetTicks
#include <ctype.h> // Para toupper
#include <algorithm> // Para max, min, transform
#include <fstream> // Para basic_ostream, operator<<, endl, basic_...
#include <iostream> // Para cerr
#include <iterator> // Para istreambuf_iterator, operator==
#include <string> // Para char_traits, string, operator+, operator==
#include "asset.h" // Para Asset, AssetType
#include "jail_shader.h" // Para init, render
#include "mouse.h" // Para updateCursorVisibility
#include "notifier.h" // Para Notifier
#include "options.h" // Para Options, options, OptionsVideo, Border
#include "resource.h" // Para Resource
#include "surface.h" // Para Surface, readPalFile
#include "text.h" // Para Text
// [SINGLETON]
Screen *Screen::screen_ = nullptr;
Screen* Screen::screen_ = nullptr;
// [SINGLETON] Crearemos el objeto con esta función estática
void Screen::init(SDL_Window *window, SDL_Renderer *renderer)
{
void Screen::init(SDL_Window* window, SDL_Renderer* renderer) {
Screen::screen_ = new Screen(window, renderer);
}
// [SINGLETON] Destruiremos el objeto con esta función estática
void Screen::destroy()
{
void Screen::destroy() {
delete Screen::screen_;
}
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
Screen *Screen::get()
{
Screen* Screen::get() {
return Screen::screen_;
}
// Constructor
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer)
Screen::Screen(SDL_Window* window, SDL_Renderer* renderer)
: window_(window),
renderer_(renderer),
palettes_(Asset::get()->getListByType(AssetType::PALETTE))
{
palettes_(Asset::get()->getListByType(AssetType::PALETTE)) {
// Inicializa variables
SDL_DisplayMode DM;
SDL_GetCurrentDisplayMode(0, &DM);
@@ -64,22 +63,18 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer)
// Crea la textura donde se dibujan los graficos del juego
game_texture_ = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, options.game.width, options.game.height);
if (!game_texture_)
{
if (!game_texture_) {
// Registrar el error si está habilitado
if (options.console)
{
if (options.console) {
std::cerr << "Error: game_texture_ could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
}
}
// Crea la textura donde se dibuja el borde que rodea el area de juego
border_texture_ = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, options.game.width + options.video.border.width * 2, options.game.height + options.video.border.height * 2);
if (!border_texture_)
{
if (!border_texture_) {
// Registrar el error si está habilitado
if (options.console)
{
if (options.console) {
std::cerr << "Error: border_texture_ could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
}
}
@@ -88,11 +83,9 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer)
const int EXTRA_WIDTH = options.video.border.enabled ? options.video.border.width * 2 : 0;
const int EXTRA_HEIGHT = options.video.border.enabled ? options.video.border.height * 2 : 0;
shaders_texture_ = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, options.game.width + EXTRA_WIDTH, options.game.height + EXTRA_HEIGHT);
if (!shaders_texture_)
{
if (!shaders_texture_) {
// Registrar el error si está habilitado
if (options.console)
{
if (options.console) {
std::cerr << "Error: shaders_texture_ could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
}
}
@@ -122,16 +115,14 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer)
}
// Destructor
Screen::~Screen()
{
Screen::~Screen() {
SDL_DestroyTexture(game_texture_);
SDL_DestroyTexture(border_texture_);
SDL_DestroyTexture(shaders_texture_);
}
// Limpia el renderer
void Screen::clearRenderer(Color color)
{
void Screen::clearRenderer(Color color) {
SDL_SetRenderDrawColor(renderer_, color.r, color.g, color.b, 0xFF);
SDL_RenderClear(renderer_);
}
@@ -140,8 +131,7 @@ void Screen::clearRenderer(Color color)
void Screen::start() { setRendererSurface(nullptr); }
// Vuelca el contenido del renderizador en pantalla
void Screen::render()
{
void Screen::render() {
fps_.increment();
// Renderiza todos los overlays
@@ -155,8 +145,7 @@ void Screen::render()
}
// Establece el modo de video
void Screen::setVideoMode(int mode)
{
void Screen::setVideoMode(int mode) {
// Actualiza las opciones
options.video.mode = mode;
@@ -170,23 +159,19 @@ void Screen::setVideoMode(int mode)
}
// Camibia entre pantalla completa y ventana
void Screen::toggleVideoMode()
{
void Screen::toggleVideoMode() {
options.video.mode = (options.video.mode == 0) ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0;
setVideoMode(options.video.mode);
}
// Reduce el tamaño de la ventana
bool Screen::decWindowZoom()
{
if (options.video.mode == 0)
{
bool Screen::decWindowZoom() {
if (options.video.mode == 0) {
const int PREVIOUS_ZOOM = options.window.zoom;
--options.window.zoom;
options.window.zoom = std::max(options.window.zoom, 1);
if (options.window.zoom != PREVIOUS_ZOOM)
{
if (options.window.zoom != PREVIOUS_ZOOM) {
setVideoMode(options.video.mode);
return true;
}
@@ -196,16 +181,13 @@ bool Screen::decWindowZoom()
}
// Aumenta el tamaño de la ventana
bool Screen::incWindowZoom()
{
if (options.video.mode == 0)
{
bool Screen::incWindowZoom() {
if (options.video.mode == 0) {
const int PREVIOUS_ZOOM = options.window.zoom;
++options.window.zoom;
options.window.zoom = std::min(options.window.zoom, options.window.max_zoom);
if (options.window.zoom != PREVIOUS_ZOOM)
{
if (options.window.zoom != PREVIOUS_ZOOM) {
setVideoMode(options.video.mode);
return true;
}
@@ -215,55 +197,47 @@ bool Screen::incWindowZoom()
}
// Cambia el color del borde
void Screen::setBorderColor(Uint8 color)
{
void Screen::setBorderColor(Uint8 color) {
border_color_ = color;
border_surface_->clear(border_color_);
}
// Cambia entre borde visible y no visible
void Screen::toggleBorder()
{
void Screen::toggleBorder() {
options.video.border.enabled = !options.video.border.enabled;
createShadersTexture();
setVideoMode(options.video.mode);
}
// Dibuja las notificaciones
void Screen::renderNotifications()
{
if (notifications_enabled_)
{
void Screen::renderNotifications() {
if (notifications_enabled_) {
Notifier::get()->render();
}
}
// Cambia el estado de los shaders
void Screen::toggleShaders()
{
void Screen::toggleShaders() {
options.video.shaders = !options.video.shaders;
setVideoMode(options.video.mode);
}
// Actualiza la lógica de la clase
void Screen::update()
{
void Screen::update() {
fps_.calculate(SDL_GetTicks());
Notifier::get()->update();
Mouse::updateCursorVisibility();
}
// Calcula el tamaño de la ventana
void Screen::adjustWindowSize()
{
void Screen::adjustWindowSize() {
window_width_ = options.game.width + (options.video.border.enabled ? options.video.border.width * 2 : 0);
window_height_ = options.game.height + (options.video.border.enabled ? options.video.border.height * 2 : 0);
options.window.max_zoom = getMaxZoom();
// Establece el nuevo tamaño
if (options.video.mode == 0)
{
if (options.video.mode == 0) {
int old_width, old_height;
SDL_GetWindowSize(window_, &old_width, &old_height);
@@ -282,8 +256,7 @@ void Screen::adjustWindowSize()
void Screen::adjustRenderLogicalSize() { SDL_RenderSetLogicalSize(renderer_, window_width_, window_height_); }
// Obtiene el tamaño máximo de zoom posible para la ventana
int Screen::getMaxZoom()
{
int Screen::getMaxZoom() {
// Obtiene información sobre la pantalla
SDL_DisplayMode DM;
SDL_GetCurrentDisplayMode(0, &DM);
@@ -298,10 +271,8 @@ int Screen::getMaxZoom()
}
// Reinicia los shaders
void Screen::resetShaders()
{
if (options.video.shaders)
{
void Screen::resetShaders() {
if (options.video.shaders) {
const std::string GLSL_FILE = options.video.border.enabled ? "crtpi_240.glsl" : "crtpi_192.glsl";
std::ifstream f(Asset::get()->get(GLSL_FILE).c_str());
std::string source((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
@@ -311,17 +282,14 @@ void Screen::resetShaders()
}
// Establece el renderizador para las surfaces
void Screen::setRendererSurface(std::shared_ptr<Surface> surface)
{
void Screen::setRendererSurface(std::shared_ptr<Surface> surface) {
(surface) ? renderer_surface_ = std::make_shared<std::shared_ptr<Surface>>(surface) : renderer_surface_ = std::make_shared<std::shared_ptr<Surface>>(game_surface_);
}
// Cambia la paleta
void Screen::nextPalette()
{
void Screen::nextPalette() {
++current_palette_;
if (current_palette_ == static_cast<int>(palettes_.size()))
{
if (current_palette_ == static_cast<int>(palettes_.size())) {
current_palette_ = 0;
}
@@ -329,14 +297,10 @@ void Screen::nextPalette()
}
// Cambia la paleta
void Screen::previousPalette()
{
if (current_palette_ > 0)
{
void Screen::previousPalette() {
if (current_palette_ > 0) {
--current_palette_;
}
else
{
} else {
current_palette_ = static_cast<Uint8>(palettes_.size() - 1);
}
@@ -344,8 +308,7 @@ void Screen::previousPalette()
}
// Establece la paleta
void Screen::setPalete()
{
void Screen::setPalete() {
game_surface_->loadPalette(Resource::get()->getPalette(palettes_.at(current_palette_)));
border_surface_->loadPalette(Resource::get()->getPalette(palettes_.at(current_palette_)));
@@ -353,8 +316,7 @@ void Screen::setPalete()
// Eliminar ".gif"
size_t pos = options.video.palette.find(".pal");
if (pos != std::string::npos)
{
if (pos != std::string::npos) {
options.video.palette.erase(pos, 4);
}
@@ -363,42 +325,32 @@ void Screen::setPalete()
}
// Extrae los nombres de las paletas
void Screen::processPaletteList()
{
for (auto &palette : palettes_)
{
void Screen::processPaletteList() {
for (auto& palette : palettes_) {
palette = getFileName(palette);
}
}
// Copia la surface a la textura
void Screen::surfaceToTexture()
{
if (options.video.border.enabled)
{
void Screen::surfaceToTexture() {
if (options.video.border.enabled) {
border_surface_->copyToTexture(renderer_, border_texture_);
game_surface_->copyToTexture(renderer_, border_texture_, nullptr, &game_surface_dstrect_);
}
else
{
} else {
game_surface_->copyToTexture(renderer_, game_texture_);
}
}
// Copia la textura al renderizador
void Screen::textureToRenderer()
{
SDL_Texture *texture_to_render = options.video.border.enabled ? border_texture_ : game_texture_;
void Screen::textureToRenderer() {
SDL_Texture* texture_to_render = options.video.border.enabled ? border_texture_ : game_texture_;
if (options.video.shaders)
{
if (options.video.shaders) {
SDL_SetRenderTarget(renderer_, shaders_texture_);
SDL_RenderCopy(renderer_, texture_to_render, nullptr, nullptr);
SDL_SetRenderTarget(renderer_, nullptr);
shader::render();
}
else
{
} else {
SDL_SetRenderTarget(renderer_, nullptr);
SDL_SetRenderDrawColor(renderer_, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderClear(renderer_);
@@ -408,21 +360,17 @@ void Screen::textureToRenderer()
}
// Renderiza todos los overlays
void Screen::renderOverlays()
{
void Screen::renderOverlays() {
renderNotifications();
renderInfo();
}
// Localiza la paleta dentro del vector de paletas
size_t Screen::findPalette(const std::string &name)
{
size_t Screen::findPalette(const std::string& name) {
std::string upper_name = toUpper(name + ".pal");
for (size_t i = 0; i < palettes_.size(); ++i)
{
if (toUpper(getFileName(palettes_[i])) == upper_name)
{
for (size_t i = 0; i < palettes_.size(); ++i) {
if (toUpper(getFileName(palettes_[i])) == upper_name) {
return i;
}
}
@@ -430,10 +378,8 @@ size_t Screen::findPalette(const std::string &name)
}
// Recrea la textura para los shaders
void Screen::createShadersTexture()
{
if (shaders_texture_)
{
void Screen::createShadersTexture() {
if (shaders_texture_) {
SDL_DestroyTexture(shaders_texture_);
}
@@ -441,21 +387,17 @@ void Screen::createShadersTexture()
const int EXTRA_WIDTH = options.video.border.enabled ? options.video.border.width * 2 : 0;
const int EXTRA_HEIGHT = options.video.border.enabled ? options.video.border.height * 2 : 0;
shaders_texture_ = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, options.game.width + EXTRA_WIDTH, options.game.height + EXTRA_HEIGHT);
if (!shaders_texture_)
{
if (!shaders_texture_) {
// Registrar el error si está habilitado
if (options.console)
{
if (options.console) {
std::cerr << "Error: shaders_texture_ could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
}
}
}
// Muestra información por pantalla
void Screen::renderInfo()
{
if (show_debug_info_ && Resource::get())
{
void Screen::renderInfo() {
if (show_debug_info_ && Resource::get()) {
auto text = Resource::get()->getText("smb2");
auto color = static_cast<Uint8>(PaletteColor::YELLOW);
@@ -493,6 +435,6 @@ void Screen::setNotificationsEnabled(bool value) { notifications_enabled_ = valu
void Screen::toggleDebugInfo() { show_debug_info_ = !show_debug_info_; }
// Getters
SDL_Renderer *Screen::getRenderer() { return renderer_; }
SDL_Renderer* Screen::getRenderer() { return renderer_; }
std::shared_ptr<Surface> Screen::getRendererSurface() { return (*renderer_surface_); }
std::shared_ptr<Surface> Screen::getBorderSurface() { return border_surface_; }