Reworked ogfx::WindowBase update loop logic
This commit is contained in:
parent
1583e72849
commit
8fea21fb58
7 changed files with 80 additions and 59 deletions
|
|
@ -1 +1 @@
|
|||
1962
|
||||
1966
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ namespace ogfx
|
|||
uint8_t extraPaddingLeft { 0 };
|
||||
|
||||
bool cursorBlink { true };
|
||||
ostd::Counter cursorBlinkCounter { 1 };
|
||||
ostd::Counter cursorBlinkCounter { 30 };
|
||||
};
|
||||
public: struct tDefaultThemes
|
||||
{
|
||||
|
|
@ -54,7 +54,7 @@ namespace ogfx
|
|||
0, //Extra Padding Left
|
||||
|
||||
true, //Cursor Blink
|
||||
{ 1 } //Cursor Blink Timer
|
||||
{ 30 } //Cursor Blink Timer
|
||||
};
|
||||
inline static const Theme DefaultTheme {
|
||||
{ 120, 120, 180 }, //Text Color
|
||||
|
|
@ -67,7 +67,7 @@ namespace ogfx
|
|||
0, //Extra Padding Left
|
||||
|
||||
true, //Cursor Blink
|
||||
{ 1 } //Cursor Blink Timer
|
||||
{ 30 } //Cursor Blink Timer
|
||||
};
|
||||
};
|
||||
public: enum eActionEventType { None = 0, Enter, Tab };
|
||||
|
|
@ -135,7 +135,7 @@ namespace ogfx
|
|||
uint16_t m_cursorPosition { 0 };
|
||||
bool m_cursorState { true };
|
||||
|
||||
ostd::Counter m_keyRepeatCounter { 80 };
|
||||
ostd::Counter m_keyRepeatCounter { 1 };
|
||||
char m_lastChar { 0 };
|
||||
int32_t m_lastKeyCode { 0 };
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
#include "WindowBase.hpp"
|
||||
#include "Time.hpp"
|
||||
|
||||
namespace ogfx
|
||||
{
|
||||
|
|
@ -43,6 +44,18 @@ namespace ogfx
|
|||
m_initialized = true;
|
||||
m_running = true;
|
||||
|
||||
m_fixedUpdateTImer.create(60.0, [this](double frameTime_s){
|
||||
this->onFixedUpdate(frameTime_s);
|
||||
});
|
||||
|
||||
m_fpsUpdateTimer.create(1.0, [this](double frameTime_s){
|
||||
if (this->m_frameCount == 0) return;
|
||||
if (this->m_frameTimeAcc == 0) return;
|
||||
this->m_fps = (int32_t)(1.0f / (static_cast<double>(this->m_frameTimeAcc) / static_cast<double>(this->m_frameCount)));
|
||||
this->m_frameTimeAcc = 0;
|
||||
this->m_frameCount = 0;
|
||||
});
|
||||
|
||||
setTypeName("dragon::WindowBase");
|
||||
enableSignals(true);
|
||||
validate();
|
||||
|
|
@ -50,34 +63,27 @@ namespace ogfx
|
|||
onInitialize();
|
||||
}
|
||||
|
||||
void WindowBase::close(void)
|
||||
{
|
||||
m_running = false;
|
||||
onClose();
|
||||
ostd::SignalHandler::emitSignal(ostd::tBuiltinSignals::WindowClosed, ostd::tSignalPriority::RealTime, *this);
|
||||
}
|
||||
|
||||
void WindowBase::update(void)
|
||||
{
|
||||
if (!m_initialized) return;
|
||||
Uint64 start = SDL_GetPerformanceCounter();
|
||||
handleEvents();
|
||||
__handle_events();
|
||||
SDL_SetRenderDrawColor(m_renderer, m_clearColor.r, m_clearColor.g, m_clearColor.b, m_clearColor.a);
|
||||
if (m_refreshScreen)
|
||||
SDL_RenderClear(m_renderer);
|
||||
m_fixedUpdateTImer.update();
|
||||
onUpdate();
|
||||
onRender();
|
||||
SDL_RenderPresent(m_renderer);
|
||||
Uint64 end = SDL_GetPerformanceCounter();
|
||||
float elapsed = (end - start) / (float)SDL_GetPerformanceFrequency();
|
||||
m_redrawAccumulator += elapsed;
|
||||
if (m_redrawAccumulator >= 0.2f)
|
||||
{
|
||||
onFixedUpdate();
|
||||
m_redrawAccumulator = 0.0f;
|
||||
}
|
||||
end = SDL_GetPerformanceCounter();
|
||||
elapsed = (end - start) / (float)SDL_GetPerformanceFrequency();
|
||||
m_timeAccumulator += elapsed;
|
||||
if (m_timeAccumulator >= 0.5f)
|
||||
{
|
||||
onSlowUpdate();
|
||||
m_fps = (int32_t)(1.0f / elapsed);
|
||||
m_timeAccumulator = 0.0f;
|
||||
}
|
||||
m_frameTimeAcc += m_fpsUpdateClock.restart(ostd::eTimeUnits::Seconds);
|
||||
m_frameCount++;
|
||||
m_fpsUpdateTimer.update();
|
||||
}
|
||||
|
||||
void WindowBase::setSize(int32_t width, int32_t height)
|
||||
|
|
@ -110,14 +116,12 @@ namespace ogfx
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
void WindowBase::enableResizable(bool enable)
|
||||
{
|
||||
SDL_SetWindowResizable(m_window, (enable ? SDL_TRUE : SDL_FALSE));
|
||||
}
|
||||
|
||||
|
||||
void WindowBase::handleEvents(void)
|
||||
void WindowBase::__handle_events(void)
|
||||
{
|
||||
if (!m_initialized) return;
|
||||
auto l_getMouseState = [this](void) -> MouseEventData {
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
#include <ogfx/SDLInclude.hpp>
|
||||
#include <ostd/Signals.hpp>
|
||||
#include <ostd/IOHandlers.hpp>
|
||||
#include <ostd/Time.hpp>
|
||||
|
||||
namespace ogfx
|
||||
{
|
||||
|
|
@ -14,7 +15,7 @@ namespace ogfx
|
|||
~WindowBase(void);
|
||||
inline WindowBase(int32_t width, int32_t height, const ostd::String& windowTitle) { initialize(width, height, windowTitle); }
|
||||
void initialize(int32_t width, int32_t height, const ostd::String& windowTitle);
|
||||
|
||||
void close(void);
|
||||
void update(void);
|
||||
void setSize(int32_t width, int32_t height);
|
||||
void setTitle(const ostd::String& title);
|
||||
|
|
@ -23,27 +24,26 @@ namespace ogfx
|
|||
|
||||
inline virtual void onRender(void) { }
|
||||
inline virtual void onUpdate(void) { }
|
||||
inline virtual void onFixedUpdate(void) { }
|
||||
inline virtual void onSlowUpdate(void) { }
|
||||
inline virtual void onFixedUpdate(double frameTime_s) { }
|
||||
inline virtual void onInitialize(void) { }
|
||||
inline virtual void onDestroy(void) { }
|
||||
inline virtual void onClose(void) { }
|
||||
|
||||
inline bool isInitialized(void) const { return m_initialized; }
|
||||
inline bool isRunning(void) const { return m_running; }
|
||||
inline void close(void) { m_running = false; }
|
||||
inline void hide(void) { SDL_HideWindow(m_window); }
|
||||
inline void show(void) { SDL_ShowWindow(m_window); }
|
||||
inline ostd::String getTitle(void) const { return m_title; }
|
||||
inline int32_t getFPS(void) const { return m_fps; }
|
||||
inline int32_t getWindowWidth(void) const { return m_windowWidth; }
|
||||
inline int32_t getWindowHeight(void) const { return m_windowHeight; }
|
||||
inline SDL_Renderer* getSDLRenderer(void) const { return m_renderer; }
|
||||
inline bool isMouseDragEventEnabled(void) const { return m_deagEventEnabled; }
|
||||
inline void enableMouseDragEvent(bool enable = true) { m_deagEventEnabled = enable; }
|
||||
inline ostd::Color getClearColor(void) const { return m_clearColor; }
|
||||
inline void setClearColor(const ostd::Color& color) { m_clearColor = color; }
|
||||
inline SDL_Renderer* getSDLRenderer(void) const { return m_renderer; }
|
||||
private:
|
||||
void handleEvents(void);
|
||||
void __handle_events(void);
|
||||
|
||||
protected:
|
||||
ostd::ConsoleOutputHandler out;
|
||||
|
|
@ -53,15 +53,18 @@ namespace ogfx
|
|||
bool m_refreshScreen { true };
|
||||
|
||||
private:
|
||||
ostd::Color m_clearColor { 10, 10, 10, 255 };
|
||||
|
||||
int32_t m_windowWidth { 0 };
|
||||
int32_t m_windowHeight { 0 };
|
||||
ostd::String m_title { "" };
|
||||
int32_t m_fps { 0 };
|
||||
|
||||
ostd::Color m_clearColor { 10, 10, 10, 255 };
|
||||
|
||||
float m_timeAccumulator { 0.0f };
|
||||
float m_redrawAccumulator { 0.0f };
|
||||
ostd::StepTimer m_fixedUpdateTImer;
|
||||
ostd::StepTimer m_fpsUpdateTimer;
|
||||
ostd::Timer m_fpsUpdateClock;
|
||||
uint64_t m_frameTimeAcc { 0 };
|
||||
int32_t m_frameCount { 0 };
|
||||
|
||||
bool m_deagEventEnabled { false };
|
||||
bool m_running { false };
|
||||
|
|
|
|||
|
|
@ -72,6 +72,7 @@ namespace ostd
|
|||
uint64_t startCount(eTimeUnits timeUnit = eTimeUnits::Nanoseconds);
|
||||
uint64_t end(bool print = true);
|
||||
uint64_t endCount(bool stop = true);
|
||||
uint64_t restart(eTimeUnits timeUnit = eTimeUnits::Nanoseconds);
|
||||
|
||||
static uint64_t getEpoch(eTimeUnits timeUnit = eTimeUnits::Milliseconds);
|
||||
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@
|
|||
#include <chrono>
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
|
|
@ -575,6 +576,18 @@ namespace ostd
|
|||
return diff;
|
||||
}
|
||||
|
||||
uint64_t Timer::restart(eTimeUnits timeUnit)
|
||||
{
|
||||
if (!m_started)
|
||||
{
|
||||
startCount(timeUnit);
|
||||
return 0;
|
||||
}
|
||||
uint64_t elapsed = endCount();
|
||||
startCount(timeUnit);
|
||||
return elapsed;
|
||||
}
|
||||
|
||||
uint64_t Timer::getEpoch(eTimeUnits timeUnit)
|
||||
{
|
||||
switch (timeUnit)
|
||||
|
|
|
|||
|
|
@ -62,7 +62,7 @@ class Window : public ogfx::WindowBase
|
|||
m_textInput.render(m_gfx);
|
||||
}
|
||||
|
||||
inline void onFixedUpdate(void) override
|
||||
inline void onFixedUpdate(double frameTime_s) override
|
||||
{
|
||||
m_textInput.fixedUpdate();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue