Afegit comptador de frames per segon a la classe Screen
This commit is contained in:
6
source/common/dbgtxt.h
Normal file
6
source/common/dbgtxt.h
Normal 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
42
source/common/dbhtxt.cpp
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user