Reworked ogfx::WindowBase update loop logic

This commit is contained in:
Sylar 2026-01-21 06:46:20 +01:00
parent 1583e72849
commit 8fea21fb58
7 changed files with 80 additions and 59 deletions

View file

@ -1 +1 @@
1962 1966

View file

@ -39,7 +39,7 @@ namespace ogfx
uint8_t extraPaddingLeft { 0 }; uint8_t extraPaddingLeft { 0 };
bool cursorBlink { true }; bool cursorBlink { true };
ostd::Counter cursorBlinkCounter { 1 }; ostd::Counter cursorBlinkCounter { 30 };
}; };
public: struct tDefaultThemes public: struct tDefaultThemes
{ {
@ -54,7 +54,7 @@ namespace ogfx
0, //Extra Padding Left 0, //Extra Padding Left
true, //Cursor Blink true, //Cursor Blink
{ 1 } //Cursor Blink Timer { 30 } //Cursor Blink Timer
}; };
inline static const Theme DefaultTheme { inline static const Theme DefaultTheme {
{ 120, 120, 180 }, //Text Color { 120, 120, 180 }, //Text Color
@ -67,7 +67,7 @@ namespace ogfx
0, //Extra Padding Left 0, //Extra Padding Left
true, //Cursor Blink true, //Cursor Blink
{ 1 } //Cursor Blink Timer { 30 } //Cursor Blink Timer
}; };
}; };
public: enum eActionEventType { None = 0, Enter, Tab }; public: enum eActionEventType { None = 0, Enter, Tab };
@ -103,7 +103,7 @@ namespace ogfx
virtual inline void onRender(ogfx::BasicRenderer2D& gfx) { } virtual inline void onRender(ogfx::BasicRenderer2D& gfx) { }
virtual inline void onUpdate(void) { } virtual inline void onUpdate(void) { }
virtual inline void onFixedUpdate(void) { } virtual inline void onFixedUpdate(void) { }
void setText(const ostd::String& text); void setText(const ostd::String& text);
void appendText(const ostd::String& text); void appendText(const ostd::String& text);
void setCursorPosition(uint16_t cursorPos); void setCursorPosition(uint16_t cursorPos);
@ -135,14 +135,14 @@ namespace ogfx
uint16_t m_cursorPosition { 0 }; uint16_t m_cursorPosition { 0 };
bool m_cursorState { true }; bool m_cursorState { true };
ostd::Counter m_keyRepeatCounter { 80 }; ostd::Counter m_keyRepeatCounter { 1 };
char m_lastChar { 0 }; char m_lastChar { 0 };
int32_t m_lastKeyCode { 0 }; int32_t m_lastKeyCode { 0 };
Theme m_theme; Theme m_theme;
bool m_mouseInside { false }; bool m_mouseInside { false };
public: public:
inline static const uint32_t actionEventSignalID { ostd::SignalHandler::newCustomSignal(11400) }; inline static const uint32_t actionEventSignalID { ostd::SignalHandler::newCustomSignal(11400) };
}; };
@ -162,4 +162,4 @@ namespace ogfx
inline bool isValidChar(char c) override { return c >= '0' && c <= '9'; } inline bool isValidChar(char c) override { return c >= '0' && c <= '9'; }
}; };
} }
} }

View file

@ -1,4 +1,5 @@
#include "WindowBase.hpp" #include "WindowBase.hpp"
#include "Time.hpp"
namespace ogfx namespace ogfx
{ {
@ -20,7 +21,7 @@ namespace ogfx
m_windowWidth = width; m_windowWidth = width;
m_windowHeight = height; m_windowHeight = height;
m_title = windowTitle; m_title = windowTitle;
if (SDL_Init(SDL_INIT_VIDEO) != 0) if (SDL_Init(SDL_INIT_VIDEO) != 0)
{ {
printf( "SDL could not initialize! Error: %s\n", SDL_GetError() ); printf( "SDL could not initialize! Error: %s\n", SDL_GetError() );
exit(1); exit(1);
@ -43,6 +44,18 @@ namespace ogfx
m_initialized = true; m_initialized = true;
m_running = 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"); setTypeName("dragon::WindowBase");
enableSignals(true); enableSignals(true);
validate(); validate();
@ -50,34 +63,27 @@ namespace ogfx
onInitialize(); onInitialize();
} }
void WindowBase::close(void)
{
m_running = false;
onClose();
ostd::SignalHandler::emitSignal(ostd::tBuiltinSignals::WindowClosed, ostd::tSignalPriority::RealTime, *this);
}
void WindowBase::update(void) void WindowBase::update(void)
{ {
if (!m_initialized) return; if (!m_initialized) return;
Uint64 start = SDL_GetPerformanceCounter(); __handle_events();
handleEvents();
SDL_SetRenderDrawColor(m_renderer, m_clearColor.r, m_clearColor.g, m_clearColor.b, m_clearColor.a); SDL_SetRenderDrawColor(m_renderer, m_clearColor.r, m_clearColor.g, m_clearColor.b, m_clearColor.a);
if (m_refreshScreen) if (m_refreshScreen)
SDL_RenderClear(m_renderer); SDL_RenderClear(m_renderer);
m_fixedUpdateTImer.update();
onUpdate(); onUpdate();
onRender(); onRender();
SDL_RenderPresent(m_renderer); SDL_RenderPresent(m_renderer);
Uint64 end = SDL_GetPerformanceCounter(); m_frameTimeAcc += m_fpsUpdateClock.restart(ostd::eTimeUnits::Seconds);
float elapsed = (end - start) / (float)SDL_GetPerformanceFrequency(); m_frameCount++;
m_redrawAccumulator += elapsed; m_fpsUpdateTimer.update();
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;
}
} }
void WindowBase::setSize(int32_t width, int32_t height) void WindowBase::setSize(int32_t width, int32_t height)
@ -110,14 +116,12 @@ namespace ogfx
} }
} }
void WindowBase::enableResizable(bool enable) void WindowBase::enableResizable(bool enable)
{ {
SDL_SetWindowResizable(m_window, (enable ? SDL_TRUE : SDL_FALSE)); SDL_SetWindowResizable(m_window, (enable ? SDL_TRUE : SDL_FALSE));
} }
void WindowBase::__handle_events(void)
void WindowBase::handleEvents(void)
{ {
if (!m_initialized) return; if (!m_initialized) return;
auto l_getMouseState = [this](void) -> MouseEventData { auto l_getMouseState = [this](void) -> MouseEventData {
@ -194,4 +198,4 @@ namespace ogfx
} }
} }
} }
} }

View file

@ -3,6 +3,7 @@
#include <ogfx/SDLInclude.hpp> #include <ogfx/SDLInclude.hpp>
#include <ostd/Signals.hpp> #include <ostd/Signals.hpp>
#include <ostd/IOHandlers.hpp> #include <ostd/IOHandlers.hpp>
#include <ostd/Time.hpp>
namespace ogfx namespace ogfx
{ {
@ -14,36 +15,35 @@ namespace ogfx
~WindowBase(void); ~WindowBase(void);
inline WindowBase(int32_t width, int32_t height, const ostd::String& windowTitle) { initialize(width, height, windowTitle); } 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 initialize(int32_t width, int32_t height, const ostd::String& windowTitle);
void close(void);
void update(void); void update(void);
void setSize(int32_t width, int32_t height); void setSize(int32_t width, int32_t height);
void setTitle(const ostd::String& title); void setTitle(const ostd::String& title);
void setCursor(eCursor cursor); void setCursor(eCursor cursor);
void enableResizable(bool enable = true); void enableResizable(bool enable = true);
inline virtual void onRender(void) { } inline virtual void onRender(void) { }
inline virtual void onUpdate(void) { } inline virtual void onUpdate(void) { }
inline virtual void onFixedUpdate(void) { } inline virtual void onFixedUpdate(double frameTime_s) { }
inline virtual void onSlowUpdate(void) { }
inline virtual void onInitialize(void) { } inline virtual void onInitialize(void) { }
inline virtual void onDestroy(void) { } inline virtual void onDestroy(void) { }
inline virtual void onClose(void) { }
inline bool isInitialized(void) const { return m_initialized; } inline bool isInitialized(void) const { return m_initialized; }
inline bool isRunning(void) const { return m_running; } 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 hide(void) { SDL_HideWindow(m_window); }
inline void show(void) { SDL_ShowWindow(m_window); } inline void show(void) { SDL_ShowWindow(m_window); }
inline ostd::String getTitle(void) const { return m_title; } inline ostd::String getTitle(void) const { return m_title; }
inline int32_t getFPS(void) const { return m_fps; } inline int32_t getFPS(void) const { return m_fps; }
inline int32_t getWindowWidth(void) const { return m_windowWidth; } inline int32_t getWindowWidth(void) const { return m_windowWidth; }
inline int32_t getWindowHeight(void) const { return m_windowHeight; } 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 bool isMouseDragEventEnabled(void) const { return m_deagEventEnabled; }
inline void enableMouseDragEvent(bool enable = true) { m_deagEventEnabled = enable; } inline void enableMouseDragEvent(bool enable = true) { m_deagEventEnabled = enable; }
inline ostd::Color getClearColor(void) const { return m_clearColor; } inline ostd::Color getClearColor(void) const { return m_clearColor; }
inline void setClearColor(const ostd::Color& color) { m_clearColor = color; } inline void setClearColor(const ostd::Color& color) { m_clearColor = color; }
inline SDL_Renderer* getSDLRenderer(void) const { return m_renderer; }
private: private:
void handleEvents(void); void __handle_events(void);
protected: protected:
ostd::ConsoleOutputHandler out; ostd::ConsoleOutputHandler out;
@ -53,15 +53,18 @@ namespace ogfx
bool m_refreshScreen { true }; bool m_refreshScreen { true };
private: private:
ostd::Color m_clearColor { 10, 10, 10, 255 };
int32_t m_windowWidth { 0 }; int32_t m_windowWidth { 0 };
int32_t m_windowHeight { 0 }; int32_t m_windowHeight { 0 };
ostd::String m_title { "" }; ostd::String m_title { "" };
int32_t m_fps { 0 }; int32_t m_fps { 0 };
ostd::Color m_clearColor { 10, 10, 10, 255 }; ostd::StepTimer m_fixedUpdateTImer;
ostd::StepTimer m_fpsUpdateTimer;
float m_timeAccumulator { 0.0f }; ostd::Timer m_fpsUpdateClock;
float m_redrawAccumulator { 0.0f }; uint64_t m_frameTimeAcc { 0 };
int32_t m_frameCount { 0 };
bool m_deagEventEnabled { false }; bool m_deagEventEnabled { false };
bool m_running { false }; bool m_running { false };
@ -77,8 +80,8 @@ namespace ogfx
{ {
setTypeName("ogfx::WindowResizedData"); setTypeName("ogfx::WindowResizedData");
validate(); validate();
} }
public: public:
int32_t new_width; int32_t new_width;
int32_t new_height; int32_t new_height;
@ -118,4 +121,4 @@ namespace ogfx
eKeyEvent eventType; eKeyEvent eventType;
WindowBase& parentWindow; WindowBase& parentWindow;
}; };
} }

View file

@ -72,6 +72,7 @@ namespace ostd
uint64_t startCount(eTimeUnits timeUnit = eTimeUnits::Nanoseconds); uint64_t startCount(eTimeUnits timeUnit = eTimeUnits::Nanoseconds);
uint64_t end(bool print = true); uint64_t end(bool print = true);
uint64_t endCount(bool stop = true); uint64_t endCount(bool stop = true);
uint64_t restart(eTimeUnits timeUnit = eTimeUnits::Nanoseconds);
static uint64_t getEpoch(eTimeUnits timeUnit = eTimeUnits::Milliseconds); static uint64_t getEpoch(eTimeUnits timeUnit = eTimeUnits::Milliseconds);

View file

@ -9,6 +9,7 @@
#include <chrono> #include <chrono>
#include <algorithm> #include <algorithm>
#include <cctype> #include <cctype>
#include <cstdint>
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <fstream> #include <fstream>
@ -575,6 +576,18 @@ namespace ostd
return diff; 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) uint64_t Timer::getEpoch(eTimeUnits timeUnit)
{ {
switch (timeUnit) switch (timeUnit)

View file

@ -14,8 +14,8 @@ class Window : public ogfx::WindowBase
{ {
public: public:
inline Window(void) : m_sigHandler(m_textInput, *this) { } inline Window(void) : m_sigHandler(m_textInput, *this) { }
inline void onInitialize(void) override inline void onInitialize(void) override
{ {
enableSignals(); enableSignals();
connectSignal(ostd::tBuiltinSignals::KeyReleased); connectSignal(ostd::tBuiltinSignals::KeyReleased);
connectSignal(ogfx::gui::RawTextInput::actionEventSignalID); connectSignal(ogfx::gui::RawTextInput::actionEventSignalID);
@ -30,7 +30,7 @@ class Window : public ogfx::WindowBase
// m_textInput.setCharacterFilter(m_numCharFilter); // m_textInput.setCharacterFilter(m_numCharFilter);
m_textInput.getTheme().extraPaddingTop = 3; m_textInput.getTheme().extraPaddingTop = 3;
} }
inline void handleSignal(ostd::tSignal& signal) override inline void handleSignal(ostd::tSignal& signal) override
{ {
if (signal.ID == ostd::tBuiltinSignals::KeyReleased) if (signal.ID == ostd::tBuiltinSignals::KeyReleased)
@ -59,19 +59,19 @@ class Window : public ogfx::WindowBase
inline void onRender(void) override inline void onRender(void) override
{ {
m_textInput.render(m_gfx); m_textInput.render(m_gfx);
} }
inline void onFixedUpdate(void) override inline void onFixedUpdate(double frameTime_s) override
{ {
m_textInput.fixedUpdate(); m_textInput.fixedUpdate();
} }
inline void onUpdate(void) override inline void onUpdate(void) override
{ {
m_textInput.update (); m_textInput.update ();
} }
private: private:
ogfx::gui::RawTextInput m_textInput; ogfx::gui::RawTextInput m_textInput;
ogfx::BasicRenderer2D m_gfx; ogfx::BasicRenderer2D m_gfx;
@ -88,11 +88,11 @@ int main(int argc, char** argv)
out.p(STR_BOOL(ostd::Utils::md5("") == "d41d8cd98f00b204e9800998ecf8427e")).nl(); out.p(STR_BOOL(ostd::Utils::md5("") == "d41d8cd98f00b204e9800998ecf8427e")).nl();
out.p(STR_BOOL(ostd::Utils::md5("abc") == "900150983cd24fb0d6963f7d28e17f72")).nl(); out.p(STR_BOOL(ostd::Utils::md5("abc") == "900150983cd24fb0d6963f7d28e17f72")).nl();
out.p(STR_BOOL(ostd::Utils::md5("message digest") == "f96b697d7cb7938d525a2f31aaf161d0")).nl(); out.p(STR_BOOL(ostd::Utils::md5("message digest") == "f96b697d7cb7938d525a2f31aaf161d0")).nl();
Window window; Window window;
window.initialize(1280, 720, "OmniaFramework - Test Window"); window.initialize(1280, 720, "OmniaFramework - Test Window");
window.setClearColor({ 0, 2 , 15 }); window.setClearColor({ 0, 2 , 15 });
while (window.isRunning()) while (window.isRunning())
{ {
window.update(); window.update();
@ -145,7 +145,7 @@ int main(int argc, char** argv)
// ostd::KeyboardController keyboard; // ostd::KeyboardController keyboard;
// keyboard.disableCommandBuffer(); // keyboard.disableCommandBuffer();
// out.p("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"); // out.p("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n");
// out.p("HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH"); // out.p("HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH");
// out.p("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"); // out.p("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
@ -159,4 +159,4 @@ int main(int argc, char** argv)
// } while (k != ostd::eKeys::Escape); // } while (k != ostd::eKeys::Escape);
return 0; return 0;
} }