37 Commits

Author SHA1 Message Date
c8bebfd2d9 Me voy a la cama. Sigo con la reescritura de todo el código 2022-09-26 22:25:46 +02:00
35e7abcd3c se va la luz 2022-09-26 18:18:27 +02:00
b092d3f86a Trabajando en la reescritura del código 2022-09-26 14:06:44 +02:00
837bcbd1da menudo berenjenal. sigo con la integracion con las ultimas versiones de mis librerias 2022-09-26 11:18:50 +02:00
26bc47d9f6 Actualizando las librerias a la ultima versión 2022-09-26 10:34:45 +02:00
9e089e9092 Añadiendo la clase asset 2022-09-26 10:17:54 +02:00
25616e1e6c Ya permite continuar 2022-09-25 23:16:20 +02:00
ed67e429d8 Trabajando en poder continuar la partida al morir 2022-09-25 22:45:09 +02:00
4ebd034b59 Cambiado un poco el outline de la fuente nokia_big2 2022-09-25 22:24:41 +02:00
38f3e2e4d3 Limpieza de código no usado 2022-09-25 21:27:12 +02:00
6493b73f4f Cambiados NULL por nullptr 2022-09-25 21:22:59 +02:00
6637ba9f10 Actualizado a la última versión de ltexture 2022-09-25 21:11:17 +02:00
a74c8d5122 Eliminado codigo sin usar 2022-09-25 20:17:06 +02:00
4df0db088f Organización de código 2022-09-25 20:10:31 +02:00
27cb772933 corregido el offset de la m en nokia_big2 2022-09-25 19:40:39 +02:00
778b07cce1 Modificado el texto al cambiar de pantalla. Quitado texto no usado 2022-09-25 19:38:03 +02:00
1fa5c75466 Actualizado README.md 2022-09-25 19:01:13 +02:00
0b00acf31b Cambiado el numero de versión 2022-09-25 18:59:35 +02:00
2979a6752c Limpieza de código. Añadida la última versión de jail_audio 2022-09-25 18:44:58 +02:00
f6494e69c8 Cambiado el idioma por defecto a valenciano 2022-08-21 10:10:09 +02:00
5167a5857d Cambiada la frecuencia a 48000Hz de dos pistas de audio 2022-08-21 10:05:38 +02:00
28fe1130cf Merge branch 'master' of https://gitea.sustancia.synology.me/JailDesigner/coffee_crisis 2022-08-07 12:53:41 +02:00
e92b7eab4e limpieza de archivos 2022-08-07 12:53:14 +02:00
14f7a79927 Actualizar 'README.md' 2022-08-05 23:13:55 +02:00
4f6a6a4918 Actualizar 'Makefile' 2021-09-20 13:48:59 +02:00
f5b35cee6b Trabajando en la versión de opendingux 2021-09-17 20:55:36 +02:00
454791fe96 Añadidos modos en pantalla completa para el escalado entero y mantener la relación de aspecto 2021-09-15 11:48:57 +02:00
3f165be114 Commit para seguir en otro ordenador 2021-09-15 06:37:54 +02:00
778672a6f5 Commit para seguir en otro ordenador 2021-09-14 22:52:25 +02:00
8c919abc9d Trabajando en el escalado entero y el tamaño personalizable de la ventana de renderizado a traves de las opciones 2021-09-14 21:15:52 +02:00
7e37da3999 Restituido ballong.png a los colores originales 2021-09-14 17:03:13 +02:00
842d5dd56b Limpieza de código 2021-09-11 09:06:53 +02:00
ea3f16b8ac Añadida la clase screen al código 2021-09-10 23:52:58 +02:00
3fe0861e4f Trabajando en integrar la clase screen 2021-09-10 23:04:52 +02:00
b28d798545 Actualizado el numero de versión 2021-09-10 20:55:46 +02:00
bdf092d46e Limpieza de código. Modificada la fuente nokiabig2. Puestas ayudas para los dos jugadores. Cambiado el tamaño del mensaje de juego completado 2021-09-10 20:46:24 +02:00
09421a093d Limpieza de código 2021-09-10 14:02:29 +02:00
118 changed files with 4827 additions and 3034 deletions

11
.gitignore vendored
View File

@@ -1,12 +1,5 @@
.vscode
*.DS_Store
bin
data/config.bin
data/score.bin
dll
docs
icon
releases
resources
scripts
*.opk
*.DS_Store
data/score.bin

View File

@@ -6,4 +6,8 @@ macos:
linux:
mkdir -p bin
g++ source/*.cpp -std=c++11 -Os -lSDL2 -ffunction-sections -fdata-sections -Wl,--gc-sections -o bin/$(executable)_linux
strip -s -R .comment -R .gnu.version bin/$(executable)_linux --strip-unneeded
strip -s -R .comment -R .gnu.version bin/$(executable)_linux --strip-unneeded
opendingux:
mkdir -p bin
/opt/gcw0-toolchain/usr/bin/mipsel-linux-gcc -D GCWZERO -O2 -std=c++11 -I/opt/gcw0-toolchain/usr/mipsel-gcw0-linux-uclibc/sysroot/usr/include/SDL2 -D_GNU_SOURCE=1 -D_REENTRANT -lSDL2 -lSDL2_mixer -lstdc++ source/*.cpp -o bin/$(executable)_opendingux
/opt/gcw0-toolchain/usr/bin/mksquashfs ./default.gcw0.desktop ./icon.png ./bin ./data ./media coffee_crisis.opk -all-root -noappend -no-exports -no-xattrs

View File

@@ -1,6 +1,6 @@
# Coffee Crisis
Coffe Crisis es un juego arcade que pondrá a prueba tus reflejos. Empezado el verano de 2020 y terminado el verano de 2021. Intenta conseguir todos los puntos que puedas con una sola vida y ayuda a Bal1 a defender la UPV ante la invasión de la cafeína durante 10 estresantes oleadas.
Coffee Crisis es un juego arcade que pondrá a prueba tus reflejos. Empezado durante el verano de 2020 y terminado un año despues, en el verano de 2021. Intenta conseguir todos los puntos que puedas con una sola vida a traves de los 10 niveles de juego y ayuda a Bal1 a defender la UPV de la invasión de la cafeína esférica y saltarina.
## Compilar

BIN
data/config/config.bin Normal file

Binary file not shown.

BIN
data/config/score.bin Normal file

Binary file not shown.

View File

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

Before

Width:  |  Height:  |  Size: 2.0 KiB

After

Width:  |  Height:  |  Size: 2.0 KiB

View File

Before

Width:  |  Height:  |  Size: 2.4 KiB

After

Width:  |  Height:  |  Size: 2.4 KiB

BIN
data/font/nokia_big2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

View File

@@ -157,7 +157,7 @@
# 108 l
8
# 109 m
10
20
# 110 n
14
# 111 o

View File

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

Before

Width:  |  Height:  |  Size: 2.2 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB

BIN
data/gfx/balloon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 57 KiB

View File

Before

Width:  |  Height:  |  Size: 644 B

After

Width:  |  Height:  |  Size: 644 B

View File

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

View File

Before

Width:  |  Height:  |  Size: 795 B

After

Width:  |  Height:  |  Size: 795 B

View File

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 13 KiB

View File

Before

Width:  |  Height:  |  Size: 2.3 KiB

After

Width:  |  Height:  |  Size: 2.3 KiB

View File

Before

Width:  |  Height:  |  Size: 3.0 KiB

After

Width:  |  Height:  |  Size: 3.0 KiB

View File

Before

Width:  |  Height:  |  Size: 6.8 KiB

After

Width:  |  Height:  |  Size: 6.8 KiB

View File

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 1.8 KiB

View File

Before

Width:  |  Height:  |  Size: 6.9 KiB

After

Width:  |  Height:  |  Size: 6.9 KiB

View File

Before

Width:  |  Height:  |  Size: 785 B

After

Width:  |  Height:  |  Size: 785 B

View File

Before

Width:  |  Height:  |  Size: 6.5 KiB

After

Width:  |  Height:  |  Size: 6.5 KiB

View File

Before

Width:  |  Height:  |  Size: 1.7 KiB

After

Width:  |  Height:  |  Size: 1.7 KiB

View File

Before

Width:  |  Height:  |  Size: 6.6 KiB

After

Width:  |  Height:  |  Size: 6.6 KiB

View File

Before

Width:  |  Height:  |  Size: 654 B

After

Width:  |  Height:  |  Size: 654 B

View File

Before

Width:  |  Height:  |  Size: 71 KiB

After

Width:  |  Height:  |  Size: 71 KiB

View File

@@ -75,21 +75,21 @@ Temps detes:
## 37 - TEXTOS DEL JUEGO
D E M O
## 38 - TEXTOS DEL JUEGO
Pantalla
## 39 - MARCADOR
PUNTS
## 40 - MARCADOR
MAX.PUNT.
## 41 - MARCADOR
MULT
## 42 - MARCADOR
PANTALLA
fases mes!
## 39 -
## 40 -
## 41 -
## 42 -
## 43 - PANTALLA DE GAME OVER
FI DEL JOC
## 44 - PANTALLA DE GAME OVER
ELS TEUS PUNTS:
## 45 - PANTALLA DE GAME OVER
REINTENTAR?
CONTINUAR?
## 46 - MENU DE PAUSA
CONTINUAR
## 47 - MENU DE PAUSA
@@ -113,7 +113,7 @@ mult
## 56 MARCADOR
max. puntuacio
## 57 MARCADOR
pantalla
fase
## 58 - MENU DE OPCIONES
MODE DE VISUALITZACIO
## 59 - MENU DE OPCIONES
@@ -126,10 +126,10 @@ SINC. VERTICAL
CONTROLS DEL JUGADOR 1
## 63 - MENU DE OPCIONES
CONTROLS DEL JUGADOR 2
## 64 - MENU DE OPCIONES
TECLAT
## 65 - MENU DE OPCIONES
MANDO
## 64 -
## 65 -
## 66 - MENU DE OPCIONES
FACIL
## 67 - MENU DE OPCIONES
@@ -155,4 +155,6 @@ Endavant!
## 77 - PANTALLA DE GAME OVER
PUNTS J1:
## 78 - PANTALLA DE GAME OVER
PUNTS J2:
PUNTS J2:
## 79 - TEXTOS DEL JUEGO
Ultima fase!

View File

@@ -75,21 +75,21 @@ Time stopped:
## 37 - TEXTOS DEL JUEGO
D E M O
## 38 - TEXTOS DEL JUEGO
Stage
## 39 - MARCADOR
SCORE
## 40 - MARCADOR
HI-SCORE
## 41 - MARCADOR
MULT
## 42 - MARCADOR
STAGE
stages left!
## 39 -
## 40 -
## 41 -
## 42 -
## 43 - PANTALLA DE GAME OVER
GAME OVER
## 44 - PANTALLA DE GAME OVER
YOUR SCORE:
## 45 - PANTALLA DE GAME OVER
RETRY?
CONTINUE?
## 46 - MENU DE PAUSA
CONTINUE
## 47 - MENU DE PAUSA
@@ -126,10 +126,10 @@ VSYNC
PLAYER 1 CONTROLS
## 63 - MENU DE OPCIONES
PLAYER 2 CONTROLS
## 64 - MENU DE OPCIONES
KEYBOARD
## 65 - MENU DE OPCIONES
GAME CONTROLLER
## 64 -
## 65 -
## 66 - MENU DE OPCIONES
EASY
## 67 - MENU DE OPCIONES
@@ -155,4 +155,6 @@ Get Ready!
## 77 - PANTALLA DE GAME OVER
PLAYER1 SCORE:
## 78 - PANTALLA DE GAME OVER
PLAYER2 SCORE:
PLAYER2 SCORE:
## 79 - TEXTOS DEL JUEGO
Last stage!

View File

@@ -75,21 +75,21 @@ Tiempo:
## 37 - TEXTOS DEL JUEGO
D E M O
## 38 - TEXTOS DEL JUEGO
Fase
## 39 - MARCADOR
PUNTOS
## 40 - MARCADOR
MAX.PUNT.
## 41 - MARCADOR
MULT
## 42 - MARCADOR
FASE
fases mas!
## 39 -
## 40 -
## 41 -
## 42 -
## 43 - PANTALLA DE GAME OVER
FIN DE JUEGO
## 44 - PANTALLA DE GAME OVER
TU PUNTUACION:
## 45 - PANTALLA DE GAME OVER
REINTENTAR?
CONTINUAR?
## 46 - MENU DE PAUSA
CONTINUAR
## 47 - MENU DE PAUSA
@@ -113,7 +113,7 @@ mult
## 56 - MARCADOR
max. puntuacion
## 57 - MARCADOR
fase
FASE
## 58 - MENU DE OPCIONES
MODO DE VISUALIZACION
## 59 - MENU DE OPCIONES
@@ -126,10 +126,10 @@ SINC. VERTICAL
CONTROLES DEL JUGADOR 1
## 63 - MENU DE OPCIONES
CONTROLES DEL JUGADOR 2
## 64 - MENU DE OPCIONES
TECLADO
## 65 - MENU DE OPCIONES
MANDO
## 64 -
## 65 -
## 66 - MENU DE OPCIONES
FACIL
## 67 - MENU DE OPCIONES
@@ -155,4 +155,6 @@ Adelante!
## 77 - PANTALLA DE GAME OVER
PUNTUACION J1:
## 78 - PANTALLA DE GAME OVER
PUNTUACION J2:
PUNTUACION J2:
## 79 - TEXTOS DEL JUEGO
Ultima fase!

BIN
data/music/playing.ogg Normal file

Binary file not shown.

BIN
data/music/title.ogg Normal file

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 57 KiB

Binary file not shown.

Binary file not shown.

View File

@@ -1,123 +1,333 @@
#include "const.h"
#include "animatedsprite.h"
// Constructor
AnimatedSprite::AnimatedSprite()
AnimatedSprite::AnimatedSprite(LTexture *texture, SDL_Renderer *renderer, std::string file)
{
//init(nullptr, nullptr);
// Copia los punteros
setTexture(texture);
setRenderer(renderer);
// Carga las animaciones
load(file);
// Inicializa variables
currentAnimation = 0;
}
// Destructor
AnimatedSprite::~AnimatedSprite()
{
//init(nullptr, nullptr);
for (auto &a : animation)
{
a.frames.clear();
}
animation.clear();
}
// Iniciador
void AnimatedSprite::init(LTexture *texture, SDL_Renderer *renderer)
// Obtiene el indice de la animación a partir del nombre
int AnimatedSprite::getIndex(std::string name)
{
mRenderer = renderer;
mTexture = texture;
for (int i = 0; i < 20; i++)
int index = -1;
for (auto a : animation)
{
mAnimation[i].numFrames = 0;
mAnimation[i].speed = 0;
mAnimation[i].loop = true;
mAnimation[i].completed = false;
for (int j = 0; i < 20; i++)
index++;
if (a.name == name)
{
mAnimation[i].frames[j].x = 0;
mAnimation[i].frames[j].y = 0;
mAnimation[i].frames[j].w = 0;
mAnimation[i].frames[j].h = 0;
return index;
}
}
mCurrentFrame = 0;
mAnimationCounter = 0;
printf("** Warning: could not find \"%s\" animation\n", name.c_str());
return -1;
}
// Calcula el frame correspondiente a la animación
void AnimatedSprite::animate(int index)
void AnimatedSprite::animate()
{
if (mEnabled)
if (!enabled || animation[currentAnimation].speed == 0)
{
// Calculamos el frame actual a partir del contador
mCurrentFrame = mAnimationCounter / mAnimation[index].speed;
return;
}
// Si alcanzamos el final de la animación, reiniciamos el contador de la animación
// en función de la variable loop
if (mCurrentFrame >= mAnimation[index].numFrames)
{
if (mAnimation[index].loop)
mAnimationCounter = 0;
else
mCurrentFrame = mAnimation[index].numFrames;
// 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;
}
// En caso contrario
else
{
// Escogemos el frame correspondiente de la animación
setSpriteClip(mAnimation[index].frames[mCurrentFrame]);
// Incrementamos el contador de la animacion
mAnimationCounter++;
{ // 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++;
}
}
// Establece el frame actual de la animación
void AnimatedSprite::setCurrentFrame(Uint8 num)
void AnimatedSprite::setCurrentFrame(int num)
{
mCurrentFrame = num;
// Descarta valores fuera de rango
if (num >= (int)animation[currentAnimation].frames.size())
{
num = 0;
}
// Cambia el valor de la variable
animation[currentAnimation].counter = animation[currentAnimation].speed * num;
// Escoge el frame correspondiente de la animación
setSpriteClip(animation[currentAnimation].frames[animation[currentAnimation].currentFrame]);
}
// Establece el valor del contador
void AnimatedSprite::setAnimationCounter(Uint16 num)
void AnimatedSprite::setAnimationCounter(std::string name, int num)
{
mAnimationCounter = 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];
}
// Carga la animación desde un fichero
bool AnimatedSprite::load(std::string filePath)
{
int frames_per_row = 0;
int frame_width = 0;
int frame_height = 0;
// Indicador de éxito en la carga
bool success = true;
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
printf("Reading file %s\n", filename.c_str());
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]")
{
t_animation 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, frame_width, frame_height};
while (getline(ss, tmp, ','))
{
int num_tile = std::stoi(tmp);
rect.x = (num_tile % frames_per_row) * frame_width;
rect.y = (num_tile / frames_per_row) * frame_height;
buffer.frames.push_back(rect);
}
}
else
{
printf("Warning: file %s, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str());
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) == "frames_per_row")
{
frames_per_row = std::stoi(line.substr(pos + 1, line.length()));
}
else if (line.substr(0, pos) == "frame_width")
{
frame_width = std::stoi(line.substr(pos + 1, line.length()));
// Normaliza valores
if (frames_per_row == 0)
{
frames_per_row = texture->getWidth() / frame_width;
}
}
else if (line.substr(0, pos) == "frame_height")
{
frame_height = std::stoi(line.substr(pos + 1, line.length()));
}
else
{
printf("Warning: file %s, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str());
success = false;
}
}
}
}
// Cierra el fichero
printf("Closing file %s\n\n", filename.c_str());
file.close();
}
// El fichero no se puede abrir
else
{
printf("Warning: Unable to open %s file\n", filename.c_str());
success = false;
}
// Pone un valor por defecto
setPos({0, 0, frame_width, frame_height});
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)
{
mAnimation[index_animation].frames[index_frame].x = x;
mAnimation[index_animation].frames[index_frame].y = y;
mAnimation[index_animation].frames[index_frame].w = w;
mAnimation[index_animation].frames[index_frame].h = h;
animation[index_animation].frames.push_back({x, y, w, h});
}
// Establece la velocidad de una animación
void AnimatedSprite::setAnimationSpeed(Uint8 index, Uint8 speed)
// OLD - Establece el contador para todas las animaciones
void AnimatedSprite::setAnimationCounter(int value)
{
mAnimation[index].speed = speed;
}
// Establece el numero de frames de una animación
void AnimatedSprite::setAnimationNumFrames(Uint8 index, Uint8 num)
{
mAnimation[index].numFrames = num;
}
// Establece si la animación se reproduce en bucle
void AnimatedSprite::setAnimationLoop(Uint8 index, bool loop)
{
mAnimation[index].loop = loop;
}
// Establece el valor de la variable
void AnimatedSprite::setCompleted(Uint8 index, bool value)
{
mAnimation[index].completed = value;
}
// Comprueba si ha terminado la animación
bool AnimatedSprite::isCompleted(Uint8 index)
{
return mAnimation[index].completed;
}
// Devuelve el rectangulo de una animación y frame concreto
SDL_Rect AnimatedSprite::getAnimationClip(Uint8 index_animation, Uint8 index_frame)
{
return mAnimation[index_animation].frames[index_frame];
for (auto &a:animation)
{
a.counter = value;
}
}

View File

@@ -1,69 +1,84 @@
#pragma once
#include "ifdefs.h"
#include <SDL2/SDL.h>
#include "movingsprite.h"
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#ifndef ANIMATEDSPRITE_H
#define ANIMATEDSPRITE_H
#define MAX_FRAMES 30
#define MAX_ANIMATIONS 20
// Clase AnimatedSprite
class AnimatedSprite : public MovingSprite
{
private:
struct sAnimation
struct t_animation
{
SDL_Rect frames[MAX_FRAMES]; // Cada uno de los frames que componen la animación
Uint8 numFrames; // Numero de frames que componen la animación
Uint8 speed; // Velocidad de la animación
bool loop; // Indica si la animación se reproduce en bucle
bool completed; // Indica si ha finalizado la animación
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
};
sAnimation mAnimation[MAX_ANIMATIONS]; // Vector con las diferentes animaciones
Uint8 mCurrentFrame; // Frame actual
Uint16 mAnimationCounter; // Contador para las animaciones
std::vector<t_animation> animation; // Vector con las diferentes animaciones
int currentAnimation; // Animacion activa
public:
// Constructor
AnimatedSprite();
AnimatedSprite(LTexture *texture = nullptr, SDL_Renderer *renderer = nullptr, std::string file = "");
// Destructor
~AnimatedSprite();
// Iniciador
void init(LTexture *texture, SDL_Renderer *renderer);
// Calcula el frame correspondiente a la animación
void animate(int index);
// Calcula el frame correspondiente a la animación actual
void animate();
// Establece el frame actual de la animación
void setCurrentFrame(Uint8 num);
void setCurrentFrame(int num);
// Establece el valor del contador
void setAnimationCounter(Uint16 num);
// 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);
void setAnimationCounter(std::string name, int num);
// Establece la velocidad de una animación
void setAnimationSpeed(Uint8 index, Uint8 speed);
void setAnimationSpeed(std::string name, int speed);
void setAnimationSpeed(int index, int speed);
// Establece el numero de frames de una animación
void setAnimationNumFrames(Uint8 index, Uint8 num);
// Establece si la animación se reproduce en bucle
void setAnimationLoop(Uint8 index, bool loop);
// 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 setCompleted(Uint8 index, bool value);
void setAnimationCompleted(std::string name, bool value);
void setAnimationCompleted(int index, bool value);
// Comprueba si ha terminado la animación
bool isCompleted(Uint8 index);
bool animationIsCompleted();
// Devuelve el rectangulo de una animación y frame concreto
SDL_Rect getAnimationClip(Uint8 index_animation, Uint8 index_frame);
SDL_Rect getAnimationClip(std::string name, Uint8 index);
// Obtiene el indice de la animación a partir del nombre
int getIndex(std::string name);
// Carga la animación desde un fichero
bool load(std::string filePath);
// 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);
};
#endif

160
source/asset.cpp Normal file
View File

@@ -0,0 +1,160 @@
#include "asset.h"
// Constructor
Asset::Asset(std::string path)
{
executablePath = path;
longest_name = 0;
}
// Destructor
Asset::~Asset()
{
}
// Añade un elemento a la lista
void Asset::add(std::string file, enum assetType_e type, bool required)
{
item_t temp;
temp.file = executablePath + file;
temp.type = type;
temp.required = required;
fileList.push_back(temp);
const std::string filename = file.substr(file.find_last_of("\\/") + 1);
longest_name = SDL_max(longest_name, 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)
{
if (f.file.find(text) != std::string::npos)
{
return f.file;
}
}
printf("Warning: file %s not found\n", text.c_str());
return "";
}
// Comprueba que existen todos los elementos
bool Asset::check()
{
bool success = true;
printf("\n** Checking files.\n");
// 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)
{
printf("\n>> %s FILES\n", getTypeName(type).c_str());
for (auto f : fileList)
{
if ((f.required) && (f.type == type))
{
success &= checkFile(f.file);
}
}
}
}
// Resultado
if (success)
{
printf("\n** All files OK.\n\n");
}
else
{
printf("\n** A file is missing. Exiting.\n\n");
}
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(), "r+b");
if (file != nullptr)
{
result = "OK";
success = true;
SDL_RWclose(file);
}
const std::string s = "Checking file %-" + std::to_string(longest_name) + "s [" + result + "]\n";
printf(s.c_str(), filename.c_str());
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;
}
}

64
source/asset.h Normal file
View File

@@ -0,0 +1,64 @@
#pragma once
#include <SDL2/SDL.h>
#include <string>
#include <vector>
#ifndef ASSET_H
#define ASSET_H
enum assetType_e
{
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_e type; // Indica el tipo de recurso
bool required; // Indica si es un fichero que debe de existir
};
int longest_name; // Contiene la longitud del nombre de fichero mas largo
std::vector<item_t> fileList;
std::string executablePath;
// Comprueba que existe un fichero
bool checkFile(std::string path);
// Devuelve el nombre del tipo de recurso
std::string getTypeName(int type);
public:
// Constructor
Asset(std::string path);
// Destructor
~Asset();
// Añade un elemento a la lista
void add(std::string file, enum assetType_e type, bool required = true);
// 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();
};
#endif

View File

@@ -2,22 +2,11 @@
#include "balloon.h"
// Constructor
Balloon::Balloon()
Balloon::Balloon(float x, float y, Uint8 kind, float velx, float speed, Uint16 creationtimer, LTexture *texture, SDL_Renderer *renderer)
{
mSprite = new AnimatedSprite();
disable();
}
// Destructor
Balloon::~Balloon()
{
delete mSprite;
mSprite = nullptr;
}
// Inicializador
void Balloon::init(float x, float y, Uint8 kind, float velx, float speed, Uint16 creationtimer, LTexture *texture, SDL_Renderer *renderer)
{
const Uint8 NUM_FRAMES_BALLON = 10;
const Uint8 NUM_FRAMES_BALLON_POP = 12;
const Uint8 NUM_FRAMES_BALLON_BORN = 10;
@@ -55,13 +44,19 @@ void Balloon::init(float x, float y, Uint8 kind, float velx, float speed, Uint16
// Establece los frames de cada animación
for (int i = 0; i < NUM_FRAMES_BALLON; i++)
{
mSprite->setAnimationFrames(BALLOON_MOVING_ANIMATION, i, 50 + OFFSET_ORANGE_BALLOONS, 21 + (37 * i), getWidth(), getHeight());
}
for (int i = 0; i < NUM_FRAMES_BALLON_BORN; i++)
{
mSprite->setAnimationFrames(BALLOON_BORN_ANIMATION, i, 50 + OFFSET_BLUE_BALLOONS, 21 + (37 * i), getWidth(), getHeight());
}
for (int i = 0; i < NUM_FRAMES_BALLON_POP; i++)
{
mSprite->setAnimationFrames(BALLOON_POP_ANIMATION, i, 50 + OFFSET_EXPLOSIONS, 21 + (37 * i), getWidth(), getHeight());
}
break;
@@ -351,13 +346,16 @@ void Balloon::init(float x, float y, Uint8 kind, float velx, float speed, Uint16
mPosX = x;
mPosY = y;
mBouncing.enabled = false; // Si el efecto está activo
mBouncing.counter = 0; // Countador para el efecto
mBouncing.speed = 0; // Velocidad a la que transcurre el efecto
mBouncing.zoomW = 1.0f; // Zoom aplicado a la anchura
mBouncing.zoomH = 1.0f; // Zoom aplicado a la altura
mBouncing.despX = 0.0f; // Desplazamiento de pixeles en el eje X antes de pintar el objeto con zoom
// Valores para el efecto de rebote
mBouncing.enabled = false;
mBouncing.counter = 0;
mBouncing.speed = 0;
mBouncing.zoomW = 1.0f;
mBouncing.zoomH = 1.0f;
mBouncing.despX = 0.0f;
mBouncing.despY = 0.0f;
mBouncing.w = {1.10f, 1.05f, 1.00f, 0.95f, 0.90f, 0.95f, 1.00f, 1.02f, 1.05f, 1.02f};
mBouncing.h = {0.90f, 0.95f, 1.00f, 1.05f, 1.10f, 1.05f, 1.00f, 0.98f, 0.95f, 0.98f};
// Textura con los gráficos del sprite
mSprite->setTexture(texture);
@@ -404,13 +402,13 @@ void Balloon::init(float x, float y, Uint8 kind, float velx, float speed, Uint16
mKind = kind;
// Inicializa las variables para la animación
mSprite->setCurrentFrame(0);
mSprite->setAnimationCounter(0);
// mSprite->setCurrentFrame(0);
// mSprite->setAnimationCounter(0);
// Establece el numero de frames de cada animacion
mSprite->setAnimationNumFrames(BALLOON_MOVING_ANIMATION, NUM_FRAMES_BALLON);
mSprite->setAnimationNumFrames(BALLOON_POP_ANIMATION, NUM_FRAMES_BALLON_POP);
mSprite->setAnimationNumFrames(BALLOON_BORN_ANIMATION, NUM_FRAMES_BALLON_BORN);
// mSprite->setAnimationNumFrames(BALLOON_MOVING_ANIMATION, NUM_FRAMES_BALLON);
// mSprite->setAnimationNumFrames(BALLOON_POP_ANIMATION, NUM_FRAMES_BALLON_POP);
// mSprite->setAnimationNumFrames(BALLOON_BORN_ANIMATION, NUM_FRAMES_BALLON_BORN);
// Establece la velocidad de cada animación
mSprite->setAnimationSpeed(BALLOON_MOVING_ANIMATION, 10);
@@ -426,19 +424,22 @@ void Balloon::init(float x, float y, Uint8 kind, float velx, float speed, Uint16
mSprite->setSpriteClip(mSprite->getAnimationClip(0, 0));
}
// Destructor
Balloon::~Balloon()
{
delete mSprite;
mSprite = nullptr;
}
// Centra el globo en la posición X
void Balloon::allignTo(int x)
{
mPosX = float(x - (mWidth / 2));
if (mPosX < PLAY_AREA_LEFT)
{
mPosX = PLAY_AREA_LEFT + 1;
}
else if ((mPosX + mWidth) > PLAY_AREA_RIGHT)
{
mPosX = float(PLAY_AREA_RIGHT - mWidth - 1);
}
// Posición X,Y del sprite
mSprite->setPosX(getPosX());
@@ -531,10 +532,12 @@ void Balloon::move()
}
/*
Para aplicar la gravedad, el diseño original la aplicaba en cada iteración del bucle
Al añadir el modificador de velocidad se reduce la distancia que recorre el objeto y por
tanto recibe mas gravedad. Para solucionarlo se va a aplicar la gravedad cuando se haya
recorrido una distancia igual a la velocidad en Y, que era el cálculo inicial
*/
// Incrementa la variable que calcula la distancia acumulada en Y
@@ -550,11 +553,6 @@ void Balloon::move()
mVelY += mGravity;
std::min(mVelY, mMaxVelY);
}
// Aplica la gravedad al objeto
//if (mVelY > mMaxVelY)
// mVelY = mMaxVelY;
//else if (mCounter % 1 == 0)
// mVelY += mGravity;
// Actualiza la posición del sprite
mSprite->setPosX(getPosX());
@@ -603,7 +601,7 @@ void Balloon::disable()
void Balloon::pop()
{
setPopping(true);
mSprite->setAnimationCounter(0);
mSprite->setAnimationCounter(0);
mSprite->disableRotate();
setTimeToLive(120);
setStop(true);
@@ -635,9 +633,9 @@ void Balloon::updateState()
{
setInvulnerable(true);
setStop(true);
if (mSprite->isCompleted(BALLOON_POP_ANIMATION))
if (mSprite->animationIsCompleted())
{
mSprite->setCompleted(BALLOON_POP_ANIMATION, false);
mSprite->setAnimationCompleted(BALLOON_POP_ANIMATION, false);
mTimeToLive = 0;
disable();
}
@@ -679,12 +677,6 @@ void Balloon::updateState()
updateColliders();
}
// Hace visible el globo de forma intermitente
//if (mCreationCounter > 100)
// setVisible(mCreationCounter / 10 % 2 == 0);
//else
// setVisible(mCreationCounter / 5 % 2 == 0);
mCreationCounter--;
}
// El contador ha llegado a cero
@@ -709,10 +701,14 @@ void Balloon::updateState()
{
// Si está detenido, reduce el contador
if (mStoppedCounter > 0)
{
mStoppedCounter--;
}
// Si el contador ha llegado a cero, ya no está detenido
else if (!isPopping()) // Quitarles el estado "detenido" si no estan explosionandoxq
else if (!isPopping())
{ // Quitarles el estado "detenido" si no estan explosionando
setStop(false);
}
}
}
@@ -721,11 +717,19 @@ void Balloon::updateAnimation()
{
// Establece el frame de animación
if (isPopping())
mSprite->animate(BALLOON_POP_ANIMATION);
{
mSprite->setCurrentAnimation(BALLOON_POP_ANIMATION);
}
else if (isBeingCreated())
mSprite->animate(BALLOON_BORN_ANIMATION);
{
mSprite->setCurrentAnimation(BALLOON_BORN_ANIMATION);
}
else
mSprite->animate(BALLOON_MOVING_ANIMATION);
{
mSprite->setCurrentAnimation(BALLOON_MOVING_ANIMATION);
}
mSprite->animate();
}
// Comprueba si el globo está habilitado
@@ -910,7 +914,6 @@ circle_t &Balloon::getCollider()
// Alinea el circulo de colisión con la posición del objeto globo
void Balloon::updateColliders()
{
// Align collider to center of balloon
mCollider.x = Uint16(mPosX + mCollider.r);
mCollider.y = mPosY + mCollider.r;
}
@@ -940,6 +943,7 @@ void Balloon::bounceStart()
mBouncing.despX = 0;
mBouncing.despY = 0;
}
void Balloon::bounceStop()
{
mBouncing.enabled = false;
@@ -951,6 +955,7 @@ void Balloon::bounceStop()
mBouncing.despX = 0.0f;
mBouncing.despY = 0.0f;
}
void Balloon::updateBounce()
{
if (mBouncing.enabled)

View File

@@ -1,16 +1,90 @@
#pragma once
#include <SDL2/SDL.h>
#include "utils.h"
#include "animatedsprite.h"
#include <vector>
#ifndef BALLOON_H
#define BALLOON_H
// Cantidad de elementos del vector con los valores de la deformación del globo al rebotar
#define MAX_BOUNCE 10
// Clase globo
// Tipos de globo
#define BALLOON_1 1
#define BALLOON_2 2
#define BALLOON_3 3
#define BALLOON_4 4
#define HEXAGON_1 5
#define HEXAGON_2 6
#define HEXAGON_3 7
#define HEXAGON_4 8
#define POWER_BALL 9
// Puntos de globo
#define BALLOON_SCORE_1 50
#define BALLOON_SCORE_2 100
#define BALLOON_SCORE_3 200
#define BALLOON_SCORE_4 400
// Tamaños de globo
#define BALLOON_SIZE_1 1
#define BALLOON_SIZE_2 2
#define BALLOON_SIZE_3 3
#define BALLOON_SIZE_4 4
// Clases de globo
#define BALLOON_CLASS 0
#define HEXAGON_CLASS 1
// Velocidad del globo
#define BALLOON_VELX_POSITIVE 0.7f
#define BALLOON_VELX_NEGATIVE -0.7f
// Indice para las animaciones de los globos
#define BALLOON_MOVING_ANIMATION 0
#define BALLOON_POP_ANIMATION 1
#define BALLOON_BORN_ANIMATION 2
// Cantidad posible de globos
#define MAX_BALLOONS 100
// Velocidades a las que se mueven los globos
#define BALLOON_SPEED_1 0.60f
#define BALLOON_SPEED_2 0.70f
#define BALLOON_SPEED_3 0.80f
#define BALLOON_SPEED_4 0.90f
#define BALLOON_SPEED_5 1.00f
// Tamaño de los globos
#define BALLOON_WIDTH_1 8
#define BALLOON_WIDTH_2 13
#define BALLOON_WIDTH_3 21
#define BALLOON_WIDTH_4 37
// PowerBall
#define POWERBALL_SCREENPOWER_MINIMUM 10
#define POWERBALL_COUNTER 8
// Clase Balloon
class Balloon
{
private:
// Estructura para las variables para el efecto de los rebotes
struct bouncing
{
bool enabled; // Si el efecto está activo
Uint8 counter; // Countador para el efecto
Uint8 speed; // Velocidad a la que transcurre el efecto
float zoomW; // Zoom aplicado a la anchura
float zoomH; // Zoom aplicado a la altura
float despX; // Desplazamiento de pixeles en el eje X antes de pintar el objeto con zoom
float despY; // Desplazamiento de pixeles en el eje Y antes de pintar el objeto con zoom
std::vector<float> w; // Vector con los valores de zoom para el ancho del globo
std::vector<float> h; // Vector con los valores de zoom para el alto del globo
};
float mPosX; // Posición en el eje X
float mPosY; // Posición en el eje Y
Uint8 mWidth; // Ancho
@@ -41,43 +115,42 @@ private:
float mSpeed; // Velocidad a la que se mueven los globos
Uint8 mSize; // Tamaño del globo
Uint8 mPower; // Cantidad de poder que alberga el globo
bouncing mBouncing; // Contiene las variables para el efecto de rebote
struct bouncing // Estructura para las variables para el efecto de los rebotes
{
bool enabled; // Si el efecto está activo
Uint8 counter; // Countador para el efecto
Uint8 speed; // Velocidad a la que transcurre el efecto
float zoomW; // Zoom aplicado a la anchura
float zoomH; // Zoom aplicado a la altura
float despX; // Desplazamiento de pixeles en el eje X antes de pintar el objeto con zoom
float despY; // Desplazamiento de pixeles en el eje Y antes de pintar el objeto con zoom
// Alinea el circulo de colisión con la posición del objeto globo
void updateColliders();
// Vector con los valores de zoom para el ancho y alto del globo
float w[MAX_BOUNCE] = {1.10f, 1.05f, 1.00f, 0.95f, 0.90f, 0.95f, 1.00f, 1.02f, 1.05f, 1.02f};
float h[MAX_BOUNCE] = {0.90f, 0.95f, 1.00f, 1.05f, 1.10f, 1.05f, 1.00f, 0.98f, 0.95f, 0.98f};
};
bouncing mBouncing;
// Activa el efecto
void bounceStart();
void updateColliders(); // Alinea el circulo de colisión con la posición del objeto globo
void bounceStart(); // Activa el efecto
void bounceStop(); // Detiene el efecto
void updateBounce(); // Aplica el efecto
void updateState(); // Actualiza los estados del globo
void updateAnimation(); // Establece la animación correspondiente
void setBeingCreated(bool state); // Establece el valor de la variable
void setTimeToLive(Uint16 time); // Establece el valor de la variable
Uint16 getTimeToLive(); // Obtiene del valor de la variable
// Detiene el efecto
void bounceStop();
// Aplica el efecto
void updateBounce();
// Actualiza los estados del globo
void updateState();
// Establece la animación correspondiente
void updateAnimation();
// Establece el valor de la variable
void setBeingCreated(bool state);
// Establece el valor de la variable
void setTimeToLive(Uint16 time);
// Obtiene del valor de la variable
Uint16 getTimeToLive();
public:
// Constructor
Balloon();
Balloon(float x, float y, Uint8 kind, float velx, float speed, Uint16 creationtimer, LTexture *texture, SDL_Renderer *renderer);
// Destructor
~Balloon();
// Inicializador
void init(float x, float y, Uint8 kind, float velx, float speed, Uint16 creationtimer, LTexture *texture, SDL_Renderer *renderer);
// Centra el globo en la posición X
void allignTo(int x);

View File

@@ -11,7 +11,6 @@ Bullet::Bullet()
// Destructor
Bullet::~Bullet()
{
//init(0, 0, NO_KIND, nullptr, nullptr);
delete mSprite;
mSprite = nullptr;
}
@@ -107,7 +106,7 @@ void Bullet::render()
Uint8 Bullet::move()
{
// Variable con el valor de retorno
Uint8 msg = MSG_OK;
Uint8 msg = BULLET_MOVE_OK;
// Mueve el objeto a su nueva posición
mPosX += mVelX;
@@ -119,7 +118,7 @@ Uint8 Bullet::move()
mKind = NO_KIND;
// Mensaje de salida
msg = MSG_BULLET_OUT;
msg = BULLET_MOVE_OUT;
}
// Mueve el objeto a su nueva posición en vertical
@@ -132,7 +131,7 @@ Uint8 Bullet::move()
mKind = NO_KIND;
// Mensaje de salida
msg = MSG_BULLET_OUT;
msg = BULLET_MOVE_OUT;
}
// Actualiza la posición del sprite
@@ -145,18 +144,6 @@ Uint8 Bullet::move()
return msg;
}
#ifdef TEST
void Bullet::testMove()
{
// Update sprite position
mSprite->setPosX(mPosX);
mSprite->setPosY(mPosY);
// Update circle colliders
shiftColliders();
}
#endif
// Deshabilita el objeto
void Bullet::erase()
{
@@ -167,13 +154,9 @@ void Bullet::erase()
bool Bullet::isActive()
{
if (mKind == NO_KIND)
{
return false;
}
else
{
return true;
}
}
// Obtiene el valor de la variable

View File

@@ -1,11 +1,22 @@
#pragma once
#include <SDL2/SDL.h>
#include "utils.h"
#include "sprite.h"
#ifndef BULLET_H
#define BULLET_H
// Clase bala
// Tipos de bala
#define BULLET_UP 1
#define BULLET_LEFT 2
#define BULLET_RIGHT 3
// Tipos de retorno de la funcion move de la bala
#define BULLET_MOVE_OK 0
#define BULLET_MOVE_OUT 1
// Clase Bullet
class Bullet
{
private:

View File

@@ -1,4 +1,6 @@
#pragma once
#include <SDL2/SDL.h>
#include "sprite.h"
#ifndef COFFEEDROP_H

View File

@@ -1,91 +1,25 @@
#pragma once
#include "ifdefs.h"
#include <SDL2/SDL.h>
#include "utils.h"
#include "lang.h"
#include <string>
#ifndef CONST_H
#define CONST_H
// Textos
#define WINDOW_CAPTION "Coffee Crisis"
#define TEXT_COPYRIGHT "@2020,2021 JailDesigner (v2.0.2)"
// Recursos
#define BINFILE_SCORE 0
#define BINFILE_DEMO 1
#define BINFILE_CONFIG 2
#define TOTAL_BINFILE 3
#define MUSIC_INTRO 0
#define MUSIC_PLAYING 1
#define MUSIC_TITLE 2
#define TOTAL_MUSIC 3
#define SOUND_BALLOON 0
#define SOUND_BUBBLE1 1
#define SOUND_BUBBLE2 2
#define SOUND_BUBBLE3 3
#define SOUND_BUBBLE4 4
#define SOUND_BULLET 5
#define SOUND_COFFEE_OUT 6
#define SOUND_HISCORE 7
#define SOUND_ITEM_DROP 8
#define SOUND_ITEM_PICKUP 9
#define SOUND_MENU_CANCEL 10
#define SOUND_MENU_MOVE 11
#define SOUND_MENU_SELECT 12
#define SOUND_PLAYER_COLLISION 13
#define SOUND_STAGE_CHANGE 14
#define SOUND_TITLE 15
#define SOUND_CLOCK 16
#define SOUND_POWERBALL 17
#define TOTAL_SOUND 18
#define TEXTURE_BALLOON 0
#define TEXTURE_BULLET 1
#define TEXTURE_FONT_BLACK 2
#define TEXTURE_FONT_BLACK_X2 3
#define TEXTURE_FONT_NOKIA 4
#define TEXTURE_FONT_WHITE 5
#define TEXTURE_FONT_WHITE_X2 6
#define TEXTURE_GAME_BG 7
#define TEXTURE_GAME_TEXT 8
#define TEXTURE_INTRO 9
#define TEXTURE_ITEMS 10
#define TEXTURE_LOGO 11
#define TEXTURE_MENU 12
#define TEXTURE_PLAYER_BODY 13
#define TEXTURE_PLAYER_DEATH 14
#define TEXTURE_PLAYER_LEGS 15
#define TEXTURE_TITLE 16
#define TOTAL_TEXTURE 17
// Tamaño de bloque
#define BLOCK 8
#define HALF_BLOCK BLOCK / 2
// Tamaño de la pantalla real
#define SCREEN_WIDTH 256
const int SCREEN_HEIGHT = SCREEN_WIDTH * 3 / 4;
// Tamaño de la pantalla que se muestra
const int VIEW_WIDTH = SCREEN_WIDTH * 3;
const int VIEW_HEIGHT = SCREEN_HEIGHT * 3;
// Cantidad de enteros a escribir en los ficheros de datos
#define TOTAL_SCORE_DATA 3
#define TOTAL_DEMO_DATA 2000
// Tamaño de la pantalla de juego
#define GAME_WIDTH 256
#define GAME_HEIGHT 192
// Zona de juego
const int PLAY_AREA_TOP = (0 * BLOCK);
const int PLAY_AREA_BOTTOM = SCREEN_HEIGHT - (4 * BLOCK);
const int PLAY_AREA_BOTTOM = GAME_HEIGHT - (4 * BLOCK);
const int PLAY_AREA_LEFT = (0 * BLOCK);
const int PLAY_AREA_RIGHT = SCREEN_WIDTH - (0 * BLOCK);
const int PLAY_AREA_RIGHT = GAME_WIDTH - (0 * BLOCK);
const int PLAY_AREA_WIDTH = PLAY_AREA_RIGHT - PLAY_AREA_LEFT;
const int PLAY_AREA_HEIGHT = PLAY_AREA_BOTTOM - PLAY_AREA_TOP;
const int PLAY_AREA_CENTER_X = PLAY_AREA_LEFT + (PLAY_AREA_WIDTH / 2);
@@ -96,54 +30,12 @@ const int PLAY_AREA_FIRST_QUARTER_Y = PLAY_AREA_HEIGHT / 4;
const int PLAY_AREA_THIRD_QUARTER_Y = (PLAY_AREA_HEIGHT / 4) * 3;
// Anclajes de pantalla
const int SCREEN_CENTER_X = SCREEN_WIDTH / 2;
const int SCREEN_FIRST_QUARTER_X = SCREEN_WIDTH / 4;
const int SCREEN_THIRD_QUARTER_X = (SCREEN_WIDTH / 4) * 3;
const int SCREEN_CENTER_Y = SCREEN_HEIGHT / 2;
const int SCREEN_FIRST_QUARTER_Y = SCREEN_HEIGHT / 4;
const int SCREEN_THIRD_QUARTER_Y = (SCREEN_HEIGHT / 4) * 3;
// Opciones de menu
#define MENU_NO_OPTION -1
#define MENU_OPTION_START 0
#define MENU_OPTION_QUIT 1
#define MENU_OPTION_TOTAL 2
// Tipos de fondos para el menu
#define MENU_BACKGROUND_TRANSPARENT 0
#define MENU_BACKGROUND_SOLID 1
// Estados del jugador
#define PLAYER_STATUS_WALKING_LEFT 0
#define PLAYER_STATUS_WALKING_RIGHT 1
#define PLAYER_STATUS_WALKING_STOP 2
#define PLAYER_STATUS_FIRING_UP 0
#define PLAYER_STATUS_FIRING_LEFT 1
#define PLAYER_STATUS_FIRING_RIGHT 2
#define PLAYER_STATUS_FIRING_NO 3
#define PLAYER_ANIMATION_LEGS_WALKING_LEFT 0
#define PLAYER_ANIMATION_LEGS_WALKING_RIGHT 1
#define PLAYER_ANIMATION_LEGS_WALKING_STOP 2
#define PLAYER_ANIMATION_BODY_WALKING_LEFT 0
#define PLAYER_ANIMATION_BODY_FIRING_LEFT 1
#define PLAYER_ANIMATION_BODY_WALKING_RIGHT 2
#define PLAYER_ANIMATION_BODY_FIRING_RIGHT 3
#define PLAYER_ANIMATION_BODY_WALKING_STOP 4
#define PLAYER_ANIMATION_BODY_FIRING_UP 5
#define PLAYER_ANIMATION_HEAD_WALKING_LEFT 0
#define PLAYER_ANIMATION_HEAD_FIRING_LEFT 1
#define PLAYER_ANIMATION_HEAD_WALKING_RIGHT 2
#define PLAYER_ANIMATION_HEAD_FIRING_RIGHT 3
#define PLAYER_ANIMATION_HEAD_WALKING_STOP 4
#define PLAYER_ANIMATION_HEAD_FIRING_UP 5
// Variables del jugador
#define PLAYER_INVULNERABLE_COUNTER 200
#define PLAYER_POWERUP_COUNTER 1500
const int SCREEN_CENTER_X = GAME_WIDTH / 2;
const int SCREEN_FIRST_QUARTER_X = GAME_WIDTH / 4;
const int SCREEN_THIRD_QUARTER_X = (GAME_WIDTH / 4) * 3;
const int SCREEN_CENTER_Y = GAME_HEIGHT / 2;
const int SCREEN_FIRST_QUARTER_Y = GAME_HEIGHT / 4;
const int SCREEN_THIRD_QUARTER_Y = (GAME_HEIGHT / 4) * 3;
// Secciones del programa
#define PROG_SECTION_LOGO 0
@@ -162,197 +54,17 @@ const int SCREEN_THIRD_QUARTER_Y = (SCREEN_HEIGHT / 4) * 3;
#define TITLE_SECTION_3 5
#define TITLE_SECTION_INSTRUCTIONS 6
// Modo para las instrucciones
#define INSTRUCTIONS_MODE_MANUAL 0
#define INSTRUCTIONS_MODE_AUTO 1
// Estados de cada elemento que pertenece a un evento
#define EVENT_WAITING 1
#define EVENT_RUNNING 2
#define EVENT_COMPLETED 3
// Cantidad de eventos de la intro
#define INTRO_TOTAL_BITMAPS 6
#define INTRO_TOTAL_TEXTS 9
const int INTRO_TOTAL_EVENTS = INTRO_TOTAL_BITMAPS + INTRO_TOTAL_TEXTS;
// Cantidad de eventos de la pantalla de titulo
#define TITLE_TOTAL_EVENTS 2
// Relaciones de Id con nomnbres
#define BITMAP0 0
#define BITMAP1 1
#define BITMAP2 2
#define BITMAP3 3
#define BITMAP4 4
#define BITMAP5 5
#define TEXT0 6
#define TEXT1 7
#define TEXT2 8
#define TEXT3 9
#define TEXT4 10
#define TEXT5 11
#define TEXT6 12
#define TEXT7 13
#define TEXT8 14
// Anclajes para el marcador de puntos
const int SCORE_WORD_X = (SCREEN_WIDTH / 4) - ((5 * BLOCK) / 2);
const int SCORE_WORD_Y = SCREEN_HEIGHT - (3 * BLOCK) + 2;
const int SCORE_NUMBER_X = (SCREEN_WIDTH / 4) - ((6 * BLOCK) / 2);
const int SCORE_NUMBER_Y = SCREEN_HEIGHT - (2 * BLOCK) + 2;
const int HISCORE_WORD_X = ((SCREEN_WIDTH / 4) * 3) - ((8 * BLOCK) / 2);
const int HISCORE_WORD_Y = SCREEN_HEIGHT - (3 * BLOCK) + 2;
const int HISCORE_NUMBER_X = ((SCREEN_WIDTH / 4) * 3) - ((6 * BLOCK) / 2);
const int HISCORE_NUMBER_Y = SCREEN_HEIGHT - (2 * BLOCK) + 2;
const int MULTIPLIER_WORD_X = (SCREEN_WIDTH / 2) - ((4 * BLOCK) / 2);
const int MULTIPLIER_WORD_Y = SCREEN_HEIGHT - (3 * BLOCK) + 2;
const int MULTIPLIER_NUMBER_X = (SCREEN_WIDTH / 2) - ((3 * BLOCK) / 2);
const int MULTIPLIER_NUMBER_Y = SCREEN_HEIGHT - (2 * BLOCK) + 2;
// Ningun tipo
#define NO_KIND 0
// Tipos de globo
#define BALLOON_1 1
#define BALLOON_2 2
#define BALLOON_3 3
#define BALLOON_4 4
#define HEXAGON_1 5
#define HEXAGON_2 6
#define HEXAGON_3 7
#define HEXAGON_4 8
#define POWER_BALL 9
// Puntos de globo
#define BALLOON_SCORE_1 50
#define BALLOON_SCORE_2 100
#define BALLOON_SCORE_3 200
#define BALLOON_SCORE_4 400
// Tamaños de globo
#define BALLOON_SIZE_1 1
#define BALLOON_SIZE_2 2
#define BALLOON_SIZE_3 3
#define BALLOON_SIZE_4 4
// Clases de globo
#define BALLOON_CLASS 0
#define HEXAGON_CLASS 1
// Velocidad del globo
#define BALLOON_VELX_POSITIVE 0.7f
#define BALLOON_VELX_NEGATIVE -0.7f
// Indice para las animaciones de los globos
#define BALLOON_MOVING_ANIMATION 0
#define BALLOON_POP_ANIMATION 1
#define BALLOON_BORN_ANIMATION 2
// Cantidad posible de globos
#define MAX_BALLOONS 100
// Velocidades a las que se mueven los enemigos
#define BALLOON_SPEED_1 0.60f
#define BALLOON_SPEED_2 0.70f
#define BALLOON_SPEED_3 0.80f
#define BALLOON_SPEED_4 0.90f
#define BALLOON_SPEED_5 1.00f
// Tamaño de los globos
#define BALLOON_WIDTH_1 8
#define BALLOON_WIDTH_2 13
#define BALLOON_WIDTH_3 21
#define BALLOON_WIDTH_4 37
// PowerBall
#define POWERBALL_SCREENPOWER_MINIMUM 10
#define POWERBALL_COUNTER 8
// Tipos de bala
#define BULLET_UP 1
#define BULLET_LEFT 2
#define BULLET_RIGHT 3
// Cantidad posible de globos
#define MAX_BULLETS 50
// Tipos de objetos
#define ITEM_POINTS_1_DISK 1
#define ITEM_POINTS_2_GAVINA 2
#define ITEM_POINTS_3_PACMAR 3
#define ITEM_CLOCK 4
#define ITEM_COFFEE 5
#define ITEM_POWER_BALL 6
#define ITEM_COFFEE_MACHINE 7
// Porcentaje de aparición de los objetos
#define ITEM_POINTS_1_DISK_ODDS 10
#define ITEM_POINTS_2_GAVINA_ODDS 6
#define ITEM_POINTS_3_PACMAR_ODDS 3
#define ITEM_CLOCK_ODDS 5
#define ITEM_COFFEE_ODDS 5
#define ITEM_POWER_BALL_ODDS 0
#define ITEM_COFFEE_MACHINE_ODDS 4
// Cantidad de objetos simultaneos
#define MAX_ITEMS 10
// Valores para las variables asociadas a los objetos
#define REMAINING_EXPLOSIONS 3
#define REMAINING_EXPLOSIONS_COUNTER 50
#define TIME_STOPPED_COUNTER 300
// Estados de entrada
#define NO_INPUT 0
#define INPUT_FIRE_LEFT 7
#define INPUT_FIRE_UP 8
#define INPUT_FIRE_RIGHT 9
#define INPUT_PAUSE 10
// Zona muerta del mando analógico
#define JOYSTICK_DEAD_ZONE 8000
// Tipos de mensajes para el retorno de las funciones
#define MSG_OK 0
#define MSG_BULLET_OUT 1
// Tipos de texto
#define TEXT_FIXED 0
#define TEXT_VARIABLE 1
// Cantidad de elementos del vector de SmartSprites
#define MAX_SMART_SPRITES 10
// Cantidad máxima de gotas de café
#define MAX_COFFEE_DROPS 100
// Contadores
#define TITLE_COUNTER 800
#define STAGE_COUNTER 200
#define INSTRUCTIONS_COUNTER 600
#define DEATH_COUNTER 350
#define SHAKE_COUNTER 10
#define HELP_COUNTER 1000
// Colores
const color_t bgColor = {0x27, 0x27, 0x36};
const color_t noColor = {0xFF, 0xFF, 0xFF};
const color_t shdwTxtColor = {0x43, 0x43, 0x4F};
// Formaciones enemigas
#define NUMBER_OF_ENEMY_FORMATIONS 100
#define MAX_NUMBER_OF_ENEMIES_IN_A_FORMATION 50
// Dificultad del juego
#define DIFFICULTY_EASY 0
#define DIFFICULTY_NORMAL 1
#define DIFFICULTY_HARD 2
// Tipo de filtro
#define FILTER_NEAREST 0
#define FILTER_LINEAL 1
#endif

View File

@@ -3,67 +3,42 @@
#include "director.h"
#include <iostream>
#include <string>
#ifdef __MIPSEL__
#include <sys/stat.h>
#include <dirent.h>
#endif
// Constructor
Director::Director(std::string path)
{
// Inicializa la ruta
setExecutablePath(path);
// Crea el objeto que controla los ficheros de recursos
asset = new Asset(path.substr(0, path.find_last_of("\\/")) + "../");
// Establece la lista de ficheros
setFileList();
// Si falta algún fichero no inicies el programa
Uint8 section = PROG_SECTION_LOGO;
if (!checkFileList())
if (!setFileList())
{// Si falta algún fichero no inicies el programa
section = PROG_SECTION_QUIT;
// Inicializa el objeto de idioma
mLang = new Lang(mFileList);
// Crea el puntero a la estructura y carga el fichero de configuración
mOptions = new options_t;
if (!loadConfigFile())
{
mOptions->fullScreenMode = 0;
mOptions->windowSize = 3;
mOptions->language = en_UK;
mOptions->difficulty = DIFFICULTY_NORMAL;
mOptions->input[0].deviceType = INPUT_USE_KEYBOARD;
mOptions->input[1].deviceType = INPUT_USE_GAMECONTROLLER;
mOptions->filter = FILTER_NEAREST;
mOptions->vSync = true;
}
// Crea el objeto de idioma
lang = new Lang(asset);
// Crea el puntero a la estructura y carga el fichero de configuración
options = new options_t;
loadConfigFile();
// Crea los objetos
mInput = new Input(mFileList[53]);
input = new Input(asset->get("controllerdb.txt"));
// Inicializa SDL
initSDL();
// Crea el objeto para dibujar en pantalla (Requiere initSDL)
screen = new Screen(window, renderer, options, GAME_WIDTH, GAME_HEIGHT);
// Inicializa JailAudio
initJailAudio();
// Aplica las opciones
SDL_SetWindowFullscreen(mWindow, mOptions->fullScreenMode);
SDL_SetWindowSize(mWindow, SCREEN_WIDTH * mOptions->windowSize, SCREEN_HEIGHT * mOptions->windowSize);
mLang->setLang(mOptions->language);
#ifdef __MIPSEL__
DIR *dir = opendir("/media/data/local/home/.coffee_crisis");
if (dir)
{
closedir(dir);
}
else if (ENOENT == errno)
{
int status = mkdir("/media/data/local/home/.coffee_crisis", 755);
}
#endif
lang->setLang(options->language);
// Inicializa el resto de variables
init(section);
@@ -73,19 +48,25 @@ Director::~Director()
{
saveConfigFile();
delete mInput;
mInput = nullptr;
delete asset;
asset = nullptr;
delete mLang;
mLang = nullptr;
delete input;
input = nullptr;
delete mOptions;
mOptions = nullptr;
delete screen;
screen = nullptr;
SDL_DestroyRenderer(mRenderer);
SDL_DestroyWindow(mWindow);
mRenderer = nullptr;
mWindow = nullptr;
delete lang;
lang = nullptr;
delete options;
options = nullptr;
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
renderer = nullptr;
window = nullptr;
SDL_Quit();
}
@@ -94,42 +75,36 @@ Director::~Director()
void Director::init(Uint8 name)
{
// Sección
mSection.name = name;
mSection.subsection = 0;
section.name = name;
section.subsection = 0;
// Textos
mLang->setLang(mOptions->language);
lang->setLang(options->language);
// Controles
mInput->bindKey(INPUT_UP, SDL_SCANCODE_UP);
mInput->bindKey(INPUT_DOWN, SDL_SCANCODE_DOWN);
mInput->bindKey(INPUT_LEFT, SDL_SCANCODE_LEFT);
mInput->bindKey(INPUT_RIGHT, SDL_SCANCODE_RIGHT);
mInput->bindKey(INPUT_ACCEPT, SDL_SCANCODE_RETURN);
mInput->bindKey(INPUT_CANCEL, SDL_SCANCODE_ESCAPE);
#ifdef __MIPSEL__
mInput->bindKey(INPUT_BUTTON_1, SDL_SCANCODE_LSHIFT);
mInput->bindKey(INPUT_BUTTON_2, SDL_SCANCODE_SPACE);
mInput->bindKey(INPUT_BUTTON_3, SDL_SCANCODE_LCTRL);
#else
mInput->bindKey(INPUT_BUTTON_1, SDL_SCANCODE_Q);
mInput->bindKey(INPUT_BUTTON_2, SDL_SCANCODE_W);
mInput->bindKey(INPUT_BUTTON_3, SDL_SCANCODE_E);
#endif
mInput->bindKey(INPUT_BUTTON_PAUSE, SDL_SCANCODE_ESCAPE); // PAUSE
mInput->bindKey(INPUT_BUTTON_ESCAPE, SDL_SCANCODE_ESCAPE); // ESCAPE
input->bindKey(INPUT_UP, SDL_SCANCODE_UP);
input->bindKey(INPUT_DOWN, SDL_SCANCODE_DOWN);
input->bindKey(INPUT_LEFT, SDL_SCANCODE_LEFT);
input->bindKey(INPUT_RIGHT, SDL_SCANCODE_RIGHT);
input->bindKey(INPUT_ACCEPT, SDL_SCANCODE_RETURN);
input->bindKey(INPUT_CANCEL, SDL_SCANCODE_ESCAPE);
input->bindKey(INPUT_BUTTON_1, SDL_SCANCODE_Q);
input->bindKey(INPUT_BUTTON_2, SDL_SCANCODE_W);
input->bindKey(INPUT_BUTTON_3, SDL_SCANCODE_E);
input->bindKey(INPUT_BUTTON_PAUSE, SDL_SCANCODE_ESCAPE); // PAUSE
input->bindKey(INPUT_BUTTON_ESCAPE, SDL_SCANCODE_ESCAPE); // ESCAPE
mInput->bindGameControllerButton(INPUT_UP, SDL_CONTROLLER_BUTTON_DPAD_UP);
mInput->bindGameControllerButton(INPUT_DOWN, SDL_CONTROLLER_BUTTON_DPAD_DOWN);
mInput->bindGameControllerButton(INPUT_LEFT, SDL_CONTROLLER_BUTTON_DPAD_LEFT);
mInput->bindGameControllerButton(INPUT_RIGHT, SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
mInput->bindGameControllerButton(INPUT_ACCEPT, SDL_CONTROLLER_BUTTON_B);
mInput->bindGameControllerButton(INPUT_CANCEL, SDL_CONTROLLER_BUTTON_A);
mInput->bindGameControllerButton(INPUT_BUTTON_1, SDL_CONTROLLER_BUTTON_X);
mInput->bindGameControllerButton(INPUT_BUTTON_2, SDL_CONTROLLER_BUTTON_Y);
mInput->bindGameControllerButton(INPUT_BUTTON_3, SDL_CONTROLLER_BUTTON_B);
mInput->bindGameControllerButton(INPUT_BUTTON_PAUSE, SDL_CONTROLLER_BUTTON_GUIDE); // PAUSE
mInput->bindGameControllerButton(INPUT_BUTTON_ESCAPE, SDL_CONTROLLER_BUTTON_GUIDE); // ESCAPE
input->bindGameControllerButton(INPUT_UP, SDL_CONTROLLER_BUTTON_DPAD_UP);
input->bindGameControllerButton(INPUT_DOWN, SDL_CONTROLLER_BUTTON_DPAD_DOWN);
input->bindGameControllerButton(INPUT_LEFT, SDL_CONTROLLER_BUTTON_DPAD_LEFT);
input->bindGameControllerButton(INPUT_RIGHT, SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
input->bindGameControllerButton(INPUT_ACCEPT, SDL_CONTROLLER_BUTTON_B);
input->bindGameControllerButton(INPUT_CANCEL, SDL_CONTROLLER_BUTTON_A);
input->bindGameControllerButton(INPUT_BUTTON_1, SDL_CONTROLLER_BUTTON_X);
input->bindGameControllerButton(INPUT_BUTTON_2, SDL_CONTROLLER_BUTTON_Y);
input->bindGameControllerButton(INPUT_BUTTON_3, SDL_CONTROLLER_BUTTON_B);
input->bindGameControllerButton(INPUT_BUTTON_PAUSE, SDL_CONTROLLER_BUTTON_GUIDE); // PAUSE
input->bindGameControllerButton(INPUT_BUTTON_ESCAPE, SDL_CONTROLLER_BUTTON_GUIDE); // ESCAPE
}
// Inicializa JailAudio
@@ -145,8 +120,7 @@ bool Director::initSDL()
bool success = true;
// Inicializa SDL
//if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER | SDL_INIT_AUDIO) < 0)
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER | SDL_INIT_AUDIO) < 0)
{
printf("SDL could not initialize!\nSDL Error: %s\n", SDL_GetError());
success = false;
@@ -156,15 +130,15 @@ bool Director::initSDL()
// Inicia el generador de numeros aleatorios
std::srand(static_cast<unsigned int>(SDL_GetTicks()));
// Establece el filtro de la textura a nearest
if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, std::to_string(mOptions->filter).c_str()))
// Establece el filtro de la textura
if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, std::to_string(options->filter).c_str()))
{
printf("Warning: Nearest texture filtering not enabled!\n");
}
// Crea la ventana
mWindow = SDL_CreateWindow(WINDOW_CAPTION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, VIEW_WIDTH, VIEW_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI);
if (mWindow == NULL)
window = SDL_CreateWindow(WINDOW_CAPTION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, options->screenWidth * options->windowSize, options->screenHeight * options->windowSize, SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI);
if (window == nullptr)
{
printf("Window could not be created!\nSDL Error: %s\n", SDL_GetError());
success = false;
@@ -172,12 +146,12 @@ bool Director::initSDL()
else
{
// Crea un renderizador para la ventana. El vsync se activa en funcion de las opciones
if (mOptions->vSync)
mRenderer = SDL_CreateRenderer(mWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (options->vSync)
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
else
mRenderer = SDL_CreateRenderer(mWindow, -1, SDL_RENDERER_ACCELERATED);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (mRenderer == NULL)
if (renderer == nullptr)
{
printf("Renderer could not be created!\nSDL Error: %s\n", SDL_GetError());
success = false;
@@ -185,13 +159,13 @@ bool Director::initSDL()
else
{
// Inicializa el color de renderizado
SDL_SetRenderDrawColor(mRenderer, 0x00, 0x00, 0x00, 0xFF);
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
// Establece el tamaño del buffer de renderizado
SDL_RenderSetLogicalSize(mRenderer, SCREEN_WIDTH, SCREEN_HEIGHT);
SDL_RenderSetLogicalSize(renderer, options->screenWidth, options->screenHeight);
// Establece el modo de mezcla
SDL_SetRenderDrawBlendMode(mRenderer, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
}
}
}
@@ -201,192 +175,129 @@ bool Director::initSDL()
}
// Crea el indice de ficheros
void Director::setFileList()
bool Director::setFileList()
{
// Inicializa el vector
for (int i = 0; i < MAX_FILE_LIST; i++)
mFileList[i] = "";
// Ficheros binarios
#ifdef __MIPSEL__
mFileList[0] = "/media/data/local/home/.coffee_crisis/score.bin";
mFileList[1] = "/media/data/local/home/.coffee_crisis/demo.bin";
mFileList[2] = "/media/data/local/home/.coffee_crisis/config.bin";
#else
mFileList[0] = mExecutablePath + "/" + "../data/score.bin";
mFileList[1] = mExecutablePath + "/" + "../data/demo.bin";
mFileList[2] = mExecutablePath + "/" + "../data/config.bin";
#endif
// Ficheros de configuración
asset->add("data/config/score.bin", t_data, false);
asset->add("data/config/demo.bin", t_data);
asset->add("data/config/config.bin", t_data, false);
asset->add("data/config/gamecontrollerdb.txt", t_data);
// Musicas
mFileList[3] = mExecutablePath + "/" + "../media/music/intro.ogg";
mFileList[4] = mExecutablePath + "/" + "../media/music/playing.ogg";
mFileList[5] = mExecutablePath + "/" + "../media/music/title.ogg";
asset->add("data/music/intro.ogg", t_music);
asset->add("data/music/playing.ogg", t_music);
asset->add("data/music/title.ogg", t_music);
// Sonidos
mFileList[6] = mExecutablePath + "/" + "../media/sound/balloon.wav";
mFileList[7] = mExecutablePath + "/" + "../media/sound/bubble1.wav";
mFileList[8] = mExecutablePath + "/" + "../media/sound/bubble2.wav";
mFileList[9] = mExecutablePath + "/" + "../media/sound/bubble3.wav";
mFileList[10] = mExecutablePath + "/" + "../media/sound/bubble4.wav";
mFileList[11] = mExecutablePath + "/" + "../media/sound/bullet.wav";
mFileList[12] = mExecutablePath + "/" + "../media/sound/coffeeout.wav";
mFileList[13] = mExecutablePath + "/" + "../media/sound/hiscore.wav";
mFileList[14] = mExecutablePath + "/" + "../media/sound/itemdrop.wav";
mFileList[15] = mExecutablePath + "/" + "../media/sound/itempickup.wav";
mFileList[16] = mExecutablePath + "/" + "../media/sound/menu_cancel.wav";
mFileList[17] = mExecutablePath + "/" + "../media/sound/menu_move.wav";
mFileList[18] = mExecutablePath + "/" + "../media/sound/menu_select.wav";
mFileList[19] = mExecutablePath + "/" + "../media/sound/player_collision.wav";
mFileList[20] = mExecutablePath + "/" + "../media/sound/stage_change.wav";
mFileList[21] = mExecutablePath + "/" + "../media/sound/title.wav";
mFileList[22] = mExecutablePath + "/" + "../media/sound/clock.wav";
mFileList[23] = mExecutablePath + "/" + "../media/sound/powerball.wav";
asset->add("data/sound/balloon.wav", t_sound);
asset->add("data/sound/bubble1.wav", t_sound);
asset->add("data/sound/bubble2.wav", t_sound);
asset->add("data/sound/bubble3.wav", t_sound);
asset->add("data/sound/bubble4.wav", t_sound);
asset->add("data/sound/bullet.wav", t_sound);
asset->add("data/sound/coffeeout.wav", t_sound);
asset->add("data/sound/hiscore.wav", t_sound);
asset->add("data/sound/itemdrop.wav", t_sound);
asset->add("data/sound/itempickup.wav", t_sound);
asset->add("data/sound/menu_cancel.wav", t_sound);
asset->add("data/sound/menu_move.wav", t_sound);
asset->add("data/sound/menu_select.wav", t_sound);
asset->add("data/sound/player_collision.wav", t_sound);
asset->add("data/sound/stage_change.wav", t_sound);
asset->add("data/sound/title.wav", t_sound);
asset->add("data/sound/clock.wav", t_sound);
asset->add("data/sound/powerball.wav", t_sound);
// Texturas
mFileList[24] = mExecutablePath + "/" + "../media/gfx/balloon.png";
mFileList[25] = mExecutablePath + "/" + "../media/gfx/bullet.png";
mFileList[31] = mExecutablePath + "/" + "../media/gfx/game_bg.png";
mFileList[32] = mExecutablePath + "/" + "../media/gfx/game_text.png";
mFileList[33] = mExecutablePath + "/" + "../media/gfx/intro.png";
mFileList[34] = mExecutablePath + "/" + "../media/gfx/items.png";
mFileList[35] = mExecutablePath + "/" + "../media/gfx/logo.png";
mFileList[37] = mExecutablePath + "/" + "../media/gfx/player1_body.png";
mFileList[38] = mExecutablePath + "/" + "../media/gfx/player1_death.png";
mFileList[39] = mExecutablePath + "/" + "../media/gfx/player1_legs.png";
mFileList[40] = mExecutablePath + "/" + "../media/gfx/title.png";
mFileList[41] = mExecutablePath + "/" + "../media/gfx/player1_head.png";
mFileList[42] = mExecutablePath + "/" + "../media/gfx/player2_body.png";
mFileList[43] = mExecutablePath + "/" + "../media/gfx/player2_death.png";
mFileList[44] = mExecutablePath + "/" + "../media/gfx/player2_legs.png";
mFileList[45] = mExecutablePath + "/" + "../media/gfx/player2_head.png";
asset->add("data/gfx/balloon.png", t_bitmap);
asset->add("data/gfx/bullet.png", t_bitmap);
asset->add("data/gfx/game_bg.png", t_bitmap);
asset->add("data/gfx/game_text.png", t_bitmap);
asset->add("data/gfx/intro.png", t_bitmap);
asset->add("data/gfx/items.png", t_bitmap);
asset->add("data/gfx/logo.png", t_bitmap);
asset->add("data/gfx/player1_body.png", t_bitmap);
asset->add("data/gfx/player1_death.png", t_bitmap);
asset->add("data/gfx/player1_legs.png", t_bitmap);
asset->add("data/gfx/title.png", t_bitmap);
asset->add("data/gfx/player1_head.png", t_bitmap);
asset->add("data/gfx/player2_body.png", t_bitmap);
asset->add("data/gfx/player2_death.png", t_bitmap);
asset->add("data/gfx/player2_legs.png", t_bitmap);
asset->add("data/gfx/player2_head.png", t_bitmap);
// Fuentes
mFileList[27] = mExecutablePath + "/" + "../media/font/8bithud.png";
mFileList[46] = mExecutablePath + "/" + "../media/font/8bithud.txt";
mFileList[28] = mExecutablePath + "/" + "../media/font/nokia.png";
mFileList[54] = mExecutablePath + "/" + "../media/font/nokia_big2.png";
mFileList[52] = mExecutablePath + "/" + "../media/font/nokia.txt";
mFileList[56] = mExecutablePath + "/" + "../media/font/nokia2.png";
mFileList[57] = mExecutablePath + "/" + "../media/font/nokia2.txt";
mFileList[55] = mExecutablePath + "/" + "../media/font/nokia_big2.txt";
mFileList[29] = mExecutablePath + "/" + "../media/font/smb2_big.png";
mFileList[47] = mExecutablePath + "/" + "../media/font/smb2_big.txt";
mFileList[30] = mExecutablePath + "/" + "../media/font/smb2.png";
mFileList[48] = mExecutablePath + "/" + "../media/font/smb2.txt";
asset->add("data/font/8bithud.png", t_font);
asset->add("data/font/8bithud.txt", t_font);
asset->add("data/font/nokia.png", t_font);
asset->add("data/font/nokia_big2.png", t_font);
asset->add("data/font/nokia.txt", t_font);
asset->add("data/font/nokia2.png", t_font);
asset->add("data/font/nokia2.txt", t_font);
asset->add("data/font/nokia_big2.txt", t_font);
asset->add("data/font/smb2_big.png", t_font);
asset->add("data/font/smb2_big.txt", t_font);
asset->add("data/font/smb2.png", t_font);
asset->add("data/font/smb2.txt", t_font);
// Textos
mFileList[49] = mExecutablePath + "/" + "../media/lang/es_ES.txt";
mFileList[50] = mExecutablePath + "/" + "../media/lang/en_UK.txt";
mFileList[51] = mExecutablePath + "/" + "../media/lang/ba_BA.txt";
asset->add("data/lang/es_ES.txt", t_lang);
asset->add("data/lang/en_UK.txt", t_lang);
asset->add("data/lang/ba_BA.txt", t_lang);
// DATA
mFileList[53] = mExecutablePath + "/" + "../data/gamecontrollerdb.txt";
}
// Comprueba los ficheros del vector de ficheros que coinciden con una ruta dada
bool Director::checkFolder(std::string name, std::string path)
{
bool success = true;
std::string p;
std::string filename;
SDL_RWops *file;
// Comprueba los ficheros de la carpeta
printf("\n>> %s FILES\n", name.c_str());
for (int i = 3; i < MAX_FILE_LIST; i++)
{
if (mFileList[i].find(path.c_str()) != std::string::npos)
{
p = mFileList[i].c_str();
filename = p.substr(p.find_last_of("\\/") + 1);
file = SDL_RWFromFile(p.c_str(), "r+b");
if (file != NULL)
{
printf("Checking file %-20s [OK]\n", filename.c_str());
}
else
{
printf("Checking file %-20s [ERROR]\n", filename.c_str());
success = false;
break;
}
SDL_RWclose(file);
}
}
return success;
}
// Comprueba que todos los ficheros existen
bool Director::checkFileList()
{
bool success = true;
printf("Checking files...\n");
if (success)
success &= checkFolder("MUSIC", "/media/music/");
if (success)
success &= checkFolder("SOUND", "/media/sound/");
if (success)
success &= checkFolder("BITMAP", "/media/gfx/");
if (success)
success &= checkFolder("FONT", "/media/font/");
if (success)
success &= checkFolder("LANG", "/media/lang/");
// Resultado
if (success)
printf("\n** All files OK.\n\n");
else
printf("\n** A file is missing. Exiting.\n\n");
return success;
return asset->check();
}
// Carga el fichero de configuración
bool Director::loadConfigFile()
{
// Pone unos valores por defecto
mOptions->fullScreenMode = 0;
mOptions->windowSize = 3;
mOptions->language = en_UK;
mOptions->difficulty = DIFFICULTY_NORMAL;
mOptions->input[0].deviceType = INPUT_USE_KEYBOARD;
mOptions->input[1].deviceType = INPUT_USE_GAMECONTROLLER;
mOptions->filter = FILTER_NEAREST;
mOptions->vSync = true;
options->fullScreenMode = 0;
options->windowSize = 3;
options->language = ba_BA;
options->difficulty = DIFFICULTY_NORMAL;
options->input[0].deviceType = INPUT_USE_KEYBOARD;
options->input[1].deviceType = INPUT_USE_GAMECONTROLLER;
options->filter = FILTER_NEAREST;
options->vSync = true;
options->screenWidth = GAME_WIDTH;
options->screenHeight = GAME_HEIGHT;
options->integerScale = true;
options->keepAspect = true;
options->borderSize = 0.0f;
options->borderEnabled = false;
// Indicador de éxito en la carga
bool success = true;
const std::string p = mFileList[2];
const std::string p = asset->get("config.bin");
std::string filename = p.substr(p.find_last_of("\\/") + 1);
SDL_RWops *file = SDL_RWFromFile(p.c_str(), "r+b");
// El fichero no existe
if (file == NULL)
if (file == nullptr)
{
printf("Warning: Unable to open %s file\n", filename.c_str());
// Crea el fichero para escritura
file = SDL_RWFromFile(p.c_str(), "w+b");
if (file != NULL)
if (file != nullptr)
{
printf("New file (%s) created!\n", filename.c_str());
// Escribe los datos
SDL_RWwrite(file, &mOptions->fullScreenMode, sizeof(mOptions->fullScreenMode), 1);
SDL_RWwrite(file, &mOptions->windowSize, sizeof(mOptions->windowSize), 1);
SDL_RWwrite(file, &mOptions->language, sizeof(mOptions->language), 1);
SDL_RWwrite(file, &mOptions->difficulty, sizeof(mOptions->difficulty), 1);
SDL_RWwrite(file, &mOptions->input[0].deviceType, sizeof(mOptions->input[0].deviceType), 1);
SDL_RWwrite(file, &mOptions->input[1].deviceType, sizeof(mOptions->input[1].deviceType), 1);
SDL_RWwrite(file, &mOptions->filter, sizeof(mOptions->filter), 1);
SDL_RWwrite(file, &mOptions->vSync, sizeof(mOptions->vSync), 1);
SDL_RWwrite(file, &options->fullScreenMode, sizeof(options->fullScreenMode), 1);
SDL_RWwrite(file, &options->windowSize, sizeof(options->windowSize), 1);
SDL_RWwrite(file, &options->language, sizeof(options->language), 1);
SDL_RWwrite(file, &options->difficulty, sizeof(options->difficulty), 1);
SDL_RWwrite(file, &options->input[0].deviceType, sizeof(options->input[0].deviceType), 1);
SDL_RWwrite(file, &options->input[1].deviceType, sizeof(options->input[1].deviceType), 1);
SDL_RWwrite(file, &options->filter, sizeof(options->filter), 1);
SDL_RWwrite(file, &options->vSync, sizeof(options->vSync), 1);
SDL_RWwrite(file, &options->screenWidth, sizeof(options->screenWidth), 1);
SDL_RWwrite(file, &options->screenHeight, sizeof(options->screenHeight), 1);
SDL_RWwrite(file, &options->integerScale, sizeof(options->integerScale), 1);
SDL_RWwrite(file, &options->keepAspect, sizeof(options->keepAspect), 1);
// Cierra el fichero
SDL_RWclose(file);
@@ -402,24 +313,28 @@ bool Director::loadConfigFile()
{
// Carga los datos
printf("Reading file %s\n", filename.c_str());
SDL_RWread(file, &mOptions->fullScreenMode, sizeof(mOptions->fullScreenMode), 1);
SDL_RWread(file, &mOptions->windowSize, sizeof(mOptions->windowSize), 1);
SDL_RWread(file, &mOptions->language, sizeof(mOptions->language), 1);
SDL_RWread(file, &mOptions->difficulty, sizeof(mOptions->difficulty), 1);
SDL_RWread(file, &mOptions->input[0].deviceType, sizeof(mOptions->input[0].deviceType), 1);
SDL_RWread(file, &mOptions->input[1].deviceType, sizeof(mOptions->input[1].deviceType), 1);
SDL_RWread(file, &mOptions->filter, sizeof(mOptions->filter), 1);
SDL_RWread(file, &mOptions->vSync, sizeof(mOptions->vSync), 1);
SDL_RWread(file, &options->fullScreenMode, sizeof(options->fullScreenMode), 1);
SDL_RWread(file, &options->windowSize, sizeof(options->windowSize), 1);
SDL_RWread(file, &options->language, sizeof(options->language), 1);
SDL_RWread(file, &options->difficulty, sizeof(options->difficulty), 1);
SDL_RWread(file, &options->input[0].deviceType, sizeof(options->input[0].deviceType), 1);
SDL_RWread(file, &options->input[1].deviceType, sizeof(options->input[1].deviceType), 1);
SDL_RWread(file, &options->filter, sizeof(options->filter), 1);
SDL_RWread(file, &options->vSync, sizeof(options->vSync), 1);
SDL_RWread(file, &options->screenWidth, sizeof(options->screenWidth), 1);
SDL_RWread(file, &options->screenHeight, sizeof(options->screenHeight), 1);
SDL_RWread(file, &options->integerScale, sizeof(options->integerScale), 1);
SDL_RWread(file, &options->keepAspect, sizeof(options->keepAspect), 1);
// Normaliza los valores
if (!((mOptions->fullScreenMode == 0) ||
(mOptions->fullScreenMode == SDL_WINDOW_FULLSCREEN) ||
(mOptions->fullScreenMode == SDL_WINDOW_FULLSCREEN_DESKTOP)))
mOptions->fullScreenMode = 0;
if ((mOptions->windowSize < 1) || (mOptions->windowSize > 4))
mOptions->windowSize = 3;
if ((mOptions->language < 0) || (mOptions->language > MAX_LANGUAGES))
mOptions->language = en_UK;
if (!((options->fullScreenMode == 0) ||
(options->fullScreenMode == SDL_WINDOW_FULLSCREEN) ||
(options->fullScreenMode == SDL_WINDOW_FULLSCREEN_DESKTOP)))
options->fullScreenMode = 0;
if ((options->windowSize < 1) || (options->windowSize > 4))
options->windowSize = 3;
if ((options->language < 0) || (options->language > MAX_LANGUAGES))
options->language = en_UK;
// Cierra el fichero
SDL_RWclose(file);
@@ -432,20 +347,24 @@ bool Director::loadConfigFile()
bool Director::saveConfigFile()
{
bool success = true;
const std::string p = mFileList[2];
const std::string p = asset->get("config.bin");
std::string filename = p.substr(p.find_last_of("\\/") + 1);
SDL_RWops *file = SDL_RWFromFile(p.c_str(), "w+b");
if (file != NULL)
if (file != nullptr)
{
// Guarda los datos
SDL_RWwrite(file, &mOptions->fullScreenMode, sizeof(mOptions->fullScreenMode), 1);
SDL_RWwrite(file, &mOptions->windowSize, sizeof(mOptions->windowSize), 1);
SDL_RWwrite(file, &mOptions->language, sizeof(mOptions->language), 1);
SDL_RWwrite(file, &mOptions->difficulty, sizeof(mOptions->difficulty), 1);
SDL_RWwrite(file, &mOptions->input[0].deviceType, sizeof(mOptions->input[0].deviceType), 1);
SDL_RWwrite(file, &mOptions->input[1].deviceType, sizeof(mOptions->input[1].deviceType), 1);
SDL_RWwrite(file, &mOptions->filter, sizeof(mOptions->filter), 1);
SDL_RWwrite(file, &mOptions->vSync, sizeof(mOptions->vSync), 1);
SDL_RWwrite(file, &options->fullScreenMode, sizeof(options->fullScreenMode), 1);
SDL_RWwrite(file, &options->windowSize, sizeof(options->windowSize), 1);
SDL_RWwrite(file, &options->language, sizeof(options->language), 1);
SDL_RWwrite(file, &options->difficulty, sizeof(options->difficulty), 1);
SDL_RWwrite(file, &options->input[0].deviceType, sizeof(options->input[0].deviceType), 1);
SDL_RWwrite(file, &options->input[1].deviceType, sizeof(options->input[1].deviceType), 1);
SDL_RWwrite(file, &options->filter, sizeof(options->filter), 1);
SDL_RWwrite(file, &options->vSync, sizeof(options->vSync), 1);
SDL_RWwrite(file, &options->screenWidth, sizeof(options->screenWidth), 1);
SDL_RWwrite(file, &options->screenHeight, sizeof(options->screenHeight), 1);
SDL_RWwrite(file, &options->integerScale, sizeof(options->integerScale), 1);
SDL_RWwrite(file, &options->keepAspect, sizeof(options->keepAspect), 1);
printf("Writing file %s\n", filename.c_str());
@@ -459,59 +378,59 @@ bool Director::saveConfigFile()
return success;
}
// Establece el valor de la variable
void Director::setExecutablePath(std::string path)
{
mExecutablePath = path.substr(0, path.find_last_of("\\/"));
}
// Obtiene el valor de la variable
Uint8 Director::getSubsection()
{
return mSection.subsection;
return section.subsection;
}
// Obtiene el valor de la variable
Uint8 Director::getSection()
{
return mSection.name;
return section.name;
}
// Establece el valor de la variable
void Director::setSection(section_t section)
{
mSection = section;
section = section;
}
void Director::runLogo()
{
mLogo = new Logo(mRenderer, mFileList);
setSection(mLogo->run());
delete mLogo;
logo = new Logo(renderer, screen, asset);
setSection(logo->run());
delete logo;
}
void Director::runIntro()
{
mIntro = new Intro(mRenderer, mFileList, mLang);
setSection(mIntro->run());
delete mIntro;
intro = new Intro(renderer, screen, asset, lang);
setSection(intro->run());
delete intro;
}
void Director::runTitle()
{
mTitle = new Title(mWindow, mRenderer, mInput, mFileList, mOptions, mLang);
setSection(mTitle->run(mSection.subsection));
delete mTitle;
title = new Title(window, renderer, screen, input, asset, options, lang);
setSection(title->run(section.subsection));
delete title;
}
void Director::runGame()
{
if (mSection.subsection == GAME_SECTION_PLAY_1P)
mGame = new Game(1, mRenderer, mFileList, mLang, mInput, false, mOptions);
if (mSection.subsection == GAME_SECTION_PLAY_2P)
mGame = new Game(2, mRenderer, mFileList, mLang, mInput, false, mOptions);
setSection(mGame->run());
delete mGame;
if (section.subsection == GAME_SECTION_PLAY_1P)
{
game = new Game(1, 0, renderer, screen, asset, lang, input, false, options);
}
else if (section.subsection == GAME_SECTION_PLAY_2P)
{
game = new Game(2, 0, renderer, screen, asset, lang, input, false, options);
}
setSection(game->run());
delete game;
}
void Director::run()
@@ -524,12 +443,15 @@ void Director::run()
case PROG_SECTION_LOGO:
runLogo();
break;
case PROG_SECTION_INTRO:
runIntro();
break;
case PROG_SECTION_TITLE:
runTitle();
break;
case PROG_SECTION_GAME:
runGame();
break;

View File

@@ -1,51 +1,51 @@
#pragma once
#include "ifdefs.h"
#include "sprite.h"
#include "movingsprite.h"
#include "smartsprite.h"
#include "player.h"
#include <SDL2/SDL.h>
#include "asset.h"
#include "balloon.h"
#include "bullet.h"
#include "coffeedrop.h"
#include "item.h"
#include "text.h"
#include "writer.h"
#include "menu.h"
#include "const.h"
#include "jail_audio.h"
#include "utils.h"
#include "logo.h"
#include "intro.h"
#include "title.h"
#include "fade.h"
#include "game.h"
#include "input.h"
#include "fade.h"
//#include <math.h>
#include "intro.h"
#include "item.h"
#include "jail_audio.h"
#include "logo.h"
#include "menu.h"
#include "movingsprite.h"
#include "player.h"
#include "screen.h"
#include "smartsprite.h"
#include "sprite.h"
#include "text.h"
#include "title.h"
#include "utils.h"
#include "writer.h"
#ifndef DIRECTOR_H
#define DIRECTOR_H
#define MAX_FILE_LIST 100
// Textos
#define WINDOW_CAPTION "Coffee Crisis"
// Director
// Clase Director
class Director
{
private:
SDL_Window *mWindow; // La ventana donde dibujamos
SDL_Renderer *mRenderer; // El renderizador de la ventana
Logo *mLogo; // Objeto para la sección del logo
Intro *mIntro; // Objeto para la sección de la intro
Title *mTitle; // Objeto para la sección del titulo y el menu de opciones
Game *mGame; // Objeto para la sección del juego
Input *mInput; // Objeto Input para gestionar las entradas
Lang *mLang; // Objeto para gestionar los textos en diferentes idiomas
std::string mFileList[MAX_FILE_LIST]; // Vector con las rutas a los ficheros de recursos
struct options_t *mOptions; // Variable con todas las opciones del programa
std::string mExecutablePath; // Path del ejecutable
section_t mSection; // Sección y subsección actual del programa;
SDL_Window *window; // La ventana donde dibujamos
SDL_Renderer *renderer; // El renderizador de la ventana
Screen *screen; // Objeto encargado de dibujar en pantalla
Logo *logo; // Objeto para la sección del logo
Intro *intro; // Objeto para la sección de la intro
Title *title; // Objeto para la sección del titulo y el menu de opciones
Game *game; // Objeto para la sección del juego
Input *input; // Objeto Input para gestionar las entradas
Lang *lang; // Objeto para gestionar los textos en diferentes idiomas
Asset *asset; // Objeto que gestiona todos los ficheros de recursos
struct options_t *options; // Variable con todas las opciones del programa
section_t section; // Sección y subsección actual del programa;
// Inicializa jail_audio
void initJailAudio();
@@ -54,10 +54,7 @@ private:
bool initSDL();
// Crea el indice de ficheros
void setFileList();
// Comprueba que todos los ficheros existen
bool checkFileList();
bool setFileList();
// Carga el fichero de configuración
bool loadConfigFile();
@@ -65,9 +62,6 @@ private:
// Guarda el fichero de configuración
bool saveConfigFile();
// Establece el valor de la variable
void setExecutablePath(std::string path);
// Obtiene el valor de la variable
Uint8 getSubsection();
@@ -89,9 +83,6 @@ private:
// Ejecuta la seccion de juego donde se juega
void runGame();
// Comprueba los ficheros del vector de ficheros que coinciden con una ruta dada
bool checkFolder(std::string name, std::string path);
public:
// Constructor
Director(std::string path);

View File

@@ -6,8 +6,8 @@ Fade::Fade(SDL_Renderer *renderer)
{
mRenderer = renderer;
mBackbuffer = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, SCREEN_WIDTH, SCREEN_HEIGHT);
if (mBackbuffer == NULL)
mBackbuffer = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAME_WIDTH, GAME_HEIGHT);
if (mBackbuffer == nullptr)
printf("Backbuffer could not be created!\nSDL Error: %s\n", SDL_GetError());
}
@@ -25,9 +25,6 @@ void Fade::init(Uint8 r, Uint8 g, Uint8 b)
mEnabled = false;
mFinished = false;
mCounter = 0;
//mR = 0x27;
//mG = 0x27;
//mB = 0x36;
mR = r;
mG = g;
mB = b;
@@ -41,15 +38,15 @@ void Fade::render()
switch (mFadeType)
{
case FADE_FULLSCREEN:
mRect1 = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
mRect1 = {0, 0, GAME_WIDTH, GAME_HEIGHT};
for (int i = 0; i < 256; i += 4)
{
// Dibujamos sobre el renderizador
SDL_SetRenderTarget(mRenderer, NULL);
SDL_SetRenderTarget(mRenderer, nullptr);
// Copia el backbuffer con la imagen que había al renderizador
SDL_RenderCopy(mRenderer, mBackbuffer, NULL, NULL);
SDL_RenderCopy(mRenderer, mBackbuffer, nullptr, nullptr);
SDL_SetRenderDrawColor(mRenderer, mR, mG, mB, i);
SDL_RenderFillRect(mRenderer, &mRect1);
@@ -63,27 +60,27 @@ void Fade::render()
SDL_SetRenderDrawColor(mRenderer, mR, mG, mB, 255);
SDL_RenderClear(mRenderer);
SDL_SetRenderTarget(mRenderer, NULL);
SDL_SetRenderTarget(mRenderer, nullptr);
SDL_SetRenderDrawColor(mRenderer, mR, mG, mB, 255);
SDL_RenderClear(mRenderer);
break;
case FADE_CENTER:
mRect1 = {0, 0, SCREEN_WIDTH, 0};
mRect2 = {0, 0, SCREEN_WIDTH, 0};
mRect1 = {0, 0, GAME_WIDTH, 0};
mRect2 = {0, 0, GAME_WIDTH, 0};
SDL_SetRenderDrawColor(mRenderer, mR, mG, mB, 64);
for (int i = 0; i < mCounter; i++)
{
mRect1.h = mRect2.h = i * 4;
mRect2.y = SCREEN_HEIGHT - (i * 4);
mRect2.y = GAME_HEIGHT - (i * 4);
SDL_RenderFillRect(mRenderer, &mRect1);
SDL_RenderFillRect(mRenderer, &mRect2);
}
if ((mCounter * 4) > SCREEN_HEIGHT)
if ((mCounter * 4) > GAME_HEIGHT)
mFinished = true;
break;
@@ -101,15 +98,15 @@ void Fade::render()
// Dibujamos sobre el backbuffer
SDL_SetRenderTarget(mRenderer, mBackbuffer);
mRect1.x = rand() % (SCREEN_WIDTH - mRect1.w);
mRect1.y = rand() % (SCREEN_HEIGHT - mRect1.h);
mRect1.x = rand() % (GAME_WIDTH - mRect1.w);
mRect1.y = rand() % (GAME_HEIGHT - mRect1.h);
SDL_RenderFillRect(mRenderer, &mRect1);
// Volvemos a usar el renderizador de forma normal
SDL_SetRenderTarget(mRenderer, NULL);
SDL_SetRenderTarget(mRenderer, nullptr);
// Copiamos el backbuffer al renderizador
SDL_RenderCopy(mRenderer, mBackbuffer, NULL, NULL);
SDL_RenderCopy(mRenderer, mBackbuffer, nullptr, nullptr);
// Volcamos el renderizador en pantalla
SDL_RenderPresent(mRenderer);
@@ -121,7 +118,7 @@ void Fade::render()
break;
}
}
if (mFinished)
{
SDL_SetRenderDrawColor(mRenderer, mR, mG, mB, 255);
@@ -153,16 +150,7 @@ bool Fade::isEnabled()
// Comprueba si ha terminado la transicion
bool Fade::hasEnded()
{
if (mFinished)
{
//mEnabled = false;
//mFinished = false;
return true;
}
else
{
return false;
}
return mFinished;
}
// Establece el tipo de fade

View File

@@ -1,15 +1,17 @@
#pragma once
#include "ifdefs.h"
#include <SDL2/SDL.h>
#include "ltexture.h"
#ifndef FADE_H
#define FADE_H
// Tipos de fundido
#define FADE_FULLSCREEN 0
#define FADE_CENTER 1
#define FADE_RANDOM_SQUARE 2
// Fade
// Clase Fade
class Fade
{
private:

File diff suppressed because it is too large Load Diff

View File

@@ -1,28 +1,62 @@
#pragma once
#include "ifdefs.h"
#include "const.h"
#include "utils.h"
#include "sprite.h"
#include "movingsprite.h"
#include "smartsprite.h"
#include "player.h"
#include <SDL2/SDL.h>
#include "asset.h"
#include "balloon.h"
#include "bullet.h"
#include "item.h"
#include "text.h"
#include "writer.h"
#include "menu.h"
#include "input.h"
#include "const.h"
#include "fade.h"
#include "input.h"
#include "item.h"
#include "jail_audio.h"
#include "menu.h"
#include "movingsprite.h"
#include "player.h"
#include "screen.h"
#include "smartsprite.h"
#include "sprite.h"
#include "text.h"
#include "utils.h"
#include "writer.h"
#ifndef GAME_H
#define GAME_H
// Game
// Cantidad de elementos a escribir en los ficheros de datos
#define TOTAL_SCORE_DATA 3
#define TOTAL_DEMO_DATA 2000
// Contadores
#define STAGE_COUNTER 200
#define SHAKE_COUNTER 10
#define HELP_COUNTER 1000
// Formaciones enemigas
#define NUMBER_OF_ENEMY_FORMATIONS 100
#define MAX_NUMBER_OF_ENEMIES_IN_A_FORMATION 50
// Cantidad de elementos del vector de SmartSprites
#define MAX_SMART_SPRITES 10
// Cantidad máxima posible de balas
#define MAX_BULLETS 50
// Porcentaje de aparición de los objetos
#define ITEM_POINTS_1_DISK_ODDS 10
#define ITEM_POINTS_2_GAVINA_ODDS 6
#define ITEM_POINTS_3_PACMAR_ODDS 3
#define ITEM_CLOCK_ODDS 5
#define ITEM_COFFEE_ODDS 5
#define ITEM_POWER_BALL_ODDS 0
#define ITEM_COFFEE_MACHINE_ODDS 4
// Cantidad de objetos simultaneos
#define MAX_ITEMS 10
// Valores para las variables asociadas a los objetos
#define TIME_STOPPED_COUNTER 300
// Clase Game
class Game
{
private:
@@ -40,13 +74,11 @@ private:
Uint8 numberOfEnemies; // Cantidad de enemigos que forman la formación
enemyInits_t init[MAX_NUMBER_OF_ENEMIES_IN_A_FORMATION]; // Vector con todas las inicializaciones de los enemigos de la formación
};
enemyFormation_t mEnemyFormation[NUMBER_OF_ENEMY_FORMATIONS]; // Vector con todas las formaciones enemigas
struct enemyPool_t
{
enemyFormation_t *set[10]; // Conjunto de formaciones enemigas
};
enemyPool_t mEnemyPool[10]; // Variable con los diferentes conjuntos de formaciones enemigas
struct stage_t // Contiene todas las variables relacionadas con una fase
{
@@ -79,43 +111,61 @@ private:
int itemCoffeeMachineOdds; // Probabilidad de aparición del objeto
};
struct demo_t
{
bool enabled; // Indica si está activo el modo demo
bool recording; // Indica si está activado el modo para grabar la demo
Uint16 counter; // Contador para el modo demo
demoKeys_t keys; // Variable con las pulsaciones de teclas del modo demo
demoKeys_t dataFile[TOTAL_DEMO_DATA]; // Datos del fichero con los movimientos para la demo
};
struct debug_t
{
bool enabled; // Indica si se va a mostrar la información de debug
Uint8 enemySet; // Escoge el set enemigo a generar
Uint8 gradR, gradG, gradB; // Colores RGB para modificar el color del gradiente de fondo
float hudW, hudH; // Multiplica el tamaño del hud de debug;
};
SDL_Renderer *mRenderer; // El renderizador de la ventana
std::string *mFileList; // Lista de ficheros con los recursos
Screen *mScreen; // Objeto encargado de dibujar en pantalla
Asset *mAsset; // Objeto que gestiona todos los ficheros de recursos
Lang *mLang; // Objeto para gestionar los textos en diferentes idiomas
Input *mInput; // Manejador de entrada
int mNumPlayers; // Numero de jugadores
Player *mPlayer[2]; // Vector con los jugadores jugador
Input *mInput; // Manejador de entrada
// Player *mPlayer[2]; // Vector con los jugadores jugador
// Balloon *mBalloon[MAX_BALLOONS]; // Vector con los objetos globo
// Bullet *mBullet[MAX_BULLETS]; // Vector con los objetos bala
// Item *mItem[MAX_ITEMS]; // Vector con los objetos item
// SmartSprite *mSmartSprite[MAX_SMART_SPRITES]; // Vector para almacenar y gestionar SmartSprites
Balloon *mBalloon[MAX_BALLOONS]; // Vector con los objetos globo
Bullet *mBullet[MAX_BULLETS]; // Vector con los objetos bala
Item *mItem[MAX_ITEMS]; // Vector con los objetos item
SmartSprite *mSmartSprite[MAX_SMART_SPRITES]; // Vector para almacenar y gestionar SmartSprites
int mNumPlayers; // Numero de jugadores
std::vector<Player *> players; // Vector con los jugadores
std::vector<Balloon *> balloons; // Vector con los globos
std::vector<Bullet *> bullets; // Vector con las balas
std::vector<Item *> items; // Vector con los items
std::vector<SmartSprite *> smartSprites; // Vector con los smartsprites
LTexture *mTextureBalloon; // Textura para los enemigos
LTexture *mTextureBullet; // Textura para las balas
LTexture *mTextureGameBG; // Textura para el fondo del juego
LTexture *mTextureGameText; // Textura para los sprites con textos
LTexture *mTextureItems; // Textura para los items
LTexture *mTexturePlayer1Head; // Textura para la cabeza del jugador1
LTexture *mTexturePlayer1Body; // Textura para el cuerpo del jugador1
LTexture *mTexturePlayer1Death; // Textura para la animación de muerte del jugador1
LTexture *mTexturePlayer1Legs; // Textura para las piernas del jugador
LTexture *mTexturePlayer2Head; // Textura para la cabeza del jugador2
LTexture *mTexturePlayer2Body; // Textura para el cuerpo del jugador2
LTexture *mTexturePlayer2Death; // Textura para la animación de muerte del jugador2
LTexture *mTexturePlayer2Legs; // Textura para las piernas del jugador
LTexture *mTextureText; // Textura para el texto del juego
LTexture *mTextureTextScoreBoard; // Textura para el texto del marcador
LTexture *mTextureTextBig; // Textura para el texto grande
LTexture *mTextureTextNokia2; // Textura para la fuente de texto Nokia
LTexture *mTextureTextNokiaBig2; // Textura para la fuente de texto Nokia grande
LTexture *mTextureBalloon; // Textura para los enemigos
LTexture *mTextureBullet; // Textura para las balas
LTexture *mTextureGameBG; // Textura para el fondo del juego
LTexture *mTextureGameText; // Textura para los sprites con textos
LTexture *mTextureItems; // Textura para los items
LTexture *mTexturePlayer1Head; // Textura para la cabeza del jugador1
LTexture *mTexturePlayer1Body; // Textura para el cuerpo del jugador1
LTexture *mTexturePlayer1Death; // Textura para la animación de muerte del jugador1
LTexture *mTexturePlayer1Legs; // Textura para las piernas del jugador
LTexture *mTexturePlayer2Head; // Textura para la cabeza del jugador2
LTexture *mTexturePlayer2Body; // Textura para el cuerpo del jugador2
LTexture *mTexturePlayer2Death; // Textura para la animación de muerte del jugador2
LTexture *mTexturePlayer2Legs; // Textura para las piernas del jugador
Text *mText; // Fuente para los textos del juego
Text *mTextBig; // Fuente de texto grande
Text *mTextScoreBoard; // Fuente para el marcador del juego
Text *mTextNokia2; // Otra fuente de texto para mesajes
Text *mTextNokiaBig2; // Y la versión en grande
Text *mTextNokiaBig2; // Y la versión en grande
Menu *mMenuGameOver; // Menú de la pantalla de game over
Menu *mMenuPause; // Menú de la pantalla de pausa
@@ -158,63 +208,47 @@ private:
Uint32 mTicks; // Contador de ticks para ajustar la velocidad del programa
Uint8 mTicksSpeed; // Velocidad a la que se repiten los bucles del programa
Uint32 mHiScore; // Puntuación máxima
bool mHiScoreAchieved; // Indica si se ha superado la puntuación máxima
section_t mSection; // Seccion actual dentro del juego
stage_t mStage[10]; // Variable con los datos de cada pantalla
Uint8 mCurrentStage; // Indica la fase actual
Uint8 mStageBitmapCounter; // Contador para el tiempo visible del texto de Stage
float mStageBitmapPath[STAGE_COUNTER]; // Vector con los puntos Y por donde se desplaza el texto
float mGetReadyBitmapPath[STAGE_COUNTER]; // Vector con los puntos X por donde se desplaza el texto
Uint16 mDeathCounter; // Contador para la animación de muerte del jugador
Uint8 mDeathIndex; // Indice del vector de smartsprites que contiene el sprite del jugador
Uint8 mMenaceCurrent; // Nivel de amenaza actual
Uint8 mMenaceThreshold; // Umbral del nivel de amenaza. Si el nivel de amenaza cae por debajo del umbral, se generan más globos. Si el umbral aumenta, aumenta el numero de globos
bool mTimeStopped; // Indica si el tiempo está detenido
Uint16 mTimeStoppedCounter; // Temporizador para llevar la cuenta del tiempo detenido
Uint8 mRemainingExplosions; // Cantidad de explosiones restantes
Uint16 mRemainingExplosionsCounter; // Temporizador para la cantidad de explosiones restantes
bool mExplosionTime; // Indica si las explosiones estan en marcha
Uint32 mCounter; // Contador para el juego
Uint32 mScoreDataFile[TOTAL_SCORE_DATA]; // Datos del fichero de puntos
SDL_Rect mGradientRect[4]; // Vector con las coordenadas de los 4 gradientes
Uint16 mBalloonsPopped; // Lleva la cuenta de los globos explotados
Uint8 mLastEnemyDeploy; // Guarda cual ha sido la última formación desplegada para no repetir;
int mEnemyDeployCounter; // Cuando se lanza una formación, se le da un valor y no sale otra hasta que llegue a cero
float mEnemySpeed; // Velocidad a la que se mueven los enemigos
float mDefaultEnemySpeed; // Velocidad base de los enemigos, sin incrementar
effect_t mEffect; // Variable para gestionar los efectos visuales
helper_t mHelper; // Variable para gestionar las ayudas
bool mPowerBallEnabled; // Indica si hay una powerball ya activa
Uint8 mPowerBallCounter; // Contador de formaciones enemigas entre la aparicion de una PowerBall y otra
bool mCoffeeMachineEnabled; // Indica si hay una máquina de café en el terreno de juego
Uint8 mPostFade; // Qué hacer al acabar el fade
float mSin[360]; // Vector con los valores del seno para 360 grados
bool mGameCompleted; // Indica si se ha completado la partida, llegando al final de la ultima pantalla
int mGameCompletedCounter; // Contador para el tramo final, cuando se ha completado la partida y ya no aparecen más enemigos
Uint8 mDifficulty; // Dificultad del juego
float mDifficultyScoreMultiplier; // Multiplicador de puntos en función de la dificultad
struct options_t *mOptions; // Variable con todas las variables de las opciones del programa
Uint8 mOnePlayerControl; // Variable para almacenar el valor de las opciones
Uint32 mHiScore; // Puntuación máxima
bool mHiScoreAchieved; // Indica si se ha superado la puntuación máxima
section_t mSection; // Seccion actual dentro del juego
stage_t mStage[10]; // Variable con los datos de cada pantalla
Uint8 mCurrentStage; // Indica la fase actual
Uint8 mStageBitmapCounter; // Contador para el tiempo visible del texto de Stage
float mStageBitmapPath[STAGE_COUNTER]; // Vector con los puntos Y por donde se desplaza el texto
float mGetReadyBitmapPath[STAGE_COUNTER]; // Vector con los puntos X por donde se desplaza el texto
Uint16 mDeathCounter; // Contador para la animación de muerte del jugador
Uint8 mDeathIndex; // Indice del vector de smartsprites que contiene el sprite del jugador
Uint8 mMenaceCurrent; // Nivel de amenaza actual
Uint8 mMenaceThreshold; // Umbral del nivel de amenaza. Si el nivel de amenaza cae por debajo del umbral, se generan más globos. Si el umbral aumenta, aumenta el numero de globos
bool mTimeStopped; // Indica si el tiempo está detenido
Uint16 mTimeStoppedCounter; // Temporizador para llevar la cuenta del tiempo detenido
Uint32 mCounter; // Contador para el juego
Uint32 mScoreDataFile[TOTAL_SCORE_DATA]; // Datos del fichero de puntos
SDL_Rect mGradientRect[4]; // Vector con las coordenadas de los 4 gradientes
Uint16 mBalloonsPopped; // Lleva la cuenta de los globos explotados
Uint8 mLastEnemyDeploy; // Guarda cual ha sido la última formación desplegada para no repetir;
int mEnemyDeployCounter; // Cuando se lanza una formación, se le da un valor y no sale otra hasta que llegue a cero
float mEnemySpeed; // Velocidad a la que se mueven los enemigos
float mDefaultEnemySpeed; // Velocidad base de los enemigos, sin incrementar
effect_t mEffect; // Variable para gestionar los efectos visuales
helper_t mHelper; // Variable para gestionar las ayudas
bool mPowerBallEnabled; // Indica si hay una powerball ya activa
Uint8 mPowerBallCounter; // Contador de formaciones enemigas entre la aparicion de una PowerBall y otra
bool mCoffeeMachineEnabled; // Indica si hay una máquina de café en el terreno de juego
Uint8 mPostFade; // Qué hacer al acabar el fade
float mSin[360]; // Vector con los valores del seno para 360 grados
bool mGameCompleted; // Indica si se ha completado la partida, llegando al final de la ultima pantalla
int mGameCompletedCounter; // Contador para el tramo final, cuando se ha completado la partida y ya no aparecen más enemigos
Uint8 mDifficulty; // Dificultad del juego
float mDifficultyScoreMultiplier; // Multiplicador de puntos en función de la dificultad
struct options_t *mOptions; // Variable con todas las variables de las opciones del programa
Uint8 mOnePlayerControl; // Variable para almacenar el valor de las opciones
enemyFormation_t mEnemyFormation[NUMBER_OF_ENEMY_FORMATIONS]; // Vector con todas las formaciones enemigas
enemyPool_t mEnemyPool[10]; // Variable con los diferentes conjuntos de formaciones enemigas
Uint8 mLastStageReached; // Contiene el numero de la última pantalla que se ha alcanzado
struct demo_t
{
bool enabled; // Indica si está activo el modo demo
bool recording; // Indica si está activado el modo para grabar la demo
Uint16 counter; // Contador para el modo demo
demoKeys_t keys; // Variable con las pulsaciones de teclas del modo demo
demoKeys_t dataFile[TOTAL_DEMO_DATA]; // Datos del fichero con los movimientos para la demo
};
demo_t mDemo; // Variable con todas las variables relacionadas con el modo demo
struct debug_t
{
bool enabled; // Indica si se va a mostrar la información de debug
Uint8 enemySet; // Escoge el set enemigo a generar
Uint8 gradR, gradG, gradB; // Colores RGB para modificar el color del gradiente de fondo
float hudW, hudH; // Multiplica el tamaño del hud de debug;
};
debug_t mDebug;
demo_t mDemo; // Variable con todas las variables relacionadas con el modo demo
debug_t mDebug; // Variable con las opciones de debug
// Inicializa el vector con los valores del seno
void initSin();
@@ -282,9 +316,6 @@ private:
// Pinta en pantalla todos los globos activos
void renderBalloons();
// Devuelve el primer indice no activo del vector de globos
Uint8 getBalloonFreeIndex();
// Crea un globo nuevo en el vector de globos
Uint8 createNewBalloon(float x, int y, Uint8 kind, float velx, float speed, Uint16 stoppedcounter, LTexture *texture);
@@ -307,10 +338,10 @@ private:
void updateBalloonSpeed();
// Explosiona un globo. Lo destruye y crea otros dos si es el caso
void popBalloon(Uint8 index);
void popBalloon(Balloon *balloon);
// Explosiona un globo. Lo destruye
void destroyBalloon(Uint8 index);
void destroyBalloon(Balloon *balloon);
// Explosiona todos los globos
void popAllBalloons();
@@ -328,10 +359,10 @@ private:
Uint8 countBalloons();
// Comprueba la colisión entre el jugador y los globos activos
bool checkPlayerBalloonCollision(int index);
bool checkPlayerBalloonCollision(Player *player);
// Comprueba la colisión entre el jugador y los items
void checkPlayerItemCollision(int index);
void checkPlayerItemCollision(Player *player);
// Comprueba la colisión entre las balas y los globos
void checkBulletBalloonCollision();
@@ -342,9 +373,6 @@ private:
// Pinta las balas activas
void renderBullets();
// Devuelve el primer indice no activo del vector de balas
Uint8 getBulletFreeIndex();
// Establece a cero todos los valores del vector de objetos bala
void resetBullets();
@@ -357,9 +385,6 @@ private:
// Pinta los items activos
void renderItems();
// Devuelve el primer indice no activo del vector de items
Uint8 getItemFreeIndex();
// Establece a cero todos los valores del vector de objetos item
void resetItems();
@@ -390,14 +415,11 @@ private:
// Pinta los SmartSprites activos
void renderSmartSprites();
// Devuelve el primer indice no activo del vector de SmartSprites
Uint8 getSmartSpriteFreeIndex();
// Establece a cero todos los valores del vector de objetos SmafrtSprite
void resetSmartSprites();
// Acciones a realizar cuando el jugador muere
void killPlayer(int index);
void killPlayer(Player *player);
// Obtiene el valor de la variable
Uint8 getSubsection();
@@ -426,18 +448,6 @@ private:
// Actualiza y comprueba el valor de la variable
void updateTimeStoppedCounter();
// Establece el valor de la variable
void setExplosionTime(bool value);
// Obtiene el valor de la variable
bool isExplosionTime();
// Establece el valor de la variable
void setRemainingExplosions(Uint8 value);
// Actualiza y comprueba el valor de la variable
void updateRemainingExplosionsCounter();
// Gestiona el nivel de amenaza
void updateMenace();
@@ -453,9 +463,6 @@ private:
// Dibuja el campo de juego
void renderPlayField();
// Gestiona las entradas desde el mando de juego
bool checkGameController(Uint8 state);
// Gestiona la entrada durante el juego
void checkGameInput();
@@ -500,7 +507,7 @@ private:
public:
// Constructor
Game(int numPlayers, SDL_Renderer *renderer, std::string *filelist, Lang *lang, Input *input, bool demo, options_t *options);
Game(int numPlayers, int currentStage, SDL_Renderer *renderer, Screen *screen, Asset *mAsset, Lang *lang, Input *input, bool demo, options_t *options);
// Destructor
~Game();

View File

@@ -1,22 +0,0 @@
#ifdef _WIN64
#include "C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\lib\gcc\x86_64-w64-mingw32\8.1.0\include\c++\SDL2\SDL.h"
#endif
#ifdef _WIN32
#include "C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\lib\gcc\x86_64-w64-mingw32\8.1.0\include\c++\SDL2\SDL.h"
#endif
#ifdef __APPLE__
//#include "/Library/Frameworks/SDL2.framework/Versions/A/Headers/SDL.h"
#include <SDL2/SDL.h>
#endif
#ifdef __linux__
#ifdef __MIPSEL__
#include "SDL.h"
#else
#include <SDL2/SDL.h>
#endif
#endif
#define UNUSED

View File

@@ -1,9 +1,6 @@
#include "input.h"
#include <iostream>
// Contestar cuantos joystics ha detectado
// Preguntarlepor los joystics que ha encontrado para ir poniendolos en la variable de opciones
// Constructor
Input::Input(std::string file)
{
@@ -53,7 +50,7 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
if ((device == INPUT_USE_KEYBOARD) || (device == INPUT_USE_ANY))
{
const Uint8 *mKeystates = SDL_GetKeyboardState(NULL);
const Uint8 *mKeystates = SDL_GetKeyboardState(nullptr);
if (repeat)
{

View File

@@ -1,5 +1,6 @@
#pragma once
#include "ifdefs.h"
#include <SDL2/SDL.h>
#include <string>
#include <vector>
@@ -49,11 +50,10 @@ private:
};
GameControllerBindings_t mGameControllerBindings[17]; // Vector con las teclas asociadas a los inputs predefinidos
//SDL_GameController *mGameController; // Manejador para el mando
std::vector<SDL_GameController*> mConnectedControllers;
std::vector<std::string> mControllerNames;
int mNumGamepads;
std::string mDBpath; // Ruta al archivo gamecontrollerdb.txt
std::vector<SDL_GameController *> mConnectedControllers; // Vector con todos los mandos conectados
std::vector<std::string> mControllerNames; // Vector con los nombres de los mandos
int mNumGamepads; // Numero de mandos conectados
std::string mDBpath; // Ruta al archivo gamecontrollerdb.txt
// Comprueba si hay un mando conectado
bool discoverGameController();
@@ -72,7 +72,7 @@ public:
void bindGameControllerButton(Uint8 input, SDL_GameControllerButton button);
// Comprueba si un input esta activo
bool checkInput(Uint8 input, bool repeat, int device=INPUT_USE_ANY, int index=0);
bool checkInput(Uint8 input, bool repeat, int device = INPUT_USE_ANY, int index = 0);
// Comprueba si hay algun mando conectado
bool gameControllerFound();

View File

@@ -1,43 +1,42 @@
#include "instructions.h"
#ifdef __MIPSEL__
#include <sys/stat.h>
#include <dirent.h>
#endif
const Uint8 SELF = 0;
// Constructor
Instructions::Instructions(SDL_Renderer *renderer, std::string *fileList, Lang *lang)
Instructions::Instructions(SDL_Renderer *renderer, Screen *screen, Asset *mAsset, Lang *lang)
{
// Copia los punteros
mRenderer = renderer;
mFileList = fileList;
mScreen = screen;
this->mAsset = mAsset;
mLang = lang;
// Reserva memoria para los punteros
mEventHandler = new SDL_Event();
mItemTexture = new LTexture();
mTextTexture = new LTexture();
mSprite = new Sprite();
mText = new Text(mFileList[48], mTextTexture, mRenderer);
mItemTexture = new LTexture(mRenderer,mAsset->get("items.png"));
mSprite = new Sprite(0, 0, GAME_WIDTH, GAME_HEIGHT, mItemTexture, mRenderer);
mText = new Text(mAsset->get("smb2.png"), mAsset->get("smb2.txt"), mRenderer);
// Crea un backbuffer para el renderizador
mBackbuffer = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, SCREEN_WIDTH, SCREEN_HEIGHT);
if (mBackbuffer == NULL)
mBackbuffer = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAME_WIDTH, GAME_HEIGHT);
if (mBackbuffer == nullptr)
{
printf("Backbuffer could not be created!\nSDL Error: %s\n", SDL_GetError());
}
}
// Destructor
Instructions::~Instructions()
{
mRenderer = nullptr;
mScreen = nullptr;
mAsset = nullptr;
mLang = nullptr;
mItemTexture->unload();
delete mItemTexture;
mItemTexture = nullptr;
mTextTexture->unload();
delete mTextTexture;
mTextTexture = nullptr;
delete mSprite;
mSprite = nullptr;
@@ -51,26 +50,11 @@ Instructions::~Instructions()
mBackbuffer = nullptr;
}
// Carga los recursos necesarios para la sección 'Instructions'
bool Instructions::loadMedia()
{
bool success = true;
success &= loadTextureFromFile(mItemTexture, mFileList[34], mRenderer);
success &= loadTextureFromFile(mTextTexture, mFileList[30], mRenderer);
return success;
}
// Inicializa las variables necesarias para la sección 'Instructions'
void Instructions::init()
{
// Carga los recursos
loadMedia();
// Inicializa variables
mSection.name = SELF;
mSprite->init(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, mItemTexture, mRenderer);
mTicks = 0;
mTicksSpeed = 15;
mManualQuit = false;
@@ -138,7 +122,7 @@ void Instructions::run(Uint8 mode)
}
// Pinta en pantalla
SDL_Rect window = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
SDL_Rect window = {0, 0, GAME_WIDTH, GAME_HEIGHT};
SDL_Rect srcRect = {0, 0, 16, 16};
const color_t orangeColor = {0xFF, 0x7A, 0x00};
@@ -169,38 +153,38 @@ void Instructions::run(Uint8 mode)
mText->writeShadowed(84, 156, mLang->getText(21), shdwTxtColor);
if ((mode == INSTRUCTIONS_MODE_MANUAL) && (mCounter % 50 > 14))
mText->writeDX(TXT_CENTER | TXT_COLOR | TXT_SHADOW, SCREEN_CENTER_X, SCREEN_HEIGHT - 12, mLang->getText(22), 1, orangeColor, 1, shdwTxtColor);
mText->writeDX(TXT_CENTER | TXT_COLOR | TXT_SHADOW, SCREEN_CENTER_X, GAME_HEIGHT - 12, mLang->getText(22), 1, orangeColor, 1, shdwTxtColor);
// Disquito
mSprite->init(destRect1, mItemTexture, mRenderer);
mSprite->setPos(destRect1);
srcRect.x = 0;
srcRect.y = 16 * (((mCounter + 12) / 36) % 2);
mSprite->setSpriteClip(srcRect);
mSprite->render();
// Gavineixon
mSprite->init(destRect2, mItemTexture, mRenderer);
mSprite->setPos(destRect2);
srcRect.x += srcRect.w;
srcRect.y = 16 * (((mCounter + 9) / 36) % 2);
mSprite->setSpriteClip(srcRect);
mSprite->render();
// Pacmar
mSprite->init(destRect3, mItemTexture, mRenderer);
mSprite->setPos(destRect3);
srcRect.x += srcRect.w;
srcRect.y = 16 * (((mCounter + 6) / 36) % 2);
mSprite->setSpriteClip(srcRect);
mSprite->render();
// Time Stopper
mSprite->init(destRect4, mItemTexture, mRenderer);
mSprite->setPos(destRect4);
srcRect.x += srcRect.w;
srcRect.y = 16 * (((mCounter + 3) / 36) % 2);
mSprite->setSpriteClip(srcRect);
mSprite->render();
// Coffee
mSprite->init(destRect5, mItemTexture, mRenderer);
mSprite->setPos(destRect5);
srcRect.x += (srcRect.w * 2); // Se salta el icono del TNT
srcRect.y = 16 * (((mCounter + 0) / 36) % 2);
mSprite->setSpriteClip(srcRect);
@@ -209,20 +193,22 @@ void Instructions::run(Uint8 mode)
// Cambia el destino de renderizado
SDL_SetRenderTarget(mRenderer, nullptr);
// Limpia el renderizador
SDL_SetRenderDrawColor(mRenderer, bgColor.r, bgColor.g, bgColor.b, 255);
SDL_RenderClear(mRenderer);
// Prepara para empezar a dibujar en la textura de juego
mScreen->start();
// Limpia la pantalla
mScreen->clean(bgColor);
// Establece la ventana del backbuffer
if (mode == INSTRUCTIONS_MODE_AUTO)
window.y = std::max(8, SCREEN_HEIGHT - mCounter + 100);
window.y = std::max(8, GAME_HEIGHT - mCounter + 100);
else
window.y = 0;
// Copia el backbuffer al renderizador
SDL_RenderCopy(mRenderer, mBackbuffer, NULL, &window);
SDL_RenderCopy(mRenderer, mBackbuffer, nullptr, &window);
// Dibuja el renderizador en pantalla
SDL_RenderPresent(mRenderer);
// Vuelca el contenido del renderizador en pantalla
mScreen->blit();
}
}

View File

@@ -1,26 +1,35 @@
#pragma once
#include "ifdefs.h"
#include "utils.h"
#include "const.h"
#include <SDL2/SDL.h>
#include "asset.h"
#include "const.h"
#include "jail_audio.h"
#include "screen.h"
#include "sprite.h"
#include "text.h"
#include "jail_audio.h"
#include "utils.h"
#ifndef INSTRUCTIONS_H
#define INSTRUCTIONS_H
// Instructions
// Contadores
#define INSTRUCTIONS_COUNTER 600
// Modo para las instrucciones
#define INSTRUCTIONS_MODE_MANUAL 0
#define INSTRUCTIONS_MODE_AUTO 1
// Clase Instructions
class Instructions
{
private:
LTexture *mItemTexture; // Textura con los graficos
LTexture *mTextTexture; // Textura con los graficos
SDL_Event *mEventHandler; // Manejador de eventos
SDL_Renderer *mRenderer; // El renderizador de la ventana
Screen *mScreen; // Objeto encargado de dibujar en pantalla
LTexture *mItemTexture; // Textura con los graficos
SDL_Event *mEventHandler; // Manejador de eventos
SDL_Texture *mBackbuffer; // Textura para usar como backbuffer
Sprite *mSprite; // Sprite con la textura de las instrucciones
std::string *mFileList; // Lista de ficheros
Asset *mAsset; // Objeto que gestiona todos los ficheros de recursos
Lang *mLang; // Objeto para gestionar los textos en diferentes idiomas
Text *mText; // Objeto para escribir texto
Uint16 mCounter; // Contador
@@ -29,9 +38,6 @@ private:
Uint8 mTicksSpeed; // Velocidad a la que se repiten los bucles del programa
bool mManualQuit; // Indica si se quiere salir del modo manual
// Carga los recursos
bool loadMedia();
// Actualiza las variables
void update();
@@ -43,7 +49,7 @@ private:
public:
// Constructor
Instructions(SDL_Renderer *renderer, std::string *fileList, Lang *lang);
Instructions(SDL_Renderer *renderer, Screen *screen, Asset *mAsset, Lang *lang);
// Destructor
~Instructions();

View File

@@ -1,33 +1,38 @@
#include "intro.h"
#ifdef __MIPSEL__
#include <sys/stat.h>
#include <dirent.h>
#endif
// Constructor
Intro::Intro(SDL_Renderer *renderer, std::string *fileList, Lang *lang)
Intro::Intro(SDL_Renderer *renderer, Screen *screen, Asset *mAsset, Lang *lang)
{
// Copia los punteros
mRenderer = renderer;
mFileList = fileList;
mScreen = screen;
mLang = lang;
this->mAsset = mAsset;
// Reserva memoria para los punteros
mEventHandler = new SDL_Event();
mBitmapTexture = new LTexture();
mTextTexture = new LTexture();
mText = new Text(mFileList[52], mTextTexture, mRenderer);
mBitmapTexture = new LTexture(mRenderer,mAsset->get("intro.png"));
mText = new Text(mAsset->get("nokia.png"), mAsset->get("nokia.txt"), mRenderer);
for (int i = 0; i < INTRO_TOTAL_BITMAPS; i++)
mBitmap[i] = new SmartSprite();
{
mBitmap[i] = new SmartSprite(mBitmapTexture, mRenderer);
}
for (int i = 0; i < INTRO_TOTAL_TEXTS; i++)
{
mWriter[i] = new Writer(mText);
}
}
// Destructor
Intro::~Intro()
{
mRenderer = nullptr;
mScreen = nullptr;
mAsset = nullptr;
mLang = nullptr;
delete mEventHandler;
mEventHandler = nullptr;
@@ -35,15 +40,12 @@ Intro::~Intro()
delete mBitmapTexture;
mBitmapTexture = nullptr;
mTextTexture->unload();
delete mTextTexture;
mTextTexture = nullptr;
for (int i = 0; i < INTRO_TOTAL_BITMAPS; i++)
{
delete mBitmap[i];
mBitmap[i] = nullptr;
}
for (int i = 0; i < INTRO_TOTAL_TEXTS; i++)
{
delete mWriter[i];
@@ -65,12 +67,14 @@ void Intro::init()
// Inicializa el vector de eventos de la intro
for (int i = 0; i < INTRO_TOTAL_EVENTS; i++)
{
mEvents[i] = EVENT_WAITING;
}
// Inicializa los bitmaps de la intro
for (int i = 0; i < INTRO_TOTAL_BITMAPS; i++)
{
mBitmap[i]->init(mBitmapTexture, mRenderer);
mBitmap[i]->init();
mBitmap[i]->setId(i);
mBitmap[i]->setIntroEvents(&mEvents[0]);
mBitmap[i]->setWidth(128);
@@ -89,7 +93,7 @@ void Intro::init()
mBitmap[0]->setAccelY(0.0f);
mBitmap[0]->setSpriteClip(0, 0, 128, 96);
mBitmap[1]->setPosX(SCREEN_WIDTH);
mBitmap[1]->setPosX(GAME_WIDTH);
mBitmap[1]->setPosY(SCREEN_FIRST_QUARTER_Y - 24);
mBitmap[1]->setVelX(-1.0f);
mBitmap[1]->setVelY(0.0f);
@@ -108,7 +112,7 @@ void Intro::init()
mBitmap[2]->setEnabledTimer(250);
mBitmap[3]->setPosX(SCREEN_CENTER_X - 64);
mBitmap[3]->setPosY(SCREEN_HEIGHT);
mBitmap[3]->setPosY(GAME_HEIGHT);
mBitmap[3]->setVelX(0.0f);
mBitmap[3]->setVelY(-0.7f);
mBitmap[3]->setAccelX(0.0f);
@@ -123,7 +127,7 @@ void Intro::init()
mBitmap[4]->setAccelY(0.3f);
mBitmap[4]->setSpriteClip(0, 192, 128, 96);
mBitmap[5]->setPosX(SCREEN_WIDTH);
mBitmap[5]->setPosX(GAME_WIDTH);
mBitmap[5]->setPosY(SCREEN_FIRST_QUARTER_Y - 24);
mBitmap[5]->setVelX(-0.7f);
mBitmap[5]->setVelY(0.0f);
@@ -138,7 +142,7 @@ void Intro::init()
mWriter[i]->setId(6 + i);
mWriter[i]->setIntroEvents(&mEvents[0]);
mWriter[i]->setPosX(BLOCK * 0);
mWriter[i]->setPosY(SCREEN_HEIGHT - (BLOCK * 6));
mWriter[i]->setPosY(GAME_HEIGHT - (BLOCK * 6));
mWriter[i]->setKerning(-1);
mWriter[i]->setEnabled(false);
mWriter[i]->setEnabledTimer(180);
@@ -181,9 +185,7 @@ void Intro::init()
mWriter[8]->setSpeed(20);
for (int i = 0; i < INTRO_TOTAL_TEXTS; i++)
{
mWriter[i]->center(SCREEN_CENTER_X);
}
}
// Carga los recursos
@@ -191,12 +193,8 @@ bool Intro::loadMedia()
{
bool success = true;
// Texturas
success &= loadTextureFromFile(mBitmapTexture, mFileList[33], mRenderer);
success &= loadTextureFromFile(mTextTexture, mFileList[28], mRenderer);
// Musicas
mMusic = JA_LoadMusic(mFileList[3].c_str());
mMusic = JA_LoadMusic(mAsset->get("intro.ogg").c_str());
return success;
}
@@ -206,42 +204,46 @@ section_t Intro::run()
{
init();
// Si la música no está sonando
// Si la música no está sonando la hace sonar
if ((JA_GetMusicState() == JA_MUSIC_INVALID) || (JA_GetMusicState() == JA_MUSIC_STOPPED))
{
// Reproduce la música
JA_PlayMusic(mMusic, 0);
}
while (mSection.name == PROG_SECTION_INTRO)
{
// Comprueba los eventos que hay en la cola
while (SDL_PollEvent(mEventHandler) != 0)
{
// Evento de salida de la aplicación
if (mEventHandler->type == SDL_QUIT)
{
mSection.name = PROG_SECTION_QUIT;
break;
}
if ((mEventHandler->type == SDL_KEYDOWN) || (mEventHandler->type == SDL_JOYBUTTONDOWN))
{
JA_StopMusic();
mSection = {PROG_SECTION_TITLE, TITLE_SECTION_1};
}
}
// Actualiza las variables
if (SDL_GetTicks() - mTicks > mTicksSpeed)
{
// Actualiza el contador de ticks
mTicks = SDL_GetTicks();
// Comprueba los eventos que hay en la cola
while (SDL_PollEvent(mEventHandler) != 0)
{
// Evento de salida de la aplicación
if (mEventHandler->type == SDL_QUIT)
{
mSection.name = PROG_SECTION_QUIT;
break;
}
if ((mEventHandler->type == SDL_KEYDOWN) || (mEventHandler->type == SDL_JOYBUTTONDOWN))
{
JA_StopMusic();
mSection = {PROG_SECTION_TITLE, TITLE_SECTION_1};
}
}
// Actualiza los objetos
for (int i = 0; i < INTRO_TOTAL_BITMAPS; i++)
{
mBitmap[i]->update();
}
for (int i = 0; i < INTRO_TOTAL_TEXTS; i++)
{
mWriter[i]->update();
}
// Guión de eventos
// Primera imagen - UPV
@@ -356,18 +358,24 @@ section_t Intro::run()
}
}
// Prepara para empezar a dibujar en la textura de juego
mScreen->start();
// Limpia la pantalla
SDL_SetRenderDrawColor(mRenderer, bgColor.r, bgColor.g, bgColor.b, 0xFF);
SDL_RenderClear(mRenderer);
mScreen->clean(bgColor);
// Dibuja los objetos
for (int i = 0; i < INTRO_TOTAL_BITMAPS; i++)
for (int i = 0; i < INTRO_TOTAL_BITMAPS; ++i)
{
mBitmap[i]->render();
for (int i = 0; i < INTRO_TOTAL_TEXTS; i++)
}
for (int i = 0; i < INTRO_TOTAL_TEXTS; ++i)
{
mWriter[i]->render();
}
// Actualiza la pantalla
SDL_RenderPresent(mRenderer);
// Vuelca el contenido del renderizador en pantalla
mScreen->blit();
}
return mSection;

View File

@@ -1,24 +1,48 @@
#pragma once
#include "ifdefs.h"
#include "const.h"
#include "utils.h"
#include "smartsprite.h"
#include "writer.h"
#include <SDL2/SDL.h>
#include "asset.h"
#include "const.h"
#include "jail_audio.h"
#include "screen.h"
#include "smartsprite.h"
#include "utils.h"
#include "writer.h"
#ifndef INTRO_H
#define INTRO_H
// Intro
// Cantidad de eventos de la intro
#define INTRO_TOTAL_BITMAPS 6
#define INTRO_TOTAL_TEXTS 9
const int INTRO_TOTAL_EVENTS = INTRO_TOTAL_BITMAPS + INTRO_TOTAL_TEXTS;
// Relaciones de Id con nombres
#define BITMAP0 0
#define BITMAP1 1
#define BITMAP2 2
#define BITMAP3 3
#define BITMAP4 4
#define BITMAP5 5
#define TEXT0 6
#define TEXT1 7
#define TEXT2 8
#define TEXT3 9
#define TEXT4 10
#define TEXT5 11
#define TEXT6 12
#define TEXT7 13
#define TEXT8 14
// Clase Intro
class Intro
{
private:
LTexture *mBitmapTexture; // Textura con los graficos
LTexture *mTextTexture; // Textura con los caracteres de texto
SDL_Event *mEventHandler; // Manejador de eventos
SDL_Renderer *mRenderer; // El renderizador de la ventana
std::string *mFileList; // Lista de ficheros
Screen *mScreen; // Objeto encargado de dibujar en pantalla
LTexture *mBitmapTexture; // Textura con los graficos
SDL_Event *mEventHandler; // Manejador de eventos
Asset *mAsset; // Objeto que gestiona todos los ficheros de recursos
Lang *mLang; // Objeto para gestionar los textos en diferentes idiomas
section_t mSection; // Estado del bucle principal para saber si continua o se sale
Uint32 mTicks; // Contador de ticks para ajustar la velocidad del programa
@@ -29,9 +53,12 @@ private:
Uint8 mEvents[INTRO_TOTAL_EVENTS]; // Vector para coordinar los eventos de la intro
JA_Music mMusic; // Musica para la intro
// Carga los recursos
bool loadMedia();
public:
// Constructor
Intro(SDL_Renderer *renderer, std::string *fileList, Lang *lang);
Intro(SDL_Renderer *renderer, Screen *screen, Asset *mAsset, Lang *lang);
// Destructor
~Intro();
@@ -39,9 +66,6 @@ public:
// Inicializa las variables
void init();
// Carga los recursos
bool loadMedia();
// Bucle principal
section_t run();
};

View File

@@ -1,12 +1,11 @@
#include "const.h"
#include "item.h"
#include <stdio.h>
// Constructor
Item::Item()
Item::Item(LTexture *texture, SDL_Renderer *renderer)
{
mSprite = new AnimatedSprite();
mClass = NO_KIND;
mSprite = new AnimatedSprite(texture, renderer);
init(NO_KIND, 0.0f, 0.0f);
}
// Destructor
@@ -16,10 +15,10 @@ Item::~Item()
mSprite = nullptr;
}
// Iniciador
void Item::init(Uint8 value, float x, float y, LTexture *texture, SDL_Renderer *renderer)
// Inicializador
void Item::init(Uint8 type, float x, float y)
{
mClass = value;
mClass = type;
mEnabled = true;
mTimeToLive = 600;
mPosX = x;
@@ -34,19 +33,17 @@ void Item::init(Uint8 value, float x, float y, LTexture *texture, SDL_Renderer *
mCollider.r = mWidth / 2;
shiftColliders();
mSprite->init(texture, renderer);
mSprite->setAnimationFrames(0, 0, 0, 48, mWidth, mHeight);
mSprite->setAnimationFrames(0, 1, 0, 64, mWidth, mHeight);
mSprite->setCurrentFrame(0);
mSprite->setAnimationCounter(0);
mSprite->setAnimationNumFrames(0, 2);
mSprite->setAnimationSpeed(0, 10);
mSprite->setAnimationLoop(0, true);
mSprite->setSpriteClip(mSprite->getAnimationClip(0, 0));
mSprite->setPosX(mPosX);
mSprite->setPosY(mPosY);
switch (value)
switch (type)
{
case NO_KIND:
mEnabled = false;
@@ -92,7 +89,6 @@ void Item::init(Uint8 value, float x, float y, LTexture *texture, SDL_Renderer *
mVelX = 0.0f;
mVelY = -0.1f;
mAccelY = 0.1f;
mSprite->setAnimationNumFrames(0, 4);
mSprite->setAnimationFrames(0, 0, 32 * 0, 16 * 2, mWidth, mHeight);
mSprite->setAnimationFrames(0, 1, 32 * 1, 16 * 2, mWidth, mHeight);
mSprite->setAnimationFrames(0, 2, 32 * 2, 16 * 2, mWidth, mHeight);
@@ -203,7 +199,7 @@ void Item::move()
// Pone a cero todos los valores del objeto
void Item::erase()
{
init(NO_KIND, 0, 0, nullptr, nullptr);
// init(NO_KIND, 0, 0, nullptr, nullptr);
}
// Actualiza el objeto a su posicion, animación y controla los contadores
@@ -213,7 +209,7 @@ void Item::update()
{
move();
shiftColliders();
mSprite->animate(0);
mSprite->animate();
updateTimeToLive();
checkTimeToLive();
}
@@ -286,8 +282,6 @@ circle_t &Item::getCollider()
// Alinea el circulo de colisión con la posición del objeto
void Item::shiftColliders()
{
//mCollider.x = int(mPosX + mCollider.r);
//mCollider.y = int(mPosY + mCollider.r);
mCollider.x = int(mPosX + (mWidth / 2));
mCollider.y = int(mPosY + (mHeight / 2));
}

View File

@@ -1,24 +1,34 @@
#pragma once
#include "ifdefs.h"
#include <SDL2/SDL.h>
#include "animatedsprite.h"
#include "utils.h"
#ifndef ITEM_H
#define ITEM_H
// Clase AnimatedSprite
// Tipos de objetos
#define ITEM_POINTS_1_DISK 1
#define ITEM_POINTS_2_GAVINA 2
#define ITEM_POINTS_3_PACMAR 3
#define ITEM_CLOCK 4
#define ITEM_COFFEE 5
#define ITEM_POWER_BALL 6
#define ITEM_COFFEE_MACHINE 7
// Clase Item
class Item
{
private:
float mPosX; // Posición X del objeto
float mPosY; // Posición Y del objeto
Uint8 mWidth; // Ancho del objeto
Uint8 mHeight; // Alto del objeto
float mVelX; // Velocidad en el eje X
float mVelY; // Velocidad en el eje Y
float mAccelX; // Aceleración en el eje X
float mAccelY; // Aceleración en el eje Y
bool mFloorCollision; // Indica si el objeto colisiona con el suelo
float mPosX; // Posición X del objeto
float mPosY; // Posición Y del objeto
Uint8 mWidth; // Ancho del objeto
Uint8 mHeight; // Alto del objeto
float mVelX; // Velocidad en el eje X
float mVelY; // Velocidad en el eje Y
float mAccelX; // Aceleración en el eje X
float mAccelY; // Aceleración en el eje Y
bool mFloorCollision; // Indica si el objeto colisiona con el suelo
AnimatedSprite *mSprite; // Sprite con los graficos del objeto
@@ -30,14 +40,16 @@ private:
void shiftColliders();
public:
Uint16 mTimeToLive; // Temporizador con el tiempo que el objeto está presente
// Constructor
Item();
Item(LTexture *texture, SDL_Renderer *renderer);
// Destructor
~Item();
// Iniciador
void init(Uint8 value, float x, float y, LTexture *texture, SDL_Renderer *renderer);
// Inicializador
void init(Uint8 type, float x, float y);
// Centra el objeto en la posición X
void allignTo(int x);
@@ -84,9 +96,6 @@ public:
// Obtiene el circulo de colisión
circle_t &getCollider();
// Temporizador con el tiempo que el objeto está presente
Uint16 mTimeToLive;
// Informa si el objeto ha colisionado con el suelo
bool floorCollision();
};

View File

@@ -1,6 +1,7 @@
#ifndef __MIPSEL__
#include "jail_audio.h"
#include "stb_vorbis.c"
#include <SDL2/SDL.h>
#include <stdio.h>
#define JA_MAX_SIMULTANEOUS_CHANNELS 5
@@ -30,16 +31,17 @@ 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;
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_memcpy(stream, current_music->output+current_music->pos, size);
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_memcpy(stream+size, current_music->output, len-size);
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 {
@@ -52,11 +54,11 @@ void audioCallback(void * userdata, uint8_t * stream, int len) {
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, 64);
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, 64);
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 {
@@ -79,7 +81,19 @@ void JA_Init(const int freq, const SDL_AudioFormat format, const int channels) {
JA_Music JA_LoadMusic(const char* filename) {
int chan, samplerate;
JA_Music music = new JA_Music_t();
music->samples = stb_vorbis_decode_filename(filename, &chan, &samplerate, &music->output);
// [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);
fread(buffer, fsize, 1, f);
fclose(f);
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);
@@ -134,6 +148,13 @@ void JA_DeleteMusic(JA_Music music) {
delete music;
}
JA_Sound JA_NewSound(Uint8* buffer, Uint32 length) {
JA_Sound sound = new JA_Sound_t();
sound->buffer = buffer;
sound->length = length;
return sound;
}
JA_Sound JA_LoadSound(const char* filename) {
JA_Sound sound = new JA_Sound_t();
SDL_AudioSpec wavSpec;
@@ -210,4 +231,8 @@ JA_Channel_state JA_GetChannelState(const int channel) {
if (channel < 0 || channel >= JA_MAX_SIMULTANEOUS_CHANNELS) return JA_CHANNEL_INVALID;
return channels[channel].state;
}
#endif
int JA_SetVolume(int volume) {
JA_volume = volume > 128 ? 128 : volume < 0 ? 0 : volume;
return JA_volume;
}

View File

@@ -1,5 +1,5 @@
#pragma once
#include "ifdefs.h"
#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 };
@@ -17,6 +17,7 @@ void JA_StopMusic();
JA_Music_state JA_GetMusicState();
void JA_DeleteMusic(JA_Music music);
JA_Sound JA_NewSound(Uint8* buffer, Uint32 length);
JA_Sound JA_LoadSound(const char* filename);
int JA_PlaySound(JA_Sound sound, const int loop = 0);
void JA_PauseChannel(const int channel);
@@ -24,3 +25,5 @@ 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 sound);
int JA_SetVolume(int volume);

View File

@@ -1,106 +0,0 @@
#ifdef __MIPSEL__
#include "jail_audio.h"
#include "SDL_mixer.h"
struct JA_Sound_t {
Mix_Chunk *mix_chunk;
};
struct JA_Music_t {
Mix_Music* mix_music;
};
JA_Music current_music{NULL};
void JA_Init(const int freq, const SDL_AudioFormat format, const int channels) {
Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 1024);
Mix_AllocateChannels(8);
}
JA_Music JA_LoadMusic(const char* filename) {
int chan, samplerate;
JA_Music music = new JA_Music_t();
music->mix_music = Mix_LoadMUS(filename);
return music;
}
void JA_PlayMusic(JA_Music music, const int loop) {
if (current_music == music) return;
if (current_music != NULL) {
Mix_HaltMusic();
}
current_music = music;
Mix_PlayMusic(music->mix_music, loop);
}
void JA_PauseMusic() {
Mix_PauseMusic();
}
void JA_ResumeMusic() {
Mix_ResumeMusic();
}
void JA_StopMusic() {
Mix_HaltMusic();
}
JA_Music_state JA_GetMusicState() {
if (current_music == NULL) return JA_MUSIC_INVALID;
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 music) {
if (current_music == music) {
Mix_HaltMusic();
current_music = NULL;
}
Mix_FreeMusic(music->mix_music);
delete music;
}
JA_Sound JA_LoadSound(const char* filename) {
JA_Sound sound = new JA_Sound_t();
sound->mix_chunk = Mix_LoadWAV(filename);
return sound;
}
int JA_PlaySound(JA_Sound sound, const int loop) {
int channel = Mix_PlayChannel(-1, sound->mix_chunk, loop);
return channel;
}
void JA_DeleteSound(JA_Sound sound) {
Mix_FreeChunk(sound->mix_chunk);
delete 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;
}
}
#endif

View File

@@ -3,9 +3,9 @@
#include <fstream>
// Constructor
Lang::Lang(std::string *fileList)
Lang::Lang(Asset *mAsset)
{
mFileList = fileList;
this->mAsset = mAsset;
}
// Destructor
@@ -21,19 +21,19 @@ bool Lang::setLang(Uint8 lang)
switch (lang)
{
case es_ES:
file = mFileList[49];
file = mAsset->get("es_ES.txt");
break;
case en_UK:
file = mFileList[50];
file = mAsset->get("en_UK.txt");
break;
case ba_BA:
file = mFileList[51];
file = mAsset->get("ba_BA.txt");
break;
default:
file = mFileList[50];
file = mAsset->get("en_UK.txt");
break;
}

View File

@@ -1,11 +1,13 @@
#pragma once
#include "ifdefs.h"
#include <SDL2/SDL.h>
#include "asset.h"
#include <string>
#ifndef LANG_H
#define LANG_H
// Lang codes
// Códigos de idioma
#define es_ES 0
#define ba_BA 1
#define en_UK 2
@@ -18,15 +20,15 @@
class Lang
{
private:
std::string *mFileList; // Lista de ficheros con los recursos
std::string mTextStrings[MAX_TEXT_STRINGS];
Asset *mAsset; // Objeto que gestiona todos los ficheros de recursos
std::string mTextStrings[MAX_TEXT_STRINGS]; // Vector con los textos
public:
// Constructor
Lang(std::string *fileList);
// Constructor
Lang(Asset *mAsset);
// Destructor
~Lang();
// Destructor
~Lang();
// Inicializa los textos del juego en el idioma seleccionado
bool setLang(Uint8 lang);

View File

@@ -1,146 +1,157 @@
#include "logo.h"
#ifdef __MIPSEL__
#include <sys/stat.h>
#include <dirent.h>
#endif
# define INIT_FADE 100
# define END_LOGO 200
#define INIT_FADE 100
#define END_LOGO 200
// Constructor
Logo::Logo(SDL_Renderer *renderer, std::string *fileList)
Logo::Logo(SDL_Renderer *renderer, Screen *screen, Asset *asset)
{
// Copia la dirección del renderizador
mRenderer = renderer;
// Copia la dirección del la lista de ficheros
mFileList = fileList;
// Copia la dirección de los objetos
this->renderer = renderer;
this->screen = screen;
this->asset = asset;
// Reserva memoria para los punteros
mEventHandler = new SDL_Event();
mTexture = new LTexture();
mSprite = new Sprite();
eventHandler = new SDL_Event();
texture = new LTexture(renderer, asset->get("logo.png"));
sprite = new Sprite(0, 0, GAME_WIDTH, GAME_HEIGHT, texture, renderer);
// Crea un backbuffer para el renderizador
mBackbuffer = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, SCREEN_WIDTH, SCREEN_HEIGHT);
if (mBackbuffer == NULL)
backbuffer = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAME_WIDTH, GAME_HEIGHT);
if (backbuffer == nullptr)
{
printf("Backbuffer could not be created!\nSDL Error: %s\n", SDL_GetError());
}
// Inicializa variables
counter = 0;
section.name = PROG_SECTION_LOGO;
section.subsection = 0;
ticks = 0;
ticksSpeed = 15;
}
// Destructor
Logo::~Logo()
{
mTexture->unload();
delete mTexture;
mTexture = nullptr;
SDL_DestroyTexture(backbuffer);
delete mSprite;
mSprite = nullptr;
texture->unload();
delete texture;
delete mEventHandler;
mEventHandler = nullptr;
SDL_DestroyTexture(mBackbuffer);
mBackbuffer = nullptr;
delete sprite;
delete eventHandler;
}
// Inicializa las variables necesarias para la sección 'Logo'
void Logo::init()
// Comprueba si ha terminado el logo
void Logo::checkLogoEnd()
{
// Carga los recursos
loadMedia();
if (counter == 0)
{
if (JA_GetMusicState() == JA_MUSIC_PLAYING)
{
JA_StopMusic();
}
}
// Inicializa variables
mCounter = 0;
mSection.name = PROG_SECTION_LOGO;
mSection.subsection = 0;
mSprite->init(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, mTexture, mRenderer);
mTicks = 0;
mTicksSpeed = 15;
if (counter == END_LOGO + 20)
{
counter = 0;
section.name = PROG_SECTION_INTRO;
section.subsection = 0;
}
else
{
counter++;
}
}
// Carga los recursos necesarios para la sección 'Logo'
bool Logo::loadMedia()
// Comprueba los eventos
void Logo::checkEventHandler()
{
bool success = true;
// Comprueba los eventos que hay en la cola
while (SDL_PollEvent(eventHandler) != 0)
{
// Evento de salida de la aplicación
if (eventHandler->type == SDL_QUIT)
{
section.name = PROG_SECTION_QUIT;
break;
}
success &= loadTextureFromFile(mTexture, mFileList[35], mRenderer);
// Cualquier tecla pulsada
if ((eventHandler->type == SDL_KEYDOWN) || (eventHandler->type == SDL_JOYBUTTONDOWN))
{
section.name = PROG_SECTION_TITLE;
section.subsection = TITLE_SECTION_1;
}
}
}
return success;
// Dibuja el fade
void Logo::renderFade()
{
const SDL_Rect rect = {0, 0, GAME_WIDTH, GAME_HEIGHT};
const int fadeLenght = END_LOGO - INIT_FADE;
// Dibuja el fade
if (counter >= INIT_FADE)
{
const Uint16 alpha = (255 * (counter - INIT_FADE)) / fadeLenght;
if (alpha < 256)
{
SDL_SetRenderDrawColor(renderer, bgColor.r, bgColor.g, bgColor.b, alpha);
}
else
{
SDL_SetRenderDrawColor(renderer, bgColor.r, bgColor.g, bgColor.b, 255);
}
SDL_RenderFillRect(renderer, &rect);
}
}
// Actualiza las variables del objeto
void Logo::update()
{
checkEventHandler();
if (SDL_GetTicks() - ticks > ticksSpeed)
{
// Actualiza el contador de ticks
ticks = SDL_GetTicks();
// Comprueba si ha terminado el logo
checkLogoEnd();
}
}
// Dibuja el objeto en pantalla
void Logo::render()
{
// Prepara para empezar a dibujar en la textura de juego
screen->start();
// Limpia la pantalla
screen->clean(bgColor);
// Dibuja los objetos
sprite->render();
// Dibuja el fade
renderFade();
// Vuelca el contenido del renderizador en pantalla
screen->blit();
}
// Bucle para el logo del juego
section_t Logo::run()
{
init();
const SDL_Rect rect = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
const int fadeLenght = END_LOGO - INIT_FADE;
while (mSection.name == PROG_SECTION_LOGO)
while (section.name == PROG_SECTION_LOGO)
{
// Comprueba los eventos que hay en la cola
while (SDL_PollEvent(mEventHandler) != 0)
{
// Evento de salida de la aplicación
if (mEventHandler->type == SDL_QUIT)
{
mSection.name = PROG_SECTION_QUIT;
break;
}
// Cualquier tecla pulsada
if ((mEventHandler->type == SDL_KEYDOWN) || (mEventHandler->type == SDL_JOYBUTTONDOWN))
{
mSection.name = PROG_SECTION_TITLE;
mSection.subsection = TITLE_SECTION_1;
}
}
// Limpia el destino
SDL_SetRenderDrawColor(mRenderer, bgColor.r, bgColor.g, bgColor.b, 255);
SDL_RenderClear(mRenderer);
// Dibuja los objetos
mSprite->render();
// Dibuja el fade
if (mCounter >= INIT_FADE)
{
Uint16 alpha = (255 * (mCounter - INIT_FADE)) / fadeLenght;
if (alpha < 256)
SDL_SetRenderDrawColor(mRenderer, bgColor.r, bgColor.g, bgColor.b, alpha);
else
SDL_SetRenderDrawColor(mRenderer, bgColor.r, bgColor.g, bgColor.b, 255);
SDL_RenderFillRect(mRenderer, &rect);
}
// Actualiza la pantalla
SDL_RenderPresent(mRenderer);
// Comprueba si ha terminado el logo
if (SDL_GetTicks() - mTicks > mTicksSpeed)
{
// Actualiza el contador de ticks
mTicks = SDL_GetTicks();
if (mCounter == 0)
{
if (JA_GetMusicState() == JA_MUSIC_PLAYING)
JA_StopMusic();
}
if (mCounter == END_LOGO + 20)
{
mCounter = 0;
mSection.name = PROG_SECTION_INTRO;
mSection.subsection = 0;
}
else
{
mCounter++;
}
}
update();
render();
}
return mSection;
return section;
}

View File

@@ -1,42 +1,54 @@
#pragma once
#include "ifdefs.h"
#include "const.h"
#include "utils.h"
#include "sprite.h"
#include <SDL2/SDL.h>
#include "asset.h"
#include "const.h"
#include "jail_audio.h"
#include "screen.h"
#include "sprite.h"
#include "utils.h"
#ifndef LOGO_H
#define LOGO_H
// Logo
// Clase Logo
class Logo
{
private:
LTexture *mTexture; // Textura con los graficos
SDL_Event *mEventHandler; // Manejador de eventos
SDL_Renderer *mRenderer; // El renderizador de la ventana
SDL_Texture *mBackbuffer; // Textura para usar como backbuffer
Sprite *mSprite; // Sprite con la textura del logo
std::string *mFileList; // Lista de ficheros
Uint16 mCounter; // Contador
section_t mSection; // Estado del bucle principal para saber si continua o se sale
Uint32 mTicks; // Contador de ticks para ajustar la velocidad del programa
Uint8 mTicksSpeed; // Velocidad a la que se repiten los bucles del programa
SDL_Renderer *renderer; // El renderizador de la ventana
Screen *screen; // Objeto encargado de dibujar en pantalla
Asset *asset; // Objeto que gestiona todos los ficheros de recursos
LTexture *texture; // Textura con los graficos
SDL_Event *eventHandler; // Manejador de eventos
SDL_Texture *backbuffer; // Textura para usar como backbuffer
Sprite *sprite; // Sprite con la textura del logo
int counter; // Contador
section_t section; // Estado del bucle principal para saber si continua o se sale
Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa
Uint32 ticksSpeed; // Velocidad a la que se repiten los bucles del programa
// Actualiza las variables del objeto
void update();
// Dibuja el objeto en pantalla
void render();
// Comprueba si ha terminado el logo
void checkLogoEnd();
// Comprueba los eventos
void checkEventHandler();
// Dibuja el fade
void renderFade();
public:
// Constructor
Logo(SDL_Renderer *renderer, std::string *fileList);
Logo(SDL_Renderer *renderer, Screen *screen, Asset *mAsset);
// Destructor
~Logo();
// Inicializa las variables
void init();
// Carga los recursos
bool loadMedia();
// Bucle principal
section_t run();
};

View File

@@ -1,28 +1,41 @@
#include "const.h"
#include "ltexture.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
LTexture::LTexture()
// Constructor
LTexture::LTexture(SDL_Renderer *renderer, std::string path)
{
// Initialize
mTexture = NULL;
mWidth = 0;
mHeight = 0;
// 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);
}
}
// Destructor
LTexture::~LTexture()
{
// Deallocate
// Libera memoria
unload();
}
// Carga una imagen desde un fichero
bool LTexture::loadFromFile(std::string path, SDL_Renderer *renderer)
{
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 == NULL)
if (data == nullptr)
{
SDL_Log("Loading image failed: %s", stbi_failure_reason());
exit(1);
@@ -33,7 +46,7 @@ bool LTexture::loadFromFile(std::string path, SDL_Renderer *renderer)
if (req_format == STBI_rgb)
{
depth = 24;
pitch = 3 * width; // 3 bytes per pixel * pixels per row
pitch = 3 * width; // 3 bytes por pixel * pixels per linea
pixel_format = SDL_PIXELFORMAT_RGB24;
}
else
@@ -43,100 +56,99 @@ bool LTexture::loadFromFile(std::string path, SDL_Renderer *renderer)
pixel_format = SDL_PIXELFORMAT_RGBA32;
}
// Get rid of preexisting texture
// Limpia
unload();
// The final texture
SDL_Texture *newTexture = NULL;
// La textura final
SDL_Texture *newTexture = nullptr;
// Load image at specified path
//SDL_Surface *loadedSurface = IMG_Load(path.c_str());
// Carga la imagen desde una ruta específica
SDL_Surface *loadedSurface = SDL_CreateRGBSurfaceWithFormatFrom((void *)data, width, height, depth, pitch, pixel_format);
if (loadedSurface == NULL)
if (loadedSurface == nullptr)
{
printf("Unable to load image %s!\n", path.c_str());
}
else
{
// Color key image
//SDL_SetColorKey(loadedSurface, SDL_TRUE, SDL_MapRGB(loadedSurface->format, COLOR_KEY_R, COLOR_KEY_G, COLOR_KEY_B));
// Create texture from surface pixels
// Crea la textura desde los pixels de la surface
newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
if (newTexture == NULL)
if (newTexture == nullptr)
{
printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
}
else
{
// Get image dimensions
mWidth = loadedSurface->w;
mHeight = loadedSurface->h;
// Obtiene las dimensiones de la imagen
this->width = loadedSurface->w;
this->height = loadedSurface->h;
}
// Get rid of old loaded surface
// Elimina la textura cargada
SDL_FreeSurface(loadedSurface);
}
// Return success
mTexture = newTexture;
return mTexture != NULL;
texture = newTexture;
return texture != nullptr;
}
// Crea una textura en blanco
bool LTexture::createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess access)
{
// Create uninitialized texture
mTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, access, width, height);
if (mTexture == NULL)
// Crea una textura sin inicializar
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, access, width, height);
if (texture == nullptr)
{
printf("Unable to create blank texture! SDL Error: %s\n", SDL_GetError());
}
else
{
mWidth = width;
mHeight = height;
this->width = width;
this->height = height;
}
return mTexture != NULL;
return texture != nullptr;
}
// Libera la memoria de la textura
void LTexture::unload()
{
// Free texture if it exists
if (mTexture != NULL)
// Libera la textura si existe
if (texture != nullptr)
{
SDL_DestroyTexture(mTexture);
mTexture = NULL;
mWidth = 0;
mHeight = 0;
SDL_DestroyTexture(texture);
texture = nullptr;
width = 0;
height = 0;
}
}
// Establece el color para la modulacion
void LTexture::setColor(Uint8 red, Uint8 green, Uint8 blue)
{
// Modulate texture rgb
SDL_SetTextureColorMod(mTexture, red, green, blue);
SDL_SetTextureColorMod(texture, red, green, blue);
}
// Establece el blending
void LTexture::setBlendMode(SDL_BlendMode blending)
{
// Set blending function
SDL_SetTextureBlendMode(mTexture, blending);
SDL_SetTextureBlendMode(texture, blending);
}
// Establece el alpha para la modulación
void LTexture::setAlpha(Uint8 alpha)
{
// Modulate texture alpha
SDL_SetTextureAlphaMod(mTexture, alpha);
SDL_SetTextureAlphaMod(texture, alpha);
}
// Renderiza la textura en un punto específico
void LTexture::render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip, float zoomW, float zoomH, double angle, SDL_Point *center, SDL_RendererFlip flip)
{
// Set rendering space and render to screen
SDL_Rect renderQuad = {x, y, mWidth, mHeight};
// Establece el destini de renderizado en la pantalla
SDL_Rect renderQuad = {x, y, width, height};
// Set clip rendering dimensions
if (clip != NULL)
// Obtiene las dimesiones del clip de renderizado
if (clip != nullptr)
{
renderQuad.w = clip->w;
renderQuad.h = clip->h;
@@ -145,22 +157,30 @@ void LTexture::render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip, floa
renderQuad.w = renderQuad.w * zoomW;
renderQuad.h = renderQuad.h * zoomH;
// Render to screen
SDL_RenderCopyEx(renderer, mTexture, clip, &renderQuad, angle, center, flip);
// Renderiza a pantalla
SDL_RenderCopyEx(renderer, texture, clip, &renderQuad, angle, center, flip);
}
// Establece la textura como objetivo de renderizado
void LTexture::setAsRenderTarget(SDL_Renderer *renderer)
{
// Make self render target
SDL_SetRenderTarget(renderer, mTexture);
SDL_SetRenderTarget(renderer, texture);
}
// Obtiene el ancho de la imagen
int LTexture::getWidth()
{
return mWidth;
return width;
}
// Obtiene el alto de la imagen
int LTexture::getHeight()
{
return mHeight;
return height;
}
// Recarga la textura
bool LTexture::reLoad()
{
return loadFromFile(path, renderer);
}

View File

@@ -1,66 +1,61 @@
#pragma once
#include "ifdefs.h"
#include <SDL2/SDL.h>
#include <stdio.h>
#include <string>
#ifndef LTEXTURE_H
#define LTEXTURE_H
// Texture wrapper class
// Clase LTexture
class LTexture
{
public:
// Initializes variables
LTexture();
private:
SDL_Texture *texture; // La textura
SDL_Renderer *renderer; // Renderizador donde dibujar la textura
int width; // Ancho de la imagen
int height; // Alto de la imagen
std::string path; // Ruta de la imagen de la textura
// Deallocates memory
public:
// Constructor
LTexture(SDL_Renderer *renderer, std::string path = "");
// Destructor
~LTexture();
// Loads image at specified path
// Carga una imagen desde un fichero
bool loadFromFile(std::string path, SDL_Renderer *renderer);
// Creates blank texture
// Crea una textura en blanco
bool createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess = SDL_TEXTUREACCESS_STREAMING);
// Deallocates texture
// Libera la memoria de la textura
void unload();
// Set color modulation
// Establece el color para la modulacion
void setColor(Uint8 red, Uint8 green, Uint8 blue);
// Set blending
// Establece el blending
void setBlendMode(SDL_BlendMode blending);
// Set alpha modulation
// Establece el alpha para la modulación
void setAlpha(Uint8 alpha);
// Renders texture at given point
void render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip = NULL, float zoomW = 1, float zoomH = 1, double angle = 0.0, SDL_Point *center = NULL, SDL_RendererFlip flip = SDL_FLIP_NONE);
// 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);
// Set self as render target
// Establece la textura como objetivo de renderizado
void setAsRenderTarget(SDL_Renderer *renderer);
// Gets image dimensions
// Obtiene el ancho de la imagen
int getWidth();
// Obtiene el alto de la imagen
int getHeight();
// Pixel manipulators
bool lockTexture();
bool unlockTexture();
void *getPixels();
void copyPixels(void *pixels);
int getPitch();
Uint32 getPixel32(unsigned int x, unsigned int y);
private:
// The actual hardware texture
SDL_Texture *mTexture;
void *mPixels;
int mPitch;
// Image dimensions
int mWidth;
int mHeight;
// Recarga la textura
bool reLoad();
};
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -1,95 +1,112 @@
#pragma once
#include "ifdefs.h"
#include <SDL2/SDL.h>
#include <vector>
#include "sprite.h"
#include "text.h"
#include "asset.h"
#include "input.h"
#include "utils.h"
#include "jail_audio.h"
#include <sstream>
#include <fstream>
#ifndef MENU_H
#define MENU_H
#define MENU_MAX_ITEMS 50
// Tipos de fondos para el menu
#define MENU_BACKGROUND_TRANSPARENT 0
#define MENU_BACKGROUND_SOLID 1
// Clase menu
// 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:
std::string mName; // Nombre del menu
int mPosX; // Posición en el eje X de la primera letra del primer elemento
int mPosY; // Posición en el eje Y de la primera letra del primer elemento
Uint16 mHeight; // Altura del menu
Uint16 mWidth; // Anchura del menu
Uint8 mTotalItems; // Numero total de items del menu
int mItemSelected; // Índice del item del menu que ha sido seleccionado
Uint8 mDefaultActionWhenCancel; // Indice del item del menu que se selecciona cuando se cancela el menu
Uint8 mBackgroundType; // Tipo de fondo para el menu
bool mIsCenteredOnX; // Variable para saber si el menu debe estar centrado respecto a un punto en el eje X
bool mIsCenteredOnY; // Variable para saber si el menu debe estar centrado respecto a un punto en el eje Y
int mCenterX; // Centro del menu en el eje X
int mCenterY; // Centro del menu en el eje Y
bool mAreElementsCenteredOnX; // Variable para saber si los elementos van centrados en el eje X
Uint16 mWidestItem; // Anchura del elemento más ancho
JA_Sound mSoundAccept; // Sonido al aceptar o elegir una opción del menu
JA_Sound mSoundCancel; // Sonido al cancelar el menu
JA_Sound mSoundMove; // Sonido al mover el selector
SDL_Renderer *mRenderer; // Puntero al renderizador de la ventana
std::string *mFileList; // Lista de ficheros
Text *mText; // Texto para poder escribir los items del menu
Input *mInput; // Gestor de eventos de entrada de teclado o gamepad
color_t mColorGreyed; // Color para los elementos agrisados
struct rectangle
struct rectangle_t
{
SDL_Rect rect; // Rectangulo
Uint8 r; // Rojo
Uint8 g; // Verde
Uint8 b; // Azul
Uint8 a; // Transparencia
color_t color; // Color
int a; // Transparencia
};
rectangle mRectBG; // Rectangulo de fondo del menu
struct item
struct item_t
{
std::string label; // Texto
SDL_Rect rect; // Rectangulo que delimita el elemento
Uint8 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
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
};
item mItem[MENU_MAX_ITEMS]; // Estructura para cada elemento del menu
struct 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
Uint8 numJumps; // Numero de pasos preestablecido para llegar al destino
Uint8 index; // Elemento del menu que tiene el foco
SDL_Rect rect; // Rectangulo del selector
Uint8 r; // Cantidad de color rojo para el rectangulo del selector
Uint8 g; // Cantidad de color verde para el rectangulo del selector
Uint8 b; // Cantidad de color azul para el rectangulo del selector
Uint8 a; // Cantidad de transparencia para el rectangulo del selector
Uint8 itemR; // Cantidad de color rojo para el texto del elemento seleccionado
Uint8 itemG; // Cantidad de color verde para el texto del elemento seleccionado
Uint8 itemB; // Cantidad de color azul para el texto del elemento seleccionado
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
};
selector mSelector; // Variables para pintar el selector del menu
// Carga los recursos necesarios para la sección 'Title'
bool loadMedia();
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 soundAccept; // Sonido al aceptar o elegir una opción del menu
JA_Sound soundCancel; // Sonido al cancelar el menu
JA_Sound soundMove; // Sonido al mover el selector
SDL_Renderer *renderer; // Puntero al renderizador de la ventana
Text *text; // Texto para poder escribir los items del menu
Input *input; // Gestor de eventos de entrada de teclado o gamepad
Asset *asset; // Objeto para gestionar los ficheros de recursos
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;
// Establece el valor de la variable
void setTotalItems(int num);
// 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);
// Inicializa las variables
void init();
// Establece el rectangulo de fondo del menu
void setRectSize();
@@ -107,16 +124,16 @@ private:
void updateSelector();
// Obtiene la anchura del elemento más ancho del menu
Uint16 getWidestItem();
int getWidestItem();
// Gestiona la entrada de teclado y mando durante el menu
void checkMenuInput(Menu *menu);
// Calcula el ancho del menu
Uint16 findWidth();
int findWidth();
// Calcula el alto del menu
Uint16 findHeight();
int findHeight();
// Recoloca los elementos del menu en el eje Y
void replaceElementsOnY();
@@ -126,19 +143,19 @@ private:
public:
// Constructor
Menu(SDL_Renderer *renderer, Text *text, Input *input, std::string *fileList);
Menu(SDL_Renderer *renderer, Asset *asset, Input *input, std::string file="");
// Destructor
~Menu();
// Inicializador
void init(std::string name, int x, int y, int backgroundType);
// 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
Uint8 getItemSelected();
int getItemSelected();
// Deja el menu apuntando al primer elemento
void reset();
@@ -153,13 +170,13 @@ public:
void render();
// Establece el color del rectangulo de fondo
void setBackgroundColor(int r, int g, int b, int alpha);
void setBackgroundColor(color_t color, int alpha);
// Establece el color del rectangulo del selector
void setSelectorColor(int r, int g, int b, int alpha);
void setSelectorColor(color_t color, int alpha);
// Establece el color del texto del selector
void setSelectorTextColor(int r, int g, int b);
void setSelectorTextColor(color_t color);
// Centra el menu respecto a un punto en el eje X
void centerMenuOnX(int value);
@@ -171,25 +188,37 @@ public:
void centerMenuElementsOnX();
// Añade un item al menu
void addItem(std::string text, Uint8 hPaddingDown = 1, bool selectable = true, bool greyed = false, bool linkedDown = false);
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(Uint8 index, std::string text);
void setItemCaption(int index, std::string text);
// Establece el indice del item que se usará por defecto al cancelar el menu
void setDefaultActionWhenCancel(Uint8 item);
void setDefaultActionWhenCancel(int item);
// Coloca el selector en una posición específica
void setSelectorPos(Uint8 index);
void setSelectorPos(int index);
// Establece el estado seleccionable de un item
void setSelectable(Uint8 index, bool value);
void setSelectable(int index, bool value);
// Establece el estado agrisado de un item
void setGreyed(Uint8 index, bool value);
void setGreyed(int index, bool value);
// Establece el estado de enlace de un item
void setLinkedDown(Uint8 index, bool value);
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);
};
#endif

Some files were not shown because too many files have changed in this diff Show More