neteja tidy a source/core i encamina Texture::loadFromFile pel ResourceHelper
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
#include "core/rendering/texture.h" // for Texture
|
||||
|
||||
// Parser compartido: lee un istream con el formato .ani
|
||||
static animatedSprite_t parseAnimationStream(std::istream &file, Texture *texture, const std::string &filename, bool verbose) {
|
||||
static auto parseAnimationStream(std::istream &file, Texture *texture, const std::string &filename, bool verbose) -> animatedSprite_t {
|
||||
animatedSprite_t as;
|
||||
as.texture = texture;
|
||||
int framesPerRow = 0;
|
||||
@@ -17,13 +17,15 @@ static animatedSprite_t parseAnimationStream(std::istream &file, Texture *textur
|
||||
std::string line;
|
||||
|
||||
if (verbose) {
|
||||
std::cout << "Animation loaded: " << filename << std::endl;
|
||||
std::cout << "Animation loaded: " << filename << '\n';
|
||||
}
|
||||
// Normalitza CRLF: fitxers .ani amb terminadors de Windows fan que
|
||||
// line == "[animation]" no faci match i el parser entri en bucle
|
||||
// infinit / no carregui cap animació.
|
||||
auto strip_cr = [](std::string &s) {
|
||||
if (!s.empty() && s.back() == '\r') s.pop_back();
|
||||
if (!s.empty() && s.back() == '\r') {
|
||||
s.pop_back();
|
||||
}
|
||||
};
|
||||
|
||||
while (std::getline(file, line)) {
|
||||
@@ -37,10 +39,12 @@ static animatedSprite_t parseAnimationStream(std::istream &file, Texture *textur
|
||||
buffer.completed = false;
|
||||
|
||||
do {
|
||||
if (!std::getline(file, line)) break;
|
||||
if (!std::getline(file, line)) {
|
||||
break;
|
||||
}
|
||||
strip_cr(line);
|
||||
int pos = line.find("=");
|
||||
if (pos != (int)line.npos) {
|
||||
int pos = line.find('=');
|
||||
if (pos != (int)std::string::npos) {
|
||||
if (line.substr(0, pos) == "name") {
|
||||
buffer.name = line.substr(pos + 1, line.length());
|
||||
} else if (line.substr(0, pos) == "speed") {
|
||||
@@ -58,15 +62,15 @@ static animatedSprite_t parseAnimationStream(std::istream &file, Texture *textur
|
||||
buffer.frames.push_back(rect);
|
||||
}
|
||||
} else {
|
||||
std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl;
|
||||
std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << '\n';
|
||||
}
|
||||
}
|
||||
} while (line != "[/animation]");
|
||||
|
||||
as.animations.push_back(buffer);
|
||||
} else {
|
||||
int pos = line.find("=");
|
||||
if (pos != (int)line.npos) {
|
||||
int pos = line.find('=');
|
||||
if (pos != (int)std::string::npos) {
|
||||
if (line.substr(0, pos) == "framesPerRow") {
|
||||
framesPerRow = std::stoi(line.substr(pos + 1, line.length()));
|
||||
} else if (line.substr(0, pos) == "frameWidth") {
|
||||
@@ -74,7 +78,7 @@ static animatedSprite_t parseAnimationStream(std::istream &file, Texture *textur
|
||||
} else if (line.substr(0, pos) == "frameHeight") {
|
||||
frameHeight = std::stoi(line.substr(pos + 1, line.length()));
|
||||
} else {
|
||||
std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl;
|
||||
std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << '\n';
|
||||
}
|
||||
|
||||
if (framesPerRow == 0 && frameWidth > 0) {
|
||||
@@ -93,12 +97,12 @@ static animatedSprite_t parseAnimationStream(std::istream &file, Texture *textur
|
||||
}
|
||||
|
||||
// Carga la animación desde un fichero
|
||||
animatedSprite_t loadAnimationFromFile(Texture *texture, const std::string &filePath, bool verbose) {
|
||||
auto loadAnimationFromFile(Texture *texture, const std::string &filePath, bool verbose) -> animatedSprite_t {
|
||||
const std::string filename = filePath.substr(filePath.find_last_of("\\/") + 1);
|
||||
std::ifstream file(filePath);
|
||||
if (!file.good()) {
|
||||
if (verbose) {
|
||||
std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl;
|
||||
std::cout << "Warning: Unable to open " << filename.c_str() << " file" << '\n';
|
||||
}
|
||||
animatedSprite_t as;
|
||||
as.texture = texture;
|
||||
@@ -108,7 +112,7 @@ animatedSprite_t loadAnimationFromFile(Texture *texture, const std::string &file
|
||||
}
|
||||
|
||||
// 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) {
|
||||
auto loadAnimationFromMemory(Texture *texture, const std::vector<uint8_t> &bytes, const std::string &nameForLogs, bool verbose) -> animatedSprite_t {
|
||||
if (bytes.empty()) {
|
||||
animatedSprite_t as;
|
||||
as.texture = texture;
|
||||
@@ -134,7 +138,7 @@ AnimatedSprite::AnimatedSprite(Texture *texture, SDL_Renderer *renderer, const s
|
||||
animation.insert(animation.end(), as.animations.begin(), as.animations.end());
|
||||
}
|
||||
|
||||
else if (buffer) {
|
||||
else if (buffer != nullptr) {
|
||||
loadFromVector(buffer);
|
||||
}
|
||||
}
|
||||
@@ -159,7 +163,7 @@ AnimatedSprite::~AnimatedSprite() {
|
||||
}
|
||||
|
||||
// Obtiene el indice de la animación a partir del nombre
|
||||
int AnimatedSprite::getIndex(const std::string &name) {
|
||||
auto AnimatedSprite::getIndex(const std::string &name) -> int {
|
||||
int index = -1;
|
||||
|
||||
for (const auto &a : animation) {
|
||||
@@ -169,7 +173,7 @@ int AnimatedSprite::getIndex(const std::string &name) {
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "** Warning: could not find \"" << name.c_str() << "\" animation" << std::endl;
|
||||
std::cout << "** Warning: could not find \"" << name.c_str() << "\" animation" << '\n';
|
||||
|
||||
return -1;
|
||||
}
|
||||
@@ -205,7 +209,7 @@ void AnimatedSprite::animate() {
|
||||
}
|
||||
|
||||
// Obtiene el numero de frames de la animación actual
|
||||
int AnimatedSprite::getNumFrames() {
|
||||
auto AnimatedSprite::getNumFrames() -> int {
|
||||
return (int)animation[currentAnimation].frames.size();
|
||||
}
|
||||
|
||||
@@ -260,22 +264,22 @@ void AnimatedSprite::setAnimationCompleted(int index, bool value) {
|
||||
}
|
||||
|
||||
// Comprueba si ha terminado la animación
|
||||
bool AnimatedSprite::animationIsCompleted() {
|
||||
auto AnimatedSprite::animationIsCompleted() -> bool {
|
||||
return animation[currentAnimation].completed;
|
||||
}
|
||||
|
||||
// Devuelve el rectangulo de una animación y frame concreto
|
||||
SDL_Rect AnimatedSprite::getAnimationClip(const std::string &name, Uint8 index) {
|
||||
auto AnimatedSprite::getAnimationClip(const std::string &name, Uint8 index) -> SDL_Rect {
|
||||
return animation[getIndex(name)].frames[index];
|
||||
}
|
||||
|
||||
// Devuelve el rectangulo de una animación y frame concreto
|
||||
SDL_Rect AnimatedSprite::getAnimationClip(int indexA, Uint8 indexF) {
|
||||
auto AnimatedSprite::getAnimationClip(int indexA, Uint8 indexF) -> SDL_Rect {
|
||||
return animation[indexA].frames[indexF];
|
||||
}
|
||||
|
||||
// Carga la animación desde un vector
|
||||
bool AnimatedSprite::loadFromVector(std::vector<std::string> *source) {
|
||||
auto AnimatedSprite::loadFromVector(std::vector<std::string> *source) -> bool {
|
||||
// Inicializa variables
|
||||
int framesPerRow = 0;
|
||||
int frameWidth = 0;
|
||||
@@ -307,10 +311,10 @@ bool AnimatedSprite::loadFromVector(std::vector<std::string> *source) {
|
||||
line = source->at(index);
|
||||
|
||||
// Encuentra la posición del caracter '='
|
||||
int pos = line.find("=");
|
||||
int pos = line.find('=');
|
||||
|
||||
// Procesa las dos subcadenas
|
||||
if (pos != (int)line.npos) {
|
||||
if (pos != (int)std::string::npos) {
|
||||
if (line.substr(0, pos) == "name") {
|
||||
buffer.name = line.substr(pos + 1, line.length());
|
||||
}
|
||||
@@ -338,7 +342,7 @@ bool AnimatedSprite::loadFromVector(std::vector<std::string> *source) {
|
||||
}
|
||||
|
||||
else {
|
||||
std::cout << "Warning: unknown parameter " << line.substr(0, pos).c_str() << std::endl;
|
||||
std::cout << "Warning: unknown parameter " << line.substr(0, pos).c_str() << '\n';
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
@@ -351,10 +355,10 @@ bool AnimatedSprite::loadFromVector(std::vector<std::string> *source) {
|
||||
// En caso contrario se parsea el fichero para buscar las variables y los valores
|
||||
else {
|
||||
// Encuentra la posición del caracter '='
|
||||
int pos = line.find("=");
|
||||
int pos = line.find('=');
|
||||
|
||||
// Procesa las dos subcadenas
|
||||
if (pos != (int)line.npos) {
|
||||
if (pos != (int)std::string::npos) {
|
||||
if (line.substr(0, pos) == "framesPerRow") {
|
||||
framesPerRow = std::stoi(line.substr(pos + 1, line.length()));
|
||||
}
|
||||
@@ -368,7 +372,7 @@ bool AnimatedSprite::loadFromVector(std::vector<std::string> *source) {
|
||||
}
|
||||
|
||||
else {
|
||||
std::cout << "Warning: unknown parameter " << line.substr(0, pos).c_str() << std::endl;
|
||||
std::cout << "Warning: unknown parameter " << line.substr(0, pos).c_str() << '\n';
|
||||
success = false;
|
||||
}
|
||||
|
||||
|
||||
@@ -25,10 +25,10 @@ struct animatedSprite_t {
|
||||
};
|
||||
|
||||
// Carga la animación desde un fichero
|
||||
animatedSprite_t loadAnimationFromFile(Texture *texture, const std::string &filePath, bool verbose = false);
|
||||
auto loadAnimationFromFile(Texture *texture, const std::string &filePath, bool verbose = false) -> animatedSprite_t;
|
||||
|
||||
// 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);
|
||||
auto loadAnimationFromMemory(Texture *texture, const std::vector<uint8_t> &bytes, const std::string &nameForLogs = "", bool verbose = false) -> animatedSprite_t;
|
||||
|
||||
class AnimatedSprite : public MovingSprite {
|
||||
private:
|
||||
@@ -48,7 +48,7 @@ class AnimatedSprite : public MovingSprite {
|
||||
void animate();
|
||||
|
||||
// Obtiene el numero de frames de la animación actual
|
||||
int getNumFrames();
|
||||
auto getNumFrames() -> int;
|
||||
|
||||
// Establece el frame actual de la animación
|
||||
void setCurrentFrame(int num);
|
||||
@@ -69,17 +69,17 @@ class AnimatedSprite : public MovingSprite {
|
||||
void setAnimationCompleted(int index, bool value);
|
||||
|
||||
// Comprueba si ha terminado la animación
|
||||
bool animationIsCompleted();
|
||||
auto animationIsCompleted() -> bool;
|
||||
|
||||
// Devuelve el rectangulo de una animación y frame concreto
|
||||
SDL_Rect getAnimationClip(const std::string &name = "default", Uint8 index = 0);
|
||||
SDL_Rect getAnimationClip(int indexA = 0, Uint8 indexF = 0);
|
||||
auto getAnimationClip(const std::string &name = "default", Uint8 index = 0) -> SDL_Rect;
|
||||
auto getAnimationClip(int indexA = 0, Uint8 indexF = 0) -> SDL_Rect;
|
||||
|
||||
// Obtiene el indice de la animación a partir del nombre
|
||||
int getIndex(const std::string &name);
|
||||
auto getIndex(const std::string &name) -> int;
|
||||
|
||||
// Carga la animación desde un vector
|
||||
bool loadFromVector(std::vector<std::string> *source);
|
||||
auto loadFromVector(std::vector<std::string> *source) -> bool;
|
||||
|
||||
// Establece la animacion actual
|
||||
void setCurrentAnimation(const std::string &name = "default");
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#include "core/rendering/fade.h"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <stdlib.h> // for rand
|
||||
|
||||
#include <cstdlib> // for rand
|
||||
#include <iostream> // for char_traits, basic_ostream, operator<<
|
||||
|
||||
#include "game/defaults.hpp" // for GAMECANVAS_HEIGHT, GAMECANVAS_WIDTH
|
||||
@@ -15,7 +15,7 @@ Fade::Fade(SDL_Renderer *renderer)
|
||||
SDL_SetTextureScaleMode(mBackbuffer, SDL_SCALEMODE_NEAREST);
|
||||
}
|
||||
if (mBackbuffer == nullptr) {
|
||||
std::cout << "Error: textTexture could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
|
||||
std::cout << "Error: textTexture could not be created!\nSDL Error: " << SDL_GetError() << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,8 +92,9 @@ void Fade::render() {
|
||||
SDL_RenderFillRect(mRenderer, &fR2);
|
||||
}
|
||||
|
||||
if ((mCounter * 4) > GAMECANVAS_HEIGHT)
|
||||
if ((mCounter * 4) > GAMECANVAS_HEIGHT) {
|
||||
mFinished = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -145,8 +146,9 @@ void Fade::render() {
|
||||
|
||||
// Actualiza las variables internas
|
||||
void Fade::update() {
|
||||
if (mEnabled)
|
||||
if (mEnabled) {
|
||||
mCounter++;
|
||||
}
|
||||
}
|
||||
|
||||
// Activa el fade
|
||||
@@ -163,12 +165,12 @@ void Fade::activateFade() {
|
||||
}
|
||||
|
||||
// Comprueba si está activo
|
||||
bool Fade::isEnabled() {
|
||||
auto Fade::isEnabled() const -> bool {
|
||||
return mEnabled;
|
||||
}
|
||||
|
||||
// Comprueba si ha terminado la transicion
|
||||
bool Fade::hasEnded() {
|
||||
auto Fade::hasEnded() const -> bool {
|
||||
return mFinished;
|
||||
}
|
||||
|
||||
|
||||
@@ -44,10 +44,10 @@ class Fade {
|
||||
void activateFade();
|
||||
|
||||
// Comprueba si ha terminado la transicion
|
||||
bool hasEnded();
|
||||
[[nodiscard]] auto hasEnded() const -> bool;
|
||||
|
||||
// Comprueba si está activo
|
||||
bool isEnabled();
|
||||
[[nodiscard]] auto isEnabled() const -> bool;
|
||||
|
||||
// Establece el tipo de fade
|
||||
void setFadeType(Uint8 fadeType);
|
||||
|
||||
@@ -12,31 +12,22 @@ MovingSprite::MovingSprite(float x, float y, int w, int h, float velx, float vel
|
||||
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) {
|
||||
ay(accely) {
|
||||
}
|
||||
|
||||
// Reinicia todas las variables
|
||||
void MovingSprite::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
|
||||
|
||||
zoomW = 1.0f; // Zoom aplicado a la anchura
|
||||
zoomH = 1.0f; // Zoom aplicado a la altura
|
||||
zoomW = 1.0F; // Zoom aplicado a la anchura
|
||||
zoomH = 1.0F; // Zoom aplicado a la altura
|
||||
|
||||
angle = 0.0; // Angulo para dibujarlo
|
||||
rotateEnabled = false; // Indica si ha de rotar
|
||||
@@ -71,48 +62,48 @@ void MovingSprite::render() {
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
// cppcheck-suppress duplInheritedMember
|
||||
float MovingSprite::getPosX() {
|
||||
auto MovingSprite::getPosX() const -> float {
|
||||
return x;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
// cppcheck-suppress duplInheritedMember
|
||||
float MovingSprite::getPosY() {
|
||||
auto MovingSprite::getPosY() const -> float {
|
||||
return y;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
float MovingSprite::getVelX() {
|
||||
auto MovingSprite::getVelX() const -> float {
|
||||
return vx;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
float MovingSprite::getVelY() {
|
||||
auto MovingSprite::getVelY() const -> float {
|
||||
return vy;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
float MovingSprite::getAccelX() {
|
||||
auto MovingSprite::getAccelX() const -> float {
|
||||
return ax;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
float MovingSprite::getAccelY() {
|
||||
auto MovingSprite::getAccelY() const -> float {
|
||||
return ay;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
float MovingSprite::getZoomW() {
|
||||
auto MovingSprite::getZoomW() const -> float {
|
||||
return zoomW;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
float MovingSprite::getZoomH() {
|
||||
auto MovingSprite::getZoomH() const -> float {
|
||||
return zoomH;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
double MovingSprite::getAngle() {
|
||||
auto MovingSprite::getAngle() const -> double {
|
||||
return angle;
|
||||
}
|
||||
|
||||
@@ -180,23 +171,24 @@ void MovingSprite::decAngle(double value) {
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
bool MovingSprite::getRotate() {
|
||||
auto MovingSprite::getRotate() const -> bool {
|
||||
return rotateEnabled;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
Uint16 MovingSprite::getRotateSpeed() {
|
||||
auto MovingSprite::getRotateSpeed() const -> Uint16 {
|
||||
return rotateSpeed;
|
||||
}
|
||||
|
||||
// Establece la rotacion
|
||||
void MovingSprite::rotate() {
|
||||
if (enabled)
|
||||
if (enabled) {
|
||||
if (rotateEnabled) {
|
||||
if (counter % rotateSpeed == 0) {
|
||||
incAngle(rotateAmount);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
@@ -250,12 +242,12 @@ void MovingSprite::flip() {
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
SDL_FlipMode MovingSprite::getFlip() {
|
||||
auto MovingSprite::getFlip() -> SDL_FlipMode {
|
||||
return currentFlip;
|
||||
}
|
||||
|
||||
// Devuelve el rectangulo donde está el sprite
|
||||
SDL_Rect MovingSprite::getRect() {
|
||||
auto MovingSprite::getRect() -> SDL_Rect {
|
||||
const SDL_Rect rect = {(int)x, (int)y, w, h};
|
||||
return rect;
|
||||
}
|
||||
@@ -278,10 +270,10 @@ void MovingSprite::undoMoveY() {
|
||||
|
||||
// Pone a cero las velocidades de desplacamiento
|
||||
void MovingSprite::clearVel() {
|
||||
vx = vy = 0.0f;
|
||||
vx = vy = 0.0F;
|
||||
}
|
||||
|
||||
// Devuelve el incremento en el eje X en pixels
|
||||
int MovingSprite::getIncX() {
|
||||
auto MovingSprite::getIncX() const -> int {
|
||||
return (int)x - (int)xPrev;
|
||||
}
|
||||
@@ -22,16 +22,16 @@ class MovingSprite : public Sprite {
|
||||
float ax; // Aceleración en el eje X. Variación de la velocidad
|
||||
float ay; // Aceleración en el eje Y. Variación de la velocidad
|
||||
|
||||
float zoomW; // Zoom aplicado a la anchura
|
||||
float zoomH; // Zoom aplicado a la altura
|
||||
float zoomW{1}; // Zoom aplicado a la anchura
|
||||
float zoomH{1}; // Zoom aplicado a la altura
|
||||
|
||||
double angle; // Angulo para dibujarlo
|
||||
bool rotateEnabled; // Indica si ha de rotar
|
||||
int rotateSpeed; // Velocidad de giro
|
||||
double rotateAmount; // Cantidad de grados a girar en cada iteración
|
||||
int counter; // Contador interno
|
||||
SDL_Point *center; // Centro de rotación
|
||||
SDL_FlipMode currentFlip; // Indica como se voltea el sprite
|
||||
double angle{0.0}; // Angulo para dibujarlo
|
||||
bool rotateEnabled{false}; // Indica si ha de rotar
|
||||
int rotateSpeed{0}; // Velocidad de giro
|
||||
double rotateAmount{0.0}; // Cantidad de grados a girar en cada iteración
|
||||
int counter{0}; // Contador interno
|
||||
SDL_Point *center{nullptr}; // Centro de rotación
|
||||
SDL_FlipMode currentFlip{SDL_FLIP_NONE}; // Indica como se voltea el sprite
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
@@ -54,38 +54,38 @@ class MovingSprite : public Sprite {
|
||||
|
||||
// Obten el valor de la variable
|
||||
// cppcheck-suppress duplInheritedMember
|
||||
float getPosX();
|
||||
[[nodiscard]] auto getPosX() const -> float;
|
||||
|
||||
// Obten el valor de la variable
|
||||
// cppcheck-suppress duplInheritedMember
|
||||
float getPosY();
|
||||
[[nodiscard]] auto getPosY() const -> float;
|
||||
|
||||
// Obten el valor de la variable
|
||||
float getVelX();
|
||||
[[nodiscard]] auto getVelX() const -> float;
|
||||
|
||||
// Obten el valor de la variable
|
||||
float getVelY();
|
||||
[[nodiscard]] auto getVelY() const -> float;
|
||||
|
||||
// Obten el valor de la variable
|
||||
float getAccelX();
|
||||
[[nodiscard]] auto getAccelX() const -> float;
|
||||
|
||||
// Obten el valor de la variable
|
||||
float getAccelY();
|
||||
[[nodiscard]] auto getAccelY() const -> float;
|
||||
|
||||
// Obten el valor de la variable
|
||||
float getZoomW();
|
||||
[[nodiscard]] auto getZoomW() const -> float;
|
||||
|
||||
// Obten el valor de la variable
|
||||
float getZoomH();
|
||||
[[nodiscard]] auto getZoomH() const -> float;
|
||||
|
||||
// Obten el valor de la variable
|
||||
double getAngle();
|
||||
[[nodiscard]] auto getAngle() const -> double;
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
bool getRotate();
|
||||
[[nodiscard]] auto getRotate() const -> bool;
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
Uint16 getRotateSpeed();
|
||||
[[nodiscard]] auto getRotateSpeed() const -> Uint16;
|
||||
|
||||
// Establece la posición y el tamaño del objeto
|
||||
void setRect(SDL_Rect rect) override;
|
||||
@@ -115,7 +115,7 @@ class MovingSprite : public Sprite {
|
||||
void setZoomH(float value);
|
||||
|
||||
// Establece el valor de la variable
|
||||
void setAngle(double vaue);
|
||||
void setAngle(double value);
|
||||
|
||||
// Incrementa el valor de la variable
|
||||
void incAngle(double value);
|
||||
@@ -145,10 +145,10 @@ class MovingSprite : public Sprite {
|
||||
void flip();
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
SDL_FlipMode getFlip();
|
||||
auto getFlip() -> SDL_FlipMode;
|
||||
|
||||
// Devuelve el rectangulo donde está el sprite
|
||||
SDL_Rect getRect() override;
|
||||
auto getRect() -> SDL_Rect override;
|
||||
|
||||
// Deshace el último movimiento
|
||||
void undoMove();
|
||||
@@ -163,5 +163,5 @@ class MovingSprite : public Sprite {
|
||||
void clearVel();
|
||||
|
||||
// Devuelve el incremento en el eje X en pixels
|
||||
int getIncX();
|
||||
[[nodiscard]] auto getIncX() const -> int;
|
||||
};
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <algorithm> // for max, min
|
||||
#include <cmath> // for lround
|
||||
#include <cstring> // for memcpy
|
||||
#include <iostream> // for basic_ostream, operator<<, cout, endl
|
||||
#include <string> // for basic_string, char_traits, string
|
||||
@@ -108,7 +109,7 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer)
|
||||
}
|
||||
if (gameCanvas == nullptr) {
|
||||
if (Options::settings.console) {
|
||||
std::cout << "gameCanvas could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
|
||||
std::cout << "gameCanvas could not be created!\nSDL Error: " << SDL_GetError() << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -351,7 +352,7 @@ void Screen::applyFullscreen(bool fullscreen) {
|
||||
void Screen::applyWindowedLayout() {
|
||||
windowWidth = gameCanvasWidth;
|
||||
windowHeight = gameCanvasHeight;
|
||||
dest = {0, 0, gameCanvasWidth, gameCanvasHeight};
|
||||
dest = {.x = 0, .y = 0, .w = gameCanvasWidth, .h = gameCanvasHeight};
|
||||
|
||||
#ifdef __EMSCRIPTEN__
|
||||
windowWidth *= WASM_RENDER_SCALE;
|
||||
@@ -396,12 +397,12 @@ void Screen::computeFullscreenGameRect() {
|
||||
float ratio = (float)gameCanvasWidth / (float)gameCanvasHeight;
|
||||
if ((windowWidth - gameCanvasWidth) >= (windowHeight - gameCanvasHeight)) {
|
||||
dest.h = windowHeight;
|
||||
dest.w = (int)((windowHeight * ratio) + 0.5f);
|
||||
dest.w = static_cast<int>(std::lround(windowHeight * ratio));
|
||||
dest.x = (windowWidth - dest.w) / 2;
|
||||
dest.y = (windowHeight - dest.h) / 2;
|
||||
} else {
|
||||
dest.w = windowWidth;
|
||||
dest.h = (int)((windowWidth / ratio) + 0.5f);
|
||||
dest.h = static_cast<int>(std::lround(windowWidth / ratio));
|
||||
dest.x = (windowWidth - dest.w) / 2;
|
||||
dest.y = (windowHeight - dest.h) / 2;
|
||||
}
|
||||
@@ -579,7 +580,7 @@ void Screen::toggleShaderEnabled() {
|
||||
setShaderEnabled(!Options::video.shader.enabled);
|
||||
}
|
||||
|
||||
auto Screen::isShaderEnabled() const -> bool {
|
||||
auto Screen::isShaderEnabled() -> bool {
|
||||
return Options::video.shader.enabled;
|
||||
}
|
||||
|
||||
@@ -595,7 +596,7 @@ void Screen::setActiveShader(Rendering::ShaderType type) {
|
||||
notify(type == Rendering::ShaderType::CRTPI ? "Shader: CRTPI" : "Shader: POSTFX", MAGENTA, BLACK, DUR_MS);
|
||||
}
|
||||
|
||||
auto Screen::getActiveShader() const -> Rendering::ShaderType {
|
||||
auto Screen::getActiveShader() -> Rendering::ShaderType {
|
||||
return Options::video.shader.current_shader;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -46,7 +46,7 @@ class Screen {
|
||||
void setVideoMode(bool fullscreen); // Establece el modo de video
|
||||
void toggleVideoMode(); // Cambia entre pantalla completa y ventana
|
||||
void handleCanvasResized(); // En Emscripten, reaplica setVideoMode tras un cambio del navegador (salida de fullscreen con Esc, rotación). No-op fuera de Emscripten
|
||||
void syncFullscreenFlagFromBrowser(bool isFullscreen); // Sincroniza el flag interno de fullscreen con el estado real del navegador. Debe llamarse antes de diferir handleCanvasResized. No-op fuera de Emscripten
|
||||
static void syncFullscreenFlagFromBrowser(bool isFullscreen); // Sincroniza el flag interno de fullscreen con el estado real del navegador. Debe llamarse antes de diferir handleCanvasResized. No-op fuera de Emscripten
|
||||
void toggleIntegerScale(); // Alterna el escalado entero
|
||||
void setIntegerScale(bool enabled); // Establece el escalado entero
|
||||
void toggleVSync(); // Alterna el V-Sync
|
||||
@@ -66,13 +66,13 @@ class Screen {
|
||||
// GPU / shaders (post-procesado). En builds con NO_SHADERS (Emscripten) son no-op.
|
||||
void initShaders(); // Crea el backend GPU si no existe y lo inicializa
|
||||
void shutdownShaders(); // Libera el backend GPU
|
||||
auto isGpuAccelerated() const -> bool; // true si el backend existe y reporta hardware OK
|
||||
[[nodiscard]] auto isGpuAccelerated() const -> bool; // true si el backend existe y reporta hardware OK
|
||||
void setShaderEnabled(bool enabled); // Activa o desactiva el post-procesado (persiste)
|
||||
void toggleShaderEnabled(); // Alterna post-procesado
|
||||
auto isShaderEnabled() const -> bool; // Estado actual (lee options)
|
||||
[[nodiscard]] static auto isShaderEnabled() -> bool; // Estado actual (lee options)
|
||||
#ifndef NO_SHADERS
|
||||
void setActiveShader(Rendering::ShaderType type); // POSTFX o CRTPI
|
||||
auto getActiveShader() const -> Rendering::ShaderType;
|
||||
[[nodiscard]] static auto getActiveShader() -> Rendering::ShaderType;
|
||||
#endif
|
||||
void toggleActiveShader(); // Alterna POSTFX ↔ CRTPI
|
||||
|
||||
@@ -80,7 +80,7 @@ class Screen {
|
||||
// Retornen false si GPU off / shaders off / llista buida (igual que a aee_plus).
|
||||
auto nextPreset() -> bool;
|
||||
auto prevPreset() -> bool;
|
||||
auto getCurrentPresetName() const -> const char *;
|
||||
[[nodiscard]] auto getCurrentPresetName() const -> const char *;
|
||||
void applyCurrentPostFXPreset(); // Escriu el preset PostFX actiu al backend
|
||||
void applyCurrentCrtPiPreset(); // Escriu el preset CrtPi actiu al backend
|
||||
|
||||
|
||||
@@ -762,7 +762,7 @@ namespace Rendering {
|
||||
}
|
||||
|
||||
// Copia directa — el upscale lo hace la GPU en el primer render pass
|
||||
std::memcpy(mapped, pixels, static_cast<size_t>(width * height * 4));
|
||||
std::memcpy(mapped, pixels, static_cast<size_t>(width) * height * 4);
|
||||
|
||||
SDL_UnmapGPUTransferBuffer(device_, upload_buffer_);
|
||||
}
|
||||
|
||||
@@ -2,13 +2,14 @@
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
namespace Rendering {
|
||||
|
||||
/** @brief Identificador del shader de post-procesado activo */
|
||||
enum class ShaderType { POSTFX,
|
||||
enum class ShaderType : std::uint8_t { POSTFX,
|
||||
CRTPI };
|
||||
|
||||
/**
|
||||
|
||||
@@ -27,8 +27,8 @@ void SmartSprite::init() {
|
||||
// Actualiza la posición y comprueba si ha llegado a su destino
|
||||
void SmartSprite::update() {
|
||||
if (enabled) {
|
||||
// Actualiza las variables internas del objeto
|
||||
MovingSprite::update();
|
||||
// Actualiza animació + posició (delegant en AnimatedSprite::update)
|
||||
AnimatedSprite::update();
|
||||
|
||||
// Comprueba el movimiento
|
||||
checkMove();
|
||||
@@ -47,7 +47,7 @@ void SmartSprite::render() {
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int SmartSprite::getEnabledCounter() {
|
||||
auto SmartSprite::getEnabledCounter() const -> int {
|
||||
return enabledCounter;
|
||||
}
|
||||
|
||||
@@ -67,12 +67,12 @@ void SmartSprite::setDestY(int y) {
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int SmartSprite::getDestX() {
|
||||
auto SmartSprite::getDestX() const -> int {
|
||||
return destX;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int SmartSprite::getDestY() {
|
||||
auto SmartSprite::getDestY() const -> int {
|
||||
return destY;
|
||||
}
|
||||
|
||||
@@ -86,8 +86,8 @@ void SmartSprite::checkMove() {
|
||||
setPosX(destX);
|
||||
|
||||
// Lo detiene
|
||||
setVelX(0.0f);
|
||||
setAccelX(0.0f);
|
||||
setVelX(0.0F);
|
||||
setAccelX(0.0F);
|
||||
}
|
||||
}
|
||||
// Comprueba si se desplaza en el eje X hacia la izquierda
|
||||
@@ -98,8 +98,8 @@ void SmartSprite::checkMove() {
|
||||
setPosX(destX);
|
||||
|
||||
// Lo detiene
|
||||
setVelX(0.0f);
|
||||
setAccelX(0.0f);
|
||||
setVelX(0.0F);
|
||||
setAccelX(0.0F);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,8 +111,8 @@ void SmartSprite::checkMove() {
|
||||
setPosY(destY);
|
||||
|
||||
// Lo detiene
|
||||
setVelY(0.0f);
|
||||
setAccelY(0.0f);
|
||||
setVelY(0.0F);
|
||||
setAccelY(0.0F);
|
||||
}
|
||||
}
|
||||
// Comprueba si se desplaza en el eje Y hacia arriba
|
||||
@@ -123,8 +123,8 @@ void SmartSprite::checkMove() {
|
||||
setPosY(destY);
|
||||
|
||||
// Lo detiene
|
||||
setVelY(0.0f);
|
||||
setAccelY(0.0f);
|
||||
setVelY(0.0F);
|
||||
setAccelY(0.0F);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -132,7 +132,7 @@ void SmartSprite::checkMove() {
|
||||
// Comprueba si ha terminado
|
||||
void SmartSprite::checkFinished() {
|
||||
// Comprueba si ha llegado a su destino
|
||||
onDestination = (getPosX() == destX && getPosY() == destY) ? true : false;
|
||||
onDestination = getPosX() == destX && getPosY() == destY;
|
||||
|
||||
if (onDestination) { // Si esta en el destino comprueba su contador
|
||||
if (enabledCounter == 0) { // Si ha llegado a cero, deshabilita el objeto y lo marca como finalizado
|
||||
@@ -144,11 +144,11 @@ void SmartSprite::checkFinished() {
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
bool SmartSprite::isOnDestination() {
|
||||
auto SmartSprite::isOnDestination() const -> bool {
|
||||
return onDestination;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
bool SmartSprite::hasFinished() {
|
||||
auto SmartSprite::hasFinished() const -> bool {
|
||||
return finished;
|
||||
}
|
||||
@@ -35,7 +35,7 @@ class SmartSprite : public AnimatedSprite {
|
||||
void render() override;
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int getEnabledCounter();
|
||||
[[nodiscard]] auto getEnabledCounter() const -> int;
|
||||
|
||||
// Establece el valor de la variable
|
||||
void setEnabledCounter(int value);
|
||||
@@ -47,14 +47,14 @@ class SmartSprite : public AnimatedSprite {
|
||||
void setDestY(int y);
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int getDestX();
|
||||
[[nodiscard]] auto getDestX() const -> int;
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int getDestY();
|
||||
[[nodiscard]] auto getDestY() const -> int;
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
bool isOnDestination();
|
||||
[[nodiscard]] auto isOnDestination() const -> bool;
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
bool hasFinished();
|
||||
[[nodiscard]] auto hasFinished() const -> bool;
|
||||
};
|
||||
|
||||
@@ -39,22 +39,22 @@ void Sprite::render() {
|
||||
}
|
||||
|
||||
// Obten el valor de la variable
|
||||
int Sprite::getPosX() {
|
||||
auto Sprite::getPosX() const -> int {
|
||||
return x;
|
||||
}
|
||||
|
||||
// Obten el valor de la variable
|
||||
int Sprite::getPosY() {
|
||||
auto Sprite::getPosY() const -> int {
|
||||
return y;
|
||||
}
|
||||
|
||||
// Obten el valor de la variable
|
||||
int Sprite::getWidth() {
|
||||
auto Sprite::getWidth() const -> int {
|
||||
return w;
|
||||
}
|
||||
|
||||
// Obten el valor de la variable
|
||||
int Sprite::getHeight() {
|
||||
auto Sprite::getHeight() const -> int {
|
||||
return h;
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@ void Sprite::setHeight(int h) {
|
||||
}
|
||||
|
||||
// Obten el valor de la variable
|
||||
SDL_Rect Sprite::getSpriteClip() {
|
||||
auto Sprite::getSpriteClip() -> SDL_Rect {
|
||||
return spriteClip;
|
||||
}
|
||||
|
||||
@@ -96,11 +96,11 @@ void Sprite::setSpriteClip(SDL_Rect rect) {
|
||||
|
||||
// Establece el valor de la variable
|
||||
void Sprite::setSpriteClip(int x, int y, int w, int h) {
|
||||
spriteClip = {x, y, w, h};
|
||||
spriteClip = {.x = x, .y = y, .w = w, .h = h};
|
||||
}
|
||||
|
||||
// Obten el valor de la variable
|
||||
Texture *Sprite::getTexture() {
|
||||
auto Sprite::getTexture() -> Texture * {
|
||||
return texture;
|
||||
}
|
||||
|
||||
@@ -110,7 +110,7 @@ void Sprite::setTexture(Texture *texture) {
|
||||
}
|
||||
|
||||
// Obten el valor de la variable
|
||||
SDL_Renderer *Sprite::getRenderer() {
|
||||
auto Sprite::getRenderer() -> SDL_Renderer * {
|
||||
return renderer;
|
||||
}
|
||||
|
||||
@@ -125,12 +125,12 @@ void Sprite::setEnabled(bool value) {
|
||||
}
|
||||
|
||||
// Comprueba si el objeto está habilitado
|
||||
bool Sprite::isEnabled() {
|
||||
auto Sprite::isEnabled() -> bool {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
// Devuelve el rectangulo donde está el sprite
|
||||
SDL_Rect Sprite::getRect() {
|
||||
auto Sprite::getRect() -> SDL_Rect {
|
||||
SDL_Rect rect = {x, y, w, h};
|
||||
return rect;
|
||||
}
|
||||
|
||||
@@ -29,16 +29,16 @@ class Sprite {
|
||||
virtual void render();
|
||||
|
||||
// Obten el valor de la variable
|
||||
int getPosX();
|
||||
[[nodiscard]] auto getPosX() const -> int;
|
||||
|
||||
// Obten el valor de la variable
|
||||
int getPosY();
|
||||
[[nodiscard]] auto getPosY() const -> int;
|
||||
|
||||
// Obten el valor de la variable
|
||||
int getWidth();
|
||||
[[nodiscard]] auto getWidth() const -> int;
|
||||
|
||||
// Obten el valor de la variable
|
||||
int getHeight();
|
||||
[[nodiscard]] auto getHeight() const -> int;
|
||||
|
||||
// Establece la posición del objeto
|
||||
void setPos(SDL_Rect rect);
|
||||
@@ -56,7 +56,7 @@ class Sprite {
|
||||
void setHeight(int h);
|
||||
|
||||
// Obten el valor de la variable
|
||||
SDL_Rect getSpriteClip();
|
||||
auto getSpriteClip() -> SDL_Rect;
|
||||
|
||||
// Establece el valor de la variable
|
||||
void setSpriteClip(SDL_Rect rect);
|
||||
@@ -65,13 +65,13 @@ class Sprite {
|
||||
void setSpriteClip(int x, int y, int w, int h);
|
||||
|
||||
// Obten el valor de la variable
|
||||
Texture *getTexture();
|
||||
auto getTexture() -> Texture *;
|
||||
|
||||
// Establece el valor de la variable
|
||||
void setTexture(Texture *texture);
|
||||
|
||||
// Obten el valor de la variable
|
||||
SDL_Renderer *getRenderer();
|
||||
auto getRenderer() -> SDL_Renderer *;
|
||||
|
||||
// Establece el valor de la variable
|
||||
void setRenderer(SDL_Renderer *renderer);
|
||||
@@ -80,10 +80,10 @@ class Sprite {
|
||||
virtual void setEnabled(bool value);
|
||||
|
||||
// Comprueba si el objeto está habilitado
|
||||
virtual bool isEnabled();
|
||||
virtual auto isEnabled() -> bool;
|
||||
|
||||
// Devuelve el rectangulo donde está el sprite
|
||||
virtual SDL_Rect getRect();
|
||||
virtual auto getRect() -> SDL_Rect;
|
||||
|
||||
// Establece los valores de posición y tamaño del sprite
|
||||
virtual void setRect(SDL_Rect rect);
|
||||
|
||||
@@ -39,25 +39,25 @@ static void computeTextFileOffsets(textFile_t &tf) {
|
||||
}
|
||||
|
||||
// Llena una estructuta textFile_t desde un fichero
|
||||
textFile_t LoadTextFile(const std::string &file, bool verbose) {
|
||||
auto LoadTextFile(const std::string &file, bool verbose) -> textFile_t {
|
||||
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;
|
||||
tf.offset[i].w = 0;
|
||||
for (auto &i : tf.offset) {
|
||||
i.x = 0;
|
||||
i.y = 0;
|
||||
i.w = 0;
|
||||
}
|
||||
|
||||
const std::string filename = file.substr(file.find_last_of("\\/") + 1).c_str();
|
||||
const std::string filename = file.substr(file.find_last_of("\\/") + 1);
|
||||
std::ifstream rfile(file);
|
||||
if (rfile.is_open() && rfile.good()) {
|
||||
parseTextFileStream(rfile, tf);
|
||||
if (verbose) {
|
||||
std::cout << "Text loaded: " << filename.c_str() << std::endl;
|
||||
std::cout << "Text loaded: " << filename.c_str() << '\n';
|
||||
}
|
||||
} else if (verbose) {
|
||||
std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl;
|
||||
std::cout << "Warning: Unable to open " << filename.c_str() << " file" << '\n';
|
||||
}
|
||||
|
||||
computeTextFileOffsets(tf);
|
||||
@@ -65,21 +65,21 @@ textFile_t LoadTextFile(const 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) {
|
||||
auto LoadTextFileFromMemory(const std::vector<uint8_t> &bytes, bool verbose) -> textFile_t {
|
||||
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;
|
||||
tf.offset[i].w = 0;
|
||||
for (auto &i : tf.offset) {
|
||||
i.x = 0;
|
||||
i.y = 0;
|
||||
i.w = 0;
|
||||
}
|
||||
if (!bytes.empty()) {
|
||||
std::string content(reinterpret_cast<const char *>(bytes.data()), bytes.size());
|
||||
std::stringstream ss(content);
|
||||
parseTextFileStream(ss, tf);
|
||||
if (verbose) {
|
||||
std::cout << "Text loaded from memory" << std::endl;
|
||||
std::cout << "Text loaded from memory" << '\n';
|
||||
}
|
||||
}
|
||||
computeTextFileOffsets(tf);
|
||||
@@ -170,9 +170,8 @@ Text::Text(const std::vector<uint8_t> &pngBytes, const std::vector<uint8_t> &txt
|
||||
// Destructor
|
||||
Text::~Text() {
|
||||
delete sprite;
|
||||
if (texture != nullptr) {
|
||||
|
||||
delete texture;
|
||||
}
|
||||
}
|
||||
|
||||
// Escribe texto en pantalla
|
||||
@@ -187,11 +186,11 @@ void Text::write(int x, int y, const std::string &text, int kerning, int lenght)
|
||||
const int width = sprite->getWidth();
|
||||
const int height = sprite->getHeight();
|
||||
for (int i = 0; i < lenght; ++i) {
|
||||
const int index = text[i];
|
||||
const int index = static_cast<unsigned char>(text[i]);
|
||||
sprite->setSpriteClip(offset[index].x, offset[index].y, width, height);
|
||||
sprite->setPosX(x + shift);
|
||||
sprite->render();
|
||||
shift += fixedWidth ? boxWidth : (offset[int(text[i])].w + kerning);
|
||||
shift += fixedWidth ? boxWidth : (offset[index].w + kerning);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -249,18 +248,19 @@ 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) {
|
||||
auto Text::lenght(const std::string &text, int kerning) -> int {
|
||||
int shift = 0;
|
||||
|
||||
for (int i = 0; i < (int)text.length(); ++i)
|
||||
shift += (offset[int(text[i])].w + kerning);
|
||||
for (int i = 0; i < (int)text.length(); ++i) {
|
||||
shift += (offset[static_cast<unsigned char>(text[i])].w + kerning);
|
||||
}
|
||||
|
||||
// Descuenta el kerning del último caracter
|
||||
return shift - kerning;
|
||||
}
|
||||
|
||||
// Devuelve el valor de la variable
|
||||
int Text::getCharacterSize() {
|
||||
auto Text::getCharacterSize() const -> int {
|
||||
return boxWidth;
|
||||
}
|
||||
|
||||
|
||||
@@ -28,10 +28,10 @@ struct textFile_t {
|
||||
};
|
||||
|
||||
// Llena una estructuta textFile_t desde un fichero
|
||||
textFile_t LoadTextFile(const std::string &file, bool verbose = false);
|
||||
auto LoadTextFile(const std::string &file, bool verbose = false) -> textFile_t;
|
||||
|
||||
// Llena una estructura textFile_t desde bytes en memoria
|
||||
textFile_t LoadTextFileFromMemory(const std::vector<uint8_t> &bytes, bool verbose = false);
|
||||
auto LoadTextFileFromMemory(const std::vector<uint8_t> &bytes, bool verbose = false) -> textFile_t;
|
||||
|
||||
// Clase texto. Pinta texto en pantalla a partir de un bitmap
|
||||
class Text {
|
||||
@@ -60,7 +60,7 @@ class Text {
|
||||
|
||||
// No copiable (gestiona memoria dinámica)
|
||||
Text(const Text &) = delete;
|
||||
Text &operator=(const Text &) = delete;
|
||||
auto operator=(const Text &) -> Text & = delete;
|
||||
|
||||
// Escribe el texto en pantalla
|
||||
void write(int x, int y, const std::string &text, int kerning = 1, int lenght = -1);
|
||||
@@ -78,10 +78,10 @@ class Text {
|
||||
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(const std::string &text, int kerning = 1);
|
||||
auto lenght(const std::string &text, int kerning = 1) -> int;
|
||||
|
||||
// Devuelve el valor de la variable
|
||||
int getCharacterSize();
|
||||
[[nodiscard]] auto getCharacterSize() const -> int;
|
||||
|
||||
// Recarga la textura
|
||||
void reLoadTexture();
|
||||
|
||||
@@ -2,11 +2,12 @@
|
||||
#include "core/rendering/texture.h"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <stdlib.h> // for exit
|
||||
|
||||
#include <cstdlib> // for exit
|
||||
#include <iostream> // for basic_ostream, operator<<, cout, endl
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "external/stb_image.h" // for stbi_failure_reason, stbi_image_free
|
||||
#include "core/resources/resource_helper.h" // for loadFile (pack + filesystem fallback)
|
||||
#include "external/stb_image.h" // for stbi_failure_reason, stbi_image_free
|
||||
|
||||
SDL_ScaleMode Texture::currentScaleMode = SDL_SCALEMODE_NEAREST;
|
||||
|
||||
@@ -32,8 +33,7 @@ Texture::Texture(SDL_Renderer *renderer, const std::vector<uint8_t> &bytes, bool
|
||||
: texture(nullptr),
|
||||
renderer(renderer),
|
||||
width(0),
|
||||
height(0),
|
||||
path("") {
|
||||
height(0) {
|
||||
if (!bytes.empty()) {
|
||||
loadFromMemory(bytes.data(), bytes.size(), renderer, verbose);
|
||||
}
|
||||
@@ -46,7 +46,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) {
|
||||
static auto createTextureFromPixels(SDL_Renderer *renderer, unsigned char *data, int w, int h, int *out_w, int *out_h) -> SDL_Texture * {
|
||||
const int pitch = 4 * w;
|
||||
SDL_Surface *loadedSurface = SDL_CreateSurfaceFrom(w, h, SDL_PIXELFORMAT_RGBA32, static_cast<void *>(data), pitch);
|
||||
if (loadedSurface == nullptr) {
|
||||
@@ -62,23 +62,32 @@ static SDL_Texture *createTextureFromPixels(SDL_Renderer *renderer, unsigned cha
|
||||
return newTexture;
|
||||
}
|
||||
|
||||
// Carga una imagen desde un fichero
|
||||
bool Texture::loadFromFile(const std::string &path, SDL_Renderer *renderer, bool verbose) {
|
||||
// Carga una imagen desde un fichero (vía ResourceHelper: pack si està inicialitzat, filesystem si no)
|
||||
auto Texture::loadFromFile(const std::string &path, SDL_Renderer *renderer, bool verbose) -> bool {
|
||||
const std::string filename = path.substr(path.find_last_of("\\/") + 1);
|
||||
|
||||
auto bytes = ResourceHelper::loadFile(path);
|
||||
if (bytes.empty()) {
|
||||
SDL_Log("Loading image failed: can't open %s", path.c_str());
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int req_format = STBI_rgb_alpha;
|
||||
int w, h, orig_format;
|
||||
unsigned char *data = stbi_load(path.c_str(), &w, &h, &orig_format, req_format);
|
||||
int w;
|
||||
int h;
|
||||
int orig_format;
|
||||
unsigned char *data = stbi_load_from_memory(bytes.data(), static_cast<int>(bytes.size()), &w, &h, &orig_format, req_format);
|
||||
if (data == nullptr) {
|
||||
SDL_Log("Loading image failed: %s", stbi_failure_reason());
|
||||
exit(1);
|
||||
} else if (verbose) {
|
||||
std::cout << "Image loaded: " << filename.c_str() << std::endl;
|
||||
std::cout << "Image loaded: " << filename.c_str() << '\n';
|
||||
}
|
||||
|
||||
unload();
|
||||
SDL_Texture *newTexture = createTextureFromPixels(renderer, data, w, h, &this->width, &this->height);
|
||||
if (newTexture == nullptr && verbose) {
|
||||
std::cout << "Unable to load image " << path.c_str() << std::endl;
|
||||
std::cout << "Unable to load image " << path.c_str() << '\n';
|
||||
}
|
||||
|
||||
stbi_image_free(data);
|
||||
@@ -87,8 +96,10 @@ bool Texture::loadFromFile(const std::string &path, SDL_Renderer *renderer, bool
|
||||
}
|
||||
|
||||
// Carga una imagen desde bytes en memoria
|
||||
bool Texture::loadFromMemory(const uint8_t *data, size_t size, SDL_Renderer *renderer, bool verbose) {
|
||||
int w, h, orig_format;
|
||||
auto Texture::loadFromMemory(const uint8_t *data, size_t size, SDL_Renderer *renderer, bool verbose) -> bool {
|
||||
int w;
|
||||
int h;
|
||||
int orig_format;
|
||||
unsigned char *pixels = stbi_load_from_memory(data, (int)size, &w, &h, &orig_format, STBI_rgb_alpha);
|
||||
if (pixels == nullptr) {
|
||||
SDL_Log("Loading image from memory failed: %s", stbi_failure_reason());
|
||||
@@ -98,7 +109,7 @@ bool Texture::loadFromMemory(const uint8_t *data, size_t size, SDL_Renderer *ren
|
||||
unload();
|
||||
SDL_Texture *newTexture = createTextureFromPixels(renderer, pixels, w, h, &this->width, &this->height);
|
||||
if (newTexture == nullptr && verbose) {
|
||||
std::cout << "Unable to create texture from memory" << std::endl;
|
||||
std::cout << "Unable to create texture from memory" << '\n';
|
||||
}
|
||||
|
||||
stbi_image_free(pixels);
|
||||
@@ -107,11 +118,11 @@ bool Texture::loadFromMemory(const uint8_t *data, size_t size, SDL_Renderer *ren
|
||||
}
|
||||
|
||||
// Crea una textura en blanco
|
||||
bool Texture::createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess access) {
|
||||
auto Texture::createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess access) -> bool {
|
||||
// Crea una textura sin inicializar
|
||||
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, access, width, height);
|
||||
if (texture == nullptr) {
|
||||
std::cout << "Unable to create blank texture! SDL Error: " << SDL_GetError() << std::endl;
|
||||
std::cout << "Unable to create blank texture! SDL Error: " << SDL_GetError() << '\n';
|
||||
} else {
|
||||
this->width = width;
|
||||
this->height = height;
|
||||
@@ -165,7 +176,7 @@ void Texture::render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip, float
|
||||
SDL_FRect srcRect;
|
||||
SDL_FRect *srcRectPtr = nullptr;
|
||||
if (clip != nullptr) {
|
||||
srcRect = {(float)clip->x, (float)clip->y, (float)clip->w, (float)clip->h};
|
||||
srcRect = {.x = (float)clip->x, .y = (float)clip->y, .w = (float)clip->w, .h = (float)clip->h};
|
||||
srcRectPtr = &srcRect;
|
||||
}
|
||||
|
||||
@@ -173,7 +184,7 @@ void Texture::render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip, float
|
||||
SDL_FPoint fCenter;
|
||||
SDL_FPoint *fCenterPtr = nullptr;
|
||||
if (center != nullptr) {
|
||||
fCenter = {(float)center->x, (float)center->y};
|
||||
fCenter = {.x = (float)center->x, .y = (float)center->y};
|
||||
fCenterPtr = &fCenter;
|
||||
}
|
||||
|
||||
@@ -187,21 +198,21 @@ void Texture::setAsRenderTarget(SDL_Renderer *renderer) {
|
||||
}
|
||||
|
||||
// Obtiene el ancho de la imagen
|
||||
int Texture::getWidth() {
|
||||
auto Texture::getWidth() const -> int {
|
||||
return width;
|
||||
}
|
||||
|
||||
// Obtiene el alto de la imagen
|
||||
int Texture::getHeight() {
|
||||
auto Texture::getHeight() const -> int {
|
||||
return height;
|
||||
}
|
||||
|
||||
// Recarga la textura
|
||||
bool Texture::reLoad() {
|
||||
auto Texture::reLoad() -> bool {
|
||||
return loadFromFile(path, renderer);
|
||||
}
|
||||
|
||||
// Obtiene la textura
|
||||
SDL_Texture *Texture::getSDLTexture() {
|
||||
auto Texture::getSDLTexture() -> SDL_Texture * {
|
||||
return texture;
|
||||
}
|
||||
@@ -33,13 +33,13 @@ class Texture {
|
||||
~Texture();
|
||||
|
||||
// Carga una imagen desde un fichero
|
||||
bool loadFromFile(const std::string &path, SDL_Renderer *renderer, bool verbose = false);
|
||||
auto loadFromFile(const std::string &path, SDL_Renderer *renderer, bool verbose = false) -> bool;
|
||||
|
||||
// Carga una imagen desde bytes en memoria
|
||||
bool loadFromMemory(const uint8_t *data, size_t size, SDL_Renderer *renderer, bool verbose = false);
|
||||
auto loadFromMemory(const uint8_t *data, size_t size, SDL_Renderer *renderer, bool verbose = false) -> bool;
|
||||
|
||||
// Crea una textura en blanco
|
||||
bool createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess = SDL_TEXTUREACCESS_STREAMING);
|
||||
auto createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess /*access*/ = SDL_TEXTUREACCESS_STREAMING) -> bool;
|
||||
|
||||
// Libera la memoria de la textura
|
||||
void unload();
|
||||
@@ -60,14 +60,14 @@ class Texture {
|
||||
void setAsRenderTarget(SDL_Renderer *renderer);
|
||||
|
||||
// Obtiene el ancho de la imagen
|
||||
int getWidth();
|
||||
[[nodiscard]] auto getWidth() const -> int;
|
||||
|
||||
// Obtiene el alto de la imagen
|
||||
int getHeight();
|
||||
[[nodiscard]] auto getHeight() const -> int;
|
||||
|
||||
// Recarga la textura
|
||||
bool reLoad();
|
||||
auto reLoad() -> bool;
|
||||
|
||||
// Obtiene la textura
|
||||
SDL_Texture *getSDLTexture();
|
||||
auto getSDLTexture() -> SDL_Texture *;
|
||||
};
|
||||
|
||||
@@ -4,19 +4,7 @@
|
||||
|
||||
// Constructor
|
||||
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) {
|
||||
: text(text) {
|
||||
}
|
||||
|
||||
// Actualiza el objeto
|
||||
@@ -87,7 +75,7 @@ void Writer::setEnabled(bool value) {
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
bool Writer::IsEnabled() {
|
||||
auto Writer::IsEnabled() const -> bool {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
@@ -97,7 +85,7 @@ void Writer::setEnabledCounter(int time) {
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int Writer::getEnabledCounter() {
|
||||
auto Writer::getEnabledCounter() const -> int {
|
||||
return enabledCounter;
|
||||
}
|
||||
|
||||
@@ -107,6 +95,6 @@ void Writer::center(int x) {
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
bool Writer::hasFinished() {
|
||||
auto Writer::hasFinished() const -> bool {
|
||||
return finished;
|
||||
}
|
||||
@@ -10,18 +10,18 @@ class Writer {
|
||||
Text *text; // Objeto encargado de escribir el texto
|
||||
|
||||
// Variables
|
||||
int posX; // Posicion en el eje X donde empezar a escribir el texto
|
||||
int posY; // Posicion en el eje Y donde empezar a escribir el texto
|
||||
int kerning; // Kerning del texto, es decir, espaciado entre caracteres
|
||||
int posX{0}; // Posicion en el eje X donde empezar a escribir el texto
|
||||
int posY{0}; // Posicion en el eje Y donde empezar a escribir el texto
|
||||
int kerning{0}; // Kerning del texto, es decir, espaciado entre caracteres
|
||||
std::string caption; // El texto para escribir
|
||||
int speed; // Velocidad de escritura
|
||||
int writingCounter; // Temporizador de escritura para cada caracter
|
||||
int index; // Posición del texto que se está escribiendo
|
||||
int lenght; // Longitud de la cadena a escribir
|
||||
bool completed; // Indica si se ha escrito todo el texto
|
||||
bool enabled; // Indica si el objeto está habilitado
|
||||
int enabledCounter; // Temporizador para deshabilitar el objeto
|
||||
bool finished; // Indica si ya ha terminado
|
||||
int speed{0}; // Velocidad de escritura
|
||||
int writingCounter{0}; // Temporizador de escritura para cada caracter
|
||||
int index{0}; // Posición del texto que se está escribiendo
|
||||
int lenght{0}; // Longitud de la cadena a escribir
|
||||
bool completed{false}; // Indica si se ha escrito todo el texto
|
||||
bool enabled{false}; // Indica si el objeto está habilitado
|
||||
int enabledCounter{0}; // Temporizador para deshabilitar el objeto
|
||||
bool finished{false}; // Indica si ya ha terminado
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
@@ -52,17 +52,17 @@ class Writer {
|
||||
void setEnabled(bool value);
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
bool IsEnabled();
|
||||
[[nodiscard]] auto IsEnabled() const -> bool;
|
||||
|
||||
// Establece el valor de la variable
|
||||
void setEnabledCounter(int time);
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int getEnabledCounter();
|
||||
[[nodiscard]] auto getEnabledCounter() const -> int;
|
||||
|
||||
// Centra la cadena de texto a un punto X
|
||||
void center(int x);
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
bool hasFinished();
|
||||
[[nodiscard]] auto hasFinished() const -> bool;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user