32 Commits
v0.1 ... v0.2

Author SHA1 Message Date
a1eaf8af07 Implementadas las animaciones en el jugador 2022-08-13 12:14:31 +02:00
e85f138be5 Finalizado el nuevo motor de animaciones 2022-08-13 11:07:04 +02:00
d2a2a1625d Trabajando en la carga de las animaciones 2022-08-12 23:44:31 +02:00
99005329de Trabajando en la clase animatedsprite 2022-08-12 13:55:56 +02:00
71897291ff Comienzo de la reescritura de la clase animatedsprite 2022-08-12 12:59:32 +02:00
60a969a9bc Ya se dibuja el grafico de fondo en el mapa 2022-08-12 12:43:51 +02:00
0afc0cf629 El jugador ya empieza a moverse 2022-08-12 12:29:35 +02:00
2740ae9b7e Ya pinta al jugador 2022-08-12 12:16:49 +02:00
fcd9cf3b79 Cambiado el nombre a una variable 2022-08-11 21:16:00 +02:00
5ec9cc4fc2 Ya empieza a pintar el mapa 2022-08-11 21:13:51 +02:00
89a43b3529 Trabajando en la clase screen 2022-08-11 18:39:56 +02:00
09ad78b02e Trabajando en prog.cpp 2022-08-11 13:58:51 +02:00
57ddf60c1b Borrado todo el código y vuelta a empezar 2022-08-11 09:19:49 +02:00
1301ef6a27 Eliminado el código del volcano_2016 del repositorio 2022-08-10 20:51:01 +02:00
3e9051f02d Añadidas mas funciones del código viejo 2022-08-10 20:49:40 +02:00
b7a2273d49 Añadiendo funciones viejas a la clase Map 2022-08-10 19:50:46 +02:00
2e4e549fd8 Añadido el audio al jugador 2022-08-10 13:57:17 +02:00
d216df7baf Eliminados los métodos init y reset de la clase player 2022-08-09 17:34:01 +02:00
61297ff340 Actualizado renderGame y screen.h 2022-08-09 17:23:58 +02:00
63bf42880e Modificado unLoadMedia por saveConfig 2022-08-09 17:16:12 +02:00
d7bd5a8ab1 Modificado loadMedia por loadConfig 2022-08-09 17:12:40 +02:00
4d1a08a300 Integrada la clase asset. Corregidos algunos fallos en el bucle principal 2022-08-09 13:58:32 +02:00
ff341d8cee Trabajando en integrar el objeto asset y mejorar los objetos player y map 2022-08-08 23:05:13 +02:00
0fa0368cca A medio integrar el objeto asset 2022-08-08 21:19:10 +02:00
4439f8477a Modificado el bucle principal 2022-08-08 20:12:51 +02:00
1801c23957 Añadidas las librerias asset y screen 2022-08-08 19:44:50 +02:00
a3c846c137 Actualizadas todas las librerias de sprites 2022-08-08 19:37:55 +02:00
c5acab7828 Actualizada la librería sprite.h 2022-08-08 19:25:03 +02:00
a180f1db46 Quitado el fichero menu.h y sus referencias 2022-08-08 18:41:57 +02:00
55c26df2a5 Actualizado el fichero utils.h a una versión posterior 2022-08-08 18:39:51 +02:00
386039336e Actualizada la libreria text.h a la última versión 2022-08-08 18:37:42 +02:00
2b2b822540 Actualizado gitignore 2022-08-08 18:35:04 +02:00
56 changed files with 3834 additions and 5443 deletions

2
.gitignore vendored
View File

@@ -1,4 +1,4 @@
.vscode/* .vscode/*
.DS_Store .DS_Store
thumbs.db thumbs.db
bin bin/*

View File

@@ -0,0 +1,31 @@
png_width_in_tiles=8
tile_width=16
tile_height=24
[animation]
name=stand
speed=8
loop=yes
frames=0,1,2,2,1,0,0,1,2,2,1,0,0,1,2,2,1,0,0,1,2,2,1,0,0,1,2,3,4,5,6,7,0,0,0,0
[/animation]
[animation]
name=walk
speed=6
loop=yes
frames=8,9,10,10,9,8,11,12,13,13,14,15
[/animation]
[animation]
name=jump
speed=10
loop=yes
frames=16
[/animation]
[animation]
name=death
speed=10
loop=no
frames=24,25,26,27,28,29,30,31
[/animation]

1144
data/gamecontrollerdb.txt Normal file

File diff suppressed because it is too large Load Diff

38
data/map/01 - copia.tmx Normal file
View File

@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.9" tiledversion="1.9.0" orientation="orthogonal" renderorder="right-down" width="20" height="13" tilewidth="16" tileheight="16" infinite="0" nextlayerid="3" nextobjectid="1">
<tileset firstgid="1" source="../../../volcano_2022_resources/tilesets/surface.tsx"/>
<layer id="2" name="actors" width="20" height="13">
<data encoding="csv">
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,225,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
</data>
</layer>
<layer id="1" name="tiles" width="20" height="13">
<data encoding="csv">
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,68,68,0,0,0,0,0,0,0,0,74,74,74,0,0,0,0,
74,74,74,75,75,74,74,68,68,68,68,68,74,74,78,74,68,74,68,68
</data>
</layer>
</map>

11
data/map/01.map Normal file
View File

@@ -0,0 +1,11 @@
tileset_img=tiles_surface.png
bg_img=bg_surface.png
room_up=0
room_down=0
room_left=0
room_right=0
[tilemap]
01.tmx
[tilemap-end]

21
data/map/01.tmx Normal file
View File

@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.9" tiledversion="1.9.1" orientation="orthogonal" renderorder="right-down" width="20" height="13" tilewidth="16" tileheight="16" infinite="0" nextlayerid="3" nextobjectid="1">
<tileset firstgid="1" source="../../../volcano_2022_resources/media_work/map_work/tiles_surface.tsx"/>
<layer id="1" name="tiles" width="20" height="13">
<data encoding="csv">
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,74,74,74,0,0,0,0,
74,74,74,75,75,74,74,68,68,68,68,68,74,74,78,74,68,74,68,68
</data>
</layer>
</map>

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
media/font/8bithud.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

194
media/font/8bithud.txt Normal file
View File

@@ -0,0 +1,194 @@
# box width
8
# box height
8
# 32 espacio ( )
2
# 33 !
2
# 34 "
5
# 35 #
6
# 36 $
6
# 37 %
6
# 38 &
6
# 39 '
2
# 40 (
3
# 41 )
3
# 42 *
4
# 43 +
3
# 44 ,
2
# 45 -
3
# 46 .
2
# 47 /
4
# 48 0
6
# 49 1
6
# 50 2
6
# 51 3
6
# 52 4
6
# 53 5
6
# 54 6
6
# 55 7
6
# 56 8
6
# 57 9
6
# 58 :
2
# 59 ;
2
# 60 <
4
# 61 =
3
# 62 >
4
# 63 ?
6
# 64 @
8
# 65 A
6
# 66 B
6
# 67 C
6
# 68 D
6
# 69 E
6
# 70 F
6
# 71 G
6
# 72 H
6
# 73 I
6
# 74 J
6
# 75 K
6
# 76 L
6
# 77 M
6
# 78 N
6
# 79 O
6
# 80 P
6
# 81 Q
6
# 82 R
6
# 83 S
6
# 84 T
6
# 85 U
6
# 86 V
5
# 87 W
6
# 88 X
6
# 89 Y
6
# 90 Z
6
# 91 [
3
# 92 \
5
# 93 ]
3
# 94 ^
4
# 95 _
6
# 96 `
2
# 97 a
5
# 98 b
5
# 99 c
5
# 100 d
5
# 101 e
5
# 102 f
5
# 103 g
5
# 104 h
5
# 105 i
4
# 106 j
5
# 107 k
5
# 108 l
5
# 109 m
6
# 110 n
5
# 111 o
5
# 112 p
5
# 113 q
5
# 114 r
5
# 115 s
5
# 116 t
4
# 117 u
5
# 118 v
5
# 119 w
6
# 120 x
4
# 121 y
4
# 122 z
5
# 123 {
3
# 124 |
2
# 125 }
3
# 126 ~
3

View File

Before

Width:  |  Height:  |  Size: 4.8 KiB

After

Width:  |  Height:  |  Size: 4.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 7.3 KiB

10
source/actors.cpp Normal file
View File

@@ -0,0 +1,10 @@
#include "actors.h"
// Constructor
Actors::Actors()
{
}
Actors::~Actors()
{
}

20
source/actors.h Normal file
View File

@@ -0,0 +1,20 @@
#pragma once
#include <SDL2/SDL.h>
#ifndef ACTORS_H
#define ACTORS_H
class Actors
{
private:
public:
// Constructor
Actors();
// Destructor
~Actors();
};
#endif

View File

@@ -1,146 +1,255 @@
//#include "const.h" #include "const.h"
#include "animatedsprite.h" #include "animatedsprite.h"
//#include <iostream>
// Constructor // Constructor
AnimatedSprite::AnimatedSprite() AnimatedSprite::AnimatedSprite(LTexture *texture, SDL_Renderer *renderer, std::string file)
{ {
MovingSprite::init(0, 0, 0, 0, 0, 0, 0, 0, nullptr, nullptr); // Copia los punteros
init(nullptr, nullptr); setTexture(texture);
setRenderer(renderer);
// Carga las animaciones
load(file);
} }
// Destructor // Destructor
AnimatedSprite::~AnimatedSprite() AnimatedSprite::~AnimatedSprite()
{ {
MovingSprite::init(0, 0, 0, 0, 0, 0, 0, 0, nullptr, nullptr); for (auto a : animation)
init(nullptr, nullptr); {
a.frames.clear();
}
animation.clear();
} }
// Iniciador // Obtiene el indice de la animación a partir del nombre
void AnimatedSprite::init(LTexture *texture, SDL_Renderer *renderer) int AnimatedSprite::getIndex(std::string name)
{ {
mRenderer = renderer; int result = -1;
mTexture = texture; for (int i = 0; i < animation.size(); i++)
for (Uint8 i = 0; i < 20; i++)
{ {
mAnimation[i].numFrames = 0; if (animation[i].name == name)
mAnimation[i].speed = 0;
mAnimation[i].loop = true;
mAnimation[i].currentFrame = 0;
mAnimation[i].counter = 0;
for (Uint8 j = 0; i < 50; i++)
{ {
mAnimation[i].frame[j].x = 0; result = i;
mAnimation[i].frame[j].y = 0;
mAnimation[i].frame[j].w = 0;
mAnimation[i].frame[j].h = 0;
} }
} }
return result;
mCurrentAnimation = 0;
} }
// Calcula el frame correspondiente a la animación // Calcula el frame correspondiente a la animación
void AnimatedSprite::animate(int index) void AnimatedSprite::animate()
{ {
// Calculamos el frame actual a partir del contador if (mEnabled)
mAnimation[index].currentFrame = mAnimation[index].counter / mAnimation[index].speed; {
// Calcula el frame actual a partir del contador
animation[currentAnimation].currentFrame = animation[currentAnimation].counter / animation[currentAnimation].speed;
// Final de la animación // Si alcanza el final de la animación, reinicia el contador de la animación
if (mAnimation[index].currentFrame >= mAnimation[index].numFrames) // en función de la variable loop
if (animation[currentAnimation].currentFrame >= animation[currentAnimation].frames.size())
{ {
// Si se reproduce en bucle if (animation[currentAnimation].loop)
if (mAnimation[index].loop) animation[currentAnimation].counter = 0;
{ else
// Reiniciamos el contador de la animación animation[currentAnimation].currentFrame = animation[currentAnimation].frames.size();
mAnimation[index].counter = 0;
} }
// En caso contrario
else else
{ {
// Mantenemos el ultimo frame // Escoge el frame correspondiente de la animación
mAnimation[index].currentFrame = mAnimation[index].numFrames; setSpriteClip(animation[currentAnimation].frames[animation[currentAnimation].currentFrame]);
}
}
// La animación no ha llegado a su fin
else
{
// Establece el frame correspondiente de la animación
setSpriteClip(mAnimation[index].frame[mAnimation[index].currentFrame]);
// Incrementa el contador de la animacion // Incrementa el contador de la animacion
mAnimation[index].counter++; animation[currentAnimation].counter++;
} }
} }
// Actualiza todas las variables del objeto: posición, velocidad y animación
void AnimatedSprite::update()
{
MovingSprite::update();
animate(mCurrentAnimation);
} }
// Establece el frame actual de la animación // Establece el frame actual de la animación
void AnimatedSprite::setCurrentFrame(Uint8 index, Uint8 num) void AnimatedSprite::setCurrentFrame(std::string name, int num)
{ {
mAnimation[index].currentFrame = num; animation[getIndex(name)].currentFrame = num;
} }
// Establece el valor del contador // Establece el valor del contador
void AnimatedSprite::setAnimationCounter(Uint8 index, Uint16 num) void AnimatedSprite::setAnimationCounter(std::string name, int num)
{ {
mAnimation[index].counter = num; animation[getIndex(name)].counter = num;
}
// Establece el rectangulo para un frame de una animación
void AnimatedSprite::setAnimationFrames(Uint8 index, Uint8 frame, int x, int y, int w, int h)
{
mAnimation[index].frame[frame].y = y;
mAnimation[index].frame[frame].w = w;
mAnimation[index].frame[frame].h = h;
mAnimation[index].frame[frame].x = x;
}
// Establece el rectangulo para un frame de una animación
void AnimatedSprite::setAnimationFrames(Uint8 index, Uint8 frame, SDL_Rect rect)
{
mAnimation[index].frame[frame] = rect;
} }
// Establece la velocidad de una animación // Establece la velocidad de una animación
void AnimatedSprite::setAnimationSpeed(Uint8 index, Uint8 speed) void AnimatedSprite::setAnimationSpeed(std::string name, int speed)
{ {
mAnimation[index].speed = speed; animation[getIndex(name)].counter = speed;
}
// Establece el numero de frames de una animación
void AnimatedSprite::setAnimationNumFrames(Uint8 index, Uint8 num)
{
if (num < MAX_FRAMES)
mAnimation[index].numFrames = num;
} }
// Establece si la animación se reproduce en bucle // Establece si la animación se reproduce en bucle
void AnimatedSprite::setAnimationLoop(Uint8 index, bool loop) void AnimatedSprite::setAnimationLoop(std::string name, bool loop)
{ {
mAnimation[index].loop = loop; animation[getIndex(name)].loop = loop;
}
// Establece el valor de la variable
void AnimatedSprite::setCompleted(std::string name, bool value)
{
animation[getIndex(name)].completed = value;
}
// Comprueba si ha terminado la animación
bool AnimatedSprite::isCompleted(std::string name)
{
return animation[getIndex(name)].completed;
} }
// Devuelve el rectangulo de una animación y frame concreto // Devuelve el rectangulo de una animación y frame concreto
SDL_Rect AnimatedSprite::getAnimationClip(Uint8 index, Uint8 frame) SDL_Rect AnimatedSprite::getAnimationClip(std::string name, Uint8 index)
{ {
return mAnimation[index].frame[frame]; return animation[getIndex(name)].frames[index];
} }
// Establece la animación actual // Carga la animación desde un fichero
void AnimatedSprite::setCurrentAnimation(Uint8 index) bool AnimatedSprite::load(std::string filePath)
{ {
if (index < MAX_ANIMATIONS) int png_width_in_tiles = 0;
mCurrentAnimation = index; int tile_width = 0;
int tile_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 [enemy] se realiza el proceso de carga de un enemigo
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 != 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")
{
if (line.substr(pos + 1, line.length()) == "yes" || line.substr(pos + 1, line.length()) == "true")
{
buffer.loop = true;
}
else
{
buffer.loop = false;
}
}
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, tile_width, tile_height};
while (getline(ss, tmp, ','))
{
int num_tile = std::stoi(tmp);
rect.x = (num_tile % png_width_in_tiles) * tile_width;
rect.y = (num_tile / png_width_in_tiles) * tile_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 el enemigo al vector de enemigos
animation.push_back(buffer);
} }
// Obtiene la animación actual // En caso contrario se parsea el fichero para buscar las variables y los valores
Uint8 AnimatedSprite::getCurrentAnimation() else
{ {
return mCurrentAnimation; // Encuentra la posición del caracter '='
int pos = line.find("=");
// Procesa las dos subcadenas
if (pos != line.npos)
{
if (line.substr(0, pos) == "png_width_in_tiles")
{
png_width_in_tiles = std::stoi(line.substr(pos + 1, line.length()));
}
else if (line.substr(0, pos) == "tile_width")
{
tile_width = std::stoi(line.substr(pos + 1, line.length()));
}
else if (line.substr(0, pos) == "tile_height")
{
tile_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", 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;
}
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;
}
}
// Actualiza las variables del objeto
void AnimatedSprite::update()
{
animate();
MovingSprite::update();
} }

View File

@@ -1,75 +1,73 @@
#pragma once #pragma once
#include "ifdefs.h" #include <SDL2/SDL.h>
#include "movingsprite.h" #include "movingsprite.h"
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#ifndef ANIMATEDSPRITE_H #ifndef ANIMATEDSPRITE_H
#define ANIMATEDSPRITE_H #define ANIMATEDSPRITE_H
#define MAX_FRAMES 50
#define MAX_ANIMATIONS 20
// Clase AnimatedSprite // Clase AnimatedSprite
class AnimatedSprite : public MovingSprite class AnimatedSprite : public MovingSprite
{ {
private:
struct t_animation
{
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
bool loop; // Indica si la animación se reproduce en bucle
bool completed; // Indica si ha finalizado la animación
int currentFrame; // Frame actual
int counter; // Contador para las animaciones
};
std::vector<t_animation> animation; // Vector con las diferentes animaciones
int currentAnimation; // Animacion activa
public: public:
// Constructor // Constructor
AnimatedSprite(); AnimatedSprite(LTexture *texture = nullptr, SDL_Renderer *renderer = nullptr, std::string file="");
// Destructor // Destructor
~AnimatedSprite(); ~AnimatedSprite();
// Iniciador // Calcula el frame correspondiente a la animación actual
void init(LTexture *texture, SDL_Renderer *renderer); void animate();
// Calcula el frame correspondiente a la animación
void animate(int index);
// Actualiza todas las variables del objeto: posición, velocidad y animación
void update();
// Establece el frame actual de la animación // Establece el frame actual de la animación
void setCurrentFrame(Uint8 index, Uint8 num); void setCurrentFrame(std::string name, int num);
// Establece el valor del contador // Establece el valor del contador
void setAnimationCounter(Uint8 index, Uint16 num); void setAnimationCounter(std::string name, int num);
// Establece el rectangulo para un frame de una animación
void setAnimationFrames(Uint8 index, Uint8 frame, int x, int y, int w, int h);
// Establece el rectangulo para un frame de una animación
void setAnimationFrames(Uint8 index, Uint8 frame, SDL_Rect rect);
// Establece la velocidad de una animación // Establece la velocidad de una animación
void setAnimationSpeed(Uint8 index, Uint8 speed); void setAnimationSpeed(std::string name, 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 // Establece si la animación se reproduce en bucle
void setAnimationLoop(Uint8 index, bool loop); void setAnimationLoop(std::string name, bool loop);
// Establece el valor de la variable
void setCompleted(std::string name, bool value);
// Comprueba si ha terminado la animación
bool isCompleted(std::string name);
// Devuelve el rectangulo de una animación y frame concreto // Devuelve el rectangulo de una animación y frame concreto
SDL_Rect getAnimationClip(Uint8 index, Uint8 frame); SDL_Rect getAnimationClip(std::string name, Uint8 index);
// Establece la animación actual // Obtiene el indice de la animación a partir del nombre
void setCurrentAnimation(Uint8 index); int getIndex(std::string name);
// Obtiene la animación actual // Carga la animación desde un fichero
Uint8 getCurrentAnimation(); bool load(std::string filePath);
private: // Establece la animacion actual
struct sAnimation void setCurrentAnimation(std::string name);
{
SDL_Rect frame[MAX_FRAMES]; // Vector con los rectangulos de cada frame de la animación
Uint8 numFrames; // Cantidad de frames de la animacion
Uint8 speed; // Velocidad de la animación
Uint8 currentFrame; // Frame actual
Uint16 counter; // Contador
bool loop; // Indica si la animación se reproduce en bucle
};
sAnimation mAnimation[MAX_ANIMATIONS]; // Vector con las animaciones // Actualiza las variables del objeto
Uint8 mCurrentAnimation; // Animación actual; void update();
}; };
#endif #endif

135
source/asset.cpp Normal file
View File

@@ -0,0 +1,135 @@
#include "asset.h"
// Constructor
Asset::Asset(std::string path)
{
mExecutablePath = path;
longest_name = 0;
}
// Destructor
Asset::~Asset()
{
}
// Añade un elemento a la lista
void Asset::add(std::string file, enum assetType type, bool required)
{
item_t temp;
temp.file = mExecutablePath + "/.." + file;
temp.type = type;
temp.required = required;
mFileList.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 (int i = 0; i < mFileList.size(); i++)
if (mFileList[i].file.find(text) != std::string::npos)
return mFileList[i].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 < maxAssetType; type++)
{
// Comprueba si hay ficheros de ese tipo
bool any = false;
for (int i = 0; i < mFileList.size(); i++)
if ((mFileList[i].required) && (mFileList[i].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 (int i = 0; i < mFileList.size(); i++)
if ((mFileList[i].required) && (mFileList[i].type == type))
success &= checkFile(mFileList[i].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 != NULL)
{
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 bitmap:
return "BITMAP";
break;
case music:
return "MUSIC";
break;
case sound:
return "SOUND";
break;
case font:
return "FONT";
break;
case lang:
return "LANG";
break;
case data:
return "DATA";
break;
case room:
return "ROOM";
break;
case enemy:
return "ENEMY";
break;
case item:
return "ITEM";
break;
default:
return "ERROR";
break;
}
}

63
source/asset.h Normal file
View File

@@ -0,0 +1,63 @@
#pragma once
#include <SDL2/SDL.h>
#include <string>
#include <vector>
#ifndef ASSET_H
#define ASSET_H
enum assetType
{
bitmap,
music,
sound,
font,
lang,
data,
room,
enemy,
item,
maxAssetType
};
// Clase Asset
class Asset
{
private:
// Estructura para definir un item
struct item_t
{
std::string file; // Ruta del fichero desde la raiz del directorio
enum assetType type; // Indica el tipo de recurso
bool required; // Indica si es un fichero que debe de existir
};
int longest_name; // Contiene la longitud del nombre de fichero mas largo
std::vector<item_t> mFileList;
std::string mExecutablePath;
// 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 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

@@ -1,387 +1,18 @@
#pragma once #pragma once
#include "ifdefs.h"
#include <string>
#ifndef CONST_H #ifndef CONST_H
#define CONST_H #define CONST_H
// Textos
#define WINDOW_CAPTION "Volcano (@2016,2021 JailDesigner v0.5)"
const Uint8 GAME_SPEED = 24; //16 = normal-rapido, 24 = normal. Cuanto mas pequeño, más rápido
const Uint8 UP = 0;
const Uint8 DOWN = 2;
const Uint8 RIGHT = 1;
const Uint8 LEFT = 3;
const Uint8 NONE = 4;
const Uint8 MAP_TILE_HEIGHT = 16;
const Uint8 MAP_TILE_WIDTH = 16;
const Uint8 ROOM_WIDTH_IN_TILES = 20;
const Uint8 ROOM_HEIGHT_IN_TILES = 14;
const Uint16 GAME_WINDOW_WIDTH = 320;
const Uint16 GAME_WINDOW_HEIGHT = 234;
const Uint8 TEST_ROOM = 118;
const Uint8 STARTING_ROOM = 161; //TEST_ROOM;
const Uint8 STARTING_PLAYER_TILE_X = 5;
const Uint8 STARTING_PLAYER_TILE_Y = 11;
const Uint8 ENEMY_HITBOX_REDUCTION = 4;
const Uint8 COOLDOWN_TIME = 50;
const Uint8 GRAVITY = 1;
const Uint8 MAX_SPEED_Y = 5;
const Uint8 BASE_SPEED = 1;
const Uint8 MAX_SPEED = 8;
const Uint8 RATIO_SPEED = 8;
const Uint8 DROP_TIMER = 100;
const Uint8 TOP_COLLISION = 0;
const Uint8 BOTTOM_COLLISION = 1;
const Uint8 LEFT_COLLISION = 2;
const Uint8 RIGHT_COLLISION = 3;
const Uint8 NO_COLLISION = 4;
const Uint8 MAX_ACTORS = 50;
const Uint8 KIND_FLYING_ENEMY = 0;
const Uint8 KIND_COIN = 1;
const Uint8 KIND_HEART = 2;
const Uint8 KIND_STATIC_ENEMY = 3;
const Uint8 KIND_MOBILE_PLATFORM = 4;
const Uint8 KIND_WALKING_ENEMY = 5;
const Uint8 KIND_DROP_GENERATOR = 6;
const Uint8 KIND_DROP_ENEMY = 7;
const Uint8 KIND_DROP_SPLAT = 8;
const Uint8 KIND_SPEED_ENEMY = 9;
const Uint8 KIND_KEY = 10;
const Uint8 KIND_LOCK = 11;
const Uint8 CODE_ENEMY_V1U = 208;
const Uint8 CODE_ENEMY_V2U = 209;
const Uint8 CODE_ENEMY_V3U = 210;
const Uint8 CODE_ENEMY_V1D = 211;
const Uint8 CODE_ENEMY_V2D = 212;
const Uint8 CODE_ENEMY_V3D = 213;
const Uint8 CODE_ENEMY_H1L = 214;
const Uint8 CODE_ENEMY_H2L = 215;
const Uint8 CODE_ENEMY_H3L = 216;
const Uint8 CODE_ENEMY_H1R = 217;
const Uint8 CODE_ENEMY_H2R = 218;
const Uint8 CODE_ENEMY_H3R = 219;
const Uint8 CODE_ENEMY_W1L = 224;
const Uint8 CODE_ENEMY_W2L = 225;
const Uint8 CODE_ENEMY_W3L = 226;
const Uint8 CODE_ENEMY_W1R = 227;
const Uint8 CODE_ENEMY_W2R = 228;
const Uint8 CODE_ENEMY_W3R = 229;
const Uint8 CODE_ENEMY_DRP = 230;
const Uint8 CODE_ENEMY_SPL = 231;
const Uint8 CODE_ENEMY_SPR = 232;
const Uint8 CODE_COIN = 240;
const Uint8 CODE_HEART = 241;
const Uint8 CODE_KEY_RED = 242;
const Uint8 CODE_LOCK_RED = 243;
const Uint8 CODE_KEY_BLUE = 244;
const Uint8 CODE_LOCK_BLUE = 245;
const Uint8 CODE_KEY_GREEN = 246;
const Uint8 CODE_LOCK_GREEN = 247;
const Uint8 CODE_KEY_YELLOW = 248;
const Uint8 CODE_LOCK_YELLOW = 249;
const Uint8 MAX_ANIMATED_TILES = 200;
const Uint8 TILE_BACKGROUND = 0;
const Uint8 TILE_PLATFORM = 1;
const Uint8 TILE_KILLING_PLATFORM = 2;
const Uint8 TILE_ACTOR = 3;
const Uint8 TILE_TRAVESABLE_PLATFORM = 4;
const Uint8 PLAYER_ANIMATION_STANDING_LEFT = 0;
const Uint8 PLAYER_ANIMATION_STANDING_RIGHT = 1;
const Uint8 PLAYER_ANIMATION_WALKING_LEFT = 2;
const Uint8 PLAYER_ANIMATION_WALKING_RIGHT = 3;
const Uint8 PLAYER_ANIMATION_JUMPING_LEFT = 4;
const Uint8 PLAYER_ANIMATION_JUMPING_RIGHT = 5;
const Uint8 PLAYER_ANIMATION_DYING_LEFT = 6;
const Uint8 PLAYER_ANIMATION_DYING_RIGHT = 7;
const Uint8 SECTION_MENU = 0;
const Uint8 SECTION_GAME = 1;
const Uint8 SECTION_QUIT = 2;
const Uint8 MENU_SECTION_MAIN = 0;
const Uint8 MENU_SECTION_CREDITS = 1;
const Uint8 MENU_SECTION_ANIMATION = 2;
const Uint8 ZONE_SURFACE = 0;
const Uint8 ZONE_VOLCANO = 1;
// Recursos
const Uint8 FILE_MAP_VOLCANO = 0;
const Uint8 FILE_CONFIG = 1;
const Uint8 TOTAL_FILE = 2;
const Uint8 TEXTURE_ACTORS = 0;
const Uint8 TEXTURE_BKG_SURFACE = 1;
const Uint8 TEXTURE_FILTER = 2;
const Uint8 TEXTURE_HUD = 3;
const Uint8 TEXTURE_MENU = 4;
const Uint8 TEXTURE_MENU_ANIMATION = 5;
const Uint8 TEXTURE_PLAYER = 6;
const Uint8 TEXTURE_TILES_SURFACE = 7;
const Uint8 TEXTURE_TILES_VOLCANO = 8;
const Uint8 TOTAL_TEXTURE = 9;
const Uint8 SOUND_COIN = 0;
const Uint8 SOUND_DEATH = 1;
const Uint8 SOUND_DROP_ENEMY = 2;
const Uint8 SOUND_DROP_SPLAT = 3;
const Uint8 SOUND_JUMP = 4;
const Uint8 SOUND_MENU_LOGO = 5;
const Uint8 SOUND_MENU_START = 6;
const Uint8 TOTAL_SOUND = 7;
const Uint8 MUSIC_MENU = 0;
const Uint8 MUSIC_SURFACE = 1;
const Uint8 MUSIC_VOLCANO = 2;
const Uint8 TOTAL_MUSIC = 3;
///////////////////////////////COFFEE CRISIS//////////////////////////////////////////////
// Tamaño de bloque // Tamaño de bloque
const Uint8 BLOCK = 8; #define BLOCK 16
const Uint8 HALF_BLOCK = BLOCK / 2; #define HALF_BLOCK 8
// Tamaño de la pantalla real // Tamaño de la pantalla real
const int SCREEN_WIDTH = 320; #define SCREEN_WIDTH 320
const int SCREEN_HEIGHT = SCREEN_WIDTH * 3 / 4; // 240 #define SCREEN_HEIGHT 240
// Tamaño de la pantalla que se muestra
const int VIEW_WIDTH = SCREEN_WIDTH * 3; // 960
const int VIEW_HEIGHT = SCREEN_HEIGHT * 3; // 720
// Cantidad de enteros a escribir en los ficheros de datos
const Uint8 TOTAL_SCORE_DATA = 3;
const Uint16 TOTAL_DEMO_DATA = 2000;
// Zona de juego
const int PLAY_AREA_TOP = (0 * BLOCK);
const int PLAY_AREA_BOTTOM = SCREEN_HEIGHT - (4 * BLOCK);
const int PLAY_AREA_LEFT = (0 * BLOCK);
const int PLAY_AREA_RIGHT = SCREEN_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);
const int PLAY_AREA_CENTER_Y = PLAY_AREA_TOP + (PLAY_AREA_HEIGHT / 2);
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;
// Color transparente para los sprites
const Uint8 COLOR_KEY_R = 0xff;
const Uint8 COLOR_KEY_G = 0x00;
const Uint8 COLOR_KEY_B = 0xff;
// Opciones de menu
const int MENU_NO_OPTION = -1;
const int MENU_OPTION_START = 0;
const int MENU_OPTION_QUIT = 1;
const int MENU_OPTION_TOTAL = 2;
// Selector de menu
const int MENU_SELECTOR_BLACK = (BLOCK * 0);
const int MENU_SELECTOR_WHITE = (BLOCK * 1);
// Tipos de fondos para el menu
const int MENU_BACKGROUND_TRANSPARENT = 0;
const int MENU_BACKGROUND_SOLID = 1;
// Estados del jugador
const Uint8 PLAYER_STATUS_WALKING_LEFT = 0;
const Uint8 PLAYER_STATUS_WALKING_RIGHT = 1;
const Uint8 PLAYER_STATUS_WALKING_STOP = 2;
const Uint8 PLAYER_STATUS_FIRING_UP = 0;
const Uint8 PLAYER_STATUS_FIRING_LEFT = 1;
const Uint8 PLAYER_STATUS_FIRING_RIGHT = 2;
const Uint8 PLAYER_STATUS_FIRING_NO = 3;
const Uint8 PLAYER_ANIMATION_LEGS_WALKING_LEFT = 0;
const Uint8 PLAYER_ANIMATION_LEGS_WALKING_RIGHT = 1;
const Uint8 PLAYER_ANIMATION_LEGS_WALKING_STOP = 2;
const Uint8 PLAYER_ANIMATION_BODY_WALKING_LEFT = 0;
const Uint8 PLAYER_ANIMATION_BODY_FIRING_LEFT = 1;
const Uint8 PLAYER_ANIMATION_BODY_WALKING_RIGHT = 2;
const Uint8 PLAYER_ANIMATION_BODY_FIRING_RIGHT = 3;
const Uint8 PLAYER_ANIMATION_BODY_WALKING_STOP = 4;
const Uint8 PLAYER_ANIMATION_BODY_FIRING_UP = 5;
const Uint8 PLAYER_ANIMATION_BODY_WALKING_LEFT_EXTRA_HIT = 6;
const Uint8 PLAYER_ANIMATION_BODY_FIRING_LEFT_EXTRA_HIT = 7;
const Uint8 PLAYER_ANIMATION_BODY_WALKING_RIGHT_EXTRA_HIT = 8;
const Uint8 PLAYER_ANIMATION_BODY_FIRING_RIGHT_EXTRA_HIT = 9;
const Uint8 PLAYER_ANIMATION_BODY_WALKING_STOP_EXTRA_HIT = 10;
const Uint8 PLAYER_ANIMATION_BODY_FIRING_UP_EXTRA_HIT = 11;
// Variables del jugador
const Uint16 PLAYER_INVULNERABLE_TIMER = 200;
// Estados del juego
const Uint8 GAME_SECTION_TITLE = 0;
const Uint8 GAME_SECTION_PLAYING = 1;
const Uint8 GAME_SECTION_QUIT = 2;
const Uint8 GAME_SECTION_GAME_OVER_SCREEN = 3;
const Uint8 GAME_SECTION_INTRO = 4;
const Uint8 GAME_SECTION_DEMO = 5;
const Uint8 GAME_SECTION_INSTRUCTIONS = 6;
const Uint8 GAME_SECTION_LOGO = 7;
const Uint8 GAME_SECTION_INIT = 8;
// Estados de cada elemento que pertenece a un evento
const Uint8 EVENT_WAITING = 1;
const Uint8 EVENT_RUNNING = 2;
const Uint8 EVENT_COMPLETED = 3;
// Cantidad de eventos de la intro
const Uint8 INTRO_TOTAL_BITMAPS = 6;
const Uint8 INTRO_TOTAL_TEXTS = 9;
const Uint8 INTRO_TOTAL_EVENTS = INTRO_TOTAL_BITMAPS + INTRO_TOTAL_TEXTS;
// Cantidad de eventos de la pantalla de titulo
const Uint8 TITLE_TOTAL_EVENTS = 2;
// Relaciones de Id con nomnbres
const Uint8 BITMAP0 = 0;
const Uint8 BITMAP1 = 1;
const Uint8 BITMAP2 = 2;
const Uint8 BITMAP3 = 3;
const Uint8 BITMAP4 = 4;
const Uint8 BITMAP5 = 5;
const Uint8 TEXT0 = 6;
const Uint8 TEXT1 = 7;
const Uint8 TEXT2 = 8;
const Uint8 TEXT3 = 9;
const Uint8 TEXT4 = 10;
const Uint8 TEXT5 = 11;
const Uint8 TEXT6 = 12;
const Uint8 TEXT7 = 13;
const Uint8 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
const Uint8 NO_KIND = 0;
// Tipos de globo
const Uint8 BALLOON_1 = 1;
const Uint8 BALLOON_2 = 2;
const Uint8 BALLOON_3 = 3;
const Uint8 BALLOON_4 = 4;
// Velocidad del globo
const float BALLON_VELX_POSITIVE = 0.7f;
const float BALLON_VELX_NEGATIVE = -0.7f;
// Indice para las animaciones de los globos
const Uint8 BALLOON_MOVING_ANIMATION = 0;
const Uint8 BALLOON_POP_ANIMATION = 1;
const Uint8 BALLOON_BORN_ANIMATION = 2;
// Cantidad posible de globos
const Uint8 MAX_BALLOONS = 75;
// Tipos de bala
const Uint8 BULLET_UP = 1;
const Uint8 BULLET_LEFT = 2;
const Uint8 BULLET_RIGHT = 3;
// Cantidad posible de globos
const Uint8 MAX_BULLETS = 50;
// Tipos de objetos
const Uint8 ITEM_POINTS_1_DISK = 1;
const Uint8 ITEM_POINTS_2_GAVINA = 2;
const Uint8 ITEM_POINTS_3_PACMAR = 3;
const Uint8 ITEM_CLOCK = 4;
const Uint8 ITEM_TNT = 5;
const Uint8 ITEM_COFFEE = 6;
// Cantidad de objetos simultaneos
const Uint8 MAX_ITEMS = 5;
// Valores para las variables asociadas a los objetos
const Uint8 REMAINING_EXPLOSIONS = 3;
const Uint8 REMAINING_EXPLOSIONS_TIMER = 50;
const Uint16 TIME_STOPPED_TIMER = 300;
// Estados de entrada
const Uint8 NO_INPUT = 0;
const Uint8 INPUT_UP = 1;
const Uint8 INPUT_DOWN = 2;
const Uint8 INPUT_LEFT = 3;
const Uint8 INPUT_RIGHT = 4;
const Uint8 INPUT_ACCEPT = 5;
const Uint8 INPUT_CANCEL = 6;
const Uint8 INPUT_FIRE_UP = 7;
const Uint8 INPUT_FIRE_LEFT = 8;
const Uint8 INPUT_FIRE_RIGHT = 9;
const Uint8 INPUT_PAUSE = 10;
// Zona muerta del mando analógico
const int JOYSTICK_DEAD_ZONE = 8000;
// Tipos de mensajes para el retorno de las funciones
const Uint8 MSG_OK = 0;
const Uint8 MSG_BULLET_OUT = 1;
// Tipos de texto
const Uint8 TEXT_FIXED = 0;
const Uint8 TEXT_VARIABLE = 1;
// Cantidad de elementos del vector de SmartSprites
const Uint8 MAX_SMART_SPRITES = 10;
// Contadores
const Uint16 TITLE_TIMER = 800;
const Uint8 STAGE_COUNTER = 200;
const Uint16 INSTRUCTIONS_COUNTER = 600;
const Uint16 DEATH_COUNTER = 350;
// Tamaño de la pantalla virtual
#define GAMECANVAS_WIDTH 320
#define GAMECANVAS_HEIGHT 240
#endif #endif

View File

@@ -1,685 +0,0 @@
#include "const.h"
#include "struct.h"
#include "director.h"
#include <iostream>
const Uint8 *keystates;
// Constructor
Director::Director(std::string _path)
{
// Crea todos los objetos del juego
initObjects();
// Inicializa todas las variables
init(_path);
}
Director::~Director()
{
// Borra todos los objetos del juego
deleteObjects();
// Destruye la ventana
SDL_DestroyRenderer(renderer);
SDL_DestroyTexture(backbuffer);
SDL_DestroyWindow(window);
backbuffer = nullptr;
renderer = nullptr;
window = nullptr;
// Sal del subsistema SDL
SDL_Quit();
}
// Inicializa todas las variables
void Director::init(std::string _path)
{
setPath(_path);
section = NONE;
gameControllerFound = false;
for (int i = 0; i < 360; i++)
sen[i] = sin(i * 3.14 / 180);
for (int i = 0; i < TOTAL_SCORE_DATA; i++)
scoreData[i] = 0;
initOptions();
quit = false;
}
// Inicializa las variables de las opciones
void Director::initOptions()
{
options.fullScreenMode = 0;
options.fullScreenModePrevious = 0;
options.windowSize = 0;
options.windowSizePrevious = 0;
}
// Inicializa las variables del juego
void Director::initGame()
{
// Variables
game.score = 0;
game.section = 0;
game.paused = false;
game.ticks = 0;
game.ticksSpeed = GAME_SPEED;
game.counter = 0;
// Player
resource.texture[TEXTURE_PLAYER].texture = new LTexture();
loadTextureFromFile(resource.texture[TEXTURE_PLAYER].texture, resource.texture[TEXTURE_PLAYER].file, renderer);
player = new Player(renderer, resource.texture[TEXTURE_PLAYER].texture);
// Map
resource.texture[TEXTURE_ACTORS].texture = new LTexture();
loadTextureFromFile(resource.texture[TEXTURE_ACTORS].texture, resource.texture[TEXTURE_ACTORS].file, renderer);
resource.texture[TEXTURE_BKG_SURFACE].texture = new LTexture();
loadTextureFromFile(resource.texture[TEXTURE_BKG_SURFACE].texture, resource.texture[TEXTURE_BKG_SURFACE].file, renderer);
resource.texture[TEXTURE_TILES_VOLCANO].texture = new LTexture();
loadTextureFromFile(resource.texture[TEXTURE_TILES_VOLCANO].texture, resource.texture[TEXTURE_TILES_VOLCANO].file, renderer);
map = new Map(renderer, resource.texture[TEXTURE_TILES_VOLCANO].texture, resource.texture[TEXTURE_ACTORS].texture,
resource.texture[TEXTURE_BKG_SURFACE].texture, resource.file[FILE_MAP_VOLCANO].file);
}
// Limpia las variables del juego
void Director::quitGame()
{
// Player
delete player;
player = nullptr;
delete resource.texture[TEXTURE_PLAYER].texture;
resource.texture[TEXTURE_PLAYER].texture = nullptr;
// Map
delete map;
map = nullptr;
delete resource.texture[TEXTURE_TILES_VOLCANO].texture;
resource.texture[TEXTURE_TILES_VOLCANO].texture = nullptr;
delete resource.texture[TEXTURE_ACTORS].texture;
resource.texture[TEXTURE_ACTORS].texture = nullptr;
delete resource.texture[TEXTURE_BKG_SURFACE].texture;
resource.texture[TEXTURE_BKG_SURFACE].texture = nullptr;
}
// Arranca SDL y crea la ventana
bool Director::initSDL()
{
// Indicador de inicialización
bool success = true;
// Inicializa SDL
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_HAPTIC) < 0)
{
printf("SDL could not initialize!\nSDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
// Establece el filtro de la textura a nearest
if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0"))
{
printf("Warning: Nearest texture filtering not enabled!\n");
}
// Inicializa jail_audio
JA_Init(48000, AUDIO_S16, 2);
// Comprueba si hay algun mando conectado
if (SDL_NumJoysticks() < 1)
{
printf("Warning: No joysticks connected!\n");
gameControllerFound = false;
}
else
{
// Carga el mando
gameController = SDL_JoystickOpen(0);
gameControllerFound = true;
if (gameController == NULL)
{
printf("Warning: Unable to open game controller!\nSDL Error: %s\n", SDL_GetError());
gameControllerFound = false;
}
else
{
printf("%i joysticks were found.\n", SDL_NumJoysticks());
std::cout << SDL_JoystickNumButtons(gameController) << " buttons\n";
//Get controller haptic device
controllerHaptic = SDL_HapticOpenFromJoystick(gameController);
if (controllerHaptic == NULL)
{
printf("Warning: Controller does not support haptics!\nSDL Error: %s\n", SDL_GetError());
}
else
{
printf("Haptics detected\n");
//Get initialize rumble
if (SDL_HapticRumbleInit(controllerHaptic) < 0)
{
printf("Warning: Unable to initialize rumble!\nSDL Error: %s\n", SDL_GetError());
}
}
}
}
// Crea la ventana
window = SDL_CreateWindow(WINDOW_CAPTION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, VIEW_WIDTH, VIEW_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI);
if (window == NULL)
{
printf("Window could not be created!\nSDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
// Crea un renderizador para la ventana con vsync
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == NULL)
{
printf("Renderer could not be created!\nSDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
// Inicializa el color de renderizado
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
// Establece el tamaño del buffer de renderizado
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
// Establece el modo de mezcla
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
}
// Crea un backbuffer para el renderizador
backbuffer = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, SCREEN_WIDTH, SCREEN_HEIGHT);
if (backbuffer == NULL)
{
printf("Backbuffer could not be created!\nSDL Error: %s\n", SDL_GetError());
success = false;
}
}
}
printf("\n");
return success;
}
// Crea los objetos del programa
void Director::initObjects()
{
eventHandler = new SDL_Event();
text.white = new Text();
}
// Borra los objetos del programa
void Director::deleteObjects()
{
delete eventHandler;
delete text.white;
eventHandler = nullptr;
text.white = nullptr;
}
// Crea el indice de ficheros de recursos
void Director::setResourceList()
{
// Ficheros binarios
resource.file[FILE_MAP_VOLCANO].file = path + "/" + "../data/volcano.map";
resource.file[FILE_CONFIG].file = path + "/" + "../data/config.bin";
// Texturas
resource.texture[TEXTURE_ACTORS].file = path + "/" + "../media/gfx/actors.png";
resource.texture[TEXTURE_BKG_SURFACE].file = path + "/" + "../media/gfx/bkg_surface.png";
resource.texture[TEXTURE_FILTER].file = path + "/" + "../media/gfx/filter.png";
resource.texture[TEXTURE_HUD].file = path + "/" + "../media/gfx/hud.png";
resource.texture[TEXTURE_MENU_ANIMATION].file = path + "/" + "../media/gfx/menu_animation.png";
resource.texture[TEXTURE_MENU].file = path + "/" + "../media/gfx/menu.png";
resource.texture[TEXTURE_PLAYER].file = path + "/" + "../media/gfx/player.png";
resource.texture[TEXTURE_TILES_SURFACE].file = path + "/" + "../media/gfx/tiles_surface.png";
resource.texture[TEXTURE_TILES_VOLCANO].file = path + "/" + "../media/gfx/tiles_volcano.png";
for (Uint8 i = 0; i < TOTAL_TEXTURE; i++)
resource.texture[i].texture = nullptr;
// Sonidos
resource.sound[SOUND_COIN].file = path + "/" + "../media/sound/sound_player_coin.wav";
resource.sound[SOUND_DEATH].file = path + "/" + "../media/sound/sound_player_death.wav";
resource.sound[SOUND_DROP_ENEMY].file = path + "/" + "../media/sound/sound_drop_enemy.wav";
resource.sound[SOUND_DROP_SPLAT].file = path + "/" + "../media/sound/sound_drop_splat.wav";
resource.sound[SOUND_JUMP].file = path + "/" + "../media/sound/sound_player_jump.wav";
resource.sound[SOUND_MENU_LOGO].file = path + "/" + "../media/sound/sound_menu_logo.wav";
resource.sound[SOUND_MENU_START].file = path + "/" + "../media/sound/sound_menu_start.wav";
for (Uint8 i = 0; i < TOTAL_SOUND; i++)
resource.sound[i].sound = nullptr;
// Musicas
resource.music[MUSIC_MENU].file = path + "/" + "../media/music/music_menu.ogg";
resource.music[MUSIC_SURFACE].file = path + "/" + "../media/music/music_surface.ogg";
resource.music[MUSIC_VOLCANO].file = path + "/" + "../media/music/music_volcano.ogg";
for (Uint8 i = 0; i < TOTAL_TEXTURE; i++)
resource.music[i].music = nullptr;
}
// Comprueba que todos los ficheros de recursos existen
bool Director::checkResourceList()
{
bool success = true;
std::string p;
std::string filename;
SDL_RWops *file;
// Comprueba los ficheros de musica
printf("\n>> MUSIC FILES\n");
for (Uint8 i = 0; i < TOTAL_MUSIC; i++)
{
p = resource.music[i].file.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
{
success = false;
printf("Checking file %-20s [ERROR]\n", filename.c_str());
}
SDL_RWclose(file);
}
// Comprueba los ficheros de sonidos
printf("\n>> SOUND FILES\n");
for (Uint8 i = 0; i < TOTAL_SOUND; i++)
{
p = resource.sound[i].file.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
{
success = false;
printf("Checking file %-20s [ERROR]\n", filename.c_str());
}
SDL_RWclose(file);
}
// Comprueba los ficheros con texturas
printf("\n>> TEXTURE FILES\n");
for (Uint8 i = 0; i < TOTAL_TEXTURE; i++)
{
p = resource.texture[i].file.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
{
success = false;
printf("Checking file %-20s [ERROR]\n", filename.c_str());
}
SDL_RWclose(file);
}
// Resultado
if (success)
{
printf("\n** All files OK.\n\n");
}
else
{
printf("\n** File is missing. Exiting.\n\n");
}
return success;
}
// Inicializa la variable con los ficheros de recursos
bool Director::initResourceList()
{
setResourceList();
return checkResourceList();
}
// Carga un archivo de imagen en una textura
bool Director::loadTextureFromFile(LTexture *texture, std::string path, SDL_Renderer *renderer)
{
bool success = true;
if (!texture->loadFromFile(path, renderer))
{
printf("Failed to load %s texture!\n", path.c_str());
success = false;
}
return success;
}
// Carga los recursos necesarios
bool Director::loadMedia(Uint8 section)
{
// Indicador de éxito en la carga
bool success = true;
std::string p;
switch (section)
{
case GAME_SECTION_INIT:
{
p = resource.file[FILE_CONFIG].file.c_str();
std::string filename = p.substr(p.find_last_of("\\/") + 1);
filename = p.substr(p.find_last_of("\\/") + 1);
// Abre el fichero con la configuracion de las opciones para leer en binario
SDL_RWops *file = SDL_RWFromFile(p.c_str(), "r+b");
// El fichero no existe
if (file == NULL)
{
printf("Warning: Unable to open %s file\n", filename.c_str());
// Crea el fichero para escribir
file = SDL_RWFromFile(p.c_str(), "w+b");
if (file != NULL)
{
printf("New file (%s) created!\n", filename.c_str());
// Inicializa los datos
options.fullScreenMode = 0;
SDL_RWwrite(file, &options.fullScreenMode, sizeof(options.fullScreenMode), 1);
options.windowSize = 3;
SDL_RWwrite(file, &options.windowSize, sizeof(options.windowSize), 1);
// Cierra el fichero
SDL_RWclose(file);
}
else
{
printf("Error: Unable to create file %s\n", filename.c_str());
success = false;
}
}
// El fichero existe
else
{
// Carga los datos
printf("Reading file %s\n", filename.c_str());
SDL_RWread(file, &options.fullScreenMode, sizeof(options.fullScreenMode), 1);
SDL_SetWindowFullscreen(window, options.fullScreenMode);
SDL_RWread(file, &options.windowSize, sizeof(options.windowSize), 1);
SDL_SetWindowSize(window, SCREEN_WIDTH * options.windowSize, SCREEN_HEIGHT * options.windowSize);
// Cierra el fichero
SDL_RWclose(file);
}
printf("\n");
// Texturas
// Sonidos
// Musicas
}
break;
case GAME_SECTION_TITLE:
{
}
break;
case GAME_SECTION_PLAYING:
{
}
break;
case GAME_SECTION_GAME_OVER_SCREEN:
{
}
break;
case GAME_SECTION_INTRO:
{
}
break;
case GAME_SECTION_DEMO:
{
}
break;
case GAME_SECTION_INSTRUCTIONS:
{
}
break;
case GAME_SECTION_LOGO:
{
}
break;
default:
{
}
break;
}
return success;
}
// Descrga los recursos necesarios
bool Director::unLoadMedia(Uint8 section)
{
// Indicador de éxito en la carga
bool success = true;
std::string p;
switch (section)
{
case GAME_SECTION_INIT:
{
p = resource.file[FILE_CONFIG].file;
std::string filename = p.substr(p.find_last_of("\\/") + 1);
// Abre el fichero de puntuación para escribir
SDL_RWops *file = SDL_RWFromFile(p.c_str(), "w+b");
if (file != NULL)
{
// Guardamos los datos
SDL_RWwrite(file, &options.fullScreenMode, sizeof(Uint32), 1);
SDL_RWwrite(file, &options.windowSize, sizeof(Uint8), 1);
printf("Writing file %s\n", filename.c_str());
// Cerramos el fichero
SDL_RWclose(file);
}
else
{
printf("Error: Unable to save %s file! %s\n", filename.c_str(), SDL_GetError());
}
}
break;
case GAME_SECTION_TITLE:
{
}
break;
case GAME_SECTION_PLAYING:
{
}
break;
case GAME_SECTION_GAME_OVER_SCREEN:
{
}
break;
case GAME_SECTION_INTRO:
{
}
break;
case GAME_SECTION_DEMO:
{
}
break;
case GAME_SECTION_INSTRUCTIONS:
{
}
break;
case GAME_SECTION_LOGO:
{
}
break;
default:
{
}
break;
}
return success;
}
// Establece el valor de la variable
void Director::setPath(std::string _path)
{
path = _path.substr(0, _path.find_last_of("\\/"));
}
// Obtiene el valor de la variable
Uint8 Director::getGameSection()
{
return game.section;
}
// Establece el valor de la variable
void Director::changeGameSection(_section sectionPrevious, _section sectionNext)
{
switch (sectionPrevious)
{
case GAME:
quitGame();
break;
default:
break;
}
switch (sectionNext)
{
case GAME:
initGame();
break;
default:
break;
}
game.section = sectionNext;
}
// Cambia el valor de la variable de modo de pantalla completa
void Director::changeFullScreenMode()
{
switch (options.fullScreenMode)
{
case 0:
options.fullScreenMode = SDL_WINDOW_FULLSCREEN;
break;
case SDL_WINDOW_FULLSCREEN:
options.fullScreenMode = SDL_WINDOW_FULLSCREEN_DESKTOP;
break;
case SDL_WINDOW_FULLSCREEN_DESKTOP:
options.fullScreenMode = 0;
break;
default:
options.fullScreenMode = 0;
break;
}
}
// Comprueba si hay que salir
bool Director::isRunning()
{
return !quit;
}
// Bucle para el logo del juego
void Director::runLogo()
{
}
// Bucle para la intro del juego
void Director::runIntro()
{
}
// Bucle para el titulo del juego
void Director::runTitle()
{
}
// Bucle para el juego
void Director::runGame()
{
// Actualiza la lógica del juego
updateGame();
// Pinta la sección de juego
renderGame();
}
// Actualiza la lógica del juego
void Director::updateGame()
{
// Comprueba que la diferencia de ticks sea mayor a la velocidad del juego
if (SDL_GetTicks() - game.ticks > game.ticksSpeed)
{
// Actualiza el contador de ticks
game.ticks = SDL_GetTicks();
// 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)
{
quit = true;
break;
}
}
game.counter++;
map->update();
player->update();
}
}
// Pinta la sección de juego
void Director::renderGame()
{
// Limpia la pantalla
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderClear(renderer);
// Dibuja los objetos
map->render();
player->render();
// Muestra el backbuffer en pantalla
SDL_RenderPresent(renderer);
}

View File

@@ -1,191 +0,0 @@
#pragma once
#include "ifdefs.h"
#include "sprite.h"
#include "movingsprite.h"
#include "smartsprite.h"
#include "player.h"
#include "map.h"
#include "text.h"
#include "text2.h"
#include "menu.h"
#include "const.h"
#include "jail_audio.h"
#include "utils.h"
#include <math.h>
#ifndef GAME_H
#define GAME_H
// director
class Director
{
private:
struct _text // Objetos de texto
{
Text *white;
};
struct _menu // Objetos menu
{
Menu *title; // Menu de la pantalla de título
};
struct _options // Variables relacionadas con las opciones del juego
{
Uint32 fullScreenMode; // Guarda el valor elegido para el modo de pantalla completa
Uint32 fullScreenModePrevious; // Guarda el valor previo del modo de pantalla completa
Uint8 windowSize; // Guarda el valor elegido para el tamaño de la ventana
Uint8 windowSizePrevious; // Guarda el valor previo del tamaño de la ventana
};
struct _game // Variables para el control del juego
{
Uint32 score; // Puntuación actual
Uint8 section; // Indicador para el bucle principal
bool paused; // Idica si el juego está en pausa
Uint32 ticks; // Contador de ticks para ajustar la velocidad del juego
Uint8 ticksSpeed; // Velocidad a la que se repite el bucle de juego
Uint32 counter; // Contador para el juego
};
// Recursos
struct _resourceFile
{
std::string file;
};
struct _resourceSound
{
std::string file;
JA_Sound sound;
};
struct _resourceMusic
{
std::string file;
JA_Music music;
};
struct _resourceTexture
{
std::string file;
LTexture *texture;
};
struct _resource
{
_resourceFile file[TOTAL_FILE];
_resourceSound sound[TOTAL_SOUND];
_resourceMusic music[TOTAL_MUSIC];
_resourceTexture texture[TOTAL_TEXTURE];
};
SDL_Window *window; // La ventana de la aplicación
SDL_Renderer *renderer; // El renderizador donde se dibuja todo
SDL_Event *eventHandler; // Manejador de eventos
SDL_Texture *backbuffer; // Texturas
SDL_Joystick *gameController; // Manejador para el mando 1
SDL_Haptic *controllerHaptic; // Manejador para la vibración del mando
_game game; // Contiene las variables de la sección de juego
//_menu menu; // Variable con los objetos menu
_resource resource; // Contiene todos los objetos y variables asociados a recursos
_options options; // Contiene todas las opciones del programa
_text text; // Variable con los objetos texto
bool gameControllerFound; // Indica si se ha encontrado algun mando conectado
double sen[360]; // Vector con los valores del seno para 360 grados
Player *player; // El jugador
Map *map; // El mapa del juego
Uint32 scoreData[TOTAL_SCORE_DATA]; // Datos del fichero de puntuación
std::string path; // Path donde está el ejecutable del juego
enum _section // Lista con todas las secciones en las que se divide el programa
{
GAME,
TITLE,
QUIT,
NONE
};
_section section; // Seccion actual del programa
bool quit; // Indica si hay que terminar el programa
public:
// Constructor
Director(std::string _path);
// Destructor
~Director();
// Inicializa todas las variables
void init(std::string _path);
// Inicializa las variables de las opciones
void initOptions();
// Inicializa las variables del juego
void initGame();
// Limpia las variables del juego
void quitGame();
// Arranca SDL y crea la ventana
bool initSDL();
// Crea los objetos del programa
void initObjects();
// Borra los objetos del programa
void deleteObjects();
// Establece el valor de la variable
void setPath(std::string _path);
// Crea el indice de ficheros de recursos
void setResourceList();
// Comprueba que todos los ficheros de recursos existen
bool checkResourceList();
// Inicializa la variable con los ficheros de recursos
bool initResourceList();
// Carga un archivo de imagen en una textura
bool loadTextureFromFile(LTexture *texture, std::string path, SDL_Renderer *renderer);
// Carga los recursos necesarios
bool loadMedia(Uint8 section);
// Descrga los recursos necesarios
bool unLoadMedia(Uint8 section);
// Obtiene el valor de la variable
Uint8 getGameSection();
// Establece el valor de la variable
void changeGameSection(_section sectionPrevious, _section sectionNext);
// Cambia el valor de la variable de modo de pantalla completa
void changeFullScreenMode();
// Comprueba si hay que salir
bool isRunning();
// Bucle para el logo del juego
void runLogo();
// Bucle para la intro del juego
void runIntro();
// Bucle para el titulo del juego
void runTitle();
// Bucle para el juego
void runGame();
// Pinta la sección de juego
void renderGame();
// Actualiza la lógica del juego
void updateGame();
};
#endif

90
source/game.cpp Normal file
View File

@@ -0,0 +1,90 @@
#include "game.h"
// Constructor
Game::Game(SDL_Renderer *renderer, Asset *asset, Screen *screen, Input *input)
{
this->renderer = renderer;
this->asset = asset;
this->screen = screen;
this->input = input;
eventHandler = new SDL_Event();
map = new Map(asset->get("01.map"), renderer, asset);
player = new Player(renderer, asset, input);
}
// Destructor
Game::~Game()
{
delete eventHandler;
delete map;
delete player;
}
// Bucle para el juego
section_t Game::run()
{
init();
while (section.name == SECTION_PROG_GAME)
{
// Sección juego jugando
if (section.subsection == SUBSECTION_GAME_PLAY)
{
update();
render();
}
}
return section;
}
// Inicializa las variables necesarias para la sección 'Game'
void Game::init()
{
ticks = 0;
ticksSpeed = 15;
section.name = SECTION_PROG_GAME;
section.subsection = SUBSECTION_GAME_PLAY;
}
// Actualiza el juego, las variables, comprueba la entrada, etc.
void Game::update()
{
// Comprueba que la diferencia de ticks sea mayor a la velocidad del juego
if (SDL_GetTicks() - ticks > ticksSpeed)
{
// Actualiza el contador de ticks
ticks = SDL_GetTicks();
// 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 = SECTION_PROG_QUIT;
break;
}
}
player->update();
}
}
// Pinta los objetos en pantalla
void Game::render()
{
// Prepara para dibujar el frame
screen->start();
screen->clean();
// Dibuja los objetos
map->render();
player->render();
// Actualiza la pantalla
screen->blit();
}

47
source/game.h Normal file
View File

@@ -0,0 +1,47 @@
#pragma once
#include <SDL2/SDL.h>
#include "utils.h"
#include "asset.h"
#include "screen.h"
#include "input.h"
#include "map.h"
#include "player.h"
#ifndef GAME_H
#define GAME_H
class Game
{
private:
SDL_Renderer *renderer; // El renderizador de la ventana
Asset *asset; // Objeto encargado de gestionar los ficheros de recursos
Screen *screen; // Objeto encargado de dibujar en pantalla
Input *input; // Objeto Input para gestionar las entradas
SDL_Event *eventHandler; // Manejador de eventos
section_t section; // Seccion actual dentro del programa
int ticks; // Contador de ticks para ajustar la velocidad del programa
int ticksSpeed; // Velocidad a la que se repiten los bucles del programa
Map *map; // Objeto encargado de gestionar el mapeado del juego
Player *player; // Objeto para gestionar el jugador
// Actualiza el juego, las variables, comprueba la entrada, etc.
void update();
// Pinta los objetos en pantalla
void render();
// Inicializa las variables necesarias para la sección 'Game'
void init();
public:
// Constructor
Game(SDL_Renderer *renderer, Asset *asset, Screen *screen, Input *input);
// Destructor
~Game();
// Bucle para el juego
section_t run();
};
#endif

View File

@@ -1 +0,0 @@
#include <SDL2/SDL.h>

206
source/input.cpp Normal file
View File

@@ -0,0 +1,206 @@
#include "input.h"
#include <iostream>
// Constructor
Input::Input(std::string file)
{
// Fichero gamecontrollerdb.txt
mDBpath = file;
// Inicializa las variables
for (int i = 0; i < 17; i++)
{
mKeyBindings[i].scancode = 0;
mKeyBindings[i].active = false;
mGameControllerBindings[i].button = SDL_CONTROLLER_BUTTON_INVALID;
mGameControllerBindings[i].active = false;
}
discoverGameController();
}
// Destructor
Input::~Input()
{
for (int i = 0; i < mNumGamepads; i++)
mConnectedControllers[i] = nullptr;
}
// Asigna uno de los posibles inputs a una tecla del teclado
void Input::bindKey(Uint8 input, SDL_Scancode code)
{
mKeyBindings[input].scancode = code;
}
// Asigna uno de los posibles inputs a un botón del mando
void Input::bindGameControllerButton(Uint8 input, SDL_GameControllerButton button)
{
mGameControllerBindings[input].button = button;
}
// Comprueba si un input esta activo
bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
{
bool successKeyboard = false;
bool successGameController = false;
if (device == INPUT_USE_ANY)
index = 0;
if ((device == INPUT_USE_KEYBOARD) || (device == INPUT_USE_ANY))
{
const Uint8 *mKeystates = SDL_GetKeyboardState(NULL);
if (repeat)
{
if (mKeystates[mKeyBindings[input].scancode] != 0)
successKeyboard = true;
else
successKeyboard = false;
}
else
{
if (!mKeyBindings[input].active)
{
if (mKeystates[mKeyBindings[input].scancode] != 0)
{
mKeyBindings[input].active = true;
successKeyboard = true;
}
else
{
successKeyboard = false;
}
}
else
{
if (mKeystates[mKeyBindings[input].scancode] == 0)
{
mKeyBindings[input].active = false;
successKeyboard = false;
}
else
{
successKeyboard = false;
}
}
}
}
if (gameControllerFound())
if ((device == INPUT_USE_GAMECONTROLLER) || (device == INPUT_USE_ANY))
{
if (repeat)
{
if (SDL_GameControllerGetButton(mConnectedControllers[index], mGameControllerBindings[input].button) != 0)
successGameController = true;
else
successGameController = false;
}
else
{
if (!mGameControllerBindings[input].active)
{
if (SDL_GameControllerGetButton(mConnectedControllers[index], mGameControllerBindings[input].button) != 0)
{
mGameControllerBindings[input].active = true;
successGameController = true;
}
else
{
successGameController = false;
}
}
else
{
if (SDL_GameControllerGetButton(mConnectedControllers[index], mGameControllerBindings[input].button) == 0)
{
mGameControllerBindings[input].active = false;
successGameController = false;
}
else
{
successGameController = false;
}
}
}
}
return (successKeyboard || successGameController);
}
// Comprueba si hay un mando conectado
bool Input::discoverGameController()
{
bool found = false;
if (SDL_WasInit(SDL_INIT_GAMECONTROLLER) != 1)
SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER);
if (SDL_GameControllerAddMappingsFromFile(mDBpath.c_str()) < 0)
printf("Error, could not load %s file: %s\n", mDBpath.c_str(), SDL_GetError());
int nJoysticks = SDL_NumJoysticks();
mNumGamepads = 0;
// Cuenta el numero de mandos
for (int i = 0; i < nJoysticks; i++)
if (SDL_IsGameController(i))
mNumGamepads++;
printf("\nChecking for game controllers...\n");
printf("%i joysticks found, %i are gamepads\n", nJoysticks, mNumGamepads);
if (mNumGamepads > 0)
{
found = true;
for (int i = 0; i < mNumGamepads; i++)
{
// Abre el mando y lo añade a la lista
SDL_GameController *pad = SDL_GameControllerOpen(i);
if (SDL_GameControllerGetAttached(pad) == 1)
{
mConnectedControllers.push_back(pad);
std::string separator(" #");
std::string name = SDL_GameControllerNameForIndex(i);
name.resize(25);
name = name + separator + std::to_string(i);
std::cout << name << std::endl;
mControllerNames.push_back(name);
}
else
std::cout << "SDL_GetError() = " << SDL_GetError() << std::endl;
}
//mGameController = mConnectedControllers[0];
SDL_GameControllerEventState(SDL_ENABLE);
}
return found;
}
// Comprueba si hay algun mando conectado
bool Input::gameControllerFound()
{
if (mNumGamepads > 0)
return true;
else
return false;
}
// Obten el nombre de un mando de juego
std::string Input::getControllerName(int index)
{
if (mNumGamepads > 0)
return mControllerNames[index];
else
return "";
}
// Obten el numero de mandos conectados
int Input::getNumControllers()
{
return mNumGamepads;
}

86
source/input.h Normal file
View File

@@ -0,0 +1,86 @@
#pragma once
#include <SDL2/SDL.h>
#include <string>
#include <vector>
#ifndef INPUT_H
#define INPUT_H
#define INPUT_NULL 0
#define INPUT_UP 1
#define INPUT_DOWN 2
#define INPUT_LEFT 3
#define INPUT_RIGHT 4
#define INPUT_ACCEPT 5
#define INPUT_CANCEL 6
#define INPUT_BUTTON_1 7
#define INPUT_BUTTON_2 8
#define INPUT_BUTTON_3 9
#define INPUT_BUTTON_4 10
#define INPUT_BUTTON_5 11
#define INPUT_BUTTON_6 12
#define INPUT_BUTTON_7 13
#define INPUT_BUTTON_8 14
#define INPUT_BUTTON_PAUSE 15
#define INPUT_BUTTON_ESCAPE 16
#define REPEAT_TRUE true
#define REPEAT_FALSE false
#define INPUT_USE_KEYBOARD 0
#define INPUT_USE_GAMECONTROLLER 1
#define INPUT_USE_ANY 2
// Clase Input
class Input
{
private:
struct keyBindings_t
{
Uint8 scancode; // Scancode asociado
bool active; // Indica si está activo
};
keyBindings_t mKeyBindings[17]; // Vector con las teclas asociadas a los inputs predefinidos
struct GameControllerBindings_t
{
SDL_GameControllerButton button; // GameControllerButton asociado
bool active; // Indica si está activo
};
GameControllerBindings_t mGameControllerBindings[17]; // Vector con las teclas asociadas a los inputs predefinidos
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();
public:
// Constructor
Input(std::string file);
// Destructor
~Input();
// Asigna uno de los posibles inputs a una tecla del teclado
void bindKey(Uint8 input, SDL_Scancode code);
// Asigna uno de los posibles inputs a un botón del mando
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);
// Comprueba si hay algun mando conectado
bool gameControllerFound();
// Obten el numero de mandos conectados
int getNumControllers();
// Obten el nombre de un mando de juego
std::string getControllerName(int index);
};
#endif

View File

@@ -1,3 +1,4 @@
#ifndef __MIPSEL__
#include "jail_audio.h" #include "jail_audio.h"
#include "stb_vorbis.c" #include "stb_vorbis.c"
@@ -77,7 +78,7 @@ void JA_Init(const int freq, const SDL_AudioFormat format, const int channels) {
JA_Music JA_LoadMusic(const char* filename) { JA_Music JA_LoadMusic(const char* filename) {
int chan, samplerate; int chan, samplerate;
JA_Music music = (JA_Music)SDL_malloc(sizeof(JA_Music_t)); JA_Music music = new JA_Music_t();
music->samples = stb_vorbis_decode_filename(filename, &chan, &samplerate, &music->output); music->samples = stb_vorbis_decode_filename(filename, &chan, &samplerate, &music->output);
SDL_AudioCVT cvt; SDL_AudioCVT cvt;
@@ -129,8 +130,8 @@ JA_Music_state JA_GetMusicState() {
void JA_DeleteMusic(JA_Music music) { void JA_DeleteMusic(JA_Music music) {
if (current_music == music) current_music = NULL; if (current_music == music) current_music = NULL;
free(music->output); SDL_free(music->output);
free(music); delete music;
} }
JA_Sound JA_LoadSound(const char* filename) { JA_Sound JA_LoadSound(const char* filename) {
@@ -144,7 +145,7 @@ JA_Sound JA_LoadSound(const char* filename) {
cvt.buf = (Uint8 *) SDL_malloc(cvt.len * cvt.len_mult); cvt.buf = (Uint8 *) SDL_malloc(cvt.len * cvt.len_mult);
SDL_memcpy(cvt.buf, sound->buffer, sound->length); SDL_memcpy(cvt.buf, sound->buffer, sound->length);
SDL_ConvertAudio(&cvt); SDL_ConvertAudio(&cvt);
free(sound->buffer); SDL_FreeWAV(sound->buffer);
sound->buffer = cvt.buf; sound->buffer = cvt.buf;
sound->length = cvt.len_cvt; sound->length = cvt.len_cvt;
@@ -167,7 +168,7 @@ void JA_DeleteSound(JA_Sound sound) {
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) { for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) {
if (channels[i].sound == sound) JA_StopChannel(i); if (channels[i].sound == sound) JA_StopChannel(i);
} }
SDL_FreeWAV(sound->buffer); SDL_free(sound->buffer);
delete sound; delete sound;
} }
@@ -209,4 +210,4 @@ JA_Channel_state JA_GetChannelState(const int channel) {
if (channel < 0 || channel >= JA_MAX_SIMULTANEOUS_CHANNELS) return JA_CHANNEL_INVALID; if (channel < 0 || channel >= JA_MAX_SIMULTANEOUS_CHANNELS) return JA_CHANNEL_INVALID;
return channels[channel].state; return channels[channel].state;
} }
#endif

View File

@@ -1,5 +1,5 @@
#pragma once #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_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 }; enum JA_Music_state { JA_MUSIC_INVALID, JA_MUSIC_PLAYING, JA_MUSIC_PAUSED, JA_MUSIC_STOPPED };

View File

@@ -1,5 +1,4 @@
#include <stdio.h> #include "const.h"
#include <string>
#include "ltexture.h" #include "ltexture.h"
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h" #include "stb_image.h"
@@ -15,7 +14,7 @@ LTexture::LTexture()
LTexture::~LTexture() LTexture::~LTexture()
{ {
// Deallocate // Deallocate
free(); unload();
} }
bool LTexture::loadFromFile(std::string path, SDL_Renderer *renderer) bool LTexture::loadFromFile(std::string path, SDL_Renderer *renderer)
@@ -45,12 +44,13 @@ bool LTexture::loadFromFile(std::string path, SDL_Renderer *renderer)
} }
// Get rid of preexisting texture // Get rid of preexisting texture
free(); unload();
// The final texture // The final texture
SDL_Texture *newTexture = NULL; SDL_Texture *newTexture = NULL;
// Load image at specified path // Load image at specified path
//SDL_Surface *loadedSurface = IMG_Load(path.c_str());
SDL_Surface *loadedSurface = SDL_CreateRGBSurfaceWithFormatFrom((void *)data, width, height, depth, pitch, pixel_format); SDL_Surface *loadedSurface = SDL_CreateRGBSurfaceWithFormatFrom((void *)data, width, height, depth, pitch, pixel_format);
if (loadedSurface == NULL) if (loadedSurface == NULL)
{ {
@@ -58,6 +58,9 @@ bool LTexture::loadFromFile(std::string path, SDL_Renderer *renderer)
} }
else 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 // Create texture from surface pixels
newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface); newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
if (newTexture == NULL) if (newTexture == NULL)
@@ -97,7 +100,7 @@ bool LTexture::createBlank(SDL_Renderer *renderer, int width, int height, SDL_Te
return mTexture != NULL; return mTexture != NULL;
} }
void LTexture::free() void LTexture::unload()
{ {
// Free texture if it exists // Free texture if it exists
if (mTexture != NULL) if (mTexture != NULL)
@@ -127,7 +130,7 @@ void LTexture::setAlpha(Uint8 alpha)
SDL_SetTextureAlphaMod(mTexture, alpha); SDL_SetTextureAlphaMod(mTexture, alpha);
} }
void LTexture::render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip, double angle, SDL_Point *center, SDL_RendererFlip flip) 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 // Set rendering space and render to screen
SDL_Rect renderQuad = {x, y, mWidth, mHeight}; SDL_Rect renderQuad = {x, y, mWidth, mHeight};
@@ -139,6 +142,9 @@ void LTexture::render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip, doub
renderQuad.h = clip->h; renderQuad.h = clip->h;
} }
renderQuad.w = renderQuad.w * zoomW;
renderQuad.h = renderQuad.h * zoomH;
// Render to screen // Render to screen
SDL_RenderCopyEx(renderer, mTexture, clip, &renderQuad, angle, center, flip); SDL_RenderCopyEx(renderer, mTexture, clip, &renderQuad, angle, center, flip);
} }

View File

@@ -1,5 +1,5 @@
#pragma once #pragma once
#include "ifdefs.h" #include <SDL2/SDL.h>
#include <stdio.h> #include <stdio.h>
#include <string> #include <string>
@@ -23,7 +23,7 @@ public:
bool createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess = SDL_TEXTUREACCESS_STREAMING); bool createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess = SDL_TEXTUREACCESS_STREAMING);
// Deallocates texture // Deallocates texture
void free(); void unload();
// Set color modulation // Set color modulation
void setColor(Uint8 red, Uint8 green, Uint8 blue); void setColor(Uint8 red, Uint8 green, Uint8 blue);
@@ -35,7 +35,7 @@ public:
void setAlpha(Uint8 alpha); void setAlpha(Uint8 alpha);
// Renders texture at given point // Renders texture at given point
void render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip = NULL, double angle = 0.0, SDL_Point *center = NULL, SDL_RendererFlip flip = SDL_FLIP_NONE); 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);
// Set self as render target // Set self as render target
void setAsRenderTarget(SDL_Renderer *renderer); void setAsRenderTarget(SDL_Renderer *renderer);

View File

@@ -12,38 +12,26 @@ Repres un 14 de febrer de 2021
*/ */
#include "ifdefs.h" #include <SDL2/SDL.h>
#include <time.h> #include <time.h>
#include <stdio.h> #include <stdio.h>
#include <string> #include "prog.h"
#include "const.h"
#include "director.h"
int main(int argc, char *args[]) int main(int argc, char *args[])
{ {
printf("Booting up the game...\n");
// Inicia el generador de numeros aleatorios // Inicia el generador de numeros aleatorios
srand(time(nullptr)); srand(time(nullptr));
// Crea el objeto director // Crea el objeto director
Director *director = new Director(args[0]); Prog *prog = new Prog(args[0]);
// Comprueba si existen todos los ficheros de recursos // Bucle principal
if (!director->initResourceList()) prog->run();
return -1;
// Arranca SDL y crea la ventana
if (!director->initSDL())
return -1;
director->initGame();
while (director->isRunning())
{
director->runGame();
}
director->quitGame();
// Libera todos los recursos y cierra SDL // Libera todos los recursos y cierra SDL
delete director; delete prog;
printf("Shutting down the game...\n"); printf("Shutting down the game...\n");
return 0; return 0;

View File

@@ -1,99 +1,216 @@
#include "const.h"
#include "map.h" #include "map.h"
// Constructor // Constructor
Map::Map(SDL_Renderer *renderer, LTexture *texture1, LTexture *texture2, LTexture *texture3, std::string file) Map::Map(std::string file, SDL_Renderer *renderer, Asset *asset)
{ {
init(renderer, texture1, texture2, texture3, file); // Copia los punteros a objetos
this->asset = asset;
this->renderer = renderer;
// Crea los objetos
texture_tile = new LTexture();
texture_bg = new LTexture();
load(file);
loadTextureFromFile(texture_tile, asset->get(tileset_img), renderer);
loadTextureFromFile(texture_bg, asset->get(bg_img), renderer);
// Crea la textura para el mapa de tiles de la habitación
map_texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
if (map_texture == NULL)
printf("Error: map_texture could not be created!\nSDL Error: %s\n", SDL_GetError());
// Pinta el mapa de la habitación en la textura
fillMapTexture();
} }
// Destructor // Destructor
Map::~Map() Map::~Map()
{ {
delete sprite_tile; // Reclama la memoria utilizada por los objetos
delete sprite_actor; texture_tile->unload();
delete background; delete texture_tile;
sprite_tile = nullptr;
sprite_actor = nullptr;
background = nullptr;
//free(tile); texture_bg->unload();
//free(actor); delete texture_bg;
delete[] tile;
delete[] actor; SDL_DestroyTexture(map_texture);
} }
// Inicializa todas las variables // Carga las variables desde un fichero
void Map::init(SDL_Renderer *renderer, LTexture *texture1, LTexture *texture2, LTexture *texture3, std::string file) bool Map::load(std::string file_path)
{ {
sprite_tile = new AnimatedSprite(); // Indicador de éxito en la carga
sprite_tile->init(texture1, renderer); bool success = true;
sprite_actor = new AnimatedSprite(); std::string filename = file_path.substr(file_path.find_last_of("\\/") + 1);
sprite_actor->init(texture2, renderer); std::string line;
std::ifstream file(file_path);
background = new Sprite(); // El fichero se puede abrir
background->init(texture3, renderer); if (file.good())
background->setSpriteClip(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); {
// 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 [tilemap] se realiza el proceso de carga del fichero tmx
if (line == "[tilemap]")
{
do
{
std::getline(file, line);
if (line.find(".tmx") != std::string::npos)
{
std::ifstream file2(asset->get(line)); // Abre el fichero tmx
if (file2.good())
{
bool data_read = false;
while (std::getline(file2, line)) // Lee el fichero linea a linea
{
if (!data_read)
{ // Lee lineas hasta que encuentre donde empiezan los datos del mapa
int pos = 0;
do
{
std::getline(file2, line);
pos = line.find("data encoding");
} while (pos == std::string::npos);
src_rect = {0, 0, 0, 0}; do
dst_rect = {0, 0, 0, 0}; { // Se introducen los valores separados por comas en un vector
//tile = (Uint8 *)malloc(1000 * sizeof(Uint8)); data_read = true;
//actor = (Uint8 *)malloc(1000 * sizeof(Uint8)); std::getline(file2, line);
w = 0; if (line != "</data>")
h = 0; {
room = 0; std::stringstream ss(line);
std::string tmp;
loadFromFile(file); while (getline(ss, tmp, ','))
{
tilemap.push_back(std::stoi(tmp));
}
}
} while (line != "</data>");
}
}
}
}
} while (line != "[tilemap-end]");
} }
// Carga el mapa a partir de un fichero // En caso contrario se parsea el fichero para buscar las variables y los valores
void Map::loadFromFile(std::string path) else
{ {
std::string filename = path.substr(path.find_last_of("\\/") + 1); // Encuentra la posición del caracter '='
SDL_RWops *file = SDL_RWFromFile(path.c_str(), "r+b"); int pos = line.find("=");
Uint8 *w; // Procesa las dos subcadenas
Uint8 *h; if (!setVars(line.substr(0, pos), line.substr(pos + 1, line.length())))
{
printf("Warning: file %s, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str());
success = false;
}
}
}
if (file == NULL) // Cierra el fichero
printf("Closing file %s\n", filename.c_str());
file.close();
}
// El fichero no se puede abrir
else
{ {
printf("Warning: Unable to open %s file\n", filename.c_str()); printf("Warning: Unable to open %s file\n", filename.c_str());
success = false;
}
return success;
}
// Asigna variables a partir de dos cadenas
bool Map::setVars(std::string var, std::string value)
{
// Indicador de éxito en la asignación
bool success = true;
if (var == "bg_img")
{
bg_img = value;
}
else if (var == "tileset_img")
{
tileset_img = value;
}
else if (var == "room_up")
{
room_up = value;
}
else if (var == "room_down")
{
room_down = value;
}
else if (var == "room_left")
{
room_left = value;
}
else if (var == "room_right")
{
room_right = value;
}
else if (var == "tilemap")
{
// Se introducen los valores separados por comas en un vector
std::stringstream ss(value);
std::string tmp;
while (getline(ss, tmp, ','))
{
tilemap.push_back(std::stoi(tmp));
}
}
else if (var == "")
{
} }
else else
{ {
printf("Reading file %s\n", filename.c_str()); success = false;
SDL_RWread(file, &w, sizeof(Uint8), 1);
SDL_RWread(file, &h, sizeof(Uint8), 1);
long size = (*w) * (*h);
tile = new Uint8[size];
actor = new Uint8[size];
for (long i = 0; i < size; i++)
SDL_RWread(file, &tile[i], sizeof(Uint8), 1);
for (long i = 0; i < size; i++)
SDL_RWread(file, &actor[i], sizeof(Uint8), 1);
SDL_RWclose(file);
}
} }
// Resetea ciertas variables return success;
void Map::reset() }
// Crea la textura con el mapeado de la habitación
void Map::fillMapTexture()
{ {
} SDL_SetRenderTarget(renderer, map_texture);
SDL_SetTextureBlendMode(map_texture, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
SDL_RenderClear(renderer);
// Actualiza todas las variables // Dibuja la textura de fondo
void Map::update() SDL_Rect clip = {0, 0, 320, 240-32};
texture_bg->render(renderer, 0, 0, &clip);
// Dibuja el mapeado de tiles
const int tile_size = 16;
const int tileset_width_in_tiles = 16;
const int map_width_in_tiles = 20;
const int map_height_in_tiles = 13;
clip = {0, 0, tile_size, tile_size};
for (int y = 0; y < map_height_in_tiles; y++)
for (int x = 0; x < map_width_in_tiles; x++)
{ {
background->setPosX(0); clip.x = ((tilemap[(y * map_width_in_tiles) + x] - 1) % tileset_width_in_tiles) * tile_size;
background->setPosY(0); clip.y = ((tilemap[(y * map_width_in_tiles) + x] - 1) / tileset_width_in_tiles) * tile_size;
texture_tile->render(renderer, x * tile_size, y * tile_size, &clip);
} }
// Dibuja el objeto SDL_SetRenderTarget(renderer, nullptr);
}
// Dibuja el mapa en pantalla
void Map::render() void Map::render()
{ {
background->render(); // Dibuja la textura con el mapa en pantalla
SDL_Rect rect = {0, 0, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT};
SDL_RenderCopy(renderer, map_texture, &rect, NULL);
} }

View File

@@ -1,7 +1,13 @@
#pragma once #pragma once
#include "animatedsprite.h"
#include "jail_audio.h" #include <SDL2/SDL.h>
#include "utils.h" #include "utils.h"
#include "asset.h"
#include "const.h"
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#ifndef MAP_H #ifndef MAP_H
#define MAP_H #define MAP_H
@@ -9,40 +15,41 @@
// The player // The player
class Map class Map
{ {
private:
Asset *asset; // Objeto con la ruta a todos los ficheros de recursos
SDL_Renderer *renderer; // El renderizador de la ventana
std::string room_up; // Identificador de la habitación que se encuentra arriba
std::string room_down; // Identificador de la habitación que se encuentra abajp
std::string room_left; // Identificador de la habitación que se encuentra a la izquierda
std::string room_right; // Identificador de la habitación que se encuentra a la derecha
std::string tileset_img; // Imagen con los graficos para la habitación
std::string bg_img; // Imagen con los graficos para la habitación
std::vector<int> tilemap; // Indice de los tiles a dibujar en la habitación
LTexture *texture_tile; // Textura con los graficos de los tiles habitación
LTexture *texture_bg; // Textura con los graficos de fondo de la habitación
SDL_Texture *map_texture; // Textura para dibujar el mapa de la habitación
// Carga las variables desde un fichero
bool load(std::string file);
// Asigna variables a partir de dos cadenas
bool setVars(std::string var, std::string value);
// Pinta el mapa de la habitación en la textura
void fillMapTexture();
public: public:
// Constructor // Constructor
Map(SDL_Renderer *renderer, LTexture *texture1, LTexture *texture2, LTexture *texture3, std::string file); Map(std::string file, SDL_Renderer *renderer, Asset *asset);
// Destructor // Destructor
~Map(); ~Map();
// Inicializa todas las variables
void init(SDL_Renderer *renderer, LTexture *texture1, LTexture *texture2, LTexture *texture3, std::string file);
// Carga el mapa a partir de un fichero
void loadFromFile(std::string file);
// Resetea ciertas variables
void reset();
// Actualiza todas las variables // Actualiza todas las variables
void update(); void update();
// Dibuja el objeto // Dibuja el objeto
void render(); void render();
private:
AnimatedSprite *sprite_tile; // Sprite de los tiles del mapa
AnimatedSprite *sprite_actor; // Sprite de los actores
Sprite *background; // Fondo de la pantalla
SDL_Rect src_rect; // Ni puta idea
SDL_Rect dst_rect; // Ni puta idea
Uint8 *tile; // Vector con los tiles
Uint8 *actor; // Vector con los acores
Uint8 w; // Anchura en habitaciones del mapa
Uint8 h; // Altura en habitaciones del mapa
Uint8 room; // Habitación actual del mapa
std::string mapfile; // Ruta con el fichero del mapa
}; };
#endif #endif

View File

@@ -1,431 +0,0 @@
#include "ifdefs.h"
#include "const.h"
#include "text.h"
#include "menu.h"
// Constructor
Menu::Menu()
{
init("", 0, 0, 0, nullptr, nullptr, nullptr);
}
Menu::~Menu()
{
mRenderer = nullptr;
mText = nullptr;
}
// Inicializador
void Menu::init(std::string name, int x, int y, int backgroundType, LTexture *texture, SDL_Renderer *renderer, Text *text)
{
// Inicia variables
mName = name;
mSelector.index = 0;
mTotalItems = 0;
mItemSelected = MENU_NO_OPTION;
mVerticalPadding = 1;
mPosX = x;
mPosY = y;
mRectBG.x = 0;
mRectBG.y = 0;
mRectBG.w = 0;
mRectBG.h = 0;
mRectBG.r = 0;
mRectBG.g = 0;
mRectBG.b = 0;
mSelector.rect.x = 0;
mSelector.rect.y = 0;
mSelector.rect.w = 0;
mSelector.rect.h = 0;
mSelector.rect.r = 0;
mSelector.rect.g = 0;
mSelector.rect.b = 0;
mSelector.rect.a = 255;
mBackgroundType = backgroundType;
mText = text;
mRenderer = renderer;
mIsCentered = false;
mAreElementsCentered = false;
mCenter = x + ((SCREEN_WIDTH - x) / 2);
mWidestItem = 0;
// Selector
mSelector.origin = 0;
mSelector.target = 0;
mSelector.y = 0;
mSelector.numJumps = 4;
mSelector.despY = 0;
mSelector.moving = false;
// Elementos del menu
for (int i = 0; i < 10; i++)
{
mItem[i].label = "";
mItem[i].w = 0;
mItem[i].h = 0;
mItem[i].x = 0;
mItem[i].y = 0;
mItem[i].hPaddingUp = 0;
mItem[i].hPaddingDown = 0;
}
}
// Obtiene el nombre del menu
std::string Menu::getName()
{
return mName;
}
// Obtiene el valor de la variable
Uint8 Menu::getItemSelected()
{
return mItemSelected;
}
// Actualiza la posicion y el estado del selector
void Menu::updateSelector()
{
if (mSelector.moving)
{
mSelector.y += mSelector.despY;
if (mSelector.despY > 0) // Va hacia abajo
{
if (mSelector.y > mSelector.target) // Ha llegado al destino
{
mSelector.origin = mSelector.y = mSelector.target;
mSelector.moving = false;
}
}
if (mSelector.despY < 0) // Va hacia abajo
{
if (mSelector.y < mSelector.target) // Ha llegado al destino
{
mSelector.origin = mSelector.y = mSelector.target;
mSelector.moving = false;
}
}
mSelector.rect.y = int(mSelector.y) - 1;
}
else
{
mSelector.rect.y = int(mSelector.y) - 1;
}
}
// Establece el origen del selector
void Menu::setSelectorOrigin(int value)
{
mSelector.origin = value;
}
// Establece el destino del selector
void Menu::setSelectorTarget(int value)
{
mSelector.target = value;
}
// Coloca el selector en una posición específica
void Menu::setSelectorPos(Uint8 index)
{
if (index < mTotalItems)
{
mSelector.index = index;
mSelector.y = mSelector.origin = mSelector.target = mItem[mSelector.index].y;
mSelector.moving = false;
}
}
// Obtiene la anchura del elemento más ancho del menu
Uint16 Menu::getWidestItem()
{
Uint16 result = 0;
// Obtenemos la anchura del item mas ancho
for (Uint8 i = 0; i < mTotalItems; i++)
{
if (mItem[i].w > result)
{
result = mItem[i].w;
}
}
return result;
}
// Deja el menu apuntando al primer elemento
void Menu::reset()
{
mItemSelected = MENU_NO_OPTION;
mSelector.index = 0;
mSelector.origin = mSelector.target = mSelector.y = mItem[0].y;
mSelector.moving = false;
}
// Deja el menu sin elemento seleccionado
void Menu::deselectItem()
{
mItemSelected = MENU_NO_OPTION;
}
// Actualiza el menu para recolocarlo correctamente y establecer el tamaño
void Menu::reorganize()
{
setRectSize();
if (mIsCentered)
centerMenu(mCenter);
if (mAreElementsCentered)
centerMenuElements();
}
// Deja el menu apuntando al siguiente elemento
bool Menu::increaseSelectorIndex()
{
bool success = false;
mSelector.y = mSelector.origin = mItem[mSelector.index].y;
if (mSelector.index < (mTotalItems - 1))
{
mSelector.index++;
success = true;
}
mSelector.target = mItem[mSelector.index].y;
mSelector.despY = (mSelector.target - mSelector.origin) / mSelector.numJumps;
mSelector.moving = true;
return success;
}
// Deja el menu apuntando al elemento anterior
bool Menu::decreaseSelectorIndex()
{
bool success = false;
mSelector.y = mSelector.origin = mItem[mSelector.index].y;
if (mSelector.index > 0)
{
mSelector.index--;
success = true;
}
mSelector.target = mItem[mSelector.index].y;
mSelector.despY = (mSelector.target - mSelector.origin) / mSelector.numJumps;
mSelector.moving = true;
return success;
}
// Comprueba la entrada (teclado, gamepad) y actua en consecuencia
bool Menu::checkInput(Uint8 input)
{
bool success = false;
switch (input)
{
case INPUT_UP:
success = decreaseSelectorIndex();
break;
case INPUT_DOWN:
success = increaseSelectorIndex();
break;
case INPUT_ACCEPT:
mItemSelected = mSelector.index;
break;
case INPUT_CANCEL:
mItemSelected = mDefaultActionWhenCancel;
break;
}
return success;
}
// Actualiza la logica del menu
void Menu::update()
{
updateSelector();
}
// Pinta el menu en pantalla
void Menu::render()
{
SDL_Rect rect = {mRectBG.x, mRectBG.y, mRectBG.w, mRectBG.h};
// Rendereritza el fondo del menu
if (mBackgroundType == MENU_BACKGROUND_SOLID)
{
SDL_SetRenderDrawColor(mRenderer, mRectBG.r, mRectBG.g, mRectBG.b, mRectBG.a);
SDL_RenderFillRect(mRenderer, &rect);
}
SDL_Rect rect2 = {mSelector.rect.x, mSelector.rect.y, mSelector.rect.w, mSelector.rect.h};
// Renderiza el rectangulo del selector
SDL_SetRenderDrawColor(mRenderer, mSelector.rect.r, mSelector.rect.g, mSelector.rect.b, mSelector.rect.a);
SDL_RenderFillRect(mRenderer, &rect2);
// Renderitza el text
for (Uint8 i = 0; i < mTotalItems; i++)
{
if (i == mSelector.index)
{
mText->writeColored(mItem[i].x, mItem[i].y, mItem[i].label, mSelector.itemR, mSelector.itemG, mSelector.itemB);
}
else
{
mText->write(mItem[i].x, mItem[i].y, mItem[i].label, 0);
}
}
}
// Establece el rectangulo de fondo del menu y el selector
void Menu::setRectSize()
{
Uint8 i = 0;
mRectBG.w = 0;
mRectBG.h = 0;
mSelector.rect.w = 0;
mSelector.rect.h = 0;
// Obtenemos la anchura del item mas ancho y la altura de la suma de alturas de los items
for (i = 0; i < mTotalItems; i++)
{
if (mItem[i].w > mRectBG.w)
{
mRectBG.w = mItem[i].w;
}
mRectBG.h += mItem[i].h + mItem[i].hPaddingDown;
}
// La anchura de la cadena más larga, mas un caracter, mas la anchura del sprite del selector
mRectBG.w += (mText->getHeight() * 1); // + mSelectorSprite.getWidth();
// La altura de la suma de los items mas un caracter y menos un pixel (porque el texto en realidad es de 7 pixeles)
mRectBG.h += (mText->getHeight() * 1) - 1;
// La posición X es la del menú menos la anchura del sprite del selector y menos medio caracter
mRectBG.x = mPosX - (mText->getHeight() / 2); // - mSelectorSprite.getWidth();
// La posición Y es la del menu menos la altura de medio caracter i el padding
mRectBG.y = mPosY - (mText->getHeight() / 2) - mVerticalPadding;
// Establecemos los valores del rectangulo del selector a partir de los valores del rectangulo de fondo
mSelector.rect.h = (mText->getHeight() * 1) + 1;
mSelector.rect.w = mRectBG.w;
mSelector.rect.x = mRectBG.x;
}
// Establece el valor de la variable
void Menu::setTotalItems(int num)
{
mTotalItems = num;
}
// Establece el color del rectangulo de fondo
void Menu::setBackgroundColor(int r, int g, int b, int alpha)
{
mRectBG.r = r;
mRectBG.g = g;
mRectBG.b = b;
mRectBG.a = alpha;
}
// Establece el color del rectangulo del selector
void Menu::setSelectorColor(int r, int g, int b, int alpha)
{
mSelector.rect.r = r;
mSelector.rect.g = g;
mSelector.rect.b = b;
mSelector.rect.a = alpha;
}
// Establece el color del texto del selector
void Menu::setSelectorTextColor(int r, int g, int b)
{
mSelector.itemR = r;
mSelector.itemG = g;
mSelector.itemB = b;
}
// Centra el menu respecto un punto
void Menu::centerMenu(int value)
{
mIsCentered = true;
mCenter = value;
// Actualiza el rectangulo de fondo para recalcular las dimensiones
setRectSize();
// Obten el acho del menu
mWidestItem = getWidestItem();
// Establece la nueva posición centrada en funcion del elemento más ancho
mPosX = (value) - (mWidestItem / 2);
// Reposiciona los elementos del menu
for (Uint8 i = 0; i < 10; i++)
{
mItem[i].x = mPosX;
}
// Recalcula el rectangulo de fondo
setRectSize();
// Recoloca el selector
mSelector.origin = mSelector.target = mSelector.y = mItem[mSelector.index].y;
mSelector.moving = false;
//moveSelectorSprite(mSelector.index);
}
// Centra los elementos del menu
void Menu::centerMenuElements()
{
mAreElementsCentered = true;
for (Uint8 i = 0; i < mTotalItems; i++)
{
//mItem[i].x = (mCenter - ((mText->lenght(mItem[i].label, 0)) / 2));
mItem[i].x = (mCenter - (mItem[i].w / 2));
}
}
// Añade un item al menu
void Menu::addItem(std::string text, const Uint8 hPaddingUp, const Uint8 hPaddingDown)
{
// Si es el primer item coge la posición y del propio menu
if (mTotalItems == 0)
{
mItem[mTotalItems].label = text;
mItem[mTotalItems].w = mText->lenght(mItem[mTotalItems].label, 0);
mItem[mTotalItems].h = mText->getHeight() + (mVerticalPadding * 2);
mItem[mTotalItems].x = mPosX;
mItem[mTotalItems].y = mPosY;
mItem[mTotalItems].hPaddingUp = hPaddingUp;
mItem[mTotalItems].hPaddingDown = hPaddingDown;
}
else
{
// En caso contrario, coge la posición y a partir del elemento anterior
if (mTotalItems < 10)
{
mItem[mTotalItems].label = text;
mItem[mTotalItems].w = mText->lenght(mItem[mTotalItems].label, 0);
mItem[mTotalItems].h = mText->getHeight() + (mVerticalPadding * 2);
mItem[mTotalItems].x = mPosX;
mItem[mTotalItems].y = mItem[mTotalItems - 1].y + mItem[mTotalItems - 1].h + mItem[mTotalItems - 1].hPaddingDown;
mItem[mTotalItems].hPaddingUp = hPaddingUp;
mItem[mTotalItems].hPaddingDown = hPaddingDown;
}
}
setTotalItems(mTotalItems + 1);
reorganize();
setSelectorPos(0);
}
// Cambia el texto de un item
void Menu::setItemCaption(Uint8 index, std::string text)
{
mItem[index].label = text;
mItem[index].w = mText->lenght(mItem[index].label, 0);
reorganize();
}
// Establece el indice del itemm que se usará por defecto al cancelar el menu
void Menu::setDefaultActionWhenCancel(Uint8 item)
{
mDefaultActionWhenCancel = item;
}

View File

@@ -1,161 +0,0 @@
#pragma once
#include "sprite.h"
#include "text.h"
#ifndef MENU_H
#define MENU_H
// Clase menu
class Menu
{
public:
// Constructor
Menu();
// Destructor
~Menu();
// Inicializador
void init(std::string name, int x, int y, int backgroundType, LTexture *texture, SDL_Renderer *renderer, Text *text);
// Obtiene el nombre del menu
std::string getName();
// Obtiene el valor de la variable
Uint8 getItemSelected();
// Deja el menu apuntando al primer elemento
void reset();
// Deja el menu sin elemento seleccionado
void deselectItem();
// Comprueba la entrada (teclado, gamepad) y actua en consecuencia
bool checkInput(Uint8 input);
// Actualiza la logica del menu
void update();
// Pinta el menu en pantalla
void render();
// Establece el color del rectangulo de fondo
void setBackgroundColor(int r, int g, int b, int alpha);
// Establece el color del rectangulo del selector
void setSelectorColor(int r, int g, int b, int alpha);
// Establece el color del texto del selector
void setSelectorTextColor(int r, int g, int b);
// Centra el menu respecto a un punto
void centerMenu(int value);
// Centra los elementos del menu
void centerMenuElements();
// Añade un item al menu
void addItem(std::string text, const Uint8 hPaddingUp = 0, const Uint8 hPaddingDown = 0);
// Cambia el texto de un item
void setItemCaption(Uint8 index, std::string text);
// Establece el indice del item que se usará por defecto al cancelar el menu
void setDefaultActionWhenCancel(Uint8 item);
private:
// Establece el valor de la variable
void setTotalItems(int num);
// Establece el rectangulo de fondo del menu
void setRectSize();
// Actualiza el menu para recolocarlo correctamente y establecer el tamaño
void reorganize();
// Deja el menu apuntando al siguiente elemento
bool increaseSelectorIndex();
// Deja el menu apuntando al elemento anterior
bool decreaseSelectorIndex();
// Mueve el grafico del selector al elemento seleccionado
//void moveSelectorSprite(int pos);
// Actualiza la posicion y el estado del selector
void updateSelector();
// Establece el origen del selector
void setSelectorOrigin(int value);
// Establece el destino del selector
void setSelectorTarget(int value);
// Coloca el selector en una posición específica
void setSelectorPos(Uint8 index);
// Obtiene la anchura del elemento más ancho del menu
Uint16 getWidestItem();
bool mAreElementsCentered; // Variable para saber si los elementos del menu van centrados respecto a este
bool mIsCentered; // Variable para saber si el menu debe estar centrado respecto a un punto
int mCenter; // Centro del menu
int mPosX; // En esta posición del eje X se pinta la primera letra del primer elemento
int mPosY; // En esta posición del eje Y se pinta la primera letra del primer elemento
SDL_Renderer *mRenderer; // Puntero al renderizador de la ventana
std::string mName; // Nombre del menu
Text *mText; // Objeto de texto para poder escribir los items del menu
Uint16 mWidestItem; // Anchura del elemento más ancho
Uint8 mBackgroundType; // Tipo de fondo para el menu, visible o no
Uint8 mDefaultActionWhenCancel; // Item del menu que se selecciona cuando se cancela el menu
Uint8 mItemSelected; // Item del menu que ha sido seleccionado
Uint8 mTotalItems; // Numero de items del menu
Uint8 mVerticalPadding; // Espacio de separacion entre items
struct rectangle
{
Uint8 x; // Posición en el eje X
Uint8 y; // Posición en el eje Y
Uint8 w; // Anchura
Uint8 h; // Altura
Uint8 r; // Rojo
Uint8 g; // Verde
Uint8 b; // Azul
Uint8 a; // Transparencia
};
rectangle mRectBG; // Rectangulo de fondo del menu
// Estructura para cada elemento del menu
struct item
{
std::string label; // Texto del elemento
int x; // Posición en el eje X donde se empieza a escribir el texto
int y; // Posición en el eje Y donde se empieza a escribir el texto
Uint8 w; // Anchura del elemento
Uint8 h; // Altura del elemento
Uint8 hPaddingUp; // Espacio vacío arriba
Uint8 hPaddingDown; // Espacio vacío abajo
};
item mItem[10];
struct selector
{
double origin; // Coordenada de origen
double target; // Coordenada de destino
double y; // Coordenada actual
Uint8 numJumps; // Numero de pasos preestablecido para llegar al destino
double despY; // Desplazamiento en el eje Y que realiza el selector en cada iteración. (target - origin) / numJumps
bool moving; // Indica si el selector está avanzando hacia el destino
Uint8 index; // Elemento del menu que tiene el foco
rectangle rect; // Rectangulo del selectur
Uint8 itemR; // Color para el texto seleccionado. Rojo
Uint8 itemG; // Color para el texto seleccionado. Verde
Uint8 itemB; // Color para el texto seleccionado. Azul
};
selector mSelector;
};
#endif

View File

@@ -1,23 +1,13 @@
#include "const.h" #include "const.h"
#include "movingsprite.h" #include "movingsprite.h"
#include <iostream>
// Constructor // Constructor
MovingSprite::MovingSprite() MovingSprite::MovingSprite(float x, float y, int w, int h, float velx, float vely, float accelx, float accely, LTexture *texture, SDL_Renderer *renderer)
{ {
init(0, 0, 0, 0, 0, 0, 0, 0, nullptr, nullptr); // Copia los punteros
} setTexture(texture);
setRenderer(renderer);
// Destructor
MovingSprite::~MovingSprite()
{
init(0, 0, 0, 0, 0, 0, 0, 0, nullptr, nullptr);
}
// Iniciador
void MovingSprite::init(float x, float y, Uint16 w, Uint16 h, float velx, float vely, float accelx, float accely,
LTexture *texture, SDL_Renderer *renderer)
{
// Establece el alto y el ancho del sprite // Establece el alto y el ancho del sprite
setWidth(w); setWidth(w);
setHeight(h); setHeight(h);
@@ -34,74 +24,135 @@ void MovingSprite::init(float x, float y, Uint16 w, Uint16 h, float velx, float
setAccelX(accelx); setAccelX(accelx);
setAccelY(accely); setAccelY(accely);
// Establece la textura donde están los gráficos para el sprite // Establece el zoom W,H del sprite
setTexture(texture); setZoomW(1);
setZoomH(1);
// Establece el renderizador // Establece el angulo con el que se dibujará
setRenderer(renderer); setAngle(0.0);
// Establece los valores de rotacion
setRotate(false);
setRotateSpeed(0);
setRotateAmount(0.0);
// Contador interno
mCounter = 0;
// Establece el rectangulo de donde coger la imagen // Establece el rectangulo de donde coger la imagen
setSpriteClip(0, 0, w, h); setSpriteClip(0, 0, w, h);
// Establece el centro de rotación
mCenter = {0,0};
// Establece el tipo de volteado
mFlip = SDL_FLIP_NONE;
};
// Destructor
MovingSprite::~MovingSprite()
{
} }
// Coloca el sprite en su nueva posición en funcion de la velocidad // Reinicia todas las variables
void MovingSprite::clear()
{
mPosX = 0.0f; // Posición en el eje X
mPosY = 0.0f; // Posición en el eje Y
mVelX = 0.0f; // Velocidad en el eje X. Cantidad de pixeles a desplazarse
mVelY = 0.0f; // Velocidad en el eje Y. Cantidad de pixeles a desplazarse
mAccelX = 0.0f; // Aceleración en el eje X. Variación de la velocidad
mAccelY = 0.0f; // Aceleración en el eje Y. Variación de la velocidad
mZoomW = 1.0f; // Zoom aplicado a la anchura
mZoomH = 1.0f; // Zoom aplicado a la altura
mAngle = 0.0; // Angulo para dibujarlo
mRotate = false; // Indica si ha de rotar
mCenter = {0, 0}; // Centro de rotación
mRotateSpeed = 0; // Velocidad de giro
mRotateAmount = 0.0; // Cantidad de grados a girar en cada iteración
mCounter = 0; // Contador interno
mFlip = SDL_FLIP_NONE; // Establece como se ha de voltear el sprite
}
// Mueve el sprite
void MovingSprite::move() void MovingSprite::move()
{
if (mEnabled)
{ {
mPosX += mVelX; mPosX += mVelX;
mPosY += mVelY; mPosY += mVelY;
}
// Actualiza las variables internas del sprite: posición y velocidad
void MovingSprite::update()
{
move();
mVelX += mAccelX; mVelX += mAccelX;
mVelY += mAccelY; mVelY += mAccelY;
} }
}
// Muestra el sprite por pantalla // Muestra el sprite por pantalla
void MovingSprite::render() void MovingSprite::render()
{ {
mTexture->render(mRenderer, (int)mPosX, (int)mPosY, &mSpriteClip); if (mEnabled)
mTexture->render(mRenderer, (int)mPosX, (int)mPosY, &mSpriteClip, mZoomW, mZoomH, mAngle, &mCenter, mFlip);
} }
// Establece el valor de la variable // Obtiene el valor de la variable
float MovingSprite::getPosX() float MovingSprite::getPosX()
{ {
return mPosX; return mPosX;
} }
// Establece el valor de la variable // Obtiene el valor de la variable
float MovingSprite::getPosY() float MovingSprite::getPosY()
{ {
return mPosY; return mPosY;
} }
// Establece el valor de la variable // Obtiene el valor de la variable
float MovingSprite::getVelX() float MovingSprite::getVelX()
{ {
return mVelX; return mVelX;
} }
// Establece el valor de la variable // Obtiene el valor de la variable
float MovingSprite::getVelY() float MovingSprite::getVelY()
{ {
return mVelY; return mVelY;
} }
// Establece el valor de la variable // Obtiene el valor de la variable
float MovingSprite::getAccelX() float MovingSprite::getAccelX()
{ {
return mAccelX; return mAccelX;
} }
// Establece el valor de la variable // Obtiene el valor de la variable
float MovingSprite::getAccelY() float MovingSprite::getAccelY()
{ {
return mAccelY; return mAccelY;
} }
// Obtiene el valor de la variable
float MovingSprite::getZoomW()
{
return mZoomW;
}
// Obtiene el valor de la variable
float MovingSprite::getZoomH()
{
return mZoomH;
}
// Obtiene el valor de la variable
double MovingSprite::getAngle()
{
return mAngle;
}
// Establece el valor de la variable // Establece el valor de la variable
void MovingSprite::setPosX(float x) void MovingSprite::setPosX(float x)
{ {
@@ -137,3 +188,118 @@ void MovingSprite::setAccelY(float y)
{ {
mAccelY = y; mAccelY = y;
} }
// Establece el valor de la variable
void MovingSprite::setZoomW(float w)
{
mZoomW = w;
}
// Establece el valor de la variable
void MovingSprite::setZoomH(float h)
{
mZoomH = h;
}
// Establece el valor de la variable
void MovingSprite::setAngle(double a)
{
mAngle = a;
}
// Incrementa el valor de la variable
void MovingSprite::incAngle(double inc)
{
mAngle += inc;
}
// Decrementa el valor de la variable
void MovingSprite::decAngle(double dec)
{
mAngle -= dec;
}
// Obtiene el valor de la variable
bool MovingSprite::getRotate()
{
return mRotate;
}
// Obtiene el valor de la variable
Uint16 MovingSprite::getRotateSpeed()
{
return mRotateSpeed;
}
// Establece la rotacion
void MovingSprite::rotate()
{
if (mEnabled)
if (mRotate)
{
if (mCounter % mRotateSpeed == 0)
{
incAngle(mRotateAmount);
}
}
}
// Establece el valor de la variable
void MovingSprite::setRotate(bool value)
{
mRotate = value;
}
// Establece el valor de la variable
void MovingSprite::setRotateSpeed(Uint16 value)
{
mRotateSpeed = value;
}
// Establece el valor de la variable
void MovingSprite::setRotateAmount(double value)
{
mRotateAmount = value;
}
// Establece el valor de la variable
void MovingSprite::disableRotate()
{
mRotate = false;
mAngle = 0;
}
// Actualiza las variables internas del objeto
void MovingSprite::update()
{
move();
rotate();
if (mEnabled)
++mCounter %= 60000;
}
// Cambia el sentido de la rotación
void MovingSprite::switchRotate()
{
mRotateAmount *= -1;
}
// Establece el valor de la variable
void MovingSprite::setFlip(SDL_RendererFlip flip)
{
mFlip = flip;
}
// Obtiene el valor de la variable
SDL_RendererFlip MovingSprite::getFlip()
{
return mFlip;
}
// Devuelve el rectangulo donde está el sprite
SDL_Rect MovingSprite::getRect()
{
SDL_Rect rect = {(int)getPosX(), (int)getPosY(), getWidth(), getHeight()};
return rect;
}

View File

@@ -1,5 +1,5 @@
#pragma once #pragma once
#include "ifdefs.h" #include <SDL2/SDL.h>
#include "sprite.h" #include "sprite.h"
#ifndef MOVINGSPRITE_H #ifndef MOVINGSPRITE_H
@@ -8,23 +8,46 @@
// Clase MovingSprite. Añade posicion y velocidad en punto flotante // Clase MovingSprite. Añade posicion y velocidad en punto flotante
class MovingSprite : public Sprite class MovingSprite : public Sprite
{ {
protected:
float mPosX; // Posición en el eje X
float mPosY; // Posición en el eje Y
float mVelX; // Velocidad en el eje X. Cantidad de pixeles a desplazarse
float mVelY; // Velocidad en el eje Y. Cantidad de pixeles a desplazarse
float mAccelX; // Aceleración en el eje X. Variación de la velocidad
float mAccelY; // Aceleración en el eje Y. Variación de la velocidad
float mZoomW; // Zoom aplicado a la anchura
float mZoomH; // Zoom aplicado a la altura
double mAngle; // Angulo para dibujarlo
bool mRotate; // Indica si ha de rotar
Uint16 mRotateSpeed; // Velocidad de giro
double mRotateAmount; // Cantidad de grados a girar en cada iteración
Uint16 mCounter; // Contador interno
SDL_Point mCenter; // Centro de rotación
SDL_RendererFlip mFlip; // Indica como se voltea el sprite
public: public:
// Constructor // Constructor
MovingSprite(); MovingSprite(float x = 0, float y = 0, int w = 0, int h = 0, float velx = 0, float vely = 0, float accelx = 0, float accely = 0, LTexture *texture = nullptr, SDL_Renderer *renderer = nullptr);
// Destructor // Destructor
~MovingSprite(); ~MovingSprite();
// Iniciador // Mueve el sprite
void init(float x, float y, Uint16 w, Uint16 h, float velx, float vely, float accelx, float accely,
LTexture *texture, SDL_Renderer *renderer);
// Coloca el sprite en su nueva posición en funcion de la velocidad
void move(); void move();
// Actualiza las variables internas del sprite: posición y velocidad // Rota el sprite
void rotate();
// Actualiza las variables internas del objeto
void update(); void update();
// Reinicia todas las variables
void clear();
// Muestra el sprite por pantalla // Muestra el sprite por pantalla
void render(); void render();
@@ -46,6 +69,21 @@ public:
// Obten el valor de la variable // Obten el valor de la variable
float getAccelY(); float getAccelY();
// Obten el valor de la variable
float getZoomW();
// Obten el valor de la variable
float getZoomH();
// Obten el valor de la variable
double getAngle();
// Obtiene el valor de la variable
bool getRotate();
// Obtiene el valor de la variable
Uint16 getRotateSpeed();
// Establece el valor de la variable // Establece el valor de la variable
void setPosX(float x); void setPosX(float x);
@@ -64,15 +102,44 @@ public:
// Establece el valor de la variable // Establece el valor de la variable
void setAccelY(float y); void setAccelY(float y);
private: // Establece el valor de la variable
float mPosX; // Posición en el eje X void setZoomW(float w);
float mPosY; // Posición en el eje Y
float mVelX; // Velocidad en el eje X // Establece el valor de la variable
float mVelY; // Velocidad en el eje Y void setZoomH(float h);
float mAccelX; // Aceleración en el eje X // Establece el valor de la variable
float mAccelY; // Aceleración en el eje Y void setAngle(double a);
// Incrementa el valor de la variable
void incAngle(double inc);
// Decrementa el valor de la variable
void decAngle(double dec);
// Establece el valor de la variable
void setRotate(bool value);
// Establece el valor de la variable
void setRotateSpeed(Uint16 value);
// Establece el valor de la variable
void setRotateAmount(double value);
// Quita el efecto de rotación y deja el sprite en su angulo inicial.
void disableRotate();
// Cambia el sentido de la rotación
void switchRotate();
// Establece el valor de la variable
void setFlip(SDL_RendererFlip flip);
// Obtiene el valor de la variable
SDL_RendererFlip getFlip();
// Devuelve el rectangulo donde está el sprite
SDL_Rect getRect();
}; };
#endif #endif

View File

@@ -1,65 +1,53 @@
#include "const.h"
#include "player.h" #include "player.h"
// Constructor // Constructor
Player::Player(SDL_Renderer *renderer, LTexture *texture) Player::Player(SDL_Renderer *renderer, Asset *asset, Input *input)
{ {
init(renderer, texture); this->asset = asset;
this->renderer = renderer;
this->input = input;
sound_jump = JA_LoadSound(asset->get("sound_player_jump.wav").c_str());
sound_death = JA_LoadSound(asset->get("sound_player_death.wav").c_str());
sound_coin = JA_LoadSound(asset->get("sound_player_coin.wav").c_str());
texture = new LTexture();
loadTextureFromFile(texture, asset->get("player.png"), renderer);
sprite = new AnimatedSprite(texture, renderer, asset->get("player.ani"));
sprite->setPosX(16);
sprite->setPosY(168);
sprite->setCurrentAnimation("stand");
can_jump = true;
standing = true;
invulnerable = true;
jumpforce = 10;
enabled = true;
cooldown = 0;
lifes = 10;
coins = 0;
key.insert(key.end(), {0, 0, 0, 0, 0, 0});
} }
// Destructor // Destructor
Player::~Player() Player::~Player()
{ {
JA_DeleteSound(sound_jump);
JA_DeleteSound(sound_death);
JA_DeleteSound(sound_coin);
texture->unload();
delete texture;
delete sprite; delete sprite;
sprite = nullptr;
}
// Inicializa todas las variables
void Player::init(SDL_Renderer *renderer, LTexture *texture)
{
rect = {0, 0, 16, 24};
can_jump = false;
enabled = false;
jump_pressed_before = false;
jump_pressed_now = false;
for (Uint8 i = 0; i < 6; i++)
key[i] = false;
standing = false;
was_on_background = false;
coins = 0;
cooldown = 0;
jumpforce = 0;
respawn_x = 0;
respawn_y = 0;
speed_x = 0;
speed_y = 0;
sprite = new AnimatedSprite();
sprite->init(texture, renderer);
sprite->setSpriteClip(rect);
sound_coin = 0;
sound_death = 0;
sound_jump = 0;
active_animation = 0;
direction = 0;
lifes = 0;
respawn_direction = 0;
}
// Resetea ciertas variables
void Player::reset()
{
} }
// Actualiza todas las variables // Actualiza todas las variables
void Player::update() void Player::update()
{ {
sprite->setPosX(rect.x); checkInput();
sprite->setPosY(rect.y); sprite->update();
} }
// Dibuja el objeto // Dibuja el objeto
@@ -67,3 +55,27 @@ void Player::render()
{ {
sprite->render(); sprite->render();
} }
// Comprueba las entradas y modifica variables
void Player::checkInput()
{
const float speed = 1.0f;
// Solo comprueba las entradas de dirección cuando está de pie
if (input->checkInput(INPUT_LEFT, REPEAT_TRUE))
{
sprite->setVelX(-speed);
sprite->setFlip(SDL_FLIP_NONE);
sprite->setCurrentAnimation("walk");
}
else if (input->checkInput(INPUT_RIGHT, REPEAT_TRUE))
{
sprite->setVelX(speed);
sprite->setFlip(SDL_FLIP_HORIZONTAL);
sprite->setCurrentAnimation("walk");
}
else
{
sprite->setVelX(0);
sprite->setCurrentAnimation("stand");
}
}

View File

@@ -1,7 +1,10 @@
#pragma once #pragma once
#include "animatedsprite.h" #include <SDL2/SDL.h>
#include "jail_audio.h" #include "jail_audio.h"
#include "utils.h" #include "utils.h"
#include "input.h"
#include "animatedsprite.h"
#include "asset.h"
#ifndef PLAYER_H #ifndef PLAYER_H
#define PLAYER_H #define PLAYER_H
@@ -9,49 +12,41 @@
// The player // The player
class Player class Player
{ {
private:
Asset *asset; // Objeto con la ruta a todos los ficheros de recursos
SDL_Renderer *renderer; // El renderizador de la ventana
Input *input; // Objeto Input para gestionar las entradas
AnimatedSprite *sprite; // Objeto con los graficos, animaciones y posición del jugador
LTexture *texture; // Textura con los graficos del jugador
bool can_jump; // Si puede saltar
bool enabled; // Si está habilitado
bool standing; // Si esta de pie (o quieto?)
bool invulnerable; // Si es invulnerable
int coins; // Cantidad de monedas
int cooldown; // Tiempo de inhabilitación
int jumpforce; // Cantidad de pixels a desplazarse y velocidad que pilla al saltar
int lifes; // Cantidad de vidas
std::vector<bool> key; // Indica las llaves que posee el jugador
JA_Sound sound_coin; // Sonido al coger monedas
JA_Sound sound_death; // Sonido al morir
JA_Sound sound_jump; // Sonido al saltar
// Comprueba las entradas y modifica variables
void checkInput();
public: public:
// Constructor // Constructor
Player(SDL_Renderer *renderer, LTexture *texture); Player(SDL_Renderer *renderer, Asset *asset, Input *input);
// Destructor // Destructor
~Player(); ~Player();
// Inicializa todas las variables
void init(SDL_Renderer *renderer, LTexture *texture);
// Resetea ciertas variables
void reset();
// Actualiza todas las variables // Actualiza todas las variables
void update(); void update();
// Dibuja el objeto // Dibuja el objeto
void render(); void render();
private:
bool can_jump; // Si puede saltar
bool enabled; // Si está habilitado
bool jump_pressed_before; // Si se ha pulsado el botón de salto previamente
bool jump_pressed_now; // Si se acaba de pulsar el salto
bool key[6]; // Indica las llaves que posee el jugador
bool standing; // Si esta de pie (o quieto?)
bool was_on_background; // Si viene de una zona atravesable
int coins; // Cantidad de monedas
int cooldown; // Tiempo de inhabilitación
int jumpforce; // Cantidad de pixels a desplazarse y velocidad que pilla al saltar
int respawn_x; // Coordenadas para revivir
int respawn_y; // Coordenades para revivir
int speed_x; // Cantidad de pixeles a desplazarse
int speed_y; // Cantidad de pixels a desplazarse
JA_Sound sound_coin; // Sonido al coger monedas
JA_Sound sound_death; // Sonido al morir
JA_Sound sound_jump; // Sonido al saltar
SDL_Rect rect; // Rectangulo con la posición del jugador
AnimatedSprite *sprite; // Sprite con los graficos y animaciones
Uint8 active_animation; // Animación activa
Uint8 direction; // Sentido del desplazamiento
Uint8 lifes; // Cantidad de vidas
Uint8 respawn_direction; // Dirección para revivir
}; };
#endif #endif

214
source/prog.cpp Normal file
View File

@@ -0,0 +1,214 @@
#include "prog.h"
// Constructor
Prog::Prog(std::string executablePath)
{
// Establece las opciones por defecto
options = new options_t;
options->fullScreenMode = 0;
options->windowSize = 2;
options->filter = FILTER_NEAREST;
options->vSync = true;
options->screenWidth = GAME_WIDTH * options->windowSize;
options->screenHeight = GAME_HEIGHT * options->windowSize;
options->integerScale = true;
options->keepAspect = true;
// Inicia las librerias
initSDL();
initJailAudio();
// Crea los objetos
asset = new Asset(executablePath.substr(0, executablePath.find_last_of("\\/")));
if (!setFileList())
{
section.name = SECTION_PROG_QUIT;
}
else
{
section.name = SECTION_PROG_GAME;
}
input = new Input(asset->get("gamecontrollerdb.txt"));
screen = new Screen(window, renderer, options);
// Controles
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_SPACE);
input->bindKey(INPUT_BUTTON_2, SDL_SCANCODE_D);
input->bindKey(INPUT_BUTTON_PAUSE, SDL_SCANCODE_ESCAPE);
input->bindKey(INPUT_BUTTON_ESCAPE, SDL_SCANCODE_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_B);
input->bindGameControllerButton(INPUT_BUTTON_PAUSE, SDL_CONTROLLER_BUTTON_GUIDE);
input->bindGameControllerButton(INPUT_BUTTON_ESCAPE, SDL_CONTROLLER_BUTTON_GUIDE);
}
Prog::~Prog()
{
delete options;
delete asset;
delete input;
delete screen;
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
// Inicializa JailAudio
void Prog::initJailAudio()
{
JA_Init(48000, AUDIO_S16, 2);
}
// Arranca SDL y crea la ventana
bool Prog::initSDL()
{
// Indicador de éxito
bool success = true;
// Inicializa SDL
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;
}
else
{
// Inicia el generador de numeros aleatorios
std::srand(static_cast<unsigned int>(SDL_GetTicks()));
// 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
window = SDL_CreateWindow(WINDOW_CAPTION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, options->screenWidth, options->screenHeight, SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI);
if (window == NULL)
{
printf("Window could not be created!\nSDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
// Crea un renderizador para la ventana. El vsync se activa en funcion de las opciones
if (options->vSync)
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
else
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == NULL)
{
printf("Renderer could not be created!\nSDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
// Inicializa el color de renderizado
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
// Establece el tamaño del buffer de renderizado
SDL_RenderSetLogicalSize(renderer, options->screenWidth, options->screenHeight);
// Establece el modo de mezcla
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
}
}
}
printf("SDL is running...\n");
return success;
}
// Crea el indice de ficheros de recursos
bool Prog::setFileList()
{
// Ficheros binarios
asset->add("/data/map/01.map", data);
asset->add("/data/map/01.tmx", data);
asset->add("/data/config.bin", data, false);
asset->add("/data/gamecontrollerdb.txt", data);
asset->add("/data/animations/player.ani", data);
// Texturas
asset->add("/media/gfx/actors.png", bitmap);
asset->add("/media/gfx/bg_surface.png", bitmap);
asset->add("/media/gfx/filter.png", bitmap);
asset->add("/media/gfx/hud.png", bitmap);
asset->add("/media/gfx/menu_animation.png", bitmap);
asset->add("/media/gfx/menu.png", bitmap);
asset->add("/media/gfx/player.png", bitmap);
asset->add("/media/gfx/tiles_surface.png", bitmap);
asset->add("/media/gfx/tiles_volcano.png", bitmap);
// Sonidos
asset->add("/media/sound/sound_player_coin.wav", sound);
asset->add("/media/sound/sound_player_death.wav", sound);
asset->add("/media/sound/sound_drop_enemy.wav", sound);
asset->add("/media/sound/sound_drop_splat.wav", sound);
asset->add("/media/sound/sound_player_jump.wav", sound);
asset->add("/media/sound/sound_menu_logo.wav", sound);
asset->add("/media/sound/sound_menu_start.wav", sound);
// Musicas
asset->add("/media/music/music_menu.ogg", music);
asset->add("/media/music/music_surface.ogg", music);
asset->add("/media/music/music_volcano.ogg", music);
return asset->check();
}
// Obtiene el valor de la variable
Uint8 Prog::getSection()
{
return section.name;
}
// Establece el valor de la variable
void Prog::setSection(section_t section)
{
this->section = section;
}
void Prog::runGame()
{
game = new Game(renderer, asset, screen, input);
setSection(game->run());
delete game;
}
void Prog::run()
{
// Bucle principal
while (!(getSection() == SECTION_PROG_QUIT))
{
switch (getSection())
{
case SECTION_PROG_LOGO:
// runLogo();
break;
case SECTION_PROG_INTRO:
// runIntro();
break;
case SECTION_PROG_TITLE:
// runTitle();
break;
case SECTION_PROG_GAME:
runGame();
break;
}
}
}

71
source/prog.h Normal file
View File

@@ -0,0 +1,71 @@
#pragma once
#include <SDL2/SDL.h>
#include <string>
#include "asset.h"
#include "jail_audio.h"
#include "game.h"
#include "input.h"
#include "utils.h"
#include "screen.h"
#ifndef PROG_H
#define PROG_H
#define WINDOW_CAPTION "Volcano"
#define GAME_WIDTH 320
#define GAME_HEIGHT 240
class Prog
{
private:
SDL_Window *window; // La ventana donde dibujamos
SDL_Renderer *renderer; // El renderizador de la ventana
Asset *asset; // Objeto encargado de gestionar los ficheros de recursos
Screen *screen; // Objeto encargado de dibujar en pantalla
Input *input; // Objeto Input para gestionar las entradas
Game *game; // Objeto para la sección del juego
section_t section; // Sección y subsección actual del programa;
struct options_t *options; // Contiene las opciones del programa
// Inicializa jail_audio
void initJailAudio();
// Arranca SDL y crea la ventana
bool initSDL();
// Crea el indice de ficheros
bool setFileList();
// Obtiene el valor de la variable
Uint8 getSubsection();
// Obtiene el valor de la variable
Uint8 getSection();
// Establece el valor de la variable
void setSection(section_t section);
// Ejecuta la seccion de juego con el logo
void runLogo();
// Ejecuta la seccion de juego de la introducción
void runIntro();
// Ejecuta la seccion de juego con el titulo y los menus
void runTitle();
// Ejecuta la seccion de juego donde se juega
void runGame();
public:
// Constructor
Prog(std::string executablePath);
// Destructor
~Prog();
// Bucle principal
void run();
};
#endif

133
source/screen.cpp Normal file
View File

@@ -0,0 +1,133 @@
#include "screen.h"
#include "const.h"
#include <string>
#include <stdio.h>
// Constructor
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, options_t *options)
{
// Inicializa variables
this->window = window;
this->renderer = renderer;
this->options = options;
gameCanvasWidth = SCREEN_WIDTH;
gameCanvasHeight = SCREEN_HEIGHT;
// Establece el modo de video
setVideoMode(options->fullScreenMode);
// Define el color del borde para el modo de pantalla completa
borderColor = {0x27, 0x27, 0x36};
borderColor = {0x00, 0x00, 0x00};
// Crea la textura donde se dibujan los graficos del juego
gameCanvas = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, gameCanvasWidth, gameCanvasHeight);
if (gameCanvas == NULL)
printf("TitleSurface could not be created!\nSDL Error: %s\n", SDL_GetError());
}
// Destructor
Screen::~Screen()
{
renderer = nullptr;
}
// Limpia la pantalla
void Screen::clean(color_t color)
{
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 0xFF);
SDL_RenderClear(renderer);
}
// Prepara para empezar a dibujar en la textura de juego
void Screen::start()
{
SDL_SetRenderTarget(renderer, gameCanvas);
}
// Vuelca el contenido del renderizador en pantalla
void Screen::blit()
{
// Vuelve a dejar el renderizador en modo normal
SDL_SetRenderTarget(renderer, NULL);
// Borra el contenido previo
SDL_SetRenderDrawColor(renderer, borderColor.r, borderColor.g, borderColor.b, 0xFF);
SDL_RenderClear(renderer);
// Copia la textura de juego en el renderizador en la posición adecuada
SDL_RenderCopy(renderer, gameCanvas, NULL, &dest);
// Muestra por pantalla el renderizador
SDL_RenderPresent(renderer);
}
// Establece el modo de video
void Screen::setVideoMode(int fullScreenMode)
{
// Aplica el modo de video
SDL_SetWindowFullscreen(window, fullScreenMode);
// Si está activo el modo ventana quita el borde
if (fullScreenMode == 0)
{
screenWidth = gameCanvasWidth;
screenHeight = gameCanvasHeight;
dest = {0, 0, gameCanvasWidth, gameCanvasHeight};
// Modifica el tamaño del renderizador y de la ventana
SDL_RenderSetLogicalSize(renderer, screenWidth, screenHeight);
SDL_SetWindowSize(window, screenWidth * options->windowSize, screenHeight * options->windowSize);
}
// Si está activo el modo de pantalla completa añade el borde
if (fullScreenMode == SDL_WINDOW_FULLSCREEN_DESKTOP)
{
// Obten el alto y el ancho de la ventana
SDL_GetWindowSize(window, &screenWidth, &screenHeight);
// Aplica el escalado al rectangulo donde se pinta la textura del juego
if (options->integerScale)
{
// Calcula el tamaño de la escala máxima
int scale = 0;
while (((gameCanvasWidth * (scale + 1)) <= screenWidth) && ((gameCanvasHeight * (scale + 1)) <= screenHeight))
{
scale++;
}
dest.w = gameCanvasWidth * scale;
dest.h = gameCanvasHeight * scale;
dest.x = (screenWidth - dest.w) / 2;
dest.y = (screenHeight - dest.h) / 2;
}
else if (options->keepAspect)
{
float ratio = (float)gameCanvasWidth / (float)gameCanvasHeight;
if ((screenWidth - gameCanvasWidth) >= (screenHeight - gameCanvasHeight))
{
dest.h = screenHeight;
dest.w = (int)((screenHeight * ratio) + 0.5f);
dest.x = (screenWidth - dest.w) / 2;
dest.y = (screenHeight - dest.h) / 2;
}
else
{
dest.w = screenWidth;
dest.h = (int)((screenWidth / ratio) + 0.5f);
dest.x = (screenWidth - dest.w) / 2;
dest.y = (screenHeight - dest.h) / 2;
}
}
else
{
dest.w = screenWidth;
dest.h = screenHeight;
dest.x = dest.y = 0;
}
// Modifica el tamaño del renderizador
SDL_RenderSetLogicalSize(renderer, screenWidth, screenHeight);
}
}

44
source/screen.h Normal file
View File

@@ -0,0 +1,44 @@
#pragma once
#include <SDL2/SDL.h>
#include "utils.h"
#ifndef SCREEN_H
#define SCREEN_H
// Clase Screen
class Screen
{
private:
SDL_Window *window; // Ventana de la aplicación
SDL_Renderer *renderer; // El renderizador de la ventana
SDL_Texture *gameCanvas; // Textura para completar la ventana de juego hasta la pantalla completa
options_t *options; // Variable con todas las opciones del programa
int screenWidth; // Ancho de la pantalla
int screenHeight; // Alto de la pantalla
int gameCanvasWidth; // Ancho de la textura donde se dibuja el juego
int gameCanvasHeight; // Alto de la textura donde se dibuja el juego
SDL_Rect dest; // Coordenadas donde se va a dibujar la textura del juego
color_t borderColor; // Color del borde añadido a la textura de juego para rellenar la pantalla
public:
// Constructor
Screen(SDL_Window *window, SDL_Renderer *renderer, options_t *options);
// Destructor
~Screen();
// Limpia la pantalla
void clean(color_t color = {0x00, 0x00, 0x00});
// Prepara para empezar a dibujar en la textura de juego
void start();
// Vuelca el contenido del renderizador en pantalla
void blit();
// Establece el modo de video
void setVideoMode(int fullScreenMode);
};
#endif

View File

@@ -1,228 +0,0 @@
#include "const.h"
#include "smartsprite.h"
// Constructor
SmartSprite::SmartSprite()
{
AnimatedSprite::init(nullptr, nullptr);
init(nullptr, nullptr);
}
// Destructor
SmartSprite::~SmartSprite()
{
init(nullptr, nullptr);
}
// Inicializador
void SmartSprite::init(LTexture *texture, SDL_Renderer *renderer)
{
setTexture(texture);
setRenderer(renderer);
setPosX(0);
setPosY(0);
setWidth(0);
setHeight(0);
setVelX(0);
setVelY(0);
setAccelX(0);
setAccelY(0);
setSpriteClip(0, 0, 0, 0);
setEnabled(false);
setCounter(0);
mIsOnDestination = false;
mDestX = 0;
mDestY = 0;
// El Id siempre es >=0, por lo tanto si no se le asigna Id se queda en negativo
mId = -1;
}
// Pone a cero los elementos del objeto
void SmartSprite::reset()
{
init(nullptr, nullptr);
}
// Obtiene el valor de la variable
bool SmartSprite::isEnabled()
{
return mEnabled;
}
// Establece el valor de la variable
void SmartSprite::setEnabled(bool state)
{
mEnabled = state;
}
// Obtiene el valor del contador
Uint16 SmartSprite::getCounter()
{
return mCounter;
}
// Establece el valor del contador
void SmartSprite::setCounter(Uint16 time)
{
mCounter = time;
}
// Establece el valor del contador
void SmartSprite::resetCounter(Uint16 time)
{
mCounter = mCounterIni;
}
// Establece el valor de la variable
void SmartSprite::setDestX(int value)
{
mDestX = value;
}
// Establece el valor de la variable
void SmartSprite::setDestY(int value)
{
mDestY = value;
}
// Obtiene el valor de la variable
int SmartSprite::getDestX()
{
return mDestX;
}
// Obtiene el valor de la variable
int SmartSprite::getDestY()
{
return mDestY;
}
// Actualiza la posición y comprueba si ha llegado a su destino
bool SmartSprite::update()
{
if (mEnabled)
{
// Actualiza las variables de posición, velocidad y animación
AnimatedSprite::update();
// Comprueba si se desplaza en el eje X hacia la derecha
if ((getAccelX() > 0) || ((getAccelX() == 0) && (getVelX() > 0)))
{
// Comprueba si hemos llegado al destino
if (getPosX() > mDestX)
{
// Lo coloca en posición
setPosX(mDestX);
// Lo detiene
setVelX(0.0f);
setAccelX(0.0f);
}
}
// Comprueba si se desplaza en el eje X hacia la izquierda
else if ((getAccelX() < 0) || ((getAccelX() == 0) && (getVelX() < 0)))
{
// Comprueba si hemos llegado al destino
if (getPosX() < mDestX)
{
// Lo coloca en posición
setPosX(mDestX);
// Lo detiene
setVelX(0.0f);
setAccelX(0.0f);
}
}
// Comprueba si se desplaza en el eje Y hacia abajo
if ((getAccelY() > 0) || ((getAccelY() == 0) && (getVelY() > 0)))
{
// Comprueba si hemos llegado al destino
if (getPosY() > mDestY)
{
// Lo coloca en posición
setPosY(mDestY);
// Lo detiene
setVelY(0.0f);
setAccelY(0.0f);
}
}
// Comprueba si se desplaza en el eje Y hacia arriba
else if ((getAccelY() < 0) || ((getAccelY() == 0) && (getVelY() < 0)))
{
// Comprueba si hemos llegado al destino
if (getPosY() < mDestY)
{
// Lo coloca en posición
setPosY(mDestY);
// Lo detiene
setVelY(0.0f);
setAccelY(0.0f);
}
}
// Comprueba si ha llegado a su destino
if ((getPosX() == mDestX) && (getPosY() == mDestY))
{
mIsOnDestination = true;
}
else
{
mIsOnDestination = false;
}
// Si esta en el destino comprueba su contador
if (mIsOnDestination)
{
// Si el contador es mayor que cero, lo decrementa
if (mCounter > 0)
{
mCounter--;
}
// Si ha llegado a cero, deshabilita el objeto o manda el aviso en función de si tiene Id
else if (mCounter == 0)
{
if (mId < 0)
{
mEnabled = false;
}
else
{
//return true;;
}
}
}
}
return mIsOnDestination;
}
// Obtiene el valor de la variable
bool SmartSprite::isOnDestination()
{
return mIsOnDestination;
}
// Pinta el objeto en pantalla
void SmartSprite::render()
{
if (mEnabled)
{
MovingSprite::render();
}
}
// Establece el valor de la variable
void SmartSprite::setId(int id)
{
mId = id;
}

View File

@@ -1,73 +0,0 @@
#pragma once
#include "struct.h"
#include "animatedsprite.h"
#ifndef SMARTSPRITE_H
#define SMARTSPRITE_H
// Clase SmartSprite
class SmartSprite : public AnimatedSprite
{
public:
// Constructor
SmartSprite();
// Destructor
~SmartSprite();
// Inicializador
void init(LTexture *texture, SDL_Renderer *renderer);
// Pone a cero los elementos del objeto
void reset();
// Obtiene el valor de la variable
bool isEnabled();
// Establece el valor de la variable
void setEnabled(bool state);
// Obtiene el valor del contador
Uint16 getCounter();
// Establece el valor del contador
void setCounter(Uint16 time);
// Inicializa el valor del contador
void resetCounter(Uint16 time);
// Establece el valor de la variable
void setDestX(int value);
// Establece el valor de la variable
void setDestY(int value);
// Obtiene el valor de la variable
int getDestX();
// Obtiene el valor de la variable
int getDestY();
// Obtiene el valor de la variable
bool isOnDestination();
// Establece el valor de la variable
void setId(int id);
// Actualiza la posición y comprueba si ha llegado a su destino
bool update();
// Pinta el objeto en pantalla
void render();
private:
bool mEnabled; // Indica si esta habilitado
bool mIsOnDestination; // Indica si está en el destino
int mDestX; // Posicion de destino en el eje X
int mDestY; // Posicion de destino en el eje Y
int mId; // Identificador
Uint16 mCounter; // Contador
Uint16 mCounterIni; // Valor inicial del contador
};
#endif

View File

@@ -1,20 +1,7 @@
#include "sprite.h" #include "sprite.h"
// Constructor // Constructor
Sprite::Sprite() Sprite::Sprite(int x, int y, int w, int h, LTexture *texture, SDL_Renderer *renderer)
{
init(nullptr, nullptr);
}
// Destructor
Sprite::~Sprite()
{
mTexture = nullptr;
mRenderer = nullptr;
}
// Inicializador
/*void Sprite::init(LTexture *texture, SDL_Renderer *renderer, int x, int y, Uint16 w, Uint16 h)
{ {
// Establece el alto y el ancho del sprite // Establece el alto y el ancho del sprite
setWidth(w); setWidth(w);
@@ -31,19 +18,21 @@ Sprite::~Sprite()
setTexture(texture); setTexture(texture);
// Establece el rectangulo de donde coger la imagen // Establece el rectangulo de donde coger la imagen
setSpriteClip(0, 0, w, h); setSpriteClip(x, y, w, h);
}*/
// Inicializador // Inicializa variables
void Sprite::init(LTexture *texture, SDL_Renderer *renderer, SDL_Rect rect) setEnabled(true);
}
Sprite::Sprite(SDL_Rect rect, LTexture *texture, SDL_Renderer *renderer)
{ {
// Establece el alto y el ancho del sprite // Establece el alto y el ancho del sprite
mWidth = rect.w; setWidth(rect.w);
mHeight = rect.h; setHeight(rect.h);
// Establece la posición X,Y del sprite // Establece la posición X,Y del sprite
mPosX = rect.x; setPosX(rect.x);
mPosY = rect.y; setPosY(rect.y);
// Establece el puntero al renderizador de la ventana // Establece el puntero al renderizador de la ventana
setRenderer(renderer); setRenderer(renderer);
@@ -53,13 +42,26 @@ void Sprite::init(LTexture *texture, SDL_Renderer *renderer, SDL_Rect rect)
// Establece el rectangulo de donde coger la imagen // Establece el rectangulo de donde coger la imagen
setSpriteClip(rect); setSpriteClip(rect);
// Inicializa variables
setEnabled(true);
}
// Destructor
Sprite::~Sprite()
{
mTexture = nullptr;
mRenderer = nullptr;
} }
// Muestra el sprite por pantalla // Muestra el sprite por pantalla
void Sprite::render() void Sprite::render()
{
if (mEnabled)
{ {
mTexture->render(mRenderer, mPosX, mPosY, &mSpriteClip); mTexture->render(mRenderer, mPosX, mPosY, &mSpriteClip);
} }
}
// Obten el valor de la variable // Obten el valor de la variable
int Sprite::getPosX() int Sprite::getPosX()
@@ -74,13 +76,13 @@ int Sprite::getPosY()
} }
// Obten el valor de la variable // Obten el valor de la variable
Uint16 Sprite::getWidth() int Sprite::getWidth()
{ {
return mWidth; return mWidth;
} }
// Obten el valor de la variable // Obten el valor de la variable
Uint16 Sprite::getHeight() int Sprite::getHeight()
{ {
return mHeight; return mHeight;
} }
@@ -98,13 +100,13 @@ void Sprite::setPosY(int y)
} }
// Establece el valor de la variable // Establece el valor de la variable
void Sprite::setWidth(Uint16 w) void Sprite::setWidth(int w)
{ {
mWidth = w; mWidth = w;
} }
// Establece el valor de la variable // Establece el valor de la variable
void Sprite::setHeight(Uint16 h) void Sprite::setHeight(int h)
{ {
mHeight = h; mHeight = h;
} }
@@ -116,7 +118,13 @@ SDL_Rect Sprite::getSpriteClip()
} }
// Establece el valor de la variable // Establece el valor de la variable
void Sprite::setSpriteClip(int x, int y, Uint16 w, Uint16 h) void Sprite::setSpriteClip(SDL_Rect rect)
{
mSpriteClip = rect;
}
// Establece el valor de la variable
void Sprite::setSpriteClip(int x, int y, int w, int h)
{ {
mSpriteClip.x = x; mSpriteClip.x = x;
mSpriteClip.y = y; mSpriteClip.y = y;
@@ -124,12 +132,6 @@ void Sprite::setSpriteClip(int x, int y, Uint16 w, Uint16 h)
mSpriteClip.h = h; mSpriteClip.h = h;
} }
// Establece el valor de la variable
void Sprite::setSpriteClip(SDL_Rect rect)
{
mSpriteClip = rect;
}
// Obten el valor de la variable // Obten el valor de la variable
LTexture *Sprite::getTexture() LTexture *Sprite::getTexture()
{ {
@@ -147,3 +149,22 @@ void Sprite::setRenderer(SDL_Renderer *renderer)
{ {
mRenderer = renderer; mRenderer = renderer;
} }
// Establece el valor de la variable
void Sprite::setEnabled(bool value)
{
mEnabled = value;
}
// Comprueba si el objeto está habilitado
bool Sprite::isEnabled()
{
return mEnabled;
}
// Devuelve el rectangulo donde está el sprite
SDL_Rect Sprite::getRect()
{
SDL_Rect rect = {getPosX(), getPosY(), getWidth(), getHeight()};
return rect;
}

View File

@@ -1,5 +1,5 @@
#pragma once #pragma once
#include "ifdefs.h" #include <SDL2/SDL.h>
#include "ltexture.h" #include "ltexture.h"
#ifndef SPRITE_H #ifndef SPRITE_H
@@ -8,17 +8,26 @@
// Clase sprite // Clase sprite
class Sprite class Sprite
{ {
protected:
int mPosX; // Posición en el eje X donde dibujar el sprite
int mPosY; // Posición en el eje Y donde dibujar el sprite
Uint16 mWidth; // Ancho del sprite
Uint16 mHeight; // Alto del sprite
SDL_Renderer *mRenderer; // Puntero al renderizador de la ventana
LTexture *mTexture; // Textura donde estan todos los dibujos del sprite
SDL_Rect mSpriteClip; // Rectangulo de origen de la textura que se dibujará en pantalla
bool mEnabled; // Indica si el sprite esta habilitado
public: public:
// Constructor // Constructor
Sprite(); Sprite(int x = 0, int y = 0, int w = 0, int h = 0, LTexture *texture = nullptr, SDL_Renderer *renderer = nullptr);
Sprite(SDL_Rect rect, LTexture *texture, SDL_Renderer *renderer);
// Destructor // Destructor
~Sprite(); ~Sprite();
// Inicializador
//void init(LTexture *texture, SDL_Renderer *renderer, int x = 0, int y = 0, Uint16 w = 0, Uint16 h = 0);
void init(LTexture *texture, SDL_Renderer *renderer, SDL_Rect rect = {0, 0, 0, 0});
// Muestra el sprite por pantalla // Muestra el sprite por pantalla
void render(); void render();
@@ -29,10 +38,10 @@ public:
int getPosY(); int getPosY();
// Obten el valor de la variable // Obten el valor de la variable
Uint16 getWidth(); int getWidth();
// Obten el valor de la variable // Obten el valor de la variable
Uint16 getHeight(); int getHeight();
// Establece el valor de la variable // Establece el valor de la variable
void setPosX(int x); void setPosX(int x);
@@ -41,18 +50,20 @@ public:
void setPosY(int y); void setPosY(int y);
// Establece el valor de la variable // Establece el valor de la variable
void setWidth(Uint16 w); void setWidth(int w);
// Establece el valor de la variable // Establece el valor de la variable
void setHeight(Uint16 h); void setHeight(int h);
// Obten el valor de la variable // Obten el valor de la variable
SDL_Rect getSpriteClip(); SDL_Rect getSpriteClip();
// Establece el valor de la variable // Establece el valor de la variable
void setSpriteClip(int x, int y, Uint16 w, Uint16 h);
void setSpriteClip(SDL_Rect rect); void setSpriteClip(SDL_Rect rect);
// Establece el valor de la variable
void setSpriteClip(int x, int y, int w, int h);
// Obten el valor de la variable // Obten el valor de la variable
LTexture *getTexture(); LTexture *getTexture();
@@ -62,16 +73,14 @@ public:
// Establece el valor de la variable // Establece el valor de la variable
void setRenderer(SDL_Renderer *renderer); void setRenderer(SDL_Renderer *renderer);
protected: // Establece el valor de la variable
int mPosX; // Posición X donde dibujar el sprite void setEnabled(bool value);
int mPosY; // Posición Y donde dibujar el sprite
Uint16 mWidth; // Ancho del sprite // Comprueba si el objeto está habilitado
Uint16 mHeight; // Alto del sprite bool isEnabled();
SDL_Renderer *mRenderer; // Puntero al renderizador // Devuelve el rectangulo donde está el sprite
LTexture *mTexture; // Textura donde estan todos los dibujos del sprite SDL_Rect getRect();
SDL_Rect mSpriteClip; // Rectangulo que apunta a la posición de la textura donde está el grafico que se dibujará en pantalla
}; };
#endif #endif

View File

@@ -1,84 +1,65 @@
#include "const.h"
#include "text.h" #include "text.h"
#include <iostream>
#include <fstream>
Text::Text() // Constructor
Text::Text(std::string file, LTexture *texture, SDL_Renderer *renderer)
{ {
mSprite = new Sprite(); SDL_Rect rect = {0,0,0,0};
init(nullptr, nullptr, 0); mSprite = new Sprite(rect, texture, renderer);
mSprite->setTexture(texture);
mSprite->setRenderer(renderer);
mFile = file;
init();
} }
// Destructor
Text::~Text() Text::~Text()
{ {
delete mSprite; delete mSprite;
mSprite = nullptr;
} }
void Text::init(LTexture *texture, SDL_Renderer *renderer, Uint8 height) // Inicializador
void Text::init()
{ {
// Inicializa variables
mHeight = height;
// Inicia los valores del sprite que dibuja las letras
mSprite->setWidth(mHeight);
mSprite->setHeight(mHeight);
mSprite->setPosX(0);
mSprite->setPosY(0);
mSprite->setTexture(texture);
mSprite->setRenderer(renderer);
mSprite->setSpriteClip(0, 0, mSprite->getWidth(), mSprite->getHeight());
// Cadena con los caracteres ascii que se van a inicializar
std::string text = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ{\\[]]^_`abcdefghijklmnopqrstuvwxyz";
// Inicializa a cero el vector con las coordenadas // Inicializa a cero el vector con las coordenadas
for (Uint8 i = 0; i < 255; i++) for (int i = 0; i < 128; i++)
{ {
mOffset[i].x = 0; mOffset[i].x = 0;
mOffset[i].y = 0; mOffset[i].y = 0;
mOffset[i].w = 0; mOffset[i].w = 0;
} }
// Carga los offsets desde el fichero
initOffsetFromFile();
// Inicia los valores del sprite que dibuja las letras
mSprite->setWidth(mBoxWidth);
mSprite->setHeight(mBoxHeight);
mSprite->setPosX(0);
mSprite->setPosY(0);
mSprite->setSpriteClip(0, 0, mSprite->getWidth(), mSprite->getHeight());
// Establece las coordenadas para cada caracter ascii de la cadena y su ancho // Establece las coordenadas para cada caracter ascii de la cadena y su ancho
for (Uint8 i = 0; i < text.length(); ++i) for (int i = 32; i < 128; i++)
{ {
mOffset[int(text[i])].x = ((int(text[i]) - 32) % 15) * mSprite->getWidth(); mOffset[i].x = ((i - 32) % 15) * mBoxWidth;
mOffset[int(text[i])].y = ((int(text[i]) - 32) / 15) * mSprite->getHeight(); mOffset[i].y = ((i - 32) / 15) * mBoxHeight;
mOffset[int(text[i])].w = mHeight;
mOffset[int(text[i])].h = mHeight;
} }
} }
void Text::write(int x, int y, std::string text, Sint8 kerning, Uint8 lenght) // Escribe texto en pantalla
void Text::write(int x, int y, std::string text, int kerning, int lenght)
{ {
Uint16 shift = 0; Uint16 shift = 0;
std::string text2;
if (lenght > 0) if (lenght == -1)
text2 = text.substr(0, lenght); lenght = text.length();
else
text2 = text;
for (Uint8 i = 0; i < text2.length(); ++i) for (int i = 0; i < lenght; ++i)
{
mSprite->setSpriteClip(mOffset[int(text2[i])].x, mOffset[int(text2[i])].y, mSprite->getWidth(), mSprite->getHeight());
mSprite->setPosX(x + shift);
mSprite->setPosY(y);
mSprite->render();
shift += (mOffset[int(text2[i])].w + kerning);
}
}
void Text::writeColored(int x, int y, std::string text, Uint8 R, Uint8 G, Uint8 B)
{
mSprite->getTexture()->setColor(R, G, B);
write(x, y, text);
mSprite->getTexture()->setColor(255, 255, 255);
}
void Text::writeCentered(int x, int y, std::string text, int kerning)
{
// Uint16 lenght = Text::lenght(text, kerning);
x = x - (Text::lenght(text, kerning) / 2);
Uint16 shift = 0;
for (Uint8 i = 0; i < text.length(); ++i)
{ {
mSprite->setSpriteClip(mOffset[int(text[i])].x, mOffset[int(text[i])].y, mSprite->getWidth(), mSprite->getHeight()); mSprite->setSpriteClip(mOffset[int(text[i])].x, mOffset[int(text[i])].y, mSprite->getWidth(), mSprite->getHeight());
mSprite->setPosX(x + shift); mSprite->setPosX(x + shift);
@@ -88,22 +69,110 @@ void Text::writeCentered(int x, int y, std::string text, int kerning)
} }
} }
// Escribe el texto con colores
void Text::writeColored(int x, int y, std::string text, color_t color, int kerning, int lenght)
{
mSprite->getTexture()->setColor(color.r, color.g, color.b);
write(x, y, text, kerning, lenght);
mSprite->getTexture()->setColor(255, 255, 255);
}
// Escribe el texto con sombra
void Text::writeShadowed(int x, int y, std::string text, color_t color, Uint8 shadowDistance, int kerning, int lenght)
{
mSprite->getTexture()->setColor(color.r, color.g, color.b);
write(x + shadowDistance, y + shadowDistance, text, kerning, lenght);
mSprite->getTexture()->setColor(255, 255, 255);
write(x, y, text, kerning, lenght);
}
// Escribe el texto centrado en un punto x
void Text::writeCentered(int x, int y, std::string text, int kerning, int lenght)
{
x -= (Text::lenght(text, kerning) / 2);
write(x, y, text, kerning, lenght);
}
// Escribe texto con extras
void Text::writeDX(Uint8 flags, int x, int y, std::string text, int kerning, color_t textColor, Uint8 shadowDistance, color_t shadowColor, int lenght)
{
const bool centered = ((flags & TXT_CENTER) == TXT_CENTER);
const bool shadowed = ((flags & TXT_SHADOW) == TXT_SHADOW);
const bool colored = ((flags & TXT_COLOR) == TXT_COLOR);
const bool stroked = ((flags & TXT_STROKE) == TXT_STROKE);
if (centered)
x -= (Text::lenght(text, kerning) / 2);
if (shadowed)
writeColored(x + shadowDistance, y + shadowDistance, text, shadowColor, kerning, lenght);
if (stroked)
{
writeColored(x + shadowDistance, y + shadowDistance, text, shadowColor, kerning, lenght);
writeColored(x - shadowDistance, y + shadowDistance, text, shadowColor, kerning, lenght);
writeColored(x + shadowDistance, y - shadowDistance, text, shadowColor, kerning, lenght);
writeColored(x - shadowDistance, y - shadowDistance, text, shadowColor, kerning, lenght);
writeColored(x, y + shadowDistance, text, shadowColor, kerning, lenght);
writeColored(x, y - shadowDistance, text, shadowColor, kerning, lenght);
writeColored(x + shadowDistance, y, text, shadowColor, kerning, lenght);
writeColored(x - shadowDistance, y, text, shadowColor, kerning, lenght);
}
if (colored)
writeColored(x, y, text, textColor, kerning, lenght);
else
write(x, y, text, kerning, lenght);
}
// Obtiene la longitud en pixels de una cadena
Uint16 Text::lenght(std::string text, int kerning) Uint16 Text::lenght(std::string text, int kerning)
{ {
Uint16 shift = 0; Uint16 shift = 0;
for (Uint8 i = 0; i < text.length(); ++i)
{ for (int i = 0; i < (int)text.length(); ++i)
shift += (mOffset[int(text[i])].w + kerning); shift += (mOffset[int(text[i])].w + kerning);
}
return shift; return shift;
} }
Uint8 Text::getHeight() // Inicializa el vector de offsets desde un fichero
void Text::initOffsetFromFile()
{ {
return mHeight; std::ifstream rfile(mFile);
if (rfile.is_open() && rfile.good())
{
std::string buffer;
//printf("Reading %s file\n", mFile.c_str());
// Lee los dos primeros valores del fichero
std::getline(rfile, buffer);
std::getline(rfile, buffer);
mBoxWidth = std::stoi(buffer);
std::getline(rfile, buffer);
std::getline(rfile, buffer);
mBoxHeight = std::stoi(buffer);
// lee el resto de datos del fichero
int index = 32;
int line_read = 0;
while (std::getline(rfile, buffer))
{
// Almacena solo las lineas impares
if (line_read % 2 == 1)
mOffset[index++].w = std::stoi(buffer);
// Limpia el buffer
buffer.clear();
line_read++;
};
}
} }
void Text::setHeight(Uint8 size) // Devuelve el valor de la variable
Uint8 Text::getCharacterWidth()
{ {
mHeight = size; return mBoxWidth;
} }

View File

@@ -1,58 +1,66 @@
#pragma once #pragma once
#include "sprite.h" #include "sprite.h"
#include "utils.h"
#ifndef TEXT_H #ifndef TEXT_H
#define TEXT_H #define TEXT_H
#define TXT_COLOR 1
#define TXT_SHADOW 2
#define TXT_CENTER 4
#define TXT_STROKE 8
// Clase texto. Pinta texto en pantalla a partir de un bitmap // Clase texto. Pinta texto en pantalla a partir de un bitmap
class Text class Text
{ {
private:
Sprite *mSprite; // Objeto con los graficos para el texto
struct Offset
{
int x;
int y;
Uint8 w;
};
Offset mOffset[128]; // Vector con las posiciones y ancho de cada letra
Uint8 mBoxWidth; // Anchura de la caja de cada caracter en el png
Uint8 mBoxHeight; // Altura de la caja de cada caracter en el png
std::string mFile; // Fichero con los descriptores de la fuente
// Inicializa el vector de offsets desde un fichero
void initOffsetFromFile();
public: public:
// Constructor // Constructor
Text(); Text(std::string file, LTexture *texture, SDL_Renderer *renderer);
// Destructor // Destructor
~Text(); ~Text();
// Inicializador // Inicializador
void init(LTexture *texture, SDL_Renderer *renderer, Uint8 height); void init();
/** // Escribe el texto en pantalla
* \brief Escribe texto en pantalla void write(int x, int y, std::string text, int kerning = 1, int lenght = -1);
*
* \param x Posición en el eje X donde empezar a escribir el texto
* \param y Posición en el eje Y donde empezar a escribir el texto
* \param string Texto para escribir
* \param kerning Espacio de separación entre letras
* \param lenght Longitud de la cadena de texto a escribir
*/
void write(int x, int y, std::string text, Sint8 kerning = 0, Uint8 lenght = 0);
// Escribe el texto con colores // Escribe el texto con colores
void writeColored(int x, int y, std::string text, Uint8 R, Uint8 G, Uint8 B); void writeColored(int x, int y, std::string text, color_t color, int kerning = 1, int lenght = -1);
// Escribe el texto con sombra
void writeShadowed(int x, int y, std::string text, color_t color, Uint8 shadowDistance = 1, int kerning = 1, int lenght = -1);
// Escribe el texto centrado en un punto x y con kerning // Escribe el texto centrado en un punto x y con kerning
void writeCentered(int x, int y, std::string text, int kerning); void writeCentered(int x, int y, std::string text, int kerning = 1, int lenght = -1);
// Escribe texto con extras
void writeDX(Uint8 flags, int x, int y, std::string text, int kerning = 1, color_t textColor = {255, 255, 255}, Uint8 shadowDistance = 1, color_t shadowColor = {0, 0, 0}, int lenght = -1);
// Obtiene la longitud en pixels de una cadena // Obtiene la longitud en pixels de una cadena
Uint16 lenght(std::string text, int kerning); Uint16 lenght(std::string text, int kerning = 1);
// Obtiene el valor de la variable // Devuelve el valor de la variable
Uint8 getType(); Uint8 getCharacterWidth();
// Establece el valor de la variable
void setType(Uint8 type);
// Obtiene el valor de la variable
Uint8 getHeight();
// Establece el valor de la variable
void setHeight(Uint8 size);
private:
Sprite *mSprite; // Objeto con los graficos para el texto
SDL_Rect mOffset[255]; // Vector con las posiciones y ancho de cada letra
Uint8 mHeight; // Altura del texto
}; };
#endif #endif

View File

@@ -1,135 +0,0 @@
#include "const.h"
#include "text2.h"
Text2::Text2()
{
init(nullptr, nullptr, 0);
}
Text2::~Text2()
{
init(nullptr, nullptr, 0);
}
void Text2::init(LTexture *texture, SDL_Renderer *renderer, Uint8 height)
{
Text::init(texture, renderer, height);
mPosX = 0;
mPosY = 0;
mKerning = 0;
mCaption = "";
mSpeed = 0;
mCounter = 0;
mIndex = 0;
mLenght = 0;
mCompleted = false;
mEnabled = false;
mEnabledCounter = 0;
mId = -1;
}
void Text2::setPosX(int value)
{
mPosX = value;
}
void Text2::setPosY(int value)
{
mPosY = value;
}
void Text2::setKerning(int value)
{
mKerning = value;
}
void Text2::setCaption(std::string text)
{
mCaption = text;
mLenght = text.length();
}
void Text2::setSpeed(Uint16 value)
{
mSpeed = value;
mCounter = value;
}
void Text2::setEnabled(bool value)
{
mEnabled = value;
}
bool Text2::IsEnabled()
{
return mEnabled;
}
void Text2::setEnabledCounter(Uint16 value)
{
mEnabledCounter = value;
}
Uint16 Text2::getEnabledCounter()
{
return mEnabledCounter;
}
void Text2::update()
{
if (mEnabled)
{
if (mCompleted == false)
{
if (mCounter > 0)
{
--mCounter;
}
if (mCounter == 0)
{
++mIndex;
mCounter = mSpeed;
}
if (mIndex == mLenght)
{
mCompleted = true;
}
}
if (mCompleted)
{
if (mEnabledCounter > 0)
{
--mEnabledCounter;
}
else if (mEnabledCounter == 0)
{
if (mId < 0)
{
mEnabled = false;
}
else
{
//mIntroEvents[mId] = EVENT_COMPLETED;
}
}
}
}
}
void Text2::render()
{
if (mEnabled)
{
Text::write(mPosX, mPosY, mCaption, mKerning, mIndex);
}
}
void Text2::center(int x)
{
setPosX(x - (lenght(mCaption, mKerning) / 2));
}
void Text2::setId(int id)
{
mId = id;
}

View File

@@ -1,75 +0,0 @@
#pragma once
#include "sprite.h"
#include "text.h"
#ifndef TEXT2_H
#define TEXT2_H
// Clase texto. Pinta texto en pantalla a partir de un bitmap
class Text2 : public Text
{
public:
// Constructor
Text2();
// Destructor
~Text2();
// Inicializador
void init(LTexture *texture, SDL_Renderer *renderer, Uint8 height);
// Establece el valor de la variable
void setPosX(int value);
// Establece el valor de la variable
void setPosY(int value);
// Establece el valor de la variable
void setKerning(int value);
// Establece el valor de la variable
void setCaption(std::string text);
// Establece el valor de la variable
void setSpeed(Uint16 value);
// Establece el valor de la variable
void setEnabled(bool value);
// Obtiene el valor de la variable
bool IsEnabled();
// Establece el valor de la variable
void setEnabledCounter(Uint16 value);
// Obtiene el valor de la variable
Uint16 getEnabledCounter();
// Actualiza todas las variables del objeto
void update();
// Dibuja el objeto en pantalla
void render();
// Centra la cadena de texto respecto a un punto X
void center(int x);
// Establece el valor de la variable mId
void setId(int id);
private:
bool mCompleted; // Indica si se ha escrito todo el texto
bool mEnabled; // Indica si el objeto está habilitado
int mId; // Identificador
int mKerning; // Espacio entre caracteres
int mPosX; // Posicion en el eje X donde empezar a escribir el texto
int mPosY; // Posicion en el eje Y donde empezar a escribir el texto
std::string mCaption; // Texto para escribir
Uint16 mCounter; // Temporizador de escritura para cada caracter
Uint16 mEnabledCounter; // Contador para deshabilitar el objeto
Uint16 mIndex; // Indice a la posición dentro de la cadena de texto que se está escribiendo
Uint16 mLenght; // Longitud de la cadena a escribir
Uint16 mSpeed; // Velocidad de escritura
};
#endif

View File

@@ -9,7 +9,7 @@ double distanceSquared(int x1, int y1, int x2, int y2)
} }
// Detector de colisiones entre dos circulos // Detector de colisiones entre dos circulos
bool checkCollision(Circle &a, Circle &b) bool checkCollision(circle_t &a, circle_t &b)
{ {
// Calcula el radio total al cuadrado // Calcula el radio total al cuadrado
int totalRadiusSquared = a.r + b.r; int totalRadiusSquared = a.r + b.r;
@@ -27,7 +27,7 @@ bool checkCollision(Circle &a, Circle &b)
} }
// Detector de colisiones entre un circulo y un rectangulo // Detector de colisiones entre un circulo y un rectangulo
bool checkCollision(Circle &a, SDL_Rect &b) bool checkCollision(circle_t &a, SDL_Rect &b)
{ {
// Closest point on collision box // Closest point on collision box
int cX, cY; int cX, cY;
@@ -60,10 +60,10 @@ bool checkCollision(Circle &a, SDL_Rect &b)
cY = a.y; cY = a.y;
} }
//If the closest point is inside the circle // If the closest point is inside the circle_t
if (distanceSquared(a.x, a.y, cX, cY) < a.r * a.r) if (distanceSquared(a.x, a.y, cX, cY) < a.r * a.r)
{ {
//This box and the circle have collided // This box and the circle_t have collided
return true; return true;
} }
@@ -110,3 +110,86 @@ bool checkCollision(SDL_Rect &a, SDL_Rect &b)
// If none of the sides from A are outside B // If none of the sides from A are outside B
return true; return true;
} }
// Carga un archivo de imagen en una textura
bool loadTextureFromFile(LTexture *texture, std::string path, SDL_Renderer *renderer)
{
bool success = true;
if (!texture->loadFromFile(path, renderer))
{
printf("Failed to load %s texture!\n", path.c_str());
success = false;
}
return success;
}
// Devuelve un color_t a partir de un string
color_t stringToColor(std::string str)
{
color_t color = {0x00, 0x00, 0x00};
if (str == "black")
{
color = {0x00, 0x00, 0x00};
}
else if (str == "light_black")
{
color = {0x3C, 0x35, 0x1F};
}
else if (str == "blue")
{
color = {0x31, 0x33, 0x90};
}
else if (str == "light_blue")
{
color = {0x15, 0x59, 0xDB};
}
else if (str == "red")
{
color = {0xA7, 0x32, 0x11};
}
else if (str == "light_red")
{
color = {0xD8, 0x55, 0x25};
}
else if (str == "purple")
{
color = {0xA1, 0x55, 0x89};
}
else if (str == "light_purple")
{
color = {0xCD, 0x7A, 0x50};
}
else if (str == "green")
{
color = {0x62, 0x9A, 0x31};
}
else if (str == "light_green")
{
color = {0x9C, 0xD3, 0x3C};
}
else if (str == "cyan")
{
color = {0x28, 0xA4, 0xCB};
}
else if (str == "light_cyan")
{
color = {0x65, 0xDC, 0xD6};
}
else if (str == "yellow")
{
color = {0xE8, 0xBC, 0x50};
}
else if (str == "light_yellow")
{
color = {0xF1, 0xE7, 0x82};
}
else if (str == "white")
{
color = {0xBF, 0xBF, 0xBD};
}
else if (str == "light_white")
{
color = {0xF2, 0xF1, 0xED};
}
return color;
}

View File

@@ -1,54 +1,82 @@
#pragma once #pragma once
#include "ifdefs.h" #include <SDL2/SDL.h>
#include "ltexture.h"
#include <string>
#ifndef UTILS_H #ifndef UTILS_H
#define UTILS_H #define UTILS_H
#define FILTER_NEAREST 0
#define FILTER_LINEAL 1
// Secciones del programa
#define SECTION_PROG_LOGO 0
#define SECTION_PROG_INTRO 1
#define SECTION_PROG_TITLE 2
#define SECTION_PROG_GAME 3
#define SECTION_PROG_QUIT 4
// Subsecciones
#define SUBSECTION_GAME_PLAY 0
#define SUBSECTION_GAME_PAUSE 1
#define SUBSECTION_GAME_GAMEOVER 2
#define SUBSECTION_TITLE_1 3
#define SUBSECTION_TITLE_2 4
#define SUBSECTION_TITLE_3 5
#define SUBSECTION_TITLE_INSTRUCTIONS 6
// Estructura para definir un circulo // Estructura para definir un circulo
struct Circle struct circle_t
{
int x;
int y;
int r;
};
// Estructura para definir un color
struct color_t
{ {
Uint16 x;
Uint16 y;
Uint8 r; Uint8 r;
Uint8 g;
Uint8 b;
}; };
// Estructura para definir todas las entradas que aceptará el programa // Estructura para saber la seccion y subseccion del programa
struct Input struct section_t
{ {
Uint8 up; Uint8 name;
Uint8 down; Uint8 subsection;
Uint8 left;
Uint8 right;
Uint8 escape;
Uint8 pause;
Uint8 fire;
Uint8 fireLeft;
Uint8 fireRight;
Uint8 accept;
Uint8 cancel;
}; };
// Estructura para mapear el teclado usado en la demo // Estructura con todas las opciones de configuración del programa
struct DemoKeys struct options_t
{ {
Uint8 left; Uint32 fullScreenMode; // Contiene el valor del modo de pantalla completa
Uint8 right; int windowSize; // Contiene el valor por el que se multiplica el tamaño de la ventana
Uint8 noInput; Uint32 filter; // Filtro usado para el escalado de la imagen
Uint8 fire; bool vSync; // Indica si se quiere usar vsync o no
Uint8 fireLeft; int screenWidth; // Ancho de la pantalla/ventana
Uint8 fireRight; int screenHeight; // Alto de la pantalla/ventana
bool integerScale; // Indica si el escalado de la imagen ha de ser entero en el modo a pantalla completa
bool keepAspect; // Indica si se ha de mantener la relación de aspecto al poner el modo a pantalla completa
}; };
// Calcula el cuadrado de la distancia entre dos puntos // Calcula el cuadrado de la distancia entre dos puntos
double distanceSquared(int x1, int y1, int x2, int y2); double distanceSquared(int x1, int y1, int x2, int y2);
// Detector de colisiones entre dos circulos // Detector de colisiones entre dos circulos
bool checkCollision(Circle &a, Circle &b); bool checkCollision(circle_t &a, circle_t &b);
// Detector de colisiones entre un circulo y un rectangulo // Detector de colisiones entre un circulo y un rectangulo
bool checkCollision(Circle &a, SDL_Rect &b); bool checkCollision(circle_t &a, SDL_Rect &b);
// Detector de colisiones entre un dos rectangulos // Detector de colisiones entre un dos rectangulos
bool checkCollision(SDL_Rect a, SDL_Rect b); bool checkCollision(SDL_Rect &a, SDL_Rect &b);
// Carga un archivo de imagen en una textura
bool loadTextureFromFile(LTexture *texture, std::string path, SDL_Renderer *renderer);
// Devuelve un color_t a partir de un string
color_t stringToColor(std::string str);
#endif #endif

File diff suppressed because it is too large Load Diff

View File

@@ -1,182 +0,0 @@
#pragma once
#ifndef VOLCANO_H
#define VOLCANO_H
#include "ifdefs.h"
#include "jail_audio.h"
#include "const.h"
#include "ltexture.h"
#include "director.h"
/*
struct Tanimation
{
bool loops; // Salta a true cuando se reinicia la animación
Uint16 timer; // Temporizador de la animación
Uint8 frame[50]; // Vector con ....
Uint8 index; // Frame actual
Uint8 num_frames; // Cantidad de frames de la animación
Uint8 speed; // Velocidad de la animación
};
struct Tprogram
{
bool debug;
bool filter;
bool music_enabled; //
LTexture *sprite;
SDL_Rect dst_rect;
SDL_Rect src_rect;
Uint8 section;
};
struct Tgame
{
bool enabled;
JA_Sound sound_drop_enemy;
JA_Sound sound_drop_splat;
JA_Music music;
Uint8 zone; // Zona en la que estamos
};
struct Tmenu
{
bool enabled;
int frame;
int timer;
LTexture *sprite_animation;
LTexture *sprite;
JA_Sound sound_logo;
JA_Sound sound_start;
JA_Music music;
SDL_Rect dst_rect_animation;
SDL_Rect dst_rect_fondo;
SDL_Rect dst_rect_logo_zoom;
SDL_Rect dst_rect_logo;
SDL_Rect dst_rect_text;
SDL_Rect dst_rect_text2;
SDL_Rect src_rect_animation;
SDL_Rect src_rect_fondo;
SDL_Rect src_rect_logo;
SDL_Rect src_rect_text;
SDL_Rect src_rect_text2;
Tanimation animation[2];
Uint8 section;
};
struct Tactor
{
bool enabled;
Uint8 frame;
int speed_x;
int speed_y; // Velocidad = cantidad de pixeles a desplazarse
int timer;
SDL_Rect dst_rect;
SDL_Rect src_rect;
Uint8 direction; // Sentido del desplazamiento
Uint8 id;
Uint8 kind; // Tipo de actor: enemigo, moneda, llave, plataforma ...
Uint8 parent; // Indice al padre del actor
};
struct Tanimated_tile
{
bool enabled;
Uint8 index;
int x;
int y;
Uint8 frame;
};
struct Thud
{
LTexture *sprite;
SDL_Rect src_rect;
SDL_Rect dst_rect;
SDL_Rect num_src_rect;
SDL_Rect num_dst_rect;
SDL_Rect bignum_src_rect;
SDL_Rect bignum_dst_rect;
};
struct Tmap
{
LTexture *sprite_tile;
LTexture *sprite_actor;
LTexture *background;
SDL_Rect src_rect;
SDL_Rect dst_rect;
Uint8 *tile;
Uint8 *actor;
Uint8 w;
Uint8 h;
Uint8 room;
};
bool fullscreen;
bool quit;
const Uint8 *keys;
SDL_Event *event;
SDL_Renderer *renderer;
SDL_Window *window;
std::string executablePath;
Tactor actor[MAX_ACTORS];
Tanimated_tile animated_tile[MAX_ANIMATED_TILES];
Tgame game;
Thud hud;
Tmap map;
Tmenu menu;
Tprogram prog;
Uint32 delta_time;
bool CheckZoneChange(int room);
bool loadTextureFromFile(LTexture *texture, std::string path, SDL_Renderer *renderer);
bool OnFloor();
Uint8 GetActor(Uint8 x, Uint8 y);
Uint8 GetTile(Uint8 x, Uint8 y);
Uint8 ReadMapTile(Uint8 x, Uint8 y);
void allocatePointers();
void Animate(Tanimation &a, SDL_Rect &s);
void AnimateIntroMenu(Tanimation &a, SDL_Rect &s);
void ApplyGravity();
void CheckPlayerCollisionWithActors();
void CheckPlayerCollisionWithActors();
void CheckPlayerCollisionWithMap();
void CloseMusic(JA_Music *music);
void ClosePicture(LTexture *picture);
void CloseSound(JA_Sound *sound);
void CreateActor(Tactor &a, Uint8 kind, Uint8 id, Sint16 dstx, Sint16 dsty, Sint16 dstw, Sint16 dsth, Sint16 srcx, Sint16 srcy, Sint16 sx, Sint16 sy, Sint16 timer, Sint16 frame, Uint8 direction, Uint8 parent);
void DrawHud();
void DrawMap();
void DrawSprite(LTexture *sprite, SDL_Rect src_rect, SDL_Rect dst_rect);
void EndGame();
void EndHud();
void EndMap();
void EndMenu();
void EndPlayer();
void EndProgram();
void IniActors();
void IniAnimatedTiles();
void IniGame(Uint8 zone);
void IniHud();
void IniMap();
void IniMenu();
void IniPlayer();
void IniProgram();
void KillPlayer();
void LoadMap();
void LoadRoom(int num);
void MoveActors();
void MovePlayer(int direction);
void SetActor(Uint8 x, Uint8 y, Uint8 valor);
void setExecutablePath(std::string path);
void SetMapGFX(Uint8 zone);
void SetMapMusic(Uint8 zone);
void SetPlayerAnimation(Uint8 anim);
void SetPlayerDirection(int direction);
void SetProgSection(Uint8 section);
void SetZone(int room);
*/
#endif