Refactoring RWindow class to support usage in ReCaveGame
This commit is contained in:
78
include/rewindow/types/WindowEvents.hpp
Normal file
78
include/rewindow/types/WindowEvents.hpp
Normal file
@@ -0,0 +1,78 @@
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <rewindow/types/key.h>
|
||||
#include <rewindow/types/mousebutton.h>
|
||||
|
||||
namespace ReWindow {
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
using precision_clock = std::chrono::high_resolution_clock;
|
||||
using precision_timestamp = precision_clock::time_point;
|
||||
|
||||
class TimestampedEvent {
|
||||
private:
|
||||
|
||||
public:
|
||||
precision_timestamp Timestamp;
|
||||
TimestampedEvent() : Timestamp(precision_clock::now())
|
||||
{ }
|
||||
};
|
||||
|
||||
class RWindowEvent : public TimestampedEvent {
|
||||
public:
|
||||
RWindowEvent() : TimestampedEvent() { }
|
||||
};
|
||||
|
||||
class KeyboardEvent : public RWindowEvent {
|
||||
public:
|
||||
Key key;
|
||||
KeyState state;
|
||||
KeyboardEvent(Key key, KeyState state) : RWindowEvent(), key(key), state(state) {}
|
||||
};
|
||||
class MouseEvent : public RWindowEvent {};
|
||||
|
||||
class GamepadEvent : public RWindowEvent {};
|
||||
|
||||
class MouseMoveEvent : public MouseEvent {
|
||||
public:
|
||||
Vector2 Position;
|
||||
Vector2 Delta;
|
||||
MouseMoveEvent(const Vector2 &pos) : MouseEvent(), Position(pos)
|
||||
{}
|
||||
MouseMoveEvent(int x, int y) : MouseEvent(), Position(Vector2(x, y))
|
||||
{}
|
||||
};
|
||||
|
||||
class KeyDownEvent : public KeyboardEvent {
|
||||
public:
|
||||
KeyDownEvent(Key key) : KeyboardEvent(key, KeyState::Pressed) {}
|
||||
};
|
||||
|
||||
class KeyUpEvent : public KeyboardEvent {
|
||||
public:
|
||||
KeyUpEvent(Key key) : KeyboardEvent(key, KeyState::Released) {}
|
||||
};
|
||||
|
||||
|
||||
class MouseButtonDownEvent : public MouseEvent {
|
||||
public:
|
||||
MouseButton Button;
|
||||
|
||||
MouseButtonDownEvent() = default;
|
||||
MouseButtonDownEvent(MouseButton Button) : MouseEvent() {}
|
||||
};
|
||||
|
||||
class MouseButtonUpEvent : public MouseEvent {
|
||||
public:
|
||||
MouseButton Button;
|
||||
MouseButtonUpEvent() = default;
|
||||
MouseButtonUpEvent(MouseButton Button) : MouseEvent() {}
|
||||
};
|
||||
|
||||
class WindowResizeRequestEvent : public RWindowEvent
|
||||
{
|
||||
public:
|
||||
Vector2 Size;
|
||||
};
|
||||
}
|
@@ -147,7 +147,7 @@ namespace Keys {
|
||||
|
||||
}
|
||||
|
||||
|
||||
enum class KeyState {Pressed, Released};
|
||||
|
||||
|
||||
|
||||
|
@@ -18,6 +18,8 @@ public:
|
||||
const char* CharCode;
|
||||
unsigned int ButtonIndex;
|
||||
bool operator == (const MouseButton& mb) const;
|
||||
bool operator<(const MouseButton& rhs) const;
|
||||
MouseButton(const MouseButton&) = default;
|
||||
};
|
||||
|
||||
|
||||
|
@@ -9,10 +9,8 @@
|
||||
#include <rewindow/types/mousebutton.h>
|
||||
#include <rewindow/types/gamepadbutton.h>
|
||||
#include <J3ML/LinearAlgebra.hpp>
|
||||
#include "WindowEvents.hpp"
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
using precision_clock = std::chrono::high_resolution_clock;
|
||||
using precision_timestamp = precision_clock::time_point;
|
||||
|
||||
enum class RWindowFlags: uint8_t {
|
||||
IN_FOCUS,
|
||||
@@ -31,113 +29,50 @@ enum class RenderingAPI: uint8_t {
|
||||
VULKAN = 1,
|
||||
};
|
||||
|
||||
enum class KeyState {Pressed, Released};
|
||||
|
||||
|
||||
namespace ReWindow
|
||||
{
|
||||
using J3ML::LinearAlgebra::Vector2;
|
||||
|
||||
class TimestampedEvent {
|
||||
private:
|
||||
class KeyboardState {
|
||||
public:
|
||||
std::map<Key, bool> PressedKeys;
|
||||
};
|
||||
|
||||
public:
|
||||
precision_timestamp Timestamp;
|
||||
TimestampedEvent() : Timestamp(precision_clock::now())
|
||||
{ }
|
||||
};
|
||||
class GamepadState {
|
||||
public:
|
||||
std::map<GamepadButton, bool> PressedButtons;
|
||||
};
|
||||
|
||||
class RWindowEvent : public TimestampedEvent {
|
||||
public:
|
||||
RWindowEvent() : TimestampedEvent() { }
|
||||
};
|
||||
const RWindowEvent EmptyRWindowEvent;
|
||||
class MouseState {
|
||||
public:
|
||||
std::map<MouseButton, bool> PressedBtns;
|
||||
Vector2 Pos;
|
||||
};
|
||||
|
||||
namespace WindowEvents {
|
||||
class KeyboardState {
|
||||
public:
|
||||
std::map<Key, bool> PressedKeys;
|
||||
};
|
||||
|
||||
class GamepadState {
|
||||
public:
|
||||
std::map<GamepadButton, bool> PressedButtons;
|
||||
};
|
||||
|
||||
class InputState {
|
||||
public:
|
||||
KeyboardState Keyboard;
|
||||
GamepadState Gamepad;
|
||||
};
|
||||
|
||||
class KeyboardEvent : public RWindowEvent {
|
||||
public:
|
||||
Key key;
|
||||
KeyState state;
|
||||
KeyboardEvent(Key key, KeyState state) : RWindowEvent(), key(key), state(state) {}
|
||||
};
|
||||
class MouseEvent : public RWindowEvent {};
|
||||
|
||||
class GamepadEvent : public RWindowEvent {};
|
||||
|
||||
class MouseMoveEvent : public MouseEvent {
|
||||
public:
|
||||
Vector2 Position;
|
||||
Vector2 Delta;
|
||||
MouseMoveEvent(const Vector2 &pos) : MouseEvent(), Position(pos)
|
||||
{}
|
||||
MouseMoveEvent(int x, int y) : MouseEvent(), Position(Vector2(x, y))
|
||||
{}
|
||||
};
|
||||
|
||||
class KeyDownEvent : public KeyboardEvent {
|
||||
public:
|
||||
KeyDownEvent(Key key) : KeyboardEvent(key, KeyState::Pressed) {}
|
||||
};
|
||||
|
||||
class KeyUpEvent : public KeyboardEvent {
|
||||
public:
|
||||
KeyUpEvent(Key key) : KeyboardEvent(key, KeyState::Released) {}
|
||||
};
|
||||
|
||||
|
||||
class MouseButtonDownEvent : public MouseEvent {
|
||||
public:
|
||||
MouseButton Button;
|
||||
|
||||
MouseButtonDownEvent() = default;
|
||||
MouseButtonDownEvent(MouseButton Button) : MouseEvent() {}
|
||||
};
|
||||
|
||||
class MouseButtonUpEvent : public MouseEvent {
|
||||
public:
|
||||
MouseButton Button;
|
||||
MouseButtonUpEvent() = default;
|
||||
MouseButtonUpEvent(MouseButton Button) : MouseEvent() {}
|
||||
};
|
||||
|
||||
class WindowResizeRequestEvent : public RWindowEvent
|
||||
{
|
||||
public:
|
||||
Vector2 Size;
|
||||
};
|
||||
}
|
||||
|
||||
using namespace WindowEvents;
|
||||
|
||||
|
||||
/// General To Do List
|
||||
/// TODO: Clean up public API to express the cross-platform, multi-graphics-mode ethos of this project.
|
||||
///
|
||||
class InputState {
|
||||
public:
|
||||
KeyboardState Keyboard;
|
||||
GamepadState Gamepad;
|
||||
};
|
||||
|
||||
/// RWindow is a class implementation of a platform-independent window abstraction.
|
||||
/// This library also provides abstractions for user-input devices, and their interaction with the window.
|
||||
class RWindow {
|
||||
public:
|
||||
#pragma region Constructors
|
||||
/// The default constructor sets a default size and window title.
|
||||
RWindow();
|
||||
/// Constructs a window by explicitly setting title, width and height.
|
||||
RWindow(const std::string& title, int width, int height);
|
||||
/// Constructs a window as above with the additional argument of explicitly setting which render API is to be used.
|
||||
RWindow(const std::string& title, int width, int height, RenderingAPI renderer);
|
||||
#pragma endregion
|
||||
|
||||
/// Bindables are provided for hooking into window instances conveniently, without the need to derive and override for simple use cases.
|
||||
#pragma region Bindable Events
|
||||
|
||||
/// We keep and support both mechanisms for extending behavior to suit:
|
||||
/// 1. Derived windows with added functionality.
|
||||
/// 2. Binding functions to a pre-existing window.
|
||||
#pragma region Callbacks
|
||||
/// Bindable Non-intrusive event handlers
|
||||
/// Use these when you can't override the base window class
|
||||
Event<> OnOpenEvent;
|
||||
Event<> OnClosingEvent;
|
||||
Event<RWindowEvent> OnFocusLostEvent;
|
||||
@@ -150,18 +85,23 @@ namespace ReWindow
|
||||
Event<MouseButtonDownEvent> OnMouseButtonDownEvent;
|
||||
Event<MouseButtonUpEvent> OnMouseButtonUpEvent;
|
||||
#pragma endregion
|
||||
|
||||
/// These methods can also be overridden in derived classes.
|
||||
#pragma region Overrides
|
||||
/// Intrusive virtual methods intended to be overridden in a derived class.
|
||||
/// Do not stuff any logic into these. Someone WILL override it and forget to call the base.
|
||||
|
||||
/// Called upon the window requesting to open.
|
||||
virtual void OnOpen() {}
|
||||
/// Called right before the window closes.
|
||||
virtual void OnClosing() {}
|
||||
/// Called when the window loses focus.
|
||||
virtual void OnFocusLost(const RWindowEvent& e) {}
|
||||
/// Called when the window gains focus.
|
||||
virtual void OnFocusGain(const RWindowEvent& e) {}
|
||||
/// Called when the window is 'refreshed', in other words, a render pass is completed.
|
||||
virtual void OnRefresh(float elapsed) {}
|
||||
/// Called when a resize request has succeeded.
|
||||
virtual void OnResizeSuccess() {}
|
||||
/// Called when a resize request is sent to the operating system.
|
||||
virtual bool OnResizeRequest(const WindowResizeRequestEvent& e) { return true;}
|
||||
virtual void OnKeyDown(const KeyDownEvent&) {}
|
||||
virtual void OnKeyUp(const KeyUpEvent&) {}
|
||||
@@ -170,20 +110,16 @@ namespace ReWindow
|
||||
virtual void OnMouseButtonUp(const MouseButtonUpEvent&) {}
|
||||
#pragma endregion
|
||||
|
||||
/// The default constructor sets a default size and window title.
|
||||
RWindow();
|
||||
/// Constructs a window by explicitly setting title, width and height.
|
||||
RWindow(const std::string& title, int width, int height);
|
||||
/// Constructs a window as above with the additional argument of explicitly setting which render API is to be used.
|
||||
RWindow(const std::string& title, int width, int height, RenderingAPI renderer);
|
||||
|
||||
/// Returns a Vector2 representing mouse coordinates relative to the top-left corner of the window.
|
||||
/// This result is cached from the operating-system, and as such may be out-of-date.
|
||||
/// @see GetAccurateMouseCoordinates().
|
||||
Vector2 GetMouseCoordinates() const;
|
||||
|
||||
/// Sets which rendering API is to be used with this window.
|
||||
void setRenderer(RenderingAPI api);
|
||||
void SetRenderer(RenderingAPI api);
|
||||
|
||||
/// Initializes all state with the window manager and rendering API, then opens the window.
|
||||
/// This function instructs the operating system to create the actual window, and give it to us to control.
|
||||
/// Calling this function, therefore, creates the real 'window' object on the operating system.
|
||||
void Open();
|
||||
|
||||
/// Cleans up and closes the window without destroying the handle.
|
||||
@@ -193,109 +129,161 @@ namespace ReWindow
|
||||
|
||||
void MessageBox(); // TODO: Must be implemented from scratch as a Motif Window in x11
|
||||
|
||||
/// Sends a request to the operating system to have this window somehow demand attention from the user.
|
||||
/// This is implemented per-operating system, and as such no guarantees about behavior can be made.
|
||||
void Flash();
|
||||
|
||||
/// Returns whether the window currently has mouse and/or keyboard focus.
|
||||
[[nodiscard]] bool isFocused() const;
|
||||
[[nodiscard]] bool isFullscreen() const;
|
||||
[[nodiscard]] bool isResizable() const;
|
||||
[[nodiscard]] bool isVsyncEnabled() const;
|
||||
[[nodiscard]] bool IsFocused() const;
|
||||
|
||||
[[nodiscard]] bool isAlive() const; // TODO: Always returns true.
|
||||
/// Returns whether the window is currently in Fullscreen.
|
||||
// TODO: Support Fullscreen, FullscreenWindowed, and Windowed?
|
||||
[[nodiscard]] bool IsFullscreen() const;
|
||||
|
||||
[[nodiscard]] bool isKeyDown(Key key) const;
|
||||
[[nodiscard]] bool isMouseButtonDown(MouseButton button) const;
|
||||
/// Returns whether the window can be resized.
|
||||
[[nodiscard]] bool IsResizable() const;
|
||||
|
||||
/// Returns whether V-Sync is enabled.
|
||||
[[nodiscard]] bool IsVsyncEnabled() const;
|
||||
|
||||
/// Returns whether the window is considered to be alive. Once dead, any logic loop should be terminated, and the cleanup procedure should run.
|
||||
[[nodiscard]] bool IsAlive() const;
|
||||
|
||||
/// Returns whether the given key is currently being pressed.
|
||||
[[nodiscard]] bool IsKeyDown(Key key) const;
|
||||
|
||||
/// Returns whether the given mouse button is currently being pressed.
|
||||
[[nodiscard]] bool IsMouseButtonDown(MouseButton button) const;
|
||||
|
||||
void SetFullscreen(bool fs);
|
||||
void SetResizable(bool resizable);
|
||||
void SetVsyncEnabled(bool vsync);
|
||||
void SetTitle(const std::string& title);
|
||||
[[nodiscard]] std::string GetTitle() const;
|
||||
[[nodiscard]] int GetWidth() const; // Honestly no idea if we'll keep these or still go with GetSize.
|
||||
[[nodiscard]] int GetHeight() const; // GetSize wasn't working for me for logging. -maxine
|
||||
void SetSizeWithoutEvent(const Vector2& size); //AAAAAHHHHHHHHH WINDOZE MAKING THINGS DIFFICULT :/ - Redacted.
|
||||
|
||||
void setFullscreen(bool fs);
|
||||
void setResizable(bool resizable);
|
||||
void setVsyncEnabled(bool);
|
||||
void setTitle(const std::string& title);
|
||||
[[nodiscard]] std::string getTitle() const;
|
||||
[[nodiscard]] int getWidth() const; // Honestly no idea if we'll keep these or still go with getSize.
|
||||
[[nodiscard]] int getHeight() const; // getSize wasn't working for me for logging. -maxine
|
||||
void setSizeWithoutEvent(const Vector2& size); //AAAAAHHHHHHHHH WINDOZE MAKING THINGS DIFFICULT :/ - Redacted.
|
||||
// TODO: Move out of public API, consumers should use setFullscreen()
|
||||
void fullscreen();
|
||||
// TODO: Move out of public API, consumers should use setFullscreen()
|
||||
void restoreFromFullscreen();
|
||||
|
||||
// TODO: Josh hates parameter-flags, it's not 1995 :/
|
||||
bool getFlag(RWindowFlags flag) const;
|
||||
bool GetFlag(RWindowFlags flag) const;
|
||||
// TODO: Josh hates parameter-flags, it's not 1995 :/
|
||||
void setFlag(RWindowFlags flag, bool state);
|
||||
void SetFlag(RWindowFlags flag, bool state);
|
||||
//void init(RenderingAPI api, const char* title, int width, int height, bool vsync);
|
||||
|
||||
/// Tells the underlying window manager to destroy this window and drop the handle.
|
||||
/// The window, in theory, can not be re-opened after this.
|
||||
/// TODO: Create a destructor and move to there?
|
||||
/// TODO: What's the semantic difference between this and Close()?
|
||||
void destroyWindow();
|
||||
void DestroyWindow();
|
||||
|
||||
/// Reads events from the underlying window manager.
|
||||
/// TODO: Move out of public API, consumers should call refresh or ideally an update() call.
|
||||
void pollEvents();
|
||||
/// Reads events from the operating system, and processes them accordingly.
|
||||
/// TODO: Move out of public API, consumers should call Refresh or ideally an update() call.
|
||||
void PollEvents();
|
||||
|
||||
void Refresh();
|
||||
|
||||
/// Updates the window and handles timing internally.
|
||||
void ManagedRefresh();
|
||||
|
||||
/// Requests the operating system to change the window size.
|
||||
/// @param width
|
||||
/// @param height
|
||||
void SetSize(int width, int height);
|
||||
/// Requests the operating system to change the window size.
|
||||
/// @param size
|
||||
void SetSize(const Vector2& size);
|
||||
|
||||
/// Updates the window and handles timing internally.
|
||||
void refresh();
|
||||
void setSize(int width, int height);
|
||||
void setSize(const Vector2& size);
|
||||
/// Returns the position of the window's top-left corner relative to the display
|
||||
Vector2 getPos() const;
|
||||
// I want to know why this is made platform specific. Is that even necessary? -maxine
|
||||
// Because each OS / WM implements it with a different API. - josh
|
||||
// If we stored ourselves a copy (accurately) of the window's size, we could implement it into the shared layer
|
||||
// But this is at BEST, unreliable.
|
||||
Vector2 getSize() const;
|
||||
Vector2 GetPos() const;
|
||||
|
||||
/// Returns the known size of the window, in {x,y} pixel measurement.
|
||||
Vector2 GetSize() const;
|
||||
|
||||
/// Returns the position of the "renderable area" of the window relative to it's top left corner.
|
||||
/// (used to account for the width or the border & title bar).
|
||||
Vector2 getPositionOfRenderableArea() const;
|
||||
Vector2 GetPositionOfRenderableArea() const;
|
||||
|
||||
void SetPos(int x, int y);
|
||||
void SetPos(const Vector2& pos);
|
||||
|
||||
void setPos(int x, int y);
|
||||
void setPos(const Vector2& pos);
|
||||
Vector2 getCursorPos() const;
|
||||
|
||||
/// Pull the window to the top, such that it is displayed on top of everything else.
|
||||
/// NOTE: The implementation is window-manager defined, and thus there is no guarantee of it always working.
|
||||
void raise() const;
|
||||
/// Push the window lower, such that it is effectively hidden behind other windows.
|
||||
/// NOTE: The implementation is window-manager defined, and thus there is no guarantee of it always working.
|
||||
void lower() const;
|
||||
void setCursorStyle(CursorStyle style) const;
|
||||
void setCursorCustomIcon() const;
|
||||
/// NOTE: The implementation is defined per-OS, and thus there is no guarantee of it always working.
|
||||
void Raise() const;
|
||||
/// Push the window Lower, such that it is effectively hidden behind other windows.
|
||||
/// NOTE: The implementation is defined per-OS, and thus there is no guarantee of it always working.
|
||||
void Lower() const;
|
||||
|
||||
void setCursorLocked();
|
||||
void setCursorCenter();
|
||||
void restoreCursorFromLastCenter(); // Feels nicer for users
|
||||
void SetCursorStyle(CursorStyle style) const;
|
||||
|
||||
///Hides the cursor when it's inside of our window.
|
||||
///Useful for 3D game camera.
|
||||
void setCursorVisible(bool cursor_enable);
|
||||
bool getCursorVisible();
|
||||
void SetCursorCustomIcon() const;
|
||||
|
||||
void SetCursorLocked();
|
||||
|
||||
void SetCursorCenter();
|
||||
|
||||
void RestoreCursorFromLastCenter(); // Feels nicer for users
|
||||
|
||||
/// Hides the cursor when it's inside of our window. Useful for 3D game camera.
|
||||
void SetCursorVisible(bool cursor_enable);
|
||||
|
||||
bool GetCursorVisible();
|
||||
|
||||
/// Calls OpenGL's SwapBuffers routine.
|
||||
/// NOTE: This is only used when the underlying rendering API is set to OpenGL.
|
||||
static void glSwapBuffers();
|
||||
Vector2 getLastKnownResize() const;
|
||||
void setLastKnownWindowSize(const Vector2& size);
|
||||
static void GLSwapBuffers();
|
||||
|
||||
|
||||
using high_res_timestamp = std::chrono::time_point<std::chrono::system_clock>;
|
||||
static high_res_timestamp GetTimestamp();
|
||||
float ComputeElapsedFrameTimeSeconds(high_res_timestamp start, high_res_timestamp end);
|
||||
|
||||
void UpdateFrameTiming(float frame_time);
|
||||
|
||||
float GetDeltaTime() const { return delta_time; }
|
||||
float GetRefreshRate() const { return refresh_rate; }
|
||||
float GetRefreshCounter() const { return refresh_count; }
|
||||
protected:
|
||||
float delta_time = 0.f;
|
||||
float refresh_rate = 0.f;
|
||||
unsigned int refresh_count = 0;
|
||||
bool cursor_visible = true;
|
||||
Vector2 lastKnownWindowSize {0, 0};
|
||||
bool flags[5];
|
||||
std::vector<RWindowEvent> eventLog;
|
||||
KeyboardState currentKeyboard; // Current Frame's Keyboard State
|
||||
KeyboardState previousKeyboard; // Previous Frame's Keyboard State
|
||||
bool fullscreenmode = false;
|
||||
// TODO: Move out of public API, consumers should use SetFullscreen()
|
||||
void Fullscreen();
|
||||
// TODO: Move out of public API, consumers should use SetFullscreen()
|
||||
void RestoreFromFullscreen();
|
||||
|
||||
protected:
|
||||
#pragma region Data
|
||||
|
||||
std::string title = "Redacted Window";
|
||||
int width = 1280;
|
||||
int height = 720;
|
||||
RenderingAPI renderer = RenderingAPI::OPENGL;
|
||||
|
||||
bool fullscreenmode = false;
|
||||
bool focused = true;
|
||||
bool vsync = false;
|
||||
bool cursor_visible = true;
|
||||
bool open = false;
|
||||
bool resizable = true;
|
||||
|
||||
Vector2 mouse_coords = {0, 0};
|
||||
Vector2 last_mouse_coords = {0, 0};
|
||||
|
||||
|
||||
|
||||
Vector2 lastKnownWindowSize {0, 0};
|
||||
bool flags[5];
|
||||
std::vector<RWindowEvent> eventLog;
|
||||
|
||||
KeyboardState currentKeyboard; // Current Frame's Keyboard State
|
||||
KeyboardState previousKeyboard; // Previous Frame's Keyboard State
|
||||
MouseState currentMouse;
|
||||
MouseState previousMouse;
|
||||
|
||||
|
||||
RenderingAPI renderer = RenderingAPI::OPENGL;
|
||||
|
||||
//Vector2 mouse_coords = {0, 0};
|
||||
//Vector2 last_mouse_coords = {0, 0};
|
||||
|
||||
|
||||
float delta_time = 0.f;
|
||||
float refresh_rate = 0.f;
|
||||
unsigned int refresh_count = 0;
|
||||
|
||||
// TODO: Implement ringbuffer / circular vector class of some sort.
|
||||
float refresh_rate_prev_1 = 0.f;
|
||||
@@ -305,15 +293,14 @@ namespace ReWindow
|
||||
float refresh_rate_prev_5 = 0.f;
|
||||
|
||||
float avg_refresh_rate = 0.0f;
|
||||
|
||||
private:
|
||||
|
||||
|
||||
#pragma endregion
|
||||
#pragma region Internals
|
||||
/// Returns the most accurate and recent available mouse coordinates.
|
||||
/// @note Call this version at most **once** per-frame. It polls the X-Window server and therefore is quite slow.
|
||||
/// @see getCursorPos();
|
||||
Vector2 GetAccurateMouseCoordinates() const;
|
||||
|
||||
#pragma region Event Callers
|
||||
/// Executes event handlers for keyboard rele;ase events.
|
||||
void processKeyRelease (Key key);
|
||||
/// Executes event handlers for keyboard press events.
|
||||
@@ -334,10 +321,8 @@ namespace ReWindow
|
||||
void processFocusOut();
|
||||
|
||||
void processMouseMove(Vector2 last_pos, Vector2 new_pos);
|
||||
#pragma endregion
|
||||
|
||||
private:
|
||||
|
||||
|
||||
|
||||
#pragma endregion
|
||||
};
|
||||
}
|
55
main.cpp
55
main.cpp
@@ -28,7 +28,7 @@ class MyWindow : public ReWindow::RWindow {
|
||||
void OnRefresh(float elapsed) override {
|
||||
glClearColor(255, 0, 0, 255);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glSwapBuffers();
|
||||
GLSwapBuffers();
|
||||
auto pos = GetMouseCoordinates();
|
||||
//std::cout << pos.x << ", " << pos.y << std::endl;
|
||||
|
||||
@@ -43,21 +43,22 @@ class MyWindow : public ReWindow::RWindow {
|
||||
|
||||
int main() {
|
||||
auto* window = new MyWindow("Test Window", 600, 480);
|
||||
jlog::Debug(std::format("New window '{}' created. width={} height={}", window->getTitle(), window->getWidth(), window->getHeight()));
|
||||
jlog::Debug(std::format("New window '{}' created. width={} height={}", window->GetTitle(), window->GetWidth(),
|
||||
window->GetHeight()));
|
||||
|
||||
window->setRenderer(RenderingAPI::OPENGL);
|
||||
jlog::Debug(std::format("Rendering API OPENGL set for window '{}'", window->getTitle()));
|
||||
window->SetRenderer(RenderingAPI::OPENGL);
|
||||
jlog::Debug(std::format("Rendering API OPENGL set for window '{}'", window->GetTitle()));
|
||||
|
||||
window->Open();
|
||||
jlog::Debug(std::format("Opened window '{}'", window->getTitle()));
|
||||
jlog::Debug(std::format("Opened window '{}'", window->GetTitle()));
|
||||
|
||||
// TODO: Cannot set flags until after window is open
|
||||
// Make this work somehow
|
||||
jlog::Debug("TODO: Cannot set flags until after window is open");
|
||||
window->setFullscreen(false);
|
||||
window->setVsyncEnabled(true);
|
||||
window->setResizable(true);
|
||||
window->setCursorVisible(false);
|
||||
window->SetFullscreen(false);
|
||||
window->SetVsyncEnabled(true);
|
||||
window->SetResizable(true);
|
||||
window->SetCursorVisible(false);
|
||||
|
||||
/*
|
||||
std::vector<ReWindow::Display> d = ReWindow::Display::getDisplaysFromWindowManager();
|
||||
@@ -73,8 +74,8 @@ int main() {
|
||||
std::cout << std::format("Mode ({},{}) @ {}hz Ratio {}", w, h, r, aspect) << std::endl;
|
||||
|
||||
for (ReWindow::FullscreenGraphicsMode& fgm : display.getGraphicsModes()) {
|
||||
w = fgm.getWidth();
|
||||
h = fgm.getHeight();
|
||||
w = fgm.GetWidth();
|
||||
h = fgm.GetHeight();
|
||||
r = fgm.getRefreshRate();
|
||||
|
||||
auto aspect = (float)w/(float)h;
|
||||
@@ -85,12 +86,12 @@ int main() {
|
||||
*/
|
||||
|
||||
ReWindow::Logger::Debug(std::format("Window '{}' flags: IN_FOCUS={} FULLSCREEN={} RESIZEABLE={} VSYNC={} QUIT={}",
|
||||
window->getTitle(),
|
||||
window->getFlag(RWindowFlags::IN_FOCUS),
|
||||
window->getFlag(RWindowFlags::FULLSCREEN),
|
||||
window->getFlag(RWindowFlags::RESIZABLE),
|
||||
window->getFlag(RWindowFlags::VSYNC),
|
||||
window->getFlag(RWindowFlags::QUIT)));
|
||||
window->GetTitle(),
|
||||
window->GetFlag(RWindowFlags::IN_FOCUS),
|
||||
window->GetFlag(RWindowFlags::FULLSCREEN),
|
||||
window->GetFlag(RWindowFlags::RESIZABLE),
|
||||
window->GetFlag(RWindowFlags::VSYNC),
|
||||
window->GetFlag(RWindowFlags::QUIT)));
|
||||
|
||||
|
||||
window->OnKeyDownEvent += [&] (ReWindow::KeyDownEvent e) {
|
||||
@@ -102,10 +103,24 @@ int main() {
|
||||
jlog::Debug(e.Button.CharCode);
|
||||
};
|
||||
|
||||
while (window->isAlive()) {
|
||||
window->pollEvents();
|
||||
window->refresh();
|
||||
while (window->IsAlive()) {
|
||||
window->PollEvents();
|
||||
window->ManagedRefresh();
|
||||
}
|
||||
// Alternatively:
|
||||
/*
|
||||
* while (window->IsAlive()) {
|
||||
* auto start = window->GetTimestamp();
|
||||
* ourUpdateLogic(window->GetDeltaTime());
|
||||
* window->update();
|
||||
* ourDrawLogic();
|
||||
* window->Refresh();
|
||||
* auto end = window->GetTimestamp();
|
||||
* auto dt = window->ComputeElapsedFrameTimeSeconds(start, end);
|
||||
* window->UpdateFrameTiming(dt);
|
||||
* }
|
||||
*
|
||||
*/
|
||||
}
|
||||
|
||||
/*
|
||||
|
@@ -53,7 +53,7 @@ std::vector<ReWindow::Display> ReWindow::Display::getDisplaysFromWindowManager()
|
||||
for (int k = 0; k < screen_resources->nmode; k++) {
|
||||
if (screen_resources->modes[k].id == mode) {
|
||||
XRRModeInfo mode_info = screen_resources->modes[k];
|
||||
// Calculate refresh rate from the mode's dot clock
|
||||
// Calculate Refresh rate from the mode's dot clock
|
||||
double refresh_rate = 0.0;
|
||||
if (mode_info.hTotal > 0 && mode_info.vTotal > 0)
|
||||
refresh_rate = mode_info.dotClock / (double)(mode_info.hTotal * mode_info.vTotal);
|
||||
|
@@ -38,7 +38,7 @@ bool should_poll_x_for_mouse_pos = true;
|
||||
|
||||
using namespace ReWindow;
|
||||
|
||||
void RWindow::raise() const {
|
||||
void RWindow::Raise() const {
|
||||
Logger::Debug(std::format("Raising window '{}'", this->title));
|
||||
|
||||
// Get the position of the renderable area relative to the rest of the window.
|
||||
@@ -48,13 +48,13 @@ void RWindow::raise() const {
|
||||
XRaiseWindow(display, window);
|
||||
}
|
||||
|
||||
void RWindow::lower() const
|
||||
void RWindow::Lower() const
|
||||
{
|
||||
Logger::Debug(std::format("Lowering window '{}'", this->title));
|
||||
XLowerWindow(display, window);
|
||||
}
|
||||
|
||||
void RWindow::destroyWindow() {
|
||||
void RWindow::DestroyWindow() {
|
||||
Logger::Debug(std::format("Destroying window '{}'", this->title));
|
||||
XDestroySubwindows(display, window);
|
||||
Logger::Debug(std::format("Destroyed window '{}'", this->title));
|
||||
@@ -62,43 +62,9 @@ void RWindow::destroyWindow() {
|
||||
delete this;
|
||||
}
|
||||
|
||||
void RWindow::refresh() {
|
||||
//void RWindow::
|
||||
|
||||
auto begin_frame = std::chrono::high_resolution_clock::now();
|
||||
|
||||
|
||||
OnRefresh(delta_time);
|
||||
|
||||
// Only call once and cache the result.
|
||||
mouse_coords = GetAccurateMouseCoordinates();
|
||||
|
||||
/// TODO: Implement optional minimum epsilon to trigger a Mouse Update.
|
||||
if (mouse_coords != last_mouse_coords) {
|
||||
processMouseMove(last_mouse_coords, mouse_coords);
|
||||
last_mouse_coords = mouse_coords;
|
||||
}
|
||||
|
||||
auto end_frame = std::chrono::high_resolution_clock::now();
|
||||
|
||||
auto frame_time = end_frame - begin_frame;
|
||||
unsigned long int frame_time_us = std::chrono::duration_cast<std::chrono::microseconds>(frame_time).count();
|
||||
double frame_time_s = frame_time_us / (1000.f * 1000.f);
|
||||
delta_time = frame_time_s;
|
||||
|
||||
refresh_rate = 1.f / delta_time;
|
||||
|
||||
refresh_rate_prev_5 = refresh_rate_prev_4;
|
||||
refresh_rate_prev_4 = refresh_rate_prev_3;
|
||||
refresh_rate_prev_3 = refresh_rate_prev_2;
|
||||
refresh_rate_prev_2 = refresh_rate_prev_1;
|
||||
refresh_rate_prev_1 = refresh_rate;
|
||||
avg_refresh_rate = (refresh_rate_prev_1 + refresh_rate_prev_2 + refresh_rate_prev_3 + refresh_rate_prev_4 + refresh_rate_prev_5) / 5.f;
|
||||
|
||||
|
||||
refresh_count++;
|
||||
}
|
||||
|
||||
void RWindow::setCursorVisible(bool cursor_enable) {
|
||||
void RWindow::SetCursorVisible(bool cursor_enable) {
|
||||
cursor_visible = cursor_enable;
|
||||
|
||||
if (invisible_cursor == 0) {
|
||||
@@ -115,7 +81,17 @@ void RWindow::setCursorVisible(bool cursor_enable) {
|
||||
XUndefineCursor(display, window);
|
||||
}
|
||||
|
||||
void RWindow::setFlag(RWindowFlags flag, bool state) {
|
||||
void RWindow::SetResizable(bool sizable) {
|
||||
this->resizable = sizable;
|
||||
//this->SetFlag(RWindowFlags::RESIZABLE, resizable);
|
||||
Logger::Debug("Once you've done this you cannot make it resizable again.");
|
||||
hints.flags = PMinSize | PMaxSize;
|
||||
hints.min_width = hints.max_width = windowAttributes.width;
|
||||
hints.min_height = hints.max_height = windowAttributes.height;
|
||||
XSetWMNormalHints(display, window, &hints);
|
||||
}
|
||||
|
||||
void RWindow::SetFlag(RWindowFlags flag, bool state) {
|
||||
XGetWindowAttributes(display,window,&windowAttributes);
|
||||
flags[(int) flag] = state;
|
||||
//Once you've done this you cannot make it resizable again.
|
||||
@@ -129,7 +105,7 @@ void RWindow::setFlag(RWindowFlags flag, bool state) {
|
||||
Logger::Debug(std::format("Set flag '{}' to state '{}' for window '{}'", RWindowFlagToStr(flag), state, this->title));
|
||||
}
|
||||
|
||||
void RWindow::pollEvents() {
|
||||
void RWindow::PollEvents() {
|
||||
while(XPending(display)) {
|
||||
XNextEvent(display, &xev);
|
||||
|
||||
@@ -139,7 +115,7 @@ void RWindow::pollEvents() {
|
||||
if (xev.xclient.message_type == XInternAtom(display, "WM_PROTOCOLS", False) && static_cast<Atom>(xev.xclient.data.l[0]) == wmDeleteWindow) {
|
||||
|
||||
processOnClose();
|
||||
destroyWindow();
|
||||
DestroyWindow();
|
||||
system("xset r on");
|
||||
exit(0);
|
||||
}
|
||||
@@ -147,7 +123,7 @@ void RWindow::pollEvents() {
|
||||
if (xev.type == FocusIn) {
|
||||
Logger::Debug(std::format("Recieved event '{}'", "FocusIn"));
|
||||
XAutoRepeatOff(display);
|
||||
setFlag(RWindowFlags::IN_FOCUS, true);
|
||||
SetFlag(RWindowFlags::IN_FOCUS, true);
|
||||
|
||||
if (!cursor_visible)
|
||||
XDefineCursor(display, window, invisible_cursor);
|
||||
@@ -162,7 +138,7 @@ void RWindow::pollEvents() {
|
||||
Logger::Debug(std::format("Recieved event '{}'", "FocusOut"));
|
||||
XAutoRepeatOn(display);
|
||||
|
||||
setFlag(RWindowFlags::IN_FOCUS, false);
|
||||
SetFlag(RWindowFlags::IN_FOCUS, false);
|
||||
if (!cursor_visible)
|
||||
XUndefineCursor(display, window);
|
||||
|
||||
@@ -231,12 +207,13 @@ void RWindow::pollEvents() {
|
||||
|
||||
}
|
||||
previousKeyboard = currentKeyboard;
|
||||
previousMouse.PressedBtns = currentMouse.PressedBtns;
|
||||
}
|
||||
|
||||
|
||||
// Might make the window go off the screen on some window managers.
|
||||
void RWindow::setSize(int newWidth, int newHeight) {
|
||||
if (!getFlag(RWindowFlags::RESIZABLE)) return;
|
||||
void RWindow::SetSize(int newWidth, int newHeight) {
|
||||
if (!resizable) return;
|
||||
|
||||
this->width = newWidth;
|
||||
this->height = newHeight;
|
||||
@@ -264,40 +241,38 @@ Vector2 RWindow::GetAccurateMouseCoordinates() const {
|
||||
return Vector2::Zero;
|
||||
}
|
||||
|
||||
// TODO: implement integer vector2/3 types
|
||||
Vector2 RWindow::getSize() const {
|
||||
|
||||
Vector2 RWindow::GetSize() const {
|
||||
return {(float) this->width, (float) this->height};
|
||||
}
|
||||
|
||||
Vector2 RWindow::getLastKnownResize() const
|
||||
{
|
||||
return lastKnownWindowSize;
|
||||
}
|
||||
//Vector2 RWindow::getLastKnownResize() const
|
||||
//{
|
||||
// return lastKnownWindowSize;
|
||||
//}
|
||||
|
||||
// TODO: implement integer vector2/3 types
|
||||
Vector2 RWindow::getPos() const {
|
||||
Vector2 RWindow::GetPos() const {
|
||||
return position;
|
||||
}
|
||||
|
||||
void RWindow::setPos(int x, int y) {
|
||||
void RWindow::SetPos(int x, int y) {
|
||||
XMoveWindow(display, window, x, y);
|
||||
position = { (float) x, (float) y };
|
||||
}
|
||||
|
||||
void RWindow::setPos(const Vector2& pos) {
|
||||
setPos(pos.x, pos.y);
|
||||
void RWindow::SetPos(const Vector2& pos) {
|
||||
SetPos(pos.x, pos.y);
|
||||
}
|
||||
|
||||
|
||||
void RWindow::glSwapBuffers() {
|
||||
void RWindow::GLSwapBuffers() {
|
||||
glXSwapBuffers(display,window);
|
||||
}
|
||||
|
||||
bool RWindow::isResizable() const {
|
||||
return getFlag(RWindowFlags::RESIZABLE);
|
||||
}
|
||||
|
||||
void RWindow::fullscreen() {
|
||||
|
||||
void RWindow::Fullscreen() {
|
||||
Logger::Debug(std::format("Fullscreening window '{}'", this->title));
|
||||
fullscreenmode = true;
|
||||
|
||||
@@ -318,8 +293,8 @@ void RWindow::fullscreen() {
|
||||
Logger::Debug(std::format("Fullscreened window '{}'", this->title));
|
||||
}
|
||||
|
||||
void RWindow::restoreFromFullscreen() {
|
||||
Logger::Debug(std::format("Restoring window '{}' from fullscreen", this->title));
|
||||
void RWindow::RestoreFromFullscreen() {
|
||||
Logger::Debug(std::format("Restoring window '{}' from Fullscreen", this->title));
|
||||
fullscreenmode = false;
|
||||
XEvent xev;
|
||||
Atom wm_state = XInternAtom(display, "_NET_WM_STATE", False);
|
||||
@@ -333,20 +308,17 @@ void RWindow::restoreFromFullscreen() {
|
||||
xev.xclient.data.l[1] = fullscreenmode;
|
||||
xev.xclient.data.l[2] = 0;
|
||||
XSendEvent(display, DefaultRootWindow(display), False, SubstructureNotifyMask | SubstructureRedirectMask, &xev);
|
||||
Logger::Debug(std::format("Restored window '{}' from fullscreen", this->title));
|
||||
Logger::Debug(std::format("Restored window '{}' from Fullscreen", this->title));
|
||||
}
|
||||
|
||||
void RWindow::setVsyncEnabled(bool b) {
|
||||
void RWindow::SetVsyncEnabled(bool b) {
|
||||
PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXT;
|
||||
glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddressARB((const GLubyte*)"glXSwapIntervalEXT");
|
||||
glXSwapIntervalEXT(display, window, b);
|
||||
}
|
||||
|
||||
bool RWindow::isFullscreen() const {
|
||||
return fullscreenmode;
|
||||
}
|
||||
|
||||
void RWindow::setCursorStyle(CursorStyle style) const {
|
||||
void RWindow::SetCursorStyle(CursorStyle style) const {
|
||||
u32 x11_cursor_resolved_enum = static_cast<u32>(style.X11Cursor);
|
||||
Cursor c = XCreateFontCursor(display, x11_cursor_resolved_enum);
|
||||
XDefineCursor(display, window, c);
|
||||
@@ -357,7 +329,7 @@ void RWindow::Open() {
|
||||
xSetWindowAttributes.background_pixel = BlackPixel(display, defaultScreen);
|
||||
xSetWindowAttributes.override_redirect = True;
|
||||
xSetWindowAttributes.event_mask = ExposureMask;
|
||||
//setVsyncEnabled(vsync);
|
||||
//SetVsyncEnabled(vsync);
|
||||
if (renderer == RenderingAPI::OPENGL) {
|
||||
GLint glAttributes[] = {GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None};
|
||||
visual = glXChooseVisual(display, defaultScreen, glAttributes);
|
||||
@@ -397,22 +369,17 @@ void RWindow::Open() {
|
||||
processOnOpen();
|
||||
}
|
||||
|
||||
void RWindow::setTitle(const std::string &title) {
|
||||
void RWindow::SetTitle(const std::string &title) {
|
||||
this->title = title;
|
||||
XStoreName(display, window, title.c_str());
|
||||
}
|
||||
|
||||
// TODO: Implement MouseButton map
|
||||
bool RWindow::isMouseButtonDown(MouseButton button) const {
|
||||
return false;
|
||||
}
|
||||
|
||||
Vector2 RWindow::getPositionOfRenderableArea() const {
|
||||
|
||||
Vector2 RWindow::GetPositionOfRenderableArea() const {
|
||||
return render_area_position;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// TODO: Implement ControllerButton map
|
||||
|
||||
|
@@ -33,27 +33,23 @@ RWindow::RWindow(const std::string& title, int width, int height, RenderingAPI r
|
||||
title(title), width(width), height(height), renderer(renderer) {}
|
||||
|
||||
Vector2 RWindow::GetMouseCoordinates() const {
|
||||
return mouse_coords;
|
||||
return currentMouse.Pos;
|
||||
}
|
||||
|
||||
bool RWindow::getFlag(RWindowFlags flag) const {
|
||||
bool RWindow::GetFlag(RWindowFlags flag) const {
|
||||
return flags[(int) flag];
|
||||
}
|
||||
|
||||
bool RWindow::isAlive() const {
|
||||
bool RWindow::IsAlive() const {
|
||||
return true;
|
||||
}
|
||||
|
||||
void RWindow::setResizable(bool resizable) {
|
||||
this->resizable = resizable;
|
||||
this->setFlag(RWindowFlags::RESIZABLE, resizable);
|
||||
}
|
||||
|
||||
void RWindow::setFullscreen(bool fs) {
|
||||
void RWindow::SetFullscreen(bool fs) {
|
||||
if (fs)
|
||||
fullscreen();
|
||||
Fullscreen();
|
||||
else
|
||||
restoreFromFullscreen();
|
||||
RestoreFromFullscreen();
|
||||
}
|
||||
|
||||
void RWindow::processFocusIn()
|
||||
@@ -72,13 +68,16 @@ void RWindow::processFocusOut()
|
||||
|
||||
void RWindow::processMousePress(MouseButton btn)
|
||||
{
|
||||
currentMouse.PressedBtns[btn] = true;
|
||||
auto eventData = MouseButtonDownEvent(btn);
|
||||
OnMouseButtonDownEvent(eventData);
|
||||
OnMouseButtonDown(eventData);
|
||||
OnMouseButtonDownEvent(eventData);
|
||||
|
||||
}
|
||||
|
||||
void RWindow::processMouseMove(Vector2 last_pos, Vector2 new_pos)
|
||||
{
|
||||
currentMouse.Pos = new_pos;
|
||||
auto eventData = MouseMoveEvent(new_pos);
|
||||
OnMouseMove(eventData);
|
||||
OnMouseMoveEvent(eventData);
|
||||
@@ -86,9 +85,11 @@ void RWindow::processMouseMove(Vector2 last_pos, Vector2 new_pos)
|
||||
|
||||
void RWindow::processMouseRelease(MouseButton btn)
|
||||
{
|
||||
currentMouse.PressedBtns[btn] = false;
|
||||
auto eventData = MouseButtonUpEvent(btn);
|
||||
OnMouseButtonUpEvent(eventData);
|
||||
OnMouseButtonUp(eventData);
|
||||
OnMouseButtonUpEvent(eventData);
|
||||
|
||||
}
|
||||
|
||||
void RWindow::processKeyRelease(Key key) {
|
||||
@@ -99,53 +100,60 @@ void RWindow::processKeyRelease(Key key) {
|
||||
}
|
||||
|
||||
|
||||
std::string RWindow::getTitle() const {
|
||||
std::string RWindow::GetTitle() const {
|
||||
return this->title;
|
||||
}
|
||||
|
||||
/*
|
||||
Vector2 RWindow::getSize() const
|
||||
Vector2 RWindow::GetSize() const
|
||||
{
|
||||
return {this->width, this->height};
|
||||
}
|
||||
*/
|
||||
|
||||
int RWindow::getWidth() const
|
||||
int RWindow::GetWidth() const
|
||||
{
|
||||
return this->width;
|
||||
}
|
||||
|
||||
int RWindow::getHeight() const
|
||||
int RWindow::GetHeight() const
|
||||
{
|
||||
return this->height;
|
||||
}
|
||||
|
||||
void RWindow::setSizeWithoutEvent(const Vector2& size) {
|
||||
void RWindow::SetSizeWithoutEvent(const Vector2& size) {
|
||||
width = size.x;
|
||||
height = size.y;
|
||||
}
|
||||
|
||||
void RWindow::setLastKnownWindowSize(const Vector2& size) {
|
||||
lastKnownWindowSize = size;
|
||||
}
|
||||
//void RWindow::setLastKnownWindowSize(const Vector2& size) {
|
||||
// lastKnownWindowSize = size;
|
||||
//}
|
||||
|
||||
void RWindow::setRenderer(RenderingAPI api) {
|
||||
void RWindow::SetRenderer(RenderingAPI api) {
|
||||
renderer = api;
|
||||
}
|
||||
|
||||
void RWindow::setSize(const Vector2& size) {
|
||||
void RWindow::SetSize(const Vector2& size) {
|
||||
this->width = size.x;
|
||||
this->height = size.y;
|
||||
this->setSize(size.x, size.y);
|
||||
this->SetSize(size.x, size.y);
|
||||
}
|
||||
|
||||
bool RWindow::isKeyDown(Key key) const {
|
||||
bool RWindow::IsKeyDown(Key key) const {
|
||||
for (const auto& pair : currentKeyboard.PressedKeys)
|
||||
if (pair.first == key && pair.second)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool RWindow::IsMouseButtonDown(MouseButton button) const {
|
||||
// TODO: Implement MouseButton map
|
||||
for (const auto& pair : currentMouse.PressedBtns)
|
||||
if (pair.first == button && pair.second)
|
||||
return true;
|
||||
}
|
||||
|
||||
void RWindow::processKeyPress(Key key) {
|
||||
currentKeyboard.PressedKeys[key] = true;
|
||||
auto eventData = KeyDownEvent(key);
|
||||
@@ -165,8 +173,73 @@ void RWindow::processOnOpen()
|
||||
OnOpenEvent();
|
||||
}
|
||||
|
||||
void RWindow::ManagedRefresh()
|
||||
{
|
||||
auto begin = GetTimestamp();
|
||||
Refresh();
|
||||
auto end = GetTimestamp();
|
||||
|
||||
float dt = ComputeElapsedFrameTimeSeconds(begin, end);
|
||||
|
||||
UpdateFrameTiming(dt);
|
||||
}
|
||||
|
||||
void RWindow::Refresh() {
|
||||
PollEvents();
|
||||
OnRefresh(delta_time);
|
||||
|
||||
// Only call once and cache the result.
|
||||
currentMouse.Pos = GetAccurateMouseCoordinates();
|
||||
|
||||
/// TODO: Implement optional minimum epsilon to trigger a Mouse Update.
|
||||
if (currentMouse.Pos != previousMouse.Pos) {
|
||||
processMouseMove(previousMouse.Pos, currentMouse.Pos);
|
||||
previousMouse.Pos = currentMouse.Pos;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
float RWindow::ComputeElapsedFrameTimeSeconds(RWindow::high_res_timestamp start, RWindow::high_res_timestamp end) {
|
||||
auto frame_time = end - start;
|
||||
unsigned long int frame_time_us = std::chrono::duration_cast<std::chrono::microseconds>(frame_time).count();
|
||||
float frame_time_s = frame_time_us / (1000.f * 1000.f);
|
||||
return frame_time_s;
|
||||
}
|
||||
|
||||
RWindow::high_res_timestamp RWindow::GetTimestamp() {
|
||||
return std::chrono::high_resolution_clock::now();
|
||||
}
|
||||
|
||||
void RWindow::UpdateFrameTiming(float frame_time) {
|
||||
delta_time = frame_time;
|
||||
refresh_rate = 1.f / delta_time;
|
||||
refresh_rate_prev_5 = refresh_rate_prev_4;
|
||||
refresh_rate_prev_4 = refresh_rate_prev_3;
|
||||
refresh_rate_prev_3 = refresh_rate_prev_2;
|
||||
refresh_rate_prev_2 = refresh_rate_prev_1;
|
||||
refresh_rate_prev_1 = refresh_rate;
|
||||
avg_refresh_rate = (refresh_rate_prev_1 + refresh_rate_prev_2 + refresh_rate_prev_3 + refresh_rate_prev_4 + refresh_rate_prev_5) / 5.f;
|
||||
refresh_count++;
|
||||
}
|
||||
|
||||
void RWindow::Close() {
|
||||
/// TODO: Implement closing the window without destroying the handle.
|
||||
processOnClose();
|
||||
}
|
||||
}
|
||||
|
||||
bool RWindow::IsResizable() const {
|
||||
return resizable;
|
||||
}
|
||||
|
||||
bool RWindow::IsFullscreen() const {
|
||||
return fullscreenmode;
|
||||
}
|
||||
|
||||
bool RWindow::IsFocused() const {
|
||||
return focused;
|
||||
}
|
||||
|
||||
bool RWindow::IsVsyncEnabled() const {
|
||||
return vsync;
|
||||
}
|
||||
|
||||
|
@@ -26,7 +26,7 @@ void RWindow::setFlag(RWindowFlags flag, bool state) {
|
||||
}
|
||||
}
|
||||
|
||||
void RWindow::pollEvents() {
|
||||
void RWindow::PollEvents() {
|
||||
MSG msg;
|
||||
while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) {
|
||||
TranslateMessage(&msg);
|
||||
@@ -34,10 +34,10 @@ void RWindow::pollEvents() {
|
||||
}
|
||||
}
|
||||
|
||||
void RWindow::setSize(int newWidth, int newHeight) {
|
||||
if (!getFlag(RWindowFlags::RESIZABLE)) return;
|
||||
this->width = newWidth;
|
||||
this->height = newHeight;
|
||||
void RWindow::SetSize(int newWidth, int newHeight) {
|
||||
if (!resizable) return;
|
||||
this->width = width;
|
||||
this->height = height;
|
||||
SetWindowPos(hwnd, nullptr, 0, 0, width, height, SWP_NOMOVE | SWP_NOZORDER);
|
||||
}
|
||||
|
||||
@@ -48,36 +48,36 @@ Vector2 RWindow::getCursorPos() const {
|
||||
return { (float)point.x, (float)point.y };
|
||||
}
|
||||
|
||||
void RWindow::setCursorVisible(bool cursor_enable) {
|
||||
void RWindow::SetCursorVisible(bool cursor_enable) {
|
||||
cursor_visible = cursor_enable;
|
||||
}
|
||||
|
||||
bool RWindow::getCursorVisible() {
|
||||
bool RWindow::GetCursorVisible() {
|
||||
return cursor_visible;
|
||||
}
|
||||
|
||||
|
||||
Vector2 RWindow::getSize() const {
|
||||
Vector2 RWindow::GetSize() const {
|
||||
RECT rect;
|
||||
GetClientRect(hwnd, &rect);
|
||||
return { (float)(rect.right - rect.left), (float)(rect.bottom - rect.top) };
|
||||
}
|
||||
|
||||
void RWindow::setPos(int x, int y) {
|
||||
void RWindow::SetPos(int x, int y) {
|
||||
SetWindowPos(hwnd, nullptr, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
|
||||
}
|
||||
|
||||
void RWindow::setPos(const Vector2& pos) {
|
||||
void RWindow::SetPos(const Vector2& pos) {
|
||||
setPos(pos.x, pos.y);
|
||||
}
|
||||
|
||||
void RWindow::fullscreen() {
|
||||
void RWindow::Fullscreen() {
|
||||
// Implement fullscreen
|
||||
SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_OVERLAPPEDWINDOW);
|
||||
SetWindowPos(hwnd, HWND_TOP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), SWP_FRAMECHANGED | SWP_NOOWNERZORDER);
|
||||
}
|
||||
|
||||
void RWindow::restoreFromFullscreen() {
|
||||
void RWindow::RestoreFromFullscreen() {
|
||||
// Implement restore from fullscreen
|
||||
SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) | WS_OVERLAPPEDWINDOW);
|
||||
SetWindowPos(hwnd, nullptr, 0, 0, width, height, SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOOWNERZORDER);
|
||||
@@ -280,7 +280,7 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
||||
return DefWindowProc(hwnd, uMsg, wParam, lParam);
|
||||
}
|
||||
|
||||
void RWindow::setCursorStyle(CursorStyle style) const {}
|
||||
void RWindow::SetCursorStyle(CursorStyle style) const {}
|
||||
|
||||
void RWindow::Open() {
|
||||
eWindow = this;
|
||||
@@ -334,11 +334,11 @@ void RWindow::Open() {
|
||||
open = true;
|
||||
}
|
||||
|
||||
void RWindow::glSwapBuffers() {
|
||||
void RWindow::GLSwapBuffers() {
|
||||
SwapBuffers(hdc);
|
||||
}
|
||||
|
||||
void RWindow::refresh() {
|
||||
void RWindow::Refresh() {
|
||||
// TODO: Implement refresh time keeping
|
||||
OnRefresh(0.f);
|
||||
|
||||
|
7
src/types/WindowEvents.cpp
Normal file
7
src/types/WindowEvents.cpp
Normal file
@@ -0,0 +1,7 @@
|
||||
#include <rewindow/types/WindowEvents.hpp>
|
||||
|
||||
|
||||
namespace ReWindow
|
||||
{
|
||||
|
||||
}
|
@@ -14,6 +14,10 @@ bool MouseButton::operator==(const MouseButton &mb) const {
|
||||
return (mb.ButtonIndex == this->ButtonIndex);
|
||||
}
|
||||
|
||||
bool MouseButton::operator<(const MouseButton &rhs) const {
|
||||
return (this->CharCode < rhs.CharCode);
|
||||
}
|
||||
|
||||
|
||||
MouseButton GetMouseButtonFromXButton(unsigned int button) {
|
||||
switch(button) {
|
||||
|
Reference in New Issue
Block a user