forked from jaildesigner-jailgames/jaildoctors_dilemma
linter
This commit is contained in:
@@ -20,7 +20,8 @@ void Gif::decompress(int code_length, const uint8_t* input, int input_length, ui
|
||||
throw std::runtime_error("Invalid LZW code length");
|
||||
}
|
||||
|
||||
int i, bit;
|
||||
int i;
|
||||
int bit;
|
||||
int prev = -1;
|
||||
std::vector<DictionaryEntry> dictionary;
|
||||
int dictionary_ind;
|
||||
@@ -69,7 +70,8 @@ void Gif::decompress(int code_length, const uint8_t* input, int input_length, ui
|
||||
dictionary_ind += 2;
|
||||
prev = -1;
|
||||
continue;
|
||||
} else if (code == stop_code) {
|
||||
}
|
||||
if (code == stop_code) {
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -83,13 +85,15 @@ void Gif::decompress(int code_length, const uint8_t* input, int input_length, ui
|
||||
int ptr;
|
||||
if (code == dictionary_ind) {
|
||||
ptr = prev;
|
||||
while (dictionary[ptr].prev != -1)
|
||||
while (dictionary[ptr].prev != -1) {
|
||||
ptr = dictionary[ptr].prev;
|
||||
}
|
||||
dictionary[dictionary_ind].byte = dictionary[ptr].byte;
|
||||
} else {
|
||||
ptr = code;
|
||||
while (dictionary[ptr].prev != -1)
|
||||
while (dictionary[ptr].prev != -1) {
|
||||
ptr = dictionary[ptr].prev;
|
||||
}
|
||||
dictionary[dictionary_ind].byte = dictionary[ptr].byte;
|
||||
}
|
||||
dictionary[dictionary_ind].prev = prev;
|
||||
@@ -111,16 +115,16 @@ void Gif::decompress(int code_length, const uint8_t* input, int input_length, ui
|
||||
throw std::runtime_error("LZW error: invalid code encountered");
|
||||
}
|
||||
|
||||
int curCode = code; // Variable temporal para recorrer la cadena.
|
||||
match_len = dictionary[curCode].len;
|
||||
while (curCode != -1) {
|
||||
int cur_code = code; // Variable temporal para recorrer la cadena.
|
||||
match_len = dictionary[cur_code].len;
|
||||
while (cur_code != -1) {
|
||||
// Se asume que dictionary[curCode].len > 0.
|
||||
out[dictionary[curCode].len - 1] = dictionary[curCode].byte;
|
||||
if (dictionary[curCode].prev == curCode) {
|
||||
out[dictionary[cur_code].len - 1] = dictionary[cur_code].byte;
|
||||
if (dictionary[cur_code].prev == cur_code) {
|
||||
std::cerr << "Internal error; self-reference detected." << std::endl;
|
||||
throw std::runtime_error("Internal error in decompress: self-reference");
|
||||
}
|
||||
curCode = dictionary[curCode].prev;
|
||||
cur_code = dictionary[cur_code].prev;
|
||||
}
|
||||
out += match_len;
|
||||
}
|
||||
@@ -165,7 +169,7 @@ std::vector<uint32_t> Gif::loadPalette(const uint8_t* buffer) {
|
||||
buffer += sizeof(ScreenDescriptor);
|
||||
|
||||
std::vector<uint32_t> global_color_table;
|
||||
if (screen_descriptor.fields & 0x80) {
|
||||
if ((screen_descriptor.fields & 0x80) != 0) {
|
||||
int global_color_table_size = 1 << (((screen_descriptor.fields & 0x07) + 1));
|
||||
global_color_table.resize(global_color_table_size);
|
||||
for (int i = 0; i < global_color_table_size; ++i) {
|
||||
@@ -186,8 +190,8 @@ std::vector<uint8_t> Gif::processGifStream(const uint8_t* buffer, uint16_t& w, u
|
||||
buffer += 6;
|
||||
|
||||
// Opcional: Validar header
|
||||
std::string headerStr(reinterpret_cast<char*>(header), 6);
|
||||
if (headerStr != "GIF87a" && headerStr != "GIF89a") {
|
||||
std::string header_str(reinterpret_cast<char*>(header), 6);
|
||||
if (header_str != "GIF87a" && header_str != "GIF89a") {
|
||||
throw std::runtime_error("Formato de archivo GIF inválido.");
|
||||
}
|
||||
|
||||
@@ -201,7 +205,7 @@ std::vector<uint8_t> Gif::processGifStream(const uint8_t* buffer, uint16_t& w, u
|
||||
|
||||
int color_resolution_bits = ((screen_descriptor.fields & 0x70) >> 4) + 1;
|
||||
std::vector<RGB> global_color_table;
|
||||
if (screen_descriptor.fields & 0x80) {
|
||||
if ((screen_descriptor.fields & 0x80) != 0) {
|
||||
int global_color_table_size = 1 << (((screen_descriptor.fields & 0x07) + 1));
|
||||
global_color_table.resize(global_color_table_size);
|
||||
std::memcpy(global_color_table.data(), buffer, 3 * global_color_table_size);
|
||||
@@ -219,13 +223,13 @@ std::vector<uint8_t> Gif::processGifStream(const uint8_t* buffer, uint16_t& w, u
|
||||
case GRAPHIC_CONTROL: // 0xF9
|
||||
{
|
||||
// Procesar Graphic Control Extension:
|
||||
uint8_t blockSize = *buffer++; // Normalmente, blockSize == 4
|
||||
buffer += blockSize; // Saltamos los 4 bytes del bloque fijo
|
||||
uint8_t block_size = *buffer++; // Normalmente, blockSize == 4
|
||||
buffer += block_size; // Saltamos los 4 bytes del bloque fijo
|
||||
// Saltar los sub-bloques
|
||||
uint8_t subBlockSize = *buffer++;
|
||||
while (subBlockSize != 0) {
|
||||
buffer += subBlockSize;
|
||||
subBlockSize = *buffer++;
|
||||
uint8_t sub_block_size = *buffer++;
|
||||
while (sub_block_size != 0) {
|
||||
buffer += sub_block_size;
|
||||
sub_block_size = *buffer++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -234,23 +238,23 @@ std::vector<uint8_t> Gif::processGifStream(const uint8_t* buffer, uint16_t& w, u
|
||||
case PLAINTEXT_EXTENSION: // 0x01
|
||||
{
|
||||
// Para estas extensiones, saltamos el bloque fijo y los sub-bloques.
|
||||
uint8_t blockSize = *buffer++;
|
||||
buffer += blockSize;
|
||||
uint8_t subBlockSize = *buffer++;
|
||||
while (subBlockSize != 0) {
|
||||
buffer += subBlockSize;
|
||||
subBlockSize = *buffer++;
|
||||
uint8_t block_size = *buffer++;
|
||||
buffer += block_size;
|
||||
uint8_t sub_block_size = *buffer++;
|
||||
while (sub_block_size != 0) {
|
||||
buffer += sub_block_size;
|
||||
sub_block_size = *buffer++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
// Si la etiqueta de extensión es desconocida, saltarla también:
|
||||
uint8_t blockSize = *buffer++;
|
||||
buffer += blockSize;
|
||||
uint8_t subBlockSize = *buffer++;
|
||||
while (subBlockSize != 0) {
|
||||
buffer += subBlockSize;
|
||||
subBlockSize = *buffer++;
|
||||
uint8_t block_size = *buffer++;
|
||||
buffer += block_size;
|
||||
uint8_t sub_block_size = *buffer++;
|
||||
while (sub_block_size != 0) {
|
||||
buffer += sub_block_size;
|
||||
sub_block_size = *buffer++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -68,11 +68,11 @@ class Gif {
|
||||
public:
|
||||
// Descompone (uncompress) el bloque comprimido usando LZW.
|
||||
// Este método puede lanzar std::runtime_error en caso de error.
|
||||
void decompress(int code_length, const uint8_t* input, int input_length, uint8_t* out);
|
||||
static void decompress(int code_length, const uint8_t* input, int input_length, uint8_t* out);
|
||||
|
||||
// Carga la paleta (global color table) a partir de un buffer,
|
||||
// retornándola en un vector de uint32_t (cada color se compone de R, G, B).
|
||||
std::vector<uint32_t> loadPalette(const uint8_t* buffer);
|
||||
static std::vector<uint32_t> loadPalette(const uint8_t* buffer);
|
||||
|
||||
// Carga el stream GIF; devuelve un vector con los datos de imagen sin comprimir y
|
||||
// asigna el ancho y alto mediante referencias.
|
||||
@@ -80,7 +80,7 @@ class Gif {
|
||||
|
||||
private:
|
||||
// Lee los sub-bloques de datos y los acumula en un std::vector<uint8_t>.
|
||||
std::vector<uint8_t> readSubBlocks(const uint8_t*& buffer);
|
||||
static std::vector<uint8_t> readSubBlocks(const uint8_t*& buffer);
|
||||
|
||||
// Procesa el Image Descriptor y retorna el vector de datos sin comprimir.
|
||||
std::vector<uint8_t> processImageDescriptor(const uint8_t*& buffer, const std::vector<RGB>& gct, int resolution_bits);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
@@ -40,13 +41,13 @@ bool OpenGLShader::initGLExtensions() {
|
||||
glVertexAttribPointer = (PFNGLVERTEXATTRIBPOINTERPROC)SDL_GL_GetProcAddress("glVertexAttribPointer");
|
||||
glEnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYPROC)SDL_GL_GetProcAddress("glEnableVertexAttribArray");
|
||||
|
||||
return glCreateShader && glShaderSource && glCompileShader && glGetShaderiv &&
|
||||
glGetShaderInfoLog && glDeleteShader && glAttachShader && glCreateProgram &&
|
||||
glLinkProgram && glValidateProgram && glGetProgramiv && glGetProgramInfoLog &&
|
||||
glUseProgram && glDeleteProgram && glGetUniformLocation && glUniform2f &&
|
||||
glGenVertexArrays && glBindVertexArray && glDeleteVertexArrays &&
|
||||
glGenBuffers && glBindBuffer && glBufferData && glDeleteBuffers &&
|
||||
glVertexAttribPointer && glEnableVertexAttribArray;
|
||||
return (glCreateShader != nullptr) && (glShaderSource != nullptr) && (glCompileShader != nullptr) && (glGetShaderiv != nullptr) &&
|
||||
(glGetShaderInfoLog != nullptr) && (glDeleteShader != nullptr) && (glAttachShader != nullptr) && (glCreateProgram != nullptr) &&
|
||||
(glLinkProgram != nullptr) && (glValidateProgram != nullptr) && (glGetProgramiv != nullptr) && (glGetProgramInfoLog != nullptr) &&
|
||||
(glUseProgram != nullptr) && (glDeleteProgram != nullptr) && (glGetUniformLocation != nullptr) && (glUniform2f != nullptr) &&
|
||||
(glGenVertexArrays != nullptr) && (glBindVertexArray != nullptr) && (glDeleteVertexArrays != nullptr) &&
|
||||
(glGenBuffers != nullptr) && (glBindBuffer != nullptr) && (glBufferData != nullptr) && (glDeleteBuffers != nullptr) &&
|
||||
(glVertexAttribPointer != nullptr) && (glEnableVertexAttribArray != nullptr);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -149,22 +150,22 @@ void OpenGLShader::createQuadGeometry() {
|
||||
// Formato: x, y, u, v
|
||||
float vertices[] = {
|
||||
// Posición // TexCoords
|
||||
-1.0f,
|
||||
-1.0f,
|
||||
0.0f,
|
||||
0.0f, // Inferior izquierda
|
||||
1.0f,
|
||||
-1.0f,
|
||||
1.0f,
|
||||
0.0f, // Inferior derecha
|
||||
1.0f,
|
||||
1.0f,
|
||||
1.0f,
|
||||
1.0f, // Superior derecha
|
||||
-1.0f,
|
||||
1.0f,
|
||||
0.0f,
|
||||
1.0f // Superior izquierda
|
||||
-1.0F,
|
||||
-1.0F,
|
||||
0.0F,
|
||||
0.0F, // Inferior izquierda
|
||||
1.0F,
|
||||
-1.0F,
|
||||
1.0F,
|
||||
0.0F, // Inferior derecha
|
||||
1.0F,
|
||||
1.0F,
|
||||
1.0F,
|
||||
1.0F, // Superior derecha
|
||||
-1.0F,
|
||||
1.0F,
|
||||
0.0F,
|
||||
1.0F // Superior izquierda
|
||||
};
|
||||
|
||||
// Índices para dibujar el quad con dos triángulos
|
||||
@@ -210,7 +211,9 @@ void OpenGLShader::createQuadGeometry() {
|
||||
}
|
||||
|
||||
GLuint OpenGLShader::getTextureID(SDL_Texture* texture) {
|
||||
if (!texture) return 1;
|
||||
if (texture == nullptr) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
SDL_PropertiesID props = SDL_GetTextureProperties(texture);
|
||||
GLuint texture_id = 0;
|
||||
@@ -243,7 +246,7 @@ bool OpenGLShader::init(SDL_Window* window,
|
||||
back_buffer_ = texture;
|
||||
renderer_ = SDL_GetRenderer(window);
|
||||
|
||||
if (!renderer_) {
|
||||
if (renderer_ == nullptr) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
||||
"Error: No se pudo obtener el renderer");
|
||||
return false;
|
||||
@@ -262,10 +265,10 @@ bool OpenGLShader::init(SDL_Window* window,
|
||||
|
||||
// Verificar que es OpenGL
|
||||
const char* renderer_name = SDL_GetRendererName(renderer_);
|
||||
if (!renderer_name || strncmp(renderer_name, "opengl", 6) != 0) {
|
||||
if ((renderer_name == nullptr) || strncmp(renderer_name, "opengl", 6) != 0) {
|
||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
||||
"Renderer no es OpenGL: %s",
|
||||
renderer_name ? renderer_name : "unknown");
|
||||
(renderer_name != nullptr) ? renderer_name : "unknown");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -291,8 +294,12 @@ bool OpenGLShader::init(SDL_Window* window,
|
||||
if (vertex_shader == 0 || fragment_shader == 0) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
||||
"Error al compilar shaders");
|
||||
if (vertex_shader != 0) glDeleteShader(vertex_shader);
|
||||
if (fragment_shader != 0) glDeleteShader(fragment_shader);
|
||||
if (vertex_shader != 0) {
|
||||
glDeleteShader(vertex_shader);
|
||||
}
|
||||
if (fragment_shader != 0) {
|
||||
glDeleteShader(fragment_shader);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -347,7 +354,8 @@ void OpenGLShader::render() {
|
||||
}
|
||||
|
||||
// Obtener tamaño actual de ventana (puede haber cambiado)
|
||||
int current_width, current_height;
|
||||
int current_width;
|
||||
int current_height;
|
||||
SDL_GetWindowSize(window_, ¤t_width, ¤t_height);
|
||||
|
||||
// Guardar estados OpenGL
|
||||
@@ -380,7 +388,8 @@ void OpenGLShader::render() {
|
||||
checkGLError("glUseProgram");
|
||||
|
||||
// Configurar viewport (obtener tamaño lógico de SDL)
|
||||
int logical_w, logical_h;
|
||||
int logical_w;
|
||||
int logical_h;
|
||||
SDL_RendererLogicalPresentation mode;
|
||||
SDL_GetRenderLogicalPresentation(renderer_, &logical_w, &logical_h, &mode);
|
||||
|
||||
@@ -390,14 +399,16 @@ void OpenGLShader::render() {
|
||||
}
|
||||
|
||||
// Calcular viewport considerando aspect ratio
|
||||
int viewport_x = 0, viewport_y = 0;
|
||||
int viewport_w = current_width, viewport_h = current_height;
|
||||
int viewport_x = 0;
|
||||
int viewport_y = 0;
|
||||
int viewport_w = current_width;
|
||||
int viewport_h = current_height;
|
||||
|
||||
if (mode == SDL_LOGICAL_PRESENTATION_INTEGER_SCALE) {
|
||||
int scale_x = current_width / logical_w;
|
||||
int scale_y = current_height / logical_h;
|
||||
int scale = (scale_x < scale_y) ? scale_x : scale_y;
|
||||
if (scale < 1) scale = 1;
|
||||
scale = std::max(scale, 1);
|
||||
|
||||
viewport_w = logical_w * scale;
|
||||
viewport_h = logical_h * scale;
|
||||
@@ -430,7 +441,7 @@ void OpenGLShader::render() {
|
||||
// Restaurar estados OpenGL
|
||||
glUseProgram(old_program);
|
||||
glBindTexture(GL_TEXTURE_2D, old_texture);
|
||||
if (!was_texture_enabled) {
|
||||
if (was_texture_enabled == 0u) {
|
||||
glDisable(GL_TEXTURE_2D);
|
||||
}
|
||||
glBindVertexArray(old_vao);
|
||||
|
||||
@@ -39,8 +39,8 @@ class OpenGLShader : public ShaderBackend {
|
||||
GLuint compileShader(const std::string& source, GLenum shader_type);
|
||||
GLuint linkProgram(GLuint vertex_shader, GLuint fragment_shader);
|
||||
void createQuadGeometry();
|
||||
GLuint getTextureID(SDL_Texture* texture);
|
||||
void checkGLError(const char* operation);
|
||||
static GLuint getTextureID(SDL_Texture* texture);
|
||||
static void checkGLError(const char* operation);
|
||||
|
||||
// Estado SDL
|
||||
SDL_Window* window_ = nullptr;
|
||||
@@ -59,13 +59,14 @@ class OpenGLShader : public ShaderBackend {
|
||||
// Tamaños
|
||||
int window_width_ = 0;
|
||||
int window_height_ = 0;
|
||||
float texture_width_ = 0.0f;
|
||||
float texture_height_ = 0.0f;
|
||||
float texture_width_ = 0.0F;
|
||||
float texture_height_ = 0.0F;
|
||||
|
||||
// Estado
|
||||
bool is_initialized_ = false;
|
||||
|
||||
#ifndef __APPLE__
|
||||
// NOLINTBEGIN
|
||||
// Punteros a funciones OpenGL en Windows/Linux
|
||||
PFNGLCREATESHADERPROC glCreateShader = nullptr;
|
||||
PFNGLSHADERSOURCEPROC glShaderSource = nullptr;
|
||||
@@ -92,6 +93,7 @@ class OpenGLShader : public ShaderBackend {
|
||||
PFNGLDELETEBUFFERSPROC glDeleteBuffers = nullptr;
|
||||
PFNGLVERTEXATTRIBPOINTERPROC glVertexAttribPointer = nullptr;
|
||||
PFNGLENABLEVERTEXATTRIBARRAYPROC glEnableVertexAttribArray = nullptr;
|
||||
// NOLINTEND
|
||||
#endif
|
||||
};
|
||||
|
||||
|
||||
@@ -19,21 +19,21 @@
|
||||
#include "game/ui/notifier.hpp" // Para Notifier
|
||||
|
||||
// [SINGLETON]
|
||||
Screen* Screen::screen_ = nullptr;
|
||||
Screen* Screen::screen = nullptr;
|
||||
|
||||
// [SINGLETON] Crearemos el objeto con esta función estática
|
||||
void Screen::init() {
|
||||
Screen::screen_ = new Screen();
|
||||
Screen::screen = new Screen();
|
||||
}
|
||||
|
||||
// [SINGLETON] Destruiremos el objeto con esta función estática
|
||||
void Screen::destroy() {
|
||||
delete Screen::screen_;
|
||||
delete Screen::screen;
|
||||
}
|
||||
|
||||
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
|
||||
Screen* Screen::get() {
|
||||
return Screen::screen_;
|
||||
return Screen::screen;
|
||||
}
|
||||
|
||||
// Constructor
|
||||
@@ -332,7 +332,7 @@ void Screen::renderInfo() {
|
||||
auto color = static_cast<Uint8>(PaletteColor::YELLOW);
|
||||
|
||||
// FPS
|
||||
const std::string FPS_TEXT = std::to_string(fps_.lastValue) + " FPS";
|
||||
const std::string FPS_TEXT = std::to_string(fps_.last_value) + " FPS";
|
||||
text->writeColored(Options::game.width - text->lenght(FPS_TEXT), 0, FPS_TEXT, color);
|
||||
|
||||
// Resolution
|
||||
|
||||
@@ -33,34 +33,34 @@ class Screen {
|
||||
|
||||
struct FPS {
|
||||
Uint32 ticks; // Tiempo en milisegundos desde que se comenzó a contar.
|
||||
int frameCount; // Número acumulado de frames en el intervalo.
|
||||
int lastValue; // Número de frames calculado en el último segundo.
|
||||
int frame_count; // Número acumulado de frames en el intervalo.
|
||||
int last_value; // Número de frames calculado en el último segundo.
|
||||
|
||||
// Constructor para inicializar la estructura.
|
||||
FPS()
|
||||
: ticks(0),
|
||||
frameCount(0),
|
||||
lastValue(0) {}
|
||||
frame_count(0),
|
||||
last_value(0) {}
|
||||
|
||||
// Incrementador que se llama en cada frame.
|
||||
void increment() {
|
||||
frameCount++;
|
||||
frame_count++;
|
||||
}
|
||||
|
||||
// Método para calcular y devolver el valor de FPS.
|
||||
int calculate(Uint32 currentTicks) {
|
||||
if (currentTicks - ticks >= 1000) // Si ha pasado un segundo o más.
|
||||
int calculate(Uint32 current_ticks) {
|
||||
if (current_ticks - ticks >= 1000) // Si ha pasado un segundo o más.
|
||||
{
|
||||
lastValue = frameCount; // Actualizamos el valor del último FPS.
|
||||
frameCount = 0; // Reiniciamos el contador de frames.
|
||||
ticks = currentTicks; // Actualizamos el tiempo base.
|
||||
last_value = frame_count; // Actualizamos el valor del último FPS.
|
||||
frame_count = 0; // Reiniciamos el contador de frames.
|
||||
ticks = current_ticks; // Actualizamos el tiempo base.
|
||||
}
|
||||
return lastValue;
|
||||
return last_value;
|
||||
}
|
||||
};
|
||||
|
||||
// [SINGLETON] Objeto privado
|
||||
static Screen* screen_;
|
||||
static Screen* screen;
|
||||
|
||||
// Objetos y punteros
|
||||
SDL_Window* window_; // Ventana de la aplicación
|
||||
|
||||
@@ -75,7 +75,9 @@ Palette readPalFile(const std::string& file_path) {
|
||||
|
||||
// Procesar las líneas restantes con valores RGB
|
||||
std::istringstream ss(line);
|
||||
int r, g, b;
|
||||
int r;
|
||||
int g;
|
||||
int b;
|
||||
if (ss >> r >> g >> b) {
|
||||
// Construir el color ARGB (A = 255 por defecto)
|
||||
Uint32 color = (255 << 24) | (r << 16) | (g << 8) | b;
|
||||
@@ -99,8 +101,8 @@ Surface::Surface(int w, int h)
|
||||
|
||||
Surface::Surface(const std::string& file_path)
|
||||
: transparent_color_(static_cast<Uint8>(PaletteColor::TRANSPARENT)) {
|
||||
SurfaceData loadedData = loadSurface(file_path);
|
||||
surface_data_ = std::make_shared<SurfaceData>(std::move(loadedData));
|
||||
SurfaceData loaded_data = loadSurface(file_path);
|
||||
surface_data_ = std::make_shared<SurfaceData>(std::move(loaded_data));
|
||||
|
||||
initializeSubPalette(sub_palette_);
|
||||
}
|
||||
@@ -127,18 +129,19 @@ SurfaceData Surface::loadSurface(const std::string& file_path) {
|
||||
|
||||
// Crear un objeto Gif y llamar a la función loadGif
|
||||
GIF::Gif gif;
|
||||
Uint16 w = 0, h = 0;
|
||||
std::vector<Uint8> rawPixels = gif.loadGif(buffer.data(), w, h);
|
||||
if (rawPixels.empty()) {
|
||||
Uint16 w = 0;
|
||||
Uint16 h = 0;
|
||||
std::vector<Uint8> raw_pixels = gif.loadGif(buffer.data(), w, h);
|
||||
if (raw_pixels.empty()) {
|
||||
std::cerr << "Error loading GIF from file: " << file_path << std::endl;
|
||||
throw std::runtime_error("Error loading GIF");
|
||||
}
|
||||
|
||||
// Si el constructor de Surface espera un std::shared_ptr<Uint8[]>,
|
||||
// reservamos un bloque dinámico y copiamos los datos del vector.
|
||||
size_t pixelCount = rawPixels.size();
|
||||
auto pixels = std::shared_ptr<Uint8[]>(new Uint8[pixelCount], std::default_delete<Uint8[]>());
|
||||
std::memcpy(pixels.get(), rawPixels.data(), pixelCount);
|
||||
size_t pixel_count = raw_pixels.size();
|
||||
auto pixels = std::shared_ptr<Uint8[]>(new Uint8[pixel_count], std::default_delete<Uint8[]>());
|
||||
std::memcpy(pixels.get(), raw_pixels.data(), pixel_count);
|
||||
|
||||
// Crear y devolver directamente el objeto SurfaceData
|
||||
printWithDots("Surface : ", file_path.substr(file_path.find_last_of("\\/") + 1), "[ LOADED ]");
|
||||
@@ -162,9 +165,9 @@ void Surface::setColor(int index, Uint32 color) {
|
||||
|
||||
// Rellena la superficie con un color
|
||||
void Surface::clear(Uint8 color) {
|
||||
const size_t total_pixels = surface_data_->width * surface_data_->height;
|
||||
const size_t TOTAL_PIXELS = surface_data_->width * surface_data_->height;
|
||||
Uint8* data_ptr = surface_data_->data.get();
|
||||
std::fill(data_ptr, data_ptr + total_pixels, color);
|
||||
std::fill(data_ptr, data_ptr + TOTAL_PIXELS, color);
|
||||
}
|
||||
|
||||
// Pone un pixel en la SurfaceData
|
||||
@@ -173,12 +176,12 @@ void Surface::putPixel(int x, int y, Uint8 color) {
|
||||
return; // Coordenadas fuera de rango
|
||||
}
|
||||
|
||||
const int index = x + y * surface_data_->width;
|
||||
surface_data_->data.get()[index] = color;
|
||||
const int INDEX = x + (y * surface_data_->width);
|
||||
surface_data_->data.get()[INDEX] = color;
|
||||
}
|
||||
|
||||
// Obtiene el color de un pixel de la surface_data
|
||||
Uint8 Surface::getPixel(int x, int y) { return surface_data_->data.get()[x + y * static_cast<int>(surface_data_->width)]; }
|
||||
Uint8 Surface::getPixel(int x, int y) { return surface_data_->data.get()[x + (y * static_cast<int>(surface_data_->width))]; }
|
||||
|
||||
// Dibuja un rectangulo relleno
|
||||
void Surface::fillRect(const SDL_FRect* rect, Uint8 color) {
|
||||
@@ -191,7 +194,7 @@ void Surface::fillRect(const SDL_FRect* rect, Uint8 color) {
|
||||
// Recorrer cada píxel dentro del rectángulo directamente
|
||||
for (int y = y_start; y < y_end; ++y) {
|
||||
for (int x = x_start; x < x_end; ++x) {
|
||||
const int INDEX = x + y * surface_data_->width;
|
||||
const int INDEX = x + (y * surface_data_->width);
|
||||
surface_data_->data.get()[INDEX] = color;
|
||||
}
|
||||
}
|
||||
@@ -208,22 +211,22 @@ void Surface::drawRectBorder(const SDL_FRect* rect, Uint8 color) {
|
||||
// Dibujar bordes horizontales
|
||||
for (int x = x_start; x < x_end; ++x) {
|
||||
// Borde superior
|
||||
const int top_index = x + y_start * surface_data_->width;
|
||||
surface_data_->data.get()[top_index] = color;
|
||||
const int TOP_INDEX = x + (y_start * surface_data_->width);
|
||||
surface_data_->data.get()[TOP_INDEX] = color;
|
||||
|
||||
// Borde inferior
|
||||
const int bottom_index = x + (y_end - 1) * surface_data_->width;
|
||||
surface_data_->data.get()[bottom_index] = color;
|
||||
const int BOTTOM_INDEX = x + ((y_end - 1) * surface_data_->width);
|
||||
surface_data_->data.get()[BOTTOM_INDEX] = color;
|
||||
}
|
||||
|
||||
// Dibujar bordes verticales
|
||||
for (int y = y_start; y < y_end; ++y) {
|
||||
// Borde izquierdo
|
||||
const int LEFT_INDEX = x_start + y * surface_data_->width;
|
||||
const int LEFT_INDEX = x_start + (y * surface_data_->width);
|
||||
surface_data_->data.get()[LEFT_INDEX] = color;
|
||||
|
||||
// Borde derecho
|
||||
const int RIGHT_INDEX = (x_end - 1) + y * surface_data_->width;
|
||||
const int RIGHT_INDEX = (x_end - 1) + (y * surface_data_->width);
|
||||
surface_data_->data.get()[RIGHT_INDEX] = color;
|
||||
}
|
||||
}
|
||||
@@ -243,12 +246,13 @@ void Surface::drawLine(float x1, float y1, float x2, float y2, Uint8 color) {
|
||||
while (true) {
|
||||
// Asegúrate de no dibujar fuera de los límites de la superficie
|
||||
if (x1 >= 0 && x1 < surface_data_->width && y1 >= 0 && y1 < surface_data_->height) {
|
||||
surface_data_->data.get()[static_cast<size_t>(x1 + y1 * surface_data_->width)] = color;
|
||||
surface_data_->data.get()[static_cast<size_t>(x1 + (y1 * surface_data_->width))] = color;
|
||||
}
|
||||
|
||||
// Si alcanzamos el punto final, salimos
|
||||
if (x1 == x2 && y1 == y2)
|
||||
if (x1 == x2 && y1 == y2) {
|
||||
break;
|
||||
}
|
||||
|
||||
int e2 = 2 * err;
|
||||
if (e2 > -dy) {
|
||||
@@ -281,9 +285,9 @@ void Surface::render(float dx, float dy, float sx, float sy, float w, float h) {
|
||||
int src_x = sx + ix;
|
||||
int src_y = sy + iy;
|
||||
|
||||
Uint8 color = surface_data_->data.get()[static_cast<size_t>(src_x + src_y * surface_data_->width)];
|
||||
Uint8 color = surface_data_->data.get()[static_cast<size_t>(src_x + (src_y * surface_data_->width))];
|
||||
if (color != transparent_color_) {
|
||||
surface_data->data.get()[static_cast<size_t>(dest_x + dest_y * surface_data->width)] = sub_palette_[color];
|
||||
surface_data->data.get()[static_cast<size_t>(dest_x + (dest_y * surface_data->width))] = sub_palette_[color];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -291,14 +295,14 @@ void Surface::render(float dx, float dy, float sx, float sy, float w, float h) {
|
||||
}
|
||||
}
|
||||
|
||||
void Surface::render(int x, int y, SDL_FRect* srcRect, SDL_FlipMode flip) {
|
||||
void Surface::render(int x, int y, SDL_FRect* src_rect, SDL_FlipMode flip) {
|
||||
auto surface_data_dest = Screen::get()->getRendererSurface()->getSurfaceData();
|
||||
|
||||
// Determina la región de origen (clip) a renderizar
|
||||
float sx = (srcRect) ? srcRect->x : 0;
|
||||
float sy = (srcRect) ? srcRect->y : 0;
|
||||
float w = (srcRect) ? srcRect->w : surface_data_->width;
|
||||
float h = (srcRect) ? srcRect->h : surface_data_->height;
|
||||
float sx = ((src_rect) != nullptr) ? src_rect->x : 0;
|
||||
float sy = ((src_rect) != nullptr) ? src_rect->y : 0;
|
||||
float w = ((src_rect) != nullptr) ? src_rect->w : surface_data_->width;
|
||||
float h = ((src_rect) != nullptr) ? src_rect->h : surface_data_->height;
|
||||
|
||||
// Limitar la región para evitar accesos fuera de rango en origen
|
||||
w = std::min(w, surface_data_->width - sx);
|
||||
@@ -324,9 +328,9 @@ void Surface::render(int x, int y, SDL_FRect* srcRect, SDL_FlipMode flip) {
|
||||
// Verificar que las coordenadas de destino están dentro de los límites
|
||||
if (dest_x >= 0 && dest_x < surface_data_dest->width && dest_y >= 0 && dest_y < surface_data_dest->height) {
|
||||
// Copia el píxel si no es transparente
|
||||
Uint8 color = surface_data_->data.get()[static_cast<size_t>(src_x + src_y * surface_data_->width)];
|
||||
Uint8 color = surface_data_->data.get()[static_cast<size_t>(src_x + (src_y * surface_data_->width))];
|
||||
if (color != transparent_color_) {
|
||||
surface_data_dest->data[dest_x + dest_y * surface_data_dest->width] = sub_palette_[color];
|
||||
surface_data_dest->data[dest_x + (dest_y * surface_data_dest->width)] = sub_palette_[color];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -334,20 +338,20 @@ void Surface::render(int x, int y, SDL_FRect* srcRect, SDL_FlipMode flip) {
|
||||
}
|
||||
|
||||
// Copia una región de la superficie de origen a la de destino
|
||||
void Surface::render(SDL_FRect* srcRect, SDL_FRect* dstRect, SDL_FlipMode flip) {
|
||||
void Surface::render(SDL_FRect* src_rect, SDL_FRect* dst_rect, SDL_FlipMode flip) {
|
||||
auto surface_data = Screen::get()->getRendererSurface()->getSurfaceData();
|
||||
|
||||
// Si srcRect es nullptr, tomar toda la superficie fuente
|
||||
float sx = (srcRect) ? srcRect->x : 0;
|
||||
float sy = (srcRect) ? srcRect->y : 0;
|
||||
float sw = (srcRect) ? srcRect->w : surface_data_->width;
|
||||
float sh = (srcRect) ? srcRect->h : surface_data_->height;
|
||||
float sx = ((src_rect) != nullptr) ? src_rect->x : 0;
|
||||
float sy = ((src_rect) != nullptr) ? src_rect->y : 0;
|
||||
float sw = ((src_rect) != nullptr) ? src_rect->w : surface_data_->width;
|
||||
float sh = ((src_rect) != nullptr) ? src_rect->h : surface_data_->height;
|
||||
|
||||
// Si dstRect es nullptr, asignar las mismas dimensiones que srcRect
|
||||
float dx = (dstRect) ? dstRect->x : 0;
|
||||
float dy = (dstRect) ? dstRect->y : 0;
|
||||
float dw = (dstRect) ? dstRect->w : sw;
|
||||
float dh = (dstRect) ? dstRect->h : sh;
|
||||
float dx = ((dst_rect) != nullptr) ? dst_rect->x : 0;
|
||||
float dy = ((dst_rect) != nullptr) ? dst_rect->y : 0;
|
||||
float dw = ((dst_rect) != nullptr) ? dst_rect->w : sw;
|
||||
float dh = ((dst_rect) != nullptr) ? dst_rect->h : sh;
|
||||
|
||||
// Asegurarse de que srcRect y dstRect tienen las mismas dimensiones
|
||||
if (sw != dw || sh != dh) {
|
||||
@@ -375,9 +379,9 @@ void Surface::render(SDL_FRect* srcRect, SDL_FRect* dstRect, SDL_FlipMode flip)
|
||||
if (int dest_x = dx + ix; dest_x >= 0 && dest_x < surface_data->width) {
|
||||
if (int dest_y = dy + iy; dest_y >= 0 && dest_y < surface_data->height) {
|
||||
// Copiar el píxel si no es transparente
|
||||
Uint8 color = surface_data_->data.get()[static_cast<size_t>(src_x + src_y * surface_data_->width)];
|
||||
Uint8 color = surface_data_->data.get()[static_cast<size_t>(src_x + (src_y * surface_data_->width))];
|
||||
if (color != transparent_color_) {
|
||||
surface_data->data[dest_x + dest_y * surface_data->width] = sub_palette_[color];
|
||||
surface_data->data[dest_x + (dest_y * surface_data->width)] = sub_palette_[color];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -386,14 +390,14 @@ void Surface::render(SDL_FRect* srcRect, SDL_FRect* dstRect, SDL_FlipMode flip)
|
||||
}
|
||||
|
||||
// Copia una región de la SurfaceData de origen a la SurfaceData de destino reemplazando un color por otro
|
||||
void Surface::renderWithColorReplace(int x, int y, Uint8 source_color, Uint8 target_color, SDL_FRect* srcRect, SDL_FlipMode flip) {
|
||||
void Surface::renderWithColorReplace(int x, int y, Uint8 source_color, Uint8 target_color, SDL_FRect* src_rect, SDL_FlipMode flip) {
|
||||
auto surface_data = Screen::get()->getRendererSurface()->getSurfaceData();
|
||||
|
||||
// Determina la región de origen (clip) a renderizar
|
||||
float sx = (srcRect) ? srcRect->x : 0;
|
||||
float sy = (srcRect) ? srcRect->y : 0;
|
||||
float w = (srcRect) ? srcRect->w : surface_data_->width;
|
||||
float h = (srcRect) ? srcRect->h : surface_data_->height;
|
||||
float sx = ((src_rect) != nullptr) ? src_rect->x : 0;
|
||||
float sy = ((src_rect) != nullptr) ? src_rect->y : 0;
|
||||
float w = ((src_rect) != nullptr) ? src_rect->w : surface_data_->width;
|
||||
float h = ((src_rect) != nullptr) ? src_rect->h : surface_data_->height;
|
||||
|
||||
// Limitar la región para evitar accesos fuera de rango
|
||||
w = std::min(w, surface_data_->width - sx);
|
||||
@@ -416,9 +420,9 @@ void Surface::renderWithColorReplace(int x, int y, Uint8 source_color, Uint8 tar
|
||||
}
|
||||
|
||||
// Copia el píxel si no es transparente
|
||||
Uint8 color = surface_data_->data.get()[static_cast<size_t>(src_x + src_y * surface_data_->width)];
|
||||
Uint8 color = surface_data_->data.get()[static_cast<size_t>(src_x + (src_y * surface_data_->width))];
|
||||
if (color != transparent_color_) {
|
||||
surface_data->data[dest_x + dest_y * surface_data->width] =
|
||||
surface_data->data[dest_x + (dest_y * surface_data->width)] =
|
||||
(color == source_color) ? target_color : color;
|
||||
}
|
||||
}
|
||||
@@ -427,11 +431,11 @@ void Surface::renderWithColorReplace(int x, int y, Uint8 source_color, Uint8 tar
|
||||
|
||||
// Vuelca la superficie a una textura
|
||||
void Surface::copyToTexture(SDL_Renderer* renderer, SDL_Texture* texture) {
|
||||
if (!renderer || !texture || !surface_data_) {
|
||||
if ((renderer == nullptr) || (texture == nullptr) || !surface_data_) {
|
||||
throw std::runtime_error("Renderer or texture is null.");
|
||||
}
|
||||
|
||||
if (surface_data_->width <= 0 || surface_data_->height <= 0 || !surface_data_->data.get()) {
|
||||
if (surface_data_->width <= 0 || surface_data_->height <= 0 || (surface_data_->data.get() == nullptr)) {
|
||||
throw std::runtime_error("Invalid surface dimensions or data.");
|
||||
}
|
||||
|
||||
@@ -449,8 +453,8 @@ void Surface::copyToTexture(SDL_Renderer* renderer, SDL_Texture* texture) {
|
||||
for (int y = 0; y < surface_data_->height; ++y) {
|
||||
for (int x = 0; x < surface_data_->width; ++x) {
|
||||
// Calcular la posición correcta en la textura teniendo en cuenta el stride
|
||||
int texture_index = y * row_stride + x;
|
||||
int surface_index = y * surface_data_->width + x;
|
||||
int texture_index = (y * row_stride) + x;
|
||||
int surface_index = (y * surface_data_->width) + x;
|
||||
|
||||
pixels[texture_index] = palette_[surface_data_->data.get()[surface_index]];
|
||||
}
|
||||
@@ -465,28 +469,28 @@ void Surface::copyToTexture(SDL_Renderer* renderer, SDL_Texture* texture) {
|
||||
}
|
||||
|
||||
// Vuelca la superficie a una textura
|
||||
void Surface::copyToTexture(SDL_Renderer* renderer, SDL_Texture* texture, SDL_FRect* srcRect, SDL_FRect* destRect) {
|
||||
if (!renderer || !texture || !surface_data_) {
|
||||
void Surface::copyToTexture(SDL_Renderer* renderer, SDL_Texture* texture, SDL_FRect* src_rect, SDL_FRect* dest_rect) {
|
||||
if ((renderer == nullptr) || (texture == nullptr) || !surface_data_) {
|
||||
throw std::runtime_error("Renderer or texture is null.");
|
||||
}
|
||||
|
||||
if (surface_data_->width <= 0 || surface_data_->height <= 0 || !surface_data_->data.get()) {
|
||||
if (surface_data_->width <= 0 || surface_data_->height <= 0 || (surface_data_->data.get() == nullptr)) {
|
||||
throw std::runtime_error("Invalid surface dimensions or data.");
|
||||
}
|
||||
|
||||
Uint32* pixels = nullptr;
|
||||
int pitch = 0;
|
||||
|
||||
SDL_Rect lockRect;
|
||||
if (destRect) {
|
||||
lockRect.x = static_cast<int>(destRect->x);
|
||||
lockRect.y = static_cast<int>(destRect->y);
|
||||
lockRect.w = static_cast<int>(destRect->w);
|
||||
lockRect.h = static_cast<int>(destRect->h);
|
||||
SDL_Rect lock_rect;
|
||||
if (dest_rect != nullptr) {
|
||||
lock_rect.x = static_cast<int>(dest_rect->x);
|
||||
lock_rect.y = static_cast<int>(dest_rect->y);
|
||||
lock_rect.w = static_cast<int>(dest_rect->w);
|
||||
lock_rect.h = static_cast<int>(dest_rect->h);
|
||||
}
|
||||
|
||||
// Usa lockRect solo si destRect no es nulo
|
||||
if (!SDL_LockTexture(texture, destRect ? &lockRect : nullptr, reinterpret_cast<void**>(&pixels), &pitch)) {
|
||||
if (!SDL_LockTexture(texture, (dest_rect != nullptr) ? &lock_rect : nullptr, reinterpret_cast<void**>(&pixels), &pitch)) {
|
||||
throw std::runtime_error("Failed to lock texture: " + std::string(SDL_GetError()));
|
||||
}
|
||||
|
||||
@@ -494,8 +498,8 @@ void Surface::copyToTexture(SDL_Renderer* renderer, SDL_Texture* texture, SDL_FR
|
||||
|
||||
for (int y = 0; y < surface_data_->height; ++y) {
|
||||
for (int x = 0; x < surface_data_->width; ++x) {
|
||||
int texture_index = y * row_stride + x;
|
||||
int surface_index = y * surface_data_->width + x;
|
||||
int texture_index = (y * row_stride) + x;
|
||||
int surface_index = (y * surface_data_->width) + x;
|
||||
|
||||
pixels[texture_index] = palette_[surface_data_->data.get()[surface_index]];
|
||||
}
|
||||
@@ -504,7 +508,7 @@ void Surface::copyToTexture(SDL_Renderer* renderer, SDL_Texture* texture, SDL_FR
|
||||
SDL_UnlockTexture(texture);
|
||||
|
||||
// Renderiza la textura con los rectángulos especificados
|
||||
if (!SDL_RenderTexture(renderer, texture, srcRect, destRect)) {
|
||||
if (!SDL_RenderTexture(renderer, texture, src_rect, dest_rect)) {
|
||||
throw std::runtime_error("Failed to copy texture to renderer: " + std::string(SDL_GetError()));
|
||||
}
|
||||
}
|
||||
@@ -512,8 +516,8 @@ void Surface::copyToTexture(SDL_Renderer* renderer, SDL_Texture* texture, SDL_FR
|
||||
// Realiza un efecto de fundido en la paleta principal
|
||||
bool Surface::fadePalette() {
|
||||
// Verificar que el tamaño mínimo de palette_ sea adecuado
|
||||
static constexpr int palette_size = 19;
|
||||
if (sizeof(palette_) / sizeof(palette_[0]) < palette_size) {
|
||||
static constexpr int PALETTE_SIZE = 19;
|
||||
if (sizeof(palette_) / sizeof(palette_[0]) < PALETTE_SIZE) {
|
||||
throw std::runtime_error("Palette size is insufficient for fadePalette operation.");
|
||||
}
|
||||
|
||||
@@ -532,22 +536,22 @@ bool Surface::fadePalette() {
|
||||
// Realiza un efecto de fundido en la paleta secundaria
|
||||
bool Surface::fadeSubPalette(Uint32 delay) {
|
||||
// Variable estática para almacenar el último tick
|
||||
static Uint32 last_tick = 0;
|
||||
static Uint32 last_tick_ = 0;
|
||||
|
||||
// Obtener el tiempo actual
|
||||
Uint32 current_tick = SDL_GetTicks();
|
||||
|
||||
// Verificar si ha pasado el tiempo de retardo
|
||||
if (current_tick - last_tick < delay) {
|
||||
if (current_tick - last_tick_ < delay) {
|
||||
return false; // No se realiza el fade
|
||||
}
|
||||
|
||||
// Actualizar el último tick
|
||||
last_tick = current_tick;
|
||||
last_tick_ = current_tick;
|
||||
|
||||
// Verificar que el tamaño mínimo de sub_palette_ sea adecuado
|
||||
static constexpr int sub_palette_size = 19;
|
||||
if (sizeof(sub_palette_) / sizeof(sub_palette_[0]) < sub_palette_size) {
|
||||
static constexpr int SUB_PALETTE_SIZE = 19;
|
||||
if (sizeof(sub_palette_) / sizeof(sub_palette_[0]) < SUB_PALETTE_SIZE) {
|
||||
throw std::runtime_error("Palette size is insufficient for fadePalette operation.");
|
||||
}
|
||||
|
||||
|
||||
@@ -70,7 +70,7 @@ class Surface {
|
||||
~Surface() = default;
|
||||
|
||||
// Carga una SurfaceData desde un archivo
|
||||
SurfaceData loadSurface(const std::string& file_path);
|
||||
static SurfaceData loadSurface(const std::string& file_path);
|
||||
|
||||
// Carga una paleta desde un archivo
|
||||
void loadPalette(const std::string& file_path);
|
||||
@@ -79,10 +79,10 @@ class Surface {
|
||||
// Copia una región de la SurfaceData de origen a la SurfaceData de destino
|
||||
void render(float dx, float dy, float sx, float sy, float w, float h);
|
||||
void render(int x, int y, SDL_FRect* clip = nullptr, SDL_FlipMode flip = SDL_FLIP_NONE);
|
||||
void render(SDL_FRect* srcRect = nullptr, SDL_FRect* dstRect = nullptr, SDL_FlipMode flip = SDL_FLIP_NONE);
|
||||
void render(SDL_FRect* src_rect = nullptr, SDL_FRect* dst_rect = nullptr, SDL_FlipMode flip = SDL_FLIP_NONE);
|
||||
|
||||
// Copia una región de la SurfaceData de origen a la SurfaceData de destino reemplazando un color por otro
|
||||
void renderWithColorReplace(int x, int y, Uint8 source_color = 0, Uint8 target_color = 0, SDL_FRect* srcRect = nullptr, SDL_FlipMode flip = SDL_FLIP_NONE);
|
||||
void renderWithColorReplace(int x, int y, Uint8 source_color = 0, Uint8 target_color = 0, SDL_FRect* src_rect = nullptr, SDL_FlipMode flip = SDL_FLIP_NONE);
|
||||
|
||||
// Establece un color en la paleta
|
||||
void setColor(int index, Uint32 color);
|
||||
@@ -92,7 +92,7 @@ class Surface {
|
||||
|
||||
// Vuelca la SurfaceData a una textura
|
||||
void copyToTexture(SDL_Renderer* renderer, SDL_Texture* texture);
|
||||
void copyToTexture(SDL_Renderer* renderer, SDL_Texture* texture, SDL_FRect* srcRect, SDL_FRect* destRect);
|
||||
void copyToTexture(SDL_Renderer* renderer, SDL_Texture* texture, SDL_FRect* src_rect, SDL_FRect* dest_rect);
|
||||
|
||||
// Realiza un efecto de fundido en las paletas
|
||||
bool fadePalette();
|
||||
@@ -129,5 +129,5 @@ class Surface {
|
||||
void setPalette(const std::array<Uint32, 256>& palette) { palette_ = palette; }
|
||||
|
||||
// Inicializa la sub paleta
|
||||
void initializeSubPalette(SubPalette& palette) { std::iota(palette.begin(), palette.end(), 0); }
|
||||
static void initializeSubPalette(SubPalette& palette) { std::iota(palette.begin(), palette.end(), 0); }
|
||||
};
|
||||
|
||||
@@ -23,8 +23,9 @@ Animations loadAnimationsFromFile(const std::string& file_path) {
|
||||
std::vector<std::string> buffer;
|
||||
std::string line;
|
||||
while (std::getline(file, line)) {
|
||||
if (!line.empty())
|
||||
if (!line.empty()) {
|
||||
buffer.push_back(line);
|
||||
}
|
||||
}
|
||||
|
||||
return buffer;
|
||||
@@ -99,9 +100,9 @@ bool SurfaceAnimatedSprite::animationIsCompleted() {
|
||||
|
||||
// Establece la animacion actual
|
||||
void SurfaceAnimatedSprite::setCurrentAnimation(const std::string& name) {
|
||||
const auto new_animation = getIndex(name);
|
||||
if (current_animation_ != new_animation) {
|
||||
current_animation_ = new_animation;
|
||||
const auto NEW_ANIMATION = getIndex(name);
|
||||
if (current_animation_ != NEW_ANIMATION) {
|
||||
current_animation_ = NEW_ANIMATION;
|
||||
animations_[current_animation_].current_frame = 0;
|
||||
animations_[current_animation_].counter = 0;
|
||||
animations_[current_animation_].completed = false;
|
||||
@@ -111,9 +112,9 @@ void SurfaceAnimatedSprite::setCurrentAnimation(const std::string& name) {
|
||||
|
||||
// Establece la animacion actual
|
||||
void SurfaceAnimatedSprite::setCurrentAnimation(int index) {
|
||||
const auto new_animation = index;
|
||||
if (current_animation_ != new_animation) {
|
||||
current_animation_ = new_animation;
|
||||
const auto NEW_ANIMATION = index;
|
||||
if (current_animation_ != NEW_ANIMATION) {
|
||||
current_animation_ = NEW_ANIMATION;
|
||||
animations_[current_animation_].current_frame = 0;
|
||||
animations_[current_animation_].counter = 0;
|
||||
animations_[current_animation_].completed = false;
|
||||
@@ -154,17 +155,18 @@ void SurfaceAnimatedSprite::setAnimations(const Animations& animations) {
|
||||
if (pos != std::string::npos) {
|
||||
std::string key = line.substr(0, pos);
|
||||
int value = std::stoi(line.substr(pos + 1));
|
||||
if (key == "frame_width")
|
||||
if (key == "frame_width") {
|
||||
frame_width = value;
|
||||
else if (key == "frame_height")
|
||||
} else if (key == "frame_height") {
|
||||
frame_height = value;
|
||||
else
|
||||
} else {
|
||||
std::cout << "Warning: unknown parameter " << key << std::endl;
|
||||
}
|
||||
|
||||
frames_per_row = surface_->getWidth() / frame_width;
|
||||
const int w = surface_->getWidth() / frame_width;
|
||||
const int h = surface_->getHeight() / frame_height;
|
||||
max_tiles = w * h;
|
||||
const int W = surface_->getWidth() / frame_width;
|
||||
const int H = surface_->getHeight() / frame_height;
|
||||
max_tiles = W * H;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,30 +182,31 @@ void SurfaceAnimatedSprite::setAnimations(const Animations& animations) {
|
||||
std::string key = line.substr(0, pos);
|
||||
std::string value = line.substr(pos + 1);
|
||||
|
||||
if (key == "name")
|
||||
if (key == "name") {
|
||||
animation.name = value;
|
||||
else if (key == "speed")
|
||||
} else if (key == "speed") {
|
||||
animation.speed = std::stoi(value);
|
||||
else if (key == "loop")
|
||||
} else if (key == "loop") {
|
||||
animation.loop = std::stoi(value);
|
||||
else if (key == "frames") {
|
||||
} else if (key == "frames") {
|
||||
// Se introducen los valores separados por comas en un vector
|
||||
std::stringstream ss(value);
|
||||
std::string tmp;
|
||||
SDL_FRect rect = {0.0F, 0.0F, frame_width, frame_height};
|
||||
while (getline(ss, tmp, ',')) {
|
||||
// Comprueba que el tile no sea mayor que el maximo indice permitido
|
||||
const int num_tile = std::stoi(tmp);
|
||||
if (num_tile <= max_tiles) {
|
||||
rect.x = (num_tile % frames_per_row) * frame_width;
|
||||
rect.y = (num_tile / frames_per_row) * frame_height;
|
||||
const int NUM_TILE = std::stoi(tmp);
|
||||
if (NUM_TILE <= max_tiles) {
|
||||
rect.x = (NUM_TILE % frames_per_row) * frame_width;
|
||||
rect.y = (NUM_TILE / frames_per_row) * frame_height;
|
||||
animation.frames.emplace_back(rect);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
else
|
||||
else {
|
||||
std::cout << "Warning: unknown parameter " << key << std::endl;
|
||||
}
|
||||
}
|
||||
} while (line != "[/animation]");
|
||||
|
||||
|
||||
@@ -19,8 +19,7 @@ struct AnimationData {
|
||||
int counter; // Contador para las animaciones
|
||||
|
||||
AnimationData()
|
||||
: name(std::string()),
|
||||
speed(5),
|
||||
: speed(5),
|
||||
loop(0),
|
||||
completed(false),
|
||||
current_frame(0),
|
||||
|
||||
@@ -17,20 +17,20 @@ SurfaceMovingSprite::SurfaceMovingSprite(std::shared_ptr<Surface> surface, SDL_F
|
||||
|
||||
SurfaceMovingSprite::SurfaceMovingSprite(std::shared_ptr<Surface> surface)
|
||||
: SurfaceSprite(surface),
|
||||
x_(0.0f),
|
||||
y_(0.0f),
|
||||
x_(0.0F),
|
||||
y_(0.0F),
|
||||
flip_(SDL_FLIP_NONE) { SurfaceSprite::clear(); }
|
||||
|
||||
// Reinicia todas las variables
|
||||
void SurfaceMovingSprite::clear() {
|
||||
x_ = 0.0f; // Posición en el eje X
|
||||
y_ = 0.0f; // Posición en el eje Y
|
||||
x_ = 0.0F; // Posición en el eje X
|
||||
y_ = 0.0F; // Posición en el eje Y
|
||||
|
||||
vx_ = 0.0f; // Velocidad en el eje X. Cantidad de pixeles a desplazarse
|
||||
vy_ = 0.0f; // Velocidad en el eje Y. Cantidad de pixeles a desplazarse
|
||||
vx_ = 0.0F; // Velocidad en el eje X. Cantidad de pixeles a desplazarse
|
||||
vy_ = 0.0F; // Velocidad en el eje Y. Cantidad de pixeles a desplazarse
|
||||
|
||||
ax_ = 0.0f; // Aceleración en el eje X. Variación de la velocidad
|
||||
ay_ = 0.0f; // Aceleración en el eje Y. Variación de la velocidad
|
||||
ax_ = 0.0F; // Aceleración en el eje X. Variación de la velocidad
|
||||
ay_ = 0.0F; // Aceleración en el eje Y. Variación de la velocidad
|
||||
|
||||
flip_ = SDL_FLIP_NONE; // Establece como se ha de voltear el sprite
|
||||
|
||||
@@ -66,8 +66,8 @@ void SurfaceMovingSprite::render(Uint8 source_color, Uint8 target_color) {
|
||||
|
||||
// Establece la posición y_ el tamaño del objeto
|
||||
void SurfaceMovingSprite::setPos(SDL_FRect rect) {
|
||||
x_ = static_cast<float>(rect.x);
|
||||
y_ = static_cast<float>(rect.y);
|
||||
x_ = rect.x;
|
||||
y_ = rect.y;
|
||||
|
||||
pos_ = rect;
|
||||
}
|
||||
|
||||
@@ -14,11 +14,11 @@ class SurfaceMovingSprite : public SurfaceSprite {
|
||||
float x_; // Posición en el eje X
|
||||
float y_; // Posición en el eje Y
|
||||
|
||||
float vx_ = 0.0f; // Velocidad en el eje X. Cantidad de pixeles a desplazarse
|
||||
float vy_ = 0.0f; // Velocidad en el eje Y. Cantidad de pixeles a desplazarse
|
||||
float vx_ = 0.0F; // Velocidad en el eje X. Cantidad de pixeles a desplazarse
|
||||
float vy_ = 0.0F; // Velocidad en el eje Y. Cantidad de pixeles a desplazarse
|
||||
|
||||
float ax_ = 0.0f; // Aceleración en el eje X. Variación de la velocidad
|
||||
float ay_ = 0.0f; // Aceleración en el eje Y. Variación de la velocidad
|
||||
float ax_ = 0.0F; // Aceleración en el eje X. Variación de la velocidad
|
||||
float ay_ = 0.0F; // Aceleración en el eje Y. Variación de la velocidad
|
||||
|
||||
SDL_FlipMode flip_; // Indica como se voltea el sprite
|
||||
|
||||
|
||||
@@ -45,8 +45,9 @@ std::shared_ptr<TextFile> loadTextFile(const std::string& file_path) {
|
||||
auto line_read = 0;
|
||||
while (std::getline(file, buffer)) {
|
||||
// Almacena solo las lineas impares
|
||||
if (line_read % 2 == 1)
|
||||
if (line_read % 2 == 1) {
|
||||
tf->offset[index++].w = std::stoi(buffer);
|
||||
}
|
||||
|
||||
// Limpia el buffer
|
||||
buffer.clear();
|
||||
@@ -116,8 +117,9 @@ Text::Text(std::shared_ptr<Surface> surface, std::shared_ptr<TextFile> text_file
|
||||
void Text::write(int x, int y, const std::string& text, int kerning, int lenght) {
|
||||
int shift = 0;
|
||||
|
||||
if (lenght == -1)
|
||||
if (lenght == -1) {
|
||||
lenght = text.length();
|
||||
}
|
||||
|
||||
sprite_->setY(y);
|
||||
for (int i = 0; i < lenght; ++i) {
|
||||
@@ -144,14 +146,14 @@ std::shared_ptr<Surface> Text::writeToSurface(const std::string& text, int zoom,
|
||||
}
|
||||
|
||||
// Escribe el texto con extras en una surface
|
||||
std::shared_ptr<Surface> Text::writeDXToSurface(Uint8 flags, const std::string& text, int kerning, Uint8 textColor, Uint8 shadow_distance, Uint8 shadow_color, int lenght) {
|
||||
std::shared_ptr<Surface> Text::writeDXToSurface(Uint8 flags, const std::string& text, int kerning, Uint8 text_color, Uint8 shadow_distance, Uint8 shadow_color, int lenght) {
|
||||
auto width = Text::lenght(text, kerning) + shadow_distance;
|
||||
auto height = box_height_ + shadow_distance;
|
||||
auto surface = std::make_shared<Surface>(width, height);
|
||||
auto previuos_renderer = Screen::get()->getRendererSurface();
|
||||
Screen::get()->setRendererSurface(surface);
|
||||
surface->clear(stringToColor("transparent"));
|
||||
writeDX(flags, 0, 0, text, kerning, textColor, shadow_distance, shadow_color, lenght);
|
||||
writeDX(flags, 0, 0, text, kerning, text_color, shadow_distance, shadow_color, lenght);
|
||||
Screen::get()->setRendererSurface(previuos_renderer);
|
||||
|
||||
return surface;
|
||||
@@ -188,21 +190,21 @@ void Text::writeCentered(int x, int y, const std::string& text, int kerning, int
|
||||
}
|
||||
|
||||
// Escribe texto con extras
|
||||
void Text::writeDX(Uint8 flags, int x, int y, const std::string& text, int kerning, Uint8 textColor, Uint8 shadow_distance, Uint8 shadow_color, int lenght) {
|
||||
const auto centered = ((flags & TEXT_CENTER) == TEXT_CENTER);
|
||||
const auto shadowed = ((flags & TEXT_SHADOW) == TEXT_SHADOW);
|
||||
const auto colored = ((flags & TEXT_COLOR) == TEXT_COLOR);
|
||||
const auto stroked = ((flags & TEXT_STROKE) == TEXT_STROKE);
|
||||
void Text::writeDX(Uint8 flags, int x, int y, const std::string& text, int kerning, Uint8 text_color, Uint8 shadow_distance, Uint8 shadow_color, int lenght) {
|
||||
const auto CENTERED = ((flags & TEXT_CENTER) == TEXT_CENTER);
|
||||
const auto SHADOWED = ((flags & TEXT_SHADOW) == TEXT_SHADOW);
|
||||
const auto COLORED = ((flags & TEXT_COLOR) == TEXT_COLOR);
|
||||
const auto STROKED = ((flags & TEXT_STROKE) == TEXT_STROKE);
|
||||
|
||||
if (centered) {
|
||||
if (CENTERED) {
|
||||
x -= (Text::lenght(text, kerning) / 2);
|
||||
}
|
||||
|
||||
if (shadowed) {
|
||||
if (SHADOWED) {
|
||||
writeColored(x + shadow_distance, y + shadow_distance, text, shadow_color, kerning, lenght);
|
||||
}
|
||||
|
||||
if (stroked) {
|
||||
if (STROKED) {
|
||||
for (int dist = 1; dist <= shadow_distance; ++dist) {
|
||||
for (int dy = -dist; dy <= dist; ++dy) {
|
||||
for (int dx = -dist; dx <= dist; ++dx) {
|
||||
@@ -212,10 +214,10 @@ void Text::writeDX(Uint8 flags, int x, int y, const std::string& text, int kerni
|
||||
}
|
||||
}
|
||||
|
||||
if (colored) {
|
||||
writeColored(x, y, text, textColor, kerning, lenght);
|
||||
if (COLORED) {
|
||||
writeColored(x, y, text, text_color, kerning, lenght);
|
||||
} else {
|
||||
writeColored(x, y, text, textColor, kerning, lenght);
|
||||
writeColored(x, y, text, text_color, kerning, lenght);
|
||||
// write(x, y, text, kerning, lenght);
|
||||
}
|
||||
}
|
||||
@@ -223,8 +225,9 @@ void Text::writeDX(Uint8 flags, int x, int y, const std::string& text, int kerni
|
||||
// Obtiene la longitud en pixels de una cadena
|
||||
int Text::lenght(const std::string& text, int kerning) const {
|
||||
int shift = 0;
|
||||
for (size_t i = 0; i < text.length(); ++i)
|
||||
for (size_t i = 0; i < text.length(); ++i) {
|
||||
shift += (offset_[static_cast<int>(text[i])].w + kerning);
|
||||
}
|
||||
|
||||
// Descuenta el kerning del último caracter
|
||||
return shift - kerning;
|
||||
|
||||
@@ -53,7 +53,7 @@ class Text {
|
||||
std::shared_ptr<Surface> writeToSurface(const std::string& text, int zoom = 1, int kerning = 1);
|
||||
|
||||
// Escribe el texto con extras en una textura
|
||||
std::shared_ptr<Surface> writeDXToSurface(Uint8 flags, const std::string& text, int kerning = 1, Uint8 textColor = Uint8(), Uint8 shadow_distance = 1, Uint8 shadow_color = Uint8(), int lenght = -1);
|
||||
std::shared_ptr<Surface> writeDXToSurface(Uint8 flags, const std::string& text, int kerning = 1, Uint8 text_color = Uint8(), Uint8 shadow_distance = 1, Uint8 shadow_color = Uint8(), int lenght = -1);
|
||||
|
||||
// Escribe el texto con colores
|
||||
void writeColored(int x, int y, const std::string& text, Uint8 color, int kerning = 1, int lenght = -1);
|
||||
@@ -65,7 +65,7 @@ class Text {
|
||||
void writeCentered(int x, int y, const std::string& text, int kerning = 1, int lenght = -1);
|
||||
|
||||
// Escribe texto con extras
|
||||
void writeDX(Uint8 flags, int x, int y, const std::string& text, int kerning = 1, Uint8 textColor = Uint8(), Uint8 shadow_distance = 1, Uint8 shadow_color = Uint8(), int lenght = -1);
|
||||
void writeDX(Uint8 flags, int x, int y, const std::string& text, int kerning = 1, Uint8 text_color = Uint8(), Uint8 shadow_distance = 1, Uint8 shadow_color = Uint8(), int lenght = -1);
|
||||
|
||||
// Obtiene la longitud en pixels de una cadena
|
||||
int lenght(const std::string& text, int kerning = 1) const;
|
||||
|
||||
@@ -20,10 +20,10 @@ Texture::Texture(SDL_Renderer* renderer, const std::string& path)
|
||||
// Carga el fichero en la textura
|
||||
if (!path_.empty()) {
|
||||
// Obtiene la extensión
|
||||
const std::string extension = path_.substr(path_.find_last_of(".") + 1);
|
||||
const std::string EXTENSION = path_.substr(path_.find_last_of(".") + 1);
|
||||
|
||||
// .png
|
||||
if (extension == "png") {
|
||||
if (EXTENSION == "png") {
|
||||
loadFromFile(path_);
|
||||
}
|
||||
}
|
||||
@@ -42,14 +42,15 @@ bool Texture::loadFromFile(const std::string& file_path) {
|
||||
}
|
||||
|
||||
int req_format = STBI_rgb_alpha;
|
||||
int width, height, orig_format;
|
||||
int width;
|
||||
int height;
|
||||
int orig_format;
|
||||
unsigned char* data = stbi_load(file_path.c_str(), &width, &height, &orig_format, req_format);
|
||||
if (!data) {
|
||||
if (data == nullptr) {
|
||||
std::cerr << "Error: Fichero no encontrado " << getFileName(file_path) << std::endl;
|
||||
throw std::runtime_error("Fichero no encontrado: " + getFileName(file_path));
|
||||
} else {
|
||||
printWithDots("Image : ", getFileName(file_path), "[ LOADED ]");
|
||||
}
|
||||
printWithDots("Image : ", getFileName(file_path), "[ LOADED ]");
|
||||
|
||||
int pitch;
|
||||
SDL_PixelFormat pixel_format;
|
||||
@@ -61,7 +62,7 @@ bool Texture::loadFromFile(const std::string& file_path) {
|
||||
unloadTexture();
|
||||
|
||||
// La textura final
|
||||
SDL_Texture* newTexture = nullptr;
|
||||
SDL_Texture* new_texture = nullptr;
|
||||
|
||||
// Carga la imagen desde una ruta específica
|
||||
auto* loaded_surface = SDL_CreateSurfaceFrom(width, height, pixel_format, static_cast<void*>(data), pitch);
|
||||
@@ -69,8 +70,8 @@ bool Texture::loadFromFile(const std::string& file_path) {
|
||||
std::cout << "Unable to load image " << file_path << std::endl;
|
||||
} else {
|
||||
// Crea la textura desde los pixels de la surface
|
||||
newTexture = SDL_CreateTextureFromSurface(renderer_, loaded_surface);
|
||||
if (newTexture == nullptr) {
|
||||
new_texture = SDL_CreateTextureFromSurface(renderer_, loaded_surface);
|
||||
if (new_texture == nullptr) {
|
||||
std::cout << "Unable to create texture from " << file_path << "! SDL Error: " << SDL_GetError() << std::endl;
|
||||
} else {
|
||||
// Obtiene las dimensiones de la imagen
|
||||
@@ -84,7 +85,7 @@ bool Texture::loadFromFile(const std::string& file_path) {
|
||||
|
||||
// Return success
|
||||
stbi_image_free(data);
|
||||
texture_ = newTexture;
|
||||
texture_ = new_texture;
|
||||
return texture_ != nullptr;
|
||||
}
|
||||
|
||||
@@ -105,7 +106,7 @@ auto Texture::createBlank(int width, int height, SDL_PixelFormat format, SDL_Tex
|
||||
// Libera la memoria de la textura
|
||||
void Texture::unloadTexture() {
|
||||
// Libera la textura
|
||||
if (texture_) {
|
||||
if (texture_ != nullptr) {
|
||||
SDL_DestroyTexture(texture_);
|
||||
texture_ = nullptr;
|
||||
width_ = 0;
|
||||
@@ -124,7 +125,7 @@ void Texture::setBlendMode(SDL_BlendMode blending) { SDL_SetTextureBlendMode(tex
|
||||
void Texture::setAlpha(Uint8 alpha) { SDL_SetTextureAlphaMod(texture_, alpha); }
|
||||
|
||||
// Renderiza la textura en un punto específico
|
||||
void Texture::render(float x, float y, SDL_FRect* clip, float zoomW, float zoomH, double angle, SDL_FPoint* center, SDL_FlipMode flip) {
|
||||
void Texture::render(float x, float y, SDL_FRect* clip, float zoom_w, float zoom_h, double angle, SDL_FPoint* center, SDL_FlipMode flip) {
|
||||
// Establece el destino de renderizado en la pantalla
|
||||
SDL_FRect render_quad = {x, y, width_, height_};
|
||||
|
||||
@@ -135,11 +136,11 @@ void Texture::render(float x, float y, SDL_FRect* clip, float zoomW, float zoomH
|
||||
}
|
||||
|
||||
// Calcula el zoom y las coordenadas
|
||||
if (zoomH != 1.0f || zoomW != 1.0f) {
|
||||
if (zoom_h != 1.0F || zoom_w != 1.0F) {
|
||||
render_quad.x = render_quad.x + (render_quad.w / 2);
|
||||
render_quad.y = render_quad.y + (render_quad.h / 2);
|
||||
render_quad.w = render_quad.w * zoomW;
|
||||
render_quad.h = render_quad.h * zoomH;
|
||||
render_quad.w = render_quad.w * zoom_w;
|
||||
render_quad.h = render_quad.h * zoom_h;
|
||||
render_quad.x = render_quad.x - (render_quad.w / 2);
|
||||
render_quad.y = render_quad.y - (render_quad.h / 2);
|
||||
}
|
||||
@@ -151,12 +152,6 @@ void Texture::render(float x, float y, SDL_FRect* clip, float zoomW, float zoomH
|
||||
// Establece la textura como objetivo de renderizado
|
||||
void Texture::setAsRenderTarget(SDL_Renderer* renderer) { SDL_SetRenderTarget(renderer, texture_); }
|
||||
|
||||
// Obtiene el ancho de la imagen
|
||||
int Texture::getWidth() { return width_; }
|
||||
|
||||
// Obtiene el alto de la imagen
|
||||
int Texture::getHeight() { return height_; }
|
||||
|
||||
// Recarga la textura
|
||||
bool Texture::reLoad() { return loadFromFile(path_); }
|
||||
|
||||
|
||||
@@ -45,16 +45,16 @@ class Texture {
|
||||
void setAlpha(Uint8 alpha);
|
||||
|
||||
// Renderiza la textura en un punto específico
|
||||
void render(float x, float y, SDL_FRect* clip = nullptr, float zoomW = 1, float zoomH = 1, double angle = 0.0, SDL_FPoint* center = nullptr, SDL_FlipMode flip = SDL_FLIP_NONE);
|
||||
void render(float x, float y, SDL_FRect* clip = nullptr, float zoom_w = 1, float zoom_h = 1, double angle = 0.0, SDL_FPoint* center = nullptr, SDL_FlipMode flip = SDL_FLIP_NONE);
|
||||
|
||||
// Establece la textura como objetivo de renderizado
|
||||
void setAsRenderTarget(SDL_Renderer* renderer);
|
||||
|
||||
// Obtiene el ancho de la imagen
|
||||
int getWidth();
|
||||
int getWidth() const { return width_; }
|
||||
|
||||
// Obtiene el alto de la imagen
|
||||
int getHeight();
|
||||
int getHeight() const { return height_; }
|
||||
|
||||
// Recarga la textura
|
||||
bool reLoad();
|
||||
|
||||
Reference in New Issue
Block a user