Compare commits
51 Commits
Prerelease
...
input_refa
Author | SHA1 | Date | |
---|---|---|---|
4b2aeea2ca | |||
feacea61b1 | |||
a3adbb6266 | |||
b75f44f1ee | |||
e4e860564c | |||
fb6825224f | |||
6b9bf4c2cc | |||
f25370cada | |||
f826b2a268 | |||
5dfa0fe114 | |||
216b7218e6 | |||
dc466e695f | |||
a4322c4b21 | |||
db63cc2272 | |||
1d9424a243 | |||
2e0d84d559 | |||
cc2cc6f7c1 | |||
fb6ed84b76 | |||
|
3085ad21a9 | ||
35b09cb026 | |||
6bd9ec0eb8 | |||
ae073b3a96 | |||
48091496d5 | |||
c202ca5c84 | |||
c5f94b7c49 | |||
b351577a20 | |||
fb3a77e2e3 | |||
b96dd03af1 | |||
3e99394b6f | |||
5b10e55f83 | |||
c0dc56bc8c | |||
a62c576d14 | |||
92b567e48f | |||
|
3617d610ea | ||
9500681420 | |||
bf41652336 | |||
68ae72e6be | |||
a4b68a5abc | |||
1345669c32 | |||
9d51ac0026 | |||
edb2e29246 | |||
bed600d4d4 | |||
acf66ae909 | |||
c319acfffb | |||
9b810101a4 | |||
4c616d68af | |||
4571016c4c | |||
a8c85a29dd | |||
483b5dadad | |||
9565bfeef9 | |||
e1fc2f289f |
@@ -17,7 +17,7 @@ jobs:
|
||||
- run: echo "The ${{ gitea.repository }} repository has been cloned to the runner."
|
||||
- run: echo "The workflow is now ready to run your tests on the runner."
|
||||
- run: echo "Install toolchain and run ReCI build test"
|
||||
- run: apt-get update && apt-get install -y lua5.3 git && git clone $RECI_GIT $RECI
|
||||
- run: apt-get update && apt-get install -y lua5.3 git libxrandr-dev && git clone $RECI_GIT $RECI
|
||||
- run: lua $RECI/reci.lua -f $RECI/scripts/buildtools.reci -f reci/scripts/builddeps.reci -f $RECI/scripts/buildtest.reci
|
||||
- run: echo this exists so I can run the reci test
|
||||
- run: echo "This job's status is ${{ job.status }}."
|
||||
|
@@ -1,4 +1,4 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
cmake_minimum_required(VERSION 3.18..3.27)
|
||||
project(ReWindowLibrary
|
||||
VERSION 1.0
|
||||
LANGUAGES CXX
|
||||
@@ -18,17 +18,17 @@ include(cmake/CPM.cmake)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME J3ML
|
||||
URL https://git.redacted.cc/josh/j3ml/archive/Release-2.2.zip
|
||||
URL https://git.redacted.cc/josh/j3ml/archive/Release-3.2.zip
|
||||
)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME Event
|
||||
URL https://git.redacted.cc/josh/Event/archive/Release-6.zip
|
||||
URL https://git.redacted.cc/josh/Event/archive/Release-12.zip
|
||||
)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME jlog
|
||||
URL https://git.redacted.cc/josh/jlog/archive/Prerelease-9.zip
|
||||
URL https://git.redacted.cc/josh/jlog/archive/Prerelease-16.zip
|
||||
)
|
||||
|
||||
find_package(OpenGL REQUIRED)
|
||||
@@ -39,12 +39,15 @@ include_directories(${jlog_SOURCE_DIR}/include)
|
||||
|
||||
file(GLOB_RECURSE HEADERS "include/*.h" "include/*.hpp")
|
||||
|
||||
file(GLOB_RECURSE HEADERS "include/logger/*.h" "include/logger/*.hpp")
|
||||
|
||||
if(UNIX AND NOT APPLE)
|
||||
file(GLOB_RECURSE SOURCES "src/types/*.cpp" "src/platform/linux/*.cpp" "src/platform/shared/*.cpp")
|
||||
|
||||
file(GLOB_RECURSE SOURCES "src/types/*.cpp" "src/platform/linux/*.cpp" "src/platform/shared/*.cpp" "src/logger/*.cpp" )
|
||||
endif()
|
||||
|
||||
if(WIN32)
|
||||
file(GLOB_RECURSE SOURCES "src/types/*.cpp" "src/platform/windows/*.cpp" "src/platform/shared/*.cpp")
|
||||
file(GLOB_RECURSE SOURCES "src/types/*.cpp" "src/platform/windows/*.cpp" "src/platform/shared/*.cpp" "src/logger/*.cpp")
|
||||
endif()
|
||||
|
||||
include_directories("include")
|
||||
@@ -63,7 +66,7 @@ target_include_directories(ReWindowLibrary PUBLIC ${Event_SOURCE_DIR}/include)
|
||||
set_target_properties(ReWindowLibrary PROPERTIES LINKER_LANGUAGE CXX)
|
||||
|
||||
if(UNIX AND NOT APPLE)
|
||||
target_link_libraries(ReWindowLibrary PUBLIC X11 J3ML Event jlog ${OPENGL_LIBRARIES})
|
||||
target_link_libraries(ReWindowLibrary PUBLIC X11 Xrandr J3ML Event jlog ${OPENGL_LIBRARIES})
|
||||
target_link_libraries(ReWindowLibrary PUBLIC)
|
||||
|
||||
add_executable(ReWindowLibraryDemo main.cpp)
|
||||
@@ -75,4 +78,4 @@ if(WIN32)
|
||||
target_link_libraries(ReWindowLibrary PUBLIC J3ML Event jlog ${OPENGL_LIBRARIES})
|
||||
add_executable(ReWindowLibraryDemo main.cpp)
|
||||
target_link_libraries(ReWindowLibraryDemo PUBLIC ReWindowLibrary)
|
||||
endif()
|
||||
endif()
|
||||
|
@@ -11,8 +11,8 @@ A library which allows easily creating and managing a window and it's events ac
|
||||
Install dependencies
|
||||
|
||||
```bash
|
||||
dnf install cmake make gcc-g++ libX11 libX11-devel
|
||||
apt-get install cmake make gcc g++ libx11-6 libx11-dev libgl-dev
|
||||
Fedora/RHEL: dnf install cmake make gcc-g++ libX11 libX11-devel mesa-libGL-devel
|
||||
Ubuntu/Debian: apt-get install cmake make gcc g++ libx11-6 libx11-dev libgl-dev libxrandr-dev
|
||||
```
|
||||
|
||||
Clone the repository
|
||||
|
273
include/rewindow/data/scancode.h
Normal file
273
include/rewindow/data/scancode.h
Normal file
@@ -0,0 +1,273 @@
|
||||
/// ReWindowLibrary
|
||||
/// A C++20 Library for creating and managing windows in a platform-independent manner
|
||||
/// Developed and Maintained by the boys @ Redacted Software.
|
||||
/// (c) 2024 Redacted Software
|
||||
/// This work is dedicated to the public domain.
|
||||
|
||||
/// @file scancode.h
|
||||
/// @desc Abstraction layer for input device scancodes
|
||||
/// @edit 2024-12-03
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace ReWindow {
|
||||
enum class ReScanCodeIndex : unsigned int {
|
||||
Unimplemented = 0,
|
||||
|
||||
MLeft = 1,
|
||||
MRight = 2,
|
||||
MMiddle = 3,
|
||||
MWheel_Up = 4,
|
||||
MWheel_Down = 5,
|
||||
MSide_A = 8,
|
||||
MSide_B = 9,
|
||||
|
||||
KP_0 = 90,
|
||||
KP_1 = 87,
|
||||
KP_2 = 88,
|
||||
KP_3 = 89,
|
||||
KP_4 = 83,
|
||||
KP_5 = 84,
|
||||
KP_6 = 85,
|
||||
KP_7 = 79,
|
||||
KP_8 = 80,
|
||||
KP_9 = 81,
|
||||
KP_PERIOD = 91,
|
||||
KP_RETURN = 104,
|
||||
KP_PLUS = 86,
|
||||
KP_MINUS = 82,
|
||||
KP_ASTERISK = 63,
|
||||
KP_SLASH = 106,
|
||||
KP_NUMLOCK = 77,
|
||||
PAGEUP = 112,
|
||||
PAGEDOWN = 117,
|
||||
HOME = 110,
|
||||
MENU = 135,
|
||||
END = 115,
|
||||
INSERT = 118,
|
||||
DEL = 119,
|
||||
UP = 111,
|
||||
DOWN = 116,
|
||||
LEFT = 113,
|
||||
RIGHT = 114,
|
||||
BACKSPACE = 22,
|
||||
EQUALS = 21,
|
||||
MINUS = 20,
|
||||
ZERO = 19,
|
||||
NINE = 18,
|
||||
EIGHT = 17,
|
||||
SEVEN = 16,
|
||||
SIX = 15,
|
||||
FIVE = 14,
|
||||
FOUR = 13,
|
||||
THREE = 12,
|
||||
TWO = 11,
|
||||
ONE = 10,
|
||||
GRAVE = 49,
|
||||
BACKSLASH = 51,
|
||||
CLOSING_SQUARE_BRACKET = 35,
|
||||
OPENING_SQUARE_BRACKET = 34,
|
||||
P = 33,
|
||||
O = 32,
|
||||
I = 31,
|
||||
U = 30,
|
||||
Y = 29,
|
||||
T = 28,
|
||||
R = 27,
|
||||
E = 26,
|
||||
W = 25,
|
||||
Q = 24,
|
||||
TAB = 23,
|
||||
RETURN = 36,
|
||||
SINGLEQUOTE = 48,
|
||||
SEMICOLON = 47,
|
||||
L = 46,
|
||||
K = 45,
|
||||
J = 44,
|
||||
H = 43,
|
||||
G = 42,
|
||||
F = 41,
|
||||
D = 40,
|
||||
S = 39,
|
||||
A = 38,
|
||||
CAPSLOCK = 66,
|
||||
RIGHT_SHIFT = 62,
|
||||
SLASH = 61,
|
||||
PERIOD = 60,
|
||||
COMMA = 59,
|
||||
M = 58,
|
||||
N = 57,
|
||||
B = 56,
|
||||
V = 55,
|
||||
C = 54,
|
||||
X = 53,
|
||||
Z = 52,
|
||||
LEFT_SHIFT = 50,
|
||||
RIGHT_CONTROL = 105,
|
||||
RIGHT_ALT = 108,
|
||||
SPACE = 65,
|
||||
LEFT_ALT = 64,
|
||||
SUPER = 133,
|
||||
LEFT_CTRL = 37,
|
||||
ESCAPE = 9,
|
||||
F1 = 67,
|
||||
F2 = 68,
|
||||
F3 = 69,
|
||||
F4 = 70,
|
||||
F5 = 71,
|
||||
F6 = 72,
|
||||
F7 = 73,
|
||||
F8 = 74,
|
||||
F9 = 75,
|
||||
F10 = 76,
|
||||
F11 = 95,
|
||||
F12 = 96,
|
||||
PRINT = 107,
|
||||
SCROLL_LOCK = 78,
|
||||
BREAK = 127,
|
||||
};
|
||||
|
||||
class ReScanCode
|
||||
{
|
||||
public:
|
||||
std::string Mnemonic;
|
||||
unsigned int Sindex;
|
||||
|
||||
ReScanCode() = default;
|
||||
explicit ReScanCode(const std::string& string, ReScanCodeIndex sindex);
|
||||
explicit ReScanCode(const std::string& string, unsigned int sindex);
|
||||
bool operator == (const ReScanCode& rs) const;
|
||||
bool operator<(const ReScanCode& rhs) const;
|
||||
ReScanCode(const ReScanCode&) = default;
|
||||
};
|
||||
|
||||
namespace ReScanCodes {
|
||||
static const ReScanCode Unimplemented ("?", 0);
|
||||
|
||||
static const ReScanCode MouseButtonLeft ("ML", ReScanCodeIndex::MLeft);
|
||||
static const ReScanCode MouseButtonRight ("MR", ReScanCodeIndex::MRight);
|
||||
static const ReScanCode MouseButtonMiddle ("MM", ReScanCodeIndex::MMiddle);
|
||||
static const ReScanCode MouseButtonSideA ("M4", ReScanCodeIndex::MSide_A);
|
||||
static const ReScanCode MouseButtonSideB ("M5", ReScanCodeIndex::MSide_B);
|
||||
/// NOTE: IsMouseButtonDown will not return correctly for the mouse-wheel-buttons, because the action is effectively instantaneous.
|
||||
static const ReScanCode MWheelUp ("MWU", ReScanCodeIndex::MWheel_Up);
|
||||
/// NOTE: IsMouseButtonDown will not return correctly for the mouse-wheel-buttons, because the action is effectively instantaneous.
|
||||
static const ReScanCode MWheelDown ("MWD", ReScanCodeIndex::MWheel_Down);
|
||||
|
||||
// TODO: Encode both Uppercase and Lowercase version for each keymap
|
||||
static const ReScanCode Escape ("ESC", ReScanCodeIndex::ESCAPE);
|
||||
static const ReScanCode F1 ("F1", ReScanCodeIndex::F1);
|
||||
static const ReScanCode F2 ("F2", ReScanCodeIndex::F2);
|
||||
static const ReScanCode F3 ("F3", ReScanCodeIndex::F3);
|
||||
static const ReScanCode F4 ("F4", ReScanCodeIndex::F4);
|
||||
static const ReScanCode F5 {"F5", ReScanCodeIndex::F5};
|
||||
static const ReScanCode F6 {"F6", ReScanCodeIndex::F6};
|
||||
static const ReScanCode F7 {"F7", ReScanCodeIndex::F7};
|
||||
static const ReScanCode F8 {"F8", ReScanCodeIndex::F8};
|
||||
static const ReScanCode F9 {"F9", ReScanCodeIndex::F9};
|
||||
static const ReScanCode F10 {"F10", ReScanCodeIndex::F10};
|
||||
static const ReScanCode F11 {"F11", ReScanCodeIndex::F11};
|
||||
static const ReScanCode F12 {"F12", ReScanCodeIndex::F12};
|
||||
static const ReScanCode Print {"PRINT", ReScanCodeIndex::PRINT};
|
||||
static const ReScanCode ScrollLock {"SCROLL_LOCK", ReScanCodeIndex::SCROLL_LOCK};
|
||||
static const ReScanCode Break {"BREAK", ReScanCodeIndex::BREAK};
|
||||
|
||||
//TODO On Windows, Return & KP_RETURN are the same thing.
|
||||
static const ReScanCode NumPadReturn {"KP_↵", ReScanCodeIndex::KP_RETURN};
|
||||
static const ReScanCode NumPadPlus {"KP_+", ReScanCodeIndex::KP_PLUS};
|
||||
static const ReScanCode NumPadMinus {"KP_-", ReScanCodeIndex::KP_MINUS};
|
||||
static const ReScanCode NumPadAsterisk {"KP_*", ReScanCodeIndex::KP_ASTERISK};
|
||||
static const ReScanCode NumPadForwardSlash {"KP_/", ReScanCodeIndex::KP_SLASH};
|
||||
static const ReScanCode NumPadPeriod {"KP_.", ReScanCodeIndex::KP_PERIOD};
|
||||
static const ReScanCode NumPadNumLock {"KP_NUMLOCK", ReScanCodeIndex::KP_NUMLOCK};
|
||||
|
||||
static const ReScanCode NumPad1 {"KP_1", ReScanCodeIndex::KP_1};
|
||||
static const ReScanCode NumPad2 {"KP_2", ReScanCodeIndex::KP_2};
|
||||
static const ReScanCode NumPad3 {"KP_3", ReScanCodeIndex::KP_3};
|
||||
static const ReScanCode NumPad4 {"KP_4", ReScanCodeIndex::KP_4};
|
||||
static const ReScanCode NumPad5 {"KP_5", ReScanCodeIndex::KP_5};
|
||||
static const ReScanCode NumPad6 {"KP_6", ReScanCodeIndex::KP_6};
|
||||
static const ReScanCode NumPad7 {"KP_7", ReScanCodeIndex::KP_7};
|
||||
static const ReScanCode NumPad8 {"kP_8", ReScanCodeIndex::KP_8};
|
||||
static const ReScanCode NumPad9 {"KP_9", ReScanCodeIndex::KP_9};
|
||||
static const ReScanCode NumPad0 {"KP_0", ReScanCodeIndex::KP_0};
|
||||
|
||||
static const ReScanCode Grave {"`", ReScanCodeIndex::GRAVE};
|
||||
static const ReScanCode One {"1", ReScanCodeIndex::ONE};
|
||||
static const ReScanCode Two {"2", ReScanCodeIndex::TWO};
|
||||
static const ReScanCode Three {"3", ReScanCodeIndex::THREE};
|
||||
static const ReScanCode Four {"4", ReScanCodeIndex::FOUR};
|
||||
static const ReScanCode Five {"5", ReScanCodeIndex::FIVE};
|
||||
static const ReScanCode Six {"6", ReScanCodeIndex::SIX};
|
||||
static const ReScanCode Seven {"7", ReScanCodeIndex::SEVEN};
|
||||
static const ReScanCode Eight {"8", ReScanCodeIndex::EIGHT};
|
||||
static const ReScanCode Nine {"9", ReScanCodeIndex::NINE};
|
||||
static const ReScanCode Zero {"0", ReScanCodeIndex::ZERO};
|
||||
static const ReScanCode Minus {"-", ReScanCodeIndex::MINUS};
|
||||
static const ReScanCode Equals {"+", ReScanCodeIndex::EQUALS};
|
||||
static const ReScanCode Backspace {"⌫", ReScanCodeIndex::BACKSPACE};
|
||||
|
||||
static const ReScanCode Tab {"⇥", ReScanCodeIndex::TAB};
|
||||
static const ReScanCode Q {"Q", ReScanCodeIndex::Q};
|
||||
static const ReScanCode W {"W", ReScanCodeIndex::W};
|
||||
static const ReScanCode E {"E", ReScanCodeIndex::E};
|
||||
static const ReScanCode R {"R", ReScanCodeIndex::R};
|
||||
static const ReScanCode T {"T", ReScanCodeIndex::T};
|
||||
static const ReScanCode Y {"Y", ReScanCodeIndex::Y};
|
||||
static const ReScanCode U {"U", ReScanCodeIndex::U};
|
||||
static const ReScanCode I {"I", ReScanCodeIndex::I};
|
||||
static const ReScanCode O {"O", ReScanCodeIndex::O};
|
||||
static const ReScanCode P {"P", ReScanCodeIndex::P};
|
||||
static const ReScanCode LeftBracket {"[", ReScanCodeIndex::OPENING_SQUARE_BRACKET};
|
||||
static const ReScanCode RightBracket {"]", ReScanCodeIndex::CLOSING_SQUARE_BRACKET};
|
||||
static const ReScanCode BackSlash {"\\", ReScanCodeIndex::BACKSLASH};
|
||||
|
||||
static const ReScanCode CapsLock {"CAPS", ReScanCodeIndex::CAPSLOCK};
|
||||
static const ReScanCode A {"A", ReScanCodeIndex::A};
|
||||
static const ReScanCode S {"S", ReScanCodeIndex::S};
|
||||
static const ReScanCode D {"D", ReScanCodeIndex::D};
|
||||
static const ReScanCode F {"F", ReScanCodeIndex::F};
|
||||
static const ReScanCode G {"G", ReScanCodeIndex::G};
|
||||
static const ReScanCode H {"H", ReScanCodeIndex::H};
|
||||
static const ReScanCode J {"J", ReScanCodeIndex::J};
|
||||
static const ReScanCode K {"K", ReScanCodeIndex::K};
|
||||
static const ReScanCode L {"L", ReScanCodeIndex::L};
|
||||
static const ReScanCode Semicolon {";", ReScanCodeIndex::SEMICOLON};
|
||||
static const ReScanCode SingeQuote {"\'", ReScanCodeIndex::SINGLEQUOTE};
|
||||
static const ReScanCode Return {"↵", ReScanCodeIndex::RETURN};
|
||||
|
||||
static const ReScanCode LeftShift {"⇧", ReScanCodeIndex::LEFT_SHIFT};
|
||||
static const ReScanCode Z {"Z", ReScanCodeIndex::Z};
|
||||
static const ReScanCode X {"X", ReScanCodeIndex::X};
|
||||
static const ReScanCode C {"C", ReScanCodeIndex::C};
|
||||
static const ReScanCode V {"V", ReScanCodeIndex::V};
|
||||
static const ReScanCode B {"B", ReScanCodeIndex::B};
|
||||
static const ReScanCode N {"N", ReScanCodeIndex::N};
|
||||
static const ReScanCode M {"M", ReScanCodeIndex::M};
|
||||
static const ReScanCode Comma {",", ReScanCodeIndex::COMMA};
|
||||
static const ReScanCode Period {".", ReScanCodeIndex::PERIOD};
|
||||
static const ReScanCode ForwardSlash {"/", ReScanCodeIndex::SLASH};
|
||||
|
||||
static const ReScanCode LeftControl {"LCTRL", ReScanCodeIndex::LEFT_CTRL};
|
||||
static const ReScanCode Super {"❖", ReScanCodeIndex::SUPER};
|
||||
static const ReScanCode LeftAlt {"🄰", ReScanCodeIndex::LEFT_ALT};
|
||||
static const ReScanCode Space {" ", ReScanCodeIndex::SPACE};
|
||||
static const ReScanCode RightAlt {"R🄰", ReScanCodeIndex::RIGHT_ALT};
|
||||
static const ReScanCode Menu {"▤", ReScanCodeIndex::MENU};
|
||||
static const ReScanCode RightControl {"RCTRL", ReScanCodeIndex::RIGHT_CONTROL};
|
||||
static const ReScanCode RightShift {"R⇧", ReScanCodeIndex::RIGHT_SHIFT};
|
||||
|
||||
static const ReScanCode Insert {"INSERT", ReScanCodeIndex::INSERT};
|
||||
static const ReScanCode Home {"HOME", ReScanCodeIndex::HOME};
|
||||
static const ReScanCode PageUp {"PAGEUP", ReScanCodeIndex::PAGEUP};
|
||||
static const ReScanCode Delete {"DELETE", ReScanCodeIndex::DEL};
|
||||
static const ReScanCode End {"End", ReScanCodeIndex::END};
|
||||
static const ReScanCode PageDown {"PAGEDOWN", ReScanCodeIndex::PAGEDOWN};
|
||||
|
||||
static const ReScanCode UpArrow {("↑"), ReScanCodeIndex::UP};
|
||||
static const ReScanCode DownArrow {("↓"), ReScanCodeIndex::DOWN};
|
||||
static const ReScanCode LeftArrow {("←"), ReScanCodeIndex::LEFT};
|
||||
static const ReScanCode RightArrow {("→"), ReScanCodeIndex::RIGHT};
|
||||
}
|
||||
}
|
||||
|
10
include/rewindow/logger/logger.h
Normal file
10
include/rewindow/logger/logger.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "jlog/Logger.hpp"
|
||||
|
||||
namespace ReWindow::Logger {
|
||||
using namespace jlog;
|
||||
|
||||
extern GenericLogger Fatal;
|
||||
extern GenericLogger 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;
|
||||
};
|
||||
}
|
39
include/rewindow/types/display.h
Normal file
39
include/rewindow/types/display.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include <J3ML/J3ML.hpp>
|
||||
|
||||
namespace ReWindow {
|
||||
class FullscreenGraphicsMode;
|
||||
class Display;
|
||||
}
|
||||
|
||||
///A given graphics mode that a display supports.
|
||||
///Upon startup, We'll poll the display server for this.
|
||||
class ReWindow::FullscreenGraphicsMode {
|
||||
private:
|
||||
u32 width;
|
||||
u32 height;
|
||||
short refresh_rate;
|
||||
public:
|
||||
FullscreenGraphicsMode() = default;
|
||||
FullscreenGraphicsMode(u32 width, u32 height, short refresh_rate);
|
||||
[[nodiscard]] u32 getWidth() const;
|
||||
[[nodiscard]] u32 getHeight() const;
|
||||
[[nodiscard]] float getRefreshRate() const;
|
||||
};
|
||||
|
||||
///Refers to a particular monitor the user has.
|
||||
class ReWindow::Display {
|
||||
private:
|
||||
///* The graphics mode of the display when our program was started. The primary use for this
|
||||
///* is so that if the user has selected a different mode in-game we can restore the old mode upon exit. *///
|
||||
FullscreenGraphicsMode default_graphics_mode{};
|
||||
///All graphics modes that a given display supports.
|
||||
std::vector<FullscreenGraphicsMode> graphics_modes;
|
||||
public:
|
||||
Display() = default;
|
||||
Display(const FullscreenGraphicsMode& default_graphics_mode, const std::vector<FullscreenGraphicsMode>& graphics_modes);
|
||||
FullscreenGraphicsMode getDefaultGraphicsMode();
|
||||
std::vector<FullscreenGraphicsMode> getGraphicsModes();
|
||||
static std::vector<Display> getDisplaysFromWindowManager();
|
||||
};
|
@@ -17,6 +17,7 @@ namespace ReWindow
|
||||
|
||||
class Gamepad : public InputDevice
|
||||
{
|
||||
public:
|
||||
|
||||
};
|
||||
|
||||
|
@@ -9,7 +9,7 @@
|
||||
/// @edit 2024-07-29
|
||||
|
||||
#include <string>
|
||||
#include <J3ML/LinearAlgebra.h>
|
||||
#include <J3ML/LinearAlgebra.hpp>
|
||||
|
||||
class GamepadButton {
|
||||
protected:
|
||||
|
39
include/rewindow/types/inputdevice.hpp
Normal file
39
include/rewindow/types/inputdevice.hpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
namespace ReWindow
|
||||
{
|
||||
|
||||
enum class DeviceType {
|
||||
MOUSE, KEYBOARD, GAMEPAD, TOUCHSCREEN, REMOTE, VRHEADSET
|
||||
};
|
||||
|
||||
enum class DeviceState {
|
||||
OK, LOW_BATTERY, UNAVAILABLE
|
||||
};
|
||||
|
||||
class InputDeviceImpl { };
|
||||
class InputDeviceImplLinux { };
|
||||
|
||||
class InputDevice {
|
||||
public:
|
||||
virtual void Update(double elapsed){}
|
||||
|
||||
/// Returns the device's index among devices of the same type.
|
||||
uint GetIndex() const;
|
||||
|
||||
virtual DeviceType GetType() const = 0;
|
||||
DeviceState GetDeviceState() const;
|
||||
bool IsAnyButtonDown();
|
||||
};
|
||||
|
||||
struct InputEvent
|
||||
{
|
||||
std::chrono::system_clock::time_point Timestamp;
|
||||
};
|
||||
|
||||
struct BinaryActuatorState
|
||||
{
|
||||
double TimeHeld;
|
||||
bool Pressed;
|
||||
};
|
||||
}
|
32
include/rewindow/types/inputservice.hpp
Normal file
32
include/rewindow/types/inputservice.hpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
#include <rewindow/types/mouse.h>
|
||||
|
||||
namespace ReWindow
|
||||
{
|
||||
|
||||
namespace detail
|
||||
{
|
||||
static Mouse* internal_mouse;
|
||||
|
||||
void UpdateMouse(Mouse* mouse) {
|
||||
detail::internal_mouse = mouse;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace InputService
|
||||
{
|
||||
namespace Mouse
|
||||
{
|
||||
Vector2 GetMousePosition()
|
||||
{
|
||||
return detail::internal_mouse->GetPosition();
|
||||
}
|
||||
|
||||
bool IsButtonDown(const MouseButton &btn) {
|
||||
return detail::internal_mouse->IsButtonDown(btn);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -8,9 +8,8 @@
|
||||
#include <string>
|
||||
#include <rewindow/data/X11Scancodes.h>
|
||||
#include <rewindow/data/WindowsScancodes.h>
|
||||
#include <J3ML/LinearAlgebra/Vector2.h>
|
||||
#include <J3ML/LinearAlgebra.hpp>
|
||||
|
||||
using J3ML::LinearAlgebra::Vector2;
|
||||
|
||||
class Key
|
||||
{
|
||||
@@ -20,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;
|
||||
|
@@ -10,13 +10,40 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <rewindow/types/inputdevice.hpp>
|
||||
#include "rewindow/data/scancode.h"
|
||||
#include <Event.h>
|
||||
#include <map>
|
||||
|
||||
namespace ReWindow
|
||||
{
|
||||
class InputDevice {};
|
||||
using Key = ReScanCode;
|
||||
using KeyState = BinaryActuatorState;
|
||||
struct KeyboardState
|
||||
{
|
||||
std::map<Key, KeyState> Keys;
|
||||
};
|
||||
|
||||
class KeyPressedEvent : public InputEvent {};
|
||||
class KeyReleasedEvent : public InputEvent {};
|
||||
|
||||
class Keyboard : public InputDevice
|
||||
{
|
||||
public:
|
||||
Keyboard() {}
|
||||
~Keyboard() {}
|
||||
bool IsKeyDown() const;
|
||||
KeyState GetKeyState() const;
|
||||
|
||||
void Update(double elapsed) override;
|
||||
DeviceType GetType() const override { return DeviceType::KEYBOARD;}
|
||||
public:
|
||||
static Event<const KeyPressedEvent&> KeyPressed;
|
||||
static Event<const KeyReleasedEvent&> KeyReleased;
|
||||
|
||||
public:
|
||||
static Keyboard* GetCurrentDevice();
|
||||
static bool GetKeyState(const Key& k);
|
||||
static KeyboardState GetState();
|
||||
};
|
||||
}
|
@@ -10,17 +10,329 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
//#include <rewindow/types/inputdevice.hpp>
|
||||
#include <rewindow/types/mousebutton.h>
|
||||
#include <rewindow/types/key.h>
|
||||
#include <Event.h>
|
||||
#include <map>
|
||||
#include "rewindow/data/scancode.h"
|
||||
#include <rewindow/types/inputdevice.hpp>
|
||||
//#include <rewindow/types/keyboard.h>
|
||||
//#include <rewindow/types/gamepad.h>
|
||||
|
||||
namespace ReWindow
|
||||
{
|
||||
|
||||
class InputDevice {}; // TODO: Remember to break InputDevice into it's own file and not define it twice!!!
|
||||
//using Key = ReScanCode;
|
||||
|
||||
class Pointer : public InputDevice {};
|
||||
using Button = ReScanCode;
|
||||
|
||||
/*
|
||||
enum class DeviceType {
|
||||
MOUSE, KEYBOARD, GAMEPAD, TOUCHSCREEN, REMOTE, VRHEADSET
|
||||
};
|
||||
|
||||
enum class DeviceState {
|
||||
OK, LOW_BATTERY, UNAVAILABLE
|
||||
};
|
||||
*/
|
||||
|
||||
|
||||
class Mouse : public Pointer
|
||||
/*
|
||||
class InputDeviceImpl { };
|
||||
class InputDeviceImplLinux { };
|
||||
*/
|
||||
class LinuxMouseImpl { };
|
||||
class LinuxKeyboardImpl {};
|
||||
|
||||
// TODO: Remember to break InputDevice into it's own file and not define it twice!!!
|
||||
/*
|
||||
class InputDevice {
|
||||
public:
|
||||
virtual void Update(double elapsed){}
|
||||
|
||||
/// Returns the device's index among devices of the same type.
|
||||
uint GetIndex() const;
|
||||
|
||||
virtual DeviceType GetType() const = 0;
|
||||
DeviceState GetDeviceState() const;
|
||||
bool IsAnyButtonDown();
|
||||
};
|
||||
*/
|
||||
|
||||
/*
|
||||
struct BinaryActuatorState
|
||||
{
|
||||
double TimeHeld;
|
||||
bool Pressed;
|
||||
};
|
||||
*/
|
||||
|
||||
using ButtonState = BinaryActuatorState;
|
||||
//using KeyState = BinaryActuatorState;
|
||||
|
||||
/*
|
||||
struct KeyboardState
|
||||
{
|
||||
std::map<Key, KeyState> Keys;
|
||||
|
||||
};
|
||||
*/
|
||||
|
||||
/*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");
|
||||
}
|
||||
};*/
|
||||
|
||||
struct MouseState
|
||||
{
|
||||
ButtonState Left;
|
||||
ButtonState Right;
|
||||
ButtonState Middle;
|
||||
ButtonState Mouse4;
|
||||
ButtonState Mouse5;
|
||||
ButtonState WheelUp;
|
||||
ButtonState WheelDown;
|
||||
|
||||
Vector2 Position;
|
||||
Vector2 Delta;
|
||||
Vector2 Velocity;
|
||||
int Wheel;
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
struct ThumbstickState
|
||||
{
|
||||
|
||||
Vector2 Unit;
|
||||
};
|
||||
|
||||
class Thumbstick
|
||||
{
|
||||
|
||||
};
|
||||
|
||||
struct TriggerState
|
||||
{
|
||||
|
||||
};
|
||||
|
||||
class Trigger
|
||||
{
|
||||
|
||||
};
|
||||
|
||||
struct GamepadState
|
||||
{
|
||||
ButtonState X;
|
||||
ButtonState Y;
|
||||
ButtonState A;
|
||||
ButtonState B;
|
||||
|
||||
// TODO: Also have analog DPad;
|
||||
ButtonState DPadLeft;
|
||||
ButtonState DPadRight;
|
||||
ButtonState DPadUp;
|
||||
ButtonState DPadDown;
|
||||
|
||||
ButtonState Pause;
|
||||
ButtonState Select;
|
||||
ButtonState Home;
|
||||
|
||||
ButtonState LeftThumbButton;
|
||||
ButtonState RightThumbButton;
|
||||
|
||||
TriggerState LeftTrigger;
|
||||
TriggerState RightTrigger;
|
||||
|
||||
ThumbstickState LeftThumbstick;
|
||||
ThumbstickState RightThumbstick;
|
||||
|
||||
};
|
||||
|
||||
class TouchscreenState
|
||||
{
|
||||
|
||||
};
|
||||
|
||||
/*
|
||||
* Gamepad
|
||||
*/
|
||||
|
||||
constexpr int MaxDeviceStateHistory = 100;
|
||||
|
||||
class Gamepad : public InputDevice
|
||||
{
|
||||
public:
|
||||
uint GetButtonCount() const;
|
||||
uint GetRumbleMotorCount() const;
|
||||
protected:
|
||||
Thumbstick LeftThumb;
|
||||
Thumbstick RightThumb;
|
||||
|
||||
Trigger RightTrigger;
|
||||
Trigger LeftTrigger;
|
||||
|
||||
GamepadState current_state;
|
||||
GamepadState previous_state;
|
||||
GamepadState history[MaxDeviceStateHistory];
|
||||
};
|
||||
|
||||
|
||||
class XboxGamepad : public Gamepad {};
|
||||
class PS4Gamepad : public Gamepad {};
|
||||
|
||||
/*
|
||||
* Pointer
|
||||
*/
|
||||
|
||||
class PointerDevice : public InputDevice {
|
||||
public:
|
||||
void Update(double elapsed) override {
|
||||
InputDevice::Update(elapsed);
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
struct InputEvent
|
||||
{
|
||||
std::chrono::system_clock::time_point Timestamp;
|
||||
};
|
||||
*/
|
||||
|
||||
struct MouseButtonEvent : public InputEvent
|
||||
{
|
||||
|
||||
};
|
||||
|
||||
class MouseMoveEvent
|
||||
{
|
||||
|
||||
};
|
||||
|
||||
class MouseWheelEvent
|
||||
{
|
||||
|
||||
};
|
||||
|
||||
struct TouchEvent
|
||||
{
|
||||
|
||||
int TouchID;
|
||||
Vector2 Position;
|
||||
Vector2 Velocity;
|
||||
float Pressure;
|
||||
};
|
||||
|
||||
class Mouse : public PointerDevice
|
||||
{
|
||||
public:
|
||||
public:
|
||||
Event<> Moved;
|
||||
Event<> ButtonPressed;
|
||||
Event<> ButtonReleased;
|
||||
Event<> WheelMoved;
|
||||
public:
|
||||
Mouse();
|
||||
~Mouse();
|
||||
Vector2 GetPosition() const;
|
||||
bool IsButtonDown(const MouseButton& btn) const;
|
||||
ButtonState GetButtonState(const MouseButton& btn) const;
|
||||
void Update(double elapsed) override;
|
||||
DeviceType GetType() const override;
|
||||
void DoButtonPress(const MouseButton& btn);
|
||||
void DoButtonRelease(const MouseButton& btn);
|
||||
void DoMove(const Vector2& pos);
|
||||
void DoScrollWheel(int scroll);
|
||||
protected:
|
||||
void SetButtonPressed(const MouseButton& btn, bool pressed);
|
||||
void SetButtonState(const MouseButton& btn, ButtonState state);
|
||||
protected:
|
||||
MouseState current_state;
|
||||
MouseState previous_state;
|
||||
MouseState history[MaxDeviceStateHistory];
|
||||
public:
|
||||
protected:
|
||||
private:
|
||||
};
|
||||
|
||||
class Touchscreen : public PointerDevice
|
||||
{
|
||||
public:
|
||||
static Event<const TouchEvent&> TouchMoved;
|
||||
static Event<const TouchEvent&> TouchPressed;
|
||||
static Event<const TouchEvent&> TouchReleased;
|
||||
|
||||
static Touchscreen GetCurrentDevice();
|
||||
|
||||
int GetPosition(int touchIndex);
|
||||
int GetTouchCount();
|
||||
int GetMaxTouchCount();
|
||||
std::vector<int> GetTouches();
|
||||
|
||||
/// Returns the pressure of the touch-press. Most touch screens aren't pressure sensitive, in which case the pressure will be 1.
|
||||
float GetPressure(int touchIndex);
|
||||
|
||||
};
|
||||
|
||||
class VRHeadset : public InputDevice { };
|
||||
|
||||
class GenericTVRemote : public InputDevice { };
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@@ -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);
|
||||
|
@@ -4,14 +4,16 @@
|
||||
#include <Event.h>
|
||||
#include <map>
|
||||
#include <thread>
|
||||
#include <rewindow/types/key.h>
|
||||
//#include <rewindow/types/key.h>
|
||||
#include <rewindow/types/cursors.h>
|
||||
#include <rewindow/types/mousebutton.h>
|
||||
#include <rewindow/types/gamepadbutton.h>
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
using precision_clock = std::chrono::high_resolution_clock;
|
||||
using precision_timestamp = precision_clock::time_point;
|
||||
#include <rewindow/types/mouse.h>
|
||||
#include <rewindow/types/WindowEvents.hpp>
|
||||
#include <rewindow/types/keyboard.h>
|
||||
#include <rewindow/types/gamepad.h>
|
||||
#include <J3ML/LinearAlgebra.hpp>
|
||||
#include <format>
|
||||
|
||||
enum class RWindowFlags: uint8_t {
|
||||
IN_FOCUS,
|
||||
@@ -30,162 +32,103 @@ enum class RenderingAPI: uint8_t {
|
||||
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(MouseButton Button) : MouseEvent() {}
|
||||
};
|
||||
|
||||
class MouseButtonUpEvent : public MouseEvent {
|
||||
public:
|
||||
MouseButton Button;
|
||||
};
|
||||
|
||||
class WindowResizeRequestEvent : public RWindowEvent
|
||||
{
|
||||
public:
|
||||
Vector2 Size;
|
||||
};
|
||||
}
|
||||
|
||||
using namespace WindowEvents;
|
||||
|
||||
|
||||
/// General To Do List
|
||||
/// TODO: Implement accurate timekeeping on refresh. Have mechanism to expose to user (hook into their game engine's timestepping)
|
||||
/// TODO: Clean up public API to express the cross-platform, multi-graphics-mode ethos of this project.
|
||||
///
|
||||
|
||||
/// RWindow is a class implementation of a platform-independent window abstraction.
|
||||
/// This library also provides abstractions for user-input devices, and their interaction with the window.
|
||||
class RWindow {
|
||||
public:
|
||||
#pragma region Constructors
|
||||
/// The default constructor sets a default size and window title.
|
||||
RWindow();
|
||||
/// Constructs a window by explicitly setting title, width and height.
|
||||
RWindow(const std::string& title, int width, int height);
|
||||
/// Constructs a window as above with the additional argument of explicitly setting which render API is to be used.
|
||||
RWindow(const std::string& title, int width, int height, RenderingAPI renderer);
|
||||
#pragma endregion
|
||||
|
||||
/// Bindables are provided for hooking into window instances conveniently, without the need to derive and override for simple use cases.
|
||||
#pragma region Bindable Events
|
||||
|
||||
/// We keep and support both mechanisms for extending behavior to suit:
|
||||
/// 1. Derived windows with added functionality.
|
||||
/// 2. Binding functions to a pre-existing window.
|
||||
#pragma region Callbacks
|
||||
/// Bindable Non-intrusive event handlers
|
||||
/// Use these when you can't override the base window class
|
||||
Event<> OnOpenEvent;
|
||||
Event<> OnClosingEvent;
|
||||
Event<RWindowEvent> OnFocusLostEvent;
|
||||
Event<RWindowEvent> OnFocusGainEvent;
|
||||
Event<float> OnRefreshEvent;
|
||||
Event<WindowResizeRequestEvent> OnResizeRequestEvent;
|
||||
Event<KeyDownEvent> OnKeyDownEvent;
|
||||
Event<KeyDownEvent> OnKeyDownEvent;
|
||||
Event<KeyUpEvent> OnKeyUpEvent;
|
||||
Event<MouseMoveEvent> OnMouseMoveEvent;
|
||||
Event<MouseButtonDownEvent> OnMouseButtonDownEvent;
|
||||
Event<MouseButtonUpEvent> OnMouseButtonUpEvent;
|
||||
Event<MouseWheelEvent> OnMouseWheelEvent;
|
||||
|
||||
Event<InputDevice*, InputEvent> OnInputDeviceEvent;
|
||||
#pragma endregion
|
||||
|
||||
/// These methods can also be overridden in derived classes.
|
||||
#pragma region Overrides
|
||||
/// Intrusive virtual methods intended to be overridden in a derived class.
|
||||
/// Do not stuff any logic into these. Someone WILL override it and forget to call the base.
|
||||
|
||||
/// Called upon the window requesting to open.
|
||||
virtual void OnOpen() {}
|
||||
/// Called right before the window closes.
|
||||
virtual void OnClosing() {}
|
||||
/// Called when the window loses focus.
|
||||
virtual void OnFocusLost(const RWindowEvent& e) {}
|
||||
/// Called when the window gains focus.
|
||||
virtual void OnFocusGain(const RWindowEvent& e) {}
|
||||
/// Called when the window is 'refreshed', in other words, a render pass is completed.
|
||||
virtual void OnRefresh(float elapsed) {}
|
||||
/// Called when a resize request has succeeded.
|
||||
virtual void OnResizeSuccess() {}
|
||||
/// Called when a resize request is sent to the operating system.
|
||||
virtual bool OnResizeRequest(const WindowResizeRequestEvent& e) { return true;}
|
||||
virtual void OnKeyDown(const KeyDownEvent&) {}
|
||||
virtual void OnKeyUp(const KeyUpEvent&) {}
|
||||
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);
|
||||
Mouse* GetMouse();
|
||||
Keyboard* GetKeyboard();
|
||||
|
||||
#pragma region Input Helpers
|
||||
/// Returns a Vector2 representing mouse coordinates relative to the top-left corner of the window.
|
||||
/// This result is cached from the operating-system, and as such may be out-of-date.
|
||||
/// @see GetAccurateMouseCoordinates().
|
||||
Vector2 GetMouseCoordinates() const;
|
||||
|
||||
// TODO: Is this part of the API interface? Can it be moved to protected?
|
||||
void liftKey (Key key);
|
||||
int GetMouseWheelPersistent() const { return currentMouse.Wheel;}
|
||||
|
||||
// TODO: Is this part of the API interface? Can it be moved to protected?
|
||||
void pressKey (Key key);
|
||||
#pragma endregion
|
||||
|
||||
#pragma region Cursor Modifiers
|
||||
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();
|
||||
|
||||
#pragma endregion
|
||||
|
||||
/// Sets which rendering API is to be used with this window.
|
||||
void setRenderer(RenderingAPI api);
|
||||
void SetRenderer(RenderingAPI api);
|
||||
|
||||
/// Initializes all state with the window manager and rendering API, then opens the window.
|
||||
/// This function instructs the operating system to create the actual window, and give it to us to control.
|
||||
/// Calling this function, therefore, creates the real 'window' object on the operating system.
|
||||
void Open();
|
||||
|
||||
/// Cleans up and closes the window without destroying the handle.
|
||||
@@ -195,95 +138,226 @@ namespace ReWindow
|
||||
|
||||
void MessageBox(); // TODO: Must be implemented from scratch as a Motif Window in x11
|
||||
|
||||
/// Sends a request to the operating system to have this window somehow demand attention from the user.
|
||||
/// This is implemented per-operating system, and as such no guarantees about behavior can be made.
|
||||
void Flash();
|
||||
|
||||
/// Returns whether the window currently has mouse and/or keyboard focus.
|
||||
bool isFocused() const;
|
||||
bool isFullscreen() const;
|
||||
bool isResizable() const;
|
||||
bool isVsyncEnabled() const;
|
||||
[[nodiscard]] bool IsFocused() const;
|
||||
|
||||
bool isAlive() const; // TODO: Always returns true.
|
||||
/// Returns whether the window is currently in Fullscreen.
|
||||
// TODO: Support Fullscreen, FullscreenWindowed, and Windowed?
|
||||
[[nodiscard]] bool IsFullscreen() const;
|
||||
|
||||
/// Sets whether the mouse is visible when inside the window.
|
||||
void setMouseVisible(bool visible);
|
||||
void setMouseLocked();
|
||||
void setMouseCenter();
|
||||
void restoreMouseFromLastCenter(); // Feels nicer for users
|
||||
/// Returns whether the window can be resized.
|
||||
[[nodiscard]] bool IsResizable() const;
|
||||
|
||||
bool isKeyDown(Key key) const;
|
||||
bool isMouseButtonDown(MouseButton button) const;
|
||||
/// Returns whether V-Sync is enabled.
|
||||
[[nodiscard]] bool IsVsyncEnabled() const;
|
||||
|
||||
void setFullscreen(bool fs);
|
||||
void setResizable(bool resizable);
|
||||
void setVsyncEnabled(bool);
|
||||
void setTitle(const std::string& title);
|
||||
std::string getTitle() const;
|
||||
int getWidth() const; // Honestly no idea if we'll keep these or still go with getSize.
|
||||
int getHeight() const; // getSize wasn't working for me for logging. -maxine
|
||||
/// 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;
|
||||
|
||||
// TODO: Move out of public API, consumers should use setFullscreen()
|
||||
void fullscreen();
|
||||
// TODO: Move out of public API, consumers should use setFullscreen()
|
||||
void restoreFromFullscreen();
|
||||
/// Returns whether the given key is currently being pressed.
|
||||
[[nodiscard]] bool IsKeyDown(Key key) const;
|
||||
|
||||
/// Returns whether the given mouse button is currently being pressed.
|
||||
[[nodiscard]] bool IsMouseButtonDown(const MouseButton &button) const;
|
||||
|
||||
/// 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);
|
||||
|
||||
/// 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);
|
||||
|
||||
/// 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);
|
||||
|
||||
/// Sets the title of this window.
|
||||
void SetTitle(const std::string& title);
|
||||
[[nodiscard]] std::string GetTitle() const;
|
||||
|
||||
/// Returns the horizontal length of this window, in pixels.
|
||||
[[nodiscard]] int GetWidth() const;
|
||||
/// Returns the vertical length of this window, in pixels.
|
||||
[[nodiscard]] int GetHeight() const;
|
||||
|
||||
/// A special-case function to change our internal size variable, without triggering event updates.
|
||||
void SetSizeWithoutEvent(const Vector2& size); //AAAAAHHHHHHHHH WINDOZE MAKING THINGS DIFFICULT :/ - Redacted.
|
||||
|
||||
void SetLastKnownWindowSize(const Vector2& size);
|
||||
|
||||
// TODO: Josh hates parameter-flags, it's not 1995 :/
|
||||
bool getFlag(RWindowFlags flag) const;
|
||||
bool GetFlag(RWindowFlags flag) const;
|
||||
// TODO: Josh hates parameter-flags, it's not 1995 :/
|
||||
void setFlag(RWindowFlags flag, bool state);
|
||||
//void init(RenderingAPI api, const char* title, int width, int height, bool vsync);
|
||||
void SetFlag(RWindowFlags flag, bool state);
|
||||
|
||||
/// Returns the name of the developer of the user's graphics driver, if it can be determined.
|
||||
std::string getGraphicsDriverVendor();
|
||||
|
||||
/// Tells the underlying window manager to destroy this window and drop the handle.
|
||||
/// The window, in theory, can not be re-opened after this.
|
||||
/// TODO: Create a destructor and move to there?
|
||||
/// TODO: What's the semantic difference between this and Close()?
|
||||
void destroyWindow();
|
||||
void DestroyWindow();
|
||||
|
||||
/// Reads events from the underlying window manager.
|
||||
/// TODO: Move out of public API, consumers should call refresh or ideally an update() call.
|
||||
void pollEvents();
|
||||
/// Reads events from the operating system, and processes them accordingly.
|
||||
/// TODO: Move out of public API, consumers should call Refresh or ideally an update() call.
|
||||
void PollEvents();
|
||||
|
||||
/// Updates internal window state, similar to ManagedRefresh, but without accounting for any timekeeping. This is left up to the user.
|
||||
void Refresh();
|
||||
|
||||
/// Updates the window and handles timing internally.
|
||||
void ManagedRefresh();
|
||||
|
||||
/// Requests the operating system to change the window size.
|
||||
/// @param width
|
||||
/// @param height
|
||||
void SetSize(int width, int height);
|
||||
/// Requests the operating system to change the window size.
|
||||
/// @param size
|
||||
void SetSize(const Vector2& size);
|
||||
|
||||
/// Updates the window and handles timing internally.
|
||||
void refresh();
|
||||
void setSize(int width, int height);
|
||||
void setSize(const Vector2& size);
|
||||
/// Returns the position of the window's top-left corner relative to the display
|
||||
Vector2 getPos() const;
|
||||
// I want to know why this is made platform specific. Is that even necessary? -maxine
|
||||
// Because each OS / WM implements it with a different API. - josh
|
||||
// If we stored ourselves a copy (accurately) of the window's size, we could implement it into the shared layer
|
||||
// But this is at BEST, unreliable.
|
||||
Vector2 getSize() const;
|
||||
Vector2 GetPos() const;
|
||||
|
||||
/// Returns the known size of the window, in {x,y} pixel measurement.
|
||||
Vector2 GetSize() const;
|
||||
|
||||
/// Returns the position of the "renderable area" of the window relative to it's top left corner.
|
||||
/// (used to account for the width of the border & title bar).
|
||||
Vector2 GetPositionOfRenderableArea() const;
|
||||
|
||||
/// Requests the operating system to move the window to the specified coordinates on the display.
|
||||
/// @param x The horizontal screen position to place the window at.
|
||||
/// @param y The vertical screen position to place the window at.
|
||||
void SetPos(int x, int y);
|
||||
/// Requests the operating system to move the window to the specified coordinates on the display.
|
||||
/// @param pos A Vector2 representing the x,y coordinates of the desired window destination. Fractional values are ignored.
|
||||
void SetPos(const Vector2& pos);
|
||||
|
||||
void setPos(int x, int y);
|
||||
void setPos(const Vector2& pos);
|
||||
Vector2 getCursorPos() const;
|
||||
|
||||
/// Pull the window to the top, such that it is displayed on top of everything else.
|
||||
/// NOTE: The implementation is window-manager defined, and thus there is no guarantee of it always working.
|
||||
void raise() const;
|
||||
/// Push the window lower, such that it is effectively hidden behind other windows.
|
||||
/// NOTE: The implementation is window-manager defined, and thus there is no guarantee of it always working.
|
||||
void lower() const;
|
||||
void setCursorStyle(CursorStyle style) const;
|
||||
void setCursorCustomIcon() const;
|
||||
/// NOTE: The implementation is defined per-OS, and thus there is no guarantee of it always working.
|
||||
void Raise() const;
|
||||
/// Push the window Lower, such that it is effectively hidden behind other windows.
|
||||
/// NOTE: The implementation is defined per-OS, and thus there is no guarantee of it always working.
|
||||
void Lower() const;
|
||||
|
||||
|
||||
/// 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;
|
||||
private:
|
||||
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;
|
||||
int width;
|
||||
int height;
|
||||
RenderingAPI renderer;
|
||||
bool open = false;
|
||||
bool resizable;
|
||||
static void GLSwapBuffers();
|
||||
|
||||
/// Returns the current time, represented as a high-resolution std::chrono alias.
|
||||
static std::chrono::steady_clock::time_point GetTimestamp();
|
||||
|
||||
/// Computes elapsed time from a start-point and end-point.
|
||||
float ComputeElapsedFrameTimeSeconds(std::chrono::steady_clock::time_point start, std::chrono::steady_clock::time_point end);
|
||||
|
||||
/// Updates internals to account for the latest calculated frame time.
|
||||
void UpdateFrameTiming(float frame_time);
|
||||
|
||||
/// 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;
|
||||
|
||||
/// Returns the approximate frames-per-second using delta time.
|
||||
[[nodiscard]] float GetRefreshRate() const;
|
||||
|
||||
/// Returns the number of frames ran since the windows' creation.
|
||||
[[nodiscard]] float GetRefreshCounter() const;
|
||||
protected:
|
||||
/// Requests the operating system to make the window fullscreen. Saves the previous window size as well.
|
||||
void Fullscreen();
|
||||
|
||||
/// Requests the operating system to take the window out of fullscreen mode. Previously saved window size is restored, if possible.
|
||||
void RestoreFromFullscreen();
|
||||
|
||||
bool DetectsMouse();
|
||||
bool DetectsKeyboard();
|
||||
bool DetectsGamepad();
|
||||
|
||||
protected:
|
||||
#pragma region Data
|
||||
|
||||
std::string title = "Redacted Window";
|
||||
int width = 1280;
|
||||
int height = 720;
|
||||
|
||||
bool fullscreen_mode = false;
|
||||
bool focused = true;
|
||||
bool vsync = false;
|
||||
bool cursor_visible = true;
|
||||
bool open = false;
|
||||
bool resizable = true;
|
||||
|
||||
Vector2 lastKnownWindowSize {0, 0};
|
||||
bool flags[5];
|
||||
std::vector<RWindowEvent> eventLog;
|
||||
|
||||
/// Input Devices;
|
||||
Mouse* mouse;
|
||||
Keyboard* keyboard;
|
||||
Gamepad* gamepad;
|
||||
|
||||
std::vector<InputDevice*> input_devices;
|
||||
|
||||
|
||||
RenderingAPI renderer = RenderingAPI::OPENGL;
|
||||
|
||||
//Vector2 mouse_coords = {0, 0};
|
||||
//Vector2 last_mouse_coords = {0, 0};
|
||||
|
||||
|
||||
float delta_time = 0.f;
|
||||
float refresh_rate = 0.f;
|
||||
unsigned int refresh_count = 0;
|
||||
|
||||
// TODO: Implement ringbuffer / circular vector class of some sort.
|
||||
float refresh_rate_prev_1 = 0.f;
|
||||
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;
|
||||
#pragma endregion
|
||||
#pragma region Internals
|
||||
/// Returns the most accurate and recent available mouse coordinates.
|
||||
/// @note Call this version at most **once** per-frame. It polls the X-Window server and therefore is quite slow.
|
||||
/// @see getCursorPos();
|
||||
Vector2 GetAccurateMouseCoordinates() const;
|
||||
|
||||
public:
|
||||
/// These unfortunately *have* to be public because of the poor design of the windows event loop.
|
||||
#pragma region Event Callers
|
||||
/// 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(const MouseButton& btn);
|
||||
/// Executes event handlers for mouse release events.
|
||||
void processMouseRelease(const 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);
|
||||
#pragma endregion
|
||||
private:
|
||||
#pragma endregion
|
||||
};
|
||||
}
|
143
main.cpp
143
main.cpp
@@ -1,8 +1,14 @@
|
||||
#include <iostream>
|
||||
#include <rewindow/types/window.h>
|
||||
#include <jlog/jlog.hpp>
|
||||
#include <GL/gl.h>
|
||||
#include <rewindow/types/display.h>
|
||||
#include <rewindow/logger/logger.h>
|
||||
|
||||
//aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa Windows :/
|
||||
#if _WIN32
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#include <GL/gl.h>
|
||||
|
||||
Vector2 mouse_pos;
|
||||
|
||||
@@ -22,55 +28,141 @@ class MyWindow : public ReWindow::RWindow {
|
||||
void OnRefresh(float elapsed) override {
|
||||
glClearColor(255, 0, 0, 255);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glSwapBuffers();
|
||||
auto pos = getCursorPos();
|
||||
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;
|
||||
|
||||
RWindow::OnRefresh(elapsed);
|
||||
}
|
||||
|
||||
bool OnResizeRequest(const ReWindow::WindowResizeRequestEvent& e) override {
|
||||
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);
|
||||
DEBUG(std::format("New window '{}' created. width={} height={}", window->getTitle(), window->getWidth(), window->getHeight()));
|
||||
jlog::Debug(std::format("New window '{}' created. width={} height={}", window->GetTitle(), window->GetWidth(),
|
||||
window->GetHeight()));
|
||||
|
||||
window->setRenderer(RenderingAPI::OPENGL);
|
||||
DEBUG(std::format("Rendering API OPENGL set for window '{}'", window->getTitle()));
|
||||
window->SetRenderer(RenderingAPI::OPENGL);
|
||||
jlog::Debug(std::format("Rendering API OPENGL set for window '{}'", window->GetTitle()));
|
||||
|
||||
window->Open();
|
||||
DEBUG(std::format("Opened window '{}'", window->getTitle()));
|
||||
jlog::Debug(std::format("Opened window '{}'", window->GetTitle()));
|
||||
|
||||
// TODO: Cannot set flags until after window is open
|
||||
// Make this work somehow
|
||||
DEBUG("TODO: Cannot set flags until after window is open")
|
||||
window->setFullscreen(false);
|
||||
window->setVsyncEnabled(false);
|
||||
window->setResizable(true);
|
||||
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)));
|
||||
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) {
|
||||
|
||||
auto w = display.getDefaultGraphicsMode().getWidth();
|
||||
auto h = display.getDefaultGraphicsMode().getHeight();
|
||||
auto r = display.getDefaultGraphicsMode().getRefreshRate();
|
||||
|
||||
auto aspect = (float)w/(float)h;
|
||||
|
||||
std::cout << "Default: ";
|
||||
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();
|
||||
r = fgm.getRefreshRate();
|
||||
|
||||
auto aspect = (float)w/(float)h;
|
||||
|
||||
std::cout << std::format("Mode ({},{}) @ {}hz Ratio {}", w, h, r, aspect) << std::endl;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
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->OnKeyDownEvent += [&] (ReWindow::KeyDownEvent e) {
|
||||
DEBUG(e.key.CharCode);
|
||||
jlog::Debug(e.key.Mnemonic);
|
||||
};
|
||||
|
||||
|
||||
window->OnMouseButtonDownEvent += [&] (ReWindow::MouseButtonDownEvent e) {
|
||||
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->IsAlive()) {
|
||||
window->PollEvents();
|
||||
window->ManagedRefresh();
|
||||
}
|
||||
// Alternatively:
|
||||
/*
|
||||
* while (window->IsAlive()) {
|
||||
* auto start = window->GetTimestamp();
|
||||
* ourUpdateLogic(window->GetDeltaTime());
|
||||
* window->update();
|
||||
* ourDrawLogic();
|
||||
* window->Refresh();
|
||||
* auto end = window->GetTimestamp();
|
||||
* auto dt = window->ComputeElapsedFrameTimeSeconds(start, end);
|
||||
* window->UpdateFrameTiming(dt);
|
||||
* }
|
||||
*
|
||||
*/
|
||||
}
|
||||
|
||||
/*
|
||||
//Windows :(
|
||||
#ifdef _WIN32
|
||||
#ifndef UNICODE
|
||||
@@ -82,4 +174,5 @@ extern "C" {
|
||||
return main();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
*/
|
7
src/logger/logger.cpp
Normal file
7
src/logger/logger.cpp
Normal file
@@ -0,0 +1,7 @@
|
||||
#include "rewindow/logger/logger.h"
|
||||
|
||||
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};
|
||||
}
|
74
src/platform/linux/display.cpp
Normal file
74
src/platform/linux/display.cpp
Normal file
@@ -0,0 +1,74 @@
|
||||
#include <rewindow/types/display.h>
|
||||
#include <X11/X.h>
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/extensions/Xrandr.h>
|
||||
#include <stdexcept>
|
||||
|
||||
using namespace ReWindow;
|
||||
|
||||
std::vector<ReWindow::Display> ReWindow::Display::getDisplaysFromWindowManager() {
|
||||
std::vector<ReWindow::Display> result;
|
||||
_XDisplay* display = XOpenDisplay(nullptr);
|
||||
|
||||
if (!display)
|
||||
throw std::runtime_error("There isn't an X11 root display????");
|
||||
|
||||
Window root_window = DefaultRootWindow(display);
|
||||
XRRScreenResources* screen_resources = XRRGetScreenResources(display, root_window);
|
||||
|
||||
if (!screen_resources)
|
||||
throw std::runtime_error("Failed to get screen resources.");
|
||||
|
||||
for (int i = 0; i < screen_resources->noutput; i++) {
|
||||
RROutput output = screen_resources->outputs[i];
|
||||
XRROutputInfo* output_info = XRRGetOutputInfo(display, screen_resources, output);
|
||||
|
||||
if (output_info->connection == RR_Connected) {
|
||||
ReWindow::Display d;
|
||||
XRRCrtcInfo* crtc_info = XRRGetCrtcInfo(display, screen_resources, output_info->crtc);
|
||||
|
||||
if (!crtc_info)
|
||||
throw std::runtime_error("Failed to get CRT info.");
|
||||
|
||||
u32 default_width = (u32) crtc_info->width;
|
||||
u32 default_height = (u32) crtc_info->height;
|
||||
short default_refresh_rate = 0;
|
||||
|
||||
for (int k = 0; k < screen_resources->nmode; k++) {
|
||||
if (screen_resources->modes[k].id == crtc_info->mode) {
|
||||
XRRModeInfo mode_info = screen_resources->modes[k];
|
||||
double refresh_rate;
|
||||
|
||||
if (mode_info.hTotal > 0 && mode_info.vTotal > 0)
|
||||
refresh_rate = mode_info.dotClock / (double)(mode_info.hTotal * mode_info.vTotal),
|
||||
default_refresh_rate = (short)refresh_rate;
|
||||
|
||||
d.default_graphics_mode = {default_width, default_height, default_refresh_rate};
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (int j = 0; j < output_info->nmode; j++) {
|
||||
RRMode mode = output_info->modes[j];
|
||||
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
|
||||
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);
|
||||
|
||||
d.graphics_modes.emplace_back((u32) mode_info.width,(u32) mode_info.height,(short) refresh_rate);
|
||||
}
|
||||
}
|
||||
}
|
||||
XRRFreeCrtcInfo(crtc_info);
|
||||
result.push_back(d);
|
||||
}
|
||||
XRRFreeOutputInfo(output_info);
|
||||
}
|
||||
|
||||
XRRFreeScreenResources(screen_resources);
|
||||
XCloseDisplay(display);
|
||||
return result;
|
||||
}
|
@@ -4,11 +4,10 @@
|
||||
#include <GL/glx.h>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <thread>
|
||||
#include <rewindow/types/window.h>
|
||||
#include <rewindow/types/cursors.h>
|
||||
#include <J3ML/J3ML.h>
|
||||
#include <jlog/jlog.hpp>
|
||||
#include <J3ML/J3ML.hpp>
|
||||
#include <rewindow/logger/logger.h>
|
||||
|
||||
|
||||
|
||||
@@ -30,229 +29,274 @@ Atom windowTypeAtom;
|
||||
Atom windowTypeUtilityAtom;
|
||||
XSizeHints hints;
|
||||
GLXContext glContext;
|
||||
Cursor invisible_cursor = 0;
|
||||
|
||||
Vector2 render_area_position = {0, 0};
|
||||
Vector2 position = {0, 0};
|
||||
bool should_poll_x_for_mouse_pos = true;
|
||||
|
||||
|
||||
using namespace ReWindow;
|
||||
|
||||
void RWindow::raise() const {
|
||||
DEBUG(std::format("Raising window '{}'", this->title));
|
||||
void RWindow::Raise() const {
|
||||
Logger::Debug(std::format("Raising window '{}'", this->title));
|
||||
|
||||
// Get the position of the renderable area relative to the rest of the window.
|
||||
XGetWindowAttributes(display, window, &windowAttributes);
|
||||
render_area_position = { (float) windowAttributes.x, (float) windowAttributes.y };
|
||||
|
||||
XRaiseWindow(display, window);
|
||||
}
|
||||
|
||||
void RWindow::lower() const
|
||||
void RWindow::Lower() const
|
||||
{
|
||||
DEBUG(std::format("Lowering window '{}'", this->title));
|
||||
Logger::Debug(std::format("Lowering window '{}'", this->title));
|
||||
XLowerWindow(display, window);
|
||||
}
|
||||
|
||||
void RWindow::destroyWindow() {
|
||||
DEBUG(std::format("Destroying window '{}'", this->title));
|
||||
void RWindow::DestroyWindow() {
|
||||
Logger::Debug(std::format("Destroying window '{}'", this->title));
|
||||
XDestroySubwindows(display, window);
|
||||
DEBUG(std::format("Destroyed window '{}'", this->title));
|
||||
Logger::Debug(std::format("Destroyed window '{}'", this->title));
|
||||
XDestroyWindow(display, window);
|
||||
delete this;
|
||||
}
|
||||
|
||||
void RWindow::refresh() {
|
||||
// TODO: Implement refresh time keeping
|
||||
OnRefresh(0.f);
|
||||
//void RWindow::
|
||||
|
||||
// TODO: Check if mouse coords have changed, only then fire OnMouseMove event
|
||||
Vector2 mouse_coords = getCursorPos();
|
||||
void RWindow::SetCursorVisible(bool cursor_enable) {
|
||||
cursor_visible = cursor_enable;
|
||||
if (invisible_cursor == 0) {
|
||||
Pixmap blank_pixmap = XCreatePixmap(display, 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);
|
||||
}
|
||||
|
||||
auto eventData = MouseMoveEvent(mouse_coords);
|
||||
OnMouseMove(eventData);
|
||||
OnMouseMoveEvent(eventData);
|
||||
//std::this_thread::sleep_for(1ms);
|
||||
if (!cursor_enable)
|
||||
XDefineCursor(display, window, invisible_cursor);
|
||||
|
||||
if (cursor_enable)
|
||||
XUndefineCursor(display, window);
|
||||
}
|
||||
|
||||
void RWindow::setFlag(RWindowFlags flag, bool state) {
|
||||
void RWindow::SetResizable(bool sizable) {
|
||||
XGetWindowAttributes(display,window,&windowAttributes);
|
||||
flags[(int) flag] = state;
|
||||
//Once you've done this you cannot make it resizable again.
|
||||
if (flag == RWindowFlags::RESIZABLE && !state) {
|
||||
DEBUG("Once you've done this you cannot make it resizable again.");
|
||||
|
||||
|
||||
this->resizable = sizable;
|
||||
if (!sizable)
|
||||
{
|
||||
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);
|
||||
}
|
||||
DEBUG(std::format("Set flag '{}' to state '{}' for window '{}'", RWindowFlagToStr(flag), state, this->title));
|
||||
//this->SetFlag(RWindowFlags::RESIZABLE, resizable);
|
||||
|
||||
}
|
||||
|
||||
void RWindow::pollEvents() {
|
||||
void RWindow::SetFlag(RWindowFlags flag, bool state) {
|
||||
XGetWindowAttributes(display,window,&windowAttributes);
|
||||
flags[(int) flag] = state;
|
||||
//Once you've done this you cannot make it resizable again.
|
||||
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);
|
||||
}
|
||||
Logger::Debug(std::format("Set flag '{}' to state '{}' for window '{}'", RWindowFlagToStr(flag), state, this->title));
|
||||
}
|
||||
|
||||
void RWindow::PollEvents() {
|
||||
while(XPending(display)) {
|
||||
XNextEvent(display, &xev);
|
||||
|
||||
if (xev.type == ClientMessage)
|
||||
DEBUG(std::format("Recieved event '{}'", "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) {
|
||||
destroyWindow();
|
||||
|
||||
processOnClose();
|
||||
DestroyWindow();
|
||||
system("xset r on");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (xev.type == FocusIn) {
|
||||
DEBUG(std::format("Recieved event '{}'", "FocusIn"));
|
||||
Logger::Debug(std::format("Recieved event '{}'", "FocusIn"));
|
||||
XAutoRepeatOff(display);
|
||||
//focusGained.Invoke();
|
||||
RWindowEvent event {};
|
||||
OnFocusGain(event);
|
||||
OnFocusGainEvent(event);
|
||||
setFlag(RWindowFlags::IN_FOCUS, true);
|
||||
SetFlag(RWindowFlags::IN_FOCUS, true);
|
||||
|
||||
if (!cursor_visible)
|
||||
XDefineCursor(display, window, 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 };
|
||||
processFocusIn();
|
||||
}
|
||||
|
||||
if (xev.type == FocusOut) {
|
||||
DEBUG(std::format("Recieved event '{}'", "FocusOut"));
|
||||
Logger::Debug(std::format("Recieved event '{}'", "FocusOut"));
|
||||
XAutoRepeatOn(display);
|
||||
//focusLost.Invoke();
|
||||
RWindowEvent event {};
|
||||
OnFocusLost(event);
|
||||
OnFocusLostEvent(event);
|
||||
setFlag(RWindowFlags::IN_FOCUS, false);
|
||||
|
||||
SetFlag(RWindowFlags::IN_FOCUS, false);
|
||||
if (!cursor_visible)
|
||||
XUndefineCursor(display, window);
|
||||
|
||||
processFocusOut();
|
||||
}
|
||||
|
||||
if (xev.type == KeyRelease) {
|
||||
DEBUG(std::format("Recieved event '{}'", "KeyRelease"));
|
||||
Logger::Debug(std::format("Recieved event '{}'", "KeyRelease"));
|
||||
auto scancode = (X11Scancode) xev.xkey.keycode;
|
||||
auto key = GetKeyFromX11Scancode(scancode);
|
||||
OnKeyUpEvent(key);
|
||||
OnKeyUp(key);
|
||||
liftKey(key);
|
||||
processKeyRelease(key);
|
||||
}
|
||||
|
||||
if (xev.type == KeyPress) {
|
||||
DEBUG(std::format("Recieved event '{}'", "KeyPress"));
|
||||
Logger::Debug(std::format("Recieved event '{}'", "KeyPress"));
|
||||
auto scancode = (X11Scancode) xev.xkey.keycode;
|
||||
auto key = GetKeyFromX11Scancode(scancode);
|
||||
OnKeyDownEvent(key);
|
||||
OnKeyDown(key);
|
||||
pressKey(key);
|
||||
//eventLog.push_back(eventData);
|
||||
processKeyPress(key);
|
||||
}
|
||||
|
||||
if (xev.type == ButtonRelease) {
|
||||
DEBUG(std::format("Recieved event '{}'", "ButtonRelease"));
|
||||
MouseButton button = GetMouseButtonFromXButton(xev.xbutton.button);
|
||||
auto eventData = MouseButtonUpEvent();
|
||||
eventData.Button = button;
|
||||
|
||||
OnMouseButtonUpEvent(eventData);
|
||||
OnMouseButtonUp(eventData);
|
||||
// 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 (xev.xbutton.button != 4 && xev.xbutton.button != 5) {
|
||||
MouseButton button = GetMouseButtonFromXButton(xev.xbutton.button);
|
||||
Logger::Debug(std::format("Recieved event '{}'", "ButtonRelease"));
|
||||
processMouseRelease(button);
|
||||
}
|
||||
}
|
||||
|
||||
if (xev.type == ButtonPress) {
|
||||
DEBUG(std::format("Recieved event '{}'", "ButtonPress"));
|
||||
MouseButton button = GetMouseButtonFromXButton(xev.xbutton.button);
|
||||
auto eventData = MouseButtonDownEvent(button);
|
||||
eventData.Button = button;
|
||||
|
||||
OnMouseButtonDownEvent(eventData);
|
||||
OnMouseButtonDown(eventData);
|
||||
// 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 (xev.xbutton.button == 4) {
|
||||
processMouseWheel(-1);
|
||||
} else if (xev.xbutton.button == 5) {
|
||||
processMouseWheel(1);
|
||||
} else
|
||||
{
|
||||
MouseButton button = GetMouseButtonFromXButton(xev.xbutton.button);
|
||||
|
||||
Logger::Debug(std::format("Window event: MouseButtonPress {}", button.Mnemonic));
|
||||
|
||||
processMousePress(button);
|
||||
}
|
||||
}
|
||||
|
||||
if (xev.type == Expose)
|
||||
{
|
||||
DEBUG(std::format("Recieved event '{}'", "Expose"));
|
||||
Logger::Debug(std::format("Recieved event '{}'", "Expose"));
|
||||
}
|
||||
|
||||
// NOTE: This event is functionally useless, as it only informs of the very beginning and end of a mouse movement.
|
||||
if (xev.type == MotionNotify)
|
||||
{
|
||||
DEBUG(std::format("Recieved event '{}'", "MotionNotify"));
|
||||
Logger::Debug(std::format("Recieved event '{}'", "MotionNotify"));
|
||||
}
|
||||
|
||||
if (xev.type == ResizeRequest) {
|
||||
DEBUG(std::format("Recieved event '{}'", "ResizeRequest"));
|
||||
this->width = xev.xresizerequest.width;
|
||||
this->height = xev.xresizerequest.height;
|
||||
auto eventData = WindowResizeRequestEvent();
|
||||
eventData.Size = {(float)xev.xresizerequest.width, (float)xev.xresizerequest.height};
|
||||
lastKnownWindowSize = eventData.Size;
|
||||
OnResizeRequest(eventData);
|
||||
OnResizeRequestEvent(eventData);
|
||||
glViewport(0, 0, (GLsizei)xev.xresizerequest.width, (GLsizei)xev.xresizerequest.height);
|
||||
if (xev.type == ConfigureNotify) {
|
||||
if (this->width != xev.xconfigurerequest.width || this->height != xev.xconfigurerequest.height) {
|
||||
Logger::Debug(std::format("Recieved event '{}'", "ResizeRequest"));
|
||||
|
||||
this->width = xev.xconfigurerequest.width;
|
||||
this->height = xev.xconfigurerequest.height;
|
||||
|
||||
auto eventData = WindowResizeRequestEvent();
|
||||
eventData.Size = { (float) xev.xconfigurerequest.width, (float) xev.xconfigurerequest.height };
|
||||
lastKnownWindowSize = eventData.Size;
|
||||
|
||||
OnResizeRequest(eventData);
|
||||
OnResizeRequestEvent(eventData);
|
||||
}
|
||||
|
||||
//Window Moved.
|
||||
if (position.x != xev.xconfigurerequest.x || position.y != xev.xconfigurerequest.y)
|
||||
position = { (float) xev.xconfigurerequest.x, (float) xev.xconfigurerequest.y };
|
||||
}
|
||||
|
||||
}
|
||||
previousKeyboard = currentKeyboard;
|
||||
previousMouse.Buttons = currentMouse.Buttons;
|
||||
}
|
||||
|
||||
|
||||
// Might make the window go off the screen on some window managers.
|
||||
void RWindow::setSize(int newWidth, int newHeight) {
|
||||
if (!getFlag(RWindowFlags::RESIZABLE)) return;
|
||||
void RWindow::SetSize(int newWidth, int newHeight) {
|
||||
if (!resizable) return;
|
||||
|
||||
this->width = newWidth;
|
||||
this->height = newHeight;
|
||||
XResizeWindow(display, window, newWidth, newHeight);
|
||||
XFlush(display);
|
||||
glViewport(0, 0, width, height);
|
||||
DEBUG(std::format("Set size for window '{}'. width={} height={}", this->title, newWidth, newHeight));
|
||||
Logger::Debug(std::format("Set size for window '{}'. width={} height={}", this->title, newWidth, newHeight));
|
||||
}
|
||||
|
||||
Vector2 RWindow::getCursorPos() const {
|
||||
Window root = XDefaultRootWindow(display);
|
||||
Window root_return;
|
||||
Window child_return;
|
||||
int root_x_ret;
|
||||
int root_y_ret;
|
||||
int win_x_ret;
|
||||
int win_y_ret;
|
||||
Vector2 RWindow::GetAccurateMouseCoordinates() const {
|
||||
|
||||
Window root_return, child_return;
|
||||
int root_x_ret, root_y_ret;
|
||||
int win_x_ret, win_y_ret;
|
||||
uint32_t mask_return;
|
||||
unsigned m;
|
||||
|
||||
// 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);
|
||||
|
||||
if (mouseAvailable) {
|
||||
// TODO: process retrieved mouse coordinates
|
||||
// TODO: normalize coordinates from displaySpace to windowSpace
|
||||
// TODO: fire mouse movement event
|
||||
//std::cout << win_x_ret << ", " << win_y_ret << std::endl;
|
||||
// TODO: Compensate for height of window TitleBar + window border width
|
||||
float window_border_width = 2;
|
||||
float window_titlebar_height = 18;
|
||||
Vector2 mouse_coords_raw = {(float)win_x_ret+window_border_width, (float)win_y_ret+window_titlebar_height};
|
||||
auto window_pos = getPos();
|
||||
return mouse_coords_raw - window_pos;
|
||||
Vector2 m_coords = { (float) win_x_ret, (float) win_y_ret };
|
||||
return m_coords;
|
||||
}
|
||||
return Vector2::Zero;
|
||||
}
|
||||
|
||||
// TODO: implement integer vector2/3 types
|
||||
Vector2 RWindow::getSize() const {
|
||||
XGetWindowAttributes(display,window,&windowAttributes);
|
||||
return {(float)windowAttributes.width, (float)windowAttributes.height};
|
||||
|
||||
Vector2 RWindow::GetSize() const {
|
||||
return {(float) this->width, (float) this->height};
|
||||
}
|
||||
|
||||
Vector2 RWindow::getLastKnownResize() const
|
||||
{
|
||||
return lastKnownWindowSize;
|
||||
}
|
||||
//Vector2 RWindow::getLastKnownResize() const
|
||||
//{
|
||||
// return lastKnownWindowSize;
|
||||
//}
|
||||
|
||||
// TODO: implement integer vector2/3 types
|
||||
Vector2 RWindow::getPos() const {
|
||||
XGetWindowAttributes(display,window,&windowAttributes);
|
||||
return {(float)windowAttributes.x, (float)windowAttributes.y};
|
||||
Vector2 RWindow::GetPos() const {
|
||||
return position;
|
||||
}
|
||||
|
||||
void RWindow::setPos(int x, int y) {
|
||||
void RWindow::SetPos(int x, int y) {
|
||||
XMoveWindow(display, window, x, y);
|
||||
position = { (float) x, (float) y };
|
||||
}
|
||||
|
||||
void RWindow::setPos(const Vector2& pos) {
|
||||
this->setPos(pos.x, pos.y);
|
||||
void RWindow::SetPos(const Vector2& pos) {
|
||||
SetPos(pos.x, pos.y);
|
||||
}
|
||||
|
||||
|
||||
void RWindow::glSwapBuffers() {
|
||||
void RWindow::GLSwapBuffers() {
|
||||
glXSwapBuffers(display,window);
|
||||
}
|
||||
|
||||
bool RWindow::isResizable() const {
|
||||
return this->getFlag(RWindowFlags::RESIZABLE);
|
||||
}
|
||||
|
||||
void RWindow::fullscreen() {
|
||||
DEBUG(std::format("Fullscreening window '{}'", this->title));
|
||||
fullscreenmode = true;
|
||||
|
||||
void RWindow::Fullscreen() {
|
||||
Logger::Debug(std::format("Fullscreening window '{}'", this->title));
|
||||
fullscreen_mode = true;
|
||||
|
||||
XEvent xev;
|
||||
Atom wm_state = XInternAtom(display, "_NET_WM_STATE", true);
|
||||
@@ -265,15 +309,15 @@ void RWindow::fullscreen() {
|
||||
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);
|
||||
DEBUG(std::format("Fullscreened window '{}'", this->title));
|
||||
Logger::Debug(std::format("Fullscreened window '{}'", this->title));
|
||||
}
|
||||
|
||||
void RWindow::restoreFromFullscreen() {
|
||||
DEBUG(std::format("Restoring window '{}' from fullscreen", this->title));
|
||||
fullscreenmode = false;
|
||||
void RWindow::RestoreFromFullscreen() {
|
||||
Logger::Debug(std::format("Restoring window '{}' 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);
|
||||
@@ -283,23 +327,20 @@ void RWindow::restoreFromFullscreen() {
|
||||
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);
|
||||
DEBUG(std::format("Restored window '{}' from fullscreen", this->title));
|
||||
Logger::Debug(std::format("Restored window '{}' from Fullscreen", this->title));
|
||||
}
|
||||
|
||||
void RWindow::setVsyncEnabled(bool b) {
|
||||
void RWindow::SetVsyncEnabled(bool b) {
|
||||
PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXT;
|
||||
glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddressARB((const GLubyte*)"glXSwapIntervalEXT");
|
||||
glXSwapIntervalEXT(display, window, b);
|
||||
}
|
||||
|
||||
bool RWindow::isFullscreen() const {
|
||||
return fullscreenmode;
|
||||
}
|
||||
|
||||
void RWindow::setCursorStyle(CursorStyle style) const {
|
||||
void RWindow::SetCursorStyle(CursorStyle style) const {
|
||||
u32 x11_cursor_resolved_enum = static_cast<u32>(style.X11Cursor);
|
||||
Cursor c = XCreateFontCursor(display, x11_cursor_resolved_enum);
|
||||
XDefineCursor(display, window, c);
|
||||
@@ -310,7 +351,7 @@ void RWindow::Open() {
|
||||
xSetWindowAttributes.background_pixel = BlackPixel(display, defaultScreen);
|
||||
xSetWindowAttributes.override_redirect = True;
|
||||
xSetWindowAttributes.event_mask = ExposureMask;
|
||||
//setVsyncEnabled(vsync);
|
||||
//SetVsyncEnabled(vsync);
|
||||
if (renderer == RenderingAPI::OPENGL) {
|
||||
GLint glAttributes[] = {GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None};
|
||||
visual = glXChooseVisual(display, defaultScreen, glAttributes);
|
||||
@@ -332,7 +373,7 @@ void RWindow::Open() {
|
||||
ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
|
||||
PointerMotionMask |
|
||||
PointerMotionHintMask | FocusChangeMask | StructureNotifyMask | SubstructureRedirectMask |
|
||||
SubstructureNotifyMask | CWColormap | ResizeRequest);
|
||||
SubstructureNotifyMask | CWColormap );
|
||||
XMapWindow(display, window);
|
||||
XStoreName(display, window, title.c_str());
|
||||
wmDeleteWindow = XInternAtom(display, "WM_DELETE_WINDOW", False);
|
||||
@@ -340,20 +381,30 @@ void RWindow::Open() {
|
||||
|
||||
if (renderer == RenderingAPI::OPENGL)
|
||||
glXMakeCurrent(display, window, 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;
|
||||
|
||||
|
||||
processOnOpen();
|
||||
}
|
||||
|
||||
void RWindow::setTitle(const std::string &title) {
|
||||
void RWindow::SetTitle(const std::string &title) {
|
||||
this->title = title;
|
||||
XStoreName(display, window, title.c_str());
|
||||
}
|
||||
|
||||
// TODO: Implement MouseButton map
|
||||
bool RWindow::isMouseButtonDown(MouseButton button) const {
|
||||
return false;
|
||||
|
||||
|
||||
Vector2 RWindow::GetPositionOfRenderableArea() const {
|
||||
return render_area_position;
|
||||
}
|
||||
|
||||
|
||||
std::string RWindow::getGraphicsDriverVendor() {
|
||||
return std::string(reinterpret_cast<const char*>(glGetString(GL_VENDOR)));
|
||||
}
|
||||
|
||||
|
||||
// TODO: Implement ControllerButton map
|
||||
|
34
src/platform/shared/display.cpp
Normal file
34
src/platform/shared/display.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#include <rewindow/types/display.h>
|
||||
|
||||
using namespace ReWindow;
|
||||
|
||||
FullscreenGraphicsMode::FullscreenGraphicsMode(u32 width, u32 height, short refresh_rate) {
|
||||
this->width = width;
|
||||
this->height = height;
|
||||
this->refresh_rate = refresh_rate;
|
||||
}
|
||||
|
||||
u32 FullscreenGraphicsMode::getWidth() const {
|
||||
return width;
|
||||
}
|
||||
|
||||
u32 FullscreenGraphicsMode::getHeight() const {
|
||||
return height;
|
||||
}
|
||||
|
||||
float FullscreenGraphicsMode::getRefreshRate() const {
|
||||
return refresh_rate;
|
||||
}
|
||||
|
||||
FullscreenGraphicsMode Display::getDefaultGraphicsMode() {
|
||||
return default_graphics_mode;
|
||||
}
|
||||
|
||||
std::vector<FullscreenGraphicsMode> Display::getGraphicsModes() {
|
||||
return graphics_modes;
|
||||
}
|
||||
|
||||
Display::Display(const FullscreenGraphicsMode& default_graphics_mode, const std::vector<FullscreenGraphicsMode>& graphics_modes) {
|
||||
this->default_graphics_mode = default_graphics_mode;
|
||||
this->graphics_modes = graphics_modes;
|
||||
}
|
@@ -1,5 +1,7 @@
|
||||
#include <rewindow/types/window.h>
|
||||
#include <jlog/jlog.hpp>
|
||||
#include <rewindow/types/window.h>
|
||||
#include "rewindow/logger/logger.h"
|
||||
#include <rewindow/types/mouse.h>
|
||||
|
||||
std::string RWindowFlagToStr(RWindowFlags flag) {
|
||||
switch (flag) {
|
||||
case RWindowFlags::IN_FOCUS: return "IN_FOCUS";
|
||||
@@ -9,17 +11,41 @@ std::string RWindowFlagToStr(RWindowFlags flag) {
|
||||
case RWindowFlags::QUIT: return "QUIT";
|
||||
case RWindowFlags::MAX_FLAG: return "MAX_FLAG";
|
||||
default:
|
||||
FATAL("")
|
||||
return "unimplemented flag";
|
||||
}
|
||||
};
|
||||
|
||||
using namespace ReWindow;
|
||||
|
||||
|
||||
|
||||
RWindow::RWindow() {
|
||||
title = "ReWindow Application";
|
||||
width = 640;
|
||||
height = 480;
|
||||
|
||||
if (DetectsKeyboard())
|
||||
{
|
||||
keyboard = new Keyboard();
|
||||
input_devices.push_back(keyboard);
|
||||
}
|
||||
|
||||
if (DetectsMouse())
|
||||
{
|
||||
mouse = new Mouse();
|
||||
input_devices.push_back(mouse);
|
||||
}
|
||||
|
||||
if (DetectsGamepad())
|
||||
{
|
||||
gamepad = new Gamepad();
|
||||
input_devices.push_back(gamepad);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//RWindow::singleton = this;
|
||||
}
|
||||
|
||||
@@ -34,81 +60,234 @@ RWindow::RWindow(const std::string& title, int width, int height, RenderingAPI r
|
||||
title(title), width(width), height(height), renderer(renderer) {}
|
||||
|
||||
Vector2 RWindow::GetMouseCoordinates() const {
|
||||
return getCursorPos();
|
||||
return mouse->GetPosition();
|
||||
}
|
||||
|
||||
bool RWindow::getFlag(RWindowFlags flag) const {return flags[(int)flag];}
|
||||
bool RWindow::GetFlag(RWindowFlags flag) const {
|
||||
return flags[(int) flag];
|
||||
}
|
||||
|
||||
bool RWindow::isAlive() const {
|
||||
bool RWindow::IsAlive() const {
|
||||
return true;
|
||||
}
|
||||
|
||||
void RWindow::setResizable(bool resizable) {
|
||||
this->resizable = resizable;
|
||||
this->setFlag(RWindowFlags::RESIZABLE, resizable);
|
||||
}
|
||||
|
||||
void RWindow::setFullscreen(bool fs) {
|
||||
void RWindow::SetFullscreen(bool fs) {
|
||||
if (fs)
|
||||
fullscreen();
|
||||
Fullscreen();
|
||||
else
|
||||
restoreFromFullscreen();
|
||||
RestoreFromFullscreen();
|
||||
}
|
||||
|
||||
void RWindow::liftKey(Key key) {
|
||||
void RWindow::processFocusIn()
|
||||
{
|
||||
RWindowEvent event {};
|
||||
OnFocusGain(event);
|
||||
OnFocusGainEvent(event);
|
||||
}
|
||||
|
||||
void RWindow::processFocusOut()
|
||||
{
|
||||
RWindowEvent event {};
|
||||
OnFocusLost(event);
|
||||
OnFocusLostEvent(event);
|
||||
}
|
||||
|
||||
void RWindow::processMousePress(const MouseButton& btn)
|
||||
{
|
||||
mouse->DoButtonPress(btn); // .Set(btn, true);
|
||||
auto eventData = MouseButtonDownEvent(btn);
|
||||
OnMouseButtonDown(eventData);
|
||||
OnMouseButtonDownEvent(eventData);
|
||||
|
||||
}
|
||||
|
||||
void RWindow::processMouseMove(Vector2 last_pos, Vector2 new_pos)
|
||||
{
|
||||
currentMouse.Position = new_pos;
|
||||
auto eventData = MouseMoveEvent(new_pos);
|
||||
OnMouseMove(eventData);
|
||||
OnMouseMoveEvent(eventData);
|
||||
}
|
||||
|
||||
void RWindow::processMouseRelease(const MouseButton& btn)
|
||||
{
|
||||
currentMouse.Set(btn, false);
|
||||
auto eventData = MouseButtonUpEvent(btn);
|
||||
OnMouseButtonUp(eventData);
|
||||
OnMouseButtonUpEvent(eventData);
|
||||
|
||||
}
|
||||
|
||||
void RWindow::processKeyRelease(Key key) {
|
||||
currentKeyboard.PressedKeys[key] = false;
|
||||
auto event = ReWindow::WindowEvents::KeyUpEvent(key);
|
||||
auto event = KeyUpEvent(key);
|
||||
OnKeyUp(event);
|
||||
OnKeyUpEvent(key);
|
||||
}
|
||||
|
||||
|
||||
std::string RWindow::getTitle() const {
|
||||
std::string RWindow::GetTitle() const {
|
||||
return this->title;
|
||||
}
|
||||
|
||||
/*
|
||||
Vector2 RWindow::getSize() const
|
||||
Vector2 RWindow::GetSize() const
|
||||
{
|
||||
return {this->width, this->height};
|
||||
}
|
||||
*/
|
||||
|
||||
int RWindow::getWidth() const
|
||||
int RWindow::GetWidth() const
|
||||
{
|
||||
return this->width;
|
||||
}
|
||||
|
||||
int RWindow::getHeight() const
|
||||
int RWindow::GetHeight() const
|
||||
{
|
||||
return this->height;
|
||||
}
|
||||
|
||||
void RWindow::SetSizeWithoutEvent(const Vector2& size) {
|
||||
width = size.x;
|
||||
height = size.y;
|
||||
}
|
||||
|
||||
void RWindow::setRenderer(RenderingAPI api) {
|
||||
void RWindow::SetLastKnownWindowSize(const Vector2& size) {
|
||||
lastKnownWindowSize = size;
|
||||
}
|
||||
|
||||
void RWindow::SetRenderer(RenderingAPI api) {
|
||||
renderer = api;
|
||||
}
|
||||
|
||||
void RWindow::setSize(const Vector2& size) {
|
||||
void RWindow::SetSize(const Vector2& size) {
|
||||
this->width = size.x;
|
||||
this->height = size.y;
|
||||
this->setSize(size.x, size.y);
|
||||
this->SetSize(size.x, size.y);
|
||||
}
|
||||
|
||||
bool RWindow::isKeyDown(Key key) const {
|
||||
bool RWindow::IsKeyDown(Key key) const {
|
||||
for (const auto& pair : currentKeyboard.PressedKeys)
|
||||
if (pair.first == key && pair.second)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
void RWindow::pressKey(Key key) {
|
||||
bool RWindow::IsMouseButtonDown(const MouseButton &button) const {
|
||||
// TODO: Implement MouseButton map
|
||||
return currentMouse.IsDown(button);
|
||||
}
|
||||
|
||||
void RWindow::processKeyPress(Key key) {
|
||||
currentKeyboard.PressedKeys[key] = true;
|
||||
|
||||
Input::
|
||||
|
||||
auto eventData = KeyDownEvent(key);
|
||||
OnKeyDown(eventData);
|
||||
OnKeyDownEvent(key);
|
||||
}
|
||||
|
||||
void RWindow::processOnClose()
|
||||
{
|
||||
OnClosing();
|
||||
OnClosingEvent();
|
||||
}
|
||||
|
||||
void RWindow::processOnOpen()
|
||||
{
|
||||
OnOpen();
|
||||
OnOpenEvent();
|
||||
}
|
||||
|
||||
void RWindow::ManagedRefresh()
|
||||
{
|
||||
auto begin = GetTimestamp();
|
||||
Refresh();
|
||||
auto end = GetTimestamp();
|
||||
|
||||
float dt = ComputeElapsedFrameTimeSeconds(begin, end);
|
||||
|
||||
UpdateFrameTiming(dt);
|
||||
}
|
||||
|
||||
void RWindow::Refresh() {
|
||||
PollEvents();
|
||||
OnRefresh(delta_time);
|
||||
|
||||
// Only call once and cache the result.
|
||||
currentMouse.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;
|
||||
}
|
||||
|
||||
detail::UpdateMouse(currentMouse);
|
||||
|
||||
}
|
||||
|
||||
float RWindow::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;
|
||||
}
|
||||
|
||||
std::chrono::steady_clock::time_point RWindow::GetTimestamp() {
|
||||
return std::chrono::steady_clock::now();
|
||||
}
|
||||
|
||||
void RWindow::UpdateFrameTiming(float frame_time) {
|
||||
delta_time = frame_time;
|
||||
refresh_rate = 1.f / delta_time;
|
||||
refresh_rate_prev_5 = refresh_rate_prev_4;
|
||||
refresh_rate_prev_4 = refresh_rate_prev_3;
|
||||
refresh_rate_prev_3 = refresh_rate_prev_2;
|
||||
refresh_rate_prev_2 = refresh_rate_prev_1;
|
||||
refresh_rate_prev_1 = refresh_rate;
|
||||
avg_refresh_rate = (refresh_rate_prev_1 + refresh_rate_prev_2 + refresh_rate_prev_3 + refresh_rate_prev_4 + refresh_rate_prev_5) / 5.f;
|
||||
refresh_count++;
|
||||
}
|
||||
|
||||
|
||||
void RWindow::Close()
|
||||
void RWindow::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 RWindow::IsResizable() const {
|
||||
return resizable;
|
||||
}
|
||||
|
||||
bool RWindow::IsFullscreen() const {
|
||||
return fullscreen_mode;
|
||||
}
|
||||
|
||||
bool RWindow::IsFocused() const {
|
||||
return focused;
|
||||
}
|
||||
|
||||
bool RWindow::IsVsyncEnabled() const {
|
||||
return vsync;
|
||||
}
|
||||
|
||||
float RWindow::GetDeltaTime() const { return delta_time; }
|
||||
|
||||
float RWindow::GetRefreshRate() const { return refresh_rate; }
|
||||
|
||||
float RWindow::GetRefreshCounter() const { return refresh_count; }
|
||||
|
||||
|
@@ -6,7 +6,7 @@ using namespace ReWindow;
|
||||
|
||||
bool fullscreenmode = false;
|
||||
bool open = false;
|
||||
HINSTANCE hInstance = GetModuleHandle(NULL);
|
||||
HINSTANCE hInstance = GetModuleHandle(nullptr);
|
||||
HWND hwnd;
|
||||
HDC hdc;
|
||||
HGLRC glContext;
|
||||
@@ -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,102 +38,208 @@ 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 };
|
||||
}
|
||||
|
||||
Vector2 RWindow::getSize() const {
|
||||
void RWindow::SetCursorVisible(bool cursor_enable) {
|
||||
cursor_visible = cursor_enable;
|
||||
}
|
||||
|
||||
bool RWindow::GetCursorVisible() {
|
||||
return cursor_visible;
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = nullptr;
|
||||
wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC) wglGetProcAddress("wglSwapIntervalEXT");
|
||||
auto wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC) wglGetProcAddress("wglSwapIntervalEXT");
|
||||
|
||||
if (wglSwapIntervalEXT)
|
||||
wglSwapIntervalEXT(b ? 1 : 0);
|
||||
}
|
||||
|
||||
bool RWindow::isFullscreen() const {
|
||||
return fullscreenmode;
|
||||
}
|
||||
|
||||
RWindow* eWindow = nullptr;
|
||||
KeyboardState* pKeyboard = nullptr;
|
||||
KeyboardState* cKeyboard = nullptr;
|
||||
|
||||
//Event loop.
|
||||
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
|
||||
switch (uMsg) {
|
||||
case WM_CLOSE:
|
||||
DestroyWindow(hwnd);
|
||||
case WM_DESTROY:
|
||||
exit(0);
|
||||
case WM_SIZE:
|
||||
break;
|
||||
case WM_KEYDOWN: {
|
||||
auto key = GetKeyFromWindowsScancode((WindowsScancode) wParam);
|
||||
//Key repeat fix.
|
||||
if (!pKeyboard->PressedKeys[key]) {
|
||||
eWindow->OnKeyDownEvent(key);
|
||||
eWindow->OnKeyDown(key);
|
||||
eWindow->pressKey(key);
|
||||
case WM_CLOSE: {
|
||||
DestroyWindow(hwnd);
|
||||
|
||||
eWindow->processOnClose();
|
||||
}
|
||||
break;
|
||||
}
|
||||
case WM_KEYUP: {
|
||||
auto key = GetKeyFromWindowsScancode((WindowsScancode) wParam);
|
||||
eWindow->OnKeyUpEvent(key);
|
||||
eWindow->OnKeyUp(key);
|
||||
eWindow->liftKey(key);
|
||||
break;
|
||||
}
|
||||
case WM_LBUTTONDOWN:
|
||||
break;
|
||||
case WM_LBUTTONUP:
|
||||
|
||||
case WM_DESTROY: {
|
||||
exit(0);
|
||||
}
|
||||
|
||||
case WM_SIZE: {
|
||||
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()});
|
||||
|
||||
// TODO: Implement eWindow->processOnResize()
|
||||
|
||||
eWindow->OnResizeRequest(eventData);
|
||||
eWindow->OnResizeRequestEvent(eventData);
|
||||
|
||||
|
||||
//Just to be absolutely sure the OpenGL viewport resizes along with the window.
|
||||
glViewport(0, 0, eWindow->GetWidth(), eWindow->GetHeight());
|
||||
break;
|
||||
}
|
||||
|
||||
case WM_SETFOCUS: {
|
||||
|
||||
eWindow->processFocusIn();
|
||||
|
||||
eWindow->SetFlag(RWindowFlags::IN_FOCUS, true);
|
||||
break;
|
||||
}
|
||||
|
||||
case WM_KILLFOCUS: {
|
||||
|
||||
eWindow->processFocusOut();
|
||||
|
||||
eWindow->SetFlag(RWindowFlags::IN_FOCUS, false);
|
||||
break;
|
||||
}
|
||||
|
||||
case WM_SETCURSOR: {
|
||||
if (LOWORD(lParam) == HTCLIENT && eWindow->GetCursorVisible() == false)
|
||||
SetCursor(nullptr);
|
||||
break;
|
||||
}
|
||||
|
||||
case WM_KEYDOWN: {
|
||||
auto key = GetKeyFromWindowsScancode((WindowsScancode) wParam);
|
||||
//Key repeat fix.
|
||||
if (!pKeyboard->PressedKeys[key]) {
|
||||
eWindow->processKeyPress(key);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case WM_KEYUP: {
|
||||
auto key = GetKeyFromWindowsScancode((WindowsScancode) wParam);
|
||||
eWindow->processKeyRelease(key);
|
||||
break;
|
||||
}
|
||||
|
||||
//Mouse Buttons.
|
||||
case WM_MOUSEWHEEL: {
|
||||
int wheel_delta = GET_WHEEL_DELTA_WPARAM(wParam);
|
||||
|
||||
// TODO: Determine sign of wheel_delta for each direction, (and on linux too), and document this.
|
||||
eWindow->processMouseWheel(wheel_delta);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case WM_LBUTTONDOWN: {
|
||||
eWindow->processMousePress(MouseButtons::Left);
|
||||
break;
|
||||
}
|
||||
|
||||
case WM_LBUTTONUP: {
|
||||
eWindow->processMouseRelease(MouseButtons::Left);
|
||||
break;
|
||||
}
|
||||
|
||||
case WM_RBUTTONDOWN: {
|
||||
eWindow->processMousePress(MouseButtons::Right);
|
||||
break;
|
||||
}
|
||||
|
||||
case WM_RBUTTONUP: {
|
||||
eWindow->processMouseRelease(MouseButtons::Right);
|
||||
break;
|
||||
}
|
||||
|
||||
case WM_MBUTTONDOWN: {
|
||||
eWindow->processMousePress(MouseButtons::Middle);
|
||||
break;
|
||||
}
|
||||
|
||||
case WM_MBUTTONUP: {
|
||||
eWindow->processMouseRelease(MouseButtons::Middle);
|
||||
break;
|
||||
}
|
||||
|
||||
case WM_XBUTTONDOWN: {
|
||||
WORD button = GET_XBUTTON_WPARAM(wParam);
|
||||
|
||||
if (button == XBUTTON1)
|
||||
eWindow->processMousePress(MouseButtons::Mouse4);
|
||||
|
||||
if (button == XBUTTON2)
|
||||
eWindow->processMousePress(MouseButtons::Mouse5);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case WM_XBUTTONUP: {
|
||||
WORD button = GET_XBUTTON_WPARAM(wParam);
|
||||
if (button == XBUTTON1)
|
||||
eWindow->processMouseRelease(MouseButtons::Mouse4);
|
||||
|
||||
if (button == XBUTTON2)
|
||||
eWindow->processMouseRelease(MouseButtons::Mouse5);
|
||||
break;
|
||||
}
|
||||
|
||||
//This is the same as "Motion Notify" in the X Window System.
|
||||
case WM_MOUSEMOVE:
|
||||
break;
|
||||
break;
|
||||
}
|
||||
|
||||
if (pKeyboard != nullptr && cKeyboard != nullptr)
|
||||
pKeyboard = cKeyboard;
|
||||
|
||||
return DefWindowProc(hwnd, uMsg, wParam, lParam);
|
||||
}
|
||||
|
||||
void RWindow::setCursorStyle(CursorStyle style) const {}
|
||||
void RWindow::SetCursorStyle(CursorStyle style) const {}
|
||||
|
||||
void RWindow::Open() {
|
||||
eWindow = this;
|
||||
@@ -183,18 +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::this_thread::sleep_for(1ms);
|
||||
}
|
||||
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
src/types/inputdevice.cpp
Normal file
1
src/types/inputdevice.cpp
Normal file
@@ -0,0 +1 @@
|
||||
#include <rewindow/types/inputdevice.hpp>
|
29
src/types/inputservice.cpp
Normal file
29
src/types/inputservice.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#include <rewindow/types/inputservice.hpp>
|
||||
|
||||
namespace ReWindow {
|
||||
namespace detail
|
||||
{
|
||||
static Mouse* internal_mouse;
|
||||
|
||||
void UpdateMouse(Mouse* mouse) {
|
||||
detail::internal_mouse = mouse;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace InputService
|
||||
{
|
||||
namespace Mouse
|
||||
{
|
||||
Vector2 GetMousePosition()
|
||||
{
|
||||
return detail::internal_mouse->GetPosition();
|
||||
}
|
||||
|
||||
bool IsButtonDown(const MouseButton &btn) {
|
||||
return detail::internal_mouse->IsButtonDown(btn);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -1,35 +1,31 @@
|
||||
#include <rewindow/types/key.h>
|
||||
#include <jlog/jlog.hpp>
|
||||
//#include <rewindow/logger.h>
|
||||
//std::vector<Key> Key::keyboard = {};
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Key GetKeyFromX11Scancode(X11Scancode code) {
|
||||
for (auto& key : Key::GetKeyboard())
|
||||
for (const auto& key : Key::GetKeyboard())
|
||||
if (key.x11ScanCode == code)
|
||||
return key;
|
||||
std::cout << "Unavaliable Scancode: " + std::to_string((int)code) << std::endl;
|
||||
|
1
src/types/keyboard.cpp
Normal file
1
src/types/keyboard.cpp
Normal file
@@ -0,0 +1 @@
|
||||
#include <rewindow/types/keyboard.h>
|
82
src/types/mouse.cpp
Normal file
82
src/types/mouse.cpp
Normal file
@@ -0,0 +1,82 @@
|
||||
#include <rewindow/types/mouse.h>
|
||||
|
||||
namespace ReWindow
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
static Mouse* internal_mouse;
|
||||
|
||||
void UpdateMouse(Mouse* mouse) {
|
||||
detail::internal_mouse = mouse;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace InputService
|
||||
{
|
||||
namespace Mouse
|
||||
{
|
||||
Vector2 GetMousePosition()
|
||||
{
|
||||
return detail::internal_mouse->GetPosition();
|
||||
}
|
||||
|
||||
bool IsButtonDown(const MouseButton &btn) {
|
||||
return detail::internal_mouse->IsButtonDown(btn);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void Mouse::DoButtonPress(const MouseButton &btn) {
|
||||
SetButtonPressed(btn, true);
|
||||
}
|
||||
|
||||
void Mouse::DoButtonRelease(const MouseButton &btn) {
|
||||
SetButtonPressed(btn, false);
|
||||
}
|
||||
|
||||
DeviceType Mouse::GetType() const { return DeviceType::MOUSE;}
|
||||
|
||||
void Mouse::Update(double elapsed) {
|
||||
current_state.Left.TimeHeld += elapsed;
|
||||
current_state.Right.TimeHeld += elapsed;
|
||||
current_state.Middle.TimeHeld += elapsed;
|
||||
current_state.Mouse4.TimeHeld += elapsed;
|
||||
current_state.Mouse5.TimeHeld += elapsed;
|
||||
|
||||
previous_state = current_state;
|
||||
}
|
||||
|
||||
ButtonState Mouse::GetButtonState(const MouseButton &btn) const {
|
||||
if (btn == MouseButtons::Left) return current_state.Left;
|
||||
if (btn == MouseButtons::Right) return current_state.Right;
|
||||
if (btn == MouseButtons::Middle) return current_state.Middle;
|
||||
if (btn == MouseButtons::Mouse4) return current_state.Mouse4;
|
||||
if (btn == MouseButtons::Mouse5) return current_state.Mouse5;
|
||||
}
|
||||
|
||||
bool Mouse::IsButtonDown(const MouseButton &btn) const {
|
||||
return GetButtonState(btn).Pressed;
|
||||
}
|
||||
|
||||
Mouse::~Mouse() {}
|
||||
|
||||
Mouse::Mouse() {}
|
||||
|
||||
void Mouse::SetButtonPressed(const MouseButton &btn, bool pressed) {
|
||||
auto state = GetButtonState(btn);
|
||||
state.Pressed = pressed;
|
||||
state.TimeHeld = 0;
|
||||
SetButtonState(btn, state);
|
||||
}
|
||||
|
||||
void Mouse::SetButtonState(const MouseButton &btn, ButtonState state) {
|
||||
if (btn == MouseButtons::Left) current_state.Left = state;
|
||||
if (btn == MouseButtons::Right) current_state.Right = state;
|
||||
if (btn == MouseButtons::Middle) current_state.Middle = state;
|
||||
if (btn == MouseButtons::Mouse4) current_state.Mouse4 = state;
|
||||
if (btn == MouseButtons::Mouse5) current_state.Mouse5 = state;
|
||||
}
|
||||
}
|
@@ -1,12 +1,11 @@
|
||||
#include <rewindow/types/mousebutton.h>
|
||||
#include <string>
|
||||
#include <jlog/jlog.hpp>
|
||||
#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,19 +13,23 @@ 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;
|
||||
default: {
|
||||
FATAL("Undefined XButtonCode: " + std::to_string((int) button));
|
||||
ReWindow::Logger::Fatal("Undefined XButtonCode: " + std::to_string((int) button));
|
||||
return MouseButtons::Unimplemented;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user