Actualizado el fichero utils.h a una versión posterior
This commit is contained in:
107
source/utils.cpp
107
source/utils.cpp
@@ -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,12 +27,12 @@ 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;
|
||||||
|
|
||||||
//Find closest x offset
|
// Find closest x offset
|
||||||
if (a.x < b.x)
|
if (a.x < b.x)
|
||||||
{
|
{
|
||||||
cX = b.x;
|
cX = b.x;
|
||||||
@@ -46,7 +46,7 @@ bool checkCollision(Circle &a, SDL_Rect &b)
|
|||||||
cX = a.x;
|
cX = a.x;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Find closest y offset
|
// Find closest y offset
|
||||||
if (a.y < b.y)
|
if (a.y < b.y)
|
||||||
{
|
{
|
||||||
cY = b.y;
|
cY = b.y;
|
||||||
@@ -60,33 +60,33 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
//If the shapes have not collided
|
// If the shapes have not collided
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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)
|
||||||
{
|
{
|
||||||
//Calculate the sides of rect A
|
// Calculate the sides of rect A
|
||||||
int leftA = a.x;
|
int leftA = a.x;
|
||||||
int rightA = a.x + a.w;
|
int rightA = a.x + a.w;
|
||||||
int topA = a.y;
|
int topA = a.y;
|
||||||
int bottomA = a.y + a.h;
|
int bottomA = a.y + a.h;
|
||||||
|
|
||||||
//Calculate the sides of rect B
|
// Calculate the sides of rect B
|
||||||
int leftB = b.x;
|
int leftB = b.x;
|
||||||
int rightB = b.x + b.w;
|
int rightB = b.x + b.w;
|
||||||
int topB = b.y;
|
int topB = b.y;
|
||||||
int bottomB = b.y + b.h;
|
int bottomB = b.y + b.h;
|
||||||
|
|
||||||
//If any of the sides from A are outside of B
|
// If any of the sides from A are outside of B
|
||||||
if (bottomA <= topB)
|
if (bottomA <= topB)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
@@ -107,6 +107,89 @@ bool checkCollision(SDL_Rect &a, SDL_Rect &b)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//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;
|
||||||
|
}
|
||||||
@@ -1,35 +1,40 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "ifdefs.h"
|
#include "ifdefs.h"
|
||||||
|
#include "ltexture.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#ifndef UTILS_H
|
#ifndef UTILS_H
|
||||||
#define UTILS_H
|
#define UTILS_H
|
||||||
|
|
||||||
|
#define DIFFICULTY_EASY 0
|
||||||
|
#define DIFFICULTY_NORMAL 1
|
||||||
|
#define DIFFICULTY_HARD 2
|
||||||
|
|
||||||
// Estructura para definir un circulo
|
// Estructura para definir un circulo
|
||||||
struct Circle
|
struct circle_t
|
||||||
{
|
{
|
||||||
Uint16 x;
|
int x;
|
||||||
Uint16 y;
|
int y;
|
||||||
Uint8 r;
|
int r;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Estructura para definir todas las entradas que aceptará el programa
|
// Estructura para definir un color
|
||||||
struct Input
|
struct color_t
|
||||||
{
|
{
|
||||||
Uint8 up;
|
Uint8 r;
|
||||||
Uint8 down;
|
Uint8 g;
|
||||||
Uint8 left;
|
Uint8 b;
|
||||||
Uint8 right;
|
};
|
||||||
Uint8 escape;
|
|
||||||
Uint8 pause;
|
// Estructura para saber la seccion y subseccion del programa
|
||||||
Uint8 fire;
|
struct section_t
|
||||||
Uint8 fireLeft;
|
{
|
||||||
Uint8 fireRight;
|
Uint8 name;
|
||||||
Uint8 accept;
|
Uint8 subsection;
|
||||||
Uint8 cancel;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Estructura para mapear el teclado usado en la demo
|
// Estructura para mapear el teclado usado en la demo
|
||||||
struct DemoKeys
|
struct demoKeys_t
|
||||||
{
|
{
|
||||||
Uint8 left;
|
Uint8 left;
|
||||||
Uint8 right;
|
Uint8 right;
|
||||||
@@ -39,16 +44,42 @@ struct DemoKeys
|
|||||||
Uint8 fireRight;
|
Uint8 fireRight;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Estructura para albergar métodos de control
|
||||||
|
struct input_t
|
||||||
|
{
|
||||||
|
int id; // Identificador en el vector de mandos
|
||||||
|
std::string name; // Nombre del dispositivo
|
||||||
|
Uint8 deviceType; // Tipo de dispositivo (teclado o mando)
|
||||||
|
};
|
||||||
|
|
||||||
|
// Estructura con todas las opciones de configuración del programa
|
||||||
|
struct options_t
|
||||||
|
{
|
||||||
|
Uint8 difficulty; // Dificultad del juego
|
||||||
|
input_t input[2]; // Modo de control (teclado o mando)
|
||||||
|
Uint8 language; // Idioma usado en el juego
|
||||||
|
Uint32 fullScreenMode; // Contiene el valor del modo de pantalla completa
|
||||||
|
Uint8 windowSize; // Contiene el valor del tamaño de la ventana
|
||||||
|
Uint32 filter; // Filtro usado para el escalado de la imagen
|
||||||
|
bool vSync; // Indica si se quiere usar vsync o no
|
||||||
|
};
|
||||||
|
|
||||||
// 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
|
||||||
Reference in New Issue
Block a user