some optimizations

master
Eduardo Bart 12 years ago
parent 4c143f4a33
commit 1c5b906b5b

@ -48,6 +48,7 @@ clean sprites cache periodically
handle corrupt errors in dat/spr
throw exceptions when fail to read a file
fix C++ exceptions messages inside onExtendedOpcode
rework outfit masks drawing
* framework
rework Settings/g_configs

@ -167,7 +167,6 @@ void Application::run()
if(!m_initialized)
return;
//ticks_t lastPollTicks = g_clock.updateTicks();
m_stopping = false;
m_running = true;
@ -176,10 +175,10 @@ void Application::run()
g_lua.callGlobalField("g_app", "onRun");
while(!m_stopping) {
// only update the current time once per frame to gain performance
g_clock.updateTicks();
// first clock update
g_clock.update();
while(!m_stopping) {
// poll all events before rendering
poll();
@ -215,20 +214,16 @@ void Application::run()
m_foreground->copyFromScreen(viewportRect);
}
//glClearColor(0,0,0,0);
//glClear(GL_COLOR_BUFFER_BIT);
// draw background (animated stuff)
m_backgroundFrameCounter.processNextFrame();
g_ui.render(false);
// transform projection matrix to render upside down
Matrix3 projectionMatrix = g_painter->getProjectionMatrix();
projectionMatrix(2,2) *= -1.0f;
projectionMatrix(3,2) *= -1.0f;
// draw the foreground (steady stuff)
g_painter->saveAndResetState();
g_painter->setProjectionMatrix(projectionMatrix);
g_painter->setColor(Color::white);
g_painter->drawTexturedRect(viewportRect, m_foreground, viewportRect);
g_painter->restoreSavedState();
g_graphics.endRender();
@ -236,6 +231,9 @@ void Application::run()
g_window.swapBuffers();
}
// only update the current time once per frame to gain performance
g_clock.update();
m_backgroundFrameCounter.update();
m_foregroundFrameCounter.update();
@ -246,7 +244,8 @@ void Application::run()
} else {
// sleeps until next poll to avoid massive cpu usage
g_clock.sleep(POLL_CYCLE_DELAY+1);
stdext::millisleep(POLL_CYCLE_DELAY+1);
g_clock.update();
}
}
@ -288,6 +287,7 @@ void Application::resize(const Size& size)
m_onInputEvent = false;
m_foreground = TexturePtr(new Texture(size.width(), size.height()));
m_foreground->setUpsideDown(true);
m_mustRepaint = true;
}

@ -21,6 +21,7 @@
*/
#include "adaptativeframecounter.h"
#include "clock.h"
AdaptativeFrameCounter::AdaptativeFrameCounter()
{
@ -35,8 +36,8 @@ AdaptativeFrameCounter::AdaptativeFrameCounter()
m_maxFps = 0;
m_sleepMicros = 0;
m_mediumFrameDelay = 0;
m_lastFpsUpdate = stdext::micros();
m_lastPartialFpsUpdate = stdext::micros();
m_lastFpsUpdate = g_clock.micros();
m_lastPartialFpsUpdate = g_clock.micros();
}
bool AdaptativeFrameCounter::shouldProcessNextFrame()
@ -44,7 +45,7 @@ bool AdaptativeFrameCounter::shouldProcessNextFrame()
if(m_maxFps == 0)
return true;
ticks_t now = stdext::micros();
ticks_t now = g_clock.micros();
if(now - m_lastFrame < m_bestFrameDelay)
return false;
return true;
@ -52,7 +53,7 @@ bool AdaptativeFrameCounter::shouldProcessNextFrame()
void AdaptativeFrameCounter::processNextFrame()
{
ticks_t now = stdext::micros();
ticks_t now = g_clock.micros();
m_frames++;
m_partialFrames++;
m_frameDelaySum += now - m_lastFrame;
@ -61,7 +62,7 @@ void AdaptativeFrameCounter::processNextFrame()
void AdaptativeFrameCounter::update()
{
ticks_t now = stdext::micros();
ticks_t now = g_clock.micros();
ticks_t delta = now - m_lastPartialFpsUpdate;
if(delta > 41000 && m_partialFrames > 0) {
m_partialFps = m_partialFrames / (delta / 1000000.0f);
@ -101,7 +102,7 @@ int AdaptativeFrameCounter::getMaximumSleepMicros()
{
if(m_maxFps == 0)
return 0;
return m_lastFrame + m_bestFrameDelay - stdext::micros();
return m_lastFrame + m_bestFrameDelay - g_clock.micros();
}
float AdaptativeFrameCounter::getFrameDelayHit()

@ -22,30 +22,18 @@
#include "clock.h"
// for usleep
#include <unistd.h>
Clock g_clock;
Clock::Clock()
{
m_startupTime = std::chrono::high_resolution_clock::now();
m_currentTicks = 0;
}
ticks_t Clock::updateTicks()
{
m_currentTicks = asyncTicks();
m_currentTime = m_currentTicks/1000.0f;
return m_currentTicks;
}
ticks_t Clock::asyncTicks()
{
auto timeNow = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast<std::chrono::milliseconds>(timeNow - m_startupTime).count();
m_currentMicros = 0;
m_currentMillis = 0;
m_currentSeconds = 0;
}
void Clock::sleep(int ms)
void Clock::update()
{
usleep(ms * 1000);
m_currentMicros = stdext::micros();
m_currentMillis = m_currentMicros / 1000;
m_currentSeconds = m_currentMicros / 1000000.0f;
}

@ -30,23 +30,16 @@ class Clock
public:
Clock();
ticks_t updateTicks();
void sleep(int ms);
void update();
ticks_t asyncTicks();
ticks_t ticks() { return m_currentTicks; }
ticks_t ticksElapsed(ticks_t prevTicks) { return m_currentTicks - prevTicks; }
ticks_t ticksFor(int delay) { return m_currentTicks + delay; }
float asyncTime() { return asyncTicks()/1000.0f; }
float time() { return m_currentTime; }
float timeElapsed(float prevTime) { return m_currentTime - prevTime; }
float timeFor(float delay) { return m_currentTime + delay; }
ticks_t micros() { return m_currentMicros; }
ticks_t millis() { return m_currentMillis; }
float seconds() { return m_currentSeconds; }
private:
ticks_t m_currentTicks;
float m_currentTime;
std::chrono::system_clock::time_point m_startupTime;
ticks_t m_currentMicros;
ticks_t m_currentMillis;
float m_currentSeconds;
};
extern Clock g_clock;

@ -53,11 +53,11 @@ class ScheduledEvent : public Event
{
public:
ScheduledEvent(const std::function<void()>& callback, int delay) : Event(callback) {
m_ticks = g_clock.ticksFor(delay);
m_ticks = g_clock.millis() + delay;
}
int ticks() const { return m_ticks; }
int reamaningTicks() const { return m_ticks - g_clock.ticks(); }
int reamaningTicks() const { return m_ticks - g_clock.millis(); }
private:
ticks_t m_ticks;

@ -26,11 +26,11 @@
void Timer::restart()
{
m_startTicks = g_clock.ticks();
m_startTicks = g_clock.millis();
m_stopped = false;
}
ticks_t Timer::ticksElapsed()
{
return g_clock.ticks() - m_startTicks;
return g_clock.millis() - m_startTicks;
}

@ -65,6 +65,7 @@ void FrameBuffer::resize(const Size& size)
m_texture = TexturePtr(new Texture(size.width(), size.height(), 4));
m_texture->setSmooth(true);
m_texture->setUpsideDown(true);
if(m_fbo) {
internalBind();
@ -97,8 +98,8 @@ void FrameBuffer::bind()
internalBind();
Matrix3 projectionMatrix = { 2.0f/m_texture->getWidth(), 0.0f, 0.0f,
0.0f, 2.0f/m_texture->getHeight(), 0.0f,
-1.0f, -1.0f, 1.0f };
0.0f, -2.0f/m_texture->getHeight(), 0.0f,
-1.0f, 1.0f, 1.0f };
g_painter->saveAndResetState();
g_painter->setProjectionMatrix(projectionMatrix);

@ -79,6 +79,13 @@ void Graphics::init()
// blending is always enabled
glEnable(GL_BLEND);
// face culling may improve performance
/*
glCullFace(GL_BACK);
glFrontFace(GL_CW);
glEnable(GL_CULL_FACE);
*/
// determine max texture size
static GLint maxTextureSize = -1;
if(maxTextureSize == -1)

@ -45,7 +45,7 @@ public:
struct PainterState {
Matrix3 projectionMatrix;
Matrix2 textureMatrix;
Matrix3 textureMatrix;
Color color;
float opacity;
Painter::CompositionMode compositionMode;
@ -75,7 +75,7 @@ public:
virtual void drawBoundingRect(const Rect& dest, int innerLineWidth = 1) = 0;
virtual void setProjectionMatrix(const Matrix3& projectionMatrix) { m_projectionMatrix = projectionMatrix; }
virtual void setTextureMatrix(const Matrix2& textureMatrix) { m_textureMatrix = textureMatrix; }
virtual void setTextureMatrix(const Matrix3& textureMatrix) { m_textureMatrix = textureMatrix; }
virtual void setColor(const Color& color) { m_color = color; }
virtual void setOpacity(float opacity) { m_opacity = opacity; }
virtual void setCompositionMode(CompositionMode compositionMode);
@ -87,7 +87,7 @@ public:
void setTexture(const TexturePtr& texture) { setTexture(texture.get()); }
Matrix3 getProjectionMatrix() { return m_projectionMatrix; }
Matrix2 getTextureMatrix() { return m_textureMatrix; }
Matrix3 getTextureMatrix() { return m_textureMatrix; }
Color getColor() { return m_color; }
float getOpacity() { return m_opacity; }
CompositionMode getCompositionMode() { return m_compositionMode; }
@ -109,7 +109,7 @@ protected:
CoordsBuffer m_coordsBuffer;
Matrix3 m_projectionMatrix;
Matrix2 m_textureMatrix;
Matrix3 m_textureMatrix;
Color m_color;
float m_opacity;
CompositionMode m_compositionMode;

@ -195,7 +195,7 @@ void PainterOGL1::setProjectionMatrix(const Matrix3& projectionMatrix)
updateGlProjectionMatrix();
}
void PainterOGL1::setTextureMatrix(const Matrix2& textureMatrix)
void PainterOGL1::setTextureMatrix(const Matrix3& textureMatrix)
{
// avoid re-updating texture matrix
if(m_textureMatrix == textureMatrix)
@ -246,10 +246,10 @@ void PainterOGL1::updateGlProjectionMatrix()
void PainterOGL1::updateGlTextureMatrix()
{
float glTextureMatrix[] = {
m_textureMatrix(1,1), m_textureMatrix(1,2), 0.0f, 0.0f,
m_textureMatrix(2,1), m_textureMatrix(2,2), 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f,
m_textureMatrix(1,1), m_textureMatrix(1,2), 0.0f, 0.0f,
m_textureMatrix(2,1), m_textureMatrix(2,2), 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
m_textureMatrix(3,1), m_textureMatrix(3,2), 0.0f, m_textureMatrix(3,3),
};
setMatrixMode(MatrixTexture);

@ -57,7 +57,7 @@ public:
void setMatrixMode(MatrixMode matrixMode);
void setProjectionMatrix(const Matrix3& projectionMatrix);
void setTextureMatrix(const Matrix2& textureMatrix);
void setTextureMatrix(const Matrix3& textureMatrix);
void setColor(const Color& color);
void setOpacity(float opacity);

@ -28,13 +28,13 @@ static const std::string glslMainVertexShader = "\n\
static const std::string glslMainWithTexCoordsVertexShader = "\n\
attribute highp vec2 a_texCoord;\n\
uniform highp mat2 textureMatrix;\n\
uniform highp mat3 textureMatrix;\n\
varying highp vec2 texCoord;\n\
highp vec4 calculatePosition();\n\
void main()\n\
{\n\
gl_Position = calculatePosition();\n\
texCoord = textureMatrix * a_texCoord;\n\
texCoord = textureMatrix * vec3(a_texCoord,1);\n\
}\n";
static std::string glslPositionOnlyVertexShader = "\n\

@ -28,7 +28,7 @@
PainterShaderProgram::PainterShaderProgram()
{
m_startTime = g_clock.time();
m_startTime = g_clock.seconds();
m_opacity = 1;
m_color = Color::white;
m_time = 0;
@ -36,7 +36,7 @@ PainterShaderProgram::PainterShaderProgram()
bool PainterShaderProgram::link()
{
m_startTime = g_clock.time();
m_startTime = g_clock.seconds();
bindAttributeLocation(VERTEX_ATTR, "a_vertex");
bindAttributeLocation(TEXCOORD_ATTR, "a_texCoord");
if(ShaderProgram::link()) {
@ -72,7 +72,7 @@ void PainterShaderProgram::setProjectionMatrix(const Matrix3& projectionMatrix)
m_projectionMatrix = projectionMatrix;
}
void PainterShaderProgram::setTextureMatrix(const Matrix2& textureMatrix)
void PainterShaderProgram::setTextureMatrix(const Matrix3& textureMatrix)
{
if(textureMatrix == m_textureMatrix)
return;
@ -104,7 +104,7 @@ void PainterShaderProgram::setOpacity(float opacity)
void PainterShaderProgram::updateTime()
{
float time = g_clock.timeElapsed(m_startTime);
float time = g_clock.seconds() - m_startTime;
if(m_time == time)
return;

@ -52,7 +52,7 @@ public:
bool link();
void setProjectionMatrix(const Matrix3& projectionMatrix);
void setTextureMatrix(const Matrix2& textureMatrix);
void setTextureMatrix(const Matrix3& textureMatrix);
void setColor(const Color& color);
void setOpacity(float opacity);
void updateTime();
@ -63,7 +63,7 @@ private:
Color m_color;
float m_opacity;
Matrix3 m_projectionMatrix;
Matrix2 m_textureMatrix;
Matrix3 m_textureMatrix;
float m_time;
};

@ -27,7 +27,7 @@
ParticleSystem::ParticleSystem()
{
m_finished = false;
m_lastUpdateTime = g_clock.time();
m_lastUpdateTime = g_clock.seconds();
}
bool ParticleSystem::load(const OTMLNodePtr& node)
@ -79,10 +79,10 @@ void ParticleSystem::update()
}
// check time
float elapsedTime = g_clock.timeElapsed(m_lastUpdateTime);
float elapsedTime = g_clock.seconds() - m_lastUpdateTime;
if(elapsedTime < delay)
return;
m_lastUpdateTime = g_clock.time() - std::fmod(elapsedTime, delay);
m_lastUpdateTime = g_clock.seconds() - std::fmod(elapsedTime, delay);
for(int i = 0; i < elapsedTime / delay; ++i) {

@ -53,7 +53,6 @@ uint Texture::internalLoadGLTexture(uchar *pixels, int channels, int width, int
m_size.resize(width, height);
// convert texture pixel data to power of two size, only required for OpenGL 1.5 or older
Size glSize;
std::vector<uint8> tmp;
if(!g_graphics.canUseNonPowerOfTwoTextures()) {
int glWidth = 1;
@ -64,7 +63,7 @@ uint Texture::internalLoadGLTexture(uchar *pixels, int channels, int width, int
while(glHeight < height)
glHeight = glHeight << 1;
if(m_size != glSize && pixels) {
if(m_size != m_glSize && pixels) {
tmp.resize(glHeight*glWidth*channels, 0);
for(int y=0; y<height; ++y)
for(int x=0; x<width; ++x)
@ -73,15 +72,14 @@ uint Texture::internalLoadGLTexture(uchar *pixels, int channels, int width, int
pixels = &tmp[0];
}
glSize.resize(glWidth, glHeight);
m_glSize.resize(glWidth, glHeight);
} else
glSize = m_size;
m_glSize = m_size;
m_transformMatrix = { 1.0f/glSize.width(), 0.0f,
0.0f, 1.0f/glSize.height() };
setupTranformMatrix();
// checks texture max size
if(std::max(glSize.width(), glSize.height()) > g_graphics.getMaxTextureSize()) {
if(std::max(m_glSize.width(), m_glSize.height()) > g_graphics.getMaxTextureSize()) {
g_logger.error(stdext::format("loading texture with size %dx%d failed, "
"the maximum size allowed by the graphics card is %dx%d,"
"to prevent crashes the texture will be displayed as a blank texture",
@ -115,7 +113,7 @@ uint Texture::internalLoadGLTexture(uchar *pixels, int channels, int width, int
}
// load pixels into gl memory
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, glSize.width(), glSize.height(), 0, format, GL_UNSIGNED_BYTE, pixels);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_glSize.width(), m_glSize.height(), 0, format, GL_UNSIGNED_BYTE, pixels);
GLint texParam = GL_REPEAT;
if(g_graphics.canUseClampToEdge())
@ -182,6 +180,14 @@ void Texture::setSmooth(bool smooth)
setupFilters();
}
void Texture::setUpsideDown(bool upsideDown)
{
if(m_upsideDown == upsideDown)
return;
m_upsideDown = upsideDown;
setupTranformMatrix();
}
void Texture::generateSoftwareMipmaps(std::vector<uint8> inPixels)
{
bind();
@ -261,3 +267,16 @@ void Texture::setupFilters()
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
}
void Texture::setupTranformMatrix()
{
if(m_upsideDown) {
m_transformMatrix = { 1.0f/m_glSize.width(), 0.0f, 0.0f,
0.0f, -1.0f/m_glSize.height(), 0.0f,
0.0f, 1.0f, 1.0f };
} else {
m_transformMatrix = { 1.0f/m_glSize.width(), 0.0f, 0.0f,
0.0f, 1.0f/m_glSize.height(), 0.0f,
0.0f, 0.0f, 1.0f };
}
}

@ -46,26 +46,30 @@ public:
/// Activate texture anti-aliasing giving a better look when they are resized
void setSmooth(bool smooth);
void setUpsideDown(bool upsideDown);
GLuint getId() { return m_textureId; }
int getWidth() { return m_size.width(); }
int getHeight() { return m_size.height(); }
const Size& getSize() { return m_size; }
const Matrix2& getTransformMatrix() { return m_transformMatrix; }
const Matrix3& getTransformMatrix() { return m_transformMatrix; }
bool isEmpty() { return m_textureId == 0; }
bool hasMipmaps() { return m_hasMipmaps; }
protected:
void setupFilters();
void setupTranformMatrix();
GLuint internalLoadGLTexture(uchar* pixels, int channels, int w, int h);
GLuint m_textureId;
Size m_size;
Matrix2 m_transformMatrix;
Size m_glSize;
Matrix3 m_transformMatrix;
Boolean<false> m_hasMipmaps;
Boolean<false> m_smooth;
Boolean<false> m_upsideDown;
};
#endif

@ -74,8 +74,8 @@ void PlatformWindow::processKeyDown(Fw::Key keyCode)
m_inputEvent.reset(Fw::KeyPressInputEvent);
m_inputEvent.keyCode = keyCode;
m_lastKeysPress[keyCode] = g_clock.ticks();
m_firstKeysPress[keyCode] = g_clock.ticks();
m_lastKeysPress[keyCode] = g_clock.millis();
m_firstKeysPress[keyCode] = g_clock.millis();
m_onInputEvent(m_inputEvent);
}
}
@ -141,14 +141,14 @@ void PlatformWindow::fireKeysPress()
ticks_t lastPressTicks = m_lastKeysPress[keyCode];
ticks_t firstKeyPress = m_firstKeysPress[keyCode];
if(g_clock.ticksElapsed(lastPressTicks) >= KEY_PRESS_REPEAT_INTERVAL) {
if(g_clock.millis() - lastPressTicks >= KEY_PRESS_REPEAT_INTERVAL) {
if(m_onInputEvent) {
m_inputEvent.reset(Fw::KeyPressInputEvent);
m_inputEvent.keyCode = keyCode;
m_inputEvent.autoRepeatTicks = g_clock.ticksElapsed(firstKeyPress);
m_inputEvent.autoRepeatTicks = g_clock.millis() - firstKeyPress;
m_onInputEvent(m_inputEvent);
}
m_lastKeysPress[keyCode] = g_clock.ticks();
m_lastKeysPress[keyCode] = g_clock.millis();
}
}
}

@ -77,7 +77,7 @@ void SoundManager::terminate()
void SoundManager::poll()
{
static ticks_t lastUpdate = 0;
ticks_t now = g_clock.ticks();
ticks_t now = g_clock.millis();
if(now - lastUpdate < POLL_DELAY)
return;

@ -114,7 +114,7 @@ void SoundSource::setVelocity(const Point& velocity)
void SoundSource::setFading(FadeState state, float fadeTime)
{
float now = g_clock.time();
float now = g_clock.seconds();
if(m_fadeState != NoFading) {
float elapsed = now - m_fadeStartTime;
float add;
@ -132,7 +132,7 @@ void SoundSource::setFading(FadeState state, float fadeTime)
void SoundSource::update()
{
float now = g_clock.time();
float now = g_clock.seconds();
if(m_fadeState == FadingOn) {
float elapsed = now - m_fadeStartTime;
if(elapsed >= m_fadeTime) {

@ -64,7 +64,8 @@ void UITextEdit::drawSelf(bool foregroundPane)
assert(m_cursorPos <= textLength);
// draw every 333ms
const int delay = 333;
if(g_clock.ticksElapsed(m_cursorTicks) <= delay) {
int elapsed = g_clock.millis() - m_cursorTicks;
if(elapsed <= delay) {
Rect cursorRect;
// when cursor is at 0 or is the first visible element
if(m_cursorPos == 0 || m_cursorPos == m_startRenderPos)
@ -72,8 +73,8 @@ void UITextEdit::drawSelf(bool foregroundPane)
else
cursorRect = Rect(m_glyphsCoords[m_cursorPos-1].right(), m_glyphsCoords[m_cursorPos-1].top(), 1, m_font->getGlyphHeight());
g_painter->drawFilledRect(cursorRect);
} else if(g_clock.ticksElapsed(m_cursorTicks) >= 2*delay) {
m_cursorTicks = g_clock.ticks();
} else if(elapsed >= 2*delay) {
m_cursorTicks = g_clock.millis();
}
}
}
@ -512,5 +513,5 @@ bool UITextEdit::onMousePress(const Point& mousePos, Fw::MouseButton button)
void UITextEdit::blinkCursor()
{
m_cursorTicks = g_clock.ticks();
m_cursorTicks = g_clock.millis();
}

@ -105,7 +105,7 @@ void Creature::internalDrawOutfit(const Point& dest, float scaleFactor, bool ani
if(isAnimateAlways() && animateIdle) {
int ticksPerFrame = 1000 / getAnimationPhases();
animationPhase = (g_clock.ticks() % (ticksPerFrame * getAnimationPhases())) / ticksPerFrame;
animationPhase = (g_clock.millis() % (ticksPerFrame * getAnimationPhases())) / ticksPerFrame;
}
// xPattern => creature direction
@ -182,7 +182,7 @@ void Creature::internalDrawOutfit(const Point& dest, float scaleFactor, bool ani
if(animationPhases > 1) {
if(animateIdle)
animationPhase = (g_clock.ticks() % (animateTicks * animationPhases)) / animateTicks;
animationPhase = (g_clock.millis() % (animateTicks * animationPhases)) / animateTicks;
else
animationPhase = animationPhases-1;
}

@ -61,7 +61,7 @@ void Item::draw(const Point& dest, float scaleFactor, bool animate)
int animationPhase = 0;
if(getAnimationPhases() > 1) {
if(animate)
animationPhase = (g_clock.ticks() % (Otc::ITEM_TICKS_PER_FRAME * getAnimationPhases())) / Otc::ITEM_TICKS_PER_FRAME;
animationPhase = (g_clock.millis() % (Otc::ITEM_TICKS_PER_FRAME * getAnimationPhases())) / Otc::ITEM_TICKS_PER_FRAME;
else
animationPhase = getAnimationPhases()-1;
}
@ -184,7 +184,7 @@ void Item::draw(const Point& dest, float scaleFactor, bool animate)
m_type->draw(dest, scaleFactor, 0, xPattern, yPattern, zPattern, animationPhase);
// release draw shader
g_painter->resetShaderProgram();
//g_painter->resetShaderProgram();
}
void Item::setId(uint32 id)

Loading…
Cancel
Save