diff --git a/Makefile b/Makefile index fcd73fd..5a9c57a 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,17 @@ executable = coffee_crisis +source = source/*.cpp source/common/*.cpp windows: @echo off if not exist bin\ (mkdir bin) - g++ -std=c++11 -Wall -O2 source/*.cpp -lmingw32 -lSDL2main -lSDL2 -ffunction-sections -fdata-sections -Wl,--gc-sections -static-libstdc++ -Wl,-subsystem,windows -o bin/$(executable).exe + g++ $(source) -std=c++11 -Wall -O2 -lmingw32 -lSDL2main -lSDL2 -ffunction-sections -fdata-sections -Wl,--gc-sections -static-libstdc++ -Wl,-subsystem,windows -o bin/$(executable).exe strip -s -R .comment -R .gnu.version bin/$(executable).exe --strip-unneeded macos: mkdir -p bin - g++ source/*.cpp -std=c++11 -Wall -O2 -lSDL2 -ffunction-sections -fdata-sections -o bin/$(executable)_macos + g++ $(source) -std=c++11 -Wall -O2 -lSDL2 -ffunction-sections -fdata-sections -o bin/$(executable)_macos linux: mkdir -p bin - g++ source/*.cpp -std=c++11 -Wall -Os -lSDL2 -ffunction-sections -fdata-sections -Wl,--gc-sections -o bin/$(executable)_linux + g++ $(source) -std=c++11 -Wall -Os -lSDL2 -ffunction-sections -fdata-sections -Wl,--gc-sections -o bin/$(executable)_linux strip -s -R .comment -R .gnu.version bin/$(executable)_linux --strip-unneeded opendingux: mkdir -p bin diff --git a/source/balloon.h b/source/balloon.h index c4a264e..11f5807 100644 --- a/source/balloon.h +++ b/source/balloon.h @@ -1,8 +1,8 @@ #pragma once #include -#include "utils.h" -#include "animatedsprite.h" +#include "common/utils.h" +#include "common/animatedsprite.h" #include #include @@ -86,7 +86,7 @@ private: std::vector h; // Vector con los valores de zoom para el alto del globo }; - // Objetos + // Objetos y punteros AnimatedSprite *sprite; // Sprite del objeto globo // Variables diff --git a/source/bullet.h b/source/bullet.h index e9508ff..9b4d825 100644 --- a/source/bullet.h +++ b/source/bullet.h @@ -1,8 +1,8 @@ #pragma once #include -#include "utils.h" -#include "sprite.h" +#include "common/utils.h" +#include "common/sprite.h" #ifndef BULLET_H #define BULLET_H @@ -20,7 +20,7 @@ class Bullet { private: - // Objetos + // Objetos y punteros Sprite *sprite; // Sprite con los graficos y métodos de pintado // Variables diff --git a/source/animatedsprite.cpp b/source/common/animatedsprite.cpp similarity index 100% rename from source/animatedsprite.cpp rename to source/common/animatedsprite.cpp diff --git a/source/animatedsprite.h b/source/common/animatedsprite.h similarity index 97% rename from source/animatedsprite.h rename to source/common/animatedsprite.h index bc0ca50..1b6031f 100644 --- a/source/animatedsprite.h +++ b/source/common/animatedsprite.h @@ -1,92 +1,92 @@ -#pragma once - -#include -#include "movingsprite.h" -#include -#include -#include -#include -#include - -#ifndef ANIMATEDSPRITE_H -#define ANIMATEDSPRITE_H - -// Clase AnimatedSprite -class AnimatedSprite : public MovingSprite -{ -private: - struct t_animation - { - std::string name; // Nombre de la animacion - std::vector frames; // Cada uno de los frames que componen la animación - int speed; // Velocidad de la animación - int loop; // Indica a que frame vuelve la animación al terminar. -1 para que no vuelva - bool completed; // Indica si ha finalizado la animación - int currentFrame; // Frame actual - int counter; // Contador para las animaciones - }; - std::vector animation; // Vector con las diferentes animaciones - int currentAnimation; // Animacion activa - -public: - // Constructor - AnimatedSprite(LTexture *texture = nullptr, SDL_Renderer *renderer = nullptr, std::string file = "", std::vector *buffer = nullptr); - - // Destructor - ~AnimatedSprite(); - - // Calcula el frame correspondiente a la animación actual - void animate(); - - // Establece el frame actual de la animación - void setCurrentFrame(int num); - - // Establece el valor del contador - void setAnimationCounter(std::string name, int num); - - // Establece la velocidad de una animación - void setAnimationSpeed(std::string name, int speed); - void setAnimationSpeed(int index, int speed); - - // Establece el frame al que vuelve la animación al finalizar - void setAnimationLoop(std::string name, int loop); - void setAnimationLoop(int index, int loop); - - // Establece el valor de la variable - void setAnimationCompleted(std::string name, bool value); - void setAnimationCompleted(int index, bool value); - - // Comprueba si ha terminado la animación - bool animationIsCompleted(); - - // Devuelve el rectangulo de una animación y frame concreto - SDL_Rect getAnimationClip(std::string name, Uint8 index); - SDL_Rect getAnimationClip(int indexA, Uint8 indexF); - - // Obtiene el indice de la animación a partir del nombre - int getIndex(std::string name); - - // Carga la animación desde un fichero - bool loadFromFile(std::string filePath); - - // Carga la animación desde un vector - bool loadFromVector(std::vector *source); - - // Establece la animacion actual - void setCurrentAnimation(std::string name = "default"); - void setCurrentAnimation(int index = 0); - - // Actualiza las variables del objeto - void update(); - - // OLD - Establece el rectangulo para un frame de una animación - void setAnimationFrames(Uint8 index_animation, Uint8 index_frame, int x, int y, int w, int h); - - // OLD - Establece el contador para todas las animaciones - void setAnimationCounter(int value); - - // Reinicia la animación - void resetAnimation(); -}; - +#pragma once + +#include +#include "movingsprite.h" +#include +#include +#include +#include +#include + +#ifndef ANIMATEDSPRITE_H +#define ANIMATEDSPRITE_H + +// Clase AnimatedSprite +class AnimatedSprite : public MovingSprite +{ +private: + struct t_animation + { + std::string name; // Nombre de la animacion + std::vector frames; // Cada uno de los frames que componen la animación + int speed; // Velocidad de la animación + int loop; // Indica a que frame vuelve la animación al terminar. -1 para que no vuelva + bool completed; // Indica si ha finalizado la animación + int currentFrame; // Frame actual + int counter; // Contador para las animaciones + }; + std::vector animation; // Vector con las diferentes animaciones + int currentAnimation; // Animacion activa + +public: + // Constructor + AnimatedSprite(LTexture *texture = nullptr, SDL_Renderer *renderer = nullptr, std::string file = "", std::vector *buffer = nullptr); + + // Destructor + ~AnimatedSprite(); + + // Calcula el frame correspondiente a la animación actual + void animate(); + + // Establece el frame actual de la animación + void setCurrentFrame(int num); + + // Establece el valor del contador + void setAnimationCounter(std::string name, int num); + + // Establece la velocidad de una animación + void setAnimationSpeed(std::string name, int speed); + void setAnimationSpeed(int index, int speed); + + // Establece el frame al que vuelve la animación al finalizar + void setAnimationLoop(std::string name, int loop); + void setAnimationLoop(int index, int loop); + + // Establece el valor de la variable + void setAnimationCompleted(std::string name, bool value); + void setAnimationCompleted(int index, bool value); + + // Comprueba si ha terminado la animación + bool animationIsCompleted(); + + // Devuelve el rectangulo de una animación y frame concreto + SDL_Rect getAnimationClip(std::string name, Uint8 index); + SDL_Rect getAnimationClip(int indexA, Uint8 indexF); + + // Obtiene el indice de la animación a partir del nombre + int getIndex(std::string name); + + // Carga la animación desde un fichero + bool loadFromFile(std::string filePath); + + // Carga la animación desde un vector + bool loadFromVector(std::vector *source); + + // Establece la animacion actual + void setCurrentAnimation(std::string name = "default"); + void setCurrentAnimation(int index = 0); + + // Actualiza las variables del objeto + void update(); + + // OLD - Establece el rectangulo para un frame de una animación + void setAnimationFrames(Uint8 index_animation, Uint8 index_frame, int x, int y, int w, int h); + + // OLD - Establece el contador para todas las animaciones + void setAnimationCounter(int value); + + // Reinicia la animación + void resetAnimation(); +}; + #endif \ No newline at end of file diff --git a/source/asset.cpp b/source/common/asset.cpp similarity index 100% rename from source/asset.cpp rename to source/common/asset.cpp diff --git a/source/asset.h b/source/common/asset.h similarity index 100% rename from source/asset.h rename to source/common/asset.h diff --git a/source/input.cpp b/source/common/input.cpp similarity index 100% rename from source/input.cpp rename to source/common/input.cpp diff --git a/source/input.h b/source/common/input.h similarity index 96% rename from source/input.h rename to source/common/input.h index a2eab87..f22b0af 100644 --- a/source/input.h +++ b/source/common/input.h @@ -1,90 +1,90 @@ -#pragma once - -#include -#include -#include - -#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 - }; - - struct GameControllerBindings_t - { - SDL_GameControllerButton button; // GameControllerButton asociado - bool active; // Indica si está activo - }; - - std::vector keyBindings; // Vector con las teclas asociadas a los inputs predefinidos - std::vector gameControllerBindings; // Vector con las teclas asociadas a los inputs predefinidos - std::vector connectedControllers; // Vector con todos los mandos conectados - std::vector controllerNames; // Vector con los nombres de los mandos - int numGamepads; // Numero de mandos conectados - std::string dbPath; // 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 almenos un input activo - bool checkAnyInput(int device, int index); - - // 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 +#pragma once + +#include +#include +#include + +#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 + }; + + struct GameControllerBindings_t + { + SDL_GameControllerButton button; // GameControllerButton asociado + bool active; // Indica si está activo + }; + + std::vector keyBindings; // Vector con las teclas asociadas a los inputs predefinidos + std::vector gameControllerBindings; // Vector con las teclas asociadas a los inputs predefinidos + std::vector connectedControllers; // Vector con todos los mandos conectados + std::vector controllerNames; // Vector con los nombres de los mandos + int numGamepads; // Numero de mandos conectados + std::string dbPath; // 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 almenos un input activo + bool checkAnyInput(int device, int index); + + // 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 diff --git a/source/jail_audio.cpp b/source/common/jail_audio.cpp similarity index 100% rename from source/jail_audio.cpp rename to source/common/jail_audio.cpp diff --git a/source/jail_audio.h b/source/common/jail_audio.h similarity index 100% rename from source/jail_audio.h rename to source/common/jail_audio.h diff --git a/source/ltexture.cpp b/source/common/ltexture.cpp similarity index 95% rename from source/ltexture.cpp rename to source/common/ltexture.cpp index 94a88fa..a68a153 100644 --- a/source/ltexture.cpp +++ b/source/common/ltexture.cpp @@ -1,191 +1,191 @@ - -#include "ltexture.h" -#define STB_IMAGE_IMPLEMENTATION -#include "stb_image.h" - -// Constructor -LTexture::LTexture(SDL_Renderer *renderer, std::string path) -{ - // Copia punteros - this->renderer = renderer; - this->path = path; - - // Inicializa - texture = nullptr; - width = 0; - height = 0; - - // Carga el fichero en la textura - if (path != "") - { - loadFromFile(path, renderer); - } -} - -// Destructor -LTexture::~LTexture() -{ - // Libera memoria - unload(); -} - -// Carga una imagen desde un fichero -bool LTexture::loadFromFile(std::string path, SDL_Renderer *renderer) -{ - const std::string filename = path.substr(path.find_last_of("\\/") + 1); - int req_format = STBI_rgb_alpha; - int width, height, orig_format; - unsigned char *data = stbi_load(path.c_str(), &width, &height, &orig_format, req_format); - if (data == nullptr) - { - SDL_Log("Loading image failed: %s", stbi_failure_reason()); - exit(1); - } - else - { - printf("Image loaded: %s\n", filename.c_str()); - } - - int depth, pitch; - Uint32 pixel_format; - if (req_format == STBI_rgb) - { - depth = 24; - pitch = 3 * width; // 3 bytes por pixel * pixels per linea - pixel_format = SDL_PIXELFORMAT_RGB24; - } - else - { // STBI_rgb_alpha (RGBA) - depth = 32; - pitch = 4 * width; - pixel_format = SDL_PIXELFORMAT_RGBA32; - } - - // Limpia - unload(); - - // La textura final - SDL_Texture *newTexture = nullptr; - - // Carga la imagen desde una ruta específica - SDL_Surface *loadedSurface = SDL_CreateRGBSurfaceWithFormatFrom((void *)data, width, height, depth, pitch, pixel_format); - if (loadedSurface == nullptr) - { - printf("Unable to load image %s!\n", path.c_str()); - } - else - { - // Crea la textura desde los pixels de la surface - newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface); - if (newTexture == nullptr) - { - printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError()); - } - else - { - // Obtiene las dimensiones de la imagen - this->width = loadedSurface->w; - this->height = loadedSurface->h; - } - - // Elimina la textura cargada - SDL_FreeSurface(loadedSurface); - } - - // Return success - texture = newTexture; - return texture != nullptr; -} - -// Crea una textura en blanco -bool LTexture::createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess access) -{ - // Crea una textura sin inicializar - texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, access, width, height); - if (texture == nullptr) - { - printf("Unable to create blank texture! SDL Error: %s\n", SDL_GetError()); - } - else - { - this->width = width; - this->height = height; - } - - return texture != nullptr; -} - -// Libera la memoria de la textura -void LTexture::unload() -{ - // Libera la textura si existe - if (texture != nullptr) - { - SDL_DestroyTexture(texture); - texture = nullptr; - width = 0; - height = 0; - } -} - -// Establece el color para la modulacion -void LTexture::setColor(Uint8 red, Uint8 green, Uint8 blue) -{ - SDL_SetTextureColorMod(texture, red, green, blue); -} - -// Establece el blending -void LTexture::setBlendMode(SDL_BlendMode blending) -{ - SDL_SetTextureBlendMode(texture, blending); -} - -// Establece el alpha para la modulación -void LTexture::setAlpha(Uint8 alpha) -{ - SDL_SetTextureAlphaMod(texture, alpha); -} - -// Renderiza la textura en un punto específico -void LTexture::render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip, float zoomW, float zoomH, double angle, SDL_Point *center, SDL_RendererFlip flip) -{ - // Establece el destino de renderizado en la pantalla - SDL_Rect renderQuad = {x, y, width, height}; - - // Obtiene las dimesiones del clip de renderizado - if (clip != nullptr) - { - renderQuad.w = clip->w; - renderQuad.h = clip->h; - } - - renderQuad.w = renderQuad.w * zoomW; - renderQuad.h = renderQuad.h * zoomH; - - // Renderiza a pantalla - SDL_RenderCopyEx(renderer, texture, clip, &renderQuad, angle, center, flip); -} - -// Establece la textura como objetivo de renderizado -void LTexture::setAsRenderTarget(SDL_Renderer *renderer) -{ - SDL_SetRenderTarget(renderer, texture); -} - -// Obtiene el ancho de la imagen -int LTexture::getWidth() -{ - return width; -} - -// Obtiene el alto de la imagen -int LTexture::getHeight() -{ - return height; -} - -// Recarga la textura -bool LTexture::reLoad() -{ - return loadFromFile(path, renderer); + +#include "ltexture.h" +#define STB_IMAGE_IMPLEMENTATION +#include "stb_image.h" + +// Constructor +LTexture::LTexture(SDL_Renderer *renderer, std::string path) +{ + // Copia punteros + this->renderer = renderer; + this->path = path; + + // Inicializa + texture = nullptr; + width = 0; + height = 0; + + // Carga el fichero en la textura + if (path != "") + { + loadFromFile(path, renderer); + } +} + +// Destructor +LTexture::~LTexture() +{ + // Libera memoria + unload(); +} + +// Carga una imagen desde un fichero +bool LTexture::loadFromFile(std::string path, SDL_Renderer *renderer) +{ + const std::string filename = path.substr(path.find_last_of("\\/") + 1); + int req_format = STBI_rgb_alpha; + int width, height, orig_format; + unsigned char *data = stbi_load(path.c_str(), &width, &height, &orig_format, req_format); + if (data == nullptr) + { + SDL_Log("Loading image failed: %s", stbi_failure_reason()); + exit(1); + } + else + { + printf("Image loaded: %s\n", filename.c_str()); + } + + int depth, pitch; + Uint32 pixel_format; + if (req_format == STBI_rgb) + { + depth = 24; + pitch = 3 * width; // 3 bytes por pixel * pixels per linea + pixel_format = SDL_PIXELFORMAT_RGB24; + } + else + { // STBI_rgb_alpha (RGBA) + depth = 32; + pitch = 4 * width; + pixel_format = SDL_PIXELFORMAT_RGBA32; + } + + // Limpia + unload(); + + // La textura final + SDL_Texture *newTexture = nullptr; + + // Carga la imagen desde una ruta específica + SDL_Surface *loadedSurface = SDL_CreateRGBSurfaceWithFormatFrom((void *)data, width, height, depth, pitch, pixel_format); + if (loadedSurface == nullptr) + { + printf("Unable to load image %s!\n", path.c_str()); + } + else + { + // Crea la textura desde los pixels de la surface + newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface); + if (newTexture == nullptr) + { + printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError()); + } + else + { + // Obtiene las dimensiones de la imagen + this->width = loadedSurface->w; + this->height = loadedSurface->h; + } + + // Elimina la textura cargada + SDL_FreeSurface(loadedSurface); + } + + // Return success + texture = newTexture; + return texture != nullptr; +} + +// Crea una textura en blanco +bool LTexture::createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess access) +{ + // Crea una textura sin inicializar + texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, access, width, height); + if (texture == nullptr) + { + printf("Unable to create blank texture! SDL Error: %s\n", SDL_GetError()); + } + else + { + this->width = width; + this->height = height; + } + + return texture != nullptr; +} + +// Libera la memoria de la textura +void LTexture::unload() +{ + // Libera la textura si existe + if (texture != nullptr) + { + SDL_DestroyTexture(texture); + texture = nullptr; + width = 0; + height = 0; + } +} + +// Establece el color para la modulacion +void LTexture::setColor(Uint8 red, Uint8 green, Uint8 blue) +{ + SDL_SetTextureColorMod(texture, red, green, blue); +} + +// Establece el blending +void LTexture::setBlendMode(SDL_BlendMode blending) +{ + SDL_SetTextureBlendMode(texture, blending); +} + +// Establece el alpha para la modulación +void LTexture::setAlpha(Uint8 alpha) +{ + SDL_SetTextureAlphaMod(texture, alpha); +} + +// Renderiza la textura en un punto específico +void LTexture::render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip, float zoomW, float zoomH, double angle, SDL_Point *center, SDL_RendererFlip flip) +{ + // Establece el destino de renderizado en la pantalla + SDL_Rect renderQuad = {x, y, width, height}; + + // Obtiene las dimesiones del clip de renderizado + if (clip != nullptr) + { + renderQuad.w = clip->w; + renderQuad.h = clip->h; + } + + renderQuad.w = renderQuad.w * zoomW; + renderQuad.h = renderQuad.h * zoomH; + + // Renderiza a pantalla + SDL_RenderCopyEx(renderer, texture, clip, &renderQuad, angle, center, flip); +} + +// Establece la textura como objetivo de renderizado +void LTexture::setAsRenderTarget(SDL_Renderer *renderer) +{ + SDL_SetRenderTarget(renderer, texture); +} + +// Obtiene el ancho de la imagen +int LTexture::getWidth() +{ + return width; +} + +// Obtiene el alto de la imagen +int LTexture::getHeight() +{ + return height; +} + +// Recarga la textura +bool LTexture::reLoad() +{ + return loadFromFile(path, renderer); } \ No newline at end of file diff --git a/source/ltexture.h b/source/common/ltexture.h similarity index 96% rename from source/ltexture.h rename to source/common/ltexture.h index 2272f94..cb065ce 100644 --- a/source/ltexture.h +++ b/source/common/ltexture.h @@ -1,61 +1,61 @@ -#pragma once - -#include -#include -#include - -#ifndef LTEXTURE_H -#define LTEXTURE_H - -// Clase LTexture -class LTexture -{ -private: - SDL_Texture *texture; // La textura - SDL_Renderer *renderer; // Renderizador donde dibujar la textura - int width; // Ancho de la imagen - int height; // Alto de la imagen - std::string path; // Ruta de la imagen de la textura - -public: - // Constructor - LTexture(SDL_Renderer *renderer, std::string path = ""); - - // Destructor - ~LTexture(); - - // Carga una imagen desde un fichero - bool loadFromFile(std::string path, SDL_Renderer *renderer); - - // Crea una textura en blanco - bool createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess = SDL_TEXTUREACCESS_STREAMING); - - // Libera la memoria de la textura - void unload(); - - // Establece el color para la modulacion - void setColor(Uint8 red, Uint8 green, Uint8 blue); - - // Establece el blending - void setBlendMode(SDL_BlendMode blending); - - // Establece el alpha para la modulación - void setAlpha(Uint8 alpha); - - // Renderiza la textura en un punto específico - void render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip = nullptr, float zoomW = 1, float zoomH = 1, double angle = 0.0, SDL_Point *center = nullptr, SDL_RendererFlip flip = SDL_FLIP_NONE); - - // Establece la textura como objetivo de renderizado - void setAsRenderTarget(SDL_Renderer *renderer); - - // Obtiene el ancho de la imagen - int getWidth(); - - // Obtiene el alto de la imagen - int getHeight(); - - // Recarga la textura - bool reLoad(); -}; - -#endif +#pragma once + +#include +#include +#include + +#ifndef LTEXTURE_H +#define LTEXTURE_H + +// Clase LTexture +class LTexture +{ +private: + SDL_Texture *texture; // La textura + SDL_Renderer *renderer; // Renderizador donde dibujar la textura + int width; // Ancho de la imagen + int height; // Alto de la imagen + std::string path; // Ruta de la imagen de la textura + +public: + // Constructor + LTexture(SDL_Renderer *renderer, std::string path = ""); + + // Destructor + ~LTexture(); + + // Carga una imagen desde un fichero + bool loadFromFile(std::string path, SDL_Renderer *renderer); + + // Crea una textura en blanco + bool createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess = SDL_TEXTUREACCESS_STREAMING); + + // Libera la memoria de la textura + void unload(); + + // Establece el color para la modulacion + void setColor(Uint8 red, Uint8 green, Uint8 blue); + + // Establece el blending + void setBlendMode(SDL_BlendMode blending); + + // Establece el alpha para la modulación + void setAlpha(Uint8 alpha); + + // Renderiza la textura en un punto específico + void render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip = nullptr, float zoomW = 1, float zoomH = 1, double angle = 0.0, SDL_Point *center = nullptr, SDL_RendererFlip flip = SDL_FLIP_NONE); + + // Establece la textura como objetivo de renderizado + void setAsRenderTarget(SDL_Renderer *renderer); + + // Obtiene el ancho de la imagen + int getWidth(); + + // Obtiene el alto de la imagen + int getHeight(); + + // Recarga la textura + bool reLoad(); +}; + +#endif diff --git a/source/menu.cpp b/source/common/menu.cpp similarity index 99% rename from source/menu.cpp rename to source/common/menu.cpp index 2d7771e..57560ff 100644 --- a/source/menu.cpp +++ b/source/common/menu.cpp @@ -1,4 +1,4 @@ -#include "const.h" +#include "../const.h" #include "menu.h" // Constructor diff --git a/source/menu.h b/source/common/menu.h similarity index 99% rename from source/menu.h rename to source/common/menu.h index fba00be..adc6b4b 100644 --- a/source/menu.h +++ b/source/common/menu.h @@ -68,7 +68,7 @@ private: int a; // Cantidad de transparencia para el rectangulo del selector }; - // Objetos + // Objetos y punteros SDL_Renderer *renderer; // Puntero al renderizador de la ventana Text *text; // Texto para poder escribir los items del menu Input *input; // Gestor de eventos de entrada de teclado o gamepad diff --git a/source/movingsprite.cpp b/source/common/movingsprite.cpp similarity index 99% rename from source/movingsprite.cpp rename to source/common/movingsprite.cpp index f3efc9a..ed8cb14 100644 --- a/source/movingsprite.cpp +++ b/source/common/movingsprite.cpp @@ -1,4 +1,4 @@ -#include "const.h" +#include "../const.h" #include "movingsprite.h" // Constructor diff --git a/source/movingsprite.h b/source/common/movingsprite.h similarity index 96% rename from source/movingsprite.h rename to source/common/movingsprite.h index 9f305b3..22777a8 100644 --- a/source/movingsprite.h +++ b/source/common/movingsprite.h @@ -1,170 +1,170 @@ -#pragma once - -#include -#include "sprite.h" - -#ifndef MOVINGSPRITE_H -#define MOVINGSPRITE_H - -// Clase MovingSprite. Añade posicion y velocidad en punto flotante -class MovingSprite : public Sprite -{ -protected: - float x; // Posición en el eje X - float y; // Posición en el eje Y - - float xPrev; // Posición anterior en el eje X - float yPrev; // Posición anterior en el eje Y - - float vx; // Velocidad en el eje X. Cantidad de pixeles a desplazarse - float vy; // Velocidad en el eje Y. Cantidad de pixeles a desplazarse - - float ax; // Aceleración en el eje X. Variación de la velocidad - float ay; // Aceleración en el eje Y. Variación de la velocidad - - float zoomW; // Zoom aplicado a la anchura - float zoomH; // Zoom aplicado a la altura - - double angle; // Angulo para dibujarlo - bool rotateEnabled; // Indica si ha de rotar - int rotateSpeed; // Velocidad de giro - double rotateAmount; // Cantidad de grados a girar en cada iteración - int counter; // Contador interno - SDL_Point *center; // Centro de rotación - SDL_RendererFlip currentFlip; // Indica como se voltea el sprite - -public: - // Constructor - MovingSprite(float x = 0, float y = 0, int w = 0, int h = 0, float velx = 0, float vely = 0, float accelx = 0, float accely = 0, LTexture *texture = nullptr, SDL_Renderer *renderer = nullptr); - - // Destructor - ~MovingSprite(); - - // Mueve el sprite - void move(); - - // Rota el sprite - void rotate(); - - // Actualiza las variables internas del objeto - void update(); - - // Reinicia todas las variables - void clear(); - - // Muestra el sprite por pantalla - void render(); - - // Obten el valor de la variable - float getPosX(); - - // Obten el valor de la variable - float getPosY(); - - // Obten el valor de la variable - float getVelX(); - - // Obten el valor de la variable - float getVelY(); - - // Obten el valor de la variable - float getAccelX(); - - // Obten el valor de la variable - float getAccelY(); - - // Obten el valor de la variable - float getZoomW(); - - // Obten el valor de la variable - float getZoomH(); - - // Obten el valor de la variable - double getAngle(); - - // Obtiene el valor de la variable - bool getRotate(); - - // Obtiene el valor de la variable - Uint16 getRotateSpeed(); - - // Establece la posición y el tamaño del objeto - void setRect(SDL_Rect rect); - - // Establece el valor de la variable - void setPosX(float value); - - // Establece el valor de la variable - void setPosY(float value); - - // Establece el valor de la variable - void setVelX(float value); - - // Establece el valor de la variable - void setVelY(float value); - - // Establece el valor de la variable - void setAccelX(float value); - - // Establece el valor de la variable - void setAccelY(float value); - - // Establece el valor de la variable - void setZoomW(float value); - - // Establece el valor de la variable - void setZoomH(float value); - - // Establece el valor de la variable - void setAngle(double vaue); - - // Incrementa el valor de la variable - void incAngle(double value); - - // Decrementa el valor de la variable - void decAngle(double value); - - // Establece el valor de la variable - void setRotate(bool value); - - // Establece el valor de la variable - void setRotateSpeed(int value); - - // Establece el valor de la variable - void setRotateAmount(double value); - - // Quita el efecto de rotación y deja el sprite en su angulo inicial. - void disableRotate(); - - // Cambia el sentido de la rotación - void switchRotate(); - - // Establece el valor de la variable - void setFlip(SDL_RendererFlip flip); - - // Gira el sprite horizontalmente - void flip(); - - // Obtiene el valor de la variable - SDL_RendererFlip getFlip(); - - // Devuelve el rectangulo donde está el sprite - SDL_Rect getRect(); - - // Deshace el último movimiento - void undoMove(); - - // Deshace el último movimiento en el eje X - void undoMoveX(); - - // Deshace el último movimiento en el eje Y - void undoMoveY(); - - // Pone a cero las velocidades de desplacamiento - void clearVel(); - - // Devuelve el incremento en el eje X en pixels - int getIncX(); -}; - -#endif +#pragma once + +#include +#include "sprite.h" + +#ifndef MOVINGSPRITE_H +#define MOVINGSPRITE_H + +// Clase MovingSprite. Añade posicion y velocidad en punto flotante +class MovingSprite : public Sprite +{ +protected: + float x; // Posición en el eje X + float y; // Posición en el eje Y + + float xPrev; // Posición anterior en el eje X + float yPrev; // Posición anterior en el eje Y + + float vx; // Velocidad en el eje X. Cantidad de pixeles a desplazarse + float vy; // Velocidad en el eje Y. Cantidad de pixeles a desplazarse + + float ax; // Aceleración en el eje X. Variación de la velocidad + float ay; // Aceleración en el eje Y. Variación de la velocidad + + float zoomW; // Zoom aplicado a la anchura + float zoomH; // Zoom aplicado a la altura + + double angle; // Angulo para dibujarlo + bool rotateEnabled; // Indica si ha de rotar + int rotateSpeed; // Velocidad de giro + double rotateAmount; // Cantidad de grados a girar en cada iteración + int counter; // Contador interno + SDL_Point *center; // Centro de rotación + SDL_RendererFlip currentFlip; // Indica como se voltea el sprite + +public: + // Constructor + MovingSprite(float x = 0, float y = 0, int w = 0, int h = 0, float velx = 0, float vely = 0, float accelx = 0, float accely = 0, LTexture *texture = nullptr, SDL_Renderer *renderer = nullptr); + + // Destructor + ~MovingSprite(); + + // Mueve el sprite + void move(); + + // Rota el sprite + void rotate(); + + // Actualiza las variables internas del objeto + void update(); + + // Reinicia todas las variables + void clear(); + + // Muestra el sprite por pantalla + void render(); + + // Obten el valor de la variable + float getPosX(); + + // Obten el valor de la variable + float getPosY(); + + // Obten el valor de la variable + float getVelX(); + + // Obten el valor de la variable + float getVelY(); + + // Obten el valor de la variable + float getAccelX(); + + // Obten el valor de la variable + float getAccelY(); + + // Obten el valor de la variable + float getZoomW(); + + // Obten el valor de la variable + float getZoomH(); + + // Obten el valor de la variable + double getAngle(); + + // Obtiene el valor de la variable + bool getRotate(); + + // Obtiene el valor de la variable + Uint16 getRotateSpeed(); + + // Establece la posición y el tamaño del objeto + void setRect(SDL_Rect rect); + + // Establece el valor de la variable + void setPosX(float value); + + // Establece el valor de la variable + void setPosY(float value); + + // Establece el valor de la variable + void setVelX(float value); + + // Establece el valor de la variable + void setVelY(float value); + + // Establece el valor de la variable + void setAccelX(float value); + + // Establece el valor de la variable + void setAccelY(float value); + + // Establece el valor de la variable + void setZoomW(float value); + + // Establece el valor de la variable + void setZoomH(float value); + + // Establece el valor de la variable + void setAngle(double vaue); + + // Incrementa el valor de la variable + void incAngle(double value); + + // Decrementa el valor de la variable + void decAngle(double value); + + // Establece el valor de la variable + void setRotate(bool value); + + // Establece el valor de la variable + void setRotateSpeed(int value); + + // Establece el valor de la variable + void setRotateAmount(double value); + + // Quita el efecto de rotación y deja el sprite en su angulo inicial. + void disableRotate(); + + // Cambia el sentido de la rotación + void switchRotate(); + + // Establece el valor de la variable + void setFlip(SDL_RendererFlip flip); + + // Gira el sprite horizontalmente + void flip(); + + // Obtiene el valor de la variable + SDL_RendererFlip getFlip(); + + // Devuelve el rectangulo donde está el sprite + SDL_Rect getRect(); + + // Deshace el último movimiento + void undoMove(); + + // Deshace el último movimiento en el eje X + void undoMoveX(); + + // Deshace el último movimiento en el eje Y + void undoMoveY(); + + // Pone a cero las velocidades de desplacamiento + void clearVel(); + + // Devuelve el incremento en el eje X en pixels + int getIncX(); +}; + +#endif diff --git a/source/screen.cpp b/source/common/screen.cpp similarity index 100% rename from source/screen.cpp rename to source/common/screen.cpp diff --git a/source/screen.h b/source/common/screen.h similarity index 100% rename from source/screen.h rename to source/common/screen.h diff --git a/source/smartsprite.cpp b/source/common/smartsprite.cpp similarity index 99% rename from source/smartsprite.cpp rename to source/common/smartsprite.cpp index 52ced2f..65e8f2c 100644 --- a/source/smartsprite.cpp +++ b/source/common/smartsprite.cpp @@ -1,4 +1,4 @@ -#include "const.h" +#include "../const.h" #include "smartsprite.h" // Constructor diff --git a/source/smartsprite.h b/source/common/smartsprite.h similarity index 95% rename from source/smartsprite.h rename to source/common/smartsprite.h index 8362465..493c25e 100644 --- a/source/smartsprite.h +++ b/source/common/smartsprite.h @@ -1,76 +1,76 @@ -#pragma once - -#include -#include "animatedsprite.h" -#include "utils.h" -#include - -#ifndef SMARTSPRITE_H -#define SMARTSPRITE_H - -// Clase SmartSprite -class SmartSprite : public AnimatedSprite -{ -private: - // VAriables - bool enabled; // Indica si esta habilitado - bool onDestination; // Indica si está en el destino - int destX; // Posicion de destino en el eje X - int destY; // Posicion de destino en el eje Y - int enabledCounter; // Contador para deshabilitarlo - bool finished; // Indica si ya ha terminado - - // Comprueba el movimiento - void checkMove(); - - // Comprueba si ha terminado - void checkFinished(); - -public: - // Constructor - SmartSprite(LTexture *texture, SDL_Renderer *renderer); - - // Destructor - ~SmartSprite(); - - // Inicializa el objeto - void init(); - - // Actualiza la posición y comprueba si ha llegado a su destino - void update(); - - // Pinta el objeto en pantalla - void render(); - - // Obtiene el valor de la variable - bool isEnabled(); - - // Establece el valor de la variable - void setEnabled(bool enabled); - - // Obtiene el valor de la variable - int getEnabledCounter(); - - // Establece el valor de la variable - void setEnabledCounter(int value); - - // Establece el valor de la variable - void setDestX(int x); - - // Establece el valor de la variable - void setDestY(int y); - - // Obtiene el valor de la variable - int getDestX(); - - // Obtiene el valor de la variable - int getDestY(); - - // Obtiene el valor de la variable - bool isOnDestination(); - - // Obtiene el valor de la variable - bool hasFinished(); -}; - -#endif +#pragma once + +#include +#include "animatedsprite.h" +#include "utils.h" +#include + +#ifndef SMARTSPRITE_H +#define SMARTSPRITE_H + +// Clase SmartSprite +class SmartSprite : public AnimatedSprite +{ +private: + // VAriables + bool enabled; // Indica si esta habilitado + bool onDestination; // Indica si está en el destino + int destX; // Posicion de destino en el eje X + int destY; // Posicion de destino en el eje Y + int enabledCounter; // Contador para deshabilitarlo + bool finished; // Indica si ya ha terminado + + // Comprueba el movimiento + void checkMove(); + + // Comprueba si ha terminado + void checkFinished(); + +public: + // Constructor + SmartSprite(LTexture *texture, SDL_Renderer *renderer); + + // Destructor + ~SmartSprite(); + + // Inicializa el objeto + void init(); + + // Actualiza la posición y comprueba si ha llegado a su destino + void update(); + + // Pinta el objeto en pantalla + void render(); + + // Obtiene el valor de la variable + bool isEnabled(); + + // Establece el valor de la variable + void setEnabled(bool enabled); + + // Obtiene el valor de la variable + int getEnabledCounter(); + + // Establece el valor de la variable + void setEnabledCounter(int value); + + // Establece el valor de la variable + void setDestX(int x); + + // Establece el valor de la variable + void setDestY(int y); + + // Obtiene el valor de la variable + int getDestX(); + + // Obtiene el valor de la variable + int getDestY(); + + // Obtiene el valor de la variable + bool isOnDestination(); + + // Obtiene el valor de la variable + bool hasFinished(); +}; + +#endif diff --git a/source/sprite.cpp b/source/common/sprite.cpp similarity index 100% rename from source/sprite.cpp rename to source/common/sprite.cpp diff --git a/source/sprite.h b/source/common/sprite.h similarity index 96% rename from source/sprite.h rename to source/common/sprite.h index 31c16db..ba0265c 100644 --- a/source/sprite.h +++ b/source/common/sprite.h @@ -1,96 +1,96 @@ -#pragma once - -#include -#include "ltexture.h" - -#ifndef SPRITE_H -#define SPRITE_H - -// Clase sprite -class Sprite -{ -protected: - int x; // Posición en el eje X donde dibujar el sprite - int y; // Posición en el eje Y donde dibujar el sprite - int w; // Ancho del sprite - int h; // Alto del sprite - - SDL_Renderer *renderer; // Puntero al renderizador de la ventana - LTexture *texture; // Textura donde estan todos los dibujos del sprite - SDL_Rect spriteClip; // Rectangulo de origen de la textura que se dibujará en pantalla - - bool enabled; // Indica si el sprite esta habilitado - -public: - // Constructor - Sprite(int x = 0, int y = 0, int w = 0, int h = 0, LTexture *texture = nullptr, SDL_Renderer *renderer = nullptr); - Sprite(SDL_Rect rect, LTexture *texture, SDL_Renderer *renderer); - - // Destructor - ~Sprite(); - - // Muestra el sprite por pantalla - void render(); - - // Obten el valor de la variable - int getPosX(); - - // Obten el valor de la variable - int getPosY(); - - // Obten el valor de la variable - int getWidth(); - - // Obten el valor de la variable - int getHeight(); - - // Establece la posición del objeto - void setPos(SDL_Rect rect); - - // Establece el valor de la variable - void setPosX(int x); - - // Establece el valor de la variable - void setPosY(int y); - - // Establece el valor de la variable - void setWidth(int w); - - // Establece el valor de la variable - void setHeight(int h); - - // Obten el valor de la variable - SDL_Rect getSpriteClip(); - - // Establece el valor de la variable - void setSpriteClip(SDL_Rect rect); - - // Establece el valor de la variable - void setSpriteClip(int x, int y, int w, int h); - - // Obten el valor de la variable - LTexture *getTexture(); - - // Establece el valor de la variable - void setTexture(LTexture *texture); - - // Obten el valor de la variable - SDL_Renderer *getRenderer(); - - // Establece el valor de la variable - void setRenderer(SDL_Renderer *renderer); - - // Establece el valor de la variable - void setEnabled(bool value); - - // Comprueba si el objeto está habilitado - bool isEnabled(); - - // Devuelve el rectangulo donde está el sprite - SDL_Rect getRect(); - - // Establece los valores de posición y tamaño del sprite - void setRect(SDL_Rect rect); -}; - -#endif +#pragma once + +#include +#include "ltexture.h" + +#ifndef SPRITE_H +#define SPRITE_H + +// Clase sprite +class Sprite +{ +protected: + int x; // Posición en el eje X donde dibujar el sprite + int y; // Posición en el eje Y donde dibujar el sprite + int w; // Ancho del sprite + int h; // Alto del sprite + + SDL_Renderer *renderer; // Puntero al renderizador de la ventana + LTexture *texture; // Textura donde estan todos los dibujos del sprite + SDL_Rect spriteClip; // Rectangulo de origen de la textura que se dibujará en pantalla + + bool enabled; // Indica si el sprite esta habilitado + +public: + // Constructor + Sprite(int x = 0, int y = 0, int w = 0, int h = 0, LTexture *texture = nullptr, SDL_Renderer *renderer = nullptr); + Sprite(SDL_Rect rect, LTexture *texture, SDL_Renderer *renderer); + + // Destructor + ~Sprite(); + + // Muestra el sprite por pantalla + void render(); + + // Obten el valor de la variable + int getPosX(); + + // Obten el valor de la variable + int getPosY(); + + // Obten el valor de la variable + int getWidth(); + + // Obten el valor de la variable + int getHeight(); + + // Establece la posición del objeto + void setPos(SDL_Rect rect); + + // Establece el valor de la variable + void setPosX(int x); + + // Establece el valor de la variable + void setPosY(int y); + + // Establece el valor de la variable + void setWidth(int w); + + // Establece el valor de la variable + void setHeight(int h); + + // Obten el valor de la variable + SDL_Rect getSpriteClip(); + + // Establece el valor de la variable + void setSpriteClip(SDL_Rect rect); + + // Establece el valor de la variable + void setSpriteClip(int x, int y, int w, int h); + + // Obten el valor de la variable + LTexture *getTexture(); + + // Establece el valor de la variable + void setTexture(LTexture *texture); + + // Obten el valor de la variable + SDL_Renderer *getRenderer(); + + // Establece el valor de la variable + void setRenderer(SDL_Renderer *renderer); + + // Establece el valor de la variable + void setEnabled(bool value); + + // Comprueba si el objeto está habilitado + bool isEnabled(); + + // Devuelve el rectangulo donde está el sprite + SDL_Rect getRect(); + + // Establece los valores de posición y tamaño del sprite + void setRect(SDL_Rect rect); +}; + +#endif diff --git a/source/stb_image.h b/source/common/stb_image.h similarity index 100% rename from source/stb_image.h rename to source/common/stb_image.h diff --git a/source/stb_vorbis.c b/source/common/stb_vorbis.c similarity index 100% rename from source/stb_vorbis.c rename to source/common/stb_vorbis.c diff --git a/source/text.cpp b/source/common/text.cpp similarity index 100% rename from source/text.cpp rename to source/common/text.cpp diff --git a/source/text.h b/source/common/text.h similarity index 95% rename from source/text.h rename to source/common/text.h index ef59417..3a6c987 100644 --- a/source/text.h +++ b/source/common/text.h @@ -1,69 +1,69 @@ -#pragma once - -#include "sprite.h" -#include "utils.h" - -#ifndef TEXT_H -#define TEXT_H - -#define TXT_COLOR 1 -#define TXT_SHADOW 2 -#define TXT_CENTER 4 -#define TXT_STROKE 8 - -// Clase texto. Pinta texto en pantalla a partir de un bitmap -class Text -{ -private: - struct offset_t - { - int x; - int y; - int w; - }; - - // Objetos - Sprite *sprite; // Objeto con los graficos para el texto - LTexture *texture; // Textura con los bitmaps del texto - - // Variables - int boxWidth; // Anchura de la caja de cada caracter en el png - int boxHeight; // Altura de la caja de cada caracter en el png - offset_t offset[128]; // Vector con las posiciones y ancho de cada letra - - // Inicializa el vector de offsets desde un fichero - void initOffsetFromFile(std::string file); - -public: - // Constructor - Text(std::string bitmapFile, std::string textFile, SDL_Renderer *renderer); - - // Destructor - ~Text(); - - // Escribe el texto en pantalla - void write(int x, int y, std::string text, int kerning = 1, int lenght = -1); - - // Escribe el texto con colores - void writeColored(int x, int y, std::string text, color_t color, int kerning = 1, int lenght = -1); - - // Escribe el texto con sombra - void writeShadowed(int x, int y, std::string text, color_t color, Uint8 shadowDistance = 1, int kerning = 1, int lenght = -1); - - // Escribe el texto centrado en un punto x - void writeCentered(int x, int y, std::string text, int kerning = 1, int lenght = -1); - - // Escribe texto con extras - void writeDX(Uint8 flags, int x, int y, std::string text, int kerning = 1, color_t textColor = {255, 255, 255}, Uint8 shadowDistance = 1, color_t shadowColor = {0, 0, 0}, int lenght = -1); - - // Obtiene la longitud en pixels de una cadena - int lenght(std::string text, int kerning = 1); - - // Devuelve el valor de la variable - int getCharacterSize(); - - // Recarga la textura - void reLoadTexture(); -}; - -#endif +#pragma once + +#include "sprite.h" +#include "utils.h" + +#ifndef TEXT_H +#define TEXT_H + +#define TXT_COLOR 1 +#define TXT_SHADOW 2 +#define TXT_CENTER 4 +#define TXT_STROKE 8 + +// Clase texto. Pinta texto en pantalla a partir de un bitmap +class Text +{ +private: + struct offset_t + { + int x; + int y; + int w; + }; + + // Objetos y punteros + Sprite *sprite; // Objeto con los graficos para el texto + LTexture *texture; // Textura con los bitmaps del texto + + // Variables + int boxWidth; // Anchura de la caja de cada caracter en el png + int boxHeight; // Altura de la caja de cada caracter en el png + offset_t offset[128]; // Vector con las posiciones y ancho de cada letra + + // Inicializa el vector de offsets desde un fichero + void initOffsetFromFile(std::string file); + +public: + // Constructor + Text(std::string bitmapFile, std::string textFile, SDL_Renderer *renderer); + + // Destructor + ~Text(); + + // Escribe el texto en pantalla + void write(int x, int y, std::string text, int kerning = 1, int lenght = -1); + + // Escribe el texto con colores + void writeColored(int x, int y, std::string text, color_t color, int kerning = 1, int lenght = -1); + + // Escribe el texto con sombra + void writeShadowed(int x, int y, std::string text, color_t color, Uint8 shadowDistance = 1, int kerning = 1, int lenght = -1); + + // Escribe el texto centrado en un punto x + void writeCentered(int x, int y, std::string text, int kerning = 1, int lenght = -1); + + // Escribe texto con extras + void writeDX(Uint8 flags, int x, int y, std::string text, int kerning = 1, color_t textColor = {255, 255, 255}, Uint8 shadowDistance = 1, color_t shadowColor = {0, 0, 0}, int lenght = -1); + + // Obtiene la longitud en pixels de una cadena + int lenght(std::string text, int kerning = 1); + + // Devuelve el valor de la variable + int getCharacterSize(); + + // Recarga la textura + void reLoadTexture(); +}; + +#endif diff --git a/source/utils.cpp b/source/common/utils.cpp similarity index 100% rename from source/utils.cpp rename to source/common/utils.cpp diff --git a/source/utils.h b/source/common/utils.h similarity index 100% rename from source/utils.h rename to source/common/utils.h diff --git a/source/writer.cpp b/source/common/writer.cpp similarity index 98% rename from source/writer.cpp rename to source/common/writer.cpp index af00439..e57707f 100644 --- a/source/writer.cpp +++ b/source/common/writer.cpp @@ -1,4 +1,4 @@ -#include "const.h" +#include "../const.h" #include "writer.h" // Constructor diff --git a/source/writer.h b/source/common/writer.h similarity index 98% rename from source/writer.h rename to source/common/writer.h index 7d7c567..0d0beb3 100644 --- a/source/writer.h +++ b/source/common/writer.h @@ -11,7 +11,7 @@ class Writer { private: - // Objetos + // Objetos y punteros Text *text; // Objeto encargado de escribir el texto // Variables diff --git a/source/const.h b/source/const.h index d6bc717..0b69fdf 100644 --- a/source/const.h +++ b/source/const.h @@ -1,7 +1,7 @@ #pragma once #include -#include "utils.h" +#include "common/utils.h" #include "lang.h" #ifndef CONST_H diff --git a/source/director.cpp b/source/director.cpp index d51149f..0416205 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -1,6 +1,6 @@ #include "const.h" #include "director.h" -#include "utils.h" +#include "common/utils.h" #include #include diff --git a/source/director.h b/source/director.h index 6b8410a..19bb4b6 100644 --- a/source/director.h +++ b/source/director.h @@ -1,27 +1,27 @@ #pragma once #include -#include "asset.h" #include "balloon.h" #include "bullet.h" +#include "common/asset.h" +#include "common/input.h" +#include "common/jail_audio.h" +#include "common/menu.h" +#include "common/movingsprite.h" +#include "common/screen.h" +#include "common/smartsprite.h" +#include "common/sprite.h" +#include "common/text.h" +#include "common/utils.h" +#include "common/writer.h" #include "const.h" #include "fade.h" #include "game.h" -#include "input.h" #include "intro.h" #include "item.h" -#include "jail_audio.h" #include "logo.h" -#include "menu.h" -#include "movingsprite.h" #include "player.h" -#include "screen.h" -#include "smartsprite.h" -#include "sprite.h" -#include "text.h" #include "title.h" -#include "utils.h" -#include "writer.h" #ifndef DIRECTOR_H #define DIRECTOR_H @@ -29,11 +29,10 @@ // Textos #define WINDOW_CAPTION "Coffee Crisis" -// Clase Director class Director { private: - // Objetos + // Objetos y punteros SDL_Window *window; // La ventana donde dibujamos SDL_Renderer *renderer; // El renderizador de la ventana Screen *screen; // Objeto encargado de dibujar en pantalla diff --git a/source/fade.h b/source/fade.h index 82050b1..5a1fda5 100644 --- a/source/fade.h +++ b/source/fade.h @@ -1,7 +1,7 @@ #pragma once #include -#include "ltexture.h" +#include "common/ltexture.h" #ifndef FADE_H #define FADE_H diff --git a/source/game.h b/source/game.h index f7e0b78..5c883ad 100644 --- a/source/game.h +++ b/source/game.h @@ -1,23 +1,23 @@ #pragma once #include -#include "asset.h" +#include "common/asset.h" #include "balloon.h" #include "bullet.h" #include "const.h" #include "fade.h" -#include "input.h" +#include "common/input.h" #include "item.h" -#include "jail_audio.h" -#include "menu.h" -#include "movingsprite.h" +#include "common/jail_audio.h" +#include "common/menu.h" +#include "common/movingsprite.h" #include "player.h" -#include "screen.h" -#include "smartsprite.h" -#include "sprite.h" -#include "text.h" -#include "utils.h" -#include "writer.h" +#include "common/screen.h" +#include "common/smartsprite.h" +#include "common/sprite.h" +#include "common/text.h" +#include "common/utils.h" +#include "common/writer.h" #include #ifndef GAME_H @@ -114,7 +114,7 @@ private: demoKeys_t dataFile[TOTAL_DEMO_DATA]; // Datos del fichero con los movimientos para la demo }; - // Objetos + // Objetos y punteros SDL_Renderer *renderer; // El renderizador de la ventana Screen *screen; // Objeto encargado de dibujar en pantalla Asset *asset; // Objeto que gestiona todos los ficheros de recursos diff --git a/source/instructions.h b/source/instructions.h index b008745..0c5b7cf 100644 --- a/source/instructions.h +++ b/source/instructions.h @@ -1,13 +1,13 @@ #pragma once #include -#include "asset.h" +#include "common/asset.h" #include "const.h" -#include "jail_audio.h" -#include "screen.h" -#include "sprite.h" -#include "text.h" -#include "utils.h" +#include "common/jail_audio.h" +#include "common/screen.h" +#include "common/sprite.h" +#include "common/text.h" +#include "common/utils.h" #ifndef INSTRUCTIONS_H #define INSTRUCTIONS_H @@ -22,7 +22,7 @@ enum mode_e class Instructions { private: - // Objetos + // Objetos y punteros SDL_Renderer *renderer; // El renderizador de la ventana Screen *screen; // Objeto encargado de dibujar en pantalla std::vector itemTextures; // Vector con las texturas de los items diff --git a/source/intro.h b/source/intro.h index 01dd792..2145531 100644 --- a/source/intro.h +++ b/source/intro.h @@ -1,13 +1,13 @@ #pragma once #include -#include "asset.h" +#include "common/asset.h" #include "const.h" -#include "jail_audio.h" -#include "screen.h" -#include "smartsprite.h" -#include "utils.h" -#include "writer.h" +#include "common/jail_audio.h" +#include "common/screen.h" +#include "common/smartsprite.h" +#include "common/utils.h" +#include "common/writer.h" #include #ifndef INTRO_H @@ -17,7 +17,7 @@ class Intro { private: - // Objetos + // Objetos y punteros SDL_Renderer *renderer; // El renderizador de la ventana Screen *screen; // Objeto encargado de dibujar en pantalla LTexture *texture; // Textura con los graficos diff --git a/source/item.h b/source/item.h index 4118ee5..460c7bf 100644 --- a/source/item.h +++ b/source/item.h @@ -1,8 +1,8 @@ #pragma once #include -#include "animatedsprite.h" -#include "utils.h" +#include "common/animatedsprite.h" +#include "common/utils.h" #ifndef ITEM_H #define ITEM_H @@ -19,7 +19,7 @@ class Item { private: - // Objetos + // Objetos y punteros AnimatedSprite *sprite; // Sprite con los graficos del objeto // Variables diff --git a/source/lang.h b/source/lang.h index 7cbe52b..00a9410 100644 --- a/source/lang.h +++ b/source/lang.h @@ -1,7 +1,7 @@ #pragma once #include -#include "asset.h" +#include "common/asset.h" #include #ifndef LANG_H diff --git a/source/logo.h b/source/logo.h index 7ffed74..98045bf 100644 --- a/source/logo.h +++ b/source/logo.h @@ -1,12 +1,12 @@ #pragma once #include -#include "asset.h" +#include "common/asset.h" #include "const.h" -#include "jail_audio.h" -#include "screen.h" -#include "sprite.h" -#include "utils.h" +#include "common/jail_audio.h" +#include "common/screen.h" +#include "common/sprite.h" +#include "common/utils.h" #ifndef LOGO_H #define LOGO_H @@ -15,7 +15,7 @@ class Logo { private: - // Objetos + // Objetos y punteros SDL_Renderer *renderer; // El renderizador de la ventana Screen *screen; // Objeto encargado de dibujar en pantalla Asset *asset; // Objeto que gestiona todos los ficheros de recursos diff --git a/source/player.h b/source/player.h index bb043f6..8b5ec44 100644 --- a/source/player.h +++ b/source/player.h @@ -1,11 +1,11 @@ #pragma once #include -#include "utils.h" -#include "input.h" -#include "asset.h" -#include "ltexture.h" -#include "animatedsprite.h" +#include "common/utils.h" +#include "common/input.h" +#include "common/asset.h" +#include "common/ltexture.h" +#include "common/animatedsprite.h" #ifndef PLAYER_H #define PLAYER_H @@ -31,7 +31,7 @@ class Player { private: - // Objetos + // Objetos y punteros SDL_Renderer *renderer; // El renderizador de la ventana AnimatedSprite *headSprite; // Sprite para dibujar la cabeza AnimatedSprite *bodySprite; // Sprite para dibujar el cuerpo diff --git a/source/title.h b/source/title.h index aa969ff..dd53693 100644 --- a/source/title.h +++ b/source/title.h @@ -1,21 +1,21 @@ #pragma once #include -#include "asset.h" +#include "common/asset.h" +#include "common/input.h" +#include "common/jail_audio.h" +#include "common/menu.h" +#include "common/movingsprite.h" +#include "common/screen.h" +#include "common/smartsprite.h" +#include "common/sprite.h" +#include "common/text.h" +#include "common/utils.h" #include "const.h" #include "fade.h" #include "game.h" -#include "input.h" #include "instructions.h" #include "item.h" -#include "jail_audio.h" -#include "menu.h" -#include "movingsprite.h" -#include "screen.h" -#include "smartsprite.h" -#include "sprite.h" -#include "text.h" -#include "utils.h" #ifndef TITLE_H #define TITLE_H @@ -29,7 +29,6 @@ // Cantidad de eventos de la pantalla de titulo #define TITLE_TOTAL_EVENTS 2 -// Clase Title class Title { private: @@ -42,7 +41,7 @@ private: bool keyPressed; // Variable para evitar la repetición de teclas en los menus }; - // Objetos + // Objetos y punteros SDL_Renderer *renderer; // El renderizador de la ventana Screen *screen; // Objeto encargado de dibujar en pantalla Asset *asset; // Objeto que gestiona todos los ficheros de recursos