diff --git a/.gitignore b/.gitignore index 0b208af..0086f94 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ thumbs.db *.tar.gz *.zip *.app -*_debug* \ No newline at end of file +*_debug* +jail_engine* \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6126fef --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +executable = jail_engine_demo +source = *.cpp units/*.cpp + +windows: + @echo off + g++ $(source) -std=c++11 -Wall -Os -lmingw32 -lws2_32 -lSDL2main -lSDL2 -ffunction-sections -fdata-sections -Wl,--gc-sections -static-libstdc++ -Wl,-subsystem,windows -o "$(executable).exe" + strip -s -R .comment -R .gnu.version "$(executable).exe" --strip-unneeded + +macos: + clang++ $(source) -std=c++11 -Wall -Os -lSDL2 -ffunction-sections -fdata-sections -o "$(executable)" + + +linux: + g++ $(source) -std=c++11 -Wall -Os -lSDL2 -ffunction-sections -fdata-sections -Wl,--gc-sections -o "$(executable)" + strip -s -R .comment -R .gnu.version "$(executable)" --strip-unneeded \ No newline at end of file diff --git a/main.cpp b/main.cpp index 10247fe..ced40ec 100644 --- a/main.cpp +++ b/main.cpp @@ -18,8 +18,8 @@ Código fuente creado por JailDesigner SDL_Event *event; SDL_Window *window; SDL_Renderer *renderer; -int ticks = 0; -int ticksSpeed = 15; +Uint32 ticks = 0; +Uint32 ticksSpeed = 15; int counter = 0; int gradColorMin = 64; // Minimo color más alto del degradado int gradColorMax = 192; // Minimo color más alto del degradado @@ -28,7 +28,6 @@ int gradBreathDirection = 0; // Indica si gradCurrentColor crece o decrece int main(int argc, char *argv[]) { - // Inicializa las opciones struct options_t *options = new options_t; initOptions(options); @@ -36,7 +35,7 @@ int main(int argc, char *argv[]) options->screen.nativeHeight = 240; options->screen.nativeZoom = 1; options->screen.windowZoom = 2; - options->console = true; + options->console = false; // Inicializa la lista de recursos Asset *asset = new Asset(argv[0]); @@ -56,6 +55,7 @@ int main(int argc, char *argv[]) // Inicializa SDL y la ventana SDL_Init(SDL_INIT_EVERYTHING); window = SDL_CreateWindow("jail_engine_demo", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, options->screen.nativeWidth * options->screen.nativeZoom * options->screen.windowZoom, options->screen.nativeHeight * options->screen.nativeZoom * options->screen.windowZoom, SDL_WINDOW_SHOWN); + // window = SDL_CreateWindow("jail_engine_demo", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 320, 240, SDL_WINDOW_SHOWN); if (window != nullptr) { renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); @@ -72,13 +72,10 @@ int main(int argc, char *argv[]) // Inicializa jail_audio JA_Init(48000, AUDIO_S16, 2); - JA_Music_t *music; JA_Sound_t *sound; - music = JA_LoadMusic(asset->get("music.ogg").c_str()); sound = JA_LoadSound(asset->get("sound.wav").c_str()); - int volume = 128; // Inicializa el objeto screen Screen *screen = new Screen(window, renderer, options); @@ -101,7 +98,7 @@ int main(int argc, char *argv[]) sprite->setVelY(2); // Bucle principal - // JA_PlayMusic(music, true); + JA_PlayMusic(music, true); bool should_exit = false; while (!should_exit) { @@ -206,6 +203,8 @@ int main(int argc, char *argv[]) // Dibuja en pantalla screen->start(); screen->clean(); + // SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0xFF); + // SDL_RenderClear(renderer); // Dibuja un degradado de fondo const int gradFirstLine = options->screen.nativeHeight / 3; @@ -233,29 +232,54 @@ int main(int argc, char *argv[]) // Vuelca el buffer en pantalla screen->blit(); + // SDL_RenderPresent(renderer); } // Finaliza el sprite - delete sprite; - delete texture; + if (sprite != nullptr) + { + delete sprite; + } + if (texture != nullptr) + { + delete texture; + } // Finaliza el texto - delete text; + if (text != nullptr) + { + delete text; + } // Finaliza el objeto screen - delete screen; + if (screen != nullptr) + { + delete screen; + } // Finaliza jail_audio JA_DeleteSound(sound); JA_DeleteMusic(music); // Finaliza el objeto con la lista de recuros - delete asset; + if (asset != nullptr) + { + delete asset; + } + + // Finaliza las opciones + if (options != nullptr) + { + delete options; + } // Finaliza SDL y la ventana + if (event != nullptr) + { + delete event; + } SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); - delete event; SDL_Quit(); return 0; diff --git a/units/animatedsprite.cpp b/units/animatedsprite.cpp index d53889e..6ce6729 100644 --- a/units/animatedsprite.cpp +++ b/units/animatedsprite.cpp @@ -148,6 +148,7 @@ animatedSprite_t loadAnimationFromFile(Texture *texture, std::string filePath, b // Constructor AnimatedSprite::AnimatedSprite(Texture *texture, SDL_Renderer *renderer, std::string file, std::vector *buffer) { + //std::cout << "Creado AnimatedSprite" << std::endl; // Copia los punteros setTexture(texture); setRenderer(renderer); @@ -176,6 +177,7 @@ AnimatedSprite::AnimatedSprite(Texture *texture, SDL_Renderer *renderer, std::st // Constructor AnimatedSprite::AnimatedSprite(SDL_Renderer *renderer, animatedSprite_t *animation) { + //std::cout << "Creado AnimatedSprite" << std::endl; // Copia los punteros setTexture(animation->texture); setRenderer(renderer); @@ -193,6 +195,7 @@ AnimatedSprite::AnimatedSprite(SDL_Renderer *renderer, animatedSprite_t *animati // Destructor AnimatedSprite::~AnimatedSprite() { + //std::cout << "Destruido AnimatedSprite" << std::endl; for (auto &a : animation) { a.frames.clear(); @@ -510,21 +513,6 @@ void AnimatedSprite::update() MovingSprite::update(); } -// Establece el rectangulo para un frame de una animación -void AnimatedSprite::setAnimationFrames(Uint8 index_animation, Uint8 index_frame, int x, int y, int w, int h) -{ - animation[index_animation].frames.push_back({x, y, w, h}); -} - -// OLD - Establece el contador para todas las animaciones -void AnimatedSprite::setAnimationCounter(int value) -{ - for (auto &a : animation) - { - a.counter = value; - } -} - // Reinicia la animación void AnimatedSprite::resetAnimation() { diff --git a/units/animatedsprite.h b/units/animatedsprite.h index 39f6f90..97b4e36 100644 --- a/units/animatedsprite.h +++ b/units/animatedsprite.h @@ -90,12 +90,6 @@ public: // 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(); }; diff --git a/units/asset.cpp b/units/asset.cpp index 1202129..d1e0255 100644 --- a/units/asset.cpp +++ b/units/asset.cpp @@ -4,11 +4,18 @@ // Constructor Asset::Asset(std::string executablePath) { + //std::cout << "Construido Asset" << std::endl; this->executablePath = executablePath.substr(0, executablePath.find_last_of("\\/")); longestName = 0; verbose = true; } +// Destructot +Asset::~Asset() +{ + //std::cout << "Destruido Asset" << std::endl; +} + // Añade un elemento a la lista void Asset::add(std::string file, enum assetType type, bool required, bool absolute) { diff --git a/units/asset.h b/units/asset.h index 2eb5bca..c1040f8 100644 --- a/units/asset.h +++ b/units/asset.h @@ -50,6 +50,9 @@ 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, bool absolute = false); diff --git a/units/menu.h b/units/menu.h index a459c4c..a2f7c0b 100644 --- a/units/menu.h +++ b/units/menu.h @@ -4,7 +4,6 @@ #include "asset.h" #include "input.h" #include "jail_audio.h" -//#include "resource.h" #include "sprite.h" #include "text.h" #include "utils.h" diff --git a/units/movingsprite.cpp b/units/movingsprite.cpp index faf429a..a36eb8f 100644 --- a/units/movingsprite.cpp +++ b/units/movingsprite.cpp @@ -1,9 +1,11 @@ #include "movingsprite.h" +#include // Constructor MovingSprite::MovingSprite(float x, float y, int w, int h, float velx, float vely, float accelx, float accely, Texture *texture, SDL_Renderer *renderer) { + //std::cout << "Construido MovingSprite" << std::endl; // Copia los punteros this->texture = texture; this->renderer = renderer; @@ -53,6 +55,12 @@ MovingSprite::MovingSprite(float x, float y, int w, int h, float velx, float vel currentFlipV = false; }; +// Destructor +MovingSprite::~MovingSprite() +{ + //std::cout << "Destruido MovingSprite" << std::endl; +} + // Reinicia todas las variables void MovingSprite::clear() { diff --git a/units/movingsprite.h b/units/movingsprite.h index 2c403c6..bc01e2a 100644 --- a/units/movingsprite.h +++ b/units/movingsprite.h @@ -42,6 +42,9 @@ public: // Constructor MovingSprite(float x = 0, float y = 0, int w = 0, int h = 0, float velx = 0, float vely = 0, float accelx = 0, float accely = 0, Texture *texture = nullptr, SDL_Renderer *renderer = nullptr); + // Destructor + ~MovingSprite(); + // Mueve el sprite void move(); diff --git a/units/notify.cpp b/units/notify.cpp index ad6a9fc..c6ff243 100644 --- a/units/notify.cpp +++ b/units/notify.cpp @@ -6,6 +6,8 @@ // Constructor Notify::Notify(SDL_Renderer *renderer, string iconFile, string bitmapFile, string textFile, string soundFile, options_t *options) { + //std::cout << "Construido Notify" << std::endl; + // Inicializa variables this->renderer = renderer; this->options = options; @@ -21,15 +23,29 @@ Notify::Notify(SDL_Renderer *renderer, string iconFile, string bitmapFile, strin // Destructor Notify::~Notify() { + //std::cout << "Destruido Notify" << std::endl; + // Libera la memoria de los objetos - delete iconTexture; - delete text; + if (iconTexture != nullptr) + { + delete iconTexture; + } + if (text != nullptr) + { + delete text; + } JA_DeleteSound(sound); for (auto notification : notifications) { - delete notification.sprite; - delete notification.texture; + if (notification.sprite != nullptr) + { + delete notification.sprite; + } + if (notification.texture != nullptr) + { + delete notification.texture; + } } } @@ -116,8 +132,14 @@ void Notify::clearFinishedNotifications() { if (notifications[i].state == ns_finished) { - delete notifications[i].sprite; - delete notifications[i].texture; + if (notifications[i].sprite != nullptr) + { + delete notifications[i].sprite; + } + if (notifications[i].texture != nullptr) + { + delete notifications[i].texture; + } notifications.erase(notifications.begin() + i); } } diff --git a/units/screen.cpp b/units/screen.cpp index bdc1fad..7110d25 100644 --- a/units/screen.cpp +++ b/units/screen.cpp @@ -5,13 +5,12 @@ // Constructor Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, options_t *options) { + //std::cout << "Construido Screen" << std::endl; // Inicializa variables this->window = window; this->renderer = renderer; this->options = options; - gameCanvasWidth = options->screen.nativeWidth; - gameCanvasHeight = options->screen.nativeHeight; borderWidth = options->screen.borderWidth * 2; borderHeight = options->screen.borderHeight * 2; @@ -22,7 +21,7 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, options_t *options) 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); + gameCanvas = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, options->screen.nativeWidth, options->screen.nativeHeight); if (gameCanvas == nullptr) { if (options->console) @@ -38,6 +37,7 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, options_t *options) // Destructor Screen::~Screen() { + //std::cout << "Destruido Screen" << std::endl; if (notify != nullptr) { delete notify; @@ -92,23 +92,25 @@ void Screen::setVideoMode(int videoMode) if (options->screen.borderEnabled) { - windowWidth = gameCanvasWidth + borderWidth; - windowHeight = gameCanvasHeight + borderHeight; - dest = {0 + (borderWidth / 2), 0 + (borderHeight / 2), gameCanvasWidth, gameCanvasHeight}; + windowWidth = options->screen.nativeWidth + borderWidth; + windowHeight = options->screen.nativeHeight + borderHeight; + dest = {0 + (borderWidth / 2), 0 + (borderHeight / 2), options->screen.nativeWidth, options->screen.nativeHeight}; } else { - //windowWidth = gameCanvasWidth; - windowWidth = gameCanvasWidth * options->screen.nativeZoom * options->screen.windowZoom; - //windowHeight = gameCanvasHeight; - windowHeight = gameCanvasHeight * options->screen.nativeZoom * options->screen.windowZoom; - //dest = {0, 0, gameCanvasWidth, gameCanvasHeight}; - dest = {0, 0, windowWidth, windowHeight}; + // windowWidth = options->screen.nativeWidth; + windowWidth = options->screen.nativeWidth * options->screen.nativeZoom * options->screen.windowZoom; + // windowHeight = options->screen.nativeHeight; + windowHeight = options->screen.nativeHeight * options->screen.nativeZoom * options->screen.windowZoom; + // dest = {0, 0, options->screen.nativeWidth, options->screen.nativeHeight}; + gameWidth = options->screen.nativeWidth * options->screen.nativeZoom; + gameHeight = options->screen.nativeHeight * options->screen.nativeZoom; + dest = {0, 0, gameWidth, gameHeight}; } // Modifica el tamaño de la ventana - //SDL_SetWindowSize(window, windowWidth * options->screen.windowZoom, windowHeight * options->screen.windowZoom); + // SDL_SetWindowSize(window, windowWidth * options->screen.windowZoom, windowHeight * options->screen.windowZoom); SDL_SetWindowSize(window, windowWidth, windowHeight); SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); } @@ -127,20 +129,20 @@ void Screen::setVideoMode(int videoMode) { // Calcula el tamaño de la escala máxima int scale = 0; - while (((gameCanvasWidth * (scale + 1)) <= windowWidth) && ((gameCanvasHeight * (scale + 1)) <= windowHeight)) + while (((options->screen.nativeWidth * (scale + 1)) <= windowWidth) && ((options->screen.nativeHeight * (scale + 1)) <= windowHeight)) { scale++; } - dest.w = gameCanvasWidth * scale; - dest.h = gameCanvasHeight * scale; + dest.w = options->screen.nativeWidth * scale; + dest.h = options->screen.nativeHeight * scale; dest.x = (windowWidth - dest.w) / 2; dest.y = (windowHeight - dest.h) / 2; } else if (options->screen.keepAspect) { - float ratio = (float)gameCanvasWidth / (float)gameCanvasHeight; - if ((windowWidth - gameCanvasWidth) >= (windowHeight - gameCanvasHeight)) + float ratio = (float)options->screen.nativeWidth / (float)options->screen.nativeHeight; + if ((windowWidth - options->screen.nativeWidth) >= (windowHeight - options->screen.nativeHeight)) { dest.h = windowHeight; dest.w = (int)((windowHeight * ratio) + 0.5f); @@ -164,7 +166,8 @@ void Screen::setVideoMode(int videoMode) } // Modifica el tamaño del renderizador - SDL_RenderSetLogicalSize(renderer, windowWidth, windowHeight); + // SDL_RenderSetLogicalSize(renderer, windowWidth, windowHeight); + SDL_RenderSetLogicalSize(renderer, gameWidth, gameHeight); // Actualiza las opciones options->screen.mode = videoMode; @@ -302,7 +305,7 @@ void Screen::renderFade() return; } - const SDL_Rect rect = {0, 0, gameCanvasWidth, gameCanvasHeight}; + const SDL_Rect rect = {0, 0, options->screen.nativeWidth, options->screen.nativeHeight}; color_t color = {0, 0, 0}; const float step = (float)fadeCounter / (float)fadeLenght; const int alpha = 0 + (255 - 0) * step; diff --git a/units/screen.h b/units/screen.h index bcc32ef..e7ad871 100644 --- a/units/screen.h +++ b/units/screen.h @@ -22,14 +22,14 @@ private: Notify *notify; // Dibuja notificaciones por pantalla // Variables - int windowWidth; // Ancho de la pantalla o ventana - int windowHeight; // Alto de la pantalla o ventana - int gameCanvasWidth; // Resolución interna del juego. Es el ancho de la textura donde se dibuja el juego - int gameCanvasHeight; // Resolución interna del juego. Es el alto de la textura donde se dibuja el juego - int borderWidth; // Anchura del borde - int borderHeight; // Anltura del borde - SDL_Rect dest; // Coordenadas donde se va a dibujar la textura del juego sobre la pantalla o ventana - color_t borderColor; // Color del borde añadido a la textura de juego para rellenar la pantalla + int borderWidth; // Anchura del borde que se añade a la resolución nativa del juego + int borderHeight; // Anltura del borde que se añade a la resolución nativa del juego + int gameWidth; // Resultado de multiplicar la resolución nativa (mas el borde) por el zoom nativo + int gameHeight; // Resultado de multiplicar la resolución nativa (mas el borde) por el zoom nativo + int windowWidth; // Resultado de multiplicar gameWidth por el zoom de la ventana + int windowHeight; // Resultado de multiplicar gameWidth por el zoom de la ventana + SDL_Rect dest; // Coordenadas donde se va a dibujar la textura del juego sobre la pantalla o ventana + color_t borderColor; // Color del borde añadido a la textura de juego para rellenar la pantalla // Variables - Efectos bool fade; // Indica si esta activo el efecto de fade diff --git a/units/sprite.cpp b/units/sprite.cpp index 0cdde12..3811377 100644 --- a/units/sprite.cpp +++ b/units/sprite.cpp @@ -1,8 +1,10 @@ #include "sprite.h" +#include // Constructor Sprite::Sprite(int x, int y, int w, int h, Texture *texture, SDL_Renderer *renderer) { + //std::cout << "Construido Sprite" << std::endl; // Establece la posición X,Y del sprite this->x = x; this->y = y; @@ -28,6 +30,7 @@ Sprite::Sprite(int x, int y, int w, int h, Texture *texture, SDL_Renderer *rende Sprite::Sprite(SDL_Rect rect, Texture *texture, SDL_Renderer *renderer) { + //std::cout << "Construido Sprite" << std::endl; // Establece la posición X,Y del sprite this->x = rect.x; this->y = rect.y; @@ -51,6 +54,12 @@ Sprite::Sprite(SDL_Rect rect, Texture *texture, SDL_Renderer *renderer) zoomH = 1.0f; } +// Destructor +Sprite::~Sprite() +{ + //std::cout << "Destruido Sprite" << std::endl; +} + // Muestra el sprite por pantalla void Sprite::render() { diff --git a/units/sprite.h b/units/sprite.h index c4e69a3..c625a8a 100644 --- a/units/sprite.h +++ b/units/sprite.h @@ -29,6 +29,9 @@ public: Sprite(int x = 0, int y = 0, int w = 0, int h = 0, Texture *texture = nullptr, SDL_Renderer *renderer = nullptr); Sprite(SDL_Rect rect, Texture *texture, SDL_Renderer *renderer); + // Destructor + ~Sprite(); + // Muestra el sprite por pantalla void render(); diff --git a/units/text.cpp b/units/text.cpp index af56872..c3267e0 100644 --- a/units/text.cpp +++ b/units/text.cpp @@ -79,6 +79,7 @@ textFile_t LoadTextFile(string file, bool verbose) // Constructor Text::Text(string textFile, string bitmapFile, SDL_Renderer *renderer) { + //std::cout << "Construido Text" << std::endl; // Carga los offsets desde el fichero textFile_t tf = LoadTextFile(textFile); @@ -104,6 +105,7 @@ Text::Text(string textFile, string bitmapFile, SDL_Renderer *renderer) // Constructor Text::Text(string textFile, Texture *texture, SDL_Renderer *renderer) { + //std::cout << "Construido Text" << std::endl; // Carga los offsets desde el fichero textFile_t tf = LoadTextFile(textFile); @@ -128,6 +130,7 @@ Text::Text(string textFile, Texture *texture, SDL_Renderer *renderer) // Constructor Text::Text(textFile_t *textFile, Texture *texture, SDL_Renderer *renderer) { + //std::cout << "Construido Text" << std::endl; // Inicializa variables desde la estructura boxHeight = textFile->boxHeight; boxWidth = textFile->boxWidth; @@ -149,8 +152,13 @@ Text::Text(textFile_t *textFile, Texture *texture, SDL_Renderer *renderer) // Destructor Text::~Text() { - delete sprite; - if (texture) + //std::cout << "Destruido Text" << std::endl; + if (sprite != nullptr) + { + delete sprite; + } + + if (texture != nullptr) { delete texture; } diff --git a/units/texture.cpp b/units/texture.cpp index dce9ed2..35dc87b 100644 --- a/units/texture.cpp +++ b/units/texture.cpp @@ -7,6 +7,7 @@ // Constructor Texture::Texture(SDL_Renderer *renderer, std::string path, bool verbose) { + //std::cout << "Construido Texture" << std::endl; // Copia punteros this->renderer = renderer; this->path = path; @@ -26,6 +27,8 @@ Texture::Texture(SDL_Renderer *renderer, std::string path, bool verbose) // Destructor Texture::~Texture() { + //std::cout << "Destruido Texture" << std::endl; + // Libera memoria unload(); }