fps counter
This commit is contained in:
parent
937070b3e0
commit
f3eaf3f726
|
@ -30,6 +30,8 @@
|
||||||
#include "configs.h"
|
#include "configs.h"
|
||||||
#include "gamestate.h"
|
#include "gamestate.h"
|
||||||
|
|
||||||
|
#define MINIMUN_UPDATE_DELAY 50
|
||||||
|
|
||||||
Engine g_engine;
|
Engine g_engine;
|
||||||
|
|
||||||
void Engine::init()
|
void Engine::init()
|
||||||
|
@ -54,31 +56,57 @@ void Engine::terminate()
|
||||||
|
|
||||||
void Engine::run()
|
void Engine::run()
|
||||||
{
|
{
|
||||||
ulong ticks;
|
int ticks = Platform::getTicks();
|
||||||
static ulong lastFrameTicks;
|
int lastUpdateTicks = ticks;
|
||||||
|
int lastFpsTicks = ticks;
|
||||||
|
int updateElapsedTicks = ticks;
|
||||||
|
int frameCount = 0;
|
||||||
|
int fps = 0;
|
||||||
m_running = true;
|
m_running = true;
|
||||||
|
|
||||||
lastFrameTicks = Platform::getTicks();
|
// before redering do the first update
|
||||||
|
update(ticks, 0);
|
||||||
|
lastUpdateTicks = ticks;
|
||||||
|
|
||||||
|
Font *font = g_fonts.getDefault();
|
||||||
|
Point fpsPos(10,10);
|
||||||
|
|
||||||
while(!m_stopping) {
|
while(!m_stopping) {
|
||||||
// fire platform events
|
// fire platform events
|
||||||
Platform::poll();
|
Platform::poll();
|
||||||
|
|
||||||
// update
|
// update before redering
|
||||||
ticks = Platform::getTicks();
|
ticks = Platform::getTicks();
|
||||||
update(ticks, ticks - lastFrameTicks);
|
updateElapsedTicks = ticks - lastUpdateTicks;
|
||||||
lastFrameTicks = ticks;
|
if(updateElapsedTicks >= MINIMUN_UPDATE_DELAY) {
|
||||||
|
update(ticks, updateElapsedTicks);
|
||||||
|
lastUpdateTicks = ticks;
|
||||||
|
}
|
||||||
|
|
||||||
// render only when visible
|
// render only when visible
|
||||||
//if(Platform::isWindowVisible()) {
|
if(Platform::isWindowVisible()) {
|
||||||
|
// calculate and fps
|
||||||
|
if(m_calculateFps && font) {
|
||||||
|
frameCount++;
|
||||||
|
if(ticks - lastFpsTicks >= 1000) {
|
||||||
|
lastFpsTicks = ticks;
|
||||||
|
fps = frameCount;
|
||||||
|
frameCount = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
render();
|
render();
|
||||||
|
|
||||||
|
// render fps
|
||||||
|
if(m_calculateFps && font) {
|
||||||
|
font->renderText(fpsPos, format("FPS: %d", fps));
|
||||||
|
}
|
||||||
|
|
||||||
// swap buffers
|
// swap buffers
|
||||||
Platform::swapBuffers();
|
Platform::swapBuffers();
|
||||||
//}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
lastFrameTicks = 0;
|
|
||||||
m_stopping = false;
|
m_stopping = false;
|
||||||
m_running = false;
|
m_running = false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,6 +36,7 @@ class Engine
|
||||||
public:
|
public:
|
||||||
Engine() : m_stopping(false),
|
Engine() : m_stopping(false),
|
||||||
m_running(false),
|
m_running(false),
|
||||||
|
m_calculateFps(false),
|
||||||
m_currentState(NULL) { }
|
m_currentState(NULL) { }
|
||||||
|
|
||||||
void init();
|
void init();
|
||||||
|
@ -60,6 +61,9 @@ public:
|
||||||
/// Fired by platform on mouse/keyboard input
|
/// Fired by platform on mouse/keyboard input
|
||||||
void onInputEvent(InputEvent *event);
|
void onInputEvent(InputEvent *event);
|
||||||
|
|
||||||
|
/// Enable FPS counter on screen
|
||||||
|
void enableFpsCounter(bool enable = true) { m_calculateFps = enable; };
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// Called to render every frame
|
/// Called to render every frame
|
||||||
void render();
|
void render();
|
||||||
|
@ -68,6 +72,7 @@ private:
|
||||||
|
|
||||||
bool m_stopping;
|
bool m_stopping;
|
||||||
bool m_running;
|
bool m_running;
|
||||||
|
bool m_calculateFps;
|
||||||
|
|
||||||
GameState *m_currentState;
|
GameState *m_currentState;
|
||||||
};
|
};
|
||||||
|
|
|
@ -109,25 +109,27 @@ void Font::renderText(const Point& pos, const std::string& text)
|
||||||
|
|
||||||
Point currentPos = pos;
|
Point currentPos = pos;
|
||||||
const Size& screenSize = g_graphics.getScreenSize();
|
const Size& screenSize = g_graphics.getScreenSize();
|
||||||
|
const Size& textureSize = m_texture->getSize();
|
||||||
int textLenght = text.length();
|
int textLenght = text.length();
|
||||||
|
|
||||||
for(int i = 0; i < textLenght; ++i) {
|
for(int i = 0; i < textLenght; ++i) {
|
||||||
int c = (int)text[i];
|
int glyph = (int)text[i];
|
||||||
|
|
||||||
// break rendering if the Y pos is below the screen
|
// break rendering if the Y pos is below the screen
|
||||||
if(currentPos.y >= screenSize.height())
|
if(currentPos.y >= screenSize.height())
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// new line
|
// new line
|
||||||
if(c == '\n') {
|
if(glyph == (uchar)'\n') {
|
||||||
currentPos.y += m_lineHeight;
|
currentPos.y += m_lineHeight;
|
||||||
currentPos.x = pos.x;
|
currentPos.x = pos.x;
|
||||||
}
|
}
|
||||||
// render glyph
|
// render only if the glyph is valid and visible
|
||||||
else {
|
else if(glyph >= 32 && currentPos.x < screenSize.width()) {
|
||||||
// render online if is visible on screen
|
g_graphics._drawTexturedRect(Rect(currentPos, m_glyphsSize[glyph]),
|
||||||
if(currentPos.x < screenSize.width())
|
m_glyphsTextureCoords[glyph],
|
||||||
currentPos.x += renderGlyph(currentPos, c);
|
textureSize);
|
||||||
|
currentPos.x += m_glyphsSize[glyph].width();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,15 +138,23 @@ void Font::renderText(const Point& pos, const std::string& text)
|
||||||
g_graphics.resetColor();
|
g_graphics.resetColor();
|
||||||
}
|
}
|
||||||
|
|
||||||
int Font::renderGlyph(const Point& pos, int glyph)
|
Size Font::calculateTextSize(const std::string& text)
|
||||||
{
|
{
|
||||||
// don't render invalid glyphs
|
int textLenght = text.length();
|
||||||
if(glyph < 32)
|
Size size;
|
||||||
return 0;
|
Point currentPos;
|
||||||
|
|
||||||
// render glyph
|
for(int i = 0; i < textLenght; ++i) {
|
||||||
Rect screenCoords(pos, m_glyphsSize[glyph]);
|
int glyph = (int)text[i];
|
||||||
g_graphics._drawTexturedRect(screenCoords, m_glyphsTextureCoords[glyph], m_texture->getSize());
|
|
||||||
|
|
||||||
return m_glyphsSize[glyph].width();
|
if(glyph == (uchar)'\n') {
|
||||||
|
currentPos.y += m_lineHeight;
|
||||||
|
size.expandedTo(currentPos.toSize());
|
||||||
|
currentPos.x = 0;
|
||||||
|
}
|
||||||
|
else if(glyph > 32) {
|
||||||
|
currentPos.x += m_glyphsSize[glyph].width();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return size;
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,9 +39,21 @@ public:
|
||||||
/// Load font from file
|
/// Load font from file
|
||||||
bool load(const std::string &file);
|
bool load(const std::string &file);
|
||||||
|
|
||||||
/// Simple text render
|
/// Simple text render starting at pos
|
||||||
void renderText(const Point& pos, const std::string& text);
|
void renderText(const Point& pos, const std::string& text);
|
||||||
|
|
||||||
|
/// Render text delimited by screenCoords rect
|
||||||
|
void renderText(const Rect& screenCoords, const std::string& text);
|
||||||
|
|
||||||
|
/** Advanced text render
|
||||||
|
* screenCoords is the rect that will be filled on the screen
|
||||||
|
* startRenderPosition is the postion to start rendering relative to the text rect
|
||||||
|
*/
|
||||||
|
void renderText(const Rect& screenCoords, const Point& startRenderPosition, const std::string& text);
|
||||||
|
|
||||||
|
/// Simulate render and calculate text size
|
||||||
|
Size calculateTextSize(const std::string& text);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
enum EAlign {
|
enum EAlign {
|
||||||
ALIGN_TOP = 1 << 0,
|
ALIGN_TOP = 1 << 0,
|
||||||
|
@ -60,9 +72,6 @@ public:
|
||||||
void renderText(const Rect& screenCoords, EAlign align, const std::string& text);
|
void renderText(const Rect& screenCoords, EAlign align, const std::string& text);
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/// Render a text
|
|
||||||
int renderGlyph(const Point& pos, int glyph);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int m_lineHeight;
|
int m_lineHeight;
|
||||||
int m_cursorSize;
|
int m_cursorSize;
|
||||||
|
|
|
@ -51,3 +51,13 @@ Font* Fonts::get(const std::string& fontName)
|
||||||
error("Font \"%s\" not found", fontName.c_str());
|
error("Font \"%s\" not found", fontName.c_str());
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Font *Fonts::getDefault()
|
||||||
|
{
|
||||||
|
Font *font = get("tibia-10px-rounded");
|
||||||
|
if(font) {
|
||||||
|
return font;
|
||||||
|
}
|
||||||
|
fatal("Default font not found!");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
|
@ -39,6 +39,9 @@ public:
|
||||||
/// Get a font by name
|
/// Get a font by name
|
||||||
Font *get(const std::string& fontName);
|
Font *get(const std::string& fontName);
|
||||||
|
|
||||||
|
/// Get default font
|
||||||
|
Font *getDefault();
|
||||||
|
|
||||||
/// Terminate all fonts
|
/// Terminate all fonts
|
||||||
void terminate() { }
|
void terminate() { }
|
||||||
|
|
||||||
|
|
|
@ -101,7 +101,7 @@ int main(int argc, const char *argv[])
|
||||||
640, 480,
|
640, 480,
|
||||||
g_configs.getBoolean("window maximized"));
|
g_configs.getBoolean("window maximized"));
|
||||||
Platform::setWindowTitle("OTClient");
|
Platform::setWindowTitle("OTClient");
|
||||||
Platform::setVsync();
|
//Platform::setVsync();
|
||||||
|
|
||||||
// init engine
|
// init engine
|
||||||
g_engine.init();
|
g_engine.init();
|
||||||
|
@ -113,6 +113,7 @@ int main(int argc, const char *argv[])
|
||||||
|
|
||||||
Platform::showWindow();
|
Platform::showWindow();
|
||||||
//Platform::hideMouseCursor();
|
//Platform::hideMouseCursor();
|
||||||
|
g_engine.enableFpsCounter();
|
||||||
|
|
||||||
// main loop, run everything
|
// main loop, run everything
|
||||||
g_engine.run();
|
g_engine.run();
|
||||||
|
|
|
@ -67,14 +67,7 @@ void MenuState::render()
|
||||||
Rect texCoords(0, 0, texCoordsSize);
|
Rect texCoords(0, 0, texCoordsSize);
|
||||||
texCoords.moveBottomRight(texSize.toPoint());
|
texCoords.moveBottomRight(texSize.toPoint());
|
||||||
g_graphics.drawTexturedRect(Rect(0, 0, screenSize), m_background.get(), texCoords);
|
g_graphics.drawTexturedRect(Rect(0, 0, screenSize), m_background.get(), texCoords);
|
||||||
|
g_fonts.getDefault()->renderText(Point(10,screenSize.height() - 20), "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam ac orci id quam condimentum semper.");
|
||||||
Font *font = g_fonts.get("sans-11px-antialised");
|
|
||||||
if(font)
|
|
||||||
font->renderText(Point(10,10), "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam ac orci id quam condimentum semper.");
|
|
||||||
|
|
||||||
font = g_fonts.get("tibia-10px-rounded");
|
|
||||||
if(font)
|
|
||||||
font->renderText(Point(10,30), "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam ac orci id quam condimentum semper.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MenuState::update(int ticks, int elapsedTicks)
|
void MenuState::update(int ticks, int elapsedTicks)
|
||||||
|
|
Loading…
Reference in New Issue