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 };
|
||||
|
|
@ -103,7 +103,7 @@ namespace ogfx
|
|||
virtual inline void onRender(ogfx::BasicRenderer2D& gfx) { }
|
||||
virtual inline void onUpdate(void) { }
|
||||
virtual inline void onFixedUpdate(void) { }
|
||||
|
||||
|
||||
void setText(const ostd::String& text);
|
||||
void appendText(const ostd::String& text);
|
||||
void setCursorPosition(uint16_t cursorPos);
|
||||
|
|
@ -135,14 +135,14 @@ 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 };
|
||||
|
||||
Theme m_theme;
|
||||
|
||||
|
||||
bool m_mouseInside { false };
|
||||
|
||||
|
||||
public:
|
||||
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'; }
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
#include "WindowBase.hpp"
|
||||
#include "Time.hpp"
|
||||
|
||||
namespace ogfx
|
||||
{
|
||||
|
|
@ -20,7 +21,7 @@ namespace ogfx
|
|||
m_windowWidth = width;
|
||||
m_windowHeight = height;
|
||||
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() );
|
||||
exit(1);
|
||||
|
|
@ -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 {
|
||||
|
|
@ -194,4 +198,4 @@ namespace ogfx
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
#include <ogfx/SDLInclude.hpp>
|
||||
#include <ostd/Signals.hpp>
|
||||
#include <ostd/IOHandlers.hpp>
|
||||
#include <ostd/Time.hpp>
|
||||
|
||||
namespace ogfx
|
||||
{
|
||||
|
|
@ -14,36 +15,35 @@ 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);
|
||||
void setCursor(eCursor cursor);
|
||||
void setCursor(eCursor cursor);
|
||||
void enableResizable(bool enable = true);
|
||||
|
||||
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 };
|
||||
|
|
@ -77,8 +80,8 @@ namespace ogfx
|
|||
{
|
||||
setTypeName("ogfx::WindowResizedData");
|
||||
validate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public:
|
||||
int32_t new_width;
|
||||
int32_t new_height;
|
||||
|
|
@ -118,4 +121,4 @@ namespace ogfx
|
|||
eKeyEvent eventType;
|
||||
WindowBase& parentWindow;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
24
src/test.cpp
24
src/test.cpp
|
|
@ -14,8 +14,8 @@ class Window : public ogfx::WindowBase
|
|||
{
|
||||
public:
|
||||
inline Window(void) : m_sigHandler(m_textInput, *this) { }
|
||||
inline void onInitialize(void) override
|
||||
{
|
||||
inline void onInitialize(void) override
|
||||
{
|
||||
enableSignals();
|
||||
connectSignal(ostd::tBuiltinSignals::KeyReleased);
|
||||
connectSignal(ogfx::gui::RawTextInput::actionEventSignalID);
|
||||
|
|
@ -30,7 +30,7 @@ class Window : public ogfx::WindowBase
|
|||
// m_textInput.setCharacterFilter(m_numCharFilter);
|
||||
m_textInput.getTheme().extraPaddingTop = 3;
|
||||
}
|
||||
|
||||
|
||||
inline void handleSignal(ostd::tSignal& signal) override
|
||||
{
|
||||
if (signal.ID == ostd::tBuiltinSignals::KeyReleased)
|
||||
|
|
@ -59,19 +59,19 @@ class Window : public ogfx::WindowBase
|
|||
|
||||
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
|
||||
{
|
||||
m_textInput.update ();
|
||||
m_textInput.update ();
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
ogfx::gui::RawTextInput m_textInput;
|
||||
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("abc") == "900150983cd24fb0d6963f7d28e17f72")).nl();
|
||||
out.p(STR_BOOL(ostd::Utils::md5("message digest") == "f96b697d7cb7938d525a2f31aaf161d0")).nl();
|
||||
|
||||
|
||||
Window window;
|
||||
window.initialize(1280, 720, "OmniaFramework - Test Window");
|
||||
window.setClearColor({ 0, 2 , 15 });
|
||||
|
||||
|
||||
while (window.isRunning())
|
||||
{
|
||||
window.update();
|
||||
|
|
@ -145,7 +145,7 @@ int main(int argc, char** argv)
|
|||
|
||||
// ostd::KeyboardController keyboard;
|
||||
// keyboard.disableCommandBuffer();
|
||||
|
||||
|
||||
// out.p("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n");
|
||||
// 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");
|
||||
|
|
@ -159,4 +159,4 @@ int main(int argc, char** argv)
|
|||
// } while (k != ostd::eKeys::Escape);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue