Compare commits
40 Commits
Prerelease
...
ReWindow-R
Author | SHA1 | Date | |
---|---|---|---|
dd4b7b7d29 | |||
a68ced6874 | |||
d5a08ecea2 | |||
df32610007 | |||
921f2118e8 | |||
780fd8308a | |||
20b6f1041f | |||
94960e0433 | |||
2a63ad8054 | |||
cbfce17d44 | |||
1f17244662 | |||
5ff4fb2a73 | |||
30f20394c8 | |||
4264f008c2 | |||
4ab07d97e3 | |||
cb5222e476 | |||
e0c001b1f1 | |||
fdde8486f7 | |||
51e3787d38 | |||
66c3ebb9da | |||
2a2293842a | |||
1950aee948 | |||
2d9f7e08b0 | |||
561e806b5f | |||
fa807256af | |||
b10a4d1d39 | |||
a3adbb6266 | |||
b75f44f1ee | |||
e4e860564c | |||
fb6825224f | |||
6b9bf4c2cc | |||
f25370cada | |||
f826b2a268 | |||
5dfa0fe114 | |||
216b7218e6 | |||
dc466e695f | |||
a4322c4b21 | |||
db63cc2272 | |||
1d9424a243 | |||
2e0d84d559 |
@@ -4,7 +4,11 @@
|
||||
|
||||
namespace ReWindow::Logger {
|
||||
using namespace jlog;
|
||||
using Logger = GenericLogger;
|
||||
|
||||
extern GenericLogger Fatal;
|
||||
extern GenericLogger Debug;
|
||||
extern Logger Info;
|
||||
extern Logger Fatal;
|
||||
extern Logger Error;
|
||||
extern Logger Warning;
|
||||
extern Logger Debug;
|
||||
}
|
||||
|
85
include/rewindow/types/WindowEvents.hpp
Normal file
85
include/rewindow/types/WindowEvents.hpp
Normal file
@@ -0,0 +1,85 @@
|
||||
#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 MouseWheelEvent : public MouseEvent
|
||||
{
|
||||
public:
|
||||
int WheelMovement;
|
||||
MouseWheelEvent() = default;
|
||||
MouseWheelEvent(int wheel) : MouseEvent(), WheelMovement(wheel) {}
|
||||
};
|
||||
|
||||
class MouseButtonDownEvent : public MouseEvent {
|
||||
public:
|
||||
MouseButton Button;
|
||||
|
||||
MouseButtonDownEvent() = default;
|
||||
MouseButtonDownEvent(MouseButton button) : MouseEvent(), Button(button) {}
|
||||
};
|
||||
|
||||
class MouseButtonUpEvent : public MouseEvent {
|
||||
public:
|
||||
MouseButton Button;
|
||||
MouseButtonUpEvent() = default;
|
||||
MouseButtonUpEvent(MouseButton button) : MouseEvent(), Button(button) {}
|
||||
};
|
||||
|
||||
class WindowResizeRequestEvent : public RWindowEvent
|
||||
{
|
||||
public:
|
||||
Vector2 Size;
|
||||
};
|
||||
}
|
@@ -10,17 +10,18 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace ReWindow
|
||||
{
|
||||
namespace ReWindow {
|
||||
class Gamepad;
|
||||
class Xbox360Gamepad;
|
||||
class XboxOneGamepad;
|
||||
class PS4Gamepad;
|
||||
class Ultimate8BitDoPro2Gamepad;
|
||||
}
|
||||
|
||||
class InputDevice {}; // TODO: Remember to break InputDevice into it's own file and not define it twice!!!
|
||||
class ReWindow::Gamepad {
|
||||
public:
|
||||
virtual ~Gamepad() = default;
|
||||
};
|
||||
|
||||
class Gamepad : public InputDevice
|
||||
{
|
||||
|
||||
};
|
||||
|
||||
|
||||
class XboxGamepad : public Gamepad {};
|
||||
class PS4Gamepad : public Gamepad {};
|
||||
}
|
||||
class ReWindow::XboxOneGamepad : public Gamepad {};
|
||||
class ReWindow::PS4Gamepad : public Gamepad {};
|
@@ -9,67 +9,101 @@
|
||||
/// @edit 2024-07-29
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <J3ML/LinearAlgebra.hpp>
|
||||
namespace ReWindow {
|
||||
class GamepadButton;
|
||||
class GamepadTrigger;
|
||||
class GamepadThumbstick;
|
||||
}
|
||||
|
||||
class GamepadButton {
|
||||
class ReWindow::GamepadButton {
|
||||
protected:
|
||||
std::string mnemonic_btn_code;
|
||||
public:
|
||||
explicit GamepadButton(const std::string& mnemonic) : mnemonic_btn_code(mnemonic) { }
|
||||
[[nodiscard]] std::string GetMnemonicButtonCode() const { return mnemonic_btn_code; }
|
||||
explicit GamepadButton(std::string mnemonic) : mnemonic_btn_code(std::move(mnemonic)){}
|
||||
[[nodiscard]] std::string GetMnemonicButtonCode() const;
|
||||
|
||||
/// Compares two GamepadButtons by their mnemonic button codes, not their activation state.
|
||||
bool operator ==(const GamepadButton& rhs) const;
|
||||
};
|
||||
|
||||
class GamepadTrigger {
|
||||
class ReWindow::GamepadTrigger {
|
||||
protected:
|
||||
std::string mnemonic_trigger_code;
|
||||
/// A float value between 0 and 1 representing how much the trigger has been pushed in by.
|
||||
float position = 0.0f;
|
||||
|
||||
/// The minimum possible movement of the trigger from the at-rest position.
|
||||
/// Movements less than this won't count.
|
||||
float dead_zone = 0.0f;
|
||||
public:
|
||||
|
||||
/// Returns a float value between 0-1 representing how much the trigger has been pushed in by.
|
||||
/// (0 being unpressed, 1 being fully pressed)
|
||||
float GetActuation() const;
|
||||
|
||||
/// TODO: Might be more appropriate in the Gamepad class representation.
|
||||
void SetActuationThreshold(float minimum = 0.01f) const;
|
||||
void SetDeadzone(float minimum = 0.01f);
|
||||
[[nodiscard]] float GetActuation() const;
|
||||
public:
|
||||
explicit GamepadTrigger(std::string mnemonic) : mnemonic_trigger_code(std::move(mnemonic)){}
|
||||
};
|
||||
|
||||
class GamepadThumbstick
|
||||
{
|
||||
class ReWindow::GamepadThumbstick {
|
||||
protected:
|
||||
std::string mnemonic_stick_code;
|
||||
// X -1 is all the way left, X +1 is all the way right.
|
||||
// Y -1 is all the way down, Y +1 is all the way up.
|
||||
Vector2 position = {0.0f, 0.0f};
|
||||
|
||||
// The minimum possible movement from the center in any direction.
|
||||
float dead_zone = 0.0f;
|
||||
public:
|
||||
/// Returns a Vector2 value representing the x,y coordinates of the joystick, with 0,0 being the center (at rest).
|
||||
/// This vector ranges from length = 0 to length = 1 (i.e. the unit circle).
|
||||
[[nodiscard]] Vector2 GetPosition() const;
|
||||
|
||||
|
||||
/// Sets the deadzone range of the thumbstick.
|
||||
/// Deadzone controls how far the stick must be moved before any movement is actually reported.
|
||||
/// This is because the thumbstick at-rest will often still report movement.
|
||||
/// If gamecode is architected to use the thumbstick position as a direction, without factoring in magnitude, this would cause problems.
|
||||
void SetDeadzone(float minimum = 0.01f) const;
|
||||
void SetDeadzone(float minimum = 0.01f);
|
||||
public:
|
||||
explicit GamepadThumbstick(std::string mnemonic) : mnemonic_stick_code(std::move(mnemonic)){}
|
||||
};
|
||||
|
||||
|
||||
|
||||
using J3ML::LinearAlgebra::Vector2;
|
||||
|
||||
namespace GamepadButtons {
|
||||
static const GamepadButton X {"X"};
|
||||
static const GamepadButton Y {"Y"};
|
||||
static const GamepadButton A {"A"};
|
||||
static const GamepadButton B {"B"};
|
||||
namespace ReWindow::GamepadButtons {
|
||||
static const GamepadButton Triangle("△");
|
||||
static const GamepadButton Square("□");
|
||||
static const GamepadButton Circle("○");
|
||||
static const GamepadButton Cross("X");
|
||||
|
||||
static const auto Triangle = Y;
|
||||
static const auto Square = X;
|
||||
static const auto Circle = A;
|
||||
static const auto Cross = B;
|
||||
// If you like xbox :shrug:
|
||||
static const GamepadButton Y = Triangle;
|
||||
static const GamepadButton X = Square;
|
||||
static const GamepadButton A = Cross;
|
||||
static const GamepadButton B = Circle;
|
||||
|
||||
static const GamepadButton LButton("LB");
|
||||
static const GamepadButton RButton("RB");
|
||||
|
||||
static const GamepadButton LeftBumper {"LB"};
|
||||
static const GamepadButton RightBumper {"RB"};
|
||||
/* For controllers where L2 & R2 is a button or counts as one when pressed all the way down.
|
||||
* Gamecube & PS2 controllers do this. - Redacted. */
|
||||
static const GamepadButton LButton2("LB2");
|
||||
static const GamepadButton RButton2("RB2");
|
||||
|
||||
// The buttons when you press in the sticks.
|
||||
static const GamepadButton LButton3("LB3");
|
||||
static const GamepadButton RButton3("RB3");
|
||||
|
||||
// Will eventually need to handle making it not possible to, for ex. Press left and right at the same time.
|
||||
static const GamepadButton DPadUp("DPU");
|
||||
static const GamepadButton DPadDown("DPD");
|
||||
static const GamepadButton DPadLeft("DPL");
|
||||
static const GamepadButton DPadRight("DPR");
|
||||
|
||||
static const GamepadButton Select("SEL");
|
||||
static const GamepadButton Start("START");
|
||||
}
|
||||
|
||||
namespace GamepadTriggers
|
||||
{
|
||||
static const GamepadTrigger Left;
|
||||
static const GamepadTrigger Right;
|
||||
namespace ReWindow::GamepadTriggers {
|
||||
static const ReWindow::GamepadTrigger Left {"LT"};
|
||||
static const ReWindow::GamepadTrigger Right {"RT"};
|
||||
}
|
||||
|
||||
namespace ReWindow::GamepadThumbsticks {
|
||||
static const GamepadThumbstick Left {"LS"};
|
||||
static const GamepadThumbstick Right {"RS"};
|
||||
}
|
@@ -19,9 +19,9 @@ private:
|
||||
|
||||
public:
|
||||
static std::vector<Key> GetKeyboard();
|
||||
Key();
|
||||
Key() = default;
|
||||
Key(const char* charcode, X11Scancode scancode, WindowsScancode wSc);
|
||||
const char* CharCode;
|
||||
std::string Mnemonic;
|
||||
X11Scancode x11ScanCode;
|
||||
WindowsScancode winScanCode;
|
||||
bool operator==(const Key& rhs) const;
|
||||
@@ -147,7 +147,7 @@ namespace Keys {
|
||||
|
||||
}
|
||||
|
||||
|
||||
enum class KeyState {Pressed, Released};
|
||||
|
||||
|
||||
|
||||
|
@@ -10,27 +10,35 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
class MouseButton {
|
||||
public:
|
||||
MouseButton();
|
||||
explicit MouseButton(const char* charcode, unsigned int index);
|
||||
const char* CharCode;
|
||||
std::string Mnemonic;
|
||||
unsigned int ButtonIndex;
|
||||
|
||||
MouseButton() = default;
|
||||
explicit MouseButton(const std::string& charcode, unsigned int index);
|
||||
|
||||
bool operator == (const MouseButton& mb) const;
|
||||
bool operator<(const MouseButton& rhs) const;
|
||||
MouseButton(const MouseButton&) = default;
|
||||
};
|
||||
|
||||
|
||||
namespace MouseButtons
|
||||
{
|
||||
static const MouseButton Left {"l", 1};
|
||||
static const MouseButton Right {"r", 2};
|
||||
static const MouseButton Middle {"m", 3};
|
||||
static const MouseButton MWheelUp {"1", 4};
|
||||
static const MouseButton MWheelDown {"2", 5};
|
||||
static const MouseButton Mouse4 {"4", 8};
|
||||
static const MouseButton Mouse5 {"5", 9};
|
||||
static const MouseButton Unimplemented {"u", 0};
|
||||
static const MouseButton Left ("L", 1);
|
||||
static const MouseButton Right ("R", 2);
|
||||
static const MouseButton Middle ("M", 3);
|
||||
static const MouseButton Mouse4 ("4", 8);
|
||||
static const MouseButton Mouse5 ("5", 9);
|
||||
static const MouseButton Unimplemented ("?", 0);
|
||||
|
||||
/// NOTE: IsMouseButtonDown will not return correctly for the mouse-wheel-buttons, because the action is effectively instantaneous.
|
||||
static const MouseButton MWheelUp ("U", 4);
|
||||
/// NOTE: IsMouseButtonDown will not return correctly for the mouse-wheel-buttons, because the action is effectively instantaneous.
|
||||
static const MouseButton MWheelDown ("D", 5);
|
||||
}
|
||||
|
||||
MouseButton GetMouseButtonFromXButton(unsigned int button);
|
||||
|
@@ -9,347 +9,454 @@
|
||||
#include <rewindow/types/mousebutton.h>
|
||||
#include <rewindow/types/gamepadbutton.h>
|
||||
#include <J3ML/LinearAlgebra.hpp>
|
||||
#include <rewindow/types/WindowEvents.hpp>
|
||||
#include <format>
|
||||
#include <queue>
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
using precision_clock = std::chrono::high_resolution_clock;
|
||||
using precision_timestamp = precision_clock::time_point;
|
||||
#include <experimental/propagate_const>
|
||||
|
||||
// Figure out what to do with this shit
|
||||
enum class RWindowFlags: uint8_t {
|
||||
IN_FOCUS,
|
||||
FULLSCREEN,
|
||||
RESIZABLE,
|
||||
VSYNC,
|
||||
QUIT,
|
||||
MAX_FLAG
|
||||
IN_FOCUS,
|
||||
FULLSCREEN,
|
||||
RESIZABLE,
|
||||
VSYNC,
|
||||
QUIT,
|
||||
MAX_FLAG
|
||||
};
|
||||
|
||||
std::string RWindowFlagToStr(RWindowFlags flag);
|
||||
|
||||
enum class RenderingAPI: uint8_t {
|
||||
OPENGL = 0,
|
||||
//Vulkan is unimplemented.
|
||||
VULKAN = 1,
|
||||
};
|
||||
|
||||
enum class KeyState {Pressed, Released};
|
||||
|
||||
namespace ReWindow
|
||||
{
|
||||
using J3ML::LinearAlgebra::Vector2;
|
||||
|
||||
class TimestampedEvent {
|
||||
private:
|
||||
|
||||
public:
|
||||
precision_timestamp Timestamp;
|
||||
TimestampedEvent() : Timestamp(precision_clock::now())
|
||||
{ }
|
||||
};
|
||||
|
||||
class RWindowEvent : public TimestampedEvent {
|
||||
public:
|
||||
RWindowEvent() : TimestampedEvent() { }
|
||||
};
|
||||
const RWindowEvent EmptyRWindowEvent;
|
||||
|
||||
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(), Button(button) {}
|
||||
};
|
||||
|
||||
class MouseButtonUpEvent : public MouseEvent {
|
||||
public:
|
||||
MouseButton Button;
|
||||
MouseButtonUpEvent() = default;
|
||||
MouseButtonUpEvent(MouseButton button) : MouseEvent(), Button(button) {}
|
||||
};
|
||||
|
||||
class MouseWheelEvent : public MouseEvent
|
||||
{
|
||||
public:
|
||||
int Delta;
|
||||
MouseWheelEvent() = default;
|
||||
MouseWheelEvent(int delta) : MouseEvent(), Delta(delta) {}
|
||||
};
|
||||
|
||||
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 RWindow {
|
||||
public:
|
||||
|
||||
/// 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;
|
||||
Event<RWindowEvent> OnFocusGainEvent;
|
||||
Event<float> OnRefreshEvent;
|
||||
Event<WindowResizeRequestEvent> OnResizeRequestEvent;
|
||||
Event<KeyDownEvent> OnKeyDownEvent;
|
||||
Event<KeyUpEvent> OnKeyUpEvent;
|
||||
Event<MouseMoveEvent> OnMouseMoveEvent;
|
||||
Event<MouseButtonDownEvent> OnMouseButtonDownEvent;
|
||||
Event<MouseButtonUpEvent> OnMouseButtonUpEvent;
|
||||
Event<MouseWheelEvent> OnMouseWheelEvent;
|
||||
#pragma endregion
|
||||
#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() {}
|
||||
virtual void OnFocusLost(const RWindowEvent& e) {}
|
||||
virtual void OnFocusGain(const RWindowEvent& e) {}
|
||||
virtual void OnRefresh(float elapsed) {}
|
||||
virtual void OnResizeSuccess() {}
|
||||
virtual bool OnResizeRequest(const WindowResizeRequestEvent& e) { return true;}
|
||||
virtual void OnKeyDown(const KeyDownEvent&) {}
|
||||
virtual void OnKeyUp(const KeyUpEvent&) {}
|
||||
virtual void OnMouseMove(const MouseMoveEvent&) {}
|
||||
virtual void OnMouseButtonDown(const MouseButtonDownEvent&) {}
|
||||
virtual void OnMouseButtonUp(const MouseButtonUpEvent&) {}
|
||||
virtual void OnMouseWheel(const MouseWheelEvent&) {}
|
||||
#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.
|
||||
Vector2 GetMouseCoordinates() const;
|
||||
|
||||
/// Sets which rendering API is to be used with this window.
|
||||
void setRenderer(RenderingAPI api);
|
||||
|
||||
/// Initializes all state with the window manager and rendering API, then opens the window.
|
||||
void Open();
|
||||
|
||||
/// Cleans up and closes the window without destroying the handle.
|
||||
void Close();
|
||||
|
||||
void CloseAndReopenInPlace();
|
||||
|
||||
void MessageBox(); // TODO: Must be implemented from scratch as a Motif Window in x11
|
||||
|
||||
/// 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 isAlive() const; // TODO: Always returns true.
|
||||
|
||||
[[nodiscard]] bool isKeyDown(Key key) const;
|
||||
[[nodiscard]] bool isMouseButtonDown(MouseButton button) const;
|
||||
|
||||
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;
|
||||
// TODO: Josh hates parameter-flags, it's not 1995 :/
|
||||
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();
|
||||
|
||||
/// Reads events from the underlying window manager.
|
||||
/// TODO: Move out of public API, consumers should call refresh or ideally an update() call.
|
||||
void pollEvents();
|
||||
|
||||
/// 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;
|
||||
|
||||
/// 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;
|
||||
|
||||
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;
|
||||
|
||||
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);
|
||||
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;
|
||||
std::string title = "Redacted Window";
|
||||
int width = 1280;
|
||||
int height = 720;
|
||||
RenderingAPI renderer = RenderingAPI::OPENGL;
|
||||
bool open = false;
|
||||
bool resizable = true;
|
||||
|
||||
Vector2 mouse_coords = {0, 0};
|
||||
Vector2 last_mouse_coords = {0, 0};
|
||||
|
||||
// TODO: Implement ringbuffer / circular vector class of some sort.
|
||||
float refresh_rate_prev_1 = 0.f;
|
||||
float refresh_rate_prev_2 = 0.f;
|
||||
float refresh_rate_prev_3 = 0.f;
|
||||
float refresh_rate_prev_4 = 0.f;
|
||||
float refresh_rate_prev_5 = 0.f;
|
||||
|
||||
float avg_refresh_rate = 0.0f;
|
||||
|
||||
private:
|
||||
|
||||
|
||||
/// 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;
|
||||
public:
|
||||
/// Executes event handlers for keyboard rele;ase events.
|
||||
void processKeyRelease (Key key);
|
||||
/// Executes event handlers for keyboard press events.
|
||||
void processKeyPress (Key key);
|
||||
/// Executes event handlers for window close events.
|
||||
/// @note This will be invoked **before** the window-close procedure begins.
|
||||
void processOnClose();
|
||||
/// Executes event handlers for window open events.
|
||||
/// @note This will be invoked **after** the window-open procedure completes.
|
||||
void processOnOpen();
|
||||
/// 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();
|
||||
|
||||
void processMouseMove(Vector2 last_pos, Vector2 new_pos);
|
||||
|
||||
void processMouseWheel(int scrolls);
|
||||
|
||||
private:
|
||||
|
||||
|
||||
|
||||
// Just throwing this in the header for now
|
||||
std::string RWindowFlagToStr(RWindowFlags flag); /*{
|
||||
switch (flag) {
|
||||
case RWindowFlags::IN_FOCUS: return "IN_FOCUS";
|
||||
case RWindowFlags::FULLSCREEN: return "FULLSCREEN";
|
||||
case RWindowFlags::RESIZABLE: return "RESIZEABLE";
|
||||
case RWindowFlags::VSYNC: return "VSYNC";
|
||||
case RWindowFlags::QUIT: return "QUIT";
|
||||
case RWindowFlags::MAX_FLAG: return "MAX_FLAG";
|
||||
default:
|
||||
return "unimplemented flag";
|
||||
}
|
||||
};
|
||||
}
|
||||
*/
|
||||
|
||||
using J3ML::LinearAlgebra::Vector2;
|
||||
|
||||
namespace ReWindow {
|
||||
|
||||
|
||||
class KeyboardState {
|
||||
public:
|
||||
std::map<Key, bool> PressedKeys;
|
||||
};
|
||||
|
||||
class GamepadState {
|
||||
public:
|
||||
std::map<GamepadButton, bool> PressedButtons;
|
||||
};
|
||||
|
||||
class MouseState {
|
||||
public:
|
||||
struct
|
||||
{
|
||||
bool LMB = false;
|
||||
bool RMB = false;
|
||||
bool MMB = false;
|
||||
bool SideButton1 = false;
|
||||
bool SideButton2 = false;
|
||||
bool MWheelUp = false;
|
||||
bool MWheelDown = false;
|
||||
} Buttons;
|
||||
|
||||
|
||||
Vector2 Position;
|
||||
int Wheel = 0;
|
||||
|
||||
[[nodiscard]] bool IsDown(const MouseButton& btn) const
|
||||
{
|
||||
if (btn == MouseButtons::Left) return Buttons.LMB;
|
||||
if (btn == MouseButtons::Right) return Buttons.RMB;
|
||||
if (btn == MouseButtons::Middle) return Buttons.MMB;
|
||||
if (btn == MouseButtons::Mouse4) return Buttons.SideButton1;
|
||||
if (btn == MouseButtons::Mouse5) return Buttons.SideButton2;
|
||||
//if (btn == MouseButtons::MWheelUp) return Buttons.MWheelUp;
|
||||
//if (btn == MouseButtons::MWheelDown) return Buttons.MWheelDown;
|
||||
|
||||
return false; // Unknown button?
|
||||
}
|
||||
|
||||
void Set(const MouseButton& btn, bool state)
|
||||
{
|
||||
if (btn == MouseButtons::Left) Buttons.LMB = state;
|
||||
if (btn == MouseButtons::Right) Buttons.RMB = state;
|
||||
if (btn == MouseButtons::Middle) Buttons.MMB = state;
|
||||
if (btn == MouseButtons::Mouse4) Buttons.SideButton1 = state;
|
||||
if (btn == MouseButtons::Mouse5) Buttons.SideButton2 = state;
|
||||
//if (btn == MouseButtons::MWheelUp) Buttons.MWheelUp = state;
|
||||
//if (btn == MouseButtons::MWheelDown) Buttons.MWheelDown = state;
|
||||
}
|
||||
|
||||
bool& operator[](const MouseButton& btn)
|
||||
{
|
||||
if (btn == MouseButtons::Left) return Buttons.LMB;
|
||||
if (btn == MouseButtons::Right) return Buttons.RMB;
|
||||
if (btn == MouseButtons::Middle) return Buttons.MMB;
|
||||
if (btn == MouseButtons::Mouse4) return Buttons.SideButton1;
|
||||
if (btn == MouseButtons::Mouse5) return Buttons.SideButton2;
|
||||
//if (btn == MouseButtons::MWheelUp) return Buttons.MWheelUp;
|
||||
//if (btn == MouseButtons::MWheelDown) return Buttons.MWheelDown;
|
||||
|
||||
throw std::runtime_error("Attempted to handle unmapped mouse button");
|
||||
}
|
||||
};
|
||||
|
||||
enum class RenderingAPI: uint8_t {
|
||||
OPENGL = 0,
|
||||
//Vulkan is unimplemented.
|
||||
VULKAN = 1,
|
||||
};
|
||||
//
|
||||
|
||||
class RWindowImpl;
|
||||
class RWindow;
|
||||
}
|
||||
|
||||
class ReWindow::RWindowImpl {
|
||||
private:
|
||||
// Class for platform specific "global" variables, information, functionality, etc
|
||||
// It is private as it should not be accessed outside the Impl. It has also
|
||||
// purposefully been left undefined, so implementors can define it as they see fit.
|
||||
// Make sure to do proper cleanup of the class and pointer if utilized. It should
|
||||
// be handled with care.
|
||||
//
|
||||
// Example for initialization:
|
||||
// RWindowImpl::RWindowImpl(ARGS) : pPlatform( new Platform ) { XYZ }
|
||||
//
|
||||
// Example for destruction:
|
||||
// RWindowImpl::~RWindowImpl() {
|
||||
// if (pPlatform) { delete pPlatform; };
|
||||
// }
|
||||
//
|
||||
// Example for definition:
|
||||
// class RWindowImpl::Platform {
|
||||
// public:
|
||||
// Platform() = default;
|
||||
// public:
|
||||
// int a;
|
||||
// int b;
|
||||
// int c;
|
||||
// };
|
||||
//
|
||||
// - Maxine
|
||||
class Platform;
|
||||
Platform* pPlatform = nullptr;
|
||||
protected: // Should maybe make private
|
||||
int width = 1280;
|
||||
int height = 720;
|
||||
|
||||
bool open = false; // Is the underlying OS-Window-Handle actually open.
|
||||
bool resizable = true;
|
||||
bool fullscreen_mode = false;
|
||||
bool focused = true;
|
||||
bool vsync = false;
|
||||
bool cursor_visible = true;
|
||||
bool closing = false;
|
||||
|
||||
float delta_time = 0.f;
|
||||
float refresh_rate = 0.f;
|
||||
unsigned int refresh_count = 0;
|
||||
|
||||
std::string title = "Redacted Window";
|
||||
|
||||
Vector2 lastKnownWindowSize {0, 0};
|
||||
|
||||
// TODO: Implement ringbuffer / circular vector class of some sort.
|
||||
float refresh_rate_prev_1 = 0.f;
|
||||
float refresh_rate_prev_2 = 0.f;
|
||||
float refresh_rate_prev_3 = 0.f;
|
||||
float refresh_rate_prev_4 = 0.f;
|
||||
float refresh_rate_prev_5 = 0.f;
|
||||
|
||||
float avg_refresh_rate = 0.0f;
|
||||
|
||||
// Figure out what to do with this shit later
|
||||
bool flags[5];
|
||||
KeyboardState currentKeyboard; // current frame keyboard state.
|
||||
KeyboardState previousKeyboard; // previous frame keyboard state.
|
||||
MouseState currentMouse; // purrent frame mouse state.
|
||||
MouseState previousMouse; // previous frame mouse state
|
||||
RenderingAPI renderer = RenderingAPI::OPENGL;
|
||||
//
|
||||
public:
|
||||
explicit RWindowImpl(const std::string& wTitle,
|
||||
int wWidth = 640, int wHeight = 480,
|
||||
RenderingAPI wRenderer = RenderingAPI::OPENGL,
|
||||
bool wFullscreen = false,
|
||||
bool wResizable = true,
|
||||
bool wVsync = false);
|
||||
~RWindowImpl();
|
||||
public:
|
||||
void Open();
|
||||
void Close();
|
||||
void PollEvents();
|
||||
void Refresh();
|
||||
public:
|
||||
bool IsFocused() const;
|
||||
bool IsFullscreen() const;
|
||||
bool IsResizable() const;
|
||||
bool IsVsyncEnabled() const;
|
||||
bool IsAlive() const;
|
||||
public:
|
||||
void SetFullscreen(bool fs);
|
||||
void SetResizable(bool fs);
|
||||
void SetVsyncEnabled(bool vsync);
|
||||
void SetSize(int width, int height);
|
||||
void SetSize(const Vector2& size);
|
||||
// Added here for now
|
||||
void SetPos(int x, int y);
|
||||
void SetPos(const Vector2& pos);
|
||||
//
|
||||
void SetTitle(const std::string& title);
|
||||
public:
|
||||
std::string GetTitle() const;
|
||||
Vector2 GetPos() const;
|
||||
Vector2 GetSize() const;
|
||||
int GetWidth() const;
|
||||
int GetHeight() const;
|
||||
float GetDeltaTime() const;
|
||||
float GetRefreshRate() const;
|
||||
float GetRefreshCounter() const;
|
||||
public:
|
||||
// Figure out what to do with these later
|
||||
void Raise() const;
|
||||
void Lower() const;
|
||||
void DestroyOSWindowHandle();
|
||||
void SetCursorVisible(bool cursor_enable);
|
||||
Vector2 GetAccurateMouseCoordinates() const;
|
||||
void GLSwapBuffers();
|
||||
void Fullscreen();
|
||||
void RestoreFromFullscreen();
|
||||
// I know this doesn't modify the class, but it indirectly modifies the window
|
||||
// Making it const just seems deceptive.
|
||||
void SetCursorStyle(CursorStyle style) const;
|
||||
Vector2 GetPositionOfRenderableArea() const;
|
||||
std::string getGraphicsDriverVendor();
|
||||
void SetFlag(RWindowFlags flag, bool state);
|
||||
void processKeyRelease (Key key);
|
||||
void processKeyPress (Key key);
|
||||
void processOnClose();
|
||||
void processOnOpen();
|
||||
void processMousePress(const MouseButton& btn);
|
||||
void processMouseRelease(const MouseButton& btn);
|
||||
void processFocusIn();
|
||||
void processFocusOut();
|
||||
void processMouseMove(Vector2 last_pos, Vector2 new_pos);
|
||||
void processMouseWheel(int scrolls);
|
||||
virtual void OnOpen() {}
|
||||
virtual void OnClosing() {}
|
||||
virtual void OnFocusLost(const RWindowEvent& e) {}
|
||||
virtual void OnFocusGain(const RWindowEvent& e) {}
|
||||
virtual void OnRefresh(float elapsed) {}
|
||||
virtual void OnResizeSuccess() {}
|
||||
virtual bool OnResizeRequest(const WindowResizeRequestEvent& e) { return true;}
|
||||
virtual void OnKeyDown(const KeyDownEvent&) {}
|
||||
virtual void OnKeyUp(const KeyUpEvent&) {}
|
||||
virtual void OnMouseMove(const MouseMoveEvent&) {}
|
||||
virtual void OnMouseButtonDown(const MouseButtonDownEvent&) {}
|
||||
virtual void OnMouseButtonUp(const MouseButtonUpEvent&) {}
|
||||
virtual void OnMouseWheel(const MouseWheelEvent&) {}
|
||||
Event<> OnOpenEvent;
|
||||
Event<> OnClosingEvent;
|
||||
Event<RWindowEvent> OnFocusLostEvent;
|
||||
Event<RWindowEvent> OnFocusGainEvent;
|
||||
Event<float> OnRefreshEvent;
|
||||
Event<WindowResizeRequestEvent> OnResizeRequestEvent;
|
||||
Event<KeyDownEvent> OnKeyDownEvent;
|
||||
Event<KeyUpEvent> OnKeyUpEvent;
|
||||
Event<MouseMoveEvent> OnMouseMoveEvent;
|
||||
Event<MouseButtonDownEvent> OnMouseButtonDownEvent;
|
||||
Event<MouseButtonUpEvent> OnMouseButtonUpEvent;
|
||||
Event<MouseWheelEvent> OnMouseWheelEvent;
|
||||
|
||||
Vector2 GetMouseCoordinates() const;
|
||||
bool GetFlag(RWindowFlags flag) const;
|
||||
//bool IsAlive() const;
|
||||
void SetSizeWithoutEvent(const Vector2& size);
|
||||
std::vector<RWindowEvent> eventLog;
|
||||
std::queue<RWindowEvent> eventQueue;
|
||||
void LogEvent(const RWindowEvent& e) { eventLog.push_back(e);};
|
||||
RWindowEvent GetLastEvent() const {
|
||||
return eventLog.back();
|
||||
};
|
||||
void SetLastKnownWindowSize(const Vector2& size);
|
||||
void SetRenderer(RenderingAPI api);
|
||||
bool IsKeyDown(Key key) const;
|
||||
bool IsMouseButtonDown(const MouseButton &button) const;
|
||||
void ManagedRefresh();
|
||||
float ComputeElapsedFrameTimeSeconds(std::chrono::steady_clock::time_point start, std::chrono::steady_clock::time_point end);
|
||||
std::chrono::steady_clock::time_point GetTimestamp();
|
||||
void UpdateFrameTiming(float frame_time);
|
||||
/*
|
||||
bool IsResizable() const;
|
||||
bool IsFullscreen() const;
|
||||
bool IsFocused() const;
|
||||
bool IsVsyncEnabled() const;
|
||||
*/
|
||||
void ForceClose();
|
||||
void ForceCloseAndTerminateProgram();
|
||||
int GetMouseWheelPersistent() const { return currentMouse.Wheel;}
|
||||
[[nodiscard]] bool IsOpen() const { return open;}
|
||||
[[nodiscard]] bool IsClosing() const { return closing;}
|
||||
|
||||
|
||||
};
|
||||
|
||||
class ReWindow::RWindow : private RWindowImpl {
|
||||
public:
|
||||
//RWindow() = default;
|
||||
//RWindow();
|
||||
explicit RWindow(const std::string& wTitle,
|
||||
int wWidth = 640, int wHeight = 480,
|
||||
RenderingAPI wRenderer = RenderingAPI::OPENGL,
|
||||
bool wFullscreen = false,
|
||||
bool wResizable = true,
|
||||
bool wVsync = false) :
|
||||
RWindowImpl(wTitle, wWidth, wHeight, wRenderer, wFullscreen, wResizable, wVsync) {};
|
||||
~RWindow() = default;
|
||||
public:
|
||||
// Platform dependant
|
||||
void Open() { RWindowImpl::Open(); };
|
||||
void Close() { RWindowImpl::Close(); };
|
||||
void PollEvents() { RWindowImpl::PollEvents(); };
|
||||
void Refresh() { RWindowImpl::Refresh(); };
|
||||
public:
|
||||
// Shared
|
||||
/// Returns whether the window currently has mouse and/or keyboard focus.
|
||||
[[nodiscard]] bool IsFocused() const { return RWindowImpl::IsFocused(); };
|
||||
/// Returns whether the window is currently in Fullscreen.
|
||||
// TODO: Support Fullscreen, FullscreenWindowed, and Windowed?
|
||||
[[nodiscard]] bool IsFullscreen() const { return RWindowImpl::IsFullscreen(); } ;
|
||||
/// Returns whether the window can be resized.
|
||||
[[nodiscard]] bool IsResizable() const { return RWindowImpl::IsResizable(); } ;
|
||||
/// Returns whether V-Sync is enabled.
|
||||
[[nodiscard]] bool IsVsyncEnabled() const { return RWindowImpl::IsVsyncEnabled(); } ;
|
||||
/// 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 { return RWindowImpl::IsAlive(); } ;
|
||||
// Should have IsOpen since window could be closed then reopened
|
||||
public:
|
||||
// Platform dependant
|
||||
/// Sets whether or not to make the window fullscreen.
|
||||
/// @note This is implemented per-OS, and as such, it simply requests the OS to do what we want. No guarantee about follow-through can be given.
|
||||
void SetFullscreen(bool fs) { RWindowImpl::SetFullscreen(fs); };
|
||||
/// Sets whether or not to make the window resizable.
|
||||
/// @note This is implemented per-OS, and as such, it simply requests the OS to do what we want. No guarantee about follow-through can be given.
|
||||
void SetResizable(bool resizable) { RWindowImpl::SetResizable(resizable); };
|
||||
/// Sets whether or not to enable vertical synchronization.
|
||||
/// @note This is implemented per-OS, and as such, it simply requests the OS to do what we want. No guarantee about follow-through can be given.
|
||||
void SetVsyncEnabled(bool vsync) { RWindowImpl::SetVsyncEnabled(vsync); };
|
||||
/// Requests the operating system to change the window size.
|
||||
/// @param width
|
||||
/// @param height
|
||||
void SetSize(int width, int height) { RWindowImpl::SetSize(width, height); };
|
||||
/// Requests the operating system to change the window size.
|
||||
/// @param size
|
||||
void SetSize(const Vector2& size) { RWindowImpl::SetSize(size); };
|
||||
|
||||
// Shared
|
||||
/// Sets the title of this window.
|
||||
void SetTitle(const std::string& title) { RWindowImpl::SetTitle(title); };
|
||||
public:
|
||||
// Shared
|
||||
[[nodiscard]] std::string GetTitle() const { return RWindowImpl::GetTitle(); } ;
|
||||
/// Returns the position of the window's top-left corner relative to the display
|
||||
[[nodiscard]] Vector2 GetPos() const { return RWindowImpl::GetPos(); } ;
|
||||
/// Returns the known size of the window, in {x,y} pixel measurement.
|
||||
[[nodiscard]] Vector2 GetSize() const { return RWindowImpl::GetSize(); } ;
|
||||
/// Returns the horizontal length of the renderable area in pixels.
|
||||
[[nodiscard]] int GetWidth() const { return RWindowImpl::GetWidth(); } ;
|
||||
/// Returns the vertical length of the renderable area, in pixels.
|
||||
[[nodiscard]] int GetHeight() const { return RWindowImpl::GetHeight(); } ;
|
||||
/// Returns the amount of time, in seconds, between the current and last frame.
|
||||
/// Technically, no, it returns the elapsed time of the frame, start to finish.
|
||||
[[nodiscard]] float GetDeltaTime() const { return RWindowImpl::GetDeltaTime(); } ;
|
||||
/// Returns the approximate frames-per-second using delta time.
|
||||
[[nodiscard]] float GetRefreshRate() const { return RWindowImpl::GetRefreshRate(); } ;
|
||||
/// Returns the number of frames ran since the windows' creation.
|
||||
[[nodiscard]] float GetRefreshCounter() const { return RWindowImpl::GetRefreshCounter(); } ;
|
||||
public:
|
||||
// Figure out what to do with these later
|
||||
void Raise() const { RWindowImpl::Raise(); };
|
||||
void Lower() const { RWindowImpl::Lower(); };
|
||||
void DestroyOSWindowHandle() { RWindowImpl::DestroyOSWindowHandle(); };
|
||||
void SetCursorVisible(bool cursor_enable) { RWindowImpl::SetCursorVisible(cursor_enable); };
|
||||
Vector2 GetAccurateMouseCoordinates() const { return RWindowImpl::GetAccurateMouseCoordinates(); } ;
|
||||
void GLSwapBuffers() { RWindowImpl::GLSwapBuffers(); };
|
||||
void Fullscreen() { RWindowImpl::Fullscreen(); };
|
||||
void RestoreFromFullscreen() { RWindowImpl::RestoreFromFullscreen(); };
|
||||
// I know this doesn't modify the class, but it indirectly modifies the window
|
||||
// Making it const just seems deceptive.
|
||||
void SetCursorStyle(CursorStyle style) const { return RWindowImpl::SetCursorStyle(style); } ;
|
||||
Vector2 GetPositionOfRenderableArea() const { return RWindowImpl::GetPositionOfRenderableArea(); } ;
|
||||
std::string getGraphicsDriverVendor() { return RWindowImpl::getGraphicsDriverVendor(); };
|
||||
void SetFlag(RWindowFlags flag, bool state) { RWindowImpl::SetFlag(flag, state); };
|
||||
void processKeyRelease (Key key) { RWindowImpl::processKeyRelease(key); };
|
||||
void processKeyPress (Key key) { RWindowImpl::processKeyPress(key); };
|
||||
void processOnClose() { RWindowImpl::processOnClose(); };
|
||||
void processOnOpen() { RWindowImpl::processOnOpen(); };
|
||||
void processMousePress(const MouseButton& btn) { RWindowImpl::processMousePress(btn); };
|
||||
void processMouseRelease(const MouseButton& btn) { RWindowImpl::processMouseRelease(btn); };
|
||||
void processFocusIn() { RWindowImpl::processFocusIn(); };
|
||||
void processFocusOut() { RWindowImpl::processFocusOut(); };
|
||||
void processMouseMove(Vector2 last_pos, Vector2 new_pos) { RWindowImpl::processMouseMove(last_pos, new_pos); };
|
||||
void processMouseWheel(int scrolls) { RWindowImpl::processMouseWheel(scrolls); };
|
||||
virtual void OnOpen() {}
|
||||
virtual void OnClosing() {}
|
||||
virtual void OnFocusLost(const RWindowEvent& e) {}
|
||||
virtual void OnFocusGain(const RWindowEvent& e) {}
|
||||
virtual void OnRefresh(float elapsed) {}
|
||||
virtual void OnResizeSuccess() {}
|
||||
virtual bool OnResizeRequest(const WindowResizeRequestEvent& e) { return true;}
|
||||
virtual void OnKeyDown(const KeyDownEvent&) {}
|
||||
virtual void OnKeyUp(const KeyUpEvent&) {}
|
||||
virtual void OnMouseMove(const MouseMoveEvent&) {}
|
||||
virtual void OnMouseButtonDown(const MouseButtonDownEvent&) {}
|
||||
virtual void OnMouseButtonUp(const MouseButtonUpEvent&) {}
|
||||
virtual void OnMouseWheel(const MouseWheelEvent&) {}
|
||||
Event<> OnOpenEvent;
|
||||
Event<> OnClosingEvent;
|
||||
Event<RWindowEvent> OnFocusLostEvent;
|
||||
Event<RWindowEvent> OnFocusGainEvent;
|
||||
Event<float> OnRefreshEvent;
|
||||
Event<WindowResizeRequestEvent> OnResizeRequestEvent;
|
||||
Event<KeyDownEvent> OnKeyDownEvent;
|
||||
Event<KeyUpEvent> OnKeyUpEvent;
|
||||
Event<MouseMoveEvent> OnMouseMoveEvent;
|
||||
Event<MouseButtonDownEvent> OnMouseButtonDownEvent;
|
||||
Event<MouseButtonUpEvent> OnMouseButtonUpEvent;
|
||||
Event<MouseWheelEvent> OnMouseWheelEvent;
|
||||
|
||||
Vector2 GetMouseCoordinates() const { return RWindowImpl::GetMouseCoordinates(); };
|
||||
bool GetFlag(RWindowFlags flag) const { return RWindowImpl::GetFlag(flag); };
|
||||
//bool IsAlive() const { return RWindowImpl::IsAlive(); };
|
||||
void SetSizeWithoutEvent(const Vector2& size) { RWindowImpl::SetSizeWithoutEvent(size); };
|
||||
void LogEvent(const RWindowEvent& e) { RWindowImpl::LogEvent(e); };
|
||||
RWindowEvent GetLastEvent() const { return RWindowImpl::GetLastEvent(); }
|
||||
void SetLastKnownWindowSize(const Vector2& size) { RWindowImpl::SetLastKnownWindowSize(size); };
|
||||
void SetRenderer(RenderingAPI api) { RWindowImpl::SetRenderer(api); };
|
||||
bool IsKeyDown(Key key) const { return RWindowImpl::IsKeyDown(key); };
|
||||
bool IsMouseButtonDown(const MouseButton &button) const { return RWindowImpl::IsMouseButtonDown(button); };
|
||||
void ManagedRefresh() { RWindowImpl::ManagedRefresh(); };
|
||||
float ComputeElapsedFrameTimeSeconds(std::chrono::steady_clock::time_point start, std::chrono::steady_clock::time_point end) { return RWindowImpl::ComputeElapsedFrameTimeSeconds(start, end); };
|
||||
std::chrono::steady_clock::time_point GetTimestamp() { return RWindowImpl::GetTimestamp(); };
|
||||
void UpdateFrameTiming(float frame_time) { RWindowImpl::UpdateFrameTiming(frame_time); };
|
||||
//bool IsResizable() const { return RWindowImpl::IsResizable(); };
|
||||
//bool IsFullscreen() const { return RWindowImpl::IsFullscreen(); };
|
||||
//bool IsFocused() const { return RWindowImpl::IsFocused(); };
|
||||
//bool IsVsyncEnabled() const { return RWindowImpl::IsVsyncEnabled(); };
|
||||
void ForceClose() { RWindowImpl::ForceClose(); };
|
||||
void ForceCloseAndTerminateProgram() { RWindowImpl::ForceCloseAndTerminateProgram(); };
|
||||
int GetMouseWheelPersistent() const { return RWindowImpl::GetMouseWheelPersistent();}
|
||||
[[nodiscard]] bool IsOpen() const { return RWindowImpl::IsOpen();}
|
||||
[[nodiscard]] bool IsClosing() const { return RWindowImpl::IsClosing();}
|
||||
|
||||
};
|
177
main.cpp
177
main.cpp
@@ -18,6 +18,10 @@ std::ostream& operator<<(std::ostream& os, const Vector2& v) {
|
||||
}
|
||||
|
||||
class MyWindow : public ReWindow::RWindow {
|
||||
public:
|
||||
float r = 255;
|
||||
float b = 0;
|
||||
float g = g;
|
||||
public:
|
||||
MyWindow(const std::string& title, int w, int h) : ReWindow::RWindow(title, w, h) {}
|
||||
|
||||
@@ -26,40 +30,67 @@ class MyWindow : public ReWindow::RWindow {
|
||||
void OnKeyDown(const ReWindow::KeyDownEvent& e) override {}
|
||||
|
||||
void OnRefresh(float elapsed) override {
|
||||
glClearColor(255, 0, 0, 255);
|
||||
if (r >= 1)
|
||||
r = 1;
|
||||
if (b >= 1)
|
||||
b = 1;
|
||||
if (g >= 1)
|
||||
g = 1;
|
||||
glClearColor(r, g, b, 255);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glSwapBuffers();
|
||||
GLSwapBuffers();
|
||||
auto pos = GetMouseCoordinates();
|
||||
//std::cout << pos.x << ", " << pos.y << std::endl;
|
||||
|
||||
//if (IsMouseButtonDown(MouseButtons::MWheelUp))
|
||||
//std::cout << "WheelUp Mouse Button" << std::endl;
|
||||
|
||||
//if (IsMouseButtonDown(MouseButtons::MWheelDown))
|
||||
//std::cout << "WheelDown Mouse Button" << std::endl;
|
||||
|
||||
if (IsMouseButtonDown(MouseButtons::Left))
|
||||
std::cout << "Left Mouse Button" << std::endl;
|
||||
|
||||
if (IsMouseButtonDown(MouseButtons::Right))
|
||||
std::cout << "Right Mouse Button" << std::endl;
|
||||
|
||||
if (IsMouseButtonDown(MouseButtons::Middle))
|
||||
std::cout << "Middle Mouse Button" << std::endl;
|
||||
|
||||
if (IsMouseButtonDown(MouseButtons::Mouse4))
|
||||
std::cout << "Mouse4 Mouse Button" << std::endl;
|
||||
|
||||
if (IsMouseButtonDown(MouseButtons::Mouse5))
|
||||
std::cout << "Mouse5 Mouse Button" << std::endl;
|
||||
|
||||
|
||||
if (IsKeyDown(Keys::N)) {
|
||||
std::cout << "Gotteem" << std::endl;
|
||||
}
|
||||
RWindow::OnRefresh(elapsed);
|
||||
}
|
||||
|
||||
bool OnResizeRequest(const ReWindow::WindowResizeRequestEvent& e) override {
|
||||
std::cout << "resized to " << e.Size.x << ", " << e.Size.y << std::endl;
|
||||
//std::cout << "resized to " << e.Size.x << ", " << e.Size.y << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
void OnMouseButtonDown(const ReWindow::MouseButtonDownEvent &e) override
|
||||
{
|
||||
//std::cout << "Overload Down: " << e.Button.Mnemonic << std::endl;
|
||||
RWindow::OnMouseButtonDown(e);
|
||||
}
|
||||
|
||||
void OnMouseButtonUp(const ReWindow::MouseButtonUpEvent &e) override
|
||||
{
|
||||
//std::cout << "Overload Up: " << e.Button.Mnemonic << std::endl;
|
||||
RWindow::OnMouseButtonUp(e);
|
||||
}
|
||||
};
|
||||
|
||||
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()));
|
||||
|
||||
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()));
|
||||
|
||||
// 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);
|
||||
|
||||
/*
|
||||
/*
|
||||
std::vector<ReWindow::Display> d = ReWindow::Display::getDisplaysFromWindowManager();
|
||||
for (ReWindow::Display& display : d) {
|
||||
|
||||
@@ -73,8 +104,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;
|
||||
@@ -84,28 +115,106 @@ int main() {
|
||||
}
|
||||
*/
|
||||
|
||||
MyWindow* window = new MyWindow("Test Window", 600, 480);
|
||||
jlog::Debug(std::format("New window '{}' created. width={} height={}", window->GetTitle(), window->GetWidth(),
|
||||
window->GetHeight()));
|
||||
|
||||
window->SetRenderer(ReWindow::RenderingAPI::OPENGL);
|
||||
jlog::Debug(std::format("Rendering API OPENGL set for window '{}'", window->GetTitle()));
|
||||
|
||||
window->Open();
|
||||
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);
|
||||
|
||||
|
||||
|
||||
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) {
|
||||
jlog::Debug(e.key.CharCode);
|
||||
jlog::Debug(e.key.Mnemonic);
|
||||
};
|
||||
|
||||
|
||||
window->OnMouseButtonDownEvent += [&] (ReWindow::MouseButtonDownEvent e) {
|
||||
jlog::Debug(e.Button.CharCode);
|
||||
jlog::Debug(e.Button.Mnemonic + std::to_string(e.Button.ButtonIndex));
|
||||
};
|
||||
|
||||
while (window->isAlive()) {
|
||||
window->pollEvents();
|
||||
window->refresh();
|
||||
}
|
||||
window->OnMouseWheelEvent += [&, window] (ReWindow::MouseWheelEvent e)
|
||||
{
|
||||
std::cout << window->GetMouseWheelPersistent() << std::endl;
|
||||
};
|
||||
|
||||
/*
|
||||
while (!window->IsClosing()) {
|
||||
//window->PollEvents();
|
||||
window->ManagedRefresh();
|
||||
}*/
|
||||
|
||||
// Found problem
|
||||
// free(): double free detected in tcache 2
|
||||
// delete window;
|
||||
// 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);
|
||||
* }
|
||||
*
|
||||
*/
|
||||
|
||||
//exit(0);
|
||||
|
||||
MyWindow* windowdos = new MyWindow("Test Window Dos", 600, 480);
|
||||
|
||||
windowdos->SetRenderer(ReWindow::RenderingAPI::OPENGL);
|
||||
|
||||
windowdos->Open();
|
||||
|
||||
window->r = 0;
|
||||
|
||||
bool swap;
|
||||
while (!windowdos->IsClosing() && !window->IsClosing()) {
|
||||
window->r += 0.01;
|
||||
if (window->r >= 1 && !swap) {
|
||||
window->g += 0.01;
|
||||
}
|
||||
if (window->g >= 1 && !swap)
|
||||
window->b += 0.01;
|
||||
if (window->b >= 1) {
|
||||
window->r = 0.01;
|
||||
window->g = 0.01;
|
||||
window->b = 0.01;
|
||||
}
|
||||
|
||||
|
||||
|
||||
window->ManagedRefresh();
|
||||
windowdos->ManagedRefresh();
|
||||
//sleep(10);
|
||||
}
|
||||
delete window;
|
||||
delete windowdos;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
@@ -2,6 +2,10 @@
|
||||
|
||||
namespace ReWindow::Logger {
|
||||
using namespace jlog;
|
||||
GenericLogger Fatal {"ReWindow::fatal", GlobalLogFile, Colors::Reds::Crimson, Colors::Gray, Colors::Gray, Colors::Reds::Crimson, Colors::White};
|
||||
GenericLogger Debug {"ReWindow::debug", GlobalLogFile, Colors::Purples::Purple, Colors::Gray, Colors::Gray, Colors::Purples::Purple, Colors::White};
|
||||
using namespace Colors;
|
||||
using Logger = GenericLogger;
|
||||
|
||||
Logger Info {"ReWindow", GlobalLogFile, Gray, Gray, Gray, Gray};
|
||||
Logger Fatal {"ReWindow::fatal", GlobalLogFile, Reds::Crimson, Gray, Gray, Reds::Crimson, White};
|
||||
Logger Debug {"ReWindow::debug", GlobalLogFile, Purples::Purple, Gray, Gray, Purples::Purple, White};
|
||||
}
|
||||
|
@@ -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);
|
||||
|
@@ -16,6 +16,9 @@
|
||||
// So should we do derived platform-specific subclasses?
|
||||
// The intended goal of the ReWindow class is a one-stop object that handles window management on **ALL** platforms
|
||||
|
||||
using namespace ReWindow;
|
||||
|
||||
/*
|
||||
Window window;
|
||||
XEvent xev;
|
||||
Display* display = XOpenDisplay(nullptr);
|
||||
@@ -34,190 +37,234 @@ Cursor invisible_cursor = 0;
|
||||
Vector2 render_area_position = {0, 0};
|
||||
Vector2 position = {0, 0};
|
||||
bool should_poll_x_for_mouse_pos = true;
|
||||
*/
|
||||
|
||||
class RWindowImpl::Platform {
|
||||
public:
|
||||
Platform() = default;
|
||||
public:
|
||||
Window window;
|
||||
XEvent xev;
|
||||
Display* display = XOpenDisplay(nullptr);
|
||||
int defaultScreen = DefaultScreen(display);
|
||||
XVisualInfo* visual;
|
||||
XSetWindowAttributes xSetWindowAttributes;
|
||||
XWindowAttributes windowAttributes;
|
||||
Atom wmDeleteWindow;
|
||||
// Make it start as floating because fuck tiling WMs
|
||||
Atom windowTypeAtom;
|
||||
Atom windowTypeUtilityAtom;
|
||||
XSizeHints hints;
|
||||
GLXContext glContext;
|
||||
Cursor invisible_cursor = 0;
|
||||
|
||||
using namespace ReWindow;
|
||||
Vector2 render_area_position = {0, 0};
|
||||
Vector2 position = {0, 0};
|
||||
bool should_poll_x_for_mouse_pos = true;
|
||||
};
|
||||
|
||||
void RWindow::raise() const {
|
||||
RWindowImpl::RWindowImpl(const std::string &wTitle, int wWidth, int wHeight, RenderingAPI wRenderer, bool wFullscreen, bool wResizable, bool wVsync) : pPlatform(new Platform) {
|
||||
title = wTitle;
|
||||
width = wWidth;
|
||||
height = wHeight;
|
||||
renderer = wRenderer;
|
||||
fullscreen_mode = wFullscreen;
|
||||
resizable = wResizable;
|
||||
vsync = wVsync;
|
||||
}
|
||||
|
||||
RWindowImpl::~RWindowImpl() {
|
||||
if (pPlatform) { std::cout << "FUCK" << std::endl; delete pPlatform; };
|
||||
if (open)
|
||||
DestroyOSWindowHandle();
|
||||
}
|
||||
|
||||
//using namespace ReWindow;
|
||||
|
||||
void RWindowImpl::Raise() const {
|
||||
Logger::Debug(std::format("Raising window '{}'", this->title));
|
||||
|
||||
// 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 };
|
||||
XGetWindowAttributes(pPlatform->display, pPlatform->window, &pPlatform->windowAttributes);
|
||||
pPlatform->render_area_position = { (float) pPlatform->windowAttributes.x, (float) pPlatform->windowAttributes.y };
|
||||
|
||||
XRaiseWindow(display, window);
|
||||
XRaiseWindow(pPlatform->display, pPlatform->window);
|
||||
}
|
||||
|
||||
void RWindow::lower() const
|
||||
void RWindowImpl::Lower() const
|
||||
{
|
||||
Logger::Debug(std::format("Lowering window '{}'", this->title));
|
||||
XLowerWindow(display, window);
|
||||
XLowerWindow(pPlatform->display, pPlatform->window);
|
||||
}
|
||||
|
||||
void RWindow::destroyWindow() {
|
||||
void RWindowImpl::DestroyOSWindowHandle() {
|
||||
Logger::Debug(std::format("Destroying window '{}'", this->title));
|
||||
XDestroySubwindows(display, window);
|
||||
XDestroySubwindows(pPlatform->display, pPlatform->window);
|
||||
Logger::Debug(std::format("Destroyed window '{}'", this->title));
|
||||
XDestroyWindow(display, window);
|
||||
delete this;
|
||||
XDestroyWindow(pPlatform->display, pPlatform->window);
|
||||
|
||||
system("xset r on"); // Re-set X-server parameter to enable key-repeat.
|
||||
|
||||
open = false;
|
||||
}
|
||||
|
||||
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 RWindowImpl::SetCursorVisible(bool cursor_enable) {
|
||||
cursor_visible = cursor_enable;
|
||||
|
||||
if (invisible_cursor == 0) {
|
||||
Pixmap blank_pixmap = XCreatePixmap(display, window, 1, 1, 1);
|
||||
if (pPlatform->invisible_cursor == 0) {
|
||||
Pixmap blank_pixmap = XCreatePixmap(pPlatform->display, pPlatform->window, 1, 1, 1);
|
||||
XColor dummy; dummy.pixel = 0; dummy.red = 0; dummy.flags = 0;
|
||||
invisible_cursor = XCreatePixmapCursor(display, blank_pixmap, blank_pixmap, &dummy, &dummy, 0, 0);
|
||||
XFreePixmap(display, blank_pixmap);
|
||||
pPlatform->invisible_cursor = XCreatePixmapCursor(pPlatform->display, blank_pixmap, blank_pixmap, &dummy, &dummy, 0, 0);
|
||||
XFreePixmap(pPlatform->display, blank_pixmap);
|
||||
}
|
||||
|
||||
if (!cursor_enable)
|
||||
XDefineCursor(display, window, invisible_cursor);
|
||||
XDefineCursor(pPlatform->display, pPlatform->window, pPlatform->invisible_cursor);
|
||||
|
||||
if (cursor_enable)
|
||||
XUndefineCursor(display, window);
|
||||
XUndefineCursor(pPlatform->display, pPlatform->window);
|
||||
}
|
||||
|
||||
void RWindow::setFlag(RWindowFlags flag, bool state) {
|
||||
XGetWindowAttributes(display,window,&windowAttributes);
|
||||
void RWindowImpl::SetResizable(bool sizable) {
|
||||
XGetWindowAttributes(pPlatform->display,pPlatform->window,&pPlatform->windowAttributes);
|
||||
|
||||
|
||||
this->resizable = sizable;
|
||||
if (!sizable)
|
||||
{
|
||||
Logger::Debug("Once you've done this you cannot make it resizable again.");
|
||||
pPlatform->hints.flags = PMinSize | PMaxSize;
|
||||
pPlatform->hints.min_width = pPlatform->hints.max_width = pPlatform->windowAttributes.width;
|
||||
pPlatform->hints.min_height = pPlatform->hints.max_height = pPlatform->windowAttributes.height;
|
||||
XSetWMNormalHints(pPlatform->display, pPlatform->window, &pPlatform->hints);
|
||||
}
|
||||
//this->SetFlag(RWindowFlags::RESIZABLE, resizable);
|
||||
|
||||
}
|
||||
|
||||
// Fuck you
|
||||
void RWindowImpl::SetFlag(RWindowFlags flag, bool state) {
|
||||
XGetWindowAttributes(pPlatform->display,pPlatform->window,&pPlatform->windowAttributes);
|
||||
flags[(int) flag] = state;
|
||||
//Once you've done this you cannot make it resizable again.
|
||||
if (flag == RWindowFlags::RESIZABLE && !state) {
|
||||
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);
|
||||
pPlatform->hints.flags = PMinSize | PMaxSize;
|
||||
pPlatform->hints.min_width = pPlatform->hints.max_width = pPlatform->windowAttributes.width;
|
||||
pPlatform->hints.min_height = pPlatform->hints.max_height = pPlatform->windowAttributes.height;
|
||||
XSetWMNormalHints(pPlatform->display, pPlatform->window, &pPlatform->hints);
|
||||
}
|
||||
Logger::Debug(std::format("Set flag '{}' to state '{}' for window '{}'", RWindowFlagToStr(flag), state, this->title));
|
||||
}
|
||||
|
||||
void RWindow::pollEvents() {
|
||||
while(XPending(display)) {
|
||||
XNextEvent(display, &xev);
|
||||
void RWindowImpl::PollEvents() {
|
||||
while(XPending(pPlatform->display)) {
|
||||
XNextEvent(pPlatform->display, &pPlatform->xev);
|
||||
|
||||
if (xev.type == ClientMessage)
|
||||
Logger::Debug(std::format("Recieved event '{}'", "ClientMessage"));
|
||||
|
||||
if (xev.xclient.message_type == XInternAtom(display, "WM_PROTOCOLS", False) && static_cast<Atom>(xev.xclient.data.l[0]) == wmDeleteWindow) {
|
||||
if (pPlatform->xev.type == ClientMessage)
|
||||
Logger::Info(std::format("Event '{}'", "ClientMessage"));
|
||||
|
||||
processOnClose();
|
||||
destroyWindow();
|
||||
system("xset r on");
|
||||
exit(0);
|
||||
if (pPlatform->xev.xclient.message_type == XInternAtom(pPlatform->display, "WM_PROTOCOLS", False) && static_cast<Atom>(pPlatform->xev.xclient.data.l[0]) == pPlatform->wmDeleteWindow) {
|
||||
Logger::Info("Closing");
|
||||
Close();
|
||||
}
|
||||
|
||||
if (xev.type == FocusIn) {
|
||||
Logger::Debug(std::format("Recieved event '{}'", "FocusIn"));
|
||||
XAutoRepeatOff(display);
|
||||
setFlag(RWindowFlags::IN_FOCUS, true);
|
||||
if (pPlatform->xev.type == FocusIn) {
|
||||
Logger::Debug(std::format("Event'{}'", "FocusIn"));
|
||||
XAutoRepeatOff(pPlatform->display);
|
||||
SetFlag(RWindowFlags::IN_FOCUS, true);
|
||||
|
||||
if (!cursor_visible)
|
||||
XDefineCursor(display, window, invisible_cursor);
|
||||
XDefineCursor(pPlatform->display, pPlatform->window, pPlatform->invisible_cursor);
|
||||
|
||||
// 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 };
|
||||
XGetWindowAttributes(pPlatform->display, pPlatform->window, &pPlatform->windowAttributes);
|
||||
pPlatform->render_area_position = { (float) pPlatform->windowAttributes.x, (float) pPlatform->windowAttributes.y };
|
||||
processFocusIn();
|
||||
}
|
||||
|
||||
if (xev.type == FocusOut) {
|
||||
Logger::Debug(std::format("Recieved event '{}'", "FocusOut"));
|
||||
XAutoRepeatOn(display);
|
||||
if (pPlatform->xev.type == FocusOut) {
|
||||
Logger::Debug(std::format("Event '{}'", "FocusOut"));
|
||||
XAutoRepeatOn(pPlatform->display);
|
||||
|
||||
setFlag(RWindowFlags::IN_FOCUS, false);
|
||||
SetFlag(RWindowFlags::IN_FOCUS, false);
|
||||
if (!cursor_visible)
|
||||
XUndefineCursor(display, window);
|
||||
XUndefineCursor(pPlatform->display, pPlatform->window);
|
||||
|
||||
processFocusOut();
|
||||
}
|
||||
|
||||
if (xev.type == KeyRelease) {
|
||||
Logger::Debug(std::format("Recieved event '{}'", "KeyRelease"));
|
||||
auto scancode = (X11Scancode) xev.xkey.keycode;
|
||||
if (pPlatform->xev.type == KeyRelease) {
|
||||
Logger::Debug(std::format("Event '{}'", "KeyRelease"));
|
||||
auto scancode = (X11Scancode) pPlatform->xev.xkey.keycode;
|
||||
auto key = GetKeyFromX11Scancode(scancode);
|
||||
processKeyRelease(key);
|
||||
}
|
||||
|
||||
if (xev.type == KeyPress) {
|
||||
Logger::Debug(std::format("Recieved event '{}'", "KeyPress"));
|
||||
auto scancode = (X11Scancode) xev.xkey.keycode;
|
||||
if (pPlatform->xev.type == KeyPress) {
|
||||
Logger::Debug(std::format("Event '{}'", "KeyPress"));
|
||||
auto scancode = (X11Scancode) pPlatform->xev.xkey.keycode;
|
||||
auto key = GetKeyFromX11Scancode(scancode);
|
||||
processKeyPress(key);
|
||||
}
|
||||
|
||||
if (xev.type == ButtonRelease) {
|
||||
Logger::Debug(std::format("Recieved event '{}'", "ButtonRelease"));
|
||||
MouseButton button = GetMouseButtonFromXButton(xev.xbutton.button);
|
||||
if (pPlatform->xev.type == ButtonRelease) {
|
||||
|
||||
processMouseRelease(button);
|
||||
// Mouse Wheel fires both the ButtonPress and ButtonRelease instantaneously.
|
||||
// Therefore, we handle it as a specific MouseWheel event rather than a MouseButton event,
|
||||
// and only call on ButtonPress, otherwise it will appear to duplicate the mouse wheel scroll.
|
||||
if (pPlatform->xev.xbutton.button != 4 && pPlatform->xev.xbutton.button != 5) {
|
||||
MouseButton button = GetMouseButtonFromXButton(pPlatform->xev.xbutton.button);
|
||||
Logger::Debug(std::format("Event '{}'", "ButtonRelease"));
|
||||
processMouseRelease(button);
|
||||
}
|
||||
}
|
||||
|
||||
if (xev.type == ButtonPress) {
|
||||
MouseButton button = GetMouseButtonFromXButton(xev.xbutton.button);
|
||||
if (pPlatform->xev.type == ButtonPress) {
|
||||
|
||||
Logger::Debug(std::format("Window event: MouseButtonPress {}", button.CharCode));
|
||||
// Mouse Wheel fires both the ButtonPress and ButtonRelease instantaneously.
|
||||
// Therefore, we handle it as a specific MouseWheel event rather than a MouseButton event,
|
||||
// and only call on ButtonPress, otherwise it will appear to duplicate the mouse wheel scroll.
|
||||
if (pPlatform->xev.xbutton.button == 4) {
|
||||
processMouseWheel(-1);
|
||||
} else if (pPlatform->xev.xbutton.button == 5) {
|
||||
processMouseWheel(1);
|
||||
} else
|
||||
{
|
||||
MouseButton button = GetMouseButtonFromXButton(pPlatform->xev.xbutton.button);
|
||||
|
||||
processMousePress(button);
|
||||
Logger::Debug(std::format("Event: MouseButtonPress {}", button.Mnemonic));
|
||||
|
||||
processMousePress(button);
|
||||
}
|
||||
}
|
||||
|
||||
if (xev.type == Expose)
|
||||
if (pPlatform->xev.type == Expose)
|
||||
{
|
||||
Logger::Debug(std::format("Recieved event '{}'", "Expose"));
|
||||
Logger::Debug(std::format("Event '{}'", "Expose"));
|
||||
// Essentially allows more than one window to be drawable
|
||||
// https://www.khronos.org/opengl/wiki/Programming_OpenGL_in_Linux:_GLX_and_Xlib
|
||||
// https://registry.khronos.org/OpenGL-Refpages/gl2.1/xhtml/glXMakeCurrent.xml
|
||||
//glXMakeCurrent(pPlatform->display, pPlatform->window, pPlatform->glContext);
|
||||
}
|
||||
|
||||
// NOTE: This event is functionally useless, as it only informs of the very beginning and end of a mouse movement.
|
||||
if (xev.type == MotionNotify)
|
||||
if (pPlatform->xev.type == MotionNotify)
|
||||
{
|
||||
Logger::Debug(std::format("Recieved event '{}'", "MotionNotify"));
|
||||
Logger::Debug(std::format("Event '{}'", "MotionNotify"));
|
||||
}
|
||||
|
||||
if (xev.type == ConfigureNotify) {
|
||||
if (this->width != xev.xconfigurerequest.width || this->height != xev.xconfigurerequest.height) {
|
||||
Logger::Debug(std::format("Recieved event '{}'", "ResizeRequest"));
|
||||
if (pPlatform->xev.type == ConfigureNotify) {
|
||||
if (this->width != pPlatform->xev.xconfigurerequest.width || this->height != pPlatform->xev.xconfigurerequest.height) {
|
||||
Logger::Debug(std::format("Event '{}'", "ResizeRequest"));
|
||||
|
||||
this->width = xev.xconfigurerequest.width;
|
||||
this->height = xev.xconfigurerequest.height;
|
||||
this->width = pPlatform->xev.xconfigurerequest.width;
|
||||
this->height = pPlatform->xev.xconfigurerequest.height;
|
||||
|
||||
auto eventData = WindowResizeRequestEvent();
|
||||
eventData.Size = { (float) xev.xconfigurerequest.width, (float) xev.xconfigurerequest.height };
|
||||
eventData.Size = { (float) pPlatform->xev.xconfigurerequest.width, (float) pPlatform->xev.xconfigurerequest.height };
|
||||
lastKnownWindowSize = eventData.Size;
|
||||
|
||||
OnResizeRequest(eventData);
|
||||
@@ -225,27 +272,48 @@ void RWindow::pollEvents() {
|
||||
}
|
||||
|
||||
//Window Moved.
|
||||
if (position.x != xev.xconfigurerequest.x || position.y != xev.xconfigurerequest.y)
|
||||
position = { (float) xev.xconfigurerequest.x, (float) xev.xconfigurerequest.y };
|
||||
if (pPlatform->position.x != pPlatform->xev.xconfigurerequest.x || pPlatform->position.y != pPlatform->xev.xconfigurerequest.y)
|
||||
pPlatform->position = { (float) pPlatform->xev.xconfigurerequest.x, (float) pPlatform->xev.xconfigurerequest.y };
|
||||
}
|
||||
|
||||
}
|
||||
previousKeyboard = currentKeyboard;
|
||||
previousMouse.Buttons = currentMouse.Buttons;
|
||||
}
|
||||
|
||||
|
||||
void RWindowImpl::Refresh() {
|
||||
// Essentially allows more than one window to be drawable
|
||||
// https://www.khronos.org/opengl/wiki/Programming_OpenGL_in_Linux:_GLX_and_Xlib
|
||||
// https://registry.khronos.org/OpenGL-Refpages/gl2.1/xhtml/glXMakeCurrent.xml
|
||||
glXMakeCurrent(pPlatform->display, pPlatform->window, pPlatform->glContext);
|
||||
|
||||
PollEvents();
|
||||
OnRefresh(delta_time);
|
||||
|
||||
// Only call once and cache the result.
|
||||
currentMouse.Position = GetAccurateMouseCoordinates();
|
||||
|
||||
/// TODO: Implement optional minimum epsilon to trigger a Mouse Update.
|
||||
if (currentMouse.Position != previousMouse.Position) {
|
||||
processMouseMove(previousMouse.Position, currentMouse.Position);
|
||||
previousMouse.Position = currentMouse.Position;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 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 RWindowImpl::SetSize(int newWidth, int newHeight) {
|
||||
if (!resizable) return;
|
||||
|
||||
this->width = newWidth;
|
||||
this->height = newHeight;
|
||||
XResizeWindow(display, window, newWidth, newHeight);
|
||||
XFlush(display);
|
||||
Logger::Debug(std::format("Set size for window '{}'. width={} height={}", this->title, newWidth, newHeight));
|
||||
XResizeWindow(pPlatform->display, pPlatform->window, newWidth, newHeight);
|
||||
XFlush(pPlatform->display);
|
||||
Logger::Info(std::format("Set size for '{}' to {} x {}", this->title, newWidth, newHeight));
|
||||
}
|
||||
|
||||
Vector2 RWindow::GetAccurateMouseCoordinates() const {
|
||||
Vector2 RWindowImpl::GetAccurateMouseCoordinates() const {
|
||||
|
||||
Window root_return, child_return;
|
||||
int root_x_ret, root_y_ret;
|
||||
@@ -253,7 +321,7 @@ Vector2 RWindow::GetAccurateMouseCoordinates() const {
|
||||
uint32_t mask_return;
|
||||
|
||||
// This seems to be relative to the top left corner of the renderable area.
|
||||
bool mouseAvailable = XQueryPointer(display, window, &root_return, &child_return, &root_x_ret, &root_y_ret, &win_x_ret, &win_y_ret, &mask_return);
|
||||
bool mouseAvailable = XQueryPointer(pPlatform->display, pPlatform->window, &root_return, &child_return, &root_x_ret, &root_y_ret, &win_x_ret, &win_y_ret, &mask_return);
|
||||
|
||||
if (mouseAvailable) {
|
||||
// TODO: normalize coordinates from displaySpace to windowSpace
|
||||
@@ -264,155 +332,150 @@ Vector2 RWindow::GetAccurateMouseCoordinates() const {
|
||||
return Vector2::Zero;
|
||||
}
|
||||
|
||||
// TODO: implement integer vector2/3 types
|
||||
Vector2 RWindow::getSize() const {
|
||||
|
||||
Vector2 RWindowImpl::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 {
|
||||
return position;
|
||||
Vector2 RWindowImpl::GetPos() const {
|
||||
return pPlatform->position;
|
||||
}
|
||||
|
||||
void RWindow::setPos(int x, int y) {
|
||||
XMoveWindow(display, window, x, y);
|
||||
position = { (float) x, (float) y };
|
||||
void RWindowImpl::SetPos(int x, int y) {
|
||||
XMoveWindow(pPlatform->display, pPlatform->window, x, y);
|
||||
pPlatform->position = { (float) x, (float) y };
|
||||
}
|
||||
|
||||
void RWindow::setPos(const Vector2& pos) {
|
||||
setPos(pos.x, pos.y);
|
||||
void RWindowImpl::SetPos(const Vector2& pos) {
|
||||
SetPos(pos.x, pos.y);
|
||||
}
|
||||
|
||||
|
||||
void RWindow::glSwapBuffers() {
|
||||
glXSwapBuffers(display,window);
|
||||
void RWindowImpl::GLSwapBuffers() {
|
||||
glXSwapBuffers(pPlatform->display,pPlatform->window);
|
||||
}
|
||||
|
||||
bool RWindow::isResizable() const {
|
||||
return getFlag(RWindowFlags::RESIZABLE);
|
||||
}
|
||||
|
||||
void RWindow::fullscreen() {
|
||||
Logger::Debug(std::format("Fullscreening window '{}'", this->title));
|
||||
fullscreenmode = true;
|
||||
void RWindowImpl::Fullscreen() {
|
||||
Logger::Info(std::format("Fullscreening '{}'", this->title));
|
||||
fullscreen_mode = true;
|
||||
|
||||
XEvent xev;
|
||||
Atom wm_state = XInternAtom(display, "_NET_WM_STATE", true);
|
||||
Atom wm_fullscreen = XInternAtom(display, "_NET_WM_STATE_FULLSCREEN", true);
|
||||
Atom wm_state = XInternAtom(pPlatform->display, "_NET_WM_STATE", true);
|
||||
Atom wm_fullscreen = XInternAtom(pPlatform->display, "_NET_WM_STATE_FULLSCREEN", true);
|
||||
|
||||
XChangeProperty(display, window, wm_state, XA_ATOM, 32, PropModeReplace, (unsigned char *)&wm_fullscreen, 1);
|
||||
XChangeProperty(pPlatform->display, pPlatform->window, wm_state, XA_ATOM, 32, PropModeReplace, (unsigned char *)&wm_fullscreen, 1);
|
||||
memset(&xev, 0, sizeof(xev));
|
||||
xev.type = ClientMessage;
|
||||
xev.xclient.window = window;
|
||||
xev.xclient.window = pPlatform->window;
|
||||
xev.xclient.message_type = wm_state;
|
||||
xev.xclient.format = 32;
|
||||
xev.xclient.data.l[0] = 1; // _NET_WM_STATE_ADD
|
||||
xev.xclient.data.l[1] = fullscreenmode;
|
||||
xev.xclient.data.l[1] = fullscreen_mode;
|
||||
xev.xclient.data.l[2] = 0;
|
||||
XSendEvent(display, DefaultRootWindow(display), False, SubstructureNotifyMask | SubstructureRedirectMask, &xev);
|
||||
Logger::Debug(std::format("Fullscreened window '{}'", this->title));
|
||||
XSendEvent(pPlatform->display, DefaultRootWindow(pPlatform->display), False, SubstructureNotifyMask | SubstructureRedirectMask, &xev);
|
||||
Logger::Debug(std::format("Fullscreened '{}'", this->title));
|
||||
}
|
||||
|
||||
void RWindow::restoreFromFullscreen() {
|
||||
Logger::Debug(std::format("Restoring window '{}' from fullscreen", this->title));
|
||||
fullscreenmode = false;
|
||||
void RWindowImpl::RestoreFromFullscreen() {
|
||||
Logger::Debug(std::format("Restoring '{}' from Fullscreen", this->title));
|
||||
fullscreen_mode = false;
|
||||
XEvent xev;
|
||||
Atom wm_state = XInternAtom(display, "_NET_WM_STATE", False);
|
||||
Atom fullscreen = XInternAtom(display, "_NET_WM_STATE_FULLSCREEN", False);
|
||||
Atom wm_state = XInternAtom(pPlatform->display, "_NET_WM_STATE", False);
|
||||
Atom fullscreen = XInternAtom(pPlatform->display, "_NET_WM_STATE_FULLSCREEN", False);
|
||||
memset(&xev, 0, sizeof(xev));
|
||||
xev.type = ClientMessage;
|
||||
xev.xclient.window = window;
|
||||
xev.xclient.window = pPlatform->window;
|
||||
xev.xclient.message_type = wm_state;
|
||||
xev.xclient.format = 32;
|
||||
xev.xclient.data.l[0] = 0; // _NET_WM_STATE_REMOVE
|
||||
xev.xclient.data.l[1] = fullscreenmode;
|
||||
xev.xclient.data.l[1] = fullscreen_mode;
|
||||
xev.xclient.data.l[2] = 0;
|
||||
XSendEvent(display, DefaultRootWindow(display), False, SubstructureNotifyMask | SubstructureRedirectMask, &xev);
|
||||
Logger::Debug(std::format("Restored window '{}' from fullscreen", this->title));
|
||||
XSendEvent(pPlatform->display, DefaultRootWindow(pPlatform->display), False, SubstructureNotifyMask | SubstructureRedirectMask, &xev);
|
||||
Logger::Debug(std::format("Restored '{}' from Fullscreen", this->title));
|
||||
}
|
||||
|
||||
void RWindow::setVsyncEnabled(bool b) {
|
||||
void RWindowImpl::SetVsyncEnabled(bool b) {
|
||||
PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXT;
|
||||
glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddressARB((const GLubyte*)"glXSwapIntervalEXT");
|
||||
glXSwapIntervalEXT(display, window, b);
|
||||
glXSwapIntervalEXT(pPlatform->display, pPlatform->window, b);
|
||||
}
|
||||
|
||||
bool RWindow::isFullscreen() const {
|
||||
return fullscreenmode;
|
||||
}
|
||||
|
||||
void RWindow::setCursorStyle(CursorStyle style) const {
|
||||
// I know this doesn't modify the class, but it indirectly modifies the window
|
||||
// Making it const just seems deceptive.
|
||||
void RWindowImpl::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);
|
||||
Cursor c = XCreateFontCursor(pPlatform->display, x11_cursor_resolved_enum);
|
||||
XDefineCursor(pPlatform->display, pPlatform->window, c);
|
||||
}
|
||||
|
||||
void RWindow::Open() {
|
||||
xSetWindowAttributes.border_pixel = BlackPixel(display, defaultScreen);
|
||||
xSetWindowAttributes.background_pixel = BlackPixel(display, defaultScreen);
|
||||
xSetWindowAttributes.override_redirect = True;
|
||||
xSetWindowAttributes.event_mask = ExposureMask;
|
||||
//setVsyncEnabled(vsync);
|
||||
void RWindowImpl::Open() {
|
||||
pPlatform->xSetWindowAttributes.border_pixel = BlackPixel(pPlatform->display, pPlatform->defaultScreen);
|
||||
pPlatform->xSetWindowAttributes.background_pixel = BlackPixel(pPlatform->display, pPlatform->defaultScreen);
|
||||
pPlatform->xSetWindowAttributes.override_redirect = True;
|
||||
pPlatform->xSetWindowAttributes.event_mask = ExposureMask;
|
||||
//SetVsyncEnabled(vsync);
|
||||
if (renderer == RenderingAPI::OPENGL) {
|
||||
GLint glAttributes[] = {GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None};
|
||||
visual = glXChooseVisual(display, defaultScreen, glAttributes);
|
||||
glContext = glXCreateContext(display, visual, nullptr, GL_TRUE);
|
||||
pPlatform->visual = glXChooseVisual(pPlatform->display, pPlatform->defaultScreen, glAttributes);
|
||||
pPlatform->glContext = glXCreateContext(pPlatform->display, pPlatform->visual, nullptr, GL_TRUE);
|
||||
}
|
||||
|
||||
xSetWindowAttributes.colormap = XCreateColormap(display, RootWindow(display, defaultScreen), visual->visual, AllocNone);
|
||||
pPlatform->xSetWindowAttributes.colormap = XCreateColormap(pPlatform->display, RootWindow(pPlatform->display, pPlatform->defaultScreen), pPlatform->visual->visual, AllocNone);
|
||||
|
||||
window = XCreateWindow(display, RootWindow(display, defaultScreen), 0, 0, width, height, 0, visual->depth,
|
||||
InputOutput, visual->visual, CWBackPixel | CWColormap | CWBorderPixel | NoEventMask,
|
||||
&xSetWindowAttributes);
|
||||
pPlatform->window = XCreateWindow(pPlatform->display, RootWindow(pPlatform->display, pPlatform->defaultScreen), 0, 0, width, height, 0, pPlatform->visual->depth,
|
||||
InputOutput, pPlatform->visual->visual, CWBackPixel | CWColormap | CWBorderPixel | NoEventMask,
|
||||
&pPlatform->xSetWindowAttributes);
|
||||
// Set window to floating because fucking tiling WMs
|
||||
windowTypeAtom = XInternAtom(display, "_NET_WM_WINDOW_TYPE", False);
|
||||
windowTypeUtilityAtom = XInternAtom(display, "_NET_WM_WINDOW_TYPE_UTILITY", False);
|
||||
XChangeProperty(display, window, windowTypeAtom, XA_ATOM, 32, PropModeReplace,
|
||||
(unsigned char *)&windowTypeUtilityAtom, 1);
|
||||
pPlatform->windowTypeAtom = XInternAtom(pPlatform->display, "_NET_WM_WINDOW_TYPE", False);
|
||||
pPlatform->windowTypeUtilityAtom = XInternAtom(pPlatform->display, "_NET_WM_WINDOW_TYPE_UTILITY", False);
|
||||
XChangeProperty(pPlatform->display, pPlatform->window, pPlatform->windowTypeAtom, XA_ATOM, 32, PropModeReplace,
|
||||
(unsigned char *)&pPlatform->windowTypeUtilityAtom, 1);
|
||||
//
|
||||
XSelectInput(display, window,
|
||||
XSelectInput(pPlatform->display, pPlatform->window,
|
||||
ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
|
||||
PointerMotionMask |
|
||||
PointerMotionHintMask | FocusChangeMask | StructureNotifyMask | SubstructureRedirectMask |
|
||||
SubstructureNotifyMask | CWColormap);
|
||||
XMapWindow(display, window);
|
||||
XStoreName(display, window, title.c_str());
|
||||
wmDeleteWindow = XInternAtom(display, "WM_DELETE_WINDOW", False);
|
||||
XSetWMProtocols(display, window, &wmDeleteWindow, 1);
|
||||
SubstructureNotifyMask | CWColormap );
|
||||
XMapWindow(pPlatform->display, pPlatform->window);
|
||||
XStoreName(pPlatform->display, pPlatform->window, title.c_str());
|
||||
pPlatform->wmDeleteWindow = XInternAtom(pPlatform->display, "WM_DELETE_WINDOW", False);
|
||||
XSetWMProtocols(pPlatform->display, pPlatform->window, &pPlatform->wmDeleteWindow, 1);
|
||||
|
||||
if (renderer == RenderingAPI::OPENGL)
|
||||
glXMakeCurrent(display, window, glContext);
|
||||
glXMakeCurrent(pPlatform->display, pPlatform->window, pPlatform->glContext);
|
||||
|
||||
// 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 };
|
||||
open = true;
|
||||
XGetWindowAttributes(pPlatform->display, pPlatform->window, &pPlatform->windowAttributes);
|
||||
pPlatform->render_area_position = { (float) pPlatform->windowAttributes.x, (float) pPlatform->windowAttributes.y };
|
||||
|
||||
open = true;
|
||||
|
||||
processOnOpen();
|
||||
}
|
||||
|
||||
void RWindow::setTitle(const std::string &title) {
|
||||
void RWindowImpl::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 {
|
||||
return render_area_position;
|
||||
XStoreName(pPlatform->display, pPlatform->window, title.c_str());
|
||||
}
|
||||
|
||||
|
||||
|
||||
Vector2 RWindowImpl::GetPositionOfRenderableArea() const {
|
||||
return pPlatform->render_area_position;
|
||||
}
|
||||
|
||||
std::string RWindowImpl::getGraphicsDriverVendor() {
|
||||
return std::string(reinterpret_cast<const char*>(glGetString(GL_VENDOR)));
|
||||
}
|
||||
|
||||
|
||||
// TODO: Implement ControllerButton map
|
||||
|
||||
|
@@ -1,5 +1,20 @@
|
||||
#include <rewindow/types/window.h>
|
||||
#include <rewindow/types/window.h>
|
||||
#include "rewindow/logger/logger.h"
|
||||
/*
|
||||
std::string RWindowFlagToStr(RWindowFlags flag) {
|
||||
switch (flag) {
|
||||
case RWindowFlags::IN_FOCUS: return "IN_FOCUS";
|
||||
case RWindowFlags::FULLSCREEN: return "FULLSCREEN";
|
||||
case RWindowFlags::RESIZABLE: return "RESIZEABLE";
|
||||
case RWindowFlags::VSYNC: return "VSYNC";
|
||||
case RWindowFlags::QUIT: return "QUIT";
|
||||
case RWindowFlags::MAX_FLAG: return "MAX_FLAG";
|
||||
default:
|
||||
return "unimplemented flag";
|
||||
}
|
||||
};
|
||||
*/
|
||||
|
||||
std::string RWindowFlagToStr(RWindowFlags flag) {
|
||||
switch (flag) {
|
||||
case RWindowFlags::IN_FOCUS: return "IN_FOCUS";
|
||||
@@ -13,167 +28,280 @@ std::string RWindowFlagToStr(RWindowFlags flag) {
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
using namespace ReWindow;
|
||||
|
||||
RWindow::RWindow() {
|
||||
title = "ReWindow Application";
|
||||
width = 640;
|
||||
height = 480;
|
||||
//RWindow::singleton = this;
|
||||
|
||||
|
||||
/*
|
||||
RWindow::RWindow(const std::string& wTitle, int wWidth, int wHeight, RenderingAPI wRenderer, bool wFullscreen, bool wResizable, bool wVsync)
|
||||
: title(wTitle), width(wWidth), height(wHeight), renderer(wRenderer), fullscreen_mode(wFullscreen), resizable(wResizable), vsync(wVsync),
|
||||
flags{false,wFullscreen,wResizable,wVsync} {
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
//RWindow::~RWindow() {
|
||||
/*
|
||||
if (open)
|
||||
DestroyOSWindowHandle();
|
||||
*/
|
||||
//RWindowImpl::~RWindowImpl();
|
||||
//}
|
||||
|
||||
|
||||
|
||||
Vector2 RWindowImpl::GetMouseCoordinates() const {
|
||||
return currentMouse.Position;
|
||||
}
|
||||
|
||||
RWindow::RWindow(const std::string& title, int width, int height) : flags{false,false,false,false} {
|
||||
this->title = title;
|
||||
this->width = width;
|
||||
this->height = height;
|
||||
//RWindow::singleton = this;
|
||||
}
|
||||
|
||||
RWindow::RWindow(const std::string& title, int width, int height, RenderingAPI renderer) :
|
||||
title(title), width(width), height(height), renderer(renderer) {}
|
||||
|
||||
Vector2 RWindow::GetMouseCoordinates() const {
|
||||
return mouse_coords;
|
||||
}
|
||||
|
||||
bool RWindow::getFlag(RWindowFlags flag) const {
|
||||
bool RWindowImpl::GetFlag(RWindowFlags flag) const {
|
||||
return flags[(int) flag];
|
||||
}
|
||||
|
||||
bool RWindow::isAlive() const {
|
||||
return true;
|
||||
bool RWindowImpl::IsAlive() const {
|
||||
return (!closing) && open;
|
||||
}
|
||||
|
||||
void RWindow::setResizable(bool resizable) {
|
||||
this->resizable = resizable;
|
||||
this->setFlag(RWindowFlags::RESIZABLE, resizable);
|
||||
}
|
||||
|
||||
void RWindow::setFullscreen(bool fs) {
|
||||
void RWindowImpl::SetFullscreen(bool fs) {
|
||||
if (fs)
|
||||
fullscreen();
|
||||
Fullscreen();
|
||||
else
|
||||
restoreFromFullscreen();
|
||||
RestoreFromFullscreen();
|
||||
}
|
||||
|
||||
void RWindow::processFocusIn()
|
||||
#pragma region Event Processors Implementation
|
||||
void RWindowImpl::processFocusIn()
|
||||
{
|
||||
RWindowEvent event {};
|
||||
OnFocusGain(event);
|
||||
OnFocusGainEvent(event);
|
||||
LogEvent(event);
|
||||
}
|
||||
|
||||
void RWindow::processFocusOut()
|
||||
void RWindowImpl::processFocusOut()
|
||||
{
|
||||
RWindowEvent event {};
|
||||
OnFocusLost(event);
|
||||
OnFocusLostEvent(event);
|
||||
LogEvent(event);
|
||||
}
|
||||
|
||||
void RWindow::processMousePress(MouseButton btn)
|
||||
void RWindowImpl::processMousePress(const MouseButton& btn)
|
||||
{
|
||||
auto eventData = MouseButtonDownEvent(btn);
|
||||
OnMouseButtonDownEvent(eventData);
|
||||
OnMouseButtonDown(eventData);
|
||||
currentMouse.Set(btn, true);
|
||||
auto event = MouseButtonDownEvent(btn);
|
||||
OnMouseButtonDown(event);
|
||||
OnMouseButtonDownEvent(event);
|
||||
LogEvent(event);
|
||||
|
||||
}
|
||||
|
||||
void RWindow::processMouseMove(Vector2 last_pos, Vector2 new_pos)
|
||||
void RWindowImpl::processMouseMove(Vector2 last_pos, Vector2 new_pos)
|
||||
{
|
||||
auto eventData = MouseMoveEvent(new_pos);
|
||||
OnMouseMove(eventData);
|
||||
OnMouseMoveEvent(eventData);
|
||||
currentMouse.Position = new_pos;
|
||||
auto event = MouseMoveEvent(new_pos);
|
||||
OnMouseMove(event);
|
||||
OnMouseMoveEvent(event);
|
||||
LogEvent(event);
|
||||
}
|
||||
|
||||
void RWindow::processMouseRelease(MouseButton btn)
|
||||
void RWindowImpl::processMouseRelease(const MouseButton& btn)
|
||||
{
|
||||
auto eventData = MouseButtonUpEvent(btn);
|
||||
OnMouseButtonUpEvent(eventData);
|
||||
OnMouseButtonUp(eventData);
|
||||
currentMouse.Set(btn, false);
|
||||
auto event = MouseButtonUpEvent(btn);
|
||||
OnMouseButtonUp(event);
|
||||
OnMouseButtonUpEvent(event);
|
||||
LogEvent(event);
|
||||
|
||||
}
|
||||
|
||||
void RWindow::processKeyRelease(Key key) {
|
||||
void RWindowImpl::processKeyRelease(Key key) {
|
||||
currentKeyboard.PressedKeys[key] = false;
|
||||
auto event = KeyUpEvent(key);
|
||||
OnKeyUp(event);
|
||||
OnKeyUpEvent(key);
|
||||
LogEvent(event);
|
||||
}
|
||||
|
||||
void RWindowImpl::processKeyPress(Key key) {
|
||||
currentKeyboard.PressedKeys[key] = true;
|
||||
auto event = KeyDownEvent(key);
|
||||
OnKeyDown(event);
|
||||
OnKeyDownEvent(key);
|
||||
LogEvent(event);
|
||||
}
|
||||
|
||||
std::string RWindow::getTitle() const {
|
||||
void RWindowImpl::processOnClose()
|
||||
{
|
||||
auto event = RWindowEvent();
|
||||
OnClosing();
|
||||
OnClosingEvent();
|
||||
LogEvent(event);
|
||||
}
|
||||
|
||||
void RWindowImpl::processOnOpen()
|
||||
{
|
||||
auto event = RWindowEvent();
|
||||
OnOpen();
|
||||
OnOpenEvent();
|
||||
LogEvent(event);
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
||||
std::string RWindowImpl::GetTitle() const {
|
||||
return this->title;
|
||||
}
|
||||
|
||||
/*
|
||||
Vector2 RWindow::getSize() const
|
||||
Vector2 RWindowImpl::GetSize() const
|
||||
{
|
||||
return {this->width, this->height};
|
||||
}
|
||||
*/
|
||||
|
||||
int RWindow::getWidth() const
|
||||
int RWindowImpl::GetWidth() const
|
||||
{
|
||||
return this->width;
|
||||
}
|
||||
|
||||
int RWindow::getHeight() const
|
||||
int RWindowImpl::GetHeight() const
|
||||
{
|
||||
return this->height;
|
||||
}
|
||||
|
||||
void RWindow::setSizeWithoutEvent(const Vector2& size) {
|
||||
void RWindowImpl::SetSizeWithoutEvent(const Vector2& size) {
|
||||
width = size.x;
|
||||
height = size.y;
|
||||
}
|
||||
|
||||
void RWindow::setLastKnownWindowSize(const Vector2& size) {
|
||||
void RWindowImpl::SetLastKnownWindowSize(const Vector2& size) {
|
||||
lastKnownWindowSize = size;
|
||||
}
|
||||
|
||||
void RWindow::setRenderer(RenderingAPI api) {
|
||||
void RWindowImpl::SetRenderer(RenderingAPI api) {
|
||||
renderer = api;
|
||||
}
|
||||
|
||||
void RWindow::setSize(const Vector2& size) {
|
||||
void RWindowImpl::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 {
|
||||
for (const auto& pair : currentKeyboard.PressedKeys)
|
||||
if (pair.first == key && pair.second)
|
||||
return true;
|
||||
return false;
|
||||
bool RWindowImpl::IsKeyDown(Key key) const {
|
||||
if (currentKeyboard.PressedKeys.contains(key))
|
||||
return currentKeyboard.PressedKeys.at(key);
|
||||
|
||||
return false; // NOTE: Key may not be mapped!!
|
||||
}
|
||||
|
||||
void RWindow::processKeyPress(Key key) {
|
||||
currentKeyboard.PressedKeys[key] = true;
|
||||
auto eventData = KeyDownEvent(key);
|
||||
OnKeyDown(eventData);
|
||||
OnKeyDownEvent(key);
|
||||
bool RWindowImpl::IsMouseButtonDown(const MouseButton &button) const {
|
||||
// TODO: Implement MouseButton map
|
||||
return currentMouse.IsDown(button);
|
||||
}
|
||||
|
||||
void RWindow::processOnClose()
|
||||
|
||||
void RWindowImpl::ManagedRefresh()
|
||||
{
|
||||
OnClosing();
|
||||
OnClosingEvent();
|
||||
auto begin = GetTimestamp();
|
||||
Refresh();
|
||||
auto end = GetTimestamp();
|
||||
|
||||
float dt = ComputeElapsedFrameTimeSeconds(begin, end);
|
||||
|
||||
UpdateFrameTiming(dt);
|
||||
}
|
||||
|
||||
void RWindow::processOnOpen()
|
||||
{
|
||||
OnOpen();
|
||||
OnOpenEvent();
|
||||
/*
|
||||
void RWindowImpl::Refresh() {
|
||||
PollEvents();
|
||||
OnRefresh(delta_time);
|
||||
|
||||
// Only call once and cache the result.
|
||||
currentMouse.Position = GetAccurateMouseCoordinates();
|
||||
|
||||
/// TODO: Implement optional minimum epsilon to trigger a Mouse Update.
|
||||
if (currentMouse.Position != previousMouse.Position) {
|
||||
processMouseMove(previousMouse.Position, currentMouse.Position);
|
||||
previousMouse.Position = currentMouse.Position;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
float RWindowImpl::ComputeElapsedFrameTimeSeconds(std::chrono::steady_clock::time_point start, std::chrono::steady_clock::time_point 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;
|
||||
}
|
||||
|
||||
void RWindow::processMouseWheel(int scrolls)
|
||||
std::chrono::steady_clock::time_point RWindowImpl::GetTimestamp() {
|
||||
return std::chrono::steady_clock::now();
|
||||
}
|
||||
|
||||
void RWindowImpl::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 RWindowImpl::processMouseWheel(int scrolls)
|
||||
{
|
||||
currentMouse.Wheel += scrolls;
|
||||
auto ev = MouseWheelEvent(scrolls);
|
||||
OnMouseWheel(ev);
|
||||
OnMouseWheelEvent(ev);
|
||||
|
||||
previousMouse.Wheel = currentMouse.Wheel;
|
||||
}
|
||||
|
||||
|
||||
void RWindow::Close() {
|
||||
/// TODO: Implement closing the window without destroying the handle.
|
||||
processOnClose();
|
||||
}
|
||||
bool RWindowImpl::IsResizable() const {
|
||||
return resizable;
|
||||
}
|
||||
|
||||
bool RWindowImpl::IsFullscreen() const {
|
||||
return fullscreen_mode;
|
||||
}
|
||||
|
||||
bool RWindowImpl::IsFocused() const {
|
||||
return focused;
|
||||
}
|
||||
|
||||
bool RWindowImpl::IsVsyncEnabled() const {
|
||||
return vsync;
|
||||
}
|
||||
|
||||
float RWindowImpl::GetDeltaTime() const { return delta_time; }
|
||||
|
||||
float RWindowImpl::GetRefreshRate() const { return refresh_rate; }
|
||||
|
||||
float RWindowImpl::GetRefreshCounter() const { return refresh_count; }
|
||||
|
||||
void RWindowImpl::Close() {
|
||||
closing = true;
|
||||
processOnClose();
|
||||
}
|
||||
|
||||
void RWindowImpl::ForceClose() {
|
||||
Close();
|
||||
DestroyOSWindowHandle();
|
||||
}
|
||||
|
||||
void RWindowImpl::ForceCloseAndTerminateProgram() {
|
||||
ForceClose();
|
||||
exit(0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@@ -14,7 +14,7 @@ HGLRC glContext;
|
||||
void raise() { SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); }
|
||||
void lower() { SetWindowPos(hwnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); }
|
||||
|
||||
void RWindow::setFlag(RWindowFlags flag, bool state) {
|
||||
void RWindow::SetFlag(RWindowFlags flag, bool state) {
|
||||
flags[(int) flag] = state;
|
||||
if (flag == RWindowFlags::RESIZABLE && !state) {
|
||||
RECT rect;
|
||||
@@ -24,9 +24,13 @@ void RWindow::setFlag(RWindowFlags flag, bool state) {
|
||||
SetWindowLong(hwnd, GWL_STYLE, style);
|
||||
SetWindowPos(hwnd, nullptr, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOOWNERZORDER);
|
||||
}
|
||||
}wasd
|
||||
void RWindow::SetResizable(bool resizable) {
|
||||
SetFlag(RWindowFlags::RESIZABLE, resizable);;
|
||||
}
|
||||
|
||||
void RWindow::pollEvents() {
|
||||
|
||||
void RWindow::PollEvents() {
|
||||
MSG msg;
|
||||
while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) {
|
||||
TranslateMessage(&msg);
|
||||
@@ -34,56 +38,56 @@ 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);
|
||||
}
|
||||
|
||||
Vector2 RWindow::getCursorPos() const {
|
||||
Vector2 RWindow::GetAccurateMouseCoordinates() const {
|
||||
POINT point;
|
||||
GetCursorPos(&point);
|
||||
ScreenToClient(hwnd, &point);
|
||||
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) {
|
||||
setPos(pos.x, pos.y);
|
||||
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);
|
||||
}
|
||||
|
||||
void RWindow::setVsyncEnabled(bool b) {
|
||||
void RWindow::SetVsyncEnabled(bool b) {
|
||||
typedef BOOL(WINAPI* PFNWGLSWAPINTERVALEXTPROC)(int interval);
|
||||
auto wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC) wglGetProcAddress("wglSwapIntervalEXT");
|
||||
|
||||
@@ -91,10 +95,6 @@ void RWindow::setVsyncEnabled(bool b) {
|
||||
wglSwapIntervalEXT(b ? 1 : 0);
|
||||
}
|
||||
|
||||
bool RWindow::isFullscreen() const {
|
||||
return fullscreenmode;
|
||||
}
|
||||
|
||||
RWindow* eWindow = nullptr;
|
||||
KeyboardState* pKeyboard = nullptr;
|
||||
KeyboardState* cKeyboard = nullptr;
|
||||
@@ -113,10 +113,10 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
||||
}
|
||||
|
||||
case WM_SIZE: {
|
||||
eWindow->setSizeWithoutEvent({(float) LOWORD(lParam), (float) HIWORD(lParam)});
|
||||
eWindow->SetSizeWithoutEvent({(float) LOWORD(lParam), (float) HIWORD(lParam)});
|
||||
auto eventData = WindowResizeRequestEvent();
|
||||
eventData.Size = {(float) eWindow->getWidth(), (float) eWindow->getHeight()};
|
||||
eWindow->setLastKnownWindowSize({(float) eWindow->getWidth(), (float) eWindow->getHeight()});
|
||||
eventData.Size = {(float) eWindow->GetWidth(), (float) eWindow->GetHeight()};
|
||||
eWindow->SetLastKnownWindowSize({(float) eWindow->GetWidth(), (float) eWindow->GetHeight()});
|
||||
|
||||
// TODO: Implement eWindow->processOnResize()
|
||||
|
||||
@@ -125,7 +125,7 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
||||
|
||||
|
||||
//Just to be absolutely sure the OpenGL viewport resizes along with the window.
|
||||
glViewport(0, 0, eWindow->getWidth(), eWindow->getHeight());
|
||||
glViewport(0, 0, eWindow->GetWidth(), eWindow->GetHeight());
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -133,7 +133,7 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
||||
|
||||
eWindow->processFocusIn();
|
||||
|
||||
eWindow->setFlag(RWindowFlags::IN_FOCUS, true);
|
||||
eWindow->SetFlag(RWindowFlags::IN_FOCUS, true);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -141,12 +141,12 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
||||
|
||||
eWindow->processFocusOut();
|
||||
|
||||
eWindow->setFlag(RWindowFlags::IN_FOCUS, false);
|
||||
eWindow->SetFlag(RWindowFlags::IN_FOCUS, false);
|
||||
break;
|
||||
}
|
||||
|
||||
case WM_SETCURSOR: {
|
||||
if (LOWORD(lParam) == HTCLIENT && eWindow->getCursorVisible() == false)
|
||||
if (LOWORD(lParam) == HTCLIENT && eWindow->GetCursorVisible() == false)
|
||||
SetCursor(nullptr);
|
||||
break;
|
||||
}
|
||||
@@ -239,7 +239,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;
|
||||
@@ -293,17 +293,10 @@ void RWindow::Open() {
|
||||
open = true;
|
||||
}
|
||||
|
||||
void RWindow::glSwapBuffers() {
|
||||
void RWindow::GLSwapBuffers() {
|
||||
SwapBuffers(hdc);
|
||||
}
|
||||
|
||||
void RWindow::refresh() {
|
||||
// TODO: Implement refresh time keeping
|
||||
OnRefresh(0.f);
|
||||
|
||||
// TODO: Check if mouse coords have changed, only then fire OnMouseMove event
|
||||
Vector2 mouse_coords = getCursorPos();
|
||||
|
||||
auto eventData = MouseMoveEvent(mouse_coords);
|
||||
OnMouseMove(eventData);
|
||||
}
|
||||
std::string RWindow::getGraphicsDriverVendor() {
|
||||
return std::string(reinterpret_cast<const char*>(glGetString(GL_VENDOR)));
|
||||
}
|
||||
|
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
|
||||
{
|
||||
|
||||
}
|
@@ -1,9 +1,25 @@
|
||||
#include <rewindow/types/gamepadbutton.h>
|
||||
|
||||
bool GamepadButton::operator==(const GamepadButton &rhs) const {
|
||||
bool ReWindow::GamepadButton::operator ==(const GamepadButton &rhs) const {
|
||||
return this->GetMnemonicButtonCode() == rhs.GetMnemonicButtonCode();
|
||||
}
|
||||
|
||||
void GamepadThumbstick::SetDeadzone(float minimum) const {
|
||||
|
||||
std::string ReWindow::GamepadButton::GetMnemonicButtonCode() const {
|
||||
return mnemonic_btn_code;
|
||||
}
|
||||
|
||||
void ReWindow::GamepadThumbstick::SetDeadzone(float minimum) {
|
||||
dead_zone = minimum;
|
||||
}
|
||||
|
||||
Vector2 ReWindow::GamepadThumbstick::GetPosition() const {
|
||||
return position;
|
||||
}
|
||||
|
||||
void ReWindow::GamepadTrigger::SetDeadzone(float minimum) {
|
||||
dead_zone = minimum;
|
||||
}
|
||||
|
||||
float ReWindow::GamepadTrigger::GetActuation() const {
|
||||
return position;
|
||||
}
|
||||
|
@@ -4,26 +4,22 @@
|
||||
|
||||
std::vector<Key> Key::GetKeyboard() { return keyboard; }
|
||||
|
||||
Key::Key() {
|
||||
keyboard.push_back(*this);
|
||||
}
|
||||
|
||||
|
||||
Key::Key(const char* charcode, X11Scancode scancode, WindowsScancode sc)
|
||||
: CharCode(charcode), x11ScanCode(scancode), winScanCode(sc)
|
||||
: Mnemonic(charcode), x11ScanCode(scancode), winScanCode(sc)
|
||||
{
|
||||
//TODO doing this is what crashes the program.
|
||||
keyboard.push_back(*this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Key::operator==(const Key &rhs) const {
|
||||
//This is not a good workaround.
|
||||
return (this->x11ScanCode == rhs.x11ScanCode);
|
||||
}
|
||||
|
||||
bool Key::operator<(const Key &rhs) const {
|
||||
return (this->CharCode < rhs.CharCode);
|
||||
return (this->Mnemonic < rhs.Mnemonic);
|
||||
}
|
||||
|
||||
|
||||
|
@@ -1,12 +1,11 @@
|
||||
#include <rewindow/types/mousebutton.h>
|
||||
#include <string>
|
||||
#include <X11/X.h>
|
||||
#include "rewindow/logger/logger.h"
|
||||
|
||||
MouseButton::MouseButton() {
|
||||
}
|
||||
|
||||
MouseButton::MouseButton(const char* charcode, unsigned int index) {
|
||||
this->CharCode = charcode;
|
||||
MouseButton::MouseButton(const std::string& charcode, unsigned int index) {
|
||||
this->Mnemonic = charcode;
|
||||
this->ButtonIndex = index;
|
||||
}
|
||||
|
||||
@@ -14,14 +13,18 @@ bool MouseButton::operator==(const MouseButton &mb) const {
|
||||
return (mb.ButtonIndex == this->ButtonIndex);
|
||||
}
|
||||
|
||||
bool MouseButton::operator<(const MouseButton &rhs) const {
|
||||
return (this->ButtonIndex < rhs.ButtonIndex);
|
||||
}
|
||||
|
||||
|
||||
MouseButton GetMouseButtonFromXButton(unsigned int button) {
|
||||
switch(button) {
|
||||
case 1: return MouseButtons::Left;
|
||||
case 2: return MouseButtons::Middle;
|
||||
case 3: return MouseButtons::Right;
|
||||
case 4: return MouseButtons::MWheelUp;
|
||||
case 5: return MouseButtons::MWheelDown;
|
||||
//case 4: return MouseButtons::MWheelUp;
|
||||
//case 5: return MouseButtons::MWheelDown;
|
||||
//For *whatever* reason. These aren't in X.h
|
||||
case 8: return MouseButtons::Mouse4;
|
||||
case 9: return MouseButtons::Mouse5;
|
||||
|
Reference in New Issue
Block a user