diff --git a/source/common/dbgtxt.h b/source/common/dbgtxt.h new file mode 100644 index 0000000..e4444ae --- /dev/null +++ b/source/common/dbgtxt.h @@ -0,0 +1,6 @@ +#pragma once + +#include + +void dbg_init(SDL_Renderer *renderer); +void dbg_print(int x, int y, const char *text, Uint8 r, Uint8 g, Uint8 b); diff --git a/source/common/dbhtxt.cpp b/source/common/dbhtxt.cpp new file mode 100644 index 0000000..1a9319c --- /dev/null +++ b/source/common/dbhtxt.cpp @@ -0,0 +1,42 @@ +#include "dbgtxt.h" + +namespace +{ + SDL_Texture *dbg_tex = NULL; + SDL_Renderer *dbg_ren = NULL; +} + +void dbg_init(SDL_Renderer *renderer) +{ + dbg_ren = renderer; + Uint8 font[448] = {0x42, 0x4D, 0xC0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x82, 0x01, 0x00, 0x00, 0x12, 0x0B, 0x00, 0x00, 0x12, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x18, 0xF3, 0x83, 0x83, 0xCF, 0x83, 0x87, 0x00, 0x00, 0xF3, 0x39, 0x39, 0xCF, 0x79, 0xF3, 0x00, 0x00, 0x01, 0xF9, 0x39, 0xCF, 0x61, 0xF9, 0x00, 0x00, 0x33, 0xF9, 0x03, 0xE7, 0x87, 0x81, 0x00, 0x00, 0x93, 0x03, 0x3F, 0xF3, 0x1B, 0x39, 0x00, 0x00, 0xC3, 0x3F, 0x9F, 0x39, 0x3B, 0x39, 0x00, 0x41, 0xE3, 0x03, 0xC3, 0x01, 0x87, 0x83, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xE7, 0x01, 0xC7, 0x81, 0x01, 0x83, 0x00, 0x00, 0xE7, 0x1F, 0x9B, 0xE7, 0x1F, 0x39, 0x00, 0x00, 0xE7, 0x8F, 0x39, 0xE7, 0x87, 0xF9, 0x00, 0x00, 0xC3, 0xC7, 0x39, 0xE7, 0xC3, 0xC3, 0x00, 0x00, 0x99, 0xE3, 0x39, 0xE7, 0xF1, 0xE7, 0x00, 0x00, 0x99, 0xF1, 0xB3, 0xC7, 0x39, 0xF3, 0x00, 0x00, 0x99, 0x01, 0xC7, 0xE7, 0x83, 0x81, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x83, 0xE7, 0x83, 0xEF, 0x39, 0x39, 0x00, 0x00, 0x39, 0xE7, 0x39, 0xC7, 0x11, 0x11, 0x00, 0x00, 0xF9, 0xE7, 0x39, 0x83, 0x01, 0x83, 0x00, 0x00, 0x83, 0xE7, 0x39, 0x11, 0x01, 0xC7, 0x00, 0x00, 0x3F, 0xE7, 0x39, 0x39, 0x29, 0x83, 0x00, 0x00, 0x33, 0xE7, 0x39, 0x39, 0x39, 0x11, 0x00, 0x00, 0x87, 0x81, 0x39, 0x39, 0x39, 0x39, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x39, 0x39, 0x83, 0x3F, 0x85, 0x31, 0x00, 0x00, 0x39, 0x31, 0x39, 0x3F, 0x33, 0x23, 0x00, 0x00, 0x29, 0x21, 0x39, 0x03, 0x21, 0x07, 0x00, 0x00, 0x01, 0x01, 0x39, 0x39, 0x39, 0x31, 0x00, 0x00, 0x01, 0x09, 0x39, 0x39, 0x39, 0x39, 0x00, 0x00, 0x11, 0x19, 0x39, 0x39, 0x39, 0x39, 0x00, 0x00, 0x39, 0x39, 0x83, 0x03, 0x83, 0x03, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xC1, 0x39, 0x81, 0x83, 0x31, 0x01, 0x00, 0x00, 0x99, 0x39, 0xE7, 0x39, 0x23, 0x3F, 0x00, 0x00, 0x39, 0x39, 0xE7, 0xF9, 0x07, 0x3F, 0x00, 0x00, 0x31, 0x01, 0xE7, 0xF9, 0x0F, 0x3F, 0x00, 0x00, 0x3F, 0x39, 0xE7, 0xF9, 0x27, 0x3F, 0x00, 0x00, 0x9F, 0x39, 0xE7, 0xF9, 0x33, 0x3F, 0x00, 0x00, 0xC1, 0x39, 0x81, 0xF9, 0x39, 0x3F, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x39, 0x03, 0xC3, 0x07, 0x01, 0x3F, 0x00, 0x00, 0x39, 0x39, 0x99, 0x33, 0x3F, 0x3F, 0x00, 0x00, 0x01, 0x39, 0x3F, 0x39, 0x3F, 0x3F, 0x00, 0x00, 0x39, 0x03, 0x3F, 0x39, 0x03, 0x03, 0x00, 0x00, 0x39, 0x39, 0x3F, 0x39, 0x3F, 0x3F, 0x00, 0x00, 0x93, 0x39, 0x99, 0x33, 0x3F, 0x3F, 0x00, 0x00, 0xC7, 0x03, 0xC3, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00}; + dbg_tex = SDL_CreateTextureFromSurface(dbg_ren, SDL_LoadBMP_RW(SDL_RWFromMem(font, 448), 1)); +} + +void dbg_print(int x, int y, const char *text, Uint8 r, Uint8 g, Uint8 b) +{ + int cc = 0; + SDL_SetTextureColorMod(dbg_tex, r, g, b); + SDL_Rect src = {0, 0, 8, 8}; + SDL_Rect dst = {x, y, 8, 8}; + while (text[cc] != 0) + { + if (text[cc] != 32) + { + if (text[cc] >= 65) + { + src.x = ((text[cc] - 65) % 6) * 8; + src.y = ((text[cc] - 65) / 6) * 8; + } + else + { + src.x = ((text[cc] - 22) % 6) * 8; + src.y = ((text[cc] - 22) / 6) * 8; + } + + SDL_RenderCopy(dbg_ren, dbg_tex, &src, &dst); + } + cc++; + dst.x += 8; + } +} \ No newline at end of file diff --git a/source/common/screen.cpp b/source/common/screen.cpp index 3a79ff8..aac0aec 100644 --- a/source/common/screen.cpp +++ b/source/common/screen.cpp @@ -6,6 +6,7 @@ #ifndef NO_SHADERS #include "jshader.h" #endif +#include "dbgtxt.h" // Constructor Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Input *input, options_t *options) @@ -36,6 +37,9 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Input * shakeEffect.remaining = 0; shakeEffect.origin = 0; attenuateEffect = false; + fpsTicks = 0; + fpsCounter = 0; + fps = 0; // Crea los objetos notify = new Notify(renderer, asset->get("notify.png"), asset->get("8bithud.png"), asset->get("8bithud.txt"), asset->get("notify.wav"), options); @@ -85,6 +89,11 @@ void Screen::blit() // Pinta las notificaciones notify->render(); + // Pinta el contador de FPS + fpsCounter++; + dbg_print(0, 0, std::to_string(fps).c_str(), 255, 255, 255); + + #ifdef NO_SHADERS // Vuelve a dejar el renderizador en modo normal SDL_SetRenderTarget(renderer, nullptr); @@ -305,6 +314,7 @@ void Screen::update() { updateShake(); notify->update(); + updateFPS(); } // Comprueba las entradas @@ -437,4 +447,15 @@ void Screen::showNotification(std::string text1, std::string text2, int icon) SDL_Renderer *Screen::getRenderer() { return renderer; +} + +// Calcula los frames por segundo +void Screen::updateFPS() +{ + if (SDL_GetTicks() - fpsTicks > 1000) + { + fpsTicks = SDL_GetTicks(); + fps = fpsCounter; + fpsCounter = 0; + } } \ No newline at end of file diff --git a/source/common/screen.h b/source/common/screen.h index ac7fb83..b911030 100644 --- a/source/common/screen.h +++ b/source/common/screen.h @@ -36,6 +36,9 @@ private: 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 bool attenuateEffect; // Indica si la pantalla ha de estar atenuada + Uint32 fpsTicks; // Ticks para contar los frames por segundo + int fpsCounter; // Contador de frames por segundo + int fps; // Frames calculados en el último segundo struct effect_t { @@ -67,6 +70,9 @@ private: // Atenua la pantalla void doAttenuate(); + // Calcula los frames por segundo + void updateFPS(); + public: // Constructor Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Input *input, options_t *options); diff --git a/source/director.cpp b/source/director.cpp index b74e602..4a93c29 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -6,6 +6,7 @@ #include #include #include +#include "common/dbgtxt.h" #ifndef _WIN32 #include @@ -60,6 +61,8 @@ Director::Director(int argc, char *argv[]) // Inicializa JailAudio initJailAudio(); + dbg_init(renderer); + // Crea los objetos lang = new Lang(asset); lang->setLang(options->game.language); diff --git a/source/enter_name.cpp b/source/enter_name.cpp new file mode 100644 index 0000000..00b61de --- /dev/null +++ b/source/enter_name.cpp @@ -0,0 +1,44 @@ +#include "enter_name.h" + +// Constructor +EnterName::EnterName(std::string *name) +{ + characterList = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + this->name = name; + pos = 0; + numCharacters = (int)characterList.size(); + for (int i = 0; i < NAME_LENGHT; ++i) + { + characterIndex[i] = 0; + } +} + +// Destructor +EnterName::~EnterName() +{ +} + +// Incrementa la posición +void EnterName::incPos() +{ + pos++; + pos = std::min(pos, NAME_LENGHT - 1); +} + +// Decrementa la posición +void EnterName::decPos() +{ + pos--; + pos = std::max(pos, 0); +} + +// Actualiza la variable +void EnterName::updateName() +{ + name->clear(); + for (int i = 0; i < NAME_LENGHT; ++i) + { + name->append("a"); + //name->append(characterIndex[i] = 0; + } +} \ No newline at end of file diff --git a/source/enter_name.h b/source/enter_name.h new file mode 100644 index 0000000..7c367ab --- /dev/null +++ b/source/enter_name.h @@ -0,0 +1,32 @@ +#pragma once + +#include + +#define NAME_LENGHT 8 + +// Clase EnterName +class EnterName +{ +private: + std::string characterList; // Lista de todos los caracteres permitidos + std::string *name; // Nombre introducido + int pos; // Posición a editar del nombre + int numCharacters; // Cantidad de caracteres de la lista de caracteres + int characterIndex[NAME_LENGHT]; // Indice de la lista para cada uno de los caracteres que forman el nombre + + // Incrementa la posición + void incPos(); + + // Decrementa la posición + void decPos(); + + // Actualiza la variable + void updateName(); + +public: + // Constructor + EnterName(std::string *name); + + // Destructor + ~EnterName(); +}; \ No newline at end of file