Implement timekeeping for OnRefresh, fix duplicate event firing.
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 2m11s

This commit is contained in:
2024-09-24 21:40:00 -04:00
parent b96dd03af1
commit fb3a77e2e3
3 changed files with 78 additions and 33 deletions

View File

@@ -182,11 +182,7 @@ namespace ReWindow
/// Returns a Vector2 representing mouse coordinates relative to the top-left corner of the window.
Vector2 GetMouseCoordinates() const;
// TODO: Is this part of the API interface? Can it be moved to protected?
void liftKey (Key key);
// TODO: Is this part of the API interface? Can it be moved to protected?
void pressKey (Key key);
/// Sets which rendering API is to be used with this window.
void setRenderer(RenderingAPI api);
@@ -298,5 +294,22 @@ namespace ReWindow
RenderingAPI renderer;
bool open = false;
bool resizable;
float delta_time;
float refresh_rate;
int refresh_count;
/// Executes event handlers for keyboard release events.
void processKeyRelease (Key key);
/// Executes event handlers for keyboard press events.
void processKeyPress (Key key);
/// Executes event handlers for mouse press events.
void processMousePress(MouseButton btn);
/// Executes event handlers for mouse release events.
void processMouseRelease(MouseButton btn);
/// Executes event handlers for window focus events.
void processFocusIn();
/// Executes event handlers for window unfocus events.
void processFocusOut();
};
}

View File

@@ -63,8 +63,11 @@ void RWindow::destroyWindow() {
}
void RWindow::refresh() {
// TODO: Implement refresh time keeping
OnRefresh(0.f);
auto begin_frame = std::chrono::high_resolution_clock::now();
OnRefresh(delta_time);
should_poll_x_for_mouse_pos = true;
Vector2 mouse_coords = getCursorPos();
should_poll_x_for_mouse_pos = false;
@@ -75,6 +78,20 @@ void RWindow::refresh() {
OnMouseMove(eventData);
OnMouseMoveEvent(eventData);
}
auto end_frame = std::chrono::high_resolution_clock::now();
auto frame_time = end_frame - begin_frame;
int frame_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(frame_time).count();
float frame_time_s = frame_time_ms / 1000.f;
delta_time = frame_time_ms;
refresh_rate = 1.f / refresh_rate;
refresh_count;
//std::this_thread::sleep_for(1ms);
}
@@ -125,10 +142,6 @@ void RWindow::pollEvents() {
if (xev.type == FocusIn) {
Logger::Debug(std::format("Recieved event '{}'", "FocusIn"));
XAutoRepeatOff(display);
//focusGained.Invoke();
RWindowEvent event {};
OnFocusGain(event);
OnFocusGainEvent(event);
setFlag(RWindowFlags::IN_FOCUS, true);
if (!cursor_visible)
@@ -137,58 +150,47 @@ void RWindow::pollEvents() {
// Get the position of the renderable area relative to the rest of the window.
XGetWindowAttributes(display, window, &windowAttributes);
render_area_position = { (float) windowAttributes.x, (float) windowAttributes.y };
processFocusIn();
}
if (xev.type == FocusOut) {
Logger::Debug(std::format("Recieved event '{}'", "FocusOut"));
XAutoRepeatOn(display);
//focusLost.Invoke();
RWindowEvent event {};
OnFocusLost(event);
OnFocusLostEvent(event);
setFlag(RWindowFlags::IN_FOCUS, false);
setFlag(RWindowFlags::IN_FOCUS, false);
if (!cursor_visible)
XUndefineCursor(display, window);
processFocusOut();
}
if (xev.type == KeyRelease) {
Logger::Debug(std::format("Recieved event '{}'", "KeyRelease"));
auto scancode = (X11Scancode) xev.xkey.keycode;
auto key = GetKeyFromX11Scancode(scancode);
OnKeyUpEvent(key);
OnKeyUp(key);
liftKey(key);
processKeyRelease(key);
}
if (xev.type == KeyPress) {
Logger::Debug(std::format("Recieved event '{}'", "KeyPress"));
auto scancode = (X11Scancode) xev.xkey.keycode;
auto key = GetKeyFromX11Scancode(scancode);
OnKeyDownEvent(key);
OnKeyDown(key);
pressKey(key);
//eventLog.push_back(eventData);
processKeyPress(key);
}
if (xev.type == ButtonRelease) {
Logger::Debug(std::format("Recieved event '{}'", "ButtonRelease"));
MouseButton button = GetMouseButtonFromXButton(xev.xbutton.button);
auto eventData = MouseButtonUpEvent();
eventData.Button = button;
OnMouseButtonUpEvent(eventData);
OnMouseButtonUp(eventData);
processMouseRelease(button);
}
if (xev.type == ButtonPress) {
Logger::Debug(std::format("Recieved event '{}'", "ButtonPress"));
MouseButton button = GetMouseButtonFromXButton(xev.xbutton.button);
auto eventData = MouseButtonDownEvent(button);
eventData.Button = button;
OnMouseButtonDownEvent(eventData);
OnMouseButtonDown(eventData);
Logger::Debug(std::format("Window event: MouseButtonPress {}", button.CharCode));
processMousePress(button);
}
if (xev.type == Expose)

View File

@@ -56,10 +56,39 @@ void RWindow::setFullscreen(bool fs) {
restoreFromFullscreen();
}
void RWindow::liftKey(Key key) {
void RWindow::processFocusIn()
{
RWindowEvent event {};
OnFocusGain(event);
OnFocusGainEvent(event);
}
void RWindow::processFocusOut()
{
RWindowEvent event {};
OnFocusLost(event);
OnFocusLostEvent(event);
}
void RWindow::processMousePress(MouseButton btn)
{
auto eventData = MouseButtonDownEvent(btn);
OnMouseButtonDownEvent(eventData);
OnMouseButtonDown(eventData);
}
void RWindow::processMouseRelease(MouseButton btn)
{
auto eventData = MouseButtonUpEvent(btn);
OnMouseButtonUpEvent(eventData);
OnMouseButtonUp(eventData);
}
void RWindow::processKeyRelease(Key key) {
currentKeyboard.PressedKeys[key] = false;
auto event = KeyUpEvent(key);
OnKeyUp(event);
OnKeyUpEvent(key);
}
@@ -110,10 +139,11 @@ bool RWindow::isKeyDown(Key key) const {
return false;
}
void RWindow::pressKey(Key key) {
void RWindow::processKeyPress(Key key) {
currentKeyboard.PressedKeys[key] = true;
auto eventData = KeyDownEvent(key);
OnKeyDown(eventData);
OnKeyDownEvent(key);
}