Primer commit
This commit is contained in:
11
.gitignore
vendored
Normal file
11
.gitignore
vendored
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
.vscode
|
||||||
|
*.DS_Store
|
||||||
|
thumbs.db
|
||||||
|
*.exe
|
||||||
|
*_macos
|
||||||
|
*_linux
|
||||||
|
*.dmg
|
||||||
|
*.tar.gz
|
||||||
|
*.zip
|
||||||
|
*.app
|
||||||
|
*_debug*
|
||||||
534
animatedsprite.cpp
Normal file
534
animatedsprite.cpp
Normal file
@@ -0,0 +1,534 @@
|
|||||||
|
#include "animatedsprite.h"
|
||||||
|
|
||||||
|
// Carga la animación desde un fichero
|
||||||
|
animatedSprite_t loadAnimationFromFile(Texture *texture, std::string filePath, bool verbose)
|
||||||
|
{
|
||||||
|
// Inicializa variables
|
||||||
|
animatedSprite_t as;
|
||||||
|
as.texture = texture;
|
||||||
|
int framesPerRow = 0;
|
||||||
|
int frameWidth = 0;
|
||||||
|
int frameHeight = 0;
|
||||||
|
int maxTiles = 0;
|
||||||
|
|
||||||
|
const std::string filename = filePath.substr(filePath.find_last_of("\\/") + 1);
|
||||||
|
std::ifstream file(filePath);
|
||||||
|
std::string line;
|
||||||
|
|
||||||
|
// El fichero se puede abrir
|
||||||
|
if (file.good())
|
||||||
|
{
|
||||||
|
// Procesa el fichero linea a linea
|
||||||
|
if (verbose)
|
||||||
|
{
|
||||||
|
std::cout << "Animation loaded: " << filename << std::endl;
|
||||||
|
}
|
||||||
|
while (std::getline(file, line))
|
||||||
|
{
|
||||||
|
// Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación
|
||||||
|
if (line == "[animation]")
|
||||||
|
{
|
||||||
|
animation_t buffer;
|
||||||
|
buffer.counter = 0;
|
||||||
|
buffer.currentFrame = 0;
|
||||||
|
buffer.completed = false;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
std::getline(file, line);
|
||||||
|
|
||||||
|
// Encuentra la posición del caracter '='
|
||||||
|
int pos = line.find("=");
|
||||||
|
|
||||||
|
// Procesa las dos subcadenas
|
||||||
|
if (pos != (int)line.npos)
|
||||||
|
{
|
||||||
|
if (line.substr(0, pos) == "name")
|
||||||
|
{
|
||||||
|
buffer.name = line.substr(pos + 1, line.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (line.substr(0, pos) == "speed")
|
||||||
|
{
|
||||||
|
buffer.speed = std::stoi(line.substr(pos + 1, line.length()));
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (line.substr(0, pos) == "loop")
|
||||||
|
{
|
||||||
|
buffer.loop = std::stoi(line.substr(pos + 1, line.length()));
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (line.substr(0, pos) == "frames")
|
||||||
|
{
|
||||||
|
// Se introducen los valores separados por comas en un vector
|
||||||
|
std::stringstream ss(line.substr(pos + 1, line.length()));
|
||||||
|
std::string tmp;
|
||||||
|
SDL_Rect rect = {0, 0, frameWidth, frameHeight};
|
||||||
|
while (getline(ss, tmp, ','))
|
||||||
|
{
|
||||||
|
// Comprueba que el tile no sea mayor que el maximo indice permitido
|
||||||
|
const int numTile = std::stoi(tmp) > maxTiles ? 0 : std::stoi(tmp);
|
||||||
|
rect.x = (numTile % framesPerRow) * frameWidth;
|
||||||
|
rect.y = (numTile / framesPerRow) * frameHeight;
|
||||||
|
buffer.frames.push_back(rect);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while (line != "[/animation]");
|
||||||
|
|
||||||
|
// Añade la animación al vector de animaciones
|
||||||
|
as.animations.push_back(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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("=");
|
||||||
|
|
||||||
|
// Procesa las dos subcadenas
|
||||||
|
if (pos != (int)line.npos)
|
||||||
|
{
|
||||||
|
if (line.substr(0, pos) == "framesPerRow")
|
||||||
|
{
|
||||||
|
framesPerRow = std::stoi(line.substr(pos + 1, line.length()));
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (line.substr(0, pos) == "frameWidth")
|
||||||
|
{
|
||||||
|
frameWidth = std::stoi(line.substr(pos + 1, line.length()));
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Normaliza valores
|
||||||
|
if (framesPerRow == 0 && frameWidth > 0)
|
||||||
|
{
|
||||||
|
framesPerRow = texture->getWidth() / frameWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (maxTiles == 0 && frameWidth > 0 && frameHeight > 0)
|
||||||
|
{
|
||||||
|
const int w = texture->getWidth() / frameWidth;
|
||||||
|
const int h = texture->getHeight() / frameHeight;
|
||||||
|
maxTiles = w * h;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cierra el fichero
|
||||||
|
file.close();
|
||||||
|
}
|
||||||
|
// El fichero no se puede abrir
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (verbose)
|
||||||
|
{
|
||||||
|
std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return as;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Constructor
|
||||||
|
AnimatedSprite::AnimatedSprite(Texture *texture, SDL_Renderer *renderer, std::string file, std::vector<std::string> *buffer)
|
||||||
|
{
|
||||||
|
// Copia los punteros
|
||||||
|
setTexture(texture);
|
||||||
|
setRenderer(renderer);
|
||||||
|
|
||||||
|
// Carga las animaciones
|
||||||
|
if (file != "")
|
||||||
|
{
|
||||||
|
animatedSprite_t as = loadAnimationFromFile(texture, file);
|
||||||
|
|
||||||
|
// Copia los datos de las animaciones
|
||||||
|
for (auto animation : as.animations)
|
||||||
|
{
|
||||||
|
this->animation.push_back(animation);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (buffer)
|
||||||
|
{
|
||||||
|
loadFromVector(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Inicializa variables
|
||||||
|
currentAnimation = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Constructor
|
||||||
|
AnimatedSprite::AnimatedSprite(SDL_Renderer *renderer, animatedSprite_t *animation)
|
||||||
|
{
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Destructor
|
||||||
|
AnimatedSprite::~AnimatedSprite()
|
||||||
|
{
|
||||||
|
for (auto &a : animation)
|
||||||
|
{
|
||||||
|
a.frames.clear();
|
||||||
|
}
|
||||||
|
animation.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene el indice de la animación a partir del nombre
|
||||||
|
int AnimatedSprite::getIndex(std::string name)
|
||||||
|
{
|
||||||
|
int index = -1;
|
||||||
|
|
||||||
|
for (auto a : animation)
|
||||||
|
{
|
||||||
|
index++;
|
||||||
|
if (a.name == name)
|
||||||
|
{
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "** Warning: could not find \"" << name.c_str() << "\" animation" << std::endl;
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calcula el frame correspondiente a la animación
|
||||||
|
void AnimatedSprite::animate()
|
||||||
|
{
|
||||||
|
if (!enabled || animation[currentAnimation].speed == 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calcula el frame actual a partir del contador
|
||||||
|
animation[currentAnimation].currentFrame = animation[currentAnimation].counter / animation[currentAnimation].speed;
|
||||||
|
|
||||||
|
// Si alcanza el final de la animación, reinicia el contador de la animación
|
||||||
|
// en función de la variable loop y coloca el nuevo frame
|
||||||
|
if (animation[currentAnimation].currentFrame >= (int)animation[currentAnimation].frames.size())
|
||||||
|
{
|
||||||
|
if (animation[currentAnimation].loop == -1)
|
||||||
|
{ // Si no hay loop, deja el último frame
|
||||||
|
animation[currentAnimation].currentFrame = animation[currentAnimation].frames.size();
|
||||||
|
animation[currentAnimation].completed = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{ // Si hay loop, vuelve al frame indicado
|
||||||
|
animation[currentAnimation].counter = 0;
|
||||||
|
animation[currentAnimation].currentFrame = animation[currentAnimation].loop;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// En caso contrario
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Escoge el frame correspondiente de la animación
|
||||||
|
setSpriteClip(animation[currentAnimation].frames[animation[currentAnimation].currentFrame]);
|
||||||
|
|
||||||
|
// Incrementa el contador de la animacion
|
||||||
|
animation[currentAnimation].counter++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene el numero de frames de la animación actual
|
||||||
|
int AnimatedSprite::getNumFrames()
|
||||||
|
{
|
||||||
|
return (int)animation[currentAnimation].frames.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el frame actual de la animación
|
||||||
|
void AnimatedSprite::setCurrentFrame(int num)
|
||||||
|
{
|
||||||
|
// Descarta valores fuera de rango
|
||||||
|
if (num >= (int)animation[currentAnimation].frames.size())
|
||||||
|
{
|
||||||
|
num = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cambia el valor de la variable
|
||||||
|
animation[currentAnimation].currentFrame = num;
|
||||||
|
animation[currentAnimation].counter = 0;
|
||||||
|
|
||||||
|
// Escoge el frame correspondiente de la animación
|
||||||
|
setSpriteClip(animation[currentAnimation].frames[animation[currentAnimation].currentFrame]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el valor del contador
|
||||||
|
void AnimatedSprite::setAnimationCounter(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)
|
||||||
|
{
|
||||||
|
animation[getIndex(name)].counter = speed;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece la velocidad de una animación
|
||||||
|
void AnimatedSprite::setAnimationSpeed(int index, int speed)
|
||||||
|
{
|
||||||
|
animation[index].counter = speed;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece si la animación se reproduce en bucle
|
||||||
|
void AnimatedSprite::setAnimationLoop(std::string name, int loop)
|
||||||
|
{
|
||||||
|
animation[getIndex(name)].loop = loop;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece si la animación se reproduce en bucle
|
||||||
|
void AnimatedSprite::setAnimationLoop(int index, int loop)
|
||||||
|
{
|
||||||
|
animation[index].loop = loop;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void AnimatedSprite::setAnimationCompleted(std::string name, bool value)
|
||||||
|
{
|
||||||
|
animation[getIndex(name)].completed = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// OLD - Establece el valor de la variable
|
||||||
|
void AnimatedSprite::setAnimationCompleted(int index, bool value)
|
||||||
|
{
|
||||||
|
animation[index].completed = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba si ha terminado la animación
|
||||||
|
bool AnimatedSprite::animationIsCompleted()
|
||||||
|
{
|
||||||
|
return animation[currentAnimation].completed;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Devuelve el rectangulo de una animación y frame concreto
|
||||||
|
SDL_Rect AnimatedSprite::getAnimationClip(std::string name, Uint8 index)
|
||||||
|
{
|
||||||
|
return animation[getIndex(name)].frames[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Devuelve el rectangulo de una animación y frame concreto
|
||||||
|
SDL_Rect AnimatedSprite::getAnimationClip(int indexA, Uint8 indexF)
|
||||||
|
{
|
||||||
|
return animation[indexA].frames[indexF];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Carga la animación desde un vector
|
||||||
|
bool AnimatedSprite::loadFromVector(std::vector<std::string> *source)
|
||||||
|
{
|
||||||
|
// Inicializa variables
|
||||||
|
int framesPerRow = 0;
|
||||||
|
int frameWidth = 0;
|
||||||
|
int frameHeight = 0;
|
||||||
|
int maxTiles = 0;
|
||||||
|
|
||||||
|
// Indicador de éxito en el proceso
|
||||||
|
bool success = true;
|
||||||
|
std::string line;
|
||||||
|
|
||||||
|
// Recorre todo el vector
|
||||||
|
int index = 0;
|
||||||
|
while (index < (int)source->size())
|
||||||
|
{
|
||||||
|
// Lee desde el vector
|
||||||
|
line = source->at(index);
|
||||||
|
|
||||||
|
// Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación
|
||||||
|
if (line == "[animation]")
|
||||||
|
{
|
||||||
|
animation_t buffer;
|
||||||
|
buffer.counter = 0;
|
||||||
|
buffer.currentFrame = 0;
|
||||||
|
buffer.completed = false;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
// Aumenta el indice para leer la siguiente linea
|
||||||
|
index++;
|
||||||
|
line = source->at(index);
|
||||||
|
|
||||||
|
// Encuentra la posición del caracter '='
|
||||||
|
int pos = line.find("=");
|
||||||
|
|
||||||
|
// Procesa las dos subcadenas
|
||||||
|
if (pos != (int)line.npos)
|
||||||
|
{
|
||||||
|
if (line.substr(0, pos) == "name")
|
||||||
|
{
|
||||||
|
buffer.name = line.substr(pos + 1, line.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (line.substr(0, pos) == "speed")
|
||||||
|
{
|
||||||
|
buffer.speed = std::stoi(line.substr(pos + 1, line.length()));
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (line.substr(0, pos) == "loop")
|
||||||
|
{
|
||||||
|
buffer.loop = std::stoi(line.substr(pos + 1, line.length()));
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (line.substr(0, pos) == "frames")
|
||||||
|
{
|
||||||
|
// Se introducen los valores separados por comas en un vector
|
||||||
|
std::stringstream ss(line.substr(pos + 1, line.length()));
|
||||||
|
std::string tmp;
|
||||||
|
SDL_Rect rect = {0, 0, frameWidth, frameHeight};
|
||||||
|
while (getline(ss, tmp, ','))
|
||||||
|
{
|
||||||
|
// Comprueba que el tile no sea mayor que el maximo indice permitido
|
||||||
|
const int numTile = std::stoi(tmp) > maxTiles ? 0 : std::stoi(tmp);
|
||||||
|
rect.x = (numTile % framesPerRow) * frameWidth;
|
||||||
|
rect.y = (numTile / framesPerRow) * frameHeight;
|
||||||
|
buffer.frames.push_back(rect);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::cout << "Warning: unknown parameter " << line.substr(0, pos).c_str() << std::endl;
|
||||||
|
success = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while (line != "[/animation]");
|
||||||
|
|
||||||
|
// Añade la animación al vector de animaciones
|
||||||
|
animation.push_back(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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("=");
|
||||||
|
|
||||||
|
// Procesa las dos subcadenas
|
||||||
|
if (pos != (int)line.npos)
|
||||||
|
{
|
||||||
|
if (line.substr(0, pos) == "framesPerRow")
|
||||||
|
{
|
||||||
|
framesPerRow = std::stoi(line.substr(pos + 1, line.length()));
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (line.substr(0, pos) == "frameWidth")
|
||||||
|
{
|
||||||
|
frameWidth = std::stoi(line.substr(pos + 1, line.length()));
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (line.substr(0, pos) == "frameHeight")
|
||||||
|
{
|
||||||
|
frameHeight = std::stoi(line.substr(pos + 1, line.length()));
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::cout << "Warning: unknown parameter " << line.substr(0, pos).c_str() << std::endl;
|
||||||
|
success = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Normaliza valores
|
||||||
|
if (framesPerRow == 0 && frameWidth > 0)
|
||||||
|
{
|
||||||
|
framesPerRow = texture->getWidth() / frameWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (maxTiles == 0 && frameWidth > 0 && frameHeight > 0)
|
||||||
|
{
|
||||||
|
const int w = texture->getWidth() / frameWidth;
|
||||||
|
const int h = texture->getHeight() / frameHeight;
|
||||||
|
maxTiles = w * h;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Una vez procesada la linea, aumenta el indice para pasar a la siguiente
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pone un valor por defecto
|
||||||
|
setRect({0, 0, frameWidth, frameHeight});
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece la animacion actual
|
||||||
|
void AnimatedSprite::setCurrentAnimation(std::string name)
|
||||||
|
{
|
||||||
|
const int newAnimation = getIndex(name);
|
||||||
|
if (currentAnimation != newAnimation)
|
||||||
|
{
|
||||||
|
currentAnimation = newAnimation;
|
||||||
|
animation[currentAnimation].currentFrame = 0;
|
||||||
|
animation[currentAnimation].counter = 0;
|
||||||
|
animation[currentAnimation].completed = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece la animacion actual
|
||||||
|
void AnimatedSprite::setCurrentAnimation(int index)
|
||||||
|
{
|
||||||
|
const int newAnimation = index;
|
||||||
|
if (currentAnimation != newAnimation)
|
||||||
|
{
|
||||||
|
currentAnimation = newAnimation;
|
||||||
|
animation[currentAnimation].currentFrame = 0;
|
||||||
|
animation[currentAnimation].counter = 0;
|
||||||
|
animation[currentAnimation].completed = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actualiza las variables del objeto
|
||||||
|
void AnimatedSprite::update()
|
||||||
|
{
|
||||||
|
animate();
|
||||||
|
MovingSprite::update();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el rectangulo para un frame de una animación
|
||||||
|
void AnimatedSprite::setAnimationFrames(Uint8 index_animation, Uint8 index_frame, int x, int y, int w, int h)
|
||||||
|
{
|
||||||
|
animation[index_animation].frames.push_back({x, y, w, h});
|
||||||
|
}
|
||||||
|
|
||||||
|
// OLD - Establece el contador para todas las animaciones
|
||||||
|
void AnimatedSprite::setAnimationCounter(int value)
|
||||||
|
{
|
||||||
|
for (auto &a : animation)
|
||||||
|
{
|
||||||
|
a.counter = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reinicia la animación
|
||||||
|
void AnimatedSprite::resetAnimation()
|
||||||
|
{
|
||||||
|
animation[currentAnimation].currentFrame = 0;
|
||||||
|
animation[currentAnimation].counter = 0;
|
||||||
|
animation[currentAnimation].completed = false;
|
||||||
|
}
|
||||||
103
animatedsprite.h
Normal file
103
animatedsprite.h
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include "movingsprite.h"
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#ifndef ANIMATEDSPRITE_H
|
||||||
|
#define ANIMATEDSPRITE_H
|
||||||
|
|
||||||
|
struct animation_t
|
||||||
|
{
|
||||||
|
std::string name; // Nombre de la animacion
|
||||||
|
std::vector<SDL_Rect> frames; // Cada uno de los frames que componen la animación
|
||||||
|
int speed; // Velocidad de la animación
|
||||||
|
int loop; // Indica a que frame vuelve la animación al terminar. -1 para que no vuelva
|
||||||
|
bool completed; // Indica si ha finalizado la animación
|
||||||
|
int currentFrame; // Frame actual
|
||||||
|
int counter; // Contador para las animaciones
|
||||||
|
};
|
||||||
|
|
||||||
|
struct animatedSprite_t
|
||||||
|
{
|
||||||
|
std::vector<animation_t> animations; // Vector con las diferentes animaciones
|
||||||
|
Texture *texture; // Textura con los graficos para el sprite
|
||||||
|
};
|
||||||
|
|
||||||
|
// Carga la animación desde un fichero
|
||||||
|
animatedSprite_t loadAnimationFromFile(Texture *texture, std::string filePath, bool verbose = false);
|
||||||
|
|
||||||
|
class AnimatedSprite : public MovingSprite
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
// Variables
|
||||||
|
std::vector<animation_t> animation; // Vector con las diferentes animaciones
|
||||||
|
int currentAnimation; // Animacion activa
|
||||||
|
|
||||||
|
public:
|
||||||
|
// Constructor
|
||||||
|
AnimatedSprite(Texture *texture = nullptr, SDL_Renderer *renderer = nullptr, std::string file = "", std::vector<std::string> *buffer = nullptr);
|
||||||
|
AnimatedSprite(SDL_Renderer *renderer, animatedSprite_t *animation);
|
||||||
|
|
||||||
|
// Destructor
|
||||||
|
~AnimatedSprite();
|
||||||
|
|
||||||
|
// Calcula el frame correspondiente a la animación actual
|
||||||
|
void animate();
|
||||||
|
|
||||||
|
// Obtiene el numero de frames de la animación actual
|
||||||
|
int getNumFrames();
|
||||||
|
|
||||||
|
// Establece el frame actual de la animación
|
||||||
|
void setCurrentFrame(int num);
|
||||||
|
|
||||||
|
// Establece el valor del contador
|
||||||
|
void setAnimationCounter(std::string name, int num);
|
||||||
|
|
||||||
|
// Establece la velocidad de una animación
|
||||||
|
void setAnimationSpeed(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(int index, int loop);
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void setAnimationCompleted(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(int indexA = 0, Uint8 indexF = 0);
|
||||||
|
|
||||||
|
// Obtiene el indice de la animación a partir del nombre
|
||||||
|
int getIndex(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(int index = 0);
|
||||||
|
|
||||||
|
// Actualiza las variables del objeto
|
||||||
|
void update();
|
||||||
|
|
||||||
|
// OLD - Establece el rectangulo para un frame de una animación
|
||||||
|
void setAnimationFrames(Uint8 index_animation, Uint8 index_frame, int x, int y, int w, int h);
|
||||||
|
|
||||||
|
// OLD - Establece el contador para todas las animaciones
|
||||||
|
void setAnimationCounter(int value);
|
||||||
|
|
||||||
|
// Reinicia la animación
|
||||||
|
void resetAnimation();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
190
asset.cpp
Normal file
190
asset.cpp
Normal file
@@ -0,0 +1,190 @@
|
|||||||
|
#include "asset.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
// Constructor
|
||||||
|
Asset::Asset(std::string executablePath)
|
||||||
|
{
|
||||||
|
this->executablePath = executablePath.substr(0, executablePath.find_last_of("\\/"));
|
||||||
|
longestName = 0;
|
||||||
|
verbose = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Añade un elemento a la lista
|
||||||
|
void Asset::add(std::string file, enum assetType type, bool required, bool absolute)
|
||||||
|
{
|
||||||
|
item_t temp;
|
||||||
|
temp.file = absolute ? file : executablePath + file;
|
||||||
|
temp.type = type;
|
||||||
|
temp.required = required;
|
||||||
|
fileList.push_back(temp);
|
||||||
|
|
||||||
|
const std::string filename = file.substr(file.find_last_of("\\/") + 1);
|
||||||
|
longestName = SDL_max(longestName, filename.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
{
|
||||||
|
const size_t lastIndex = f.file.find_last_of("/") + 1;
|
||||||
|
const std::string file = f.file.substr(lastIndex, std::string::npos);
|
||||||
|
|
||||||
|
if (file == text)
|
||||||
|
{
|
||||||
|
return f.file;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (verbose)
|
||||||
|
{
|
||||||
|
std::cout << "Warning: file " << text.c_str() << " not found" << std::endl;
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba que existen todos los elementos
|
||||||
|
bool Asset::check()
|
||||||
|
{
|
||||||
|
bool success = true;
|
||||||
|
|
||||||
|
if (verbose)
|
||||||
|
{
|
||||||
|
std::cout << "\n** Checking files" << std::endl;
|
||||||
|
|
||||||
|
std::cout << "Executable path is: " << executablePath << std::endl;
|
||||||
|
std::cout << "Sample filepath: " << fileList.back().file << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba la lista de ficheros clasificandolos por tipo
|
||||||
|
for (int type = 0; type < t_maxAssetType; ++type)
|
||||||
|
{
|
||||||
|
// Comprueba si hay ficheros de ese tipo
|
||||||
|
bool any = false;
|
||||||
|
|
||||||
|
for (auto f : fileList)
|
||||||
|
{
|
||||||
|
if ((f.required) && (f.type == type))
|
||||||
|
{
|
||||||
|
any = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Si hay ficheros de ese tipo, comprueba si existen
|
||||||
|
if (any)
|
||||||
|
{
|
||||||
|
if (verbose)
|
||||||
|
{
|
||||||
|
std::cout << "\n>> " << getTypeName(type).c_str() << " FILES" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto f : fileList)
|
||||||
|
{
|
||||||
|
if ((f.required) && (f.type == type))
|
||||||
|
{
|
||||||
|
success &= checkFile(f.file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resultado
|
||||||
|
if (verbose)
|
||||||
|
{
|
||||||
|
if (success)
|
||||||
|
{
|
||||||
|
std::cout << "\n** All files OK.\n"
|
||||||
|
<< std::endl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::cout << "\n** A file is missing. Exiting.\n"
|
||||||
|
<< std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba que existe un fichero
|
||||||
|
bool Asset::checkFile(std::string path)
|
||||||
|
{
|
||||||
|
bool success = false;
|
||||||
|
std::string result = "ERROR";
|
||||||
|
|
||||||
|
// Comprueba si existe el fichero
|
||||||
|
const std::string filename = path.substr(path.find_last_of("\\/") + 1);
|
||||||
|
SDL_RWops *file = SDL_RWFromFile(path.c_str(), "rb");
|
||||||
|
|
||||||
|
if (file != nullptr)
|
||||||
|
{
|
||||||
|
result = "OK";
|
||||||
|
success = true;
|
||||||
|
SDL_RWclose(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (verbose)
|
||||||
|
{
|
||||||
|
std::cout.setf(std::ios::left, std::ios::adjustfield);
|
||||||
|
std::cout << "Checking file: ";
|
||||||
|
std::cout.width(longestName + 2);
|
||||||
|
std::cout.fill('.');
|
||||||
|
std::cout << filename + " ";
|
||||||
|
std::cout << " [" + result + "]" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Devuelve el nombre del tipo de recurso
|
||||||
|
std::string Asset::getTypeName(int type)
|
||||||
|
{
|
||||||
|
switch (type)
|
||||||
|
{
|
||||||
|
case t_bitmap:
|
||||||
|
return "BITMAP";
|
||||||
|
break;
|
||||||
|
|
||||||
|
case t_music:
|
||||||
|
return "MUSIC";
|
||||||
|
break;
|
||||||
|
|
||||||
|
case t_sound:
|
||||||
|
return "SOUND";
|
||||||
|
break;
|
||||||
|
|
||||||
|
case t_font:
|
||||||
|
return "FONT";
|
||||||
|
break;
|
||||||
|
|
||||||
|
case t_lang:
|
||||||
|
return "LANG";
|
||||||
|
break;
|
||||||
|
|
||||||
|
case t_data:
|
||||||
|
return "DATA";
|
||||||
|
break;
|
||||||
|
|
||||||
|
case t_room:
|
||||||
|
return "ROOM";
|
||||||
|
break;
|
||||||
|
|
||||||
|
case t_enemy:
|
||||||
|
return "ENEMY";
|
||||||
|
break;
|
||||||
|
|
||||||
|
case t_item:
|
||||||
|
return "ITEM";
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return "ERROR";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece si ha de mostrar texto por pantalla
|
||||||
|
void Asset::setVerbose(bool value)
|
||||||
|
{
|
||||||
|
verbose = value;
|
||||||
|
}
|
||||||
66
asset.h
Normal file
66
asset.h
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#ifndef ASSET_H
|
||||||
|
#define ASSET_H
|
||||||
|
|
||||||
|
enum assetType
|
||||||
|
{
|
||||||
|
t_bitmap,
|
||||||
|
t_music,
|
||||||
|
t_sound,
|
||||||
|
t_font,
|
||||||
|
t_lang,
|
||||||
|
t_data,
|
||||||
|
t_room,
|
||||||
|
t_enemy,
|
||||||
|
t_item,
|
||||||
|
t_maxAssetType
|
||||||
|
};
|
||||||
|
|
||||||
|
// Clase Asset
|
||||||
|
class Asset
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
// Estructura para definir un item
|
||||||
|
struct item_t
|
||||||
|
{
|
||||||
|
std::string file; // Ruta del fichero desde la raiz del directorio
|
||||||
|
enum assetType type; // Indica el tipo de recurso
|
||||||
|
bool required; // Indica si es un fichero que debe de existir
|
||||||
|
//bool absolute; // Indica si la ruta que se ha proporcionado es una ruta absoluta
|
||||||
|
};
|
||||||
|
|
||||||
|
// Variables
|
||||||
|
int longestName; // Contiene la longitud del nombre de fichero mas largo
|
||||||
|
std::vector<item_t> fileList; // Listado con todas las rutas a los ficheros
|
||||||
|
std::string executablePath; // Ruta al ejecutable
|
||||||
|
bool verbose; // Indica si ha de mostrar información por pantalla
|
||||||
|
|
||||||
|
// Comprueba que existe un fichero
|
||||||
|
bool checkFile(std::string executablePath);
|
||||||
|
|
||||||
|
// Devuelve el nombre del tipo de recurso
|
||||||
|
std::string getTypeName(int type);
|
||||||
|
|
||||||
|
public:
|
||||||
|
// Constructor
|
||||||
|
Asset(std::string path);
|
||||||
|
|
||||||
|
// Añade un elemento a la lista
|
||||||
|
void add(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);
|
||||||
|
|
||||||
|
// Comprueba que existen todos los elementos
|
||||||
|
bool check();
|
||||||
|
|
||||||
|
// Establece si ha de mostrar texto por pantalla
|
||||||
|
void setVerbose(bool value);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
106
debug.cpp
Normal file
106
debug.cpp
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
#include "debug.h"
|
||||||
|
|
||||||
|
// Constructor
|
||||||
|
Debug::Debug(SDL_Renderer *renderer, Screen *screen, Asset *asset)
|
||||||
|
{
|
||||||
|
// Copia la dirección de los objetos
|
||||||
|
this->renderer = renderer;
|
||||||
|
this->screen = screen;
|
||||||
|
this->asset = asset;
|
||||||
|
|
||||||
|
// Reserva memoria para los punteros
|
||||||
|
texture = new Texture(renderer, asset->get("debug.png"));
|
||||||
|
text = new Text(asset->get("debug.txt"), texture, renderer);
|
||||||
|
|
||||||
|
// Inicializa variables
|
||||||
|
x = 0;
|
||||||
|
y = 0;
|
||||||
|
enabled = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Destructor
|
||||||
|
Debug::~Debug()
|
||||||
|
{
|
||||||
|
delete texture;
|
||||||
|
delete text;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actualiza las variables
|
||||||
|
void Debug::update()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dibuja en pantalla
|
||||||
|
void Debug::render()
|
||||||
|
{
|
||||||
|
int y = this->y;
|
||||||
|
int w = 0;
|
||||||
|
|
||||||
|
for (auto s : slot)
|
||||||
|
{
|
||||||
|
text->write(x, y, s);
|
||||||
|
w = (std::max(w, (int)s.length()));
|
||||||
|
y += text->getCharacterSize() + 1;
|
||||||
|
if (y > 192 - text->getCharacterSize())
|
||||||
|
{
|
||||||
|
y = this->y;
|
||||||
|
x += w * text->getCharacterSize() + 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
y = 0;
|
||||||
|
for (auto l : log)
|
||||||
|
{
|
||||||
|
text->writeColored(x + 10, y, l, {255, 255, 255});
|
||||||
|
y += text->getCharacterSize() + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece la posición donde se colocará la información de debug
|
||||||
|
void Debug::setPos(SDL_Point p)
|
||||||
|
{
|
||||||
|
x = p.x;
|
||||||
|
y = p.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Añade un texto para mostrar
|
||||||
|
void Debug::add(std::string text)
|
||||||
|
{
|
||||||
|
slot.push_back(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Borra la información de debug
|
||||||
|
void Debug::clear()
|
||||||
|
{
|
||||||
|
slot.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Añade un texto para mostrar en el apartado log
|
||||||
|
void Debug::addToLog(std::string text)
|
||||||
|
{
|
||||||
|
log.push_back(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Borra la información de debug del apartado log
|
||||||
|
void Debug::clearLog()
|
||||||
|
{
|
||||||
|
log.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void Debug::setEnabled(bool value)
|
||||||
|
{
|
||||||
|
enabled = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene el valor de la variable
|
||||||
|
bool Debug::getEnabled()
|
||||||
|
{
|
||||||
|
return enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cambia el valor de la variable
|
||||||
|
void Debug::switchEnabled()
|
||||||
|
{
|
||||||
|
enabled = !enabled;
|
||||||
|
}
|
||||||
72
debug.h
Normal file
72
debug.h
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include "../const.h"
|
||||||
|
#include "asset.h"
|
||||||
|
#include "screen.h"
|
||||||
|
#include "text.h"
|
||||||
|
#include "texture.h"
|
||||||
|
#include "utils.h"
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#ifndef DEBUG_H
|
||||||
|
#define DEBUG_H
|
||||||
|
|
||||||
|
// Clase Debug
|
||||||
|
class Debug
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
// Objetos y punteros
|
||||||
|
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||||
|
Screen *screen; // Objeto encargado de dibujar en pantalla
|
||||||
|
Asset *asset; // Objeto con los ficheros de recursos
|
||||||
|
Text *text; // Objeto encargado de escribir texto en pantalla
|
||||||
|
Texture *texture; // Textura para el texto
|
||||||
|
|
||||||
|
// Variables
|
||||||
|
std::vector<std::string> slot; // Vector con los textos a escribir
|
||||||
|
std::vector<std::string> log; // Vector con los textos a escribir
|
||||||
|
int x; // Posicion donde escribir el texto de debug
|
||||||
|
int y; // Posición donde escribir el texto de debug
|
||||||
|
bool enabled; // Indica si esta activo el modo debug
|
||||||
|
|
||||||
|
public:
|
||||||
|
// Constructor
|
||||||
|
Debug(SDL_Renderer *renderer, Screen *screen, Asset *asset);
|
||||||
|
|
||||||
|
// Destructor
|
||||||
|
~Debug();
|
||||||
|
|
||||||
|
// Actualiza las variables
|
||||||
|
void update();
|
||||||
|
|
||||||
|
// Dibuja en pantalla
|
||||||
|
void render();
|
||||||
|
|
||||||
|
// Establece la posición donde se colocará la información de debug
|
||||||
|
void setPos(SDL_Point p);
|
||||||
|
|
||||||
|
// Añade un texto para mostrar
|
||||||
|
void add(std::string text);
|
||||||
|
|
||||||
|
// Borra la información de debug
|
||||||
|
void clear();
|
||||||
|
|
||||||
|
// Añade un texto para mostrar en el apartado log
|
||||||
|
void addToLog(std::string text);
|
||||||
|
|
||||||
|
// Borra la información de debug del apartado log
|
||||||
|
void clearLog();
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void setEnabled(bool value);
|
||||||
|
|
||||||
|
// Obtiene el valor de la variable
|
||||||
|
bool getEnabled();
|
||||||
|
|
||||||
|
// Cambia el valor de la variable
|
||||||
|
void switchEnabled();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
312
input.cpp
Normal file
312
input.cpp
Normal file
@@ -0,0 +1,312 @@
|
|||||||
|
#include "input.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
// Constructor
|
||||||
|
Input::Input(std::string file)
|
||||||
|
{
|
||||||
|
// Fichero gamecontrollerdb.txt
|
||||||
|
dbPath = file;
|
||||||
|
|
||||||
|
// Inicializa las variables
|
||||||
|
keyBindings_t kb;
|
||||||
|
kb.scancode = 0;
|
||||||
|
kb.active = false;
|
||||||
|
keyBindings.resize(input_number_of_inputs, kb);
|
||||||
|
|
||||||
|
GameControllerBindings_t gcb;
|
||||||
|
gcb.button = SDL_CONTROLLER_BUTTON_INVALID;
|
||||||
|
gcb.active = false;
|
||||||
|
gameControllerBindings.resize(input_number_of_inputs, gcb);
|
||||||
|
|
||||||
|
verbose = true;
|
||||||
|
enabled = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actualiza el estado del objeto
|
||||||
|
void Input::update()
|
||||||
|
{
|
||||||
|
if (disabledUntil == d_keyPressed && !checkAnyInput())
|
||||||
|
{
|
||||||
|
enable();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Asigna inputs a teclas
|
||||||
|
void Input::bindKey(Uint8 input, SDL_Scancode code)
|
||||||
|
{
|
||||||
|
keyBindings[input].scancode = code;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Asigna inputs a botones del mando
|
||||||
|
void Input::bindGameControllerButton(Uint8 input, SDL_GameControllerButton button)
|
||||||
|
{
|
||||||
|
gameControllerBindings[input].button = button;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba si un input esta activo
|
||||||
|
bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
|
||||||
|
{
|
||||||
|
if (!enabled)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool successKeyboard = false;
|
||||||
|
bool successGameController = false;
|
||||||
|
|
||||||
|
if (device == INPUT_USE_ANY)
|
||||||
|
{
|
||||||
|
index = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (device == INPUT_USE_KEYBOARD || device == INPUT_USE_ANY)
|
||||||
|
{
|
||||||
|
const Uint8 *keyStates = SDL_GetKeyboardState(nullptr);
|
||||||
|
|
||||||
|
if (repeat)
|
||||||
|
{
|
||||||
|
if (keyStates[keyBindings[input].scancode] != 0)
|
||||||
|
{
|
||||||
|
successKeyboard = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
successKeyboard = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (!keyBindings[input].active)
|
||||||
|
{
|
||||||
|
if (keyStates[keyBindings[input].scancode] != 0)
|
||||||
|
{
|
||||||
|
keyBindings[input].active = true;
|
||||||
|
successKeyboard = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
successKeyboard = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (keyStates[keyBindings[input].scancode] == 0)
|
||||||
|
{
|
||||||
|
keyBindings[input].active = false;
|
||||||
|
successKeyboard = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
successKeyboard = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (gameControllerFound())
|
||||||
|
if ((device == INPUT_USE_GAMECONTROLLER) || (device == INPUT_USE_ANY))
|
||||||
|
{
|
||||||
|
if (repeat)
|
||||||
|
{
|
||||||
|
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[input].button) != 0)
|
||||||
|
{
|
||||||
|
successGameController = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
successGameController = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (!gameControllerBindings[input].active)
|
||||||
|
{
|
||||||
|
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[input].button) != 0)
|
||||||
|
{
|
||||||
|
gameControllerBindings[input].active = true;
|
||||||
|
successGameController = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
successGameController = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[input].button) == 0)
|
||||||
|
{
|
||||||
|
gameControllerBindings[input].active = false;
|
||||||
|
successGameController = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
successGameController = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (successKeyboard || successGameController);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba si hay almenos un input activo
|
||||||
|
bool Input::checkAnyInput(int device, int index)
|
||||||
|
{
|
||||||
|
if (device == INPUT_USE_ANY)
|
||||||
|
{
|
||||||
|
index = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (device == INPUT_USE_KEYBOARD || device == INPUT_USE_ANY)
|
||||||
|
{
|
||||||
|
const Uint8 *mKeystates = SDL_GetKeyboardState(nullptr);
|
||||||
|
|
||||||
|
for (int i = 0; i < (int)keyBindings.size(); ++i)
|
||||||
|
{
|
||||||
|
if (mKeystates[keyBindings[i].scancode] != 0)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (gameControllerFound())
|
||||||
|
{
|
||||||
|
if (device == INPUT_USE_GAMECONTROLLER || device == INPUT_USE_ANY)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < (int)gameControllerBindings.size(); ++i)
|
||||||
|
{
|
||||||
|
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[i].button) != 0)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Busca si hay un mando conectado
|
||||||
|
bool Input::discoverGameController()
|
||||||
|
{
|
||||||
|
bool found = false;
|
||||||
|
|
||||||
|
if (SDL_WasInit(SDL_INIT_GAMECONTROLLER) != 1)
|
||||||
|
{
|
||||||
|
SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (SDL_GameControllerAddMappingsFromFile(dbPath.c_str()) < 0)
|
||||||
|
{
|
||||||
|
if (verbose)
|
||||||
|
{
|
||||||
|
std::cout << "Error, could not load " << dbPath.c_str() << " file: " << SDL_GetError() << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const int nJoysticks = SDL_NumJoysticks();
|
||||||
|
numGamepads = 0;
|
||||||
|
|
||||||
|
// Cuenta el numero de mandos
|
||||||
|
for (int i = 0; i < nJoysticks; ++i)
|
||||||
|
{
|
||||||
|
if (SDL_IsGameController(i))
|
||||||
|
{
|
||||||
|
numGamepads++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (verbose)
|
||||||
|
{
|
||||||
|
std::cout << "\nChecking for game controllers...\n";
|
||||||
|
std::cout << nJoysticks << " joysticks found, " << numGamepads << " are gamepads\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (numGamepads > 0)
|
||||||
|
{
|
||||||
|
found = true;
|
||||||
|
|
||||||
|
for (int i = 0; i < numGamepads; i++)
|
||||||
|
{
|
||||||
|
// Abre el mando y lo añade a la lista
|
||||||
|
SDL_GameController *pad = SDL_GameControllerOpen(i);
|
||||||
|
if (SDL_GameControllerGetAttached(pad) == 1)
|
||||||
|
{
|
||||||
|
connectedControllers.push_back(pad);
|
||||||
|
const std::string separator(" #");
|
||||||
|
std::string name = SDL_GameControllerNameForIndex(i);
|
||||||
|
name.resize(25);
|
||||||
|
name = name + separator + std::to_string(i);
|
||||||
|
if (verbose)
|
||||||
|
{
|
||||||
|
std::cout << name << std::endl;
|
||||||
|
}
|
||||||
|
controllerNames.push_back(name);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (verbose)
|
||||||
|
{
|
||||||
|
std::cout << "SDL_GetError() = " << SDL_GetError() << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_GameControllerEventState(SDL_ENABLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
return found;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba si hay algun mando conectado
|
||||||
|
bool Input::gameControllerFound()
|
||||||
|
{
|
||||||
|
if (numGamepads > 0)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obten el nombre de un mando de juego
|
||||||
|
std::string Input::getControllerName(int index)
|
||||||
|
{
|
||||||
|
if (numGamepads > 0)
|
||||||
|
{
|
||||||
|
return controllerNames[index];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obten el numero de mandos conectados
|
||||||
|
int Input::getNumControllers()
|
||||||
|
{
|
||||||
|
return numGamepads;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece si ha de mostrar mensajes
|
||||||
|
void Input::setVerbose(bool value)
|
||||||
|
{
|
||||||
|
verbose = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deshabilita las entradas durante un periodo de tiempo
|
||||||
|
void Input::disableUntil(i_disable_e value)
|
||||||
|
{
|
||||||
|
disabledUntil = value;
|
||||||
|
enabled = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hablita las entradas
|
||||||
|
void Input::enable()
|
||||||
|
{
|
||||||
|
enabled = true;
|
||||||
|
disabledUntil = d_notDisabled;
|
||||||
|
}
|
||||||
119
input.h
Normal file
119
input.h
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#ifndef INPUT_H
|
||||||
|
#define INPUT_H
|
||||||
|
|
||||||
|
enum inputs_e
|
||||||
|
{
|
||||||
|
// Inputs obligatorios
|
||||||
|
input_null,
|
||||||
|
input_up,
|
||||||
|
input_down,
|
||||||
|
input_left,
|
||||||
|
input_right,
|
||||||
|
input_pause,
|
||||||
|
input_exit,
|
||||||
|
input_accept,
|
||||||
|
input_cancel,
|
||||||
|
|
||||||
|
// Inputs personalizados
|
||||||
|
input_jump,
|
||||||
|
input_window_fullscreen,
|
||||||
|
input_window_inc_size,
|
||||||
|
input_window_dec_size,
|
||||||
|
input_toggle_border,
|
||||||
|
input_switch_music,
|
||||||
|
input_swap_palette,
|
||||||
|
|
||||||
|
// Input obligatorio
|
||||||
|
input_number_of_inputs
|
||||||
|
};
|
||||||
|
|
||||||
|
#define REPEAT_TRUE true
|
||||||
|
#define REPEAT_FALSE false
|
||||||
|
|
||||||
|
#define INPUT_USE_KEYBOARD 0
|
||||||
|
#define INPUT_USE_GAMECONTROLLER 1
|
||||||
|
#define INPUT_USE_ANY 2
|
||||||
|
|
||||||
|
enum i_disable_e
|
||||||
|
{
|
||||||
|
d_notDisabled,
|
||||||
|
d_forever,
|
||||||
|
d_keyPressed
|
||||||
|
};
|
||||||
|
|
||||||
|
class Input
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
struct keyBindings_t
|
||||||
|
{
|
||||||
|
Uint8 scancode; // Scancode asociado
|
||||||
|
bool active; // Indica si está activo
|
||||||
|
};
|
||||||
|
|
||||||
|
struct GameControllerBindings_t
|
||||||
|
{
|
||||||
|
SDL_GameControllerButton button; // GameControllerButton asociado
|
||||||
|
bool active; // Indica si está activo
|
||||||
|
};
|
||||||
|
|
||||||
|
// Objetos y punteros
|
||||||
|
std::vector<SDL_GameController *> connectedControllers; // Vector con todos los mandos conectados
|
||||||
|
|
||||||
|
// Variables
|
||||||
|
std::vector<keyBindings_t> keyBindings; // Vector con las teclas asociadas a los inputs predefinidos
|
||||||
|
std::vector<GameControllerBindings_t> gameControllerBindings; // Vector con las teclas asociadas a los inputs predefinidos
|
||||||
|
std::vector<std::string> controllerNames; // Vector con los nombres de los mandos
|
||||||
|
int numGamepads; // Numero de mandos conectados
|
||||||
|
std::string dbPath; // Ruta al archivo gamecontrollerdb.txt
|
||||||
|
bool verbose; // Indica si ha de mostrar mensajes
|
||||||
|
i_disable_e disabledUntil; // Tiempo que esta deshabilitado
|
||||||
|
bool enabled; // Indica si está habilitado
|
||||||
|
|
||||||
|
public:
|
||||||
|
// Constructor
|
||||||
|
Input(std::string file);
|
||||||
|
|
||||||
|
// Actualiza el estado del objeto
|
||||||
|
void update();
|
||||||
|
|
||||||
|
// Asigna inputs a teclas
|
||||||
|
void bindKey(Uint8 input, SDL_Scancode code);
|
||||||
|
|
||||||
|
// Asigna inputs a botones del mando
|
||||||
|
void bindGameControllerButton(Uint8 input, SDL_GameControllerButton button);
|
||||||
|
|
||||||
|
// Comprueba si un input esta activo
|
||||||
|
bool checkInput(Uint8 input, bool repeat = true, int device = INPUT_USE_ANY, int index = 0);
|
||||||
|
|
||||||
|
// Comprueba si hay almenos un input activo
|
||||||
|
bool checkAnyInput(int device = INPUT_USE_ANY, int index = 0);
|
||||||
|
|
||||||
|
// Busca si hay un mando conectado
|
||||||
|
bool discoverGameController();
|
||||||
|
|
||||||
|
// Comprueba si hay algun mando conectado
|
||||||
|
bool gameControllerFound();
|
||||||
|
|
||||||
|
// Obten el numero de mandos conectados
|
||||||
|
int getNumControllers();
|
||||||
|
|
||||||
|
// Obten el nombre de un mando de juego
|
||||||
|
std::string getControllerName(int index);
|
||||||
|
|
||||||
|
// Establece si ha de mostrar mensajes
|
||||||
|
void setVerbose(bool value);
|
||||||
|
|
||||||
|
// Deshabilita las entradas durante un periodo de tiempo
|
||||||
|
void disableUntil(i_disable_e value);
|
||||||
|
|
||||||
|
// Hablita las entradas
|
||||||
|
void enable();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
251
jail_audio.cpp
Normal file
251
jail_audio.cpp
Normal file
@@ -0,0 +1,251 @@
|
|||||||
|
#ifndef JA_USESDLMIXER
|
||||||
|
#include "jail_audio.h"
|
||||||
|
#include "stb_vorbis.c"
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define JA_MAX_SIMULTANEOUS_CHANNELS 5
|
||||||
|
|
||||||
|
struct JA_Sound_t {
|
||||||
|
Uint32 length {0};
|
||||||
|
Uint8* buffer {NULL};
|
||||||
|
};
|
||||||
|
|
||||||
|
struct JA_Channel_t {
|
||||||
|
JA_Sound_t *sound;
|
||||||
|
int pos {0};
|
||||||
|
int times {0};
|
||||||
|
JA_Channel_state state { JA_CHANNEL_FREE };
|
||||||
|
};
|
||||||
|
|
||||||
|
struct JA_Music_t {
|
||||||
|
int samples {0};
|
||||||
|
int pos {0};
|
||||||
|
int times {0};
|
||||||
|
short* output {NULL};
|
||||||
|
JA_Music_state state {JA_MUSIC_INVALID};
|
||||||
|
};
|
||||||
|
|
||||||
|
JA_Music_t *current_music{NULL};
|
||||||
|
JA_Channel_t channels[JA_MAX_SIMULTANEOUS_CHANNELS];
|
||||||
|
|
||||||
|
int JA_freq {48000};
|
||||||
|
SDL_AudioFormat JA_format {AUDIO_S16};
|
||||||
|
Uint8 JA_channels {2};
|
||||||
|
int JA_volume = 128;
|
||||||
|
SDL_AudioDeviceID sdlAudioDevice = 0;
|
||||||
|
|
||||||
|
void audioCallback(void * userdata, uint8_t * stream, int len) {
|
||||||
|
SDL_memset(stream, 0, len);
|
||||||
|
if (current_music != NULL && current_music->state == JA_MUSIC_PLAYING) {
|
||||||
|
const int size = SDL_min(len, current_music->samples*2-current_music->pos);
|
||||||
|
SDL_MixAudioFormat(stream, (Uint8*)(current_music->output+current_music->pos), AUDIO_S16, size, JA_volume);
|
||||||
|
current_music->pos += size/2;
|
||||||
|
if (size < len) {
|
||||||
|
if (current_music->times != 0) {
|
||||||
|
SDL_MixAudioFormat(stream+size, (Uint8*)current_music->output, AUDIO_S16, len-size, JA_volume);
|
||||||
|
current_music->pos = (len-size)/2;
|
||||||
|
if (current_music->times > 0) current_music->times--;
|
||||||
|
} else {
|
||||||
|
current_music->pos = 0;
|
||||||
|
current_music->state = JA_MUSIC_STOPPED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Mixar els channels mi amol
|
||||||
|
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) {
|
||||||
|
if (channels[i].state == JA_CHANNEL_PLAYING) {
|
||||||
|
const int size = SDL_min(len, channels[i].sound->length - channels[i].pos);
|
||||||
|
SDL_MixAudioFormat(stream, channels[i].sound->buffer + channels[i].pos, AUDIO_S16, size, JA_volume/2);
|
||||||
|
channels[i].pos += size;
|
||||||
|
if (size < len) {
|
||||||
|
if (channels[i].times != 0) {
|
||||||
|
SDL_MixAudioFormat(stream + size, channels[i].sound->buffer, AUDIO_S16, len-size, JA_volume/2);
|
||||||
|
channels[i].pos = len-size;
|
||||||
|
if (channels[i].times > 0) channels[i].times--;
|
||||||
|
} else {
|
||||||
|
JA_StopChannel(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void JA_Init(const int freq, const SDL_AudioFormat format, const int channels) {
|
||||||
|
JA_freq = freq;
|
||||||
|
JA_format = format;
|
||||||
|
JA_channels = channels;
|
||||||
|
SDL_AudioSpec audioSpec{JA_freq, JA_format, JA_channels, 0, 1024, 0, 0, audioCallback, NULL};
|
||||||
|
if (sdlAudioDevice != 0) SDL_CloseAudioDevice(sdlAudioDevice);
|
||||||
|
sdlAudioDevice = SDL_OpenAudioDevice(NULL, 0, &audioSpec, NULL, 0);
|
||||||
|
SDL_PauseAudioDevice(sdlAudioDevice, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void JA_Quit() {
|
||||||
|
SDL_PauseAudioDevice(sdlAudioDevice, 1);
|
||||||
|
if (sdlAudioDevice != 0) SDL_CloseAudioDevice(sdlAudioDevice);
|
||||||
|
sdlAudioDevice = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
JA_Music_t *JA_LoadMusic(const char* filename) {
|
||||||
|
int chan, samplerate;
|
||||||
|
|
||||||
|
// [RZC 28/08/22] Carreguem primer el arxiu en memòria i després el descomprimim. Es algo més rapid.
|
||||||
|
FILE *f = fopen(filename, "rb");
|
||||||
|
fseek(f, 0, SEEK_END);
|
||||||
|
long fsize = ftell(f);
|
||||||
|
fseek(f, 0, SEEK_SET);
|
||||||
|
Uint8 *buffer = (Uint8*)malloc(fsize + 1);
|
||||||
|
if (fread(buffer, fsize, 1, f)!=1) return NULL;
|
||||||
|
fclose(f);
|
||||||
|
|
||||||
|
JA_Music_t *music = new JA_Music_t();
|
||||||
|
|
||||||
|
music->samples = stb_vorbis_decode_memory(buffer, fsize, &chan, &samplerate, &music->output);
|
||||||
|
free(buffer);
|
||||||
|
// [RZC 28/08/22] Abans el descomprimiem mentre el teniem obert
|
||||||
|
// music->samples = stb_vorbis_decode_filename(filename, &chan, &samplerate, &music->output);
|
||||||
|
|
||||||
|
SDL_AudioCVT cvt;
|
||||||
|
SDL_BuildAudioCVT(&cvt, AUDIO_S16, chan, samplerate, JA_format, JA_channels, JA_freq);
|
||||||
|
if (cvt.needed) {
|
||||||
|
cvt.len = music->samples * chan * 2;
|
||||||
|
cvt.buf = (Uint8 *) SDL_malloc(cvt.len * cvt.len_mult);
|
||||||
|
SDL_memcpy(cvt.buf, music->output, cvt.len);
|
||||||
|
SDL_ConvertAudio(&cvt);
|
||||||
|
free(music->output);
|
||||||
|
music->output = (short*)cvt.buf;
|
||||||
|
}
|
||||||
|
music->pos = 0;
|
||||||
|
music->state = JA_MUSIC_STOPPED;
|
||||||
|
|
||||||
|
return music;
|
||||||
|
}
|
||||||
|
|
||||||
|
void JA_PlayMusic(JA_Music_t *music, const int loop) {
|
||||||
|
if (current_music != NULL) {
|
||||||
|
current_music->pos = 0;
|
||||||
|
current_music->state = JA_MUSIC_STOPPED;
|
||||||
|
}
|
||||||
|
current_music = music;
|
||||||
|
current_music->pos = 0;
|
||||||
|
current_music->state = JA_MUSIC_PLAYING;
|
||||||
|
current_music->times = loop;
|
||||||
|
}
|
||||||
|
|
||||||
|
void JA_PauseMusic() {
|
||||||
|
if (current_music == NULL || current_music->state == JA_MUSIC_INVALID) return;
|
||||||
|
current_music->state = JA_MUSIC_PAUSED;
|
||||||
|
}
|
||||||
|
|
||||||
|
void JA_ResumeMusic() {
|
||||||
|
if (current_music == NULL || current_music->state == JA_MUSIC_INVALID) return;
|
||||||
|
current_music->state = JA_MUSIC_PLAYING;
|
||||||
|
}
|
||||||
|
|
||||||
|
void JA_StopMusic() {
|
||||||
|
if (current_music == NULL || current_music->state == JA_MUSIC_INVALID) return;
|
||||||
|
current_music->pos = 0;
|
||||||
|
current_music->state = JA_MUSIC_STOPPED;
|
||||||
|
}
|
||||||
|
|
||||||
|
JA_Music_state JA_GetMusicState() {
|
||||||
|
if (current_music == NULL) return JA_MUSIC_INVALID;
|
||||||
|
return current_music->state;
|
||||||
|
}
|
||||||
|
|
||||||
|
void JA_DeleteMusic(JA_Music_t *music) {
|
||||||
|
if (current_music == music) current_music = NULL;
|
||||||
|
free(music->output);
|
||||||
|
delete music;
|
||||||
|
}
|
||||||
|
|
||||||
|
JA_Sound_t *JA_NewSound(Uint8* buffer, Uint32 length) {
|
||||||
|
JA_Sound_t *sound = new JA_Sound_t();
|
||||||
|
sound->buffer = buffer;
|
||||||
|
sound->length = length;
|
||||||
|
return sound;
|
||||||
|
}
|
||||||
|
|
||||||
|
JA_Sound_t *JA_LoadSound(const char* filename) {
|
||||||
|
JA_Sound_t *sound = new JA_Sound_t();
|
||||||
|
SDL_AudioSpec wavSpec;
|
||||||
|
SDL_LoadWAV(filename, &wavSpec, &sound->buffer, &sound->length);
|
||||||
|
|
||||||
|
SDL_AudioCVT cvt;
|
||||||
|
SDL_BuildAudioCVT(&cvt, wavSpec.format, wavSpec.channels, wavSpec.freq, JA_format, JA_channels, JA_freq);
|
||||||
|
cvt.len = sound->length;
|
||||||
|
cvt.buf = (Uint8 *) SDL_malloc(cvt.len * cvt.len_mult);
|
||||||
|
SDL_memcpy(cvt.buf, sound->buffer, sound->length);
|
||||||
|
SDL_ConvertAudio(&cvt);
|
||||||
|
SDL_FreeWAV(sound->buffer);
|
||||||
|
sound->buffer = cvt.buf;
|
||||||
|
sound->length = cvt.len_cvt;
|
||||||
|
|
||||||
|
return sound;
|
||||||
|
}
|
||||||
|
|
||||||
|
int JA_PlaySound(JA_Sound_t *sound, const int loop) {
|
||||||
|
int channel = 0;
|
||||||
|
while (channel < JA_MAX_SIMULTANEOUS_CHANNELS && channels[channel].state != JA_CHANNEL_FREE) { channel++; }
|
||||||
|
if (channel == JA_MAX_SIMULTANEOUS_CHANNELS) channel = 0;
|
||||||
|
|
||||||
|
channels[channel].sound = sound;
|
||||||
|
channels[channel].times = loop;
|
||||||
|
channels[channel].pos = 0;
|
||||||
|
channels[channel].state = JA_CHANNEL_PLAYING;
|
||||||
|
return channel;
|
||||||
|
}
|
||||||
|
|
||||||
|
void JA_DeleteSound(JA_Sound_t *sound) {
|
||||||
|
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) {
|
||||||
|
if (channels[i].sound == sound) JA_StopChannel(i);
|
||||||
|
}
|
||||||
|
SDL_free(sound->buffer);
|
||||||
|
delete sound;
|
||||||
|
}
|
||||||
|
|
||||||
|
void JA_PauseChannel(const int channel) {
|
||||||
|
if (channel == -1) {
|
||||||
|
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) {
|
||||||
|
if (channels[i].state == JA_CHANNEL_PLAYING) channels[i].state = JA_CHANNEL_PAUSED;
|
||||||
|
}
|
||||||
|
} else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) {
|
||||||
|
if (channels[channel].state == JA_CHANNEL_PLAYING) channels[channel].state = JA_CHANNEL_PAUSED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void JA_ResumeChannel(const int channel) {
|
||||||
|
if (channel == -1) {
|
||||||
|
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) {
|
||||||
|
if (channels[i].state == JA_CHANNEL_PAUSED) channels[i].state = JA_CHANNEL_PLAYING;
|
||||||
|
}
|
||||||
|
} else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) {
|
||||||
|
if (channels[channel].state == JA_CHANNEL_PAUSED) channels[channel].state = JA_CHANNEL_PLAYING;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void JA_StopChannel(const int channel) {
|
||||||
|
if (channel == -1) {
|
||||||
|
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) {
|
||||||
|
channels[i].state = JA_CHANNEL_FREE;
|
||||||
|
channels[i].pos = 0;
|
||||||
|
channels[i].sound = NULL;
|
||||||
|
}
|
||||||
|
} else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) {
|
||||||
|
channels[channel].state = JA_CHANNEL_FREE;
|
||||||
|
channels[channel].pos = 0;
|
||||||
|
channels[channel].sound = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
JA_Channel_state JA_GetChannelState(const int channel) {
|
||||||
|
if (channel < 0 || channel >= JA_MAX_SIMULTANEOUS_CHANNELS) return JA_CHANNEL_INVALID;
|
||||||
|
return channels[channel].state;
|
||||||
|
}
|
||||||
|
|
||||||
|
int JA_SetVolume(int volume) {
|
||||||
|
JA_volume = volume > 128 ? 128 : volume < 0 ? 0 : volume;
|
||||||
|
return JA_volume;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
30
jail_audio.h
Normal file
30
jail_audio.h
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
|
||||||
|
enum JA_Channel_state { JA_CHANNEL_INVALID, JA_CHANNEL_FREE, JA_CHANNEL_PLAYING, JA_CHANNEL_PAUSED };
|
||||||
|
enum JA_Music_state { JA_MUSIC_INVALID, JA_MUSIC_PLAYING, JA_MUSIC_PAUSED, JA_MUSIC_STOPPED };
|
||||||
|
|
||||||
|
struct JA_Sound_t;
|
||||||
|
struct JA_Music_t;
|
||||||
|
|
||||||
|
void JA_Init(const int freq, const SDL_AudioFormat format, const int channels);
|
||||||
|
void JA_Quit();
|
||||||
|
|
||||||
|
JA_Music_t *JA_LoadMusic(const char* filename);
|
||||||
|
void JA_PlayMusic(JA_Music_t *music, const int loop = -1);
|
||||||
|
void JA_PauseMusic();
|
||||||
|
void JA_ResumeMusic();
|
||||||
|
void JA_StopMusic();
|
||||||
|
JA_Music_state JA_GetMusicState();
|
||||||
|
void JA_DeleteMusic(JA_Music_t *music);
|
||||||
|
|
||||||
|
JA_Sound_t *JA_NewSound(Uint8* buffer, Uint32 length);
|
||||||
|
JA_Sound_t *JA_LoadSound(const char* filename);
|
||||||
|
int JA_PlaySound(JA_Sound_t *sound, const int loop = 0);
|
||||||
|
void JA_PauseChannel(const int channel);
|
||||||
|
void JA_ResumeChannel(const int channel);
|
||||||
|
void JA_StopChannel(const int channel);
|
||||||
|
JA_Channel_state JA_GetChannelState(const int channel);
|
||||||
|
void JA_DeleteSound(JA_Sound_t *sound);
|
||||||
|
|
||||||
|
int JA_SetVolume(int volume);
|
||||||
101
jail_audio_sdlmixer.cpp
Normal file
101
jail_audio_sdlmixer.cpp
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
#ifdef JA_USESDLMIXER
|
||||||
|
#include "jail_audio.h"
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include <SDL2/SDL_mixer.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
struct JA_Sound_t {}; // Dummy structs
|
||||||
|
struct JA_Music_t {};
|
||||||
|
|
||||||
|
int JA_freq {48000};
|
||||||
|
SDL_AudioFormat JA_format {AUDIO_S16};
|
||||||
|
Uint8 JA_channels {2};
|
||||||
|
|
||||||
|
void JA_Init(const int freq, const SDL_AudioFormat format, const int channels) {
|
||||||
|
JA_freq = freq;
|
||||||
|
JA_format = format;
|
||||||
|
JA_channels = channels;
|
||||||
|
Mix_OpenAudio(JA_freq, JA_format, JA_channels, 1024);
|
||||||
|
}
|
||||||
|
|
||||||
|
void JA_Quit() {
|
||||||
|
Mix_CloseAudio();
|
||||||
|
}
|
||||||
|
|
||||||
|
JA_Music_t *JA_LoadMusic(const char* filename) {
|
||||||
|
return (JA_Music_t*)Mix_LoadMUS(filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
void JA_PlayMusic(JA_Music_t *music, const int loop) {
|
||||||
|
Mix_PlayMusic((Mix_Music*)music, loop);
|
||||||
|
}
|
||||||
|
|
||||||
|
void JA_PauseMusic() {
|
||||||
|
Mix_PauseMusic();
|
||||||
|
}
|
||||||
|
|
||||||
|
void JA_ResumeMusic() {
|
||||||
|
Mix_ResumeMusic();
|
||||||
|
}
|
||||||
|
|
||||||
|
void JA_StopMusic() {
|
||||||
|
Mix_HaltMusic();
|
||||||
|
}
|
||||||
|
|
||||||
|
JA_Music_state JA_GetMusicState() {
|
||||||
|
if (Mix_PausedMusic()) {
|
||||||
|
return JA_MUSIC_PAUSED;
|
||||||
|
} else if (Mix_PlayingMusic()) {
|
||||||
|
return JA_MUSIC_PLAYING;
|
||||||
|
} else {
|
||||||
|
return JA_MUSIC_STOPPED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void JA_DeleteMusic(JA_Music_t *music) {
|
||||||
|
Mix_FreeMusic((Mix_Music*)music);
|
||||||
|
}
|
||||||
|
|
||||||
|
JA_Sound_t *JA_NewSound(Uint8* buffer, Uint32 length) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
JA_Sound_t *JA_LoadSound(const char* filename) {
|
||||||
|
JA_Sound_t *sound = (JA_Sound_t*)Mix_LoadWAV(filename);
|
||||||
|
return sound;
|
||||||
|
}
|
||||||
|
|
||||||
|
int JA_PlaySound(JA_Sound_t *sound, const int loop) {
|
||||||
|
return Mix_PlayChannel(-1, (Mix_Chunk*)sound, loop);
|
||||||
|
}
|
||||||
|
|
||||||
|
void JA_DeleteSound(JA_Sound_t *sound) {
|
||||||
|
Mix_FreeChunk((Mix_Chunk*)sound);
|
||||||
|
}
|
||||||
|
|
||||||
|
void JA_PauseChannel(const int channel) {
|
||||||
|
Mix_Pause(channel);
|
||||||
|
}
|
||||||
|
|
||||||
|
void JA_ResumeChannel(const int channel) {
|
||||||
|
Mix_Resume(channel);
|
||||||
|
}
|
||||||
|
|
||||||
|
void JA_StopChannel(const int channel) {
|
||||||
|
Mix_HaltChannel(channel);
|
||||||
|
}
|
||||||
|
|
||||||
|
JA_Channel_state JA_GetChannelState(const int channel) {
|
||||||
|
if (Mix_Paused(channel)) {
|
||||||
|
return JA_CHANNEL_PAUSED;
|
||||||
|
} else if (Mix_Playing(channel)) {
|
||||||
|
return JA_CHANNEL_PLAYING;
|
||||||
|
} else {
|
||||||
|
return JA_CHANNEL_FREE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int JA_SetVolume(int volume) {
|
||||||
|
return Mix_Volume(-1, volume);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
154
jscore.cpp
Normal file
154
jscore.cpp
Normal file
@@ -0,0 +1,154 @@
|
|||||||
|
#include "jscore.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <winsock2.h>
|
||||||
|
#else
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#endif
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace jscore {
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
struct user {
|
||||||
|
string name;
|
||||||
|
int points;
|
||||||
|
};
|
||||||
|
vector<user> score;
|
||||||
|
|
||||||
|
#define bzero(b,len) (memset((b), '\0', (len)), (void) 0)
|
||||||
|
int sock;
|
||||||
|
struct sockaddr_in client;
|
||||||
|
|
||||||
|
int PORT = 9911;
|
||||||
|
string HOST = "jaildoctor.duckdns.org";
|
||||||
|
|
||||||
|
#ifdef WIN32
|
||||||
|
WSADATA WsaData;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
bool jscore_error = false;
|
||||||
|
string error_message;
|
||||||
|
|
||||||
|
void init(std::string host, const int port) {
|
||||||
|
PORT = port;
|
||||||
|
HOST = host;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setErrorMessage(string message) {
|
||||||
|
jscore_error = true;
|
||||||
|
error_message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
string sendRequest(const string request) {
|
||||||
|
#ifdef WIN32
|
||||||
|
int ret = WSAStartup(0x101,&WsaData);
|
||||||
|
if (ret != 0) return 0;
|
||||||
|
#endif
|
||||||
|
struct hostent * host = gethostbyname(HOST.c_str());
|
||||||
|
|
||||||
|
if ( (host == NULL) || (host->h_addr == NULL) ) {
|
||||||
|
setErrorMessage("Error retrieving DNS information.\n");
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
bzero(&client, sizeof(client));
|
||||||
|
client.sin_family = AF_INET;
|
||||||
|
client.sin_port = htons( PORT );
|
||||||
|
memcpy(&client.sin_addr, host->h_addr, host->h_length);
|
||||||
|
|
||||||
|
sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
|
||||||
|
if (sock < 0) {
|
||||||
|
setErrorMessage("Error creating socket.\n");
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( connect(sock, (struct sockaddr *)&client, sizeof(client)) < 0 ) {
|
||||||
|
close(sock);
|
||||||
|
setErrorMessage("Could not connect\n");
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
string r = request + " HTTP/1.1\r\nHost: "+HOST+"\r\nConnection: close\r\n\r\n\r\n";
|
||||||
|
if (send(sock, r.c_str(), r.length(), 0) != (int)r.length()) {
|
||||||
|
setErrorMessage("Error sending request.\n");
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
char cur;
|
||||||
|
char start[5]="\r\n\r\n";
|
||||||
|
int pos = 0;
|
||||||
|
while ( recv(sock, &cur, 1,0) > 0 ) {
|
||||||
|
if (cur==start[pos]) { pos++; if (pos == 4) break; } else { pos = 0; }
|
||||||
|
}
|
||||||
|
|
||||||
|
char buffer[1024]; buffer[0]=0; pos=0;
|
||||||
|
while ( recv(sock, &cur, 1,0) > 0 ) {
|
||||||
|
buffer[pos] = cur;
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
#ifdef WIN32
|
||||||
|
WSACleanup();
|
||||||
|
#endif
|
||||||
|
buffer[pos]=0;
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
const bool initOnlineScore(string game) {
|
||||||
|
string strbuff = sendRequest("GET /score-list.php?game=" + game);
|
||||||
|
if (jscore_error) return not jscore_error;
|
||||||
|
|
||||||
|
user u;
|
||||||
|
char buffer[1024];
|
||||||
|
strcpy(buffer, strbuff.c_str());
|
||||||
|
char *str = buffer;
|
||||||
|
char *p = str;
|
||||||
|
score.clear();
|
||||||
|
while (*p!=0) {
|
||||||
|
while (*p!=',') {p++;}
|
||||||
|
*p=0; u.name = str; p++; str=p;
|
||||||
|
while (*p!='\n') {p++;}
|
||||||
|
*p=0; u.points = atoi(str); p++; str=p;
|
||||||
|
score.push_back(u);
|
||||||
|
}
|
||||||
|
return not jscore_error;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int getNumUsers() {
|
||||||
|
return score.size();
|
||||||
|
}
|
||||||
|
string getUserName(const int index) {
|
||||||
|
return score[index].name;
|
||||||
|
}
|
||||||
|
const int getPoints(const int index) {
|
||||||
|
return score[index].points;
|
||||||
|
}
|
||||||
|
|
||||||
|
const bool updateUserPoints(string game, string user, const int points) {
|
||||||
|
string strbuff = sendRequest("GET /score-update.php?game=" + game + "&user=" + user + "&points=" + to_string(points));
|
||||||
|
initOnlineScore(game);
|
||||||
|
return not jscore_error;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int getUserPoints(string game, std::string user) {
|
||||||
|
return atoi(sendRequest("GET /getuserpoints.php?game=" + game + "&user=" + user).c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
string getUserData(string game, string user) {
|
||||||
|
return sendRequest("GET /getuserdata.php?game=" + game + "&user=" + user);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setUserData(string game, string user, string data) {
|
||||||
|
sendRequest("GET /setuserdata.php?game=" + game + "&user=" + user + "&data=" + data);
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
16
jscore.h
Normal file
16
jscore.h
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace jscore {
|
||||||
|
void init(std::string host, const int port);
|
||||||
|
const bool initOnlineScore(std::string game);
|
||||||
|
const int getNumUsers();
|
||||||
|
std::string getUserName(const int index);
|
||||||
|
const int getPoints(const int index);
|
||||||
|
const int getUserPoints(std::string game, std::string user);
|
||||||
|
|
||||||
|
const bool updateUserPoints(std::string game, std::string user, const int points);
|
||||||
|
std::string getUserData(std::string game, std::string user);
|
||||||
|
void setUserData(std::string game, std::string user, std::string data);
|
||||||
|
};
|
||||||
|
|
||||||
982
menu.cpp
Normal file
982
menu.cpp
Normal file
@@ -0,0 +1,982 @@
|
|||||||
|
#include "../const.h"
|
||||||
|
#include "menu.h"
|
||||||
|
|
||||||
|
// Constructor
|
||||||
|
Menu::Menu(SDL_Renderer *renderer, Resource *resource, Asset *asset, Input *input, std::string file)
|
||||||
|
{
|
||||||
|
// Copia punteros
|
||||||
|
this->renderer = renderer;
|
||||||
|
this->asset = asset;
|
||||||
|
this->input = input;
|
||||||
|
|
||||||
|
// Inicializa punteros
|
||||||
|
soundMove = nullptr;
|
||||||
|
soundAccept = nullptr;
|
||||||
|
soundCancel = nullptr;
|
||||||
|
|
||||||
|
// Inicializa variables
|
||||||
|
name = "";
|
||||||
|
selector.index = 0;
|
||||||
|
itemSelected = MENU_NO_OPTION;
|
||||||
|
x = 0;
|
||||||
|
y = 0;
|
||||||
|
w = 0;
|
||||||
|
rectBG.rect = {0, 0, 0, 0};
|
||||||
|
rectBG.color = {0, 0, 0};
|
||||||
|
rectBG.a = 0;
|
||||||
|
backgroundType = MENU_BACKGROUND_SOLID;
|
||||||
|
isCenteredOnX = false;
|
||||||
|
isCenteredOnY = false;
|
||||||
|
areElementsCenteredOnX = false;
|
||||||
|
centerX = 0;
|
||||||
|
centerY = 0;
|
||||||
|
widestItem = 0;
|
||||||
|
colorGreyed = {128, 128, 128};
|
||||||
|
defaultActionWhenCancel = 0;
|
||||||
|
font_png = "";
|
||||||
|
font_txt = "";
|
||||||
|
|
||||||
|
// Selector
|
||||||
|
selector.originY = 0;
|
||||||
|
selector.targetY = 0;
|
||||||
|
selector.despY = 0;
|
||||||
|
selector.originH = 0;
|
||||||
|
selector.targetH = 0;
|
||||||
|
selector.incH = 0;
|
||||||
|
selector.y = 0.0f;
|
||||||
|
selector.h = 0.0f;
|
||||||
|
selector.numJumps = 8;
|
||||||
|
selector.moving = false;
|
||||||
|
selector.resizing = false;
|
||||||
|
selector.rect = {0, 0, 0, 0};
|
||||||
|
selector.color = {0, 0, 0};
|
||||||
|
selector.itemColor = {0, 0, 0};
|
||||||
|
selector.a = 255;
|
||||||
|
|
||||||
|
// Inicializa las variables desde un fichero
|
||||||
|
if (file != "")
|
||||||
|
{
|
||||||
|
load(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deja el cursor en el primer elemento
|
||||||
|
reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
Menu::~Menu()
|
||||||
|
{
|
||||||
|
renderer = nullptr;
|
||||||
|
asset = nullptr;
|
||||||
|
input = nullptr;
|
||||||
|
|
||||||
|
if (soundMove)
|
||||||
|
{
|
||||||
|
JA_DeleteSound(soundMove);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (soundAccept)
|
||||||
|
{
|
||||||
|
JA_DeleteSound(soundAccept);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (soundCancel)
|
||||||
|
{
|
||||||
|
JA_DeleteSound(soundCancel);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (text != nullptr)
|
||||||
|
{
|
||||||
|
delete text;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Carga la configuración del menu desde un archivo de texto
|
||||||
|
bool Menu::load(std::string file_path)
|
||||||
|
{
|
||||||
|
// Indicador de éxito en la carga
|
||||||
|
bool success = true;
|
||||||
|
|
||||||
|
// Indica si se ha creado ya el objeto de texto
|
||||||
|
bool textAllocated = false;
|
||||||
|
|
||||||
|
const std::string filename = file_path.substr(file_path.find_last_of("\\/") + 1);
|
||||||
|
std::string line;
|
||||||
|
std::ifstream file(file_path);
|
||||||
|
|
||||||
|
// El fichero se puede abrir
|
||||||
|
if (file.good())
|
||||||
|
{
|
||||||
|
// Procesa el fichero linea a linea
|
||||||
|
std::cout << "Reading file " << filename.c_str() << std::endl;
|
||||||
|
while (std::getline(file, line))
|
||||||
|
{
|
||||||
|
if (line == "[item]")
|
||||||
|
{
|
||||||
|
item_t item;
|
||||||
|
item.label = "";
|
||||||
|
item.hPaddingDown = 1;
|
||||||
|
item.selectable = true;
|
||||||
|
item.greyed = false;
|
||||||
|
item.linkedDown = false;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
// Lee la siguiente linea
|
||||||
|
std::getline(file, line);
|
||||||
|
|
||||||
|
// Encuentra la posición del caracter '='
|
||||||
|
int pos = line.find("=");
|
||||||
|
|
||||||
|
// Procesa las dos subcadenas
|
||||||
|
if (!setItem(&item, line.substr(0, pos), line.substr(pos + 1, line.length())))
|
||||||
|
{
|
||||||
|
std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl;
|
||||||
|
success = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
} while (line != "[/item]");
|
||||||
|
|
||||||
|
addItem(item.label, item.hPaddingDown, item.selectable, item.greyed, item.linkedDown);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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("=");
|
||||||
|
// Procesa las dos subcadenas
|
||||||
|
if (!setVars(line.substr(0, pos), line.substr(pos + 1, line.length())))
|
||||||
|
{
|
||||||
|
std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl;
|
||||||
|
success = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Crea el objeto text tan pronto como se pueda. Necesario para añadir items
|
||||||
|
if (font_png != "" && font_txt != "" && !textAllocated)
|
||||||
|
{
|
||||||
|
text = new Text(resource->getOffset(font_txt), resource->getTexture(font_png), renderer);
|
||||||
|
textAllocated = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cierra el fichero
|
||||||
|
std::cout << "Closing file " << filename.c_str() << std::endl;
|
||||||
|
file.close();
|
||||||
|
}
|
||||||
|
// El fichero no se puede abrir
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl;
|
||||||
|
success = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Asigna variables a partir de dos cadenas
|
||||||
|
bool Menu::setItem(item_t *item, std::string var, std::string value)
|
||||||
|
{
|
||||||
|
// Indicador de éxito en la asignación
|
||||||
|
bool success = true;
|
||||||
|
|
||||||
|
if (var == "text")
|
||||||
|
{
|
||||||
|
item->label = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (var == "hPaddingDown")
|
||||||
|
{
|
||||||
|
item->hPaddingDown = std::stoi(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (var == "selectable")
|
||||||
|
{
|
||||||
|
item->selectable = value == "true" ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (var == "greyed")
|
||||||
|
{
|
||||||
|
item->greyed = value == "true" ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (var == "linkedDown")
|
||||||
|
{
|
||||||
|
item->linkedDown = value == "true" ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if ((var == "") || (var == "[/item]"))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
success = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Asigna variables a partir de dos cadenas
|
||||||
|
bool Menu::setVars(std::string var, std::string value)
|
||||||
|
{
|
||||||
|
// Indicador de éxito en la asignación
|
||||||
|
bool success = true;
|
||||||
|
|
||||||
|
if (var == "font_png")
|
||||||
|
{
|
||||||
|
font_png = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (var == "font_txt")
|
||||||
|
{
|
||||||
|
font_txt = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (var == "sound_cancel")
|
||||||
|
{
|
||||||
|
soundCancel = JA_LoadSound(asset->get(value).c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (var == "sound_accept")
|
||||||
|
{
|
||||||
|
soundAccept = JA_LoadSound(asset->get(value).c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (var == "sound_move")
|
||||||
|
{
|
||||||
|
soundMove = JA_LoadSound(asset->get(value).c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (var == "name")
|
||||||
|
{
|
||||||
|
name = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (var == "x")
|
||||||
|
{
|
||||||
|
x = std::stoi(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (var == "centerX")
|
||||||
|
{
|
||||||
|
centerX = std::stoi(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (var == "centerY")
|
||||||
|
{
|
||||||
|
centerY = std::stoi(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (var == "y")
|
||||||
|
{
|
||||||
|
y = std::stoi(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (var == "backgroundType")
|
||||||
|
{
|
||||||
|
backgroundType = std::stoi(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (var == "backgroundColor")
|
||||||
|
{
|
||||||
|
// Se introducen los valores separados por comas en un vector
|
||||||
|
std::stringstream ss(value);
|
||||||
|
std::string tmp;
|
||||||
|
getline(ss, tmp, ',');
|
||||||
|
rectBG.color.r = std::stoi(tmp);
|
||||||
|
getline(ss, tmp, ',');
|
||||||
|
rectBG.color.g = std::stoi(tmp);
|
||||||
|
getline(ss, tmp, ',');
|
||||||
|
rectBG.color.b = std::stoi(tmp);
|
||||||
|
getline(ss, tmp, ',');
|
||||||
|
rectBG.a = std::stoi(tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (var == "selector_color")
|
||||||
|
{
|
||||||
|
// Se introducen los valores separados por comas en un vector
|
||||||
|
std::stringstream ss(value);
|
||||||
|
std::string tmp;
|
||||||
|
getline(ss, tmp, ',');
|
||||||
|
selector.color.r = std::stoi(tmp);
|
||||||
|
getline(ss, tmp, ',');
|
||||||
|
selector.color.g = std::stoi(tmp);
|
||||||
|
getline(ss, tmp, ',');
|
||||||
|
selector.color.b = std::stoi(tmp);
|
||||||
|
getline(ss, tmp, ',');
|
||||||
|
selector.a = std::stoi(tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (var == "selector_text_color")
|
||||||
|
{
|
||||||
|
// Se introducen los valores separados por comas en un vector
|
||||||
|
std::stringstream ss(value);
|
||||||
|
std::string tmp;
|
||||||
|
getline(ss, tmp, ',');
|
||||||
|
selector.itemColor.r = std::stoi(tmp);
|
||||||
|
getline(ss, tmp, ',');
|
||||||
|
selector.itemColor.g = std::stoi(tmp);
|
||||||
|
getline(ss, tmp, ',');
|
||||||
|
selector.itemColor.b = std::stoi(tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (var == "areElementsCenteredOnX")
|
||||||
|
{
|
||||||
|
areElementsCenteredOnX = value == "true" ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (var == "isCenteredOnX")
|
||||||
|
{
|
||||||
|
isCenteredOnX = value == "true" ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (var == "isCenteredOnY")
|
||||||
|
{
|
||||||
|
isCenteredOnY = value == "true" ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (var == "defaultActionWhenCancel")
|
||||||
|
{
|
||||||
|
defaultActionWhenCancel = std::stoi(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (var == "")
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
success = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Carga los ficheros de audio
|
||||||
|
void Menu::loadAudioFile(std::string file, int sound)
|
||||||
|
{
|
||||||
|
switch (sound)
|
||||||
|
{
|
||||||
|
case SOUND_ACCEPT:
|
||||||
|
soundAccept = JA_LoadSound(file.c_str());
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SOUND_CANCEL:
|
||||||
|
soundCancel = JA_LoadSound(file.c_str());
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SOUND_MOVE:
|
||||||
|
soundMove = JA_LoadSound(file.c_str());
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene el nombre del menu
|
||||||
|
std::string Menu::getName()
|
||||||
|
{
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene el valor de la variable
|
||||||
|
int Menu::getItemSelected()
|
||||||
|
{
|
||||||
|
// Al llamar a esta funcion, se obtiene el valor y se borra
|
||||||
|
const int temp = itemSelected;
|
||||||
|
itemSelected = MENU_NO_OPTION;
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actualiza la posicion y el estado del selector
|
||||||
|
void Menu::updateSelector()
|
||||||
|
{
|
||||||
|
if (selector.moving)
|
||||||
|
{
|
||||||
|
// Calcula el desplazamiento en Y
|
||||||
|
selector.y += selector.despY;
|
||||||
|
if (selector.despY > 0) // Va hacia abajo
|
||||||
|
{
|
||||||
|
if (selector.y > selector.targetY) // Ha llegado al destino
|
||||||
|
{
|
||||||
|
selector.originY = selector.y = selector.targetY;
|
||||||
|
selector.moving = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (selector.despY < 0) // Va hacia arriba
|
||||||
|
{
|
||||||
|
if (selector.y < selector.targetY) // Ha llegado al destino
|
||||||
|
{
|
||||||
|
selector.originY = selector.y = selector.targetY;
|
||||||
|
selector.moving = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
selector.rect.y = int(selector.y);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
selector.rect.y = int(selector.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (selector.resizing)
|
||||||
|
{
|
||||||
|
// Calcula el incremento en H
|
||||||
|
selector.h += selector.incH;
|
||||||
|
if (selector.incH > 0) // Crece
|
||||||
|
{
|
||||||
|
if (selector.h > selector.targetH) // Ha llegado al destino
|
||||||
|
{
|
||||||
|
// selector.originH = selector.targetH = selector.rect.h = getSelectorHeight(selector.index);
|
||||||
|
selector.originH = selector.h = selector.targetH;
|
||||||
|
selector.resizing = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (selector.incH < 0) // Decrece
|
||||||
|
{
|
||||||
|
if (selector.h < selector.targetH) // Ha llegado al destino
|
||||||
|
{
|
||||||
|
// selector.originH = selector.targetH = selector.rect.h = getSelectorHeight(selector.index);
|
||||||
|
selector.originH = selector.h = selector.targetH;
|
||||||
|
selector.resizing = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
selector.rect.h = int(selector.h);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
selector.rect.h = getSelectorHeight(selector.index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Coloca el selector en una posición específica
|
||||||
|
void Menu::setSelectorPos(int index)
|
||||||
|
{
|
||||||
|
if (index < (int)item.size())
|
||||||
|
{
|
||||||
|
selector.index = index;
|
||||||
|
selector.rect.y = selector.y = selector.originY = selector.targetY = item[selector.index].rect.y;
|
||||||
|
selector.rect.w = rectBG.rect.w;
|
||||||
|
selector.rect.x = rectBG.rect.x;
|
||||||
|
selector.originH = selector.targetH = selector.rect.h = getSelectorHeight(selector.index);
|
||||||
|
selector.moving = false;
|
||||||
|
selector.resizing = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene la anchura del elemento más ancho del menu
|
||||||
|
int Menu::getWidestItem()
|
||||||
|
{
|
||||||
|
int result = 0;
|
||||||
|
|
||||||
|
// Obtenemos la anchura del item mas ancho
|
||||||
|
for (auto &i : item)
|
||||||
|
{
|
||||||
|
result = std::max(result, i.rect.w);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deja el menu apuntando al primer elemento
|
||||||
|
void Menu::reset()
|
||||||
|
{
|
||||||
|
itemSelected = MENU_NO_OPTION;
|
||||||
|
selector.index = 0;
|
||||||
|
selector.originY = selector.targetY = selector.y = item[0].rect.y;
|
||||||
|
selector.originH = selector.targetH = item[0].rect.h;
|
||||||
|
selector.moving = false;
|
||||||
|
selector.resizing = false;
|
||||||
|
|
||||||
|
// Si el primer elemento no es seleccionable, incrementa el selector
|
||||||
|
if (!item[selector.index].selectable)
|
||||||
|
{
|
||||||
|
increaseSelectorIndex();
|
||||||
|
setSelectorPos(selector.index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actualiza el menu para recolocarlo correctamente y establecer el tamaño
|
||||||
|
void Menu::reorganize()
|
||||||
|
{
|
||||||
|
setRectSize();
|
||||||
|
|
||||||
|
if (isCenteredOnX)
|
||||||
|
{
|
||||||
|
centerMenuOnX(centerX);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isCenteredOnY)
|
||||||
|
{
|
||||||
|
centerMenuOnY(centerY);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (areElementsCenteredOnX)
|
||||||
|
{
|
||||||
|
centerMenuElementsOnX();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deja el menu apuntando al siguiente elemento
|
||||||
|
bool Menu::increaseSelectorIndex()
|
||||||
|
{
|
||||||
|
// Obten las coordenadas del elemento actual
|
||||||
|
selector.y = selector.originY = item[selector.index].rect.y;
|
||||||
|
selector.h = selector.originH = getSelectorHeight(selector.index);
|
||||||
|
|
||||||
|
// Calcula cual es el siguiente elemento
|
||||||
|
++selector.index %= item.size();
|
||||||
|
while (!item[selector.index].selectable)
|
||||||
|
{
|
||||||
|
++selector.index %= item.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece las coordenadas y altura de destino
|
||||||
|
selector.targetY = item[selector.index].rect.y;
|
||||||
|
selector.despY = (selector.targetY - selector.originY) / selector.numJumps;
|
||||||
|
|
||||||
|
selector.targetH = getSelectorHeight(selector.index);
|
||||||
|
selector.incH = (selector.targetH - selector.originH) / selector.numJumps;
|
||||||
|
|
||||||
|
selector.moving = true;
|
||||||
|
if (selector.incH != 0)
|
||||||
|
{
|
||||||
|
selector.resizing = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deja el menu apuntando al elemento anterior
|
||||||
|
bool Menu::decreaseSelectorIndex()
|
||||||
|
{
|
||||||
|
// Obten las coordenadas del elemento actual
|
||||||
|
selector.y = selector.originY = item[selector.index].rect.y;
|
||||||
|
selector.h = selector.originH = getSelectorHeight(selector.index);
|
||||||
|
|
||||||
|
// Calcula cual es el siguiente elemento
|
||||||
|
if (selector.index == 0)
|
||||||
|
{
|
||||||
|
selector.index = item.size() - 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
selector.index--;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (!item[selector.index].selectable)
|
||||||
|
{
|
||||||
|
if (selector.index == 0)
|
||||||
|
{
|
||||||
|
selector.index = item.size() - 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
selector.index--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece las coordenadas y altura de destino
|
||||||
|
selector.targetY = item[selector.index].rect.y;
|
||||||
|
selector.despY = (selector.targetY - selector.originY) / selector.numJumps;
|
||||||
|
|
||||||
|
selector.targetH = getSelectorHeight(selector.index);
|
||||||
|
selector.incH = (selector.targetH - selector.originH) / selector.numJumps;
|
||||||
|
|
||||||
|
selector.moving = true;
|
||||||
|
if (selector.incH != 0)
|
||||||
|
{
|
||||||
|
selector.resizing = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actualiza la logica del menu
|
||||||
|
void Menu::update()
|
||||||
|
{
|
||||||
|
checkInput();
|
||||||
|
updateSelector();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pinta el menu en pantalla
|
||||||
|
void Menu::render()
|
||||||
|
{
|
||||||
|
// Rendereritza el fondo del menu
|
||||||
|
if (backgroundType == MENU_BACKGROUND_SOLID)
|
||||||
|
{
|
||||||
|
SDL_SetRenderDrawColor(renderer, rectBG.color.r, rectBG.color.g, rectBG.color.b, rectBG.a);
|
||||||
|
SDL_RenderFillRect(renderer, &rectBG.rect);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Renderiza el rectangulo del selector
|
||||||
|
const SDL_Rect temp = {selector.rect.x, selector.rect.y - 1, selector.rect.w, selector.rect.h + 1};
|
||||||
|
SDL_SetRenderDrawColor(renderer, selector.color.r, selector.color.g, selector.color.b, selector.a);
|
||||||
|
SDL_RenderFillRect(renderer, &temp);
|
||||||
|
|
||||||
|
// Renderiza el borde del fondo
|
||||||
|
if (backgroundType == MENU_BACKGROUND_SOLID)
|
||||||
|
{
|
||||||
|
SDL_SetRenderDrawColor(renderer, rectBG.color.r, rectBG.color.g, rectBG.color.b, 255);
|
||||||
|
SDL_RenderDrawRect(renderer, &rectBG.rect);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Renderiza el texto
|
||||||
|
for (int i = 0; i < (int)item.size(); ++i)
|
||||||
|
{
|
||||||
|
if (i == selector.index)
|
||||||
|
{
|
||||||
|
const color_t color = {selector.itemColor.r, selector.itemColor.g, selector.itemColor.b};
|
||||||
|
text->writeColored(item[i].rect.x, item[i].rect.y, item[i].label, color);
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (item[i].selectable)
|
||||||
|
{
|
||||||
|
text->write(item[i].rect.x, item[i].rect.y, item[i].label);
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (item[i].greyed)
|
||||||
|
{
|
||||||
|
text->writeColored(item[i].rect.x, item[i].rect.y, item[i].label, colorGreyed);
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{ // No seleccionable
|
||||||
|
if ((item[i].linkedUp) && (i == selector.index + 1))
|
||||||
|
{
|
||||||
|
const color_t color = {selector.itemColor.r, selector.itemColor.g, selector.itemColor.b};
|
||||||
|
text->writeColored(item[i].rect.x, item[i].rect.y, item[i].label, color);
|
||||||
|
}
|
||||||
|
else // No enlazado con el de arriba
|
||||||
|
{
|
||||||
|
text->write(item[i].rect.x, item[i].rect.y, item[i].label);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el rectangulo de fondo del menu y el selector
|
||||||
|
void Menu::setRectSize(int w, int h)
|
||||||
|
{
|
||||||
|
// Establece el ancho
|
||||||
|
if (w == 0)
|
||||||
|
{ // Si no se pasa un valor, se busca si hay uno prefijado
|
||||||
|
if (this->w == 0)
|
||||||
|
{ // Si no hay prefijado, coge el item mas ancho
|
||||||
|
rectBG.rect.w = findWidth() + text->getCharacterSize();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{ // Si hay prefijado, coge ese
|
||||||
|
rectBG.rect.w = this->w;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{ // Si se pasa un valor, se usa y se prefija
|
||||||
|
rectBG.rect.w = w;
|
||||||
|
this->w = w;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el alto
|
||||||
|
if (h == 0)
|
||||||
|
{ // Si no se pasa un valor, se busca de manera automatica
|
||||||
|
rectBG.rect.h = findHeight() + text->getCharacterSize();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{ // Si se pasa un valor, se aplica
|
||||||
|
rectBG.rect.h = h;
|
||||||
|
}
|
||||||
|
|
||||||
|
// La posición X es la del menú menos medio caracter
|
||||||
|
if (this->w != 0)
|
||||||
|
{ // Si el ancho esta prefijado, la x coinccide
|
||||||
|
rectBG.rect.x = x;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{ // Si el ancho es automatico, se le da un poco de margen
|
||||||
|
rectBG.rect.x = x - (text->getCharacterSize() / 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// La posición Y es la del menu menos la altura de medio caracter
|
||||||
|
rectBG.rect.y = y - (text->getCharacterSize() / 2);
|
||||||
|
|
||||||
|
// Establecemos los valores del rectangulo del selector a partir de los valores del rectangulo de fondo
|
||||||
|
setSelectorPos(selector.index);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el color del rectangulo de fondo
|
||||||
|
void Menu::setBackgroundColor(color_t color, int alpha)
|
||||||
|
{
|
||||||
|
rectBG.color = color;
|
||||||
|
rectBG.a = alpha;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el color del rectangulo del selector
|
||||||
|
void Menu::setSelectorColor(color_t color, int alpha)
|
||||||
|
{
|
||||||
|
selector.color = color;
|
||||||
|
selector.a = alpha;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el color del texto del selector
|
||||||
|
void Menu::setSelectorTextColor(color_t color)
|
||||||
|
{
|
||||||
|
selector.itemColor = color;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Centra el menu respecto un punto en el eje X
|
||||||
|
void Menu::centerMenuOnX(int value)
|
||||||
|
{
|
||||||
|
isCenteredOnX = true;
|
||||||
|
if (value != 0)
|
||||||
|
{
|
||||||
|
centerX = value;
|
||||||
|
}
|
||||||
|
else if (centerX == 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece la nueva posición centrada en funcion del elemento más ancho o del ancho fijo del menu
|
||||||
|
if (w != 0)
|
||||||
|
{ // Si se ha definido un ancho fijo
|
||||||
|
x = (centerX) - (w / 2);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{ // Si se actua en función del elemento más ancho
|
||||||
|
x = (centerX) - (findWidth() / 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actualiza el rectangulo de fondo y del selector
|
||||||
|
rectBG.rect.x = x;
|
||||||
|
selector.rect.x = x;
|
||||||
|
|
||||||
|
// Reposiciona los elementos del menu
|
||||||
|
for (auto &i : item)
|
||||||
|
{
|
||||||
|
i.rect.x = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Recalcula el rectangulo de fondo
|
||||||
|
setRectSize();
|
||||||
|
|
||||||
|
// Vuelve a centrar los elementos si fuera el caso
|
||||||
|
if (areElementsCenteredOnX)
|
||||||
|
{
|
||||||
|
centerMenuElementsOnX();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Centra el menu respecto un punto en el eje Y
|
||||||
|
void Menu::centerMenuOnY(int value)
|
||||||
|
{
|
||||||
|
isCenteredOnY = true;
|
||||||
|
centerY = value;
|
||||||
|
|
||||||
|
// Establece la nueva posición centrada en funcion del elemento más ancho
|
||||||
|
y = (value) - (findHeight() / 2);
|
||||||
|
|
||||||
|
// Reposiciona los elementos del menu
|
||||||
|
replaceElementsOnY();
|
||||||
|
|
||||||
|
// Recalcula el rectangulo de fondo
|
||||||
|
setRectSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Centra los elementos del menu en el eje X
|
||||||
|
void Menu::centerMenuElementsOnX()
|
||||||
|
{
|
||||||
|
areElementsCenteredOnX = true;
|
||||||
|
|
||||||
|
for (auto &i : item)
|
||||||
|
{
|
||||||
|
i.rect.x = (centerX - (i.rect.w / 2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Añade un item al menu
|
||||||
|
void Menu::addItem(std::string text, int hPaddingDown, bool selectable, bool greyed, bool linkedDown)
|
||||||
|
{
|
||||||
|
item_t temp;
|
||||||
|
|
||||||
|
if (item.empty())
|
||||||
|
{ // Si es el primer item coge la posición en el eje Y del propio menu
|
||||||
|
temp.rect.y = y;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{ // En caso contrario, coge la posición en el eje Y a partir del último elemento
|
||||||
|
temp.rect.y = item.back().rect.y + item.back().rect.h + item.back().hPaddingDown;
|
||||||
|
}
|
||||||
|
|
||||||
|
temp.rect.x = x;
|
||||||
|
temp.hPaddingDown = hPaddingDown;
|
||||||
|
temp.selectable = selectable;
|
||||||
|
temp.greyed = greyed;
|
||||||
|
temp.linkedDown = linkedDown;
|
||||||
|
|
||||||
|
item.push_back(temp);
|
||||||
|
|
||||||
|
setItemCaption(item.size() - 1, text);
|
||||||
|
|
||||||
|
if (item.size() > 1)
|
||||||
|
{
|
||||||
|
if (item[item.size() - 2].linkedDown)
|
||||||
|
{
|
||||||
|
item.back().linkedUp = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
centerX = x + (findWidth() / 2);
|
||||||
|
reorganize();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cambia el texto de un item
|
||||||
|
void Menu::setItemCaption(int index, std::string text)
|
||||||
|
{
|
||||||
|
item[index].label = text;
|
||||||
|
item[index].rect.w = this->text->lenght(item[index].label);
|
||||||
|
item[index].rect.h = this->text->getCharacterSize();
|
||||||
|
reorganize();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el indice del itemm que se usará por defecto al cancelar el menu
|
||||||
|
void Menu::setDefaultActionWhenCancel(int item)
|
||||||
|
{
|
||||||
|
defaultActionWhenCancel = item;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gestiona la entrada de teclado y mando durante el menu
|
||||||
|
void Menu::checkInput()
|
||||||
|
{
|
||||||
|
if (input->checkInput(input_up, REPEAT_FALSE))
|
||||||
|
{
|
||||||
|
if (decreaseSelectorIndex())
|
||||||
|
{
|
||||||
|
if (soundMove)
|
||||||
|
{
|
||||||
|
JA_PlaySound(soundMove);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (input->checkInput(input_down, REPEAT_FALSE))
|
||||||
|
{
|
||||||
|
if (increaseSelectorIndex())
|
||||||
|
{
|
||||||
|
if (soundMove)
|
||||||
|
{
|
||||||
|
JA_PlaySound(soundMove);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (input->checkInput(input_accept, REPEAT_FALSE))
|
||||||
|
{
|
||||||
|
itemSelected = selector.index;
|
||||||
|
if (soundAccept)
|
||||||
|
{
|
||||||
|
JA_PlaySound(soundAccept);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (input->checkInput(input_cancel, REPEAT_FALSE))
|
||||||
|
{
|
||||||
|
itemSelected = defaultActionWhenCancel;
|
||||||
|
if (soundCancel)
|
||||||
|
{
|
||||||
|
JA_PlaySound(soundCancel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calcula el ancho del menu
|
||||||
|
int Menu::findWidth()
|
||||||
|
{
|
||||||
|
return getWidestItem();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calcula el alto del menu
|
||||||
|
int Menu::findHeight()
|
||||||
|
{
|
||||||
|
int height = 0;
|
||||||
|
|
||||||
|
// Obtenemos la altura de la suma de alturas de los items
|
||||||
|
for (auto &i : item)
|
||||||
|
{
|
||||||
|
height += i.rect.h + i.hPaddingDown;
|
||||||
|
}
|
||||||
|
|
||||||
|
return height - item.back().hPaddingDown;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Recoloca los elementos del menu en el eje Y
|
||||||
|
void Menu::replaceElementsOnY()
|
||||||
|
{
|
||||||
|
item[0].rect.y = y;
|
||||||
|
|
||||||
|
for (int i = 1; i < (int)item.size(); i++)
|
||||||
|
{
|
||||||
|
item[i].rect.y = item[i - 1].rect.y + item[i - 1].rect.h + item[i - 1].hPaddingDown;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el estado seleccionable de un item
|
||||||
|
void Menu::setSelectable(int index, bool value)
|
||||||
|
{
|
||||||
|
item[index].selectable = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el estado agrisado de un item
|
||||||
|
void Menu::setGreyed(int index, bool value)
|
||||||
|
{
|
||||||
|
item[index].greyed = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el estado de enlace de un item
|
||||||
|
void Menu::setLinkedDown(int index, bool value)
|
||||||
|
{
|
||||||
|
item[index].linkedDown = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calcula la altura del selector
|
||||||
|
int Menu::getSelectorHeight(int value)
|
||||||
|
{
|
||||||
|
if (item[value].linkedDown)
|
||||||
|
{
|
||||||
|
return item[value].rect.h + item[value].hPaddingDown + item[value + 1].rect.h;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return item[value].rect.h;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el nombre del menu
|
||||||
|
void Menu::setName(std::string name)
|
||||||
|
{
|
||||||
|
this->name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece la posición del menu
|
||||||
|
void Menu::setPos(int x, int y)
|
||||||
|
{
|
||||||
|
this->x = x;
|
||||||
|
this->y = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el tipo de fondo del menu
|
||||||
|
void Menu::setBackgroundType(int value)
|
||||||
|
{
|
||||||
|
backgroundType = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece la fuente de texto que se utilizará
|
||||||
|
void Menu::setText(std::string font_png, std::string font_txt)
|
||||||
|
{
|
||||||
|
if (!text)
|
||||||
|
{
|
||||||
|
text = new Text(resource->getOffset(font_txt), resource->getTexture(font_png), renderer);
|
||||||
|
}
|
||||||
|
}
|
||||||
226
menu.h
Normal file
226
menu.h
Normal file
@@ -0,0 +1,226 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include "asset.h"
|
||||||
|
#include "input.h"
|
||||||
|
#include "jail_audio.h"
|
||||||
|
#include "resource.h"
|
||||||
|
#include "sprite.h"
|
||||||
|
#include "text.h"
|
||||||
|
#include "utils.h"
|
||||||
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#ifndef MENU_H
|
||||||
|
#define MENU_H
|
||||||
|
|
||||||
|
// Tipos de fondos para el menu
|
||||||
|
#define MENU_BACKGROUND_TRANSPARENT 0
|
||||||
|
#define MENU_BACKGROUND_SOLID 1
|
||||||
|
|
||||||
|
// Tipos de archivos de audio
|
||||||
|
#define SOUND_ACCEPT 0
|
||||||
|
#define SOUND_MOVE 1
|
||||||
|
#define SOUND_CANCEL 2
|
||||||
|
|
||||||
|
// Opciones de menu
|
||||||
|
#define MENU_NO_OPTION -1
|
||||||
|
|
||||||
|
// Clase Menu
|
||||||
|
class Menu
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
struct rectangle_t
|
||||||
|
{
|
||||||
|
SDL_Rect rect; // Rectangulo
|
||||||
|
color_t color; // Color
|
||||||
|
int a; // Transparencia
|
||||||
|
};
|
||||||
|
|
||||||
|
struct item_t
|
||||||
|
{
|
||||||
|
std::string label; // Texto
|
||||||
|
SDL_Rect rect; // Rectangulo que delimita el elemento
|
||||||
|
int hPaddingDown; // Espaciado bajo el elemento
|
||||||
|
bool selectable; // Indica si se puede seleccionar
|
||||||
|
bool greyed; // Indica si ha de aparecer con otro color mas oscuro
|
||||||
|
bool linkedDown; // Indica si el elemento actual y el siguiente se tratan como uno solo. Afecta al selector
|
||||||
|
bool linkedUp; // Indica si el elemento actual y el anterior se tratan como uno solo. Afecta al selector
|
||||||
|
};
|
||||||
|
|
||||||
|
struct selector_t
|
||||||
|
{
|
||||||
|
float originY; // Coordenada de origen
|
||||||
|
float targetY; // Coordenada de destino
|
||||||
|
float despY; // Cantidad de pixeles que se desplaza el selector en cada salto: (target - origin) / numJumps
|
||||||
|
bool moving; // Indica si el selector está avanzando hacia el destino
|
||||||
|
float originH; // Altura de origen
|
||||||
|
float targetH; // Altura de destino
|
||||||
|
float incH; // Cantidad de pixels que debe incrementar o decrementar el selector en cada salto
|
||||||
|
bool resizing; // Indica si el selector está cambiando de tamaño
|
||||||
|
float y; // Coordenada actual, usado para el desplazamiento
|
||||||
|
float h; // Altura actual, usado para el cambio de tamaño
|
||||||
|
int numJumps; // Numero de pasos preestablecido para llegar al destino
|
||||||
|
int index; // Elemento del menu que tiene el foco
|
||||||
|
SDL_Rect rect; // Rectangulo del selector
|
||||||
|
color_t color; // Color del selector
|
||||||
|
color_t itemColor; // Color del item
|
||||||
|
int a; // Cantidad de transparencia para el rectangulo del selector
|
||||||
|
};
|
||||||
|
|
||||||
|
// Objetos y punteros
|
||||||
|
SDL_Renderer *renderer; // Puntero al renderizador de la ventana
|
||||||
|
Resource *resource; // Objeto con los recursos
|
||||||
|
Asset *asset; // Objeto para gestionar los ficheros de recursos
|
||||||
|
Text *text; // Texto para poder escribir los items del menu
|
||||||
|
Input *input; // Gestor de eventos de entrada de teclado o gamepad
|
||||||
|
|
||||||
|
// Variables
|
||||||
|
std::string name; // Nombre del menu
|
||||||
|
int x; // Posición en el eje X de la primera letra del primer elemento
|
||||||
|
int y; // Posición en el eje Y de la primera letra del primer elemento
|
||||||
|
int h; // Altura del menu
|
||||||
|
int w; // Anchura del menu
|
||||||
|
int itemSelected; // Índice del item del menu que ha sido seleccionado
|
||||||
|
int defaultActionWhenCancel; // Indice del item del menu que se selecciona cuando se cancela el menu
|
||||||
|
int backgroundType; // Tipo de fondo para el menu
|
||||||
|
int centerX; // Centro del menu en el eje X
|
||||||
|
int centerY; // Centro del menu en el eje Y
|
||||||
|
bool isCenteredOnX; // Variable para saber si el menu debe estar centrado respecto a un punto en el eje X
|
||||||
|
bool isCenteredOnY; // Variable para saber si el menu debe estar centrado respecto a un punto en el eje Y
|
||||||
|
bool areElementsCenteredOnX; // Variable para saber si los elementos van centrados en el eje X
|
||||||
|
int widestItem; // Anchura del elemento más ancho
|
||||||
|
JA_Sound_t* soundAccept; // Sonido al aceptar o elegir una opción del menu
|
||||||
|
JA_Sound_t* soundCancel; // Sonido al cancelar el menu
|
||||||
|
JA_Sound_t* soundMove; // Sonido al mover el selector
|
||||||
|
color_t colorGreyed; // Color para los elementos agrisados
|
||||||
|
rectangle_t rectBG; // Rectangulo de fondo del menu
|
||||||
|
std::vector<item_t> item; // Estructura para cada elemento del menu
|
||||||
|
selector_t selector; // Variables para pintar el selector del menu
|
||||||
|
std::string font_png;
|
||||||
|
std::string font_txt;
|
||||||
|
|
||||||
|
// Carga la configuración del menu desde un archivo de texto
|
||||||
|
bool load(std::string file_path);
|
||||||
|
|
||||||
|
// Asigna variables a partir de dos cadenas
|
||||||
|
bool setVars(std::string var, std::string value);
|
||||||
|
|
||||||
|
// Asigna variables a partir de dos cadenas
|
||||||
|
bool setItem(item_t *item, std::string var, std::string value);
|
||||||
|
|
||||||
|
// Actualiza el menu para recolocarlo correctamente y establecer el tamaño
|
||||||
|
void reorganize();
|
||||||
|
|
||||||
|
// Deja el menu apuntando al siguiente elemento
|
||||||
|
bool increaseSelectorIndex();
|
||||||
|
|
||||||
|
// Deja el menu apuntando al elemento anterior
|
||||||
|
bool decreaseSelectorIndex();
|
||||||
|
|
||||||
|
// Actualiza la posicion y el estado del selector
|
||||||
|
void updateSelector();
|
||||||
|
|
||||||
|
// Obtiene la anchura del elemento más ancho del menu
|
||||||
|
int getWidestItem();
|
||||||
|
|
||||||
|
// Gestiona la entrada de teclado y mando durante el menu
|
||||||
|
void checkMenuInput(Menu *menu);
|
||||||
|
|
||||||
|
// Calcula el ancho del menu
|
||||||
|
int findWidth();
|
||||||
|
|
||||||
|
// Calcula el alto del menu
|
||||||
|
int findHeight();
|
||||||
|
|
||||||
|
// Recoloca los elementos del menu en el eje Y
|
||||||
|
void replaceElementsOnY();
|
||||||
|
|
||||||
|
// Calcula la altura del selector
|
||||||
|
int getSelectorHeight(int value);
|
||||||
|
|
||||||
|
public:
|
||||||
|
// Constructor
|
||||||
|
Menu(SDL_Renderer *renderer, Resource *resource, Asset *asset, Input *input, std::string file = "");
|
||||||
|
|
||||||
|
// Destructor
|
||||||
|
~Menu();
|
||||||
|
|
||||||
|
// Carga los ficheros de audio
|
||||||
|
void loadAudioFile(std::string file, int sound);
|
||||||
|
|
||||||
|
// Obtiene el nombre del menu
|
||||||
|
std::string getName();
|
||||||
|
|
||||||
|
// Obtiene el valor de la variable
|
||||||
|
int getItemSelected();
|
||||||
|
|
||||||
|
// Deja el menu apuntando al primer elemento
|
||||||
|
void reset();
|
||||||
|
|
||||||
|
// Gestiona la entrada de teclado y mando durante el menu
|
||||||
|
void checkInput();
|
||||||
|
|
||||||
|
// Actualiza la logica del menu
|
||||||
|
void update();
|
||||||
|
|
||||||
|
// Pinta el menu en pantalla
|
||||||
|
void render();
|
||||||
|
|
||||||
|
// Establece el color del rectangulo de fondo
|
||||||
|
void setBackgroundColor(color_t color, int alpha);
|
||||||
|
|
||||||
|
// Establece el color del rectangulo del selector
|
||||||
|
void setSelectorColor(color_t color, int alpha);
|
||||||
|
|
||||||
|
// Establece el color del texto del selector
|
||||||
|
void setSelectorTextColor(color_t color);
|
||||||
|
|
||||||
|
// Centra el menu respecto a un punto en el eje X
|
||||||
|
void centerMenuOnX(int value = 0);
|
||||||
|
|
||||||
|
// Centra el menu respecto a un punto en el eje Y
|
||||||
|
void centerMenuOnY(int value);
|
||||||
|
|
||||||
|
// Centra los elementos del menu en el eje X
|
||||||
|
void centerMenuElementsOnX();
|
||||||
|
|
||||||
|
// Añade un item al menu
|
||||||
|
void addItem(std::string text, int hPaddingDown = 1, bool selectable = true, bool greyed = false, bool linkedDown = false);
|
||||||
|
|
||||||
|
// Cambia el texto de un item
|
||||||
|
void setItemCaption(int index, std::string text);
|
||||||
|
|
||||||
|
// Establece el indice del item que se usará por defecto al cancelar el menu
|
||||||
|
void setDefaultActionWhenCancel(int item);
|
||||||
|
|
||||||
|
// Coloca el selector en una posición específica
|
||||||
|
void setSelectorPos(int index);
|
||||||
|
|
||||||
|
// Establece el estado seleccionable de un item
|
||||||
|
void setSelectable(int index, bool value);
|
||||||
|
|
||||||
|
// Establece el estado agrisado de un item
|
||||||
|
void setGreyed(int index, bool value);
|
||||||
|
|
||||||
|
// Establece el estado de enlace de un item
|
||||||
|
void setLinkedDown(int index, bool value);
|
||||||
|
|
||||||
|
// Establece el nombre del menu
|
||||||
|
void setName(std::string name);
|
||||||
|
|
||||||
|
// Establece la posición del menu
|
||||||
|
void setPos(int x, int y);
|
||||||
|
|
||||||
|
// Establece el tipo de fondo del menu
|
||||||
|
void setBackgroundType(int value);
|
||||||
|
|
||||||
|
// Establece la fuente de texto que se utilizará
|
||||||
|
void setText(std::string font_png, std::string font_txt);
|
||||||
|
|
||||||
|
// Establece el rectangulo de fondo del menu
|
||||||
|
void setRectSize(int w = 0, int h = 0);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
416
movingsprite.cpp
Normal file
416
movingsprite.cpp
Normal file
@@ -0,0 +1,416 @@
|
|||||||
|
#include "../const.h"
|
||||||
|
#include "movingsprite.h"
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
currentFlipH = false;
|
||||||
|
currentFlipV = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 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
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
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
|
||||||
|
center = nullptr; // Centro de rotación
|
||||||
|
rotateSpeed = 0; // Velocidad de giro
|
||||||
|
rotateAmount = 0.0; // Cantidad de grados a girar en cada iteración
|
||||||
|
counter = 0; // Contador interno
|
||||||
|
|
||||||
|
currentFlip = SDL_FLIP_NONE; // Establece como se ha de voltear el sprite
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mueve el sprite
|
||||||
|
void MovingSprite::move()
|
||||||
|
{
|
||||||
|
if (enabled)
|
||||||
|
{
|
||||||
|
xPrev = x;
|
||||||
|
yPrev = y;
|
||||||
|
|
||||||
|
x += vx;
|
||||||
|
y += vy;
|
||||||
|
|
||||||
|
vx += ax;
|
||||||
|
vy += ay;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Muestra el sprite por pantalla
|
||||||
|
void MovingSprite::render()
|
||||||
|
{
|
||||||
|
if (enabled)
|
||||||
|
{
|
||||||
|
texture->render(renderer, (int)x, (int)y, &spriteClip, zoomW, zoomH, angle, center, currentFlip);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene el valor de la variable
|
||||||
|
float MovingSprite::getPosX()
|
||||||
|
{
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene el valor de la variable
|
||||||
|
float MovingSprite::getPosY()
|
||||||
|
{
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene el valor de la variable
|
||||||
|
float MovingSprite::getVelX()
|
||||||
|
{
|
||||||
|
return vx;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene el valor de la variable
|
||||||
|
float MovingSprite::getVelY()
|
||||||
|
{
|
||||||
|
return vy;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene el valor de la variable
|
||||||
|
float MovingSprite::getAccelX()
|
||||||
|
{
|
||||||
|
return ax;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene el valor de la variable
|
||||||
|
float MovingSprite::getAccelY()
|
||||||
|
{
|
||||||
|
return ay;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene el valor de la variable
|
||||||
|
float MovingSprite::getZoomW()
|
||||||
|
{
|
||||||
|
return zoomW;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene el valor de la variable
|
||||||
|
float MovingSprite::getZoomH()
|
||||||
|
{
|
||||||
|
return zoomH;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene el valor de la variable
|
||||||
|
double MovingSprite::getAngle()
|
||||||
|
{
|
||||||
|
return angle;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece la posición y el tamaño del objeto
|
||||||
|
void MovingSprite::setRect(SDL_Rect rect)
|
||||||
|
{
|
||||||
|
x = (float)rect.x;
|
||||||
|
y = (float)rect.y;
|
||||||
|
w = rect.w;
|
||||||
|
h = rect.h;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void MovingSprite::setPosX(float value)
|
||||||
|
{
|
||||||
|
x = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void MovingSprite::setPosY(float value)
|
||||||
|
{
|
||||||
|
y = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void MovingSprite::setVelX(float value)
|
||||||
|
{
|
||||||
|
vx = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void MovingSprite::setVelY(float value)
|
||||||
|
{
|
||||||
|
vy = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void MovingSprite::setAccelX(float value)
|
||||||
|
{
|
||||||
|
ax = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void MovingSprite::setAccelY(float value)
|
||||||
|
{
|
||||||
|
ay = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void MovingSprite::setZoomW(float value)
|
||||||
|
{
|
||||||
|
zoomW = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void MovingSprite::setZoomH(float value)
|
||||||
|
{
|
||||||
|
zoomH = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void MovingSprite::setAngle(double value)
|
||||||
|
{
|
||||||
|
angle = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Incrementa el valor de la variable
|
||||||
|
void MovingSprite::incAngle(double value)
|
||||||
|
{
|
||||||
|
angle += value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decrementa el valor de la variable
|
||||||
|
void MovingSprite::decAngle(double value)
|
||||||
|
{
|
||||||
|
angle -= value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene el valor de la variable
|
||||||
|
bool MovingSprite::getRotate()
|
||||||
|
{
|
||||||
|
return rotateEnabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene el valor de la variable
|
||||||
|
Uint16 MovingSprite::getRotateSpeed()
|
||||||
|
{
|
||||||
|
return rotateSpeed;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece la rotacion
|
||||||
|
void MovingSprite::rotate()
|
||||||
|
{
|
||||||
|
if (enabled)
|
||||||
|
if (rotateEnabled)
|
||||||
|
{
|
||||||
|
if (counter % rotateSpeed == 0)
|
||||||
|
{
|
||||||
|
incAngle(rotateAmount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void MovingSprite::setRotate(bool value)
|
||||||
|
{
|
||||||
|
rotateEnabled = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void MovingSprite::setRotateSpeed(int value)
|
||||||
|
{
|
||||||
|
if (value < 1)
|
||||||
|
{
|
||||||
|
rotateSpeed = 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
rotateSpeed = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void MovingSprite::setRotateAmount(double value)
|
||||||
|
{
|
||||||
|
rotateAmount = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void MovingSprite::disableRotate()
|
||||||
|
{
|
||||||
|
rotateEnabled = false;
|
||||||
|
angle = (double)0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actualiza las variables internas del objeto
|
||||||
|
void MovingSprite::update()
|
||||||
|
{
|
||||||
|
move();
|
||||||
|
rotate();
|
||||||
|
|
||||||
|
if (enabled)
|
||||||
|
{
|
||||||
|
++counter %= 60000;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cambia el sentido de la rotación
|
||||||
|
void MovingSprite::switchRotate()
|
||||||
|
{
|
||||||
|
rotateAmount *= -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actualiza el valor de la variable
|
||||||
|
void MovingSprite::updateCurrentFlip()
|
||||||
|
{
|
||||||
|
if (currentFlipH && currentFlipV)
|
||||||
|
{
|
||||||
|
currentFlip = SDL_RendererFlip(SDL_FLIP_HORIZONTAL | SDL_FLIP_VERTICAL);
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (currentFlipH && !currentFlipV)
|
||||||
|
{
|
||||||
|
currentFlip = SDL_FLIP_HORIZONTAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (!currentFlipH && currentFlipV)
|
||||||
|
{
|
||||||
|
currentFlip = SDL_FLIP_VERTICAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (!currentFlipH && !currentFlipV)
|
||||||
|
{
|
||||||
|
currentFlip = SDL_FLIP_NONE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void MovingSprite::setFlipH(bool flip)
|
||||||
|
{
|
||||||
|
currentFlipH = flip;
|
||||||
|
updateCurrentFlip();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gira el sprite horizontalmente
|
||||||
|
void MovingSprite::flipH()
|
||||||
|
{
|
||||||
|
currentFlipH = !currentFlipH;
|
||||||
|
updateCurrentFlip();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void MovingSprite::setFlipV(bool flip)
|
||||||
|
{
|
||||||
|
currentFlipV = flip;
|
||||||
|
updateCurrentFlip();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Voltea el sprite verticalmente
|
||||||
|
void MovingSprite::flipV()
|
||||||
|
{
|
||||||
|
currentFlipV = !currentFlipV;
|
||||||
|
updateCurrentFlip();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene el valor de la variable
|
||||||
|
SDL_RendererFlip MovingSprite::getFlip()
|
||||||
|
{
|
||||||
|
return currentFlip;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene el valor de la variable
|
||||||
|
bool MovingSprite::getFlipH()
|
||||||
|
{
|
||||||
|
return currentFlipH;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene el valor de la variable
|
||||||
|
bool MovingSprite::getFlipV()
|
||||||
|
{
|
||||||
|
return currentFlipV;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Devuelve el rectangulo donde está el sprite
|
||||||
|
SDL_Rect MovingSprite::getRect()
|
||||||
|
{
|
||||||
|
const SDL_Rect rect = {(int)x, (int)y, w, h};
|
||||||
|
return rect;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deshace el último movimiento
|
||||||
|
void MovingSprite::undoMove()
|
||||||
|
{
|
||||||
|
x = xPrev;
|
||||||
|
y = yPrev;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deshace el último movimiento en el eje X
|
||||||
|
void MovingSprite::undoMoveX()
|
||||||
|
{
|
||||||
|
x = xPrev;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deshace el último movimiento en el eje Y
|
||||||
|
void MovingSprite::undoMoveY()
|
||||||
|
{
|
||||||
|
y = yPrev;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pone a cero las velocidades de desplacamiento
|
||||||
|
void MovingSprite::clearVel()
|
||||||
|
{
|
||||||
|
vx = vy = 0.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Devuelve el incremento en el eje X en pixels
|
||||||
|
int MovingSprite::getIncX()
|
||||||
|
{
|
||||||
|
return (int)x - (int)xPrev;
|
||||||
|
}
|
||||||
187
movingsprite.h
Normal file
187
movingsprite.h
Normal file
@@ -0,0 +1,187 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include "sprite.h"
|
||||||
|
|
||||||
|
#ifndef MOVINGSPRITE_H
|
||||||
|
#define MOVINGSPRITE_H
|
||||||
|
|
||||||
|
// Clase MovingSprite. Añade posicion y velocidad en punto flotante
|
||||||
|
class MovingSprite : public Sprite
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
// Objetos y punteros
|
||||||
|
SDL_Point *center; // Centro de rotación
|
||||||
|
|
||||||
|
// Variables
|
||||||
|
float x; // Posición en el eje X
|
||||||
|
float y; // Posición en el eje Y
|
||||||
|
|
||||||
|
float xPrev; // Posición anterior en el eje X
|
||||||
|
float yPrev; // Posición anterior en el eje Y
|
||||||
|
|
||||||
|
float vx; // Velocidad en el eje X. Cantidad de pixeles a desplazarse
|
||||||
|
float vy; // Velocidad en el eje Y. Cantidad de pixeles a desplazarse
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
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_RendererFlip currentFlip; // Indica como se voltea el sprite
|
||||||
|
bool currentFlipV;
|
||||||
|
bool currentFlipH;
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
// Mueve el sprite
|
||||||
|
void move();
|
||||||
|
|
||||||
|
// Rota el sprite
|
||||||
|
void rotate();
|
||||||
|
|
||||||
|
// Actualiza las variables internas del objeto
|
||||||
|
void update();
|
||||||
|
|
||||||
|
// Reinicia todas las variables
|
||||||
|
void clear();
|
||||||
|
|
||||||
|
// Muestra el sprite por pantalla
|
||||||
|
void render();
|
||||||
|
|
||||||
|
// Obten el valor de la variable
|
||||||
|
float getPosX();
|
||||||
|
|
||||||
|
// Obten el valor de la variable
|
||||||
|
float getPosY();
|
||||||
|
|
||||||
|
// Obten el valor de la variable
|
||||||
|
float getVelX();
|
||||||
|
|
||||||
|
// Obten el valor de la variable
|
||||||
|
float getVelY();
|
||||||
|
|
||||||
|
// Obten el valor de la variable
|
||||||
|
float getAccelX();
|
||||||
|
|
||||||
|
// Obten el valor de la variable
|
||||||
|
float getAccelY();
|
||||||
|
|
||||||
|
// Obten el valor de la variable
|
||||||
|
float getZoomW();
|
||||||
|
|
||||||
|
// Obten el valor de la variable
|
||||||
|
float getZoomH();
|
||||||
|
|
||||||
|
// Obten el valor de la variable
|
||||||
|
double getAngle();
|
||||||
|
|
||||||
|
// Obtiene el valor de la variable
|
||||||
|
bool getRotate();
|
||||||
|
|
||||||
|
// Obtiene el valor de la variable
|
||||||
|
Uint16 getRotateSpeed();
|
||||||
|
|
||||||
|
// Establece la posición y el tamaño del objeto
|
||||||
|
void setRect(SDL_Rect rect);
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void setPosX(float value);
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void setPosY(float value);
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void setVelX(float value);
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void setVelY(float value);
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void setAccelX(float value);
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void setAccelY(float value);
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void setZoomW(float value);
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void setZoomH(float value);
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void setAngle(double vaue);
|
||||||
|
|
||||||
|
// Incrementa el valor de la variable
|
||||||
|
void incAngle(double value);
|
||||||
|
|
||||||
|
// Decrementa el valor de la variable
|
||||||
|
void decAngle(double value);
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void setRotate(bool value);
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void setRotateSpeed(int value);
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void setRotateAmount(double value);
|
||||||
|
|
||||||
|
// Quita el efecto de rotación y deja el sprite en su angulo inicial.
|
||||||
|
void disableRotate();
|
||||||
|
|
||||||
|
// Cambia el sentido de la rotación
|
||||||
|
void switchRotate();
|
||||||
|
|
||||||
|
// Actualiza el valor de la variable
|
||||||
|
void updateCurrentFlip();
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void setFlipH(bool flip);
|
||||||
|
|
||||||
|
// Gira el sprite horizontalmente
|
||||||
|
void flipH();
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void setFlipV(bool flip);
|
||||||
|
|
||||||
|
// Voltea el sprite verticalmente
|
||||||
|
void flipV();
|
||||||
|
|
||||||
|
// Obtiene el valor de la variable
|
||||||
|
SDL_RendererFlip getFlip();
|
||||||
|
|
||||||
|
// Obtiene el valor de la variable
|
||||||
|
bool getFlipH();
|
||||||
|
|
||||||
|
// Obtiene el valor de la variable
|
||||||
|
bool getFlipV();
|
||||||
|
|
||||||
|
// Devuelve el rectangulo donde está el sprite
|
||||||
|
SDL_Rect getRect();
|
||||||
|
|
||||||
|
// Deshace el último movimiento
|
||||||
|
void undoMove();
|
||||||
|
|
||||||
|
// Deshace el último movimiento en el eje X
|
||||||
|
void undoMoveX();
|
||||||
|
|
||||||
|
// Deshace el último movimiento en el eje Y
|
||||||
|
void undoMoveY();
|
||||||
|
|
||||||
|
// Pone a cero las velocidades de desplacamiento
|
||||||
|
void clearVel();
|
||||||
|
|
||||||
|
// Devuelve el incremento en el eje X en pixels
|
||||||
|
int getIncX();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
267
notify.cpp
Normal file
267
notify.cpp
Normal file
@@ -0,0 +1,267 @@
|
|||||||
|
#include "notify.h"
|
||||||
|
#include <string>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
// Constructor
|
||||||
|
Notify::Notify(SDL_Renderer *renderer, std::string iconFile, std::string bitmapFile, std::string textFile, std::string soundFile, options_t *options)
|
||||||
|
{
|
||||||
|
// Inicializa variables
|
||||||
|
this->renderer = renderer;
|
||||||
|
this->options = options;
|
||||||
|
bgColor = options->notifications.color;
|
||||||
|
waitTime = 300;
|
||||||
|
|
||||||
|
// Crea objetos
|
||||||
|
iconTexture = new Texture(renderer, iconFile);
|
||||||
|
textTexture = new Texture(renderer, bitmapFile);
|
||||||
|
text = new Text(textFile, textTexture, renderer);
|
||||||
|
sound = JA_LoadSound(soundFile.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Destructor
|
||||||
|
Notify::~Notify()
|
||||||
|
{
|
||||||
|
// Libera la memoria de los objetos
|
||||||
|
delete textTexture;
|
||||||
|
delete iconTexture;
|
||||||
|
delete text;
|
||||||
|
JA_DeleteSound(sound);
|
||||||
|
|
||||||
|
for (auto notification : notifications)
|
||||||
|
{
|
||||||
|
delete notification.sprite;
|
||||||
|
delete notification.texture;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dibuja las notificaciones por pantalla
|
||||||
|
void Notify::render()
|
||||||
|
{
|
||||||
|
for (int i = (int)notifications.size() - 1; i >= 0; --i)
|
||||||
|
{
|
||||||
|
notifications[i].sprite->render();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actualiza el estado de las notificaiones
|
||||||
|
void Notify::update()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < (int)notifications.size(); ++i)
|
||||||
|
{
|
||||||
|
notifications[i].counter++;
|
||||||
|
|
||||||
|
// Comprueba los estados
|
||||||
|
if (notifications[i].state == ns_rising)
|
||||||
|
{
|
||||||
|
const float step = ((float)notifications[i].counter / notifications[i].travelDist);
|
||||||
|
const int alpha = 255 * step;
|
||||||
|
|
||||||
|
if (options->notifications.posV == pos_top)
|
||||||
|
{
|
||||||
|
notifications[i].rect.y++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
notifications[i].rect.y--;
|
||||||
|
}
|
||||||
|
notifications[i].texture->setAlpha(alpha);
|
||||||
|
|
||||||
|
if (notifications[i].rect.y == notifications[i].y)
|
||||||
|
{
|
||||||
|
notifications[i].state = ns_stay;
|
||||||
|
notifications[i].texture->setAlpha(255);
|
||||||
|
notifications[i].counter = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (notifications[i].state == ns_stay)
|
||||||
|
{
|
||||||
|
if (notifications[i].counter == waitTime)
|
||||||
|
{
|
||||||
|
notifications[i].state = ns_vanishing;
|
||||||
|
notifications[i].counter = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (notifications[i].state == ns_vanishing)
|
||||||
|
{
|
||||||
|
|
||||||
|
const float step = (notifications[i].counter / (float)notifications[i].travelDist);
|
||||||
|
const int alpha = 255 * (1 - step);
|
||||||
|
|
||||||
|
if (options->notifications.posV == pos_top)
|
||||||
|
{
|
||||||
|
notifications[i].rect.y--;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
notifications[i].rect.y++;
|
||||||
|
}
|
||||||
|
notifications[i].texture->setAlpha(alpha);
|
||||||
|
|
||||||
|
if (notifications[i].rect.y == notifications[i].y - notifications[i].travelDist)
|
||||||
|
{
|
||||||
|
notifications[i].state = ns_finished;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
notifications[i].sprite->setRect(notifications[i].rect);
|
||||||
|
}
|
||||||
|
|
||||||
|
clearFinishedNotifications();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Elimina las notificaciones finalizadas
|
||||||
|
void Notify::clearFinishedNotifications()
|
||||||
|
{
|
||||||
|
for (int i = (int)notifications.size() - 1; i >= 0; --i)
|
||||||
|
{
|
||||||
|
if (notifications[i].state == ns_finished)
|
||||||
|
{
|
||||||
|
delete notifications[i].sprite;
|
||||||
|
delete notifications[i].texture;
|
||||||
|
notifications.erase(notifications.begin() + i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Muestra una notificación de texto por pantalla;
|
||||||
|
void Notify::showText(std::string text1, std::string text2, int icon)
|
||||||
|
{
|
||||||
|
// Inicializa variables
|
||||||
|
const int iconSize = 16;
|
||||||
|
const int padding = text->getCharacterSize();
|
||||||
|
const int iconSpace = icon >= 0 ? iconSize + padding : 0;
|
||||||
|
const std::string txt = text1.length() > text2.length() ? text1 : text2;
|
||||||
|
const int width = text->lenght(txt) + (padding * 2) + iconSpace;
|
||||||
|
const int height = (text->getCharacterSize() * 2) + (padding * 2);
|
||||||
|
|
||||||
|
// Posición horizontal
|
||||||
|
int despH = 0;
|
||||||
|
if (options->notifications.posH == pos_left)
|
||||||
|
{
|
||||||
|
despH = padding;
|
||||||
|
}
|
||||||
|
else if (options->notifications.posH == pos_middle)
|
||||||
|
{
|
||||||
|
despH = ((options->screen.windowWidth * options->windowSize) / 2 - (width / 2));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
despH = (options->screen.windowWidth * options->windowSize) - width - padding;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Posición vertical
|
||||||
|
int despV = 0;
|
||||||
|
if (options->notifications.posV == pos_top)
|
||||||
|
{
|
||||||
|
despV = padding;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
despV = (options->screen.windowHeight * options->windowSize) - height - padding;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int travelDist = height + padding;
|
||||||
|
|
||||||
|
// Offset
|
||||||
|
int offset = 0;
|
||||||
|
if (options->notifications.posV == pos_top)
|
||||||
|
{
|
||||||
|
offset = (int)notifications.size() > 0 ? notifications.back().y + travelDist : despV;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
offset = (int)notifications.size() > 0 ? notifications.back().y - travelDist : despV;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Crea la notificacion
|
||||||
|
notification_t n;
|
||||||
|
|
||||||
|
// Inicializa variables
|
||||||
|
n.y = offset;
|
||||||
|
n.travelDist = travelDist;
|
||||||
|
n.counter = 0;
|
||||||
|
n.state = ns_rising;
|
||||||
|
n.text1 = text1;
|
||||||
|
n.text2 = text2;
|
||||||
|
if (options->notifications.posV == pos_top)
|
||||||
|
{
|
||||||
|
n.rect = {despH, offset - travelDist, width, height};
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
n.rect = {despH, offset + travelDist, width, height};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Crea la textura
|
||||||
|
n.texture = new Texture(renderer);
|
||||||
|
n.texture->createBlank(renderer, width, height, SDL_TEXTUREACCESS_TARGET);
|
||||||
|
n.texture->setBlendMode(SDL_BLENDMODE_BLEND);
|
||||||
|
|
||||||
|
// Prepara para dibujar en la textura
|
||||||
|
n.texture->setAsRenderTarget(renderer);
|
||||||
|
|
||||||
|
// Dibuja el fondo de la notificación
|
||||||
|
SDL_SetRenderDrawColor(renderer, bgColor.r, bgColor.g, bgColor.b, 255);
|
||||||
|
SDL_Rect rect;
|
||||||
|
rect = {4, 0, width - (4 * 2), height};
|
||||||
|
SDL_RenderFillRect(renderer, &rect);
|
||||||
|
|
||||||
|
rect = {4 / 2, 1, width - 4, height - 2};
|
||||||
|
SDL_RenderFillRect(renderer, &rect);
|
||||||
|
|
||||||
|
rect = {1, 4 / 2, width - 2, height - 4};
|
||||||
|
SDL_RenderFillRect(renderer, &rect);
|
||||||
|
|
||||||
|
rect = {0, 4, width, height - (4 * 2)};
|
||||||
|
SDL_RenderFillRect(renderer, &rect);
|
||||||
|
|
||||||
|
// Dibuja el icono de la notificación
|
||||||
|
if (icon >= 0)
|
||||||
|
{
|
||||||
|
Sprite *sp = new Sprite({0, 0, iconSize, iconSize}, iconTexture, renderer);
|
||||||
|
sp->setPos({padding, padding, iconSize, iconSize});
|
||||||
|
sp->setSpriteClip({iconSize * (icon % 10), iconSize * (icon / 10), iconSize, iconSize});
|
||||||
|
sp->render();
|
||||||
|
delete sp;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Escribe el texto de la notificación
|
||||||
|
color_t color = {255, 255, 255};
|
||||||
|
if (text2 != "")
|
||||||
|
{ // Dos lineas de texto
|
||||||
|
text->writeColored(padding + iconSpace, padding, text1, color);
|
||||||
|
text->writeColored(padding + iconSpace, padding + text->getCharacterSize() + 1, text2, color);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{ // Una linea de texto
|
||||||
|
text->writeColored(padding + iconSpace, (height / 2) - (text->getCharacterSize() / 2), text1, color);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deja de dibujar en la textura
|
||||||
|
SDL_SetRenderTarget(renderer, nullptr);
|
||||||
|
|
||||||
|
// Crea el sprite de la notificación
|
||||||
|
n.sprite = new Sprite(n.rect, n.texture, renderer);
|
||||||
|
|
||||||
|
// Añade la notificación a la lista
|
||||||
|
notifications.push_back(n);
|
||||||
|
|
||||||
|
// Reproduce el sonido de la notificación
|
||||||
|
if (options->notifications.sound)
|
||||||
|
{
|
||||||
|
JA_PlaySound(sound);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Indica si hay notificaciones activas
|
||||||
|
bool Notify::active()
|
||||||
|
{
|
||||||
|
if ((int)notifications.size() > 0)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
87
notify.h
Normal file
87
notify.h
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include "jail_audio.h"
|
||||||
|
#include "sprite.h"
|
||||||
|
#include "text.h"
|
||||||
|
#include "texture.h"
|
||||||
|
#include "utils.h"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#ifndef NOTIFY_H
|
||||||
|
#define NOTIFY_H
|
||||||
|
|
||||||
|
class Notify
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
enum notification_state_e
|
||||||
|
{
|
||||||
|
ns_rising,
|
||||||
|
ns_stay,
|
||||||
|
ns_vanishing,
|
||||||
|
ns_finished
|
||||||
|
};
|
||||||
|
|
||||||
|
enum notification_position_e
|
||||||
|
{
|
||||||
|
upperLeft,
|
||||||
|
upperCenter,
|
||||||
|
upperRight,
|
||||||
|
middleLeft,
|
||||||
|
middleRight,
|
||||||
|
bottomLeft,
|
||||||
|
bottomCenter,
|
||||||
|
bottomRight
|
||||||
|
};
|
||||||
|
|
||||||
|
struct notification_t
|
||||||
|
{
|
||||||
|
std::string text1;
|
||||||
|
std::string text2;
|
||||||
|
int counter;
|
||||||
|
notification_state_e state;
|
||||||
|
notification_position_e position;
|
||||||
|
Texture *texture;
|
||||||
|
Sprite *sprite;
|
||||||
|
SDL_Rect rect;
|
||||||
|
int y;
|
||||||
|
int travelDist;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Objetos y punteros
|
||||||
|
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||||
|
Texture *textTexture; // Textura para la fuente de las notificaciones
|
||||||
|
Texture *iconTexture; // Textura para los iconos de las notificaciones
|
||||||
|
Text *text; // Objeto para dibujar texto
|
||||||
|
options_t *options; // Variable con todas las opciones del programa
|
||||||
|
|
||||||
|
// Variables
|
||||||
|
color_t bgColor; // Color de fondo de las notificaciones
|
||||||
|
int waitTime; // Tiempo que se ve la notificación
|
||||||
|
std::vector<notification_t> notifications; // La lista de notificaciones activas
|
||||||
|
JA_Sound_t *sound; // Sonido a reproducir cuando suena la notificación
|
||||||
|
|
||||||
|
// Elimina las notificaciones finalizadas
|
||||||
|
void clearFinishedNotifications();
|
||||||
|
|
||||||
|
public:
|
||||||
|
// Dibuja las notificaciones por pantalla
|
||||||
|
void render();
|
||||||
|
|
||||||
|
// Actualiza el estado de las notificaiones
|
||||||
|
void update();
|
||||||
|
|
||||||
|
// Constructor
|
||||||
|
Notify(SDL_Renderer *renderer, std::string iconFile, std::string bitmapFile, std::string textFile, std::string soundFile, options_t *options);
|
||||||
|
|
||||||
|
// Destructor
|
||||||
|
~Notify();
|
||||||
|
|
||||||
|
// Muestra una notificación de texto por pantalla;
|
||||||
|
void showText(std::string text1 = "", std::string text2 = "", int icon = -1);
|
||||||
|
|
||||||
|
// Indica si hay notificaciones activas
|
||||||
|
bool active();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
337
resource.cpp
Normal file
337
resource.cpp
Normal file
@@ -0,0 +1,337 @@
|
|||||||
|
#include "resource.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
// Constructor
|
||||||
|
Resource::Resource(SDL_Renderer *renderer, Asset *asset, options_t *options)
|
||||||
|
{
|
||||||
|
this->renderer = renderer;
|
||||||
|
this->asset = asset;
|
||||||
|
this->options = options;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Carga las texturas de una lista
|
||||||
|
void Resource::loadTextures(std::vector<std::string> list)
|
||||||
|
{
|
||||||
|
for (auto l : list)
|
||||||
|
{
|
||||||
|
if (options->console)
|
||||||
|
{
|
||||||
|
std::cout << "\nLOAD TEXTURE: " << l << std::endl;
|
||||||
|
std::cout << "png: " << asset->get(l) << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
res_texture_t t;
|
||||||
|
t.name = l;
|
||||||
|
t.texture = new Texture(renderer, asset->get(t.name), options->console);
|
||||||
|
textures.push_back(t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vuelve a cargar las texturas
|
||||||
|
void Resource::reLoadTextures()
|
||||||
|
{
|
||||||
|
for (auto texture : textures)
|
||||||
|
{
|
||||||
|
texture.texture->reLoad();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Carga las animaciones desde una lista
|
||||||
|
void Resource::loadAnimations(std::vector<std::string> list)
|
||||||
|
{
|
||||||
|
for (auto l : list)
|
||||||
|
{
|
||||||
|
// Extrae el nombre del fichero sin la extension para crear el nombre del fichero de la textura
|
||||||
|
const std::string pngFile = l.substr(0, l.find_last_of(".")) + ".png";
|
||||||
|
|
||||||
|
if (options->console)
|
||||||
|
{
|
||||||
|
std::cout << "\nLOAD ANIMATION: " << l << std::endl;
|
||||||
|
std::cout << "png: " << asset->get(pngFile) << std::endl;
|
||||||
|
std::cout << "ani: " << asset->get(l) << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
res_animation_t as;
|
||||||
|
as.name = l;
|
||||||
|
as.animation = new animatedSprite_t(loadAnimationFromFile(getTexture(pngFile), asset->get(as.name), options->console));
|
||||||
|
animations.push_back(as);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vuelve a cargar las animaciones
|
||||||
|
void Resource::reLoadAnimations()
|
||||||
|
{
|
||||||
|
// reLoadTextures();
|
||||||
|
|
||||||
|
for (auto &a : animations)
|
||||||
|
{
|
||||||
|
// Extrae el nombre del fichero sin la extension para crear el nombre del fichero de la textura
|
||||||
|
const std::string pngFile = a.name.substr(0, a.name.find_last_of(".")) + ".png";
|
||||||
|
delete a.animation;
|
||||||
|
a.animation = new animatedSprite_t(loadAnimationFromFile(getTexture(pngFile), asset->get(a.name), options->console));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Carga los offsets desde una lista
|
||||||
|
void Resource::loadOffsets(std::vector<std::string> list)
|
||||||
|
{
|
||||||
|
for (auto l : list)
|
||||||
|
{
|
||||||
|
res_textOffset_t to;
|
||||||
|
to.name = l;
|
||||||
|
to.textFile = new textFile_t(LoadTextFile(asset->get(l), options->console));
|
||||||
|
offsets.push_back(to);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vuelve a cargar los offsets
|
||||||
|
void Resource::reLoadOffsets()
|
||||||
|
{
|
||||||
|
for (auto &o : offsets)
|
||||||
|
{
|
||||||
|
delete o.textFile;
|
||||||
|
o.textFile = new textFile_t(LoadTextFile(asset->get(o.name), options->console));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Carga los mapas de tiles desde una lista
|
||||||
|
void Resource::loadTileMaps(std::vector<std::string> list)
|
||||||
|
{
|
||||||
|
for (auto l : list)
|
||||||
|
{
|
||||||
|
res_tileMap_t tm;
|
||||||
|
tm.name = l;
|
||||||
|
tm.tileMap = new std::vector<int>(loadRoomTileFile(asset->get(l), options->console));
|
||||||
|
tileMaps.push_back(tm);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vuelve a cargar los mapas de tiles
|
||||||
|
void Resource::reLoadTileMaps()
|
||||||
|
{
|
||||||
|
for (auto &tm : tileMaps)
|
||||||
|
{
|
||||||
|
delete tm.tileMap;
|
||||||
|
tm.tileMap = new std::vector<int>(loadRoomTileFile(asset->get(tm.name), options->console));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Carga las habitaciones desde una lista
|
||||||
|
void Resource::loadRooms(std::vector<std::string> list)
|
||||||
|
{
|
||||||
|
for (auto l : list)
|
||||||
|
{
|
||||||
|
res_room_t r;
|
||||||
|
r.name = l;
|
||||||
|
r.room = new room_t(loadRoomFile(asset->get(l), options->console));
|
||||||
|
r.room->tileMap = getTileMap(r.room->tileMapFile);
|
||||||
|
for (auto &e : r.room->enemies)
|
||||||
|
{
|
||||||
|
e.animation = getAnimation(e.animationString);
|
||||||
|
}
|
||||||
|
for (auto &i : r.room->items)
|
||||||
|
{
|
||||||
|
i.texture = getTexture(i.tileSetFile);
|
||||||
|
}
|
||||||
|
r.room->textureA = getTexture("standard.png");
|
||||||
|
r.room->textureB = getTexture("standard_zxarne.png");
|
||||||
|
rooms.push_back(r);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vuelve a cargar las habitaciones
|
||||||
|
void Resource::reLoadRooms()
|
||||||
|
{
|
||||||
|
reLoadTileMaps();
|
||||||
|
|
||||||
|
for (auto &r : rooms)
|
||||||
|
{
|
||||||
|
delete r.room;
|
||||||
|
r.room = new room_t(loadRoomFile(asset->get(r.name)));
|
||||||
|
r.room->tileMap = getTileMap(r.room->tileMapFile);
|
||||||
|
for (auto &e : r.room->enemies)
|
||||||
|
{
|
||||||
|
e.animation = getAnimation(e.animationString);
|
||||||
|
}
|
||||||
|
for (auto &i : r.room->items)
|
||||||
|
{
|
||||||
|
i.texture = getTexture(i.tileSetFile);
|
||||||
|
}
|
||||||
|
r.room->textureA = getTexture("standard.png");
|
||||||
|
r.room->textureB = getTexture("standard_zxarne.png");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vuelve a cargar todos los recursos
|
||||||
|
void Resource::reLoad()
|
||||||
|
{
|
||||||
|
reLoadAnimations();
|
||||||
|
reLoadOffsets();
|
||||||
|
reLoadRooms();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Libera las texturas
|
||||||
|
void Resource::freeTextures()
|
||||||
|
{
|
||||||
|
for (auto texture : textures)
|
||||||
|
{
|
||||||
|
delete texture.texture;
|
||||||
|
}
|
||||||
|
textures.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Libera las animaciones
|
||||||
|
void Resource::freeAnimations()
|
||||||
|
{
|
||||||
|
for (auto a : animations)
|
||||||
|
{
|
||||||
|
delete a.animation;
|
||||||
|
}
|
||||||
|
animations.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Libera los offsets
|
||||||
|
void Resource::freeOffsets()
|
||||||
|
{
|
||||||
|
for (auto o : offsets)
|
||||||
|
{
|
||||||
|
delete o.textFile;
|
||||||
|
}
|
||||||
|
offsets.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Libera los mapas de tiles
|
||||||
|
void Resource::freeTileMaps()
|
||||||
|
{
|
||||||
|
for (auto t : tileMaps)
|
||||||
|
{
|
||||||
|
delete t.tileMap;
|
||||||
|
}
|
||||||
|
tileMaps.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Libera las habitaciones
|
||||||
|
void Resource::freeRooms()
|
||||||
|
{
|
||||||
|
for (auto r : rooms)
|
||||||
|
{
|
||||||
|
delete r.room;
|
||||||
|
}
|
||||||
|
rooms.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Libera todos los recursos
|
||||||
|
void Resource::free()
|
||||||
|
{
|
||||||
|
freeTextures();
|
||||||
|
freeAnimations();
|
||||||
|
freeOffsets();
|
||||||
|
freeTileMaps();
|
||||||
|
freeRooms();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene una textura
|
||||||
|
Texture *Resource::getTexture(std::string name)
|
||||||
|
{
|
||||||
|
for (auto texture : textures)
|
||||||
|
{
|
||||||
|
// if (texture.name.find(name) != std::string::npos)
|
||||||
|
if (texture.name == name)
|
||||||
|
{
|
||||||
|
// std::cout << "\nTEXTURE REQUESTED: " << name << std::endl;
|
||||||
|
// std::cout << "served: " << texture.name << std::endl;
|
||||||
|
|
||||||
|
return texture.texture;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options->console)
|
||||||
|
{
|
||||||
|
std::cout << "NOT FOUND ON CACHE: " << name << std::endl;
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene una animación
|
||||||
|
animatedSprite_t *Resource::getAnimation(std::string name)
|
||||||
|
{
|
||||||
|
for (auto animation : animations)
|
||||||
|
{
|
||||||
|
// if (animation.name.find(name) != std::string::npos)
|
||||||
|
if (animation.name == name)
|
||||||
|
{
|
||||||
|
// std::cout << "\nANIMATION REQUESTED: " << name << std::endl;
|
||||||
|
// std::cout << "served: " << animation.name << std::endl;
|
||||||
|
return animation.animation;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options->console)
|
||||||
|
{
|
||||||
|
std::cout << "NOT FOUND ON CACHE: " << name << std::endl;
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene un offset
|
||||||
|
textFile_t *Resource::getOffset(std::string name)
|
||||||
|
{
|
||||||
|
for (auto offset : offsets)
|
||||||
|
{
|
||||||
|
// if (offset.name.find(name) != std::string::npos)
|
||||||
|
if (offset.name == name)
|
||||||
|
{
|
||||||
|
return offset.textFile;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options->console)
|
||||||
|
{
|
||||||
|
std::cout << "NOT FOUND ON CACHE: " << name << std::endl;
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene un mapa de tiles
|
||||||
|
std::vector<int> *Resource::getTileMap(std::string name)
|
||||||
|
{
|
||||||
|
for (auto tileMap : tileMaps)
|
||||||
|
{
|
||||||
|
// if (tileMap.name.find(name) != std::string::npos)
|
||||||
|
if (tileMap.name == name)
|
||||||
|
{
|
||||||
|
return tileMap.tileMap;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options->console)
|
||||||
|
{
|
||||||
|
std::cout << "NOT FOUND ON CACHE: " << name << std::endl;
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene una habitacion
|
||||||
|
room_t *Resource::getRoom(std::string name)
|
||||||
|
{
|
||||||
|
for (auto room : rooms)
|
||||||
|
{
|
||||||
|
// if (room.name.find(name) != std::string::npos)
|
||||||
|
if (room.name == name)
|
||||||
|
{
|
||||||
|
return room.room;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options->console)
|
||||||
|
{
|
||||||
|
std::cout << "NOT FOUND ON CACHE: " << name << std::endl;
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene todas las habitaciones
|
||||||
|
std::vector<res_room_t> *Resource::getAllRooms()
|
||||||
|
{
|
||||||
|
return &rooms;
|
||||||
|
}
|
||||||
136
resource.h
Normal file
136
resource.h
Normal file
@@ -0,0 +1,136 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include "animatedsprite.h"
|
||||||
|
#include "asset.h"
|
||||||
|
#include "../room.h"
|
||||||
|
#include "text.h"
|
||||||
|
#include "texture.h"
|
||||||
|
#include "utils.h"
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#ifndef RESOURCE_H
|
||||||
|
#define RESOURCE_H
|
||||||
|
|
||||||
|
struct res_texture_t
|
||||||
|
{
|
||||||
|
std::string name; // Nombre de la textura
|
||||||
|
Texture *texture; // La textura
|
||||||
|
};
|
||||||
|
|
||||||
|
struct res_animation_t
|
||||||
|
{
|
||||||
|
std::string name; // Nombre de la textura
|
||||||
|
animatedSprite_t *animation; // La animación
|
||||||
|
};
|
||||||
|
|
||||||
|
struct res_textOffset_t
|
||||||
|
{
|
||||||
|
std::string name; // Nombre del offset
|
||||||
|
textFile_t *textFile; // Los offsets de la fuente
|
||||||
|
};
|
||||||
|
|
||||||
|
struct res_tileMap_t
|
||||||
|
{
|
||||||
|
std::string name; // Nombre del mapa de tiles
|
||||||
|
std::vector<int> *tileMap; // Vector con los indices del mapa de tiles
|
||||||
|
};
|
||||||
|
|
||||||
|
struct res_room_t
|
||||||
|
{
|
||||||
|
std::string name; // Nombre de la habitación
|
||||||
|
room_t *room; // Vector con las habitaciones
|
||||||
|
};
|
||||||
|
|
||||||
|
// Clase Resource. Almacena recursos de disco en memoria
|
||||||
|
class Resource
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
// Objetos y punteros
|
||||||
|
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||||
|
Asset *asset; // Objeto con la ruta a todos los ficheros de recursos
|
||||||
|
options_t *options; // Puntero a las opciones del juego
|
||||||
|
|
||||||
|
// Variables
|
||||||
|
std::vector<res_texture_t> textures;
|
||||||
|
std::vector<res_animation_t> animations;
|
||||||
|
std::vector<res_textOffset_t> offsets;
|
||||||
|
std::vector<res_tileMap_t> tileMaps;
|
||||||
|
std::vector<res_room_t> rooms;
|
||||||
|
|
||||||
|
public:
|
||||||
|
// Constructor
|
||||||
|
Resource(SDL_Renderer *renderer, Asset *asset, options_t *options);
|
||||||
|
|
||||||
|
// Carga las texturas de una lista
|
||||||
|
void loadTextures(std::vector<std::string> list);
|
||||||
|
|
||||||
|
// Vuelve a cargar las texturas
|
||||||
|
void reLoadTextures();
|
||||||
|
|
||||||
|
// Carga las animaciones desde una lista
|
||||||
|
void loadAnimations(std::vector<std::string> list);
|
||||||
|
|
||||||
|
// Vuelve a cargar las animaciones
|
||||||
|
void reLoadAnimations();
|
||||||
|
|
||||||
|
// Carga los offsets desde una lista
|
||||||
|
void loadOffsets(std::vector<std::string> list);
|
||||||
|
|
||||||
|
// Vuelve a cargar los offsets
|
||||||
|
void reLoadOffsets();
|
||||||
|
|
||||||
|
// Carga los mapas de tiles desde una lista
|
||||||
|
void loadTileMaps(std::vector<std::string> list);
|
||||||
|
|
||||||
|
// Vuelve a cargar los mapas de tiles
|
||||||
|
void reLoadTileMaps();
|
||||||
|
|
||||||
|
// Carga las habitaciones desde una lista
|
||||||
|
void loadRooms(std::vector<std::string> list);
|
||||||
|
|
||||||
|
// Vuelve a cargar las habitaciones
|
||||||
|
void reLoadRooms();
|
||||||
|
|
||||||
|
// Vuelve a cargar todos los recursos
|
||||||
|
void reLoad();
|
||||||
|
|
||||||
|
// Libera las texturas
|
||||||
|
void freeTextures();
|
||||||
|
|
||||||
|
// Libera las animaciones
|
||||||
|
void freeAnimations();
|
||||||
|
|
||||||
|
// Libera los offsets
|
||||||
|
void freeOffsets();
|
||||||
|
|
||||||
|
// Libera los mapas de tiles
|
||||||
|
void freeTileMaps();
|
||||||
|
|
||||||
|
// Libera las habitaciones
|
||||||
|
void freeRooms();
|
||||||
|
|
||||||
|
// Libera todos los recursos
|
||||||
|
void free();
|
||||||
|
|
||||||
|
// Obtiene una textura
|
||||||
|
Texture *getTexture(std::string name);
|
||||||
|
|
||||||
|
// Obtiene una animación
|
||||||
|
animatedSprite_t *getAnimation(std::string name);
|
||||||
|
|
||||||
|
// Obtiene un offset
|
||||||
|
textFile_t *getOffset(std::string name);
|
||||||
|
|
||||||
|
// Obtiene un mapa de tiles
|
||||||
|
std::vector<int> *getTileMap(std::string name);
|
||||||
|
|
||||||
|
// Obtiene una habitacion
|
||||||
|
room_t *getRoom(std::string name);
|
||||||
|
|
||||||
|
// Obtiene todas las habitaciones
|
||||||
|
std::vector<res_room_t> *getAllRooms();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
436
screen.cpp
Normal file
436
screen.cpp
Normal file
@@ -0,0 +1,436 @@
|
|||||||
|
#include "screen.h"
|
||||||
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
// Constructor
|
||||||
|
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options_t *options)
|
||||||
|
{
|
||||||
|
// Inicializa variables
|
||||||
|
this->window = window;
|
||||||
|
this->renderer = renderer;
|
||||||
|
this->options = options;
|
||||||
|
this->asset = asset;
|
||||||
|
|
||||||
|
// Crea los objetos
|
||||||
|
notify = new Notify(renderer, asset->get("notify.png"), asset->get("smb2.png"), asset->get("smb2.txt"), asset->get("notify.wav"), options);
|
||||||
|
|
||||||
|
gameCanvasWidth = options->gameWidth;
|
||||||
|
gameCanvasHeight = options->gameHeight;
|
||||||
|
borderWidth = options->borderWidth * 2;
|
||||||
|
borderHeight = options->borderHeight * 2;
|
||||||
|
notificationLogicalWidth = gameCanvasWidth;
|
||||||
|
notificationLogicalHeight = gameCanvasHeight;
|
||||||
|
|
||||||
|
iniFade();
|
||||||
|
iniSpectrumFade();
|
||||||
|
|
||||||
|
// Define el color del borde para el modo de pantalla completa
|
||||||
|
borderColor = {0x00, 0x00, 0x00};
|
||||||
|
|
||||||
|
// Crea la textura donde se dibujan los graficos del juego
|
||||||
|
gameCanvas = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, gameCanvasWidth, gameCanvasHeight);
|
||||||
|
if (gameCanvas == nullptr)
|
||||||
|
{
|
||||||
|
if (options->console)
|
||||||
|
{
|
||||||
|
std::cout << "TitleSurface could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el modo de video
|
||||||
|
setVideoMode(options->videoMode);
|
||||||
|
|
||||||
|
// Inicializa variables
|
||||||
|
notifyActive = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Destructor
|
||||||
|
Screen::~Screen()
|
||||||
|
{
|
||||||
|
delete notify;
|
||||||
|
SDL_DestroyTexture(gameCanvas);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Limpia la pantalla
|
||||||
|
void Screen::clean(color_t color)
|
||||||
|
{
|
||||||
|
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 0xFF);
|
||||||
|
SDL_RenderClear(renderer);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepara para empezar a dibujar en la textura de juego
|
||||||
|
void Screen::start()
|
||||||
|
{
|
||||||
|
SDL_SetRenderTarget(renderer, gameCanvas);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vuelca el contenido del renderizador en pantalla
|
||||||
|
void Screen::blit()
|
||||||
|
{
|
||||||
|
// Vuelve a dejar el renderizador en modo normal
|
||||||
|
SDL_SetRenderTarget(renderer, nullptr);
|
||||||
|
|
||||||
|
// Borra el contenido previo
|
||||||
|
SDL_SetRenderDrawColor(renderer, borderColor.r, borderColor.g, borderColor.b, 0xFF);
|
||||||
|
SDL_RenderClear(renderer);
|
||||||
|
|
||||||
|
// Copia la textura de juego en el renderizador en la posición adecuada
|
||||||
|
SDL_RenderCopy(renderer, gameCanvas, nullptr, &dest);
|
||||||
|
|
||||||
|
// Dibuja las notificaciones
|
||||||
|
renderNotifications();
|
||||||
|
|
||||||
|
// Muestra por pantalla el renderizador
|
||||||
|
SDL_RenderPresent(renderer);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el modo de video
|
||||||
|
void Screen::setVideoMode(int videoMode)
|
||||||
|
{
|
||||||
|
// Aplica el modo de video
|
||||||
|
SDL_SetWindowFullscreen(window, videoMode);
|
||||||
|
|
||||||
|
// Si está activo el modo ventana quita el borde
|
||||||
|
if (videoMode == 0)
|
||||||
|
{
|
||||||
|
// Muestra el puntero
|
||||||
|
SDL_ShowCursor(SDL_ENABLE);
|
||||||
|
|
||||||
|
// Esconde la ventana
|
||||||
|
//SDL_HideWindow(window);
|
||||||
|
|
||||||
|
if (options->borderEnabled)
|
||||||
|
{
|
||||||
|
windowWidth = gameCanvasWidth + borderWidth;
|
||||||
|
windowHeight = gameCanvasHeight + borderHeight;
|
||||||
|
dest = {0 + (borderWidth / 2), 0 + (borderHeight / 2), gameCanvasWidth, gameCanvasHeight};
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
windowWidth = gameCanvasWidth;
|
||||||
|
windowHeight = gameCanvasHeight;
|
||||||
|
dest = {0, 0, gameCanvasWidth, gameCanvasHeight};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Modifica el tamaño de la ventana
|
||||||
|
SDL_SetWindowSize(window, windowWidth * options->windowSize, windowHeight * options->windowSize);
|
||||||
|
SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
|
||||||
|
|
||||||
|
// Muestra la ventana
|
||||||
|
//SDL_ShowWindow(window);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Si está activo el modo de pantalla completa añade el borde
|
||||||
|
else if (videoMode == SDL_WINDOW_FULLSCREEN_DESKTOP)
|
||||||
|
{
|
||||||
|
// Oculta el puntero
|
||||||
|
SDL_ShowCursor(SDL_DISABLE);
|
||||||
|
|
||||||
|
// Obten el alto y el ancho de la ventana
|
||||||
|
SDL_GetWindowSize(window, &windowWidth, &windowHeight);
|
||||||
|
|
||||||
|
// Aplica el escalado al rectangulo donde se pinta la textura del juego
|
||||||
|
if (options->integerScale)
|
||||||
|
{
|
||||||
|
// Calcula el tamaño de la escala máxima
|
||||||
|
int scale = 0;
|
||||||
|
while (((gameCanvasWidth * (scale + 1)) <= windowWidth) && ((gameCanvasHeight * (scale + 1)) <= windowHeight))
|
||||||
|
{
|
||||||
|
scale++;
|
||||||
|
}
|
||||||
|
|
||||||
|
dest.w = gameCanvasWidth * scale;
|
||||||
|
dest.h = gameCanvasHeight * scale;
|
||||||
|
dest.x = (windowWidth - dest.w) / 2;
|
||||||
|
dest.y = (windowHeight - dest.h) / 2;
|
||||||
|
}
|
||||||
|
else if (options->keepAspect)
|
||||||
|
{
|
||||||
|
float ratio = (float)gameCanvasWidth / (float)gameCanvasHeight;
|
||||||
|
if ((windowWidth - gameCanvasWidth) >= (windowHeight - gameCanvasHeight))
|
||||||
|
{
|
||||||
|
dest.h = windowHeight;
|
||||||
|
dest.w = (int)((windowHeight * ratio) + 0.5f);
|
||||||
|
dest.x = (windowWidth - dest.w) / 2;
|
||||||
|
dest.y = (windowHeight - dest.h) / 2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
dest.w = windowWidth;
|
||||||
|
dest.h = (int)((windowWidth / ratio) + 0.5f);
|
||||||
|
dest.x = (windowWidth - dest.w) / 2;
|
||||||
|
dest.y = (windowHeight - dest.h) / 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
dest.w = windowWidth;
|
||||||
|
dest.h = windowHeight;
|
||||||
|
dest.x = dest.y = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Modifica el tamaño del renderizador
|
||||||
|
SDL_RenderSetLogicalSize(renderer, windowWidth, windowHeight);
|
||||||
|
|
||||||
|
// Actualiza las opciones
|
||||||
|
options->videoMode = videoMode;
|
||||||
|
options->screen.windowWidth = windowWidth;
|
||||||
|
options->screen.windowHeight = windowHeight;
|
||||||
|
|
||||||
|
// Establece el tamaño de las notificaciones
|
||||||
|
setNotificationSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Camibia entre pantalla completa y ventana
|
||||||
|
void Screen::switchVideoMode()
|
||||||
|
{
|
||||||
|
options->videoMode = (options->videoMode == 0) ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0;
|
||||||
|
setVideoMode(options->videoMode);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cambia el tamaño de la ventana
|
||||||
|
void Screen::setWindowSize(int size)
|
||||||
|
{
|
||||||
|
options->windowSize = size;
|
||||||
|
setVideoMode(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reduce el tamaño de la ventana
|
||||||
|
void Screen::decWindowSize()
|
||||||
|
{
|
||||||
|
--options->windowSize;
|
||||||
|
options->windowSize = std::max(options->windowSize, 1);
|
||||||
|
setVideoMode(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Aumenta el tamaño de la ventana
|
||||||
|
void Screen::incWindowSize()
|
||||||
|
{
|
||||||
|
++options->windowSize;
|
||||||
|
options->windowSize = std::min(options->windowSize, 4);
|
||||||
|
setVideoMode(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cambia el color del borde
|
||||||
|
void Screen::setBorderColor(color_t color)
|
||||||
|
{
|
||||||
|
borderColor = color;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cambia el tipo de mezcla
|
||||||
|
void Screen::setBlendMode(SDL_BlendMode blendMode)
|
||||||
|
{
|
||||||
|
SDL_SetRenderDrawBlendMode(renderer, blendMode);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el tamaño del borde
|
||||||
|
void Screen::setBorderWidth(int s)
|
||||||
|
{
|
||||||
|
options->borderWidth = s;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el tamaño del borde
|
||||||
|
void Screen::setBorderHeight(int s)
|
||||||
|
{
|
||||||
|
options->borderHeight = s;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece si se ha de ver el borde en el modo ventana
|
||||||
|
void Screen::setBorderEnabled(bool value)
|
||||||
|
{
|
||||||
|
options->borderEnabled = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cambia entre borde visible y no visible
|
||||||
|
void Screen::switchBorder()
|
||||||
|
{
|
||||||
|
options->borderEnabled = !options->borderEnabled;
|
||||||
|
setVideoMode(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Activa el fade
|
||||||
|
void Screen::setFade()
|
||||||
|
{
|
||||||
|
fade = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba si ha terminado el fade
|
||||||
|
bool Screen::fadeEnded()
|
||||||
|
{
|
||||||
|
if (fade || fadeCounter > 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Activa el spectrum fade
|
||||||
|
void Screen::setspectrumFade()
|
||||||
|
{
|
||||||
|
spectrumFade = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba si ha terminado el spectrum fade
|
||||||
|
bool Screen::spectrumFadeEnded()
|
||||||
|
{
|
||||||
|
if (spectrumFade || spectrumFadeCounter > 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Inicializa las variables para el fade
|
||||||
|
void Screen::iniFade()
|
||||||
|
{
|
||||||
|
fade = false;
|
||||||
|
fadeCounter = 0;
|
||||||
|
fadeLenght = 200;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actualiza el fade
|
||||||
|
void Screen::updateFade()
|
||||||
|
{
|
||||||
|
if (!fade)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fadeCounter++;
|
||||||
|
if (fadeCounter > fadeLenght)
|
||||||
|
{
|
||||||
|
iniFade();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dibuja el fade
|
||||||
|
void Screen::renderFade()
|
||||||
|
{
|
||||||
|
if (!fade)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const SDL_Rect rect = {0, 0, gameCanvasWidth, gameCanvasHeight};
|
||||||
|
color_t color = {0, 0, 0};
|
||||||
|
const float step = (float)fadeCounter / (float)fadeLenght;
|
||||||
|
const int alpha = 0 + (255 - 0) * step;
|
||||||
|
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, alpha);
|
||||||
|
SDL_RenderFillRect(renderer, &rect);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Inicializa las variables para el fade spectrum
|
||||||
|
void Screen::iniSpectrumFade()
|
||||||
|
{
|
||||||
|
spectrumFade = false;
|
||||||
|
spectrumFadeCounter = 0;
|
||||||
|
spectrumFadeLenght = 50;
|
||||||
|
|
||||||
|
spectrumColor.clear();
|
||||||
|
|
||||||
|
// Inicializa el vector de colores
|
||||||
|
const std::vector<std::string> vColors = {"black", "blue", "red", "magenta", "green", "cyan", "yellow", "bright_white"};
|
||||||
|
for (auto v : vColors)
|
||||||
|
{
|
||||||
|
spectrumColor.push_back(stringToColor(options->palette, v));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actualiza el spectrum fade
|
||||||
|
void Screen::updateSpectrumFade()
|
||||||
|
{
|
||||||
|
if (!spectrumFade)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
spectrumFadeCounter++;
|
||||||
|
if (spectrumFadeCounter > spectrumFadeLenght)
|
||||||
|
{
|
||||||
|
iniSpectrumFade();
|
||||||
|
SDL_SetTextureColorMod(gameCanvas, 255, 255, 255);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dibuja el spectrum fade
|
||||||
|
void Screen::renderSpectrumFade()
|
||||||
|
{
|
||||||
|
if (!spectrumFade)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const float step = (float)spectrumFadeCounter / (float)spectrumFadeLenght;
|
||||||
|
const int max = spectrumColor.size() - 1;
|
||||||
|
const int index = max + (0 - max) * step;
|
||||||
|
const color_t c = spectrumColor[index];
|
||||||
|
SDL_SetTextureColorMod(gameCanvas, c.r, c.g, c.b);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actualiza los efectos
|
||||||
|
void Screen::updateFX()
|
||||||
|
{
|
||||||
|
updateFade();
|
||||||
|
updateSpectrumFade();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dibuja los efectos
|
||||||
|
void Screen::renderFX()
|
||||||
|
{
|
||||||
|
renderFade();
|
||||||
|
renderSpectrumFade();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actualiza el notificador
|
||||||
|
void Screen::updateNotifier()
|
||||||
|
{
|
||||||
|
notify->update();
|
||||||
|
notifyActive = notify->active();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Muestra una notificación de texto por pantalla;
|
||||||
|
void Screen::showNotification(std::string text1, std::string text2, int icon)
|
||||||
|
{
|
||||||
|
notify->showText(text1, text2, icon);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dibuja las notificaciones
|
||||||
|
void Screen::renderNotifications()
|
||||||
|
{
|
||||||
|
if (!notifyActive)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_RenderSetLogicalSize(renderer, notificationLogicalWidth, notificationLogicalHeight);
|
||||||
|
notify->render();
|
||||||
|
SDL_RenderSetLogicalSize(renderer, windowWidth, windowHeight);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el tamaño de las notificaciones
|
||||||
|
void Screen::setNotificationSize()
|
||||||
|
{
|
||||||
|
if (options->videoMode == 0)
|
||||||
|
{
|
||||||
|
if (options->windowSize == 3)
|
||||||
|
{
|
||||||
|
notificationLogicalWidth = (windowWidth * 3) / 2;
|
||||||
|
notificationLogicalHeight = (windowHeight * 3) / 2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
notificationLogicalWidth = windowWidth * 2;
|
||||||
|
notificationLogicalHeight = windowHeight * 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options->videoMode == SDL_WINDOW_FULLSCREEN_DESKTOP)
|
||||||
|
{
|
||||||
|
notificationLogicalWidth = windowWidth / 3;
|
||||||
|
notificationLogicalHeight = windowHeight / 3;
|
||||||
|
}
|
||||||
|
}
|
||||||
145
screen.h
Normal file
145
screen.h
Normal file
@@ -0,0 +1,145 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include "asset.h"
|
||||||
|
#include "notify.h"
|
||||||
|
#include "utils.h"
|
||||||
|
#include "../const.h"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#ifndef SCREEN_H
|
||||||
|
#define SCREEN_H
|
||||||
|
|
||||||
|
#define FILTER_NEAREST 0
|
||||||
|
#define FILTER_LINEAL 1
|
||||||
|
|
||||||
|
class Screen
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
// Objetos y punteros
|
||||||
|
SDL_Window *window; // Ventana de la aplicación
|
||||||
|
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||||
|
Asset *asset; // Objeto con el listado de recursos
|
||||||
|
SDL_Texture *gameCanvas; // Textura para completar la ventana de juego hasta la pantalla completa
|
||||||
|
options_t *options; // Variable con todas las opciones del programa
|
||||||
|
Notify *notify; // Dibuja notificaciones por pantalla
|
||||||
|
|
||||||
|
// Variables
|
||||||
|
int windowWidth; // Ancho de la pantalla o ventana
|
||||||
|
int windowHeight; // Alto de la pantalla o ventana
|
||||||
|
int gameCanvasWidth; // Resolución interna del juego. Es el ancho de la textura donde se dibuja el juego
|
||||||
|
int gameCanvasHeight; // Resolución interna del juego. Es el alto de la textura donde se dibuja el juego
|
||||||
|
int borderWidth; // Anchura del borde
|
||||||
|
int borderHeight; // Anltura del borde
|
||||||
|
SDL_Rect dest; // Coordenadas donde se va a dibujar la textura del juego sobre la pantalla o ventana
|
||||||
|
color_t borderColor; // Color del borde añadido a la textura de juego para rellenar la pantalla
|
||||||
|
bool notifyActive; // Indica si hay notificaciones activas
|
||||||
|
int notificationLogicalWidth; // Ancho lógico de las notificaciones en relación al tamaño de pantalla
|
||||||
|
int notificationLogicalHeight; // Alto lógico de las notificaciones en relación al tamaño de pantalla
|
||||||
|
|
||||||
|
// Variables - Efectos
|
||||||
|
bool fade; // Indica si esta activo el efecto de fade
|
||||||
|
int fadeCounter; // Temporizador para el efecto de fade
|
||||||
|
int fadeLenght; // Duración del fade
|
||||||
|
bool spectrumFade; // Indica si esta activo el efecto de fade spectrum
|
||||||
|
int spectrumFadeCounter; // Temporizador para el efecto de fade spectrum
|
||||||
|
int spectrumFadeLenght; // Duración del fade spectrum
|
||||||
|
std::vector<color_t> spectrumColor; // Colores para el fade spectrum
|
||||||
|
|
||||||
|
// Inicializa las variables para el fade
|
||||||
|
void iniFade();
|
||||||
|
|
||||||
|
// Actualiza el fade
|
||||||
|
void updateFade();
|
||||||
|
|
||||||
|
// Dibuja el fade
|
||||||
|
void renderFade();
|
||||||
|
|
||||||
|
// Inicializa las variables para el fade spectrum
|
||||||
|
void iniSpectrumFade();
|
||||||
|
|
||||||
|
// Actualiza el spectrum fade
|
||||||
|
void updateSpectrumFade();
|
||||||
|
|
||||||
|
// Dibuja el spectrum fade
|
||||||
|
void renderSpectrumFade();
|
||||||
|
|
||||||
|
// Dibuja las notificaciones
|
||||||
|
void renderNotifications();
|
||||||
|
|
||||||
|
// Establece el tamaño de las notificaciones
|
||||||
|
void setNotificationSize();
|
||||||
|
|
||||||
|
public:
|
||||||
|
// Constructor
|
||||||
|
Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options_t *options);
|
||||||
|
|
||||||
|
// Destructor
|
||||||
|
~Screen();
|
||||||
|
|
||||||
|
// Limpia la pantalla
|
||||||
|
void clean(color_t color = {0x00, 0x00, 0x00});
|
||||||
|
|
||||||
|
// Prepara para empezar a dibujar en la textura de juego
|
||||||
|
void start();
|
||||||
|
|
||||||
|
// Vuelca el contenido del renderizador en pantalla
|
||||||
|
void blit();
|
||||||
|
|
||||||
|
// Establece el modo de video
|
||||||
|
void setVideoMode(int videoMode);
|
||||||
|
|
||||||
|
// Camibia entre pantalla completa y ventana
|
||||||
|
void switchVideoMode();
|
||||||
|
|
||||||
|
// Cambia el tamaño de la ventana
|
||||||
|
void setWindowSize(int size);
|
||||||
|
|
||||||
|
// Reduce el tamaño de la ventana
|
||||||
|
void decWindowSize();
|
||||||
|
|
||||||
|
// Aumenta el tamaño de la ventana
|
||||||
|
void incWindowSize();
|
||||||
|
|
||||||
|
// Cambia el color del borde
|
||||||
|
void setBorderColor(color_t color);
|
||||||
|
|
||||||
|
// Cambia el tipo de mezcla
|
||||||
|
void setBlendMode(SDL_BlendMode blendMode);
|
||||||
|
|
||||||
|
// Establece el tamaño del borde
|
||||||
|
void setBorderWidth(int s);
|
||||||
|
void setBorderHeight(int s);
|
||||||
|
|
||||||
|
// Establece si se ha de ver el borde en el modo ventana
|
||||||
|
void setBorderEnabled(bool value);
|
||||||
|
|
||||||
|
// Cambia entre borde visible y no visible
|
||||||
|
void switchBorder();
|
||||||
|
|
||||||
|
// Activa el fade
|
||||||
|
void setFade();
|
||||||
|
|
||||||
|
// Comprueba si ha terminado el fade
|
||||||
|
bool fadeEnded();
|
||||||
|
|
||||||
|
// Activa el spectrum fade
|
||||||
|
void setspectrumFade();
|
||||||
|
|
||||||
|
// Comprueba si ha terminado el spectrum fade
|
||||||
|
bool spectrumFadeEnded();
|
||||||
|
|
||||||
|
// Actualiza los efectos
|
||||||
|
void updateFX();
|
||||||
|
|
||||||
|
// Dibuja los efectos
|
||||||
|
void renderFX();
|
||||||
|
|
||||||
|
// Actualiza el notificador
|
||||||
|
void updateNotifier();
|
||||||
|
|
||||||
|
// Muestra una notificación de texto por pantalla;
|
||||||
|
void showNotification(std::string text1 = "", std::string text2 = "", int icon = -1);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
201
sprite.cpp
Normal file
201
sprite.cpp
Normal file
@@ -0,0 +1,201 @@
|
|||||||
|
#include "sprite.h"
|
||||||
|
|
||||||
|
// 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(SDL_Rect rect, Texture *texture, SDL_Renderer *renderer)
|
||||||
|
{
|
||||||
|
// Establece la posición X,Y del sprite
|
||||||
|
this->x = rect.x;
|
||||||
|
this->y = rect.y;
|
||||||
|
|
||||||
|
// Establece el alto y el ancho del sprite
|
||||||
|
this->w = rect.w;
|
||||||
|
this->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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Muestra el sprite por pantalla
|
||||||
|
void Sprite::render()
|
||||||
|
{
|
||||||
|
if (enabled)
|
||||||
|
{
|
||||||
|
texture->render(renderer, x, y, &spriteClip);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obten el valor de la variable
|
||||||
|
int Sprite::getPosX()
|
||||||
|
{
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obten el valor de la variable
|
||||||
|
int Sprite::getPosY()
|
||||||
|
{
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obten el valor de la variable
|
||||||
|
int Sprite::getWidth()
|
||||||
|
{
|
||||||
|
return w;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obten el valor de la variable
|
||||||
|
int Sprite::getHeight()
|
||||||
|
{
|
||||||
|
return h;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece la posición del objeto
|
||||||
|
void Sprite::setPos(SDL_Rect rect)
|
||||||
|
{
|
||||||
|
this->x = rect.x;
|
||||||
|
this->y = rect.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void Sprite::setPosX(int x)
|
||||||
|
{
|
||||||
|
this->x = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void Sprite::setPosY(int y)
|
||||||
|
{
|
||||||
|
this->y = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Incrementa el valor de la variable
|
||||||
|
void Sprite::incPosX(int value)
|
||||||
|
{
|
||||||
|
x += value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Incrementa el valor de la variable
|
||||||
|
void Sprite::incPosY(int value)
|
||||||
|
{
|
||||||
|
y += value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void Sprite::setWidth(int w)
|
||||||
|
{
|
||||||
|
this->w = w;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void Sprite::setHeight(int h)
|
||||||
|
{
|
||||||
|
this->h = h;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obten el valor de la variable
|
||||||
|
SDL_Rect Sprite::getSpriteClip()
|
||||||
|
{
|
||||||
|
return spriteClip;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void Sprite::setSpriteClip(SDL_Rect rect)
|
||||||
|
{
|
||||||
|
spriteClip = rect;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void Sprite::setSpriteClip(int x, int y, int w, int h)
|
||||||
|
{
|
||||||
|
if (w == -1 || h == -1)
|
||||||
|
{
|
||||||
|
spriteClip = {x, y, this->w, this->h};
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
spriteClip = {x, y, w, h};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obten el valor de la variable
|
||||||
|
Texture *Sprite::getTexture()
|
||||||
|
{
|
||||||
|
return texture;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void Sprite::setTexture(Texture *texture)
|
||||||
|
{
|
||||||
|
this->texture = texture;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void Sprite::setRenderer(SDL_Renderer *renderer)
|
||||||
|
{
|
||||||
|
this->renderer = renderer;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obten el valor de la variable
|
||||||
|
SDL_Renderer *Sprite::getRenderer()
|
||||||
|
{
|
||||||
|
return renderer;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void Sprite::setEnabled(bool value)
|
||||||
|
{
|
||||||
|
enabled = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba si el objeto está habilitado
|
||||||
|
bool Sprite::isEnabled()
|
||||||
|
{
|
||||||
|
return enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Devuelve el rectangulo donde está el sprite
|
||||||
|
SDL_Rect Sprite::getRect()
|
||||||
|
{
|
||||||
|
SDL_Rect rect = {x, y, w, h};
|
||||||
|
return rect;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece los valores de posición y tamaño del sprite
|
||||||
|
void Sprite::setRect(SDL_Rect rect)
|
||||||
|
{
|
||||||
|
x = rect.x;
|
||||||
|
y = rect.y;
|
||||||
|
w = rect.w;
|
||||||
|
h = rect.h;
|
||||||
|
}
|
||||||
100
sprite.h
Normal file
100
sprite.h
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include "texture.h"
|
||||||
|
|
||||||
|
#ifndef SPRITE_H
|
||||||
|
#define SPRITE_H
|
||||||
|
|
||||||
|
// Clase sprite
|
||||||
|
class Sprite
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
// Objetos y punteros
|
||||||
|
SDL_Renderer *renderer; // Puntero al renderizador de la ventana
|
||||||
|
Texture *texture; // Textura donde estan todos los dibujos del sprite
|
||||||
|
|
||||||
|
// Variables
|
||||||
|
int x; // Posición en el eje X donde dibujar el sprite
|
||||||
|
int y; // Posición en el eje Y donde dibujar el sprite
|
||||||
|
int w; // Ancho del sprite
|
||||||
|
int h; // Alto del sprite
|
||||||
|
SDL_Rect spriteClip; // Rectangulo de origen de la textura que se dibujará en pantalla
|
||||||
|
bool enabled; // Indica si el sprite esta habilitado
|
||||||
|
|
||||||
|
public:
|
||||||
|
// Constructor
|
||||||
|
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);
|
||||||
|
|
||||||
|
// Muestra el sprite por pantalla
|
||||||
|
void render();
|
||||||
|
|
||||||
|
// Obten el valor de la variable
|
||||||
|
int getPosX();
|
||||||
|
|
||||||
|
// Obten el valor de la variable
|
||||||
|
int getPosY();
|
||||||
|
|
||||||
|
// Obten el valor de la variable
|
||||||
|
int getWidth();
|
||||||
|
|
||||||
|
// Obten el valor de la variable
|
||||||
|
int getHeight();
|
||||||
|
|
||||||
|
// Establece la posición del objeto
|
||||||
|
void setPos(SDL_Rect rect);
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void setPosX(int x);
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void setPosY(int y);
|
||||||
|
|
||||||
|
// Incrementa el valor de la variable
|
||||||
|
void incPosX(int value);
|
||||||
|
|
||||||
|
// Incrementa el valor de la variable
|
||||||
|
void incPosY(int value);
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void setWidth(int w);
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void setHeight(int h);
|
||||||
|
|
||||||
|
// Obten el valor de la variable
|
||||||
|
SDL_Rect getSpriteClip();
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void setSpriteClip(SDL_Rect rect);
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void setSpriteClip(int x, int y, int w, int h);
|
||||||
|
|
||||||
|
// Obten el valor de la variable
|
||||||
|
Texture *getTexture();
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void setTexture(Texture *texture);
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void setRenderer(SDL_Renderer *renderer);
|
||||||
|
|
||||||
|
// Obten el valor de la variable
|
||||||
|
SDL_Renderer *getRenderer();
|
||||||
|
|
||||||
|
// Establece el valor de la variable
|
||||||
|
void setEnabled(bool value);
|
||||||
|
|
||||||
|
// Comprueba si el objeto está habilitado
|
||||||
|
bool isEnabled();
|
||||||
|
|
||||||
|
// Devuelve el rectangulo donde está el sprite
|
||||||
|
SDL_Rect getRect();
|
||||||
|
|
||||||
|
// Establece los valores de posición y tamaño del sprite
|
||||||
|
void setRect(SDL_Rect rect);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
7897
stb_image.h
Normal file
7897
stb_image.h
Normal file
File diff suppressed because it is too large
Load Diff
5565
stb_vorbis.c
Normal file
5565
stb_vorbis.c
Normal file
File diff suppressed because it is too large
Load Diff
241
text.cpp
Normal file
241
text.cpp
Normal file
@@ -0,0 +1,241 @@
|
|||||||
|
|
||||||
|
#include "text.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
// Llena una estructuta textFile_t desde un fichero
|
||||||
|
textFile_t LoadTextFile(std::string file, bool verbose)
|
||||||
|
{
|
||||||
|
textFile_t tf;
|
||||||
|
|
||||||
|
// Inicializa a cero el vector con las coordenadas
|
||||||
|
for (int i = 0; i < 128; ++i)
|
||||||
|
{
|
||||||
|
tf.offset[i].x = 0;
|
||||||
|
tf.offset[i].y = 0;
|
||||||
|
tf.offset[i].w = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Abre el fichero para leer los valores
|
||||||
|
const std::string filename = file.substr(file.find_last_of("\\/") + 1).c_str();
|
||||||
|
std::ifstream rfile(file);
|
||||||
|
|
||||||
|
if (rfile.is_open() && rfile.good())
|
||||||
|
{
|
||||||
|
std::string buffer;
|
||||||
|
|
||||||
|
// Lee los dos primeros valores del fichero
|
||||||
|
std::getline(rfile, buffer);
|
||||||
|
std::getline(rfile, buffer);
|
||||||
|
tf.boxWidth = std::stoi(buffer);
|
||||||
|
|
||||||
|
std::getline(rfile, buffer);
|
||||||
|
std::getline(rfile, buffer);
|
||||||
|
tf.boxHeight = std::stoi(buffer);
|
||||||
|
|
||||||
|
// lee el resto de datos del fichero
|
||||||
|
int index = 32;
|
||||||
|
int line_read = 0;
|
||||||
|
while (std::getline(rfile, buffer))
|
||||||
|
{
|
||||||
|
// Almacena solo las lineas impares
|
||||||
|
if (line_read % 2 == 1)
|
||||||
|
{
|
||||||
|
tf.offset[index++].w = std::stoi(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Limpia el buffer
|
||||||
|
buffer.clear();
|
||||||
|
line_read++;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Cierra el fichero
|
||||||
|
if (verbose)
|
||||||
|
{
|
||||||
|
std::cout << "Text loaded: " << filename.c_str() << std::endl;
|
||||||
|
}
|
||||||
|
rfile.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
// El fichero no se puede abrir
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (verbose)
|
||||||
|
{
|
||||||
|
std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece las coordenadas para cada caracter ascii de la cadena y su ancho
|
||||||
|
for (int i = 32; i < 128; ++i)
|
||||||
|
{
|
||||||
|
tf.offset[i].x = ((i - 32) % 15) * tf.boxWidth;
|
||||||
|
tf.offset[i].y = ((i - 32) / 15) * tf.boxHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
return tf;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Constructor
|
||||||
|
Text::Text(std::string textFile, Texture *texture, SDL_Renderer *renderer)
|
||||||
|
{
|
||||||
|
// Carga los offsets desde el fichero
|
||||||
|
textFile_t tf = LoadTextFile(textFile);
|
||||||
|
|
||||||
|
// Inicializa variables desde la estructura
|
||||||
|
boxHeight = tf.boxHeight;
|
||||||
|
boxWidth = tf.boxWidth;
|
||||||
|
for (int i = 0; i < 128; ++i)
|
||||||
|
{
|
||||||
|
offset[i].x = tf.offset[i].x;
|
||||||
|
offset[i].y = tf.offset[i].y;
|
||||||
|
offset[i].w = tf.offset[i].w;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Crea los objetos
|
||||||
|
sprite = new Sprite({0, 0, boxWidth, boxHeight}, texture, renderer);
|
||||||
|
|
||||||
|
// Inicializa variables
|
||||||
|
fixedWidth = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Constructor
|
||||||
|
Text::Text(textFile_t *textFile, Texture *texture, SDL_Renderer *renderer)
|
||||||
|
{
|
||||||
|
// Inicializa variables desde la estructura
|
||||||
|
boxHeight = textFile->boxHeight;
|
||||||
|
boxWidth = textFile->boxWidth;
|
||||||
|
for (int i = 0; i < 128; ++i)
|
||||||
|
{
|
||||||
|
offset[i].x = textFile->offset[i].x;
|
||||||
|
offset[i].y = textFile->offset[i].y;
|
||||||
|
offset[i].w = textFile->offset[i].w;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Crea los objetos
|
||||||
|
sprite = new Sprite({0, 0, boxWidth, boxHeight}, texture, renderer);
|
||||||
|
|
||||||
|
// Inicializa variables
|
||||||
|
fixedWidth = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Destructor
|
||||||
|
Text::~Text()
|
||||||
|
{
|
||||||
|
delete sprite;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Escribe texto en pantalla
|
||||||
|
void Text::write(int x, int y, std::string text, int kerning, int lenght)
|
||||||
|
{
|
||||||
|
int shift = 0;
|
||||||
|
|
||||||
|
if (lenght == -1)
|
||||||
|
lenght = text.length();
|
||||||
|
|
||||||
|
for (int i = 0; i < lenght; ++i)
|
||||||
|
{
|
||||||
|
sprite->setSpriteClip(offset[int(text[i])].x, offset[int(text[i])].y, sprite->getWidth(), sprite->getHeight());
|
||||||
|
sprite->setPosX(x + shift);
|
||||||
|
sprite->setPosY(y);
|
||||||
|
sprite->render();
|
||||||
|
// shift += (offset[int(text[i])].w + kerning);
|
||||||
|
shift += fixedWidth ? boxWidth : (offset[int(text[i])].w + kerning);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Escribe el texto con colores
|
||||||
|
void Text::writeColored(int x, int y, 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)
|
||||||
|
{
|
||||||
|
sprite->getTexture()->setColor(color.r, color.g, color.b);
|
||||||
|
write(x + shadowDistance, y + shadowDistance, text, kerning, lenght);
|
||||||
|
sprite->getTexture()->setColor(255, 255, 255);
|
||||||
|
write(x, y, text, kerning, lenght);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Escribe el texto centrado en un punto x
|
||||||
|
void Text::writeCentered(int x, int y, 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)
|
||||||
|
{
|
||||||
|
const bool centered = ((flags & TXT_CENTER) == TXT_CENTER);
|
||||||
|
const bool shadowed = ((flags & TXT_SHADOW) == TXT_SHADOW);
|
||||||
|
const bool colored = ((flags & TXT_COLOR) == TXT_COLOR);
|
||||||
|
const bool stroked = ((flags & TXT_STROKE) == TXT_STROKE);
|
||||||
|
|
||||||
|
if (centered)
|
||||||
|
{
|
||||||
|
x -= (Text::lenght(text, kerning) / 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (shadowed)
|
||||||
|
{
|
||||||
|
writeColored(x + shadowDistance, y + shadowDistance, text, shadowColor, kerning, lenght);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stroked)
|
||||||
|
{
|
||||||
|
for (int dist = 1; dist <= shadowDistance; ++dist)
|
||||||
|
{
|
||||||
|
for (int dy = -dist; dy <= dist; ++dy)
|
||||||
|
{
|
||||||
|
for (int dx = -dist; dx <= dist; ++dx)
|
||||||
|
{
|
||||||
|
writeColored(x + dx, y + dy, text, shadowColor, kerning, lenght);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (colored)
|
||||||
|
{
|
||||||
|
writeColored(x, y, text, textColor, kerning, lenght);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
write(x, y, text, kerning, lenght);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene la longitud en pixels de una cadena
|
||||||
|
int Text::lenght(std::string text, int kerning)
|
||||||
|
{
|
||||||
|
int shift = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < (int)text.length(); ++i)
|
||||||
|
shift += (offset[int(text[i])].w + kerning);
|
||||||
|
|
||||||
|
// Descuenta el kerning del último caracter
|
||||||
|
return shift - kerning;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Devuelve el valor de la variable
|
||||||
|
int Text::getCharacterSize()
|
||||||
|
{
|
||||||
|
return boxWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Recarga la textura
|
||||||
|
void Text::reLoadTexture()
|
||||||
|
{
|
||||||
|
sprite->getTexture()->reLoad();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece si se usa un tamaño fijo de letra
|
||||||
|
void Text::setFixedWidth(bool value)
|
||||||
|
{
|
||||||
|
fixedWidth = value;
|
||||||
|
}
|
||||||
80
text.h
Normal file
80
text.h
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "sprite.h"
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
#ifndef TEXT_H
|
||||||
|
#define TEXT_H
|
||||||
|
|
||||||
|
#define TXT_COLOR 1
|
||||||
|
#define TXT_SHADOW 2
|
||||||
|
#define TXT_CENTER 4
|
||||||
|
#define TXT_STROKE 8
|
||||||
|
|
||||||
|
struct offset_t
|
||||||
|
{
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
int w;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct textFile_t
|
||||||
|
{
|
||||||
|
int boxWidth; // Anchura de la caja de cada caracter en el png
|
||||||
|
int boxHeight; // Altura de la caja de cada caracter en el png
|
||||||
|
offset_t offset[128]; // Vector con las posiciones y ancho de cada letra
|
||||||
|
};
|
||||||
|
|
||||||
|
// Llena una estructuta textFile_t desde un fichero
|
||||||
|
textFile_t LoadTextFile(std::string file, bool verbose = false);
|
||||||
|
|
||||||
|
// Clase texto. Pinta texto en pantalla a partir de un bitmap
|
||||||
|
class Text
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
// Objetos y punteros
|
||||||
|
Sprite *sprite; // Objeto con los graficos para el texto
|
||||||
|
|
||||||
|
// Variables
|
||||||
|
int boxWidth; // Anchura de la caja de cada caracter en el png
|
||||||
|
int boxHeight; // Altura de la caja de cada caracter en el png
|
||||||
|
bool fixedWidth; // Indica si el texto se ha de escribir con longitud fija en todas las letras
|
||||||
|
offset_t offset[128]; // Vector con las posiciones y ancho de cada letra
|
||||||
|
|
||||||
|
public:
|
||||||
|
// Constructor
|
||||||
|
Text(std::string textFile, Texture *texture, SDL_Renderer *renderer);
|
||||||
|
Text(textFile_t *textFile, Texture *texture, SDL_Renderer *renderer);
|
||||||
|
|
||||||
|
// Destructor
|
||||||
|
~Text();
|
||||||
|
|
||||||
|
// Escribe el texto en pantalla
|
||||||
|
void write(int x, int y, 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);
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
// Escribe el texto centrado en un punto x
|
||||||
|
void writeCentered(int x, int y, 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 = {255, 255, 255}, Uint8 shadowDistance = 1, color_t shadowColor = {0, 0, 0}, int lenght = -1);
|
||||||
|
|
||||||
|
// Obtiene la longitud en pixels de una cadena
|
||||||
|
int lenght(std::string text, int kerning = 1);
|
||||||
|
|
||||||
|
// Devuelve el valor de la variable
|
||||||
|
int getCharacterSize();
|
||||||
|
|
||||||
|
// Recarga la textura
|
||||||
|
void reLoadTexture();
|
||||||
|
|
||||||
|
// Establece si se usa un tamaño fijo de letra
|
||||||
|
void setFixedWidth(bool value);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
208
texture.cpp
Normal file
208
texture.cpp
Normal file
@@ -0,0 +1,208 @@
|
|||||||
|
|
||||||
|
#include "texture.h"
|
||||||
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
|
#include "stb_image.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
|
||||||
|
// Carga el fichero en la textura
|
||||||
|
if (path != "")
|
||||||
|
{
|
||||||
|
loadFromFile(path, renderer, verbose);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Destructor
|
||||||
|
Texture::~Texture()
|
||||||
|
{
|
||||||
|
// Libera memoria
|
||||||
|
unload();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Carga una imagen desde un fichero
|
||||||
|
bool Texture::loadFromFile(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 width, height, orig_format;
|
||||||
|
unsigned char *data = stbi_load(path.c_str(), &width, &height, &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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int depth, pitch;
|
||||||
|
Uint32 pixel_format;
|
||||||
|
if (req_format == STBI_rgb)
|
||||||
|
{
|
||||||
|
depth = 24;
|
||||||
|
pitch = 3 * width; // 3 bytes por pixel * pixels per linea
|
||||||
|
pixel_format = SDL_PIXELFORMAT_RGB24;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{ // STBI_rgb_alpha (RGBA)
|
||||||
|
depth = 32;
|
||||||
|
pitch = 4 * width;
|
||||||
|
pixel_format = SDL_PIXELFORMAT_RGBA32;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Limpia
|
||||||
|
unload();
|
||||||
|
|
||||||
|
// La textura final
|
||||||
|
SDL_Texture *newTexture = nullptr;
|
||||||
|
|
||||||
|
// Carga la imagen desde una ruta específica
|
||||||
|
SDL_Surface *loadedSurface = SDL_CreateRGBSurfaceWithFormatFrom((void *)data, width, height, depth, pitch, pixel_format);
|
||||||
|
if (loadedSurface == nullptr)
|
||||||
|
{
|
||||||
|
if (verbose)
|
||||||
|
{
|
||||||
|
std::cout << "Unable to load image " << path.c_str() << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Crea la textura desde los pixels de la surface
|
||||||
|
newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
|
||||||
|
if (newTexture == nullptr)
|
||||||
|
{
|
||||||
|
if (verbose)
|
||||||
|
{
|
||||||
|
std::cout << "Unable to create texture from " << path.c_str() << "! SDL Error: " << SDL_GetError() << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Obtiene las dimensiones de la imagen
|
||||||
|
this->width = loadedSurface->w;
|
||||||
|
this->height = loadedSurface->h;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Elimina la textura cargada
|
||||||
|
SDL_FreeSurface(loadedSurface);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return success
|
||||||
|
stbi_image_free(data);
|
||||||
|
texture = newTexture;
|
||||||
|
return texture != nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Crea una textura en blanco
|
||||||
|
bool Texture::createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess access)
|
||||||
|
{
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this->width = width;
|
||||||
|
this->height = height;
|
||||||
|
}
|
||||||
|
|
||||||
|
return texture != nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Libera la memoria de la textura
|
||||||
|
void Texture::unload()
|
||||||
|
{
|
||||||
|
// Libera la textura si existe
|
||||||
|
if (texture != nullptr)
|
||||||
|
{
|
||||||
|
SDL_DestroyTexture(texture);
|
||||||
|
texture = nullptr;
|
||||||
|
width = 0;
|
||||||
|
height = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el color para la modulacion
|
||||||
|
void Texture::setColor(Uint8 red, Uint8 green, Uint8 blue)
|
||||||
|
{
|
||||||
|
SDL_SetTextureColorMod(texture, red, green, blue);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el blending
|
||||||
|
void Texture::setBlendMode(SDL_BlendMode blending)
|
||||||
|
{
|
||||||
|
SDL_SetTextureBlendMode(texture, blending);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el alpha para la modulación
|
||||||
|
void Texture::setAlpha(Uint8 alpha)
|
||||||
|
{
|
||||||
|
SDL_SetTextureAlphaMod(texture, alpha);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Renderiza la textura en un punto específico
|
||||||
|
void Texture::render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip, float zoomW, float zoomH, double angle, SDL_Point *center, SDL_RendererFlip flip)
|
||||||
|
{
|
||||||
|
// Establece el destino de renderizado en la pantalla
|
||||||
|
SDL_Rect renderQuad = {x, y, width, height};
|
||||||
|
|
||||||
|
// Obtiene las dimesiones del clip de renderizado
|
||||||
|
if (clip != nullptr)
|
||||||
|
{
|
||||||
|
renderQuad.w = clip->w;
|
||||||
|
renderQuad.h = clip->h;
|
||||||
|
}
|
||||||
|
|
||||||
|
renderQuad.w = renderQuad.w * zoomW;
|
||||||
|
renderQuad.h = renderQuad.h * zoomH;
|
||||||
|
|
||||||
|
// Renderiza a pantalla
|
||||||
|
SDL_RenderCopyEx(renderer, texture, clip, &renderQuad, angle, center, flip);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece la textura como objetivo de renderizado
|
||||||
|
void Texture::setAsRenderTarget(SDL_Renderer *renderer)
|
||||||
|
{
|
||||||
|
SDL_SetRenderTarget(renderer, texture);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene el ancho de la imagen
|
||||||
|
int Texture::getWidth()
|
||||||
|
{
|
||||||
|
return width;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene el alto de la imagen
|
||||||
|
int Texture::getHeight()
|
||||||
|
{
|
||||||
|
return height;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Recarga la textura
|
||||||
|
bool Texture::reLoad()
|
||||||
|
{
|
||||||
|
return loadFromFile(path, renderer);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene la textura
|
||||||
|
SDL_Texture *Texture::getSDLTexture()
|
||||||
|
{
|
||||||
|
return texture;
|
||||||
|
}
|
||||||
66
texture.h
Normal file
66
texture.h
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#ifndef TEXTURE_H
|
||||||
|
#define TEXTURE_H
|
||||||
|
|
||||||
|
class Texture
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
// Objetos y punteros
|
||||||
|
SDL_Texture *texture; // La textura
|
||||||
|
SDL_Renderer *renderer; // Renderizador donde dibujar la textura
|
||||||
|
|
||||||
|
// Variables
|
||||||
|
int width; // Ancho de la imagen
|
||||||
|
int height; // Alto de la imagen
|
||||||
|
std::string path; // Ruta de la imagen de la textura
|
||||||
|
|
||||||
|
public:
|
||||||
|
// Constructor
|
||||||
|
Texture(SDL_Renderer *renderer, std::string path = "", bool verbose = false);
|
||||||
|
|
||||||
|
// Destructor
|
||||||
|
~Texture();
|
||||||
|
|
||||||
|
// Carga una imagen desde un fichero
|
||||||
|
bool loadFromFile(std::string path, SDL_Renderer *renderer, bool verbose = false);
|
||||||
|
|
||||||
|
// Crea una textura en blanco
|
||||||
|
bool createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess = SDL_TEXTUREACCESS_STREAMING);
|
||||||
|
|
||||||
|
// Libera la memoria de la textura
|
||||||
|
void unload();
|
||||||
|
|
||||||
|
// Establece el color para la modulacion
|
||||||
|
void setColor(Uint8 red, Uint8 green, Uint8 blue);
|
||||||
|
|
||||||
|
// Establece el blending
|
||||||
|
void setBlendMode(SDL_BlendMode blending);
|
||||||
|
|
||||||
|
// Establece el alpha para la modulación
|
||||||
|
void setAlpha(Uint8 alpha);
|
||||||
|
|
||||||
|
// Renderiza la textura en un punto específico
|
||||||
|
void render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip = nullptr, float zoomW = 1, float zoomH = 1, double angle = 0.0, SDL_Point *center = nullptr, SDL_RendererFlip flip = SDL_FLIP_NONE);
|
||||||
|
|
||||||
|
// Establece la textura como objetivo de renderizado
|
||||||
|
void setAsRenderTarget(SDL_Renderer *renderer);
|
||||||
|
|
||||||
|
// Obtiene el ancho de la imagen
|
||||||
|
int getWidth();
|
||||||
|
|
||||||
|
// Obtiene el alto de la imagen
|
||||||
|
int getHeight();
|
||||||
|
|
||||||
|
// Recarga la textura
|
||||||
|
bool reLoad();
|
||||||
|
|
||||||
|
// Obtiene la textura
|
||||||
|
SDL_Texture *getSDLTexture();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
556
utils.cpp
Normal file
556
utils.cpp
Normal file
@@ -0,0 +1,556 @@
|
|||||||
|
#include "utils.h"
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
// Calcula el cuadrado de la distancia entre dos puntos
|
||||||
|
double distanceSquared(int x1, int y1, int x2, int y2)
|
||||||
|
{
|
||||||
|
const int deltaX = x2 - x1;
|
||||||
|
const int deltaY = y2 - y1;
|
||||||
|
return deltaX * deltaX + deltaY * deltaY;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Detector de colisiones entre dos circulos
|
||||||
|
bool checkCollision(circle_t &a, circle_t &b)
|
||||||
|
{
|
||||||
|
// Calcula el radio total al cuadrado
|
||||||
|
int totalRadiusSquared = a.r + b.r;
|
||||||
|
totalRadiusSquared = totalRadiusSquared * totalRadiusSquared;
|
||||||
|
|
||||||
|
// Si la distancia entre el centro de los circulos es inferior a la suma de sus radios
|
||||||
|
if (distanceSquared(a.x, a.y, b.x, b.y) < (totalRadiusSquared))
|
||||||
|
{
|
||||||
|
// Los circulos han colisionado
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// En caso contrario
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Detector de colisiones entre un circulo y un rectangulo
|
||||||
|
bool checkCollision(circle_t &a, SDL_Rect &b)
|
||||||
|
{
|
||||||
|
// Closest point on collision box
|
||||||
|
int cX, cY;
|
||||||
|
|
||||||
|
// Find closest x offset
|
||||||
|
if (a.x < b.x)
|
||||||
|
{
|
||||||
|
cX = b.x;
|
||||||
|
}
|
||||||
|
else if (a.x > b.x + b.w)
|
||||||
|
{
|
||||||
|
cX = b.x + b.w;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cX = a.x;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find closest y offset
|
||||||
|
if (a.y < b.y)
|
||||||
|
{
|
||||||
|
cY = b.y;
|
||||||
|
}
|
||||||
|
else if (a.y > b.y + b.h)
|
||||||
|
{
|
||||||
|
cY = b.y + b.h;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cY = a.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the closest point is inside the circle_t
|
||||||
|
if (distanceSquared(a.x, a.y, cX, cY) < a.r * a.r)
|
||||||
|
{
|
||||||
|
// This box and the circle_t have collided
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the shapes have not collided
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Detector de colisiones entre dos rectangulos
|
||||||
|
bool checkCollision(SDL_Rect &a, SDL_Rect &b)
|
||||||
|
{
|
||||||
|
// Calcula las caras del rectangulo a
|
||||||
|
const int leftA = a.x;
|
||||||
|
const int rightA = a.x + a.w;
|
||||||
|
const int topA = a.y;
|
||||||
|
const int bottomA = a.y + a.h;
|
||||||
|
|
||||||
|
// Calcula las caras del rectangulo b
|
||||||
|
const int leftB = b.x;
|
||||||
|
const int rightB = b.x + b.w;
|
||||||
|
const int topB = b.y;
|
||||||
|
const int bottomB = b.y + b.h;
|
||||||
|
|
||||||
|
// Si cualquiera de las caras de a está fuera de b
|
||||||
|
if (bottomA <= topB)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (topA >= bottomB)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rightA <= leftB)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (leftA >= rightB)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Si ninguna de las caras está fuera de b
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Detector de colisiones entre un punto y un rectangulo
|
||||||
|
bool checkCollision(SDL_Point &p, SDL_Rect &r)
|
||||||
|
{
|
||||||
|
// Comprueba si el punto está a la izquierda del rectangulo
|
||||||
|
if (p.x < r.x)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba si el punto está a la derecha del rectangulo
|
||||||
|
if (p.x > r.x + r.w)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba si el punto está por encima del rectangulo
|
||||||
|
if (p.y < r.y)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba si el punto está por debajo del rectangulo
|
||||||
|
if (p.y > r.y + r.h)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Si no está fuera, es que está dentro
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Detector de colisiones entre una linea horizontal y un rectangulo
|
||||||
|
bool checkCollision(h_line_t &l, SDL_Rect &r)
|
||||||
|
{
|
||||||
|
// Comprueba si la linea esta por encima del rectangulo
|
||||||
|
if (l.y < r.y)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba si la linea esta por debajo del rectangulo
|
||||||
|
if (l.y >= r.y + r.h)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba si el inicio de la linea esta a la derecha del rectangulo
|
||||||
|
if (l.x1 >= r.x + r.w)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba si el final de la linea esta a la izquierda del rectangulo
|
||||||
|
if (l.x2 < r.x)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Si ha llegado hasta aquí, hay colisión
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Detector de colisiones entre una linea vertical y un rectangulo
|
||||||
|
bool checkCollision(v_line_t &l, SDL_Rect &r)
|
||||||
|
{
|
||||||
|
// Comprueba si la linea esta por la izquierda del rectangulo
|
||||||
|
if (l.x < r.x)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba si la linea esta por la derecha del rectangulo
|
||||||
|
if (l.x >= r.x + r.w)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba si el inicio de la linea esta debajo del rectangulo
|
||||||
|
if (l.y1 >= r.y + r.h)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba si el final de la linea esta encima del rectangulo
|
||||||
|
if (l.y2 < r.y)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Si ha llegado hasta aquí, hay colisión
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Detector de colisiones entre una linea horizontal y un punto
|
||||||
|
bool checkCollision(h_line_t &l, SDL_Point &p)
|
||||||
|
{
|
||||||
|
// Comprueba si el punto esta sobre la linea
|
||||||
|
if (p.y > l.y)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba si el punto esta bajo la linea
|
||||||
|
if (p.y < l.y)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba si el punto esta a la izquierda de la linea
|
||||||
|
if (p.x < l.x1)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba si el punto esta a la derecha de la linea
|
||||||
|
if (p.x > l.x2)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Si ha llegado aquí, hay colisión
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Detector de colisiones entre dos lineas
|
||||||
|
SDL_Point checkCollision(line_t &l1, line_t &l2)
|
||||||
|
{
|
||||||
|
const float x1 = l1.x1;
|
||||||
|
const float y1 = l1.y1;
|
||||||
|
const float x2 = l1.x2;
|
||||||
|
const float y2 = l1.y2;
|
||||||
|
|
||||||
|
const float x3 = l2.x1;
|
||||||
|
const float y3 = l2.y1;
|
||||||
|
const float x4 = l2.x2;
|
||||||
|
const float y4 = l2.y2;
|
||||||
|
|
||||||
|
// calculate the direction of the lines
|
||||||
|
float uA = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1));
|
||||||
|
float uB = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1));
|
||||||
|
|
||||||
|
// if uA and uB are between 0-1, lines are colliding
|
||||||
|
if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1)
|
||||||
|
{
|
||||||
|
// Calcula la intersección
|
||||||
|
const float x = x1 + (uA * (x2 - x1));
|
||||||
|
const float y = y1 + (uA * (y2 - y1));
|
||||||
|
|
||||||
|
return {(int)round(x), (int)round(y)};
|
||||||
|
}
|
||||||
|
return {-1, -1};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Detector de colisiones entre dos lineas
|
||||||
|
SDL_Point checkCollision(d_line_t &l1, v_line_t &l2)
|
||||||
|
{
|
||||||
|
const float x1 = l1.x1;
|
||||||
|
const float y1 = l1.y1;
|
||||||
|
const float x2 = l1.x2;
|
||||||
|
const float y2 = l1.y2;
|
||||||
|
|
||||||
|
const float x3 = l2.x;
|
||||||
|
const float y3 = l2.y1;
|
||||||
|
const float x4 = l2.x;
|
||||||
|
const float y4 = l2.y2;
|
||||||
|
|
||||||
|
// calculate the direction of the lines
|
||||||
|
float uA = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1));
|
||||||
|
float uB = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1));
|
||||||
|
|
||||||
|
// if uA and uB are between 0-1, lines are colliding
|
||||||
|
if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1)
|
||||||
|
{
|
||||||
|
// Calcula la intersección
|
||||||
|
const float x = x1 + (uA * (x2 - x1));
|
||||||
|
const float y = y1 + (uA * (y2 - y1));
|
||||||
|
|
||||||
|
return {(int)x, (int)y};
|
||||||
|
}
|
||||||
|
return {-1, -1};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Normaliza una linea diagonal
|
||||||
|
void normalizeLine(d_line_t &l)
|
||||||
|
{
|
||||||
|
// Las lineas diagonales van de izquierda a derecha
|
||||||
|
// x2 mayor que x1
|
||||||
|
if (l.x2 < l.x1)
|
||||||
|
{
|
||||||
|
const int x = l.x1;
|
||||||
|
const int y = l.y1;
|
||||||
|
l.x1 = l.x2;
|
||||||
|
l.y1 = l.y2;
|
||||||
|
l.x2 = x;
|
||||||
|
l.y2 = y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Detector de colisiones entre un punto y una linea diagonal
|
||||||
|
bool checkCollision(SDL_Point &p, d_line_t &l)
|
||||||
|
{
|
||||||
|
// Comprueba si el punto está en alineado con la linea
|
||||||
|
if (abs(p.x - l.x1) != abs(p.y - l.y1))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba si está a la derecha de la linea
|
||||||
|
if (p.x > l.x1 && p.x > l.x2)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba si está a la izquierda de la linea
|
||||||
|
if (p.x < l.x1 && p.x < l.x2)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba si está por encima de la linea
|
||||||
|
if (p.y > l.y1 && p.y > l.y2)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba si está por debajo de la linea
|
||||||
|
if (p.y < l.y1 && p.y < l.y2)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// En caso contrario, el punto está en la linea
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Devuelve un color_t a partir de un string
|
||||||
|
color_t stringToColor(palette_e pal, std::string str)
|
||||||
|
{
|
||||||
|
if (pal == p_zxspectrum)
|
||||||
|
{
|
||||||
|
if (str == "black")
|
||||||
|
{
|
||||||
|
return {0x00, 0x00, 0x00};
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (str == "bright_black")
|
||||||
|
{
|
||||||
|
return {0x00, 0x00, 0x00};
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (str == "blue")
|
||||||
|
{
|
||||||
|
return {0x00, 0x00, 0xd8};
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (str == "bright_blue")
|
||||||
|
{
|
||||||
|
return {0x00, 0x00, 0xFF};
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (str == "red")
|
||||||
|
{
|
||||||
|
return {0xd8, 0x00, 0x00};
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (str == "bright_red")
|
||||||
|
{
|
||||||
|
return {0xFF, 0x00, 0x00};
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (str == "magenta")
|
||||||
|
{
|
||||||
|
return {0xd8, 0x00, 0xd8};
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (str == "bright_magenta")
|
||||||
|
{
|
||||||
|
return {0xFF, 0x00, 0xFF};
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (str == "green")
|
||||||
|
{
|
||||||
|
return {0x00, 0xd8, 0x00};
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (str == "bright_green")
|
||||||
|
{
|
||||||
|
return {0x00, 0xFF, 0x00};
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (str == "cyan")
|
||||||
|
{
|
||||||
|
return {0x00, 0xd8, 0xd8};
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (str == "bright_cyan")
|
||||||
|
{
|
||||||
|
return {0x00, 0xFF, 0xFF};
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (str == "yellow")
|
||||||
|
{
|
||||||
|
return {0xd8, 0xd8, 0x00};
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (str == "bright_yellow")
|
||||||
|
{
|
||||||
|
return {0xFF, 0xFF, 0x00};
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (str == "white")
|
||||||
|
{
|
||||||
|
return {0xd8, 0xd8, 0xd8};
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (str == "bright_white")
|
||||||
|
{
|
||||||
|
return {0xFF, 0xFF, 0xFF};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (pal == p_zxarne)
|
||||||
|
{ // zxarne
|
||||||
|
if (str == "black")
|
||||||
|
{
|
||||||
|
return {0x00, 0x00, 0x00};
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (str == "bright_black")
|
||||||
|
{
|
||||||
|
return {0x3C, 0x35, 0x1F};
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (str == "blue")
|
||||||
|
{
|
||||||
|
return {0x31, 0x33, 0x90};
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (str == "bright_blue")
|
||||||
|
{
|
||||||
|
return {0x15, 0x59, 0xDB};
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (str == "red")
|
||||||
|
{
|
||||||
|
return {0xA7, 0x32, 0x11};
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (str == "bright_red")
|
||||||
|
{
|
||||||
|
return {0xD8, 0x55, 0x25};
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (str == "magenta")
|
||||||
|
{
|
||||||
|
return {0xA1, 0x55, 0x89};
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (str == "bright_magenta")
|
||||||
|
{
|
||||||
|
return {0xCD, 0x7A, 0x50};
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (str == "green")
|
||||||
|
{
|
||||||
|
return {0x62, 0x9A, 0x31};
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (str == "bright_green")
|
||||||
|
{
|
||||||
|
return {0x9C, 0xD3, 0x3C};
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (str == "cyan")
|
||||||
|
{
|
||||||
|
return {0x28, 0xA4, 0xCB};
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (str == "bright_cyan")
|
||||||
|
{
|
||||||
|
return {0x65, 0xDC, 0xD6};
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (str == "yellow")
|
||||||
|
{
|
||||||
|
return {0xE8, 0xBC, 0x50};
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (str == "bright_yellow")
|
||||||
|
{
|
||||||
|
return {0xF1, 0xE7, 0x82};
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (str == "white")
|
||||||
|
{
|
||||||
|
return {0xBF, 0xBF, 0xBD};
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (str == "bright_white")
|
||||||
|
{
|
||||||
|
return {0xF2, 0xF1, 0xED};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {0x00, 0x00, 0x00};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convierte una cadena en un valor booleano
|
||||||
|
bool stringToBool(std::string str)
|
||||||
|
{
|
||||||
|
if (str == "true")
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convierte un valor booleano en una cadena
|
||||||
|
std::string boolToString(bool value)
|
||||||
|
{
|
||||||
|
if (value)
|
||||||
|
{
|
||||||
|
return "true";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return "false";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compara dos colores
|
||||||
|
bool colorAreEqual(color_t color1, color_t color2)
|
||||||
|
{
|
||||||
|
const bool r = color1.r == color2.r;
|
||||||
|
const bool g = color1.g == color2.g;
|
||||||
|
const bool b = color1.b == color2.b;
|
||||||
|
|
||||||
|
return (r && g && b);
|
||||||
|
}
|
||||||
200
utils.h
Normal file
200
utils.h
Normal file
@@ -0,0 +1,200 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include "texture.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#ifndef UTILS_H
|
||||||
|
#define UTILS_H
|
||||||
|
|
||||||
|
// Estructura para definir un circulo
|
||||||
|
struct circle_t
|
||||||
|
{
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
int r;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Estructura para definir una linea horizontal
|
||||||
|
struct h_line_t
|
||||||
|
{
|
||||||
|
int x1, x2, y;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Estructura para definir una linea vertical
|
||||||
|
struct v_line_t
|
||||||
|
{
|
||||||
|
int x, y1, y2;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Estructura para definir una linea diagonal
|
||||||
|
struct d_line_t
|
||||||
|
{
|
||||||
|
int x1, y1, x2, y2;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Estructura para definir una linea
|
||||||
|
struct line_t
|
||||||
|
{
|
||||||
|
int x1, y1, x2, y2;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Estructura para definir un color
|
||||||
|
struct color_t
|
||||||
|
{
|
||||||
|
Uint8 r;
|
||||||
|
Uint8 g;
|
||||||
|
Uint8 b;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Tipos de paleta
|
||||||
|
enum palette_e
|
||||||
|
{
|
||||||
|
p_zxspectrum,
|
||||||
|
p_zxarne
|
||||||
|
};
|
||||||
|
|
||||||
|
// Posiciones de las notificaciones
|
||||||
|
enum not_pos_e
|
||||||
|
{
|
||||||
|
pos_top,
|
||||||
|
pos_bottom,
|
||||||
|
pos_left,
|
||||||
|
pos_middle,
|
||||||
|
pos_right
|
||||||
|
};
|
||||||
|
|
||||||
|
// Tipos de control de teclado
|
||||||
|
enum ctrl_schem_e
|
||||||
|
{
|
||||||
|
ctrl_cursor,
|
||||||
|
ctrl_opqa,
|
||||||
|
ctrl_wasd
|
||||||
|
};
|
||||||
|
|
||||||
|
// Estructura para las opciones de las notificaciones
|
||||||
|
struct op_notification_t
|
||||||
|
{
|
||||||
|
not_pos_e posH; // Ubicación de las notificaciones en pantalla
|
||||||
|
not_pos_e posV; // Ubicación de las notificaciones en pantalla
|
||||||
|
bool sound; // Indica si las notificaciones suenan
|
||||||
|
color_t color; // Color de las notificaciones
|
||||||
|
};
|
||||||
|
|
||||||
|
// Estructura para saber la seccion y subseccion del programa
|
||||||
|
struct section_t
|
||||||
|
{
|
||||||
|
Uint8 name;
|
||||||
|
Uint8 subsection;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Estructura para albergar trucos
|
||||||
|
struct cheat_t
|
||||||
|
{
|
||||||
|
bool infiniteLives; // Indica si el jugador dispone de vidas infinitas
|
||||||
|
bool invincible; // Indica si el jugador puede morir
|
||||||
|
bool jailEnabled; // Indica si la Jail está abierta
|
||||||
|
bool altSkin; // Indicxa si se usa una skin diferente para el jugador
|
||||||
|
};
|
||||||
|
|
||||||
|
// Estructura para el servicio online
|
||||||
|
struct online_t
|
||||||
|
{
|
||||||
|
bool enabled; // Indica si se quiere usar el modo online o no
|
||||||
|
bool sessionEnabled; // Indica ya se ha hecho login
|
||||||
|
std::string server; // Servidor para los servicios online
|
||||||
|
int port; // Puerto del servidor
|
||||||
|
std::string gameID; // Identificador del juego para los servicios online
|
||||||
|
std::string jailerID; // Identificador del jugador para los servicios online
|
||||||
|
int score; // Puntuación almacenada online
|
||||||
|
};
|
||||||
|
|
||||||
|
// Estructura para almacenar estadísticas
|
||||||
|
struct op_stats_t
|
||||||
|
{
|
||||||
|
int rooms; // Cantidad de habitaciones visitadas
|
||||||
|
int items; // Cantidad de items obtenidos
|
||||||
|
std::string worstNightmare; // Habitación con más muertes acumuladas
|
||||||
|
};
|
||||||
|
|
||||||
|
// Estructura con opciones de la pantalla
|
||||||
|
struct op_screen_t
|
||||||
|
{
|
||||||
|
int windowWidth; // Ancho de la ventana
|
||||||
|
int windowHeight; // Alto de la ventana
|
||||||
|
};
|
||||||
|
|
||||||
|
// Estructura con todas las opciones de configuración del programa
|
||||||
|
struct options_t
|
||||||
|
{
|
||||||
|
std::string configVersion; // Versión del programa. Sirve para saber si las opciones son compatibles
|
||||||
|
Uint32 videoMode; // Contiene el valor del modo de pantalla completa
|
||||||
|
int windowSize; // Contiene el valor por el que se multiplica el tamaño de la ventana
|
||||||
|
Uint32 filter; // Filtro usado para el escalado de la imagen
|
||||||
|
bool vSync; // Indica si se quiere usar vsync o no
|
||||||
|
int gameWidth; // Ancho de la resolucion nativa del juego
|
||||||
|
int gameHeight; // Alto de la resolucion nativa del juego
|
||||||
|
bool integerScale; // Indica si el escalado de la imagen ha de ser entero en el modo a pantalla completa
|
||||||
|
bool keepAspect; // Indica si se ha de mantener la relación de aspecto al poner el modo a pantalla completa
|
||||||
|
bool borderEnabled; // Indica si ha de mostrar el borde en el modo de ventana
|
||||||
|
int borderWidth; // Cantidad de pixels que se añade en el borde de la ventana
|
||||||
|
int borderHeight; // Cantidad de pixels que se añade en el borde de la ventana
|
||||||
|
palette_e palette; // Paleta de colores a usar en el juego
|
||||||
|
bool console; // Indica si ha de mostrar información por la consola de texto
|
||||||
|
cheat_t cheat; // Contiene trucos y ventajas para el juego
|
||||||
|
op_stats_t stats; // Datos con las estadisticas de juego
|
||||||
|
online_t online; // Datos del servicio online
|
||||||
|
op_notification_t notifications; // Opciones relativas a las notificaciones;
|
||||||
|
op_screen_t screen; // Opciones relativas a la clase screen
|
||||||
|
ctrl_schem_e keys; // Teclas usadas para jugar
|
||||||
|
};
|
||||||
|
|
||||||
|
// Calcula el cuadrado de la distancia entre dos puntos
|
||||||
|
double distanceSquared(int x1, int y1, int x2, int y2);
|
||||||
|
|
||||||
|
// Detector de colisiones entre dos circulos
|
||||||
|
bool checkCollision(circle_t &a, circle_t &b);
|
||||||
|
|
||||||
|
// Detector de colisiones entre un circulo y un rectangulo
|
||||||
|
bool checkCollision(circle_t &a, SDL_Rect &b);
|
||||||
|
|
||||||
|
// Detector de colisiones entre un dos rectangulos
|
||||||
|
bool checkCollision(SDL_Rect &a, SDL_Rect &b);
|
||||||
|
|
||||||
|
// Detector de colisiones entre un punto y un rectangulo
|
||||||
|
bool checkCollision(SDL_Point &p, SDL_Rect &r);
|
||||||
|
|
||||||
|
// Detector de colisiones entre una linea horizontal y un rectangulo
|
||||||
|
bool checkCollision(h_line_t &l, SDL_Rect &r);
|
||||||
|
|
||||||
|
// Detector de colisiones entre una linea vertical y un rectangulo
|
||||||
|
bool checkCollision(v_line_t &l, SDL_Rect &r);
|
||||||
|
|
||||||
|
// Detector de colisiones entre una linea horizontal y un punto
|
||||||
|
bool checkCollision(h_line_t &l, SDL_Point &p);
|
||||||
|
|
||||||
|
// Detector de colisiones entre dos lineas
|
||||||
|
SDL_Point checkCollision(line_t &l1, line_t &l2);
|
||||||
|
|
||||||
|
// Detector de colisiones entre dos lineas
|
||||||
|
SDL_Point checkCollision(d_line_t &l1, v_line_t &l2);
|
||||||
|
|
||||||
|
// Detector de colisiones entre un punto y una linea diagonal
|
||||||
|
bool checkCollision(SDL_Point &p, d_line_t &l);
|
||||||
|
|
||||||
|
// Normaliza una linea diagonal
|
||||||
|
void normalizeLine(d_line_t &l);
|
||||||
|
|
||||||
|
// Devuelve un color_t a partir de un string
|
||||||
|
color_t stringToColor(palette_e pal, std::string str);
|
||||||
|
|
||||||
|
// Convierte una cadena en un valor booleano
|
||||||
|
bool stringToBool(std::string str);
|
||||||
|
|
||||||
|
// Convierte un valor booleano en una cadena
|
||||||
|
std::string boolToString(bool value);
|
||||||
|
|
||||||
|
// Compara dos colores
|
||||||
|
bool colorAreEqual(color_t color1, color_t color2);
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user