diff --git a/data/music.ogg b/data/music.ogg new file mode 100644 index 0000000..1593ade Binary files /dev/null and b/data/music.ogg differ diff --git a/data/smb2.png b/data/smb2.png new file mode 100644 index 0000000..7f71dd5 Binary files /dev/null and b/data/smb2.png differ diff --git a/data/smb2.txt b/data/smb2.txt new file mode 100644 index 0000000..a9c8655 --- /dev/null +++ b/data/smb2.txt @@ -0,0 +1,194 @@ +# box width +8 +# box height +8 +# 32 espacio ( ) +7 +# 33 ! +7 +# 34 " +7 +# 35 # +7 +# 36 $ +7 +# 37 % +7 +# 38 & +7 +# 39 ' +7 +# 40 ( +7 +# 41 ) +7 +# 42 * +7 +# 43 + +7 +# 44 , +7 +# 45 - +7 +# 46 . +7 +# 47 / +7 +# 48 0 +7 +# 49 1 +7 +# 50 2 +7 +# 51 3 +7 +# 52 4 +7 +# 53 5 +7 +# 54 6 +7 +# 55 7 +7 +# 56 8 +7 +# 57 9 +7 +# 58 : +7 +# 59 ; +7 +# 60 < +7 +# 61 = +7 +# 62 > +7 +# 63 ? +7 +# 64 @ +7 +# 65 A +7 +# 66 B +7 +# 67 C +7 +# 68 D +7 +# 69 E +7 +# 70 F +7 +# 71 G +7 +# 72 H +7 +# 73 I +7 +# 74 J +7 +# 75 K +7 +# 76 L +7 +# 77 M +7 +# 78 N +7 +# 79 O +7 +# 80 P +7 +# 81 Q +7 +# 82 R +7 +# 83 S +7 +# 84 T +7 +# 85 U +7 +# 86 V +7 +# 87 W +7 +# 88 X +7 +# 89 Y +7 +# 90 Z +7 +# 91 [ +7 +# 92 \ +7 +# 93 ] +7 +# 94 ^ +7 +# 95 _ +7 +# 96 ` +7 +# 97 a +7 +# 98 b +7 +# 99 c +7 +# 100 d +7 +# 101 e +7 +# 102 f +7 +# 103 g +7 +# 104 h +7 +# 105 i +7 +# 106 j +7 +# 107 k +7 +# 108 l +7 +# 109 m +7 +# 110 n +7 +# 111 o +7 +# 112 p +7 +# 113 q +7 +# 114 r +7 +# 115 s +7 +# 116 t +7 +# 117 u +7 +# 118 v +7 +# 119 w +7 +# 120 x +7 +# 121 y +7 +# 122 z +7 +# 123 { +7 +# 124 | +7 +# 125 } +7 +# 126 ~ +7 \ No newline at end of file diff --git a/data/sound.wav b/data/sound.wav new file mode 100644 index 0000000..7475499 Binary files /dev/null and b/data/sound.wav differ diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..9263e98 --- /dev/null +++ b/main.cpp @@ -0,0 +1,94 @@ +/* + +Ejemplo de uso de las unidades incluídas en jail_engine +Código fuente creado por JailDesigner + +*/ + +#include +#include +#include "units/jail_audio.h" +#include "units/text.h" +#include "units/utils.h" + +SDL_Event *event; +SDL_Window *window; +SDL_Renderer *renderer; + +int main(int argc, char *argv[]) +{ + + // Inicializa las opciones + struct options_t *options = new options_t; + options->gameWidth = 640; + options->gameHeight = 480; + + // Inicializa SDL y la ventana + SDL_Init(SDL_INIT_EVERYTHING); + window = SDL_CreateWindow("jail_engine_demo", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, options->gameWidth, options->gameHeight, SDL_WINDOW_SHOWN); + if (window != nullptr) + { + renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); + } + event = new SDL_Event(); + + // Inicializa jail_audio + JA_Init(48000, AUDIO_S16, 2); + + JA_Music_t *music; + JA_Sound_t *peiv; + + music = JA_LoadMusic("data/music.ogg"); + peiv = JA_LoadSound("data/sound.wav"); + int volume = 128; + + // Inicializa el texto + Text *text = new Text("data/smb2.txt", "data/smb2.png", renderer); + + // Bucle principal + JA_PlayMusic(music, true); + bool should_exit = false; + while (!should_exit) + { + + // Actualiza la lógica del programa + while (SDL_PollEvent(event)) + { + if (event->type == SDL_QUIT) + { + should_exit = true; + break; + } + if (event->type == SDL_KEYDOWN) + { + switch (event->key.keysym.scancode) + { + + default: + break; + } + } + } + + // Dibuja en pantalla + SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF); + SDL_RenderClear(renderer); + text->writeCentered(options->gameWidth / 2 ,text->getCharacterSize(),"Jail Engine DEMO"); + SDL_RenderPresent(renderer); + } + + // Finaliza el texto + delete text; + + // Finaliza jail_audio + JA_DeleteSound(peiv); + JA_DeleteMusic(music); + + // Finaliza SDL y la ventana + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + delete event; + SDL_Quit(); + + return 0; +} diff --git a/resource.cpp b/resource.cpp deleted file mode 100644 index 3400fb1..0000000 --- a/resource.cpp +++ /dev/null @@ -1,337 +0,0 @@ -#include "resource.h" -#include - -// Constructor -Resource::Resource(SDL_Renderer *renderer, Asset *asset, options_t *options) -{ - this->renderer = renderer; - this->asset = asset; - this->options = options; -} - -// Carga las texturas de una lista -void Resource::loadTextures(std::vector list) -{ - for (auto l : list) - { - if (options->console) - { - std::cout << "\nLOAD TEXTURE: " << l << std::endl; - std::cout << "png: " << asset->get(l) << std::endl; - } - - res_texture_t t; - t.name = l; - t.texture = new Texture(renderer, asset->get(t.name), options->console); - textures.push_back(t); - } -} - -// Vuelve a cargar las texturas -void Resource::reLoadTextures() -{ - for (auto texture : textures) - { - texture.texture->reLoad(); - } -} - -// Carga las animaciones desde una lista -void Resource::loadAnimations(std::vector list) -{ - for (auto l : list) - { - // Extrae el nombre del fichero sin la extension para crear el nombre del fichero de la textura - const std::string pngFile = l.substr(0, l.find_last_of(".")) + ".png"; - - if (options->console) - { - std::cout << "\nLOAD ANIMATION: " << l << std::endl; - std::cout << "png: " << asset->get(pngFile) << std::endl; - std::cout << "ani: " << asset->get(l) << std::endl; - } - - res_animation_t as; - as.name = l; - as.animation = new animatedSprite_t(loadAnimationFromFile(getTexture(pngFile), asset->get(as.name), options->console)); - animations.push_back(as); - } -} - -// Vuelve a cargar las animaciones -void Resource::reLoadAnimations() -{ - // reLoadTextures(); - - for (auto &a : animations) - { - // Extrae el nombre del fichero sin la extension para crear el nombre del fichero de la textura - const std::string pngFile = a.name.substr(0, a.name.find_last_of(".")) + ".png"; - delete a.animation; - a.animation = new animatedSprite_t(loadAnimationFromFile(getTexture(pngFile), asset->get(a.name), options->console)); - } -} - -// Carga los offsets desde una lista -void Resource::loadOffsets(std::vector list) -{ - for (auto l : list) - { - res_textOffset_t to; - to.name = l; - to.textFile = new textFile_t(LoadTextFile(asset->get(l), options->console)); - offsets.push_back(to); - } -} - -// Vuelve a cargar los offsets -void Resource::reLoadOffsets() -{ - for (auto &o : offsets) - { - delete o.textFile; - o.textFile = new textFile_t(LoadTextFile(asset->get(o.name), options->console)); - } -} - -// Carga los mapas de tiles desde una lista -void Resource::loadTileMaps(std::vector list) -{ - for (auto l : list) - { - res_tileMap_t tm; - tm.name = l; - tm.tileMap = new std::vector(loadRoomTileFile(asset->get(l), options->console)); - tileMaps.push_back(tm); - } -} - -// Vuelve a cargar los mapas de tiles -void Resource::reLoadTileMaps() -{ - for (auto &tm : tileMaps) - { - delete tm.tileMap; - tm.tileMap = new std::vector(loadRoomTileFile(asset->get(tm.name), options->console)); - } -} - -// Carga las habitaciones desde una lista -void Resource::loadRooms(std::vector list) -{ - for (auto l : list) - { - res_room_t r; - r.name = l; - r.room = new room_t(loadRoomFile(asset->get(l), options->console)); - r.room->tileMap = getTileMap(r.room->tileMapFile); - for (auto &e : r.room->enemies) - { - e.animation = getAnimation(e.animationString); - } - for (auto &i : r.room->items) - { - i.texture = getTexture(i.tileSetFile); - } - r.room->textureA = getTexture("standard.png"); - r.room->textureB = getTexture("standard_zxarne.png"); - rooms.push_back(r); - } -} - -// Vuelve a cargar las habitaciones -void Resource::reLoadRooms() -{ - reLoadTileMaps(); - - for (auto &r : rooms) - { - delete r.room; - r.room = new room_t(loadRoomFile(asset->get(r.name))); - r.room->tileMap = getTileMap(r.room->tileMapFile); - for (auto &e : r.room->enemies) - { - e.animation = getAnimation(e.animationString); - } - for (auto &i : r.room->items) - { - i.texture = getTexture(i.tileSetFile); - } - r.room->textureA = getTexture("standard.png"); - r.room->textureB = getTexture("standard_zxarne.png"); - } -} - -// Vuelve a cargar todos los recursos -void Resource::reLoad() -{ - reLoadAnimations(); - reLoadOffsets(); - reLoadRooms(); -} - -// Libera las texturas -void Resource::freeTextures() -{ - for (auto texture : textures) - { - delete texture.texture; - } - textures.clear(); -} - -// Libera las animaciones -void Resource::freeAnimations() -{ - for (auto a : animations) - { - delete a.animation; - } - animations.clear(); -} - -// Libera los offsets -void Resource::freeOffsets() -{ - for (auto o : offsets) - { - delete o.textFile; - } - offsets.clear(); -} - -// Libera los mapas de tiles -void Resource::freeTileMaps() -{ - for (auto t : tileMaps) - { - delete t.tileMap; - } - tileMaps.clear(); -} - -// Libera las habitaciones -void Resource::freeRooms() -{ - for (auto r : rooms) - { - delete r.room; - } - rooms.clear(); -} - -// Libera todos los recursos -void Resource::free() -{ - freeTextures(); - freeAnimations(); - freeOffsets(); - freeTileMaps(); - freeRooms(); -} - -// Obtiene una textura -Texture *Resource::getTexture(std::string name) -{ - for (auto texture : textures) - { - // if (texture.name.find(name) != std::string::npos) - if (texture.name == name) - { - // std::cout << "\nTEXTURE REQUESTED: " << name << std::endl; - // std::cout << "served: " << texture.name << std::endl; - - return texture.texture; - } - } - - if (options->console) - { - std::cout << "NOT FOUND ON CACHE: " << name << std::endl; - } - return nullptr; -} - -// Obtiene una animación -animatedSprite_t *Resource::getAnimation(std::string name) -{ - for (auto animation : animations) - { - // if (animation.name.find(name) != std::string::npos) - if (animation.name == name) - { - // std::cout << "\nANIMATION REQUESTED: " << name << std::endl; - // std::cout << "served: " << animation.name << std::endl; - return animation.animation; - } - } - - if (options->console) - { - std::cout << "NOT FOUND ON CACHE: " << name << std::endl; - } - return nullptr; -} - -// Obtiene un offset -textFile_t *Resource::getOffset(std::string name) -{ - for (auto offset : offsets) - { - // if (offset.name.find(name) != std::string::npos) - if (offset.name == name) - { - return offset.textFile; - } - } - - if (options->console) - { - std::cout << "NOT FOUND ON CACHE: " << name << std::endl; - } - return nullptr; -} - -// Obtiene un mapa de tiles -std::vector *Resource::getTileMap(std::string name) -{ - for (auto tileMap : tileMaps) - { - // if (tileMap.name.find(name) != std::string::npos) - if (tileMap.name == name) - { - return tileMap.tileMap; - } - } - - if (options->console) - { - std::cout << "NOT FOUND ON CACHE: " << name << std::endl; - } - return nullptr; -} - -// Obtiene una habitacion -room_t *Resource::getRoom(std::string name) -{ - for (auto room : rooms) - { - // if (room.name.find(name) != std::string::npos) - if (room.name == name) - { - return room.room; - } - } - - if (options->console) - { - std::cout << "NOT FOUND ON CACHE: " << name << std::endl; - } - return nullptr; -} - -// Obtiene todas las habitaciones -std::vector *Resource::getAllRooms() -{ - return &rooms; -} \ No newline at end of file diff --git a/resource.h b/resource.h deleted file mode 100644 index bcc96c4..0000000 --- a/resource.h +++ /dev/null @@ -1,136 +0,0 @@ -#pragma once - -#include -#include "animatedsprite.h" -#include "asset.h" -#include "../room.h" -#include "text.h" -#include "texture.h" -#include "utils.h" -#include -#include - -#ifndef RESOURCE_H -#define RESOURCE_H - -struct res_texture_t -{ - std::string name; // Nombre de la textura - Texture *texture; // La textura -}; - -struct res_animation_t -{ - std::string name; // Nombre de la textura - animatedSprite_t *animation; // La animación -}; - -struct res_textOffset_t -{ - std::string name; // Nombre del offset - textFile_t *textFile; // Los offsets de la fuente -}; - -struct res_tileMap_t -{ - std::string name; // Nombre del mapa de tiles - std::vector *tileMap; // Vector con los indices del mapa de tiles -}; - -struct res_room_t -{ - std::string name; // Nombre de la habitación - room_t *room; // Vector con las habitaciones -}; - -// Clase Resource. Almacena recursos de disco en memoria -class Resource -{ -private: - // Objetos y punteros - SDL_Renderer *renderer; // El renderizador de la ventana - Asset *asset; // Objeto con la ruta a todos los ficheros de recursos - options_t *options; // Puntero a las opciones del juego - - // Variables - std::vector textures; - std::vector animations; - std::vector offsets; - std::vector tileMaps; - std::vector rooms; - -public: - // Constructor - Resource(SDL_Renderer *renderer, Asset *asset, options_t *options); - - // Carga las texturas de una lista - void loadTextures(std::vector list); - - // Vuelve a cargar las texturas - void reLoadTextures(); - - // Carga las animaciones desde una lista - void loadAnimations(std::vector list); - - // Vuelve a cargar las animaciones - void reLoadAnimations(); - - // Carga los offsets desde una lista - void loadOffsets(std::vector list); - - // Vuelve a cargar los offsets - void reLoadOffsets(); - - // Carga los mapas de tiles desde una lista - void loadTileMaps(std::vector list); - - // Vuelve a cargar los mapas de tiles - void reLoadTileMaps(); - - // Carga las habitaciones desde una lista - void loadRooms(std::vector list); - - // Vuelve a cargar las habitaciones - void reLoadRooms(); - - // Vuelve a cargar todos los recursos - void reLoad(); - - // Libera las texturas - void freeTextures(); - - // Libera las animaciones - void freeAnimations(); - - // Libera los offsets - void freeOffsets(); - - // Libera los mapas de tiles - void freeTileMaps(); - - // Libera las habitaciones - void freeRooms(); - - // Libera todos los recursos - void free(); - - // Obtiene una textura - Texture *getTexture(std::string name); - - // Obtiene una animación - animatedSprite_t *getAnimation(std::string name); - - // Obtiene un offset - textFile_t *getOffset(std::string name); - - // Obtiene un mapa de tiles - std::vector *getTileMap(std::string name); - - // Obtiene una habitacion - room_t *getRoom(std::string name); - - // Obtiene todas las habitaciones - std::vector *getAllRooms(); -}; - -#endif diff --git a/animatedsprite.cpp b/units/animatedsprite.cpp similarity index 100% rename from animatedsprite.cpp rename to units/animatedsprite.cpp diff --git a/animatedsprite.h b/units/animatedsprite.h similarity index 100% rename from animatedsprite.h rename to units/animatedsprite.h diff --git a/asset.cpp b/units/asset.cpp similarity index 100% rename from asset.cpp rename to units/asset.cpp diff --git a/asset.h b/units/asset.h similarity index 100% rename from asset.h rename to units/asset.h diff --git a/debug.cpp b/units/debug.cpp similarity index 100% rename from debug.cpp rename to units/debug.cpp diff --git a/debug.h b/units/debug.h similarity index 98% rename from debug.h rename to units/debug.h index 3d6f396..6546ab1 100644 --- a/debug.h +++ b/units/debug.h @@ -1,7 +1,6 @@ #pragma once #include -#include "../const.h" #include "asset.h" #include "screen.h" #include "text.h" diff --git a/input.cpp b/units/input.cpp similarity index 100% rename from input.cpp rename to units/input.cpp diff --git a/input.h b/units/input.h similarity index 100% rename from input.h rename to units/input.h diff --git a/jail_audio.cpp b/units/jail_audio.cpp similarity index 100% rename from jail_audio.cpp rename to units/jail_audio.cpp diff --git a/jail_audio.h b/units/jail_audio.h similarity index 100% rename from jail_audio.h rename to units/jail_audio.h diff --git a/jail_audio_sdlmixer.cpp b/units/jail_audio_sdlmixer.cpp similarity index 100% rename from jail_audio_sdlmixer.cpp rename to units/jail_audio_sdlmixer.cpp diff --git a/jscore.cpp b/units/jscore.cpp similarity index 100% rename from jscore.cpp rename to units/jscore.cpp diff --git a/jscore.h b/units/jscore.h similarity index 100% rename from jscore.h rename to units/jscore.h diff --git a/menu.cpp b/units/menu.cpp similarity index 98% rename from menu.cpp rename to units/menu.cpp index f29f8ac..f072dc7 100644 --- a/menu.cpp +++ b/units/menu.cpp @@ -1,8 +1,8 @@ -#include "../const.h" #include "menu.h" // Constructor -Menu::Menu(SDL_Renderer *renderer, Resource *resource, Asset *asset, Input *input, std::string file) +//Menu::Menu(SDL_Renderer *renderer, Resource *resource, Asset *asset, Input *input, std::string file) +Menu::Menu(SDL_Renderer *renderer, Asset *asset, Input *input, std::string file) { // Copia punteros this->renderer = renderer; @@ -154,7 +154,7 @@ bool Menu::load(std::string file_path) // Crea el objeto text tan pronto como se pueda. Necesario para añadir items if (font_png != "" && font_txt != "" && !textAllocated) { - text = new Text(resource->getOffset(font_txt), resource->getTexture(font_png), renderer); + text = new Text(asset->get(font_txt), asset->get(font_png), renderer); textAllocated = true; } } @@ -977,6 +977,6 @@ void Menu::setText(std::string font_png, std::string font_txt) { if (!text) { - text = new Text(resource->getOffset(font_txt), resource->getTexture(font_png), renderer); + text = new Text(asset->get(font_txt), asset->get(font_png), renderer); } } \ No newline at end of file diff --git a/menu.h b/units/menu.h similarity index 96% rename from menu.h rename to units/menu.h index 6164b7c..a459c4c 100644 --- a/menu.h +++ b/units/menu.h @@ -4,13 +4,14 @@ #include "asset.h" #include "input.h" #include "jail_audio.h" -#include "resource.h" +//#include "resource.h" #include "sprite.h" #include "text.h" #include "utils.h" #include #include #include +#include #ifndef MENU_H #define MENU_H @@ -71,7 +72,7 @@ private: // Objetos y punteros SDL_Renderer *renderer; // Puntero al renderizador de la ventana - Resource *resource; // Objeto con los recursos + //Resource *resource; // Objeto con los recursos Asset *asset; // Objeto para gestionar los ficheros de recursos Text *text; // Texto para poder escribir los items del menu Input *input; // Gestor de eventos de entrada de teclado o gamepad @@ -142,7 +143,8 @@ private: public: // Constructor - Menu(SDL_Renderer *renderer, Resource *resource, Asset *asset, Input *input, std::string file = ""); + //Menu(SDL_Renderer *renderer, Resource *resource, Asset *asset, Input *input, std::string file = ""); + Menu(SDL_Renderer *renderer, Asset *asset, Input *input, std::string file = ""); // Destructor ~Menu(); diff --git a/movingsprite.cpp b/units/movingsprite.cpp similarity index 99% rename from movingsprite.cpp rename to units/movingsprite.cpp index 80e4fba..faf429a 100644 --- a/movingsprite.cpp +++ b/units/movingsprite.cpp @@ -1,4 +1,4 @@ -#include "../const.h" + #include "movingsprite.h" // Constructor diff --git a/movingsprite.h b/units/movingsprite.h similarity index 100% rename from movingsprite.h rename to units/movingsprite.h diff --git a/notify.cpp b/units/notify.cpp similarity index 100% rename from notify.cpp rename to units/notify.cpp diff --git a/notify.h b/units/notify.h similarity index 100% rename from notify.h rename to units/notify.h diff --git a/screen.cpp b/units/screen.cpp similarity index 100% rename from screen.cpp rename to units/screen.cpp diff --git a/screen.h b/units/screen.h similarity index 99% rename from screen.h rename to units/screen.h index 8842d65..a35fd5b 100644 --- a/screen.h +++ b/units/screen.h @@ -4,7 +4,6 @@ #include "asset.h" #include "notify.h" #include "utils.h" -#include "../const.h" #include #ifndef SCREEN_H diff --git a/sprite.cpp b/units/sprite.cpp similarity index 100% rename from sprite.cpp rename to units/sprite.cpp diff --git a/sprite.h b/units/sprite.h similarity index 100% rename from sprite.h rename to units/sprite.h diff --git a/stb_image.h b/units/stb_image.h similarity index 100% rename from stb_image.h rename to units/stb_image.h diff --git a/stb_vorbis.c b/units/stb_vorbis.c similarity index 100% rename from stb_vorbis.c rename to units/stb_vorbis.c diff --git a/text.cpp b/units/text.cpp similarity index 71% rename from text.cpp rename to units/text.cpp index e420e43..8b713d7 100644 --- a/text.cpp +++ b/units/text.cpp @@ -4,7 +4,7 @@ #include // Llena una estructuta textFile_t desde un fichero -textFile_t LoadTextFile(std::string file, bool verbose) +textFile_t LoadTextFile(string file, bool verbose) { textFile_t tf; @@ -17,31 +17,31 @@ textFile_t LoadTextFile(std::string file, bool verbose) } // Abre el fichero para leer los valores - const std::string filename = file.substr(file.find_last_of("\\/") + 1).c_str(); - std::ifstream rfile(file); + const string filename = file.substr(file.find_last_of("\\/") + 1).c_str(); + ifstream rfile(file); if (rfile.is_open() && rfile.good()) { - std::string buffer; + string buffer; // Lee los dos primeros valores del fichero - std::getline(rfile, buffer); - std::getline(rfile, buffer); - tf.boxWidth = std::stoi(buffer); + getline(rfile, buffer); + getline(rfile, buffer); + tf.boxWidth = stoi(buffer); - std::getline(rfile, buffer); - std::getline(rfile, buffer); - tf.boxHeight = std::stoi(buffer); + getline(rfile, buffer); + getline(rfile, buffer); + tf.boxHeight = stoi(buffer); // lee el resto de datos del fichero int index = 32; int line_read = 0; - while (std::getline(rfile, buffer)) + while (getline(rfile, buffer)) { // Almacena solo las lineas impares if (line_read % 2 == 1) { - tf.offset[index++].w = std::stoi(buffer); + tf.offset[index++].w = stoi(buffer); } // Limpia el buffer @@ -52,7 +52,7 @@ textFile_t LoadTextFile(std::string file, bool verbose) // Cierra el fichero if (verbose) { - std::cout << "Text loaded: " << filename.c_str() << std::endl; + cout << "Text loaded: " << filename.c_str() << endl; } rfile.close(); } @@ -62,7 +62,7 @@ textFile_t LoadTextFile(std::string file, bool verbose) { if (verbose) { - std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl; + cout << "Warning: Unable to open " << filename.c_str() << " file" << endl; } } @@ -77,7 +77,31 @@ textFile_t LoadTextFile(std::string file, bool verbose) } // Constructor -Text::Text(std::string textFile, Texture *texture, SDL_Renderer *renderer) +Text::Text(string textFile, string bitmapFile, SDL_Renderer *renderer) +{ + // Carga los offsets desde el fichero + textFile_t tf = LoadTextFile(textFile); + + // Inicializa variables desde la estructura + boxHeight = tf.boxHeight; + boxWidth = tf.boxWidth; + for (int i = 0; i < 128; ++i) + { + offset[i].x = tf.offset[i].x; + offset[i].y = tf.offset[i].y; + offset[i].w = tf.offset[i].w; + } + + // Crea los objetos + texture = new Texture(renderer, bitmapFile); + sprite = new Sprite({0, 0, boxWidth, boxHeight}, texture, renderer); + + // Inicializa variables + fixedWidth = false; +} + +// Constructor +Text::Text(string textFile, Texture *texture, SDL_Renderer *renderer) { // Carga los offsets desde el fichero textFile_t tf = LoadTextFile(textFile); @@ -123,10 +147,14 @@ Text::Text(textFile_t *textFile, Texture *texture, SDL_Renderer *renderer) Text::~Text() { delete sprite; + if (texture) + { + delete texture; + } } // Escribe texto en pantalla -void Text::write(int x, int y, std::string text, int kerning, int lenght) +void Text::write(int x, int y, string text, int kerning, int lenght) { int shift = 0; @@ -145,7 +173,7 @@ void Text::write(int x, int y, std::string text, int kerning, int lenght) } // Escribe el texto con colores -void Text::writeColored(int x, int y, std::string text, color_t color, int kerning, int lenght) +void Text::writeColored(int x, int y, string text, color_t color, int kerning, int lenght) { sprite->getTexture()->setColor(color.r, color.g, color.b); write(x, y, text, kerning, lenght); @@ -153,7 +181,7 @@ void Text::writeColored(int x, int y, std::string text, color_t color, int kerni } // Escribe el texto con sombra -void Text::writeShadowed(int x, int y, std::string text, color_t color, Uint8 shadowDistance, int kerning, int lenght) +void Text::writeShadowed(int x, int y, string text, color_t color, Uint8 shadowDistance, int kerning, int lenght) { sprite->getTexture()->setColor(color.r, color.g, color.b); write(x + shadowDistance, y + shadowDistance, text, kerning, lenght); @@ -162,14 +190,14 @@ void Text::writeShadowed(int x, int y, std::string text, color_t color, Uint8 sh } // Escribe el texto centrado en un punto x -void Text::writeCentered(int x, int y, std::string text, int kerning, int lenght) +void Text::writeCentered(int x, int y, 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) +void Text::writeDX(Uint8 flags, int x, int y, 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); @@ -211,7 +239,7 @@ void Text::writeDX(Uint8 flags, int x, int y, std::string text, int kerning, col } // Obtiene la longitud en pixels de una cadena -int Text::lenght(std::string text, int kerning) +int Text::lenght(string text, int kerning) { int shift = 0; diff --git a/text.h b/units/text.h similarity index 61% rename from text.h rename to units/text.h index f1ae260..7946e59 100644 --- a/text.h +++ b/units/text.h @@ -26,14 +26,15 @@ struct textFile_t }; // Llena una estructuta textFile_t desde un fichero -textFile_t LoadTextFile(std::string file, bool verbose = false); +textFile_t LoadTextFile(string file, bool verbose = false); // Clase texto. Pinta texto en pantalla a partir de un bitmap class Text { private: // Objetos y punteros - Sprite *sprite; // Objeto con los graficos para el texto + Texture *texture; // Textura con los gráficos para el texto + Sprite *sprite; // Objeto para dibujar el texto // Variables int boxWidth; // Anchura de la caja de cada caracter en el png @@ -43,29 +44,30 @@ private: public: // Constructor - Text(std::string textFile, Texture *texture, SDL_Renderer *renderer); + Text(string textFile, string bitmapFile, SDL_Renderer *renderer); + Text(string textFile, Texture *texture, SDL_Renderer *renderer); Text(textFile_t *textFile, Texture *texture, 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); + void write(int x, int y, 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); + void writeColored(int x, int y, 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); + void writeShadowed(int x, int y, 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); + void writeCentered(int x, int y, 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); + void writeDX(Uint8 flags, int x, int y, 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); + int lenght(string text, int kerning = 1); // Devuelve el valor de la variable int getCharacterSize(); diff --git a/texture.cpp b/units/texture.cpp similarity index 100% rename from texture.cpp rename to units/texture.cpp diff --git a/texture.h b/units/texture.h similarity index 100% rename from texture.h rename to units/texture.h diff --git a/utils.cpp b/units/utils.cpp similarity index 98% rename from utils.cpp rename to units/utils.cpp index 7f6fae7..c2d109a 100644 --- a/utils.cpp +++ b/units/utils.cpp @@ -348,7 +348,7 @@ bool checkCollision(SDL_Point &p, d_line_t &l) } // Devuelve un color_t a partir de un string -color_t stringToColor(palette_e pal, std::string str) +color_t stringToColor(palette_e pal, string str) { if (pal == p_zxspectrum) { @@ -520,7 +520,7 @@ color_t stringToColor(palette_e pal, std::string str) } // Convierte una cadena en un valor booleano -bool stringToBool(std::string str) +bool stringToBool(string str) { if (str == "true") { @@ -533,7 +533,7 @@ bool stringToBool(std::string str) } // Convierte un valor booleano en una cadena -std::string boolToString(bool value) +string boolToString(bool value) { if (value) { diff --git a/utils.h b/units/utils.h similarity index 90% rename from utils.h rename to units/utils.h index 2dd9e2b..512d4d7 100644 --- a/utils.h +++ b/units/utils.h @@ -7,6 +7,8 @@ #ifndef UTILS_H #define UTILS_H +using namespace std; + // Estructura para definir un circulo struct circle_t { @@ -102,10 +104,10 @@ struct online_t { bool enabled; // Indica si se quiere usar el modo online o no bool sessionEnabled; // Indica ya se ha hecho login - std::string server; // Servidor para los servicios online + string server; // Servidor para los servicios online int port; // Puerto del servidor - std::string gameID; // Identificador del juego para los servicios online - std::string jailerID; // Identificador del jugador para los servicios online + string gameID; // Identificador del juego para los servicios online + string jailerID; // Identificador del jugador para los servicios online int score; // Puntuación almacenada online }; @@ -114,7 +116,7 @@ struct op_stats_t { int rooms; // Cantidad de habitaciones visitadas int items; // Cantidad de items obtenidos - std::string worstNightmare; // Habitación con más muertes acumuladas + string worstNightmare; // Habitación con más muertes acumuladas }; // Estructura con opciones de la pantalla @@ -127,7 +129,7 @@ struct op_screen_t // Estructura con todas las opciones de configuración del programa struct options_t { - std::string configVersion; // Versión del programa. Sirve para saber si las opciones son compatibles + string configVersion; // Versión del programa. Sirve para saber si las opciones son compatibles Uint32 videoMode; // Contiene el valor del modo de pantalla completa int windowSize; // Contiene el valor por el que se multiplica el tamaño de la ventana Uint32 filter; // Filtro usado para el escalado de la imagen @@ -186,13 +188,13 @@ bool checkCollision(SDL_Point &p, d_line_t &l); void normalizeLine(d_line_t &l); // Devuelve un color_t a partir de un string -color_t stringToColor(palette_e pal, std::string str); +color_t stringToColor(palette_e pal, string str); // Convierte una cadena en un valor booleano -bool stringToBool(std::string str); +bool stringToBool(string str); // Convierte un valor booleano en una cadena -std::string boolToString(bool value); +string boolToString(bool value); // Compara dos colores bool colorAreEqual(color_t color1, color_t color2);