cppcheck
This commit is contained in:
@@ -96,15 +96,15 @@ inline int JA_FeedMusicChunk(JA_Music_t* music) {
|
||||
if (!music || !music->vorbis || !music->stream) return 0;
|
||||
|
||||
short chunk[JA_MUSIC_CHUNK_SHORTS];
|
||||
const int channels = music->spec.channels;
|
||||
const int numChannels = music->spec.channels;
|
||||
const int samples_per_channel = stb_vorbis_get_samples_short_interleaved(
|
||||
music->vorbis,
|
||||
channels,
|
||||
numChannels,
|
||||
chunk,
|
||||
JA_MUSIC_CHUNK_SHORTS);
|
||||
if (samples_per_channel <= 0) return 0;
|
||||
|
||||
const int bytes = samples_per_channel * channels * JA_MUSIC_BYTES_PER_SAMPLE;
|
||||
const int bytes = samples_per_channel * numChannels * JA_MUSIC_BYTES_PER_SAMPLE;
|
||||
SDL_PutAudioStreamData(music->stream, chunk, bytes);
|
||||
return samples_per_channel;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <iostream> // for basic_ostream, operator<<, cout, basi...
|
||||
#include <algorithm> // for any_of
|
||||
#include <iostream> // for basic_ostream, operator<<, cout, basi...
|
||||
|
||||
// Emscripten-only: SDL 3.4+ ja no casa el GUID dels mandos de Chrome Android
|
||||
// amb gamecontrollerdb (el gamepad.id d'Android no porta Vendor/Product, el
|
||||
@@ -57,10 +58,12 @@ auto Input::get() -> Input * {
|
||||
}
|
||||
|
||||
// Constructor
|
||||
Input::Input(std::string file) {
|
||||
// Fichero gamecontrollerdb.txt
|
||||
dbPath = file;
|
||||
|
||||
Input::Input(const std::string &file)
|
||||
: numGamepads(0),
|
||||
dbPath(file),
|
||||
verbose(true),
|
||||
disabledUntil(d_notDisabled),
|
||||
enabled(true) {
|
||||
// Inicializa las variables
|
||||
keyBindings_t kb;
|
||||
kb.scancode = 0;
|
||||
@@ -71,10 +74,6 @@ Input::Input(std::string file) {
|
||||
gcb.button = SDL_GAMEPAD_BUTTON_INVALID;
|
||||
gcb.active = false;
|
||||
gameControllerBindings.resize(input_number_of_inputs, gcb);
|
||||
|
||||
numGamepads = 0;
|
||||
verbose = true;
|
||||
enabled = true;
|
||||
}
|
||||
|
||||
// Destructor
|
||||
@@ -311,10 +310,8 @@ bool Input::handleGamepadAdded(SDL_JoystickID jid, std::string &outName) {
|
||||
}
|
||||
|
||||
// Si el mando ya está registrado no hace nada (ej. evento retroactivo tras el scan inicial)
|
||||
for (SDL_JoystickID existing : connectedControllerIds) {
|
||||
if (existing == jid) {
|
||||
return false;
|
||||
}
|
||||
if (std::any_of(connectedControllerIds.begin(), connectedControllerIds.end(), [jid](SDL_JoystickID existing) { return existing == jid; })) {
|
||||
return false;
|
||||
}
|
||||
|
||||
installWebStandardMapping(jid);
|
||||
|
||||
@@ -80,7 +80,7 @@ class Input {
|
||||
std::string buildControllerName(SDL_Gamepad *pad, int padIndex);
|
||||
|
||||
// Constructor privado (usar Input::init)
|
||||
Input(std::string file);
|
||||
explicit Input(const std::string &file);
|
||||
|
||||
// Instancia única
|
||||
static Input *instance;
|
||||
|
||||
@@ -22,6 +22,8 @@ static animatedSprite_t parseAnimationStream(std::istream &file, Texture *textur
|
||||
while (std::getline(file, line)) {
|
||||
if (line == "[animation]") {
|
||||
animation_t buffer;
|
||||
buffer.speed = 0;
|
||||
buffer.loop = -1;
|
||||
buffer.counter = 0;
|
||||
buffer.currentFrame = 0;
|
||||
buffer.completed = false;
|
||||
@@ -82,7 +84,7 @@ static animatedSprite_t parseAnimationStream(std::istream &file, Texture *textur
|
||||
}
|
||||
|
||||
// Carga la animación desde un fichero
|
||||
animatedSprite_t loadAnimationFromFile(Texture *texture, std::string filePath, bool verbose) {
|
||||
animatedSprite_t loadAnimationFromFile(Texture *texture, const std::string &filePath, bool verbose) {
|
||||
const std::string filename = filePath.substr(filePath.find_last_of("\\/") + 1);
|
||||
std::ifstream file(filePath);
|
||||
if (!file.good()) {
|
||||
@@ -109,42 +111,34 @@ animatedSprite_t loadAnimationFromMemory(Texture *texture, const std::vector<uin
|
||||
}
|
||||
|
||||
// Constructor
|
||||
AnimatedSprite::AnimatedSprite(Texture *texture, SDL_Renderer *renderer, std::string file, std::vector<std::string> *buffer) {
|
||||
AnimatedSprite::AnimatedSprite(Texture *texture, SDL_Renderer *renderer, const std::string &file, std::vector<std::string> *buffer)
|
||||
: currentAnimation(0) {
|
||||
// Copia los punteros
|
||||
setTexture(texture);
|
||||
setRenderer(renderer);
|
||||
|
||||
// Carga las animaciones
|
||||
if (file != "") {
|
||||
if (!file.empty()) {
|
||||
animatedSprite_t as = loadAnimationFromFile(texture, file);
|
||||
|
||||
// Copia los datos de las animaciones
|
||||
for (auto animation : as.animations) {
|
||||
this->animation.push_back(animation);
|
||||
}
|
||||
animation.insert(animation.end(), as.animations.begin(), as.animations.end());
|
||||
}
|
||||
|
||||
else if (buffer) {
|
||||
loadFromVector(buffer);
|
||||
}
|
||||
|
||||
// Inicializa variables
|
||||
currentAnimation = 0;
|
||||
}
|
||||
|
||||
// Constructor
|
||||
AnimatedSprite::AnimatedSprite(SDL_Renderer *renderer, animatedSprite_t *animation) {
|
||||
AnimatedSprite::AnimatedSprite(SDL_Renderer *renderer, animatedSprite_t *animation)
|
||||
: currentAnimation(0) {
|
||||
// Copia los punteros
|
||||
setTexture(animation->texture);
|
||||
setRenderer(renderer);
|
||||
|
||||
// Inicializa variables
|
||||
currentAnimation = 0;
|
||||
|
||||
// Copia los datos de las animaciones
|
||||
for (auto a : animation->animations) {
|
||||
this->animation.push_back(a);
|
||||
}
|
||||
this->animation.insert(this->animation.end(), animation->animations.begin(), animation->animations.end());
|
||||
}
|
||||
|
||||
// Destructor
|
||||
@@ -156,10 +150,10 @@ AnimatedSprite::~AnimatedSprite() {
|
||||
}
|
||||
|
||||
// Obtiene el indice de la animación a partir del nombre
|
||||
int AnimatedSprite::getIndex(std::string name) {
|
||||
int AnimatedSprite::getIndex(const std::string &name) {
|
||||
int index = -1;
|
||||
|
||||
for (auto a : animation) {
|
||||
for (const auto &a : animation) {
|
||||
index++;
|
||||
if (a.name == name) {
|
||||
return index;
|
||||
@@ -222,12 +216,12 @@ void AnimatedSprite::setCurrentFrame(int num) {
|
||||
}
|
||||
|
||||
// Establece el valor del contador
|
||||
void AnimatedSprite::setAnimationCounter(std::string name, int num) {
|
||||
void AnimatedSprite::setAnimationCounter(const std::string &name, int num) {
|
||||
animation[getIndex(name)].counter = num;
|
||||
}
|
||||
|
||||
// Establece la velocidad de una animación
|
||||
void AnimatedSprite::setAnimationSpeed(std::string name, int speed) {
|
||||
void AnimatedSprite::setAnimationSpeed(const std::string &name, int speed) {
|
||||
animation[getIndex(name)].counter = speed;
|
||||
}
|
||||
|
||||
@@ -237,7 +231,7 @@ void AnimatedSprite::setAnimationSpeed(int index, int speed) {
|
||||
}
|
||||
|
||||
// Establece si la animación se reproduce en bucle
|
||||
void AnimatedSprite::setAnimationLoop(std::string name, int loop) {
|
||||
void AnimatedSprite::setAnimationLoop(const std::string &name, int loop) {
|
||||
animation[getIndex(name)].loop = loop;
|
||||
}
|
||||
|
||||
@@ -247,7 +241,7 @@ void AnimatedSprite::setAnimationLoop(int index, int loop) {
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void AnimatedSprite::setAnimationCompleted(std::string name, bool value) {
|
||||
void AnimatedSprite::setAnimationCompleted(const std::string &name, bool value) {
|
||||
animation[getIndex(name)].completed = value;
|
||||
}
|
||||
|
||||
@@ -262,7 +256,7 @@ bool AnimatedSprite::animationIsCompleted() {
|
||||
}
|
||||
|
||||
// Devuelve el rectangulo de una animación y frame concreto
|
||||
SDL_Rect AnimatedSprite::getAnimationClip(std::string name, Uint8 index) {
|
||||
SDL_Rect AnimatedSprite::getAnimationClip(const std::string &name, Uint8 index) {
|
||||
return animation[getIndex(name)].frames[index];
|
||||
}
|
||||
|
||||
@@ -292,6 +286,8 @@ bool AnimatedSprite::loadFromVector(std::vector<std::string> *source) {
|
||||
// Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación
|
||||
if (line == "[animation]") {
|
||||
animation_t buffer;
|
||||
buffer.speed = 0;
|
||||
buffer.loop = -1;
|
||||
buffer.counter = 0;
|
||||
buffer.currentFrame = 0;
|
||||
buffer.completed = false;
|
||||
@@ -391,7 +387,7 @@ bool AnimatedSprite::loadFromVector(std::vector<std::string> *source) {
|
||||
}
|
||||
|
||||
// Establece la animacion actual
|
||||
void AnimatedSprite::setCurrentAnimation(std::string name) {
|
||||
void AnimatedSprite::setCurrentAnimation(const std::string &name) {
|
||||
const int newAnimation = getIndex(name);
|
||||
if (currentAnimation != newAnimation) {
|
||||
currentAnimation = newAnimation;
|
||||
|
||||
@@ -25,7 +25,7 @@ struct animatedSprite_t {
|
||||
};
|
||||
|
||||
// Carga la animación desde un fichero
|
||||
animatedSprite_t loadAnimationFromFile(Texture *texture, std::string filePath, bool verbose = false);
|
||||
animatedSprite_t loadAnimationFromFile(Texture *texture, const std::string &filePath, bool verbose = false);
|
||||
|
||||
// Carga la animación desde bytes en memoria
|
||||
animatedSprite_t loadAnimationFromMemory(Texture *texture, const std::vector<uint8_t> &bytes, const std::string &nameForLogs = "", bool verbose = false);
|
||||
@@ -38,7 +38,7 @@ class AnimatedSprite : public MovingSprite {
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
AnimatedSprite(Texture *texture = nullptr, SDL_Renderer *renderer = nullptr, std::string file = "", std::vector<std::string> *buffer = nullptr);
|
||||
explicit AnimatedSprite(Texture *texture = nullptr, SDL_Renderer *renderer = nullptr, const std::string &file = "", std::vector<std::string> *buffer = nullptr);
|
||||
AnimatedSprite(SDL_Renderer *renderer, animatedSprite_t *animation);
|
||||
|
||||
// Destructor
|
||||
@@ -54,35 +54,35 @@ class AnimatedSprite : public MovingSprite {
|
||||
void setCurrentFrame(int num);
|
||||
|
||||
// Establece el valor del contador
|
||||
void setAnimationCounter(std::string name, int num);
|
||||
void setAnimationCounter(const std::string &name, int num);
|
||||
|
||||
// Establece la velocidad de una animación
|
||||
void setAnimationSpeed(std::string name, int speed);
|
||||
void setAnimationSpeed(const std::string &name, int speed);
|
||||
void setAnimationSpeed(int index, int speed);
|
||||
|
||||
// Establece el frame al que vuelve la animación al finalizar
|
||||
void setAnimationLoop(std::string name, int loop);
|
||||
void setAnimationLoop(const std::string &name, int loop);
|
||||
void setAnimationLoop(int index, int loop);
|
||||
|
||||
// Establece el valor de la variable
|
||||
void setAnimationCompleted(std::string name, bool value);
|
||||
void setAnimationCompleted(const std::string &name, bool value);
|
||||
void setAnimationCompleted(int index, bool value);
|
||||
|
||||
// Comprueba si ha terminado la animación
|
||||
bool animationIsCompleted();
|
||||
|
||||
// Devuelve el rectangulo de una animación y frame concreto
|
||||
SDL_Rect getAnimationClip(std::string name = "default", Uint8 index = 0);
|
||||
SDL_Rect getAnimationClip(const std::string &name = "default", Uint8 index = 0);
|
||||
SDL_Rect getAnimationClip(int indexA = 0, Uint8 indexF = 0);
|
||||
|
||||
// Obtiene el indice de la animación a partir del nombre
|
||||
int getIndex(std::string name);
|
||||
int getIndex(const std::string &name);
|
||||
|
||||
// Carga la animación desde un vector
|
||||
bool loadFromVector(std::vector<std::string> *source);
|
||||
|
||||
// Establece la animacion actual
|
||||
void setCurrentAnimation(std::string name = "default");
|
||||
void setCurrentAnimation(const std::string &name = "default");
|
||||
void setCurrentAnimation(int index = 0);
|
||||
|
||||
// Actualiza las variables del objeto
|
||||
|
||||
@@ -8,9 +8,8 @@
|
||||
#include "game/defaults.hpp" // for GAMECANVAS_HEIGHT, GAMECANVAS_WIDTH
|
||||
|
||||
// Constructor
|
||||
Fade::Fade(SDL_Renderer *renderer) {
|
||||
mRenderer = renderer;
|
||||
|
||||
Fade::Fade(SDL_Renderer *renderer)
|
||||
: mRenderer(renderer) {
|
||||
mBackbuffer = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
|
||||
if (mBackbuffer != nullptr) {
|
||||
SDL_SetTextureScaleMode(mBackbuffer, SDL_SCALEMODE_NEAREST);
|
||||
@@ -53,7 +52,6 @@ void Fade::render() {
|
||||
|
||||
int alpha = mCounter * 4;
|
||||
if (alpha >= 255) {
|
||||
alpha = 255;
|
||||
mFullscreenDone = true;
|
||||
|
||||
// Deja todos los buffers del mismo color
|
||||
|
||||
@@ -10,23 +10,23 @@ constexpr int FADE_RANDOM_SQUARE = 2;
|
||||
// Clase Fade
|
||||
class Fade {
|
||||
private:
|
||||
SDL_Renderer *mRenderer; // El renderizador de la ventana
|
||||
SDL_Texture *mBackbuffer; // Textura para usar como backbuffer
|
||||
Uint8 mFadeType; // Tipo de fade a realizar
|
||||
Uint16 mCounter; // Contador interno
|
||||
bool mEnabled; // Indica si el fade está activo
|
||||
bool mFinished; // Indica si ha terminado la transición
|
||||
Uint8 mR, mG, mB; // Colores para el fade
|
||||
Uint8 mROriginal, mGOriginal, mBOriginal; // Colores originales para FADE_RANDOM_SQUARE
|
||||
Uint32 mLastSquareTicks; // Ticks del último cuadrado dibujado (FADE_RANDOM_SQUARE)
|
||||
Uint16 mSquaresDrawn; // Número de cuadrados dibujados (FADE_RANDOM_SQUARE)
|
||||
bool mFullscreenDone; // Indica si el fade fullscreen ha terminado la fase de fundido
|
||||
SDL_Rect mRect1; // Rectangulo usado para crear los efectos de transición
|
||||
SDL_Rect mRect2; // Rectangulo usado para crear los efectos de transición
|
||||
SDL_Renderer *mRenderer = nullptr; // El renderizador de la ventana
|
||||
SDL_Texture *mBackbuffer = nullptr; // Textura para usar como backbuffer
|
||||
Uint8 mFadeType = FADE_FULLSCREEN; // Tipo de fade a realizar
|
||||
Uint16 mCounter = 0; // Contador interno
|
||||
bool mEnabled = false; // Indica si el fade está activo
|
||||
bool mFinished = false; // Indica si ha terminado la transición
|
||||
Uint8 mR = 0, mG = 0, mB = 0; // Colores para el fade
|
||||
Uint8 mROriginal = 0, mGOriginal = 0, mBOriginal = 0; // Colores originales para FADE_RANDOM_SQUARE
|
||||
Uint32 mLastSquareTicks = 0; // Ticks del último cuadrado dibujado (FADE_RANDOM_SQUARE)
|
||||
Uint16 mSquaresDrawn = 0; // Número de cuadrados dibujados (FADE_RANDOM_SQUARE)
|
||||
bool mFullscreenDone = false; // Indica si el fade fullscreen ha terminado la fase de fundido
|
||||
SDL_Rect mRect1{}; // Rectangulo usado para crear los efectos de transición
|
||||
SDL_Rect mRect2{}; // Rectangulo usado para crear los efectos de transición
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Fade(SDL_Renderer *renderer);
|
||||
explicit Fade(SDL_Renderer *renderer);
|
||||
|
||||
// Destructor
|
||||
~Fade();
|
||||
|
||||
@@ -3,53 +3,26 @@
|
||||
#include "core/rendering/texture.h" // for Texture
|
||||
|
||||
// Constructor
|
||||
MovingSprite::MovingSprite(float x, float y, int w, int h, float velx, float vely, float accelx, float accely, Texture *texture, SDL_Renderer *renderer) {
|
||||
// Copia los punteros
|
||||
this->texture = texture;
|
||||
this->renderer = renderer;
|
||||
|
||||
// Establece el alto y el ancho del sprite
|
||||
this->w = w;
|
||||
this->h = h;
|
||||
|
||||
// Establece la posición X,Y del sprite
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
xPrev = x;
|
||||
yPrev = y;
|
||||
|
||||
// Establece la velocidad X,Y del sprite
|
||||
vx = velx;
|
||||
vy = vely;
|
||||
|
||||
// Establece la aceleración X,Y del sprite
|
||||
ax = accelx;
|
||||
ay = accely;
|
||||
|
||||
// Establece el zoom W,H del sprite
|
||||
zoomW = 1;
|
||||
zoomH = 1;
|
||||
|
||||
// Establece el angulo con el que se dibujará
|
||||
angle = (double)0;
|
||||
|
||||
// Establece los valores de rotacion
|
||||
rotateEnabled = false;
|
||||
rotateSpeed = 0;
|
||||
rotateAmount = (double)0;
|
||||
|
||||
// Contador interno
|
||||
counter = 0;
|
||||
|
||||
// Establece el rectangulo de donde coger la imagen
|
||||
spriteClip = {0, 0, w, h};
|
||||
|
||||
// Establece el centro de rotación
|
||||
center = nullptr;
|
||||
|
||||
// Establece el tipo de volteado
|
||||
currentFlip = SDL_FLIP_NONE;
|
||||
};
|
||||
MovingSprite::MovingSprite(float x, float y, int w, int h, float velx, float vely, float accelx, float accely, Texture *texture, SDL_Renderer *renderer)
|
||||
: Sprite(0, 0, w, h, texture, renderer),
|
||||
x(x),
|
||||
y(y),
|
||||
xPrev(x),
|
||||
yPrev(y),
|
||||
vx(velx),
|
||||
vy(vely),
|
||||
ax(accelx),
|
||||
ay(accely),
|
||||
zoomW(1),
|
||||
zoomH(1),
|
||||
angle(0.0),
|
||||
rotateEnabled(false),
|
||||
rotateSpeed(0),
|
||||
rotateAmount(0.0),
|
||||
counter(0),
|
||||
center(nullptr),
|
||||
currentFlip(SDL_FLIP_NONE) {
|
||||
}
|
||||
|
||||
// Reinicia todas las variables
|
||||
void MovingSprite::clear() {
|
||||
|
||||
@@ -33,7 +33,7 @@ class MovingSprite : public Sprite {
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
MovingSprite(float x = 0, float y = 0, int w = 0, int h = 0, float velx = 0, float vely = 0, float accelx = 0, float accely = 0, Texture *texture = nullptr, SDL_Renderer *renderer = nullptr);
|
||||
explicit MovingSprite(float x = 0, float y = 0, int w = 0, int h = 0, float velx = 0, float vely = 0, float accelx = 0, float accely = 0, Texture *texture = nullptr, SDL_Renderer *renderer = nullptr);
|
||||
|
||||
// Mueve el sprite
|
||||
void move();
|
||||
|
||||
@@ -77,7 +77,8 @@ auto Screen::get() -> Screen * {
|
||||
}
|
||||
|
||||
// Constructor
|
||||
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer) {
|
||||
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer)
|
||||
: borderColor{0x00, 0x00, 0x00} {
|
||||
// Inicializa variables
|
||||
this->window = window;
|
||||
this->renderer = renderer;
|
||||
@@ -85,9 +86,6 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer) {
|
||||
gameCanvasWidth = GAMECANVAS_WIDTH;
|
||||
gameCanvasHeight = GAMECANVAS_HEIGHT;
|
||||
|
||||
// Define el color del borde para el modo de pantalla completa
|
||||
borderColor = {0x00, 0x00, 0x00};
|
||||
|
||||
// Establece el modo de video (fullscreen/ventana + logical presentation)
|
||||
// ANTES de crear la textura — SDL3 GPU necesita la logical presentation
|
||||
// del renderer ya aplicada al swapchain quan es reclama la ventana per a GPU.
|
||||
|
||||
@@ -3,48 +3,26 @@
|
||||
#include "core/rendering/texture.h" // for Texture
|
||||
|
||||
// Constructor
|
||||
Sprite::Sprite(int x, int y, int w, int h, Texture *texture, SDL_Renderer *renderer) {
|
||||
// Establece la posición X,Y del sprite
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
|
||||
// Establece el alto y el ancho del sprite
|
||||
this->w = w;
|
||||
this->h = h;
|
||||
|
||||
// Establece el puntero al renderizador de la ventana
|
||||
this->renderer = renderer;
|
||||
|
||||
// Establece la textura donde están los gráficos para el sprite
|
||||
this->texture = texture;
|
||||
|
||||
// Establece el rectangulo de donde coger la imagen
|
||||
spriteClip = {0, 0, w, h};
|
||||
|
||||
// Inicializa variables
|
||||
enabled = true;
|
||||
Sprite::Sprite(int x, int y, int w, int h, Texture *texture, SDL_Renderer *renderer)
|
||||
: x(x),
|
||||
y(y),
|
||||
w(w),
|
||||
h(h),
|
||||
renderer(renderer),
|
||||
texture(texture),
|
||||
spriteClip{0, 0, w, h},
|
||||
enabled(true) {
|
||||
}
|
||||
|
||||
Sprite::Sprite(SDL_Rect rect, Texture *texture, SDL_Renderer *renderer) {
|
||||
// Establece la posición X,Y del sprite
|
||||
x = rect.x;
|
||||
y = rect.y;
|
||||
|
||||
// Establece el alto y el ancho del sprite
|
||||
w = rect.w;
|
||||
h = rect.h;
|
||||
|
||||
// Establece el puntero al renderizador de la ventana
|
||||
this->renderer = renderer;
|
||||
|
||||
// Establece la textura donde están los gráficos para el sprite
|
||||
this->texture = texture;
|
||||
|
||||
// Establece el rectangulo de donde coger la imagen
|
||||
spriteClip = {0, 0, w, h};
|
||||
|
||||
// Inicializa variables
|
||||
enabled = true;
|
||||
Sprite::Sprite(SDL_Rect rect, Texture *texture, SDL_Renderer *renderer)
|
||||
: x(rect.x),
|
||||
y(rect.y),
|
||||
w(rect.w),
|
||||
h(rect.h),
|
||||
renderer(renderer),
|
||||
texture(texture),
|
||||
spriteClip{0, 0, rect.w, rect.h},
|
||||
enabled(true) {
|
||||
}
|
||||
|
||||
// Destructor
|
||||
|
||||
@@ -19,7 +19,7 @@ class Sprite {
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Sprite(int x = 0, int y = 0, int w = 0, int h = 0, Texture *texture = nullptr, SDL_Renderer *renderer = nullptr);
|
||||
explicit Sprite(int x = 0, int y = 0, int w = 0, int h = 0, Texture *texture = nullptr, SDL_Renderer *renderer = nullptr);
|
||||
Sprite(SDL_Rect rect, Texture *texture, SDL_Renderer *renderer);
|
||||
|
||||
// Destructor
|
||||
|
||||
@@ -39,8 +39,10 @@ static void computeTextFileOffsets(textFile_t &tf) {
|
||||
}
|
||||
|
||||
// Llena una estructuta textFile_t desde un fichero
|
||||
textFile_t LoadTextFile(std::string file, bool verbose) {
|
||||
textFile_t LoadTextFile(const std::string &file, bool verbose) {
|
||||
textFile_t tf;
|
||||
tf.boxWidth = 0;
|
||||
tf.boxHeight = 0;
|
||||
for (int i = 0; i < 128; ++i) {
|
||||
tf.offset[i].x = 0;
|
||||
tf.offset[i].y = 0;
|
||||
@@ -65,6 +67,8 @@ textFile_t LoadTextFile(std::string file, bool verbose) {
|
||||
// Llena una estructura textFile_t desde bytes en memoria
|
||||
textFile_t LoadTextFileFromMemory(const std::vector<uint8_t> &bytes, bool verbose) {
|
||||
textFile_t tf;
|
||||
tf.boxWidth = 0;
|
||||
tf.boxHeight = 0;
|
||||
for (int i = 0; i < 128; ++i) {
|
||||
tf.offset[i].x = 0;
|
||||
tf.offset[i].y = 0;
|
||||
@@ -83,7 +87,7 @@ textFile_t LoadTextFileFromMemory(const std::vector<uint8_t> &bytes, bool verbos
|
||||
}
|
||||
|
||||
// Constructor
|
||||
Text::Text(std::string bitmapFile, std::string textFile, SDL_Renderer *renderer) {
|
||||
Text::Text(const std::string &bitmapFile, const std::string &textFile, SDL_Renderer *renderer) {
|
||||
// Carga los offsets desde el fichero
|
||||
textFile_t tf = LoadTextFile(textFile);
|
||||
|
||||
@@ -105,7 +109,7 @@ Text::Text(std::string bitmapFile, std::string textFile, SDL_Renderer *renderer)
|
||||
}
|
||||
|
||||
// Constructor
|
||||
Text::Text(std::string textFile, Texture *texture, SDL_Renderer *renderer) {
|
||||
Text::Text(const std::string &textFile, Texture *texture, SDL_Renderer *renderer) {
|
||||
// Carga los offsets desde el fichero
|
||||
textFile_t tf = LoadTextFile(textFile);
|
||||
|
||||
@@ -172,7 +176,7 @@ Text::~Text() {
|
||||
}
|
||||
|
||||
// Escribe texto en pantalla
|
||||
void Text::write(int x, int y, std::string text, int kerning, int lenght) {
|
||||
void Text::write(int x, int y, const std::string &text, int kerning, int lenght) {
|
||||
int shift = 0;
|
||||
|
||||
if (lenght == -1) {
|
||||
@@ -192,14 +196,14 @@ void Text::write(int x, int y, std::string text, int kerning, int lenght) {
|
||||
}
|
||||
|
||||
// Escribe el texto con colores
|
||||
void Text::writeColored(int x, int y, std::string text, color_t color, int kerning, int lenght) {
|
||||
void Text::writeColored(int x, int y, const std::string &text, color_t color, int kerning, int lenght) {
|
||||
sprite->getTexture()->setColor(color.r, color.g, color.b);
|
||||
write(x, y, text, kerning, lenght);
|
||||
sprite->getTexture()->setColor(255, 255, 255);
|
||||
}
|
||||
|
||||
// Escribe el texto con sombra
|
||||
void Text::writeShadowed(int x, int y, std::string text, color_t color, Uint8 shadowDistance, int kerning, int lenght) {
|
||||
void Text::writeShadowed(int x, int y, const std::string &text, color_t color, Uint8 shadowDistance, int kerning, int lenght) {
|
||||
sprite->getTexture()->setColor(color.r, color.g, color.b);
|
||||
write(x + shadowDistance, y + shadowDistance, text, kerning, lenght);
|
||||
sprite->getTexture()->setColor(255, 255, 255);
|
||||
@@ -207,13 +211,13 @@ void Text::writeShadowed(int x, int y, std::string text, color_t color, Uint8 sh
|
||||
}
|
||||
|
||||
// Escribe el texto centrado en un punto x
|
||||
void Text::writeCentered(int x, int y, std::string text, int kerning, int lenght) {
|
||||
void Text::writeCentered(int x, int y, const std::string &text, int kerning, int lenght) {
|
||||
x -= (Text::lenght(text, kerning) / 2);
|
||||
write(x, y, text, kerning, lenght);
|
||||
}
|
||||
|
||||
// Escribe texto con extras
|
||||
void Text::writeDX(Uint8 flags, int x, int y, std::string text, int kerning, color_t textColor, Uint8 shadowDistance, color_t shadowColor, int lenght) {
|
||||
void Text::writeDX(Uint8 flags, int x, int y, const std::string &text, int kerning, color_t textColor, Uint8 shadowDistance, color_t shadowColor, int lenght) {
|
||||
const bool centered = ((flags & TXT_CENTER) == TXT_CENTER);
|
||||
const bool shadowed = ((flags & TXT_SHADOW) == TXT_SHADOW);
|
||||
const bool colored = ((flags & TXT_COLOR) == TXT_COLOR);
|
||||
@@ -245,7 +249,7 @@ void Text::writeDX(Uint8 flags, int x, int y, std::string text, int kerning, col
|
||||
}
|
||||
|
||||
// Obtiene la longitud en pixels de una cadena
|
||||
int Text::lenght(std::string text, int kerning) {
|
||||
int Text::lenght(const std::string &text, int kerning) {
|
||||
int shift = 0;
|
||||
|
||||
for (int i = 0; i < (int)text.length(); ++i)
|
||||
|
||||
@@ -28,7 +28,7 @@ struct textFile_t {
|
||||
};
|
||||
|
||||
// Llena una estructuta textFile_t desde un fichero
|
||||
textFile_t LoadTextFile(std::string file, bool verbose = false);
|
||||
textFile_t LoadTextFile(const std::string &file, bool verbose = false);
|
||||
|
||||
// Llena una estructura textFile_t desde bytes en memoria
|
||||
textFile_t LoadTextFileFromMemory(const std::vector<uint8_t> &bytes, bool verbose = false);
|
||||
@@ -48,8 +48,8 @@ class Text {
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Text(std::string bitmapFile, std::string textFile, SDL_Renderer *renderer);
|
||||
Text(std::string textFile, Texture *texture, SDL_Renderer *renderer);
|
||||
Text(const std::string &bitmapFile, const std::string &textFile, SDL_Renderer *renderer);
|
||||
Text(const std::string &textFile, Texture *texture, SDL_Renderer *renderer);
|
||||
Text(textFile_t *textFile, Texture *texture, SDL_Renderer *renderer);
|
||||
|
||||
// Constructor desde bytes en memoria: comparte ownership del texture (no lo libera)
|
||||
@@ -58,23 +58,27 @@ class Text {
|
||||
// Destructor
|
||||
~Text();
|
||||
|
||||
// No copiable (gestiona memoria dinámica)
|
||||
Text(const Text &) = delete;
|
||||
Text &operator=(const Text &) = delete;
|
||||
|
||||
// Escribe el texto en pantalla
|
||||
void write(int x, int y, std::string text, int kerning = 1, int lenght = -1);
|
||||
void write(int x, int y, const std::string &text, int kerning = 1, int lenght = -1);
|
||||
|
||||
// Escribe el texto con colores
|
||||
void writeColored(int x, int y, std::string text, color_t color, int kerning = 1, int lenght = -1);
|
||||
void writeColored(int x, int y, const std::string &text, color_t color, int kerning = 1, int lenght = -1);
|
||||
|
||||
// Escribe el texto con sombra
|
||||
void writeShadowed(int x, int y, std::string text, color_t color, Uint8 shadowDistance = 1, int kerning = 1, int lenght = -1);
|
||||
void writeShadowed(int x, int y, const std::string &text, color_t color, Uint8 shadowDistance = 1, int kerning = 1, int lenght = -1);
|
||||
|
||||
// Escribe el texto centrado en un punto x
|
||||
void writeCentered(int x, int y, std::string text, int kerning = 1, int lenght = -1);
|
||||
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, std::string text, int kerning = 1, color_t textColor = color_t(255, 255, 255), Uint8 shadowDistance = 1, color_t shadowColor = color_t(0, 0, 0), int lenght = -1);
|
||||
void writeDX(Uint8 flags, int x, int y, const std::string &text, int kerning = 1, color_t textColor = color_t(255, 255, 255), Uint8 shadowDistance = 1, color_t shadowColor = color_t(0, 0, 0), int lenght = -1);
|
||||
|
||||
// Obtiene la longitud en pixels de una cadena
|
||||
int lenght(std::string text, int kerning = 1);
|
||||
int lenght(const std::string &text, int kerning = 1);
|
||||
|
||||
// Devuelve el valor de la variable
|
||||
int getCharacterSize();
|
||||
|
||||
@@ -15,30 +15,25 @@ void Texture::setGlobalScaleMode(SDL_ScaleMode mode) {
|
||||
}
|
||||
|
||||
// Constructor
|
||||
Texture::Texture(SDL_Renderer *renderer, std::string path, bool verbose) {
|
||||
// Copia punteros
|
||||
this->renderer = renderer;
|
||||
this->path = path;
|
||||
|
||||
// Inicializa
|
||||
texture = nullptr;
|
||||
width = 0;
|
||||
height = 0;
|
||||
|
||||
Texture::Texture(SDL_Renderer *renderer, const std::string &path, bool verbose)
|
||||
: texture(nullptr),
|
||||
renderer(renderer),
|
||||
width(0),
|
||||
height(0),
|
||||
path(path) {
|
||||
// Carga el fichero en la textura
|
||||
if (path != "") {
|
||||
if (!path.empty()) {
|
||||
loadFromFile(path, renderer, verbose);
|
||||
}
|
||||
}
|
||||
|
||||
// Constructor desde bytes
|
||||
Texture::Texture(SDL_Renderer *renderer, const std::vector<uint8_t> &bytes, bool verbose) {
|
||||
this->renderer = renderer;
|
||||
this->path = "";
|
||||
texture = nullptr;
|
||||
width = 0;
|
||||
height = 0;
|
||||
|
||||
Texture::Texture(SDL_Renderer *renderer, const std::vector<uint8_t> &bytes, bool verbose)
|
||||
: texture(nullptr),
|
||||
renderer(renderer),
|
||||
width(0),
|
||||
height(0),
|
||||
path("") {
|
||||
if (!bytes.empty()) {
|
||||
loadFromMemory(bytes.data(), bytes.size(), renderer, verbose);
|
||||
}
|
||||
@@ -53,7 +48,7 @@ Texture::~Texture() {
|
||||
// Helper: convierte píxeles RGBA decodificados por stbi en SDL_Texture
|
||||
static SDL_Texture *createTextureFromPixels(SDL_Renderer *renderer, unsigned char *data, int w, int h, int *out_w, int *out_h) {
|
||||
const int pitch = 4 * w;
|
||||
SDL_Surface *loadedSurface = SDL_CreateSurfaceFrom(w, h, SDL_PIXELFORMAT_RGBA32, (void *)data, pitch);
|
||||
SDL_Surface *loadedSurface = SDL_CreateSurfaceFrom(w, h, SDL_PIXELFORMAT_RGBA32, static_cast<void *>(data), pitch);
|
||||
if (loadedSurface == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
@@ -68,7 +63,7 @@ static SDL_Texture *createTextureFromPixels(SDL_Renderer *renderer, unsigned cha
|
||||
}
|
||||
|
||||
// Carga una imagen desde un fichero
|
||||
bool Texture::loadFromFile(std::string path, SDL_Renderer *renderer, bool verbose) {
|
||||
bool Texture::loadFromFile(const std::string &path, SDL_Renderer *renderer, bool verbose) {
|
||||
const std::string filename = path.substr(path.find_last_of("\\/") + 1);
|
||||
int req_format = STBI_rgb_alpha;
|
||||
int w, h, orig_format;
|
||||
|
||||
@@ -24,7 +24,7 @@ class Texture {
|
||||
static void setGlobalScaleMode(SDL_ScaleMode mode);
|
||||
|
||||
// Constructor
|
||||
Texture(SDL_Renderer *renderer, std::string path = "", bool verbose = false);
|
||||
explicit Texture(SDL_Renderer *renderer, const std::string &path = "", bool verbose = false);
|
||||
|
||||
// Constructor desde bytes (PNG en memoria)
|
||||
Texture(SDL_Renderer *renderer, const std::vector<uint8_t> &bytes, bool verbose = false);
|
||||
@@ -33,7 +33,7 @@ class Texture {
|
||||
~Texture();
|
||||
|
||||
// Carga una imagen desde un fichero
|
||||
bool loadFromFile(std::string path, SDL_Renderer *renderer, bool verbose = false);
|
||||
bool loadFromFile(const std::string &path, SDL_Renderer *renderer, bool verbose = false);
|
||||
|
||||
// Carga una imagen desde bytes en memoria
|
||||
bool loadFromMemory(const uint8_t *data, size_t size, SDL_Renderer *renderer, bool verbose = false);
|
||||
|
||||
@@ -3,23 +3,20 @@
|
||||
#include "core/rendering/text.h" // for Text
|
||||
|
||||
// Constructor
|
||||
Writer::Writer(Text *text) {
|
||||
// Copia los punteros
|
||||
this->text = text;
|
||||
|
||||
// Inicializa variables
|
||||
posX = 0;
|
||||
posY = 0;
|
||||
kerning = 0;
|
||||
caption = "";
|
||||
speed = 0;
|
||||
writingCounter = 0;
|
||||
index = 0;
|
||||
lenght = 0;
|
||||
completed = false;
|
||||
enabled = false;
|
||||
enabledCounter = 0;
|
||||
finished = false;
|
||||
Writer::Writer(Text *text)
|
||||
: text(text),
|
||||
posX(0),
|
||||
posY(0),
|
||||
kerning(0),
|
||||
caption(""),
|
||||
speed(0),
|
||||
writingCounter(0),
|
||||
index(0),
|
||||
lenght(0),
|
||||
completed(false),
|
||||
enabled(false),
|
||||
enabledCounter(0),
|
||||
finished(false) {
|
||||
}
|
||||
|
||||
// Actualiza el objeto
|
||||
@@ -73,7 +70,7 @@ void Writer::setKerning(int value) {
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void Writer::setCaption(std::string text) {
|
||||
void Writer::setCaption(const std::string &text) {
|
||||
caption = text;
|
||||
lenght = text.length();
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ class Writer {
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Writer(Text *text);
|
||||
explicit Writer(Text *text);
|
||||
|
||||
// Actualiza el objeto
|
||||
void update();
|
||||
@@ -43,7 +43,7 @@ class Writer {
|
||||
void setKerning(int value);
|
||||
|
||||
// Establece el valor de la variable
|
||||
void setCaption(std::string text);
|
||||
void setCaption(const std::string &text);
|
||||
|
||||
// Establece el valor de la variable
|
||||
void setSpeed(int value);
|
||||
|
||||
@@ -25,14 +25,14 @@ auto Asset::get() -> Asset * {
|
||||
}
|
||||
|
||||
// Constructor
|
||||
Asset::Asset(std::string executablePath) {
|
||||
this->executablePath = executablePath.substr(0, executablePath.find_last_of("\\/"));
|
||||
longestName = 0;
|
||||
verbose = true;
|
||||
Asset::Asset(const std::string &executablePath)
|
||||
: longestName(0),
|
||||
executablePath(executablePath.substr(0, executablePath.find_last_of("\\/"))),
|
||||
verbose(true) {
|
||||
}
|
||||
|
||||
// Añade un elemento a la lista
|
||||
void Asset::add(std::string file, enum assetType type, bool required, bool absolute) {
|
||||
void Asset::add(const std::string &file, enum assetType type, bool required, bool absolute) {
|
||||
item_t temp;
|
||||
temp.file = absolute ? file : executablePath + file;
|
||||
temp.type = type;
|
||||
@@ -44,8 +44,8 @@ void Asset::add(std::string file, enum assetType type, bool required, bool absol
|
||||
}
|
||||
|
||||
// Devuelve el fichero de un elemento de la lista a partir de una cadena
|
||||
std::string Asset::get(std::string text) {
|
||||
for (auto f : fileList) {
|
||||
std::string Asset::get(const std::string &text) {
|
||||
for (const auto &f : fileList) {
|
||||
const size_t lastIndex = f.file.find_last_of("/") + 1;
|
||||
const std::string file = f.file.substr(lastIndex, std::string::npos);
|
||||
|
||||
@@ -76,7 +76,7 @@ bool Asset::check() {
|
||||
// Comprueba si hay ficheros de ese tipo
|
||||
bool any = false;
|
||||
|
||||
for (auto f : fileList) {
|
||||
for (const auto &f : fileList) {
|
||||
if ((f.required) && (f.type == type)) {
|
||||
any = true;
|
||||
}
|
||||
@@ -88,7 +88,7 @@ bool Asset::check() {
|
||||
std::cout << "\n>> " << getTypeName(type).c_str() << " FILES" << std::endl;
|
||||
}
|
||||
|
||||
for (auto f : fileList) {
|
||||
for (const auto &f : fileList) {
|
||||
if ((f.required) && (f.type == type)) {
|
||||
success &= checkFile(f.file);
|
||||
}
|
||||
@@ -111,7 +111,7 @@ bool Asset::check() {
|
||||
}
|
||||
|
||||
// Comprueba que existe un fichero
|
||||
bool Asset::checkFile(std::string path) {
|
||||
bool Asset::checkFile(const std::string &path) {
|
||||
bool success = false;
|
||||
std::string result = "ERROR";
|
||||
|
||||
|
||||
@@ -34,13 +34,13 @@ class Asset {
|
||||
bool verbose; // Indica si ha de mostrar información por pantalla
|
||||
|
||||
// Comprueba que existe un fichero
|
||||
bool checkFile(std::string executablePath);
|
||||
bool checkFile(const std::string &executablePath);
|
||||
|
||||
// Devuelve el nombre del tipo de recurso
|
||||
std::string getTypeName(int type);
|
||||
|
||||
// Constructor privado (usar Asset::init)
|
||||
Asset(std::string path);
|
||||
explicit Asset(const std::string &path);
|
||||
|
||||
// Instancia única
|
||||
static Asset *instance;
|
||||
@@ -52,10 +52,10 @@ class Asset {
|
||||
static auto get() -> Asset *; // Obtiene el puntero a la instancia
|
||||
|
||||
// Añade un elemento a la lista
|
||||
void add(std::string file, enum assetType type, bool required = true, bool absolute = false);
|
||||
void add(const std::string &file, enum assetType type, bool required = true, bool absolute = false);
|
||||
|
||||
// Devuelve un elemento de la lista a partir de una cadena
|
||||
std::string get(std::string text);
|
||||
std::string get(const std::string &text);
|
||||
|
||||
// Devuelve toda la lista de items registrados
|
||||
const std::vector<item_t> &getAll() const { return fileList; }
|
||||
|
||||
@@ -30,7 +30,7 @@ class Resource {
|
||||
const std::vector<uint8_t> &getDemoBytes() const { return demoBytes_; }
|
||||
|
||||
private:
|
||||
Resource(SDL_Renderer *renderer);
|
||||
explicit Resource(SDL_Renderer *renderer);
|
||||
~Resource();
|
||||
|
||||
void preloadAll();
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <numeric>
|
||||
#include <utility>
|
||||
|
||||
const std::string ResourcePack::DEFAULT_ENCRYPT_KEY = "CCRS_RESOURCES__2026";
|
||||
@@ -17,11 +18,8 @@ ResourcePack::~ResourcePack() {
|
||||
}
|
||||
|
||||
uint32_t ResourcePack::calculateChecksum(const std::vector<uint8_t>& data) {
|
||||
uint32_t checksum = 0x12345678;
|
||||
for (unsigned char i : data) {
|
||||
checksum = ((checksum << 5) + checksum) + i;
|
||||
}
|
||||
return checksum;
|
||||
return std::accumulate(data.begin(), data.end(), uint32_t(0x12345678),
|
||||
[](uint32_t acc, uint8_t b) { return ((acc << 5) + acc) + b; });
|
||||
}
|
||||
|
||||
void ResourcePack::encryptData(std::vector<uint8_t>& data, const std::string& key) {
|
||||
|
||||
Reference in New Issue
Block a user