Afegit comptador de frames per segon a la classe Screen

This commit is contained in:
2024-08-26 10:18:06 +02:00
parent c7cc5df920
commit 65b8988ba2
7 changed files with 154 additions and 0 deletions

6
source/common/dbgtxt.h Normal file
View File

@@ -0,0 +1,6 @@
#pragma once
#include <SDL2/SDL.h>
void dbg_init(SDL_Renderer *renderer);
void dbg_print(int x, int y, const char *text, Uint8 r, Uint8 g, Uint8 b);

42
source/common/dbhtxt.cpp Normal file
View File

@@ -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;
}
}

View File

@@ -6,6 +6,7 @@
#ifndef NO_SHADERS #ifndef NO_SHADERS
#include "jshader.h" #include "jshader.h"
#endif #endif
#include "dbgtxt.h"
// Constructor // Constructor
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Input *input, options_t *options) 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.remaining = 0;
shakeEffect.origin = 0; shakeEffect.origin = 0;
attenuateEffect = false; attenuateEffect = false;
fpsTicks = 0;
fpsCounter = 0;
fps = 0;
// Crea los objetos // Crea los objetos
notify = new Notify(renderer, asset->get("notify.png"), asset->get("8bithud.png"), asset->get("8bithud.txt"), asset->get("notify.wav"), options); 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 // Pinta las notificaciones
notify->render(); notify->render();
// Pinta el contador de FPS
fpsCounter++;
dbg_print(0, 0, std::to_string(fps).c_str(), 255, 255, 255);
#ifdef NO_SHADERS #ifdef NO_SHADERS
// Vuelve a dejar el renderizador en modo normal // Vuelve a dejar el renderizador en modo normal
SDL_SetRenderTarget(renderer, nullptr); SDL_SetRenderTarget(renderer, nullptr);
@@ -305,6 +314,7 @@ void Screen::update()
{ {
updateShake(); updateShake();
notify->update(); notify->update();
updateFPS();
} }
// Comprueba las entradas // Comprueba las entradas
@@ -438,3 +448,14 @@ SDL_Renderer *Screen::getRenderer()
{ {
return renderer; return renderer;
} }
// Calcula los frames por segundo
void Screen::updateFPS()
{
if (SDL_GetTicks() - fpsTicks > 1000)
{
fpsTicks = SDL_GetTicks();
fps = fpsCounter;
fpsCounter = 0;
}
}

View File

@@ -36,6 +36,9 @@ private:
SDL_Rect dest; // Coordenadas donde se va a dibujar la textura del juego sobre la pantalla o 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 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 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 struct effect_t
{ {
@@ -67,6 +70,9 @@ private:
// Atenua la pantalla // Atenua la pantalla
void doAttenuate(); void doAttenuate();
// Calcula los frames por segundo
void updateFPS();
public: public:
// Constructor // Constructor
Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Input *input, options_t *options); Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Input *input, options_t *options);

View File

@@ -6,6 +6,7 @@
#include <string> #include <string>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include "common/dbgtxt.h"
#ifndef _WIN32 #ifndef _WIN32
#include <pwd.h> #include <pwd.h>
@@ -60,6 +61,8 @@ Director::Director(int argc, char *argv[])
// Inicializa JailAudio // Inicializa JailAudio
initJailAudio(); initJailAudio();
dbg_init(renderer);
// Crea los objetos // Crea los objetos
lang = new Lang(asset); lang = new Lang(asset);
lang->setLang(options->game.language); lang->setLang(options->game.language);

44
source/enter_name.cpp Normal file
View File

@@ -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;
}
}

32
source/enter_name.h Normal file
View File

@@ -0,0 +1,32 @@
#pragma once
#include <string>
#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();
};