Compare commits

...

30 Commits

Author SHA1 Message Date
483b5dadad WINDOZEEEEEEEEEEEEEEEEEEEEEEEEEEEE
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 7m11s
2024-08-07 23:49:11 -04:00
9565bfeef9 Merge remote-tracking branch 'origin/main'
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m27s
2024-08-05 18:28:35 -04:00
e1fc2f289f Migrated to J3ML 2.4 2024-08-05 18:28:30 -04:00
7bc90af3d5 Update
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 4m10s
2024-08-05 13:35:39 -04:00
d9ee8ebedc Update window.cpp
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 4m1s
2024-08-05 11:58:29 -04:00
94e61aa2ea Changed MouseButton::operator==() to compare buttons via their ButtonIndices instead of CharCodes
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m16s
2024-07-30 16:41:37 -04:00
c95ad0ee0d Accidentally lost invokation of OnMouseMoveEvent, so we're re-adding it.
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 4m29s
2024-07-30 15:02:45 -04:00
3b3d1ba197 should be all deps for ubuntu
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m32s
2024-07-29 20:19:04 -04:00
1542224ffc Merge remote-tracking branch 'refs/remotes/origin/main'
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 48s
reci shit
2024-07-29 20:15:03 -04:00
8d92abdecf use ubuntu names 2024-07-29 20:14:51 -04:00
9247472a13 use ubuntu names
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Has been cancelled
2024-07-29 20:08:42 -04:00
7e5d85c88a Merge pull request 'Input Class Refactoring' (#11) from litandbased_refactors into main
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 50s
Reviewed-on: #11
2024-07-29 20:08:00 -04:00
7dad83bbc1 reci test
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 45s
2024-07-29 20:07:32 -04:00
c18fd72b74 reci test
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 43s
2024-07-29 20:06:36 -04:00
663109da75 reci 2024-07-29 20:02:52 -04:00
c7a2b42f1d Implement operator == on MouseButton class 2024-07-29 19:53:50 -04:00
1f81c0b843 More small refactoring 2024-07-29 18:54:56 -04:00
c25983a87d Random refactoring of the input systems. 2024-07-29 18:22:23 -04:00
1b02dcc364 Small oopsie. 2024-07-17 00:44:10 -04:00
254f991c49 Made sure all event raisers were implemented, implemented getLastKnownResize() 2024-07-17 00:41:56 -04:00
b4c4827c9c Adding dev notes and documenting the public API. 2024-07-15 13:01:27 -04:00
ebf939d747 Add missing keys 2024-07-03 12:15:17 -04:00
04c82df9c5 Fix window resize on windows 2024-07-02 12:41:32 -04:00
2f0cedcd04 Windows smh 2024-07-01 14:58:55 -04:00
a215d9f398 Merge branch 'latest' of https://git.redacted.cc/Redacted/ReWindow into latest 2024-06-28 19:39:41 -04:00
a3697cd022 Refactor 2024-06-28 19:39:20 -04:00
68a1f52515 Refactor 2024-06-28 19:32:45 -04:00
Redacted
8f8eabfe47 Update CMakeLists.txt 2024-06-26 23:27:20 -04:00
Redacted
a4ec53fdd9 Update CMakeLists.txt 2024-06-26 23:25:18 -04:00
c0ecb08a3b Update window.cpp
Fix use after free
2024-06-21 16:24:38 -04:00
21 changed files with 746 additions and 449 deletions

View File

@@ -0,0 +1,23 @@
name: Run ReCI Build Test
run-name: Run ReCI Build Test For ${{ gitea.repository }}.
on: [push]
jobs:
Explore-Gitea-Actions:
runs-on: ubuntu-22.04
env:
RECI_GIT: https://git.redacted.cc/maxine/ReCI
RECI: /RECI
steps:
- run: echo "The job was automatically triggered by a ${{ gitea.event_name }} event."
- run: echo "This job is now running on a ${{ runner.os }} server hosted by Gitea!"
- run: echo "The name of your branch is ${{ gitea.ref }} and your repository is ${{ gitea.repository }}."
- name: Check out repository code
uses: actions/checkout@v3
- 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: 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 }}."

View File

@@ -14,11 +14,11 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
# Enable Package Managers
include(cmake/CPM.cmake)
include(cmake/CPM.cmake)
CPMAddPackage(
NAME J3ML
URL https://git.redacted.cc/josh/j3ml/archive/Release-2.1.zip
URL https://git.redacted.cc/josh/j3ml/archive/Release-3.0.zip
)
CPMAddPackage(
@@ -28,7 +28,7 @@ CPMAddPackage(
CPMAddPackage(
NAME jlog
URL https://git.redacted.cc/josh/jlog/archive/Prerelease-8.zip
URL https://git.redacted.cc/josh/jlog/archive/Prerelease-12.zip
)
find_package(OpenGL REQUIRED)
@@ -40,11 +40,11 @@ include_directories(${jlog_SOURCE_DIR}/include)
file(GLOB_RECURSE HEADERS "include/*.h" "include/*.hpp")
if(UNIX AND NOT APPLE)
file(GLOB_RECURSE SOURCES "src/rewindow/*.cpp" "src/platform/linux/*.cpp" "src/platform/shared/*.cpp")
file(GLOB_RECURSE SOURCES "src/types/*.cpp" "src/platform/linux/*.cpp" "src/platform/shared/*.cpp")
endif()
if(WIN32)
file(GLOB_RECURSE SOURCES "src/rewindow/*.cpp" "src/platform/windows/*.cpp" "src/platform/shared/*.cpp")
file(GLOB_RECURSE SOURCES "src/types/*.cpp" "src/platform/windows/*.cpp" "src/platform/shared/*.cpp")
endif()
include_directories("include")
@@ -71,8 +71,8 @@ if(UNIX AND NOT APPLE)
endif()
if(WIN32)
target_compile_options(ReWindowLibrary PUBLIC /utf-8)
target_link_libraries(ReWindowLibrary PUBLIC J3ML Event jlog ${OPENGL_LIBRARIES})
add_executable(ReWindowLibraryDemo main.cpp)
target_link_libraries(ReWindowLibraryDemo PUBLIC ReWindowLibrary)
endif()

View File

@@ -12,6 +12,7 @@ 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
```
Clone the repository
@@ -30,4 +31,4 @@ Run it
```bash
./ReWindowDemo
```
```

View File

@@ -1,7 +1,7 @@
#pragma once
enum class WindowsScancode {
Nothing = 0,
Nothing = 0,
ESCAPE = 0x1B, // VK_ESCAPE
ONE = 0x31, // '1'
TWO = 0x32, // '2'
@@ -43,7 +43,7 @@ enum class WindowsScancode {
SEMICOLON = 0xBA, // VK_OEM_1
SINGLEQUOTE = 0xDE, // VK_OEM_7
GRAVE = 0xC0, // VK_OEM_3
LEFT_SHIFT = 0xA0, // VK_LSHIFT
LEFT_SHIFT = 0x10, // VK_LSHIFT
BACKSLASH = 0xDC, // VK_OEM_5
Z = 0x5A, // 'Z'
X = 0x58, // 'X'
@@ -52,7 +52,6 @@ enum class WindowsScancode {
B = 0x42, // 'B'
N = 0x4E, // 'N'
M = 0x4D, // 'M'
COMMA = 0xBC, // VK_OEM_COMMA
PERIOD = 0xBE, // VK_OEM_PERIOD
SLASH = 0xBF, // VK_OEM_2
RIGHT_SHIFT = 0xA1, // VK_RSHIFT
@@ -97,6 +96,12 @@ enum class WindowsScancode {
NUMPAD_NINE = 0x69, // VK_NUMPAD9
NUMPAD_PLUS = 0x6B, // VK_ADD
NUMPAD_MINUS = 0x6D, // VK_SUBTRACT
NUMPAD_DECIMAL = 0x6E, // VK_DECIMAL
NUMPAD_ENTER = 0x0D // VK_RETURN
NUMPAD_ASTERISK = 0x6A,
NUMPAD_SLASH = 0x6F,
NUMPAD_PERIOD = 0x6E, // VK_DECIMAL
NUMPAD_ENTER = 0x0D, // VK_RETURN
SUPER = 0x5B,
COMMA = 0xBC,
MENU = 93,
BREAK = 0x13
};

View File

@@ -24,6 +24,7 @@ enum class X11Scancode {
PAGEUP = 112,
PAGEDOWN = 117,
HOME = 110,
MENU = 135,
END = 115,
INSERT = 118,
DEL = 119,

View File

@@ -0,0 +1,26 @@
/// 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 gamepad.h
/// @desc A class that models the functionality of a gamepad / controller device.
/// @edit 2024-07-29
#pragma once
namespace ReWindow
{
class InputDevice {}; // TODO: Remember to break InputDevice into it's own file and not define it twice!!!
class Gamepad : public InputDevice
{
};
class XboxGamepad : public Gamepad {};
class PS4Gamepad : public Gamepad {};
}

View File

@@ -0,0 +1,75 @@
/// 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 gamepadbutton.h
/// @desc GamepadButton class and enumerations to define standard buttons found on a Gamepad
/// @edit 2024-07-29
#include <string>
#include <J3ML/LinearAlgebra.hpp>
class GamepadButton {
protected:
std::string mnemonic_btn_code;
public:
explicit GamepadButton(const std::string& mnemonic) : mnemonic_btn_code(mnemonic) { }
[[nodiscard]] std::string GetMnemonicButtonCode() const { return mnemonic_btn_code; }
/// Compares two GamepadButtons by their mnemonic button codes, not their activation state.
bool operator ==(const GamepadButton& rhs) const;
};
class GamepadTrigger {
public:
/// Returns a float value between 0-1 representing how much the trigger has been pushed in by.
/// (0 being unpressed, 1 being fully pressed)
float GetActuation() const;
/// TODO: Might be more appropriate in the Gamepad class representation.
void SetActuationThreshold(float minimum = 0.01f) const;
};
class GamepadThumbstick
{
public:
/// Returns a Vector2 value representing the x,y coordinates of the joystick, with 0,0 being the center (at rest).
/// This vector ranges from length = 0 to length = 1 (i.e. the unit circle).
[[nodiscard]] Vector2 GetPosition() const;
/// Sets the deadzone range of the thumbstick.
/// Deadzone controls how far the stick must be moved before any movement is actually reported.
/// This is because the thumbstick at-rest will often still report movement.
/// If gamecode is architected to use the thumbstick position as a direction, without factoring in magnitude, this would cause problems.
void SetDeadzone(float minimum = 0.01f) const;
};
using J3ML::LinearAlgebra::Vector2;
namespace GamepadButtons {
static const GamepadButton X {"X"};
static const GamepadButton Y {"Y"};
static const GamepadButton A {"A"};
static const GamepadButton B {"B"};
static const auto Triangle = Y;
static const auto Square = X;
static const auto Circle = A;
static const auto Cross = B;
static const GamepadButton LeftBumper {"LB"};
static const GamepadButton RightBumper {"RB"};
}
namespace GamepadTriggers
{
static const GamepadTrigger Left;
static const GamepadTrigger Right;
}

View File

@@ -3,19 +3,13 @@
#pragma once
#include <vector>
#include <J3ML/LinearAlgebra/Vector2.h>
#include <jlog/jlog.hpp>
#include <stdexcept>
#include <iostream>
#if __linux__
#include <X11/X.h>
#endif
#include <string>
#include <rewindow/data/X11Scancodes.h>
#include <rewindow/data/WindowsScancodes.h>
#include <J3ML/LinearAlgebra.hpp>
using J3ML::LinearAlgebra::Vector2;
class Key
{
@@ -37,31 +31,44 @@ public:
namespace Keys {
// TODO: Encode both Uppercase and Lowercase version for each keymap
static const Key Escape {"\b", X11Scancode::ESCAPE, WindowsScancode::ESCAPE};
static const Key F1 {"\u000f", X11Scancode::F1, WindowsScancode::F1};
static const Key F2 {"\u000f", X11Scancode::F2, WindowsScancode::F2};
static const Key F3 {"\u000f", X11Scancode::F3, WindowsScancode::F3};
static const Key F4 {"\u000f", X11Scancode::F4, WindowsScancode::F4};
static const Key F5 {"\u000f", X11Scancode::F5, WindowsScancode::F5};
static const Key F6 {"\u000f", X11Scancode::F6, WindowsScancode::F6};
static const Key F7 {"\u000f", X11Scancode::F7, WindowsScancode::F7};
static const Key F8 {"\u000f", X11Scancode::F8, WindowsScancode::F8};
static const Key F9 {"\u000f", X11Scancode::F9, WindowsScancode::F9};
static const Key F10 {"\u000f", X11Scancode::F10, WindowsScancode::F10};
static const Key F11 {"\u000f", X11Scancode::F11, WindowsScancode::F11};
static const Key F12 {"\u000f", X11Scancode::F12, WindowsScancode::F12};
static const Key Escape {"ESC", X11Scancode::ESCAPE, WindowsScancode::ESCAPE};
static const Key F1 {"F1", X11Scancode::F1, WindowsScancode::F1};
static const Key F2 {"F2", X11Scancode::F2, WindowsScancode::F2};
static const Key F3 {"F3", X11Scancode::F3, WindowsScancode::F3};
static const Key F4 {"F4", X11Scancode::F4, WindowsScancode::F4};
static const Key F5 {"F5", X11Scancode::F5, WindowsScancode::F5};
static const Key F6 {"F6", X11Scancode::F6, WindowsScancode::F6};
static const Key F7 {"F7", X11Scancode::F7, WindowsScancode::F7};
static const Key F8 {"F8", X11Scancode::F8, WindowsScancode::F8};
static const Key F9 {"F9", X11Scancode::F9, WindowsScancode::F9};
static const Key F10 {"F10", X11Scancode::F10, WindowsScancode::F10};
static const Key F11 {"F11", X11Scancode::F11, WindowsScancode::F11};
static const Key F12 {"F12", X11Scancode::F12, WindowsScancode::F12};
static const Key Print {"PRINT", X11Scancode::PRINT, WindowsScancode::PRINTSCREEN};
static const Key ScrollLock {"SCROLL_LOCK", X11Scancode::SCROLL_LOCK, WindowsScancode::SCROLL_LOCK};
static const Key Break {"BREAK", X11Scancode::BREAK, WindowsScancode::BREAK};
static const Key NumPad1 {"\b", X11Scancode::KP_1, WindowsScancode::NUMPAD_ONE};
static const Key NumPad2 {"\b", X11Scancode::KP_2, WindowsScancode::NUMPAD_TWO};
static const Key NumPad3 {"\b", X11Scancode::KP_3, WindowsScancode::NUMPAD_THREE};
static const Key NumPad4 {"\b", X11Scancode::KP_4, WindowsScancode::NUMPAD_FOUR};
static const Key NumPad5 {"\b", X11Scancode::KP_5, WindowsScancode::NUMPAD_FIVE};
static const Key NumPad6 {"\b", X11Scancode::KP_6, WindowsScancode::NUMPAD_SIX};
static const Key NumPad7 {"\b", X11Scancode::KP_7, WindowsScancode::NUMPAD_SEVEN};
static const Key NumPad8 {"\b", X11Scancode::KP_8, WindowsScancode::NUMPAD_EIGHT};
static const Key NumPad9 {"\b", X11Scancode::KP_9, WindowsScancode::NUMPAD_NINE};
static const Key NumPad0 {"\b", X11Scancode::KP_0, WindowsScancode::NUMPAD_ZERO};
//TODO On Windows, Return & KP_RETURN are the same thing.
static const Key NumPadReturn {"KP_↵", X11Scancode::KP_RETURN, WindowsScancode::NUMPAD_ENTER};
static const Key NumPadPlus {"KP_+", X11Scancode::KP_PLUS, WindowsScancode::NUMPAD_PLUS};
static const Key NumPadMinus {"KP_-", X11Scancode::KP_MINUS, WindowsScancode::NUMPAD_MINUS};
static const Key NumPadAsterisk {"KP_*", X11Scancode::KP_ASTERISK, WindowsScancode::NUMPAD_ASTERISK};
static const Key NumPadForwardSlash {"KP_/", X11Scancode::KP_SLASH, WindowsScancode::NUMPAD_SLASH};
static const Key NumPadPeriod {"KP_.", X11Scancode::KP_PERIOD, WindowsScancode::NUMPAD_PERIOD};
static const Key NumPadNumLock {"KP_NUMLOCK", X11Scancode::KP_NUMLOCK, WindowsScancode::NUM_LOCK};
static const Key NumPad1 {"KP_1", X11Scancode::KP_1, WindowsScancode::NUMPAD_ONE};
static const Key NumPad2 {"KP_2", X11Scancode::KP_2, WindowsScancode::NUMPAD_TWO};
static const Key NumPad3 {"KP_3", X11Scancode::KP_3, WindowsScancode::NUMPAD_THREE};
static const Key NumPad4 {"KP_4", X11Scancode::KP_4, WindowsScancode::NUMPAD_FOUR};
static const Key NumPad5 {"KP_5", X11Scancode::KP_5, WindowsScancode::NUMPAD_FIVE};
static const Key NumPad6 {"KP_6", X11Scancode::KP_6, WindowsScancode::NUMPAD_SIX};
static const Key NumPad7 {"KP_7", X11Scancode::KP_7, WindowsScancode::NUMPAD_SEVEN};
static const Key NumPad8 {"kP_8", X11Scancode::KP_8, WindowsScancode::NUMPAD_EIGHT};
static const Key NumPad9 {"KP_9", X11Scancode::KP_9, WindowsScancode::NUMPAD_NINE};
static const Key NumPad0 {"KP_0", X11Scancode::KP_0, WindowsScancode::NUMPAD_ZERO};
static const Key Grave {"`", X11Scancode::GRAVE, WindowsScancode::GRAVE};
static const Key One {"1", X11Scancode::ONE, WindowsScancode::ONE};
static const Key Two {"2", X11Scancode::TWO, WindowsScancode::TWO};
static const Key Three {"3", X11Scancode::THREE, WindowsScancode::THREE};
@@ -72,7 +79,11 @@ namespace Keys {
static const Key Eight {"8", X11Scancode::EIGHT, WindowsScancode::EIGHT};
static const Key Nine {"9", X11Scancode::NINE, WindowsScancode::NINE};
static const Key Zero {"0", X11Scancode::ZERO, WindowsScancode::ZERO};
static const Key Minus {"-", X11Scancode::MINUS, WindowsScancode::MINUS};
static const Key Equals {"+", X11Scancode::EQUALS, WindowsScancode::EQUALS};
static const Key Backspace {"", X11Scancode::BACKSPACE, WindowsScancode::BACKSPACE};
static const Key Tab {"", X11Scancode::TAB, WindowsScancode::TAB};
static const Key Q {"Q", X11Scancode::Q, WindowsScancode::Q};
static const Key W {"W", X11Scancode::W, WindowsScancode::W};
static const Key E {"E", X11Scancode::E, WindowsScancode::E};
@@ -83,6 +94,11 @@ namespace Keys {
static const Key I {"I", X11Scancode::I, WindowsScancode::I};
static const Key O {"O", X11Scancode::O, WindowsScancode::O};
static const Key P {"P", X11Scancode::P, WindowsScancode::P};
static const Key LeftBracket {"[", X11Scancode::OPENING_SQUARE_BRACKET, WindowsScancode::LEFT_BRACKET};
static const Key RightBracket {"]", X11Scancode::CLOSING_SQUARE_BRACKET, WindowsScancode::RIGHT_BRACKET};
static const Key BackSlash {"\\", X11Scancode::BACKSLASH, WindowsScancode::BACKSLASH};
static const Key CapsLock = {"CAPS", X11Scancode::CAPSLOCK, WindowsScancode::CAPS_LOCK};
static const Key A {"A", X11Scancode::A, WindowsScancode::A};
static const Key S {"S", X11Scancode::S, WindowsScancode::S};
static const Key D {"D", X11Scancode::D, WindowsScancode::D};
@@ -93,12 +109,10 @@ namespace Keys {
static const Key K {"K", X11Scancode::K, WindowsScancode::K};
static const Key L {"L", X11Scancode::L, WindowsScancode::L};
static const Key Semicolon {";", X11Scancode::SEMICOLON, WindowsScancode::SEMICOLON};
static const Key SingeQuote {"\"", X11Scancode::SINGLEQUOTE, WindowsScancode::SINGLEQUOTE};
static const Key Enter {"\n", X11Scancode::RETURN, WindowsScancode::ENTER};
//static const Key KeyPadEnter {"\n", X11Scancode::KP_RETURN};
static const Key Minus {"-", X11Scancode::MINUS, WindowsScancode::MINUS};
//static const Key KeyPadMinus {"-", X11Scancode::KP_MINUS};
static const Key Equals {"=", X11Scancode::EQUALS, WindowsScancode::EQUALS};
static const Key SingeQuote {"\'", X11Scancode::SINGLEQUOTE, WindowsScancode::SINGLEQUOTE};
static const Key Return {"", X11Scancode::RETURN, WindowsScancode::ENTER};
static const Key LeftShift {"", X11Scancode::LEFT_SHIFT, WindowsScancode::LEFT_SHIFT};
static const Key Z {"Z", X11Scancode::Z, WindowsScancode::Z};
static const Key X {"X", X11Scancode::X, WindowsScancode::X};
static const Key C {"C", X11Scancode::C, WindowsScancode::C};
@@ -106,105 +120,36 @@ namespace Keys {
static const Key B {"B", X11Scancode::B, WindowsScancode::B};
static const Key N {"N", X11Scancode::N, WindowsScancode::N};
static const Key M {"M", X11Scancode::M, WindowsScancode::M};
static const Key Comma = {",", X11Scancode::COMMA, WindowsScancode::COMMA};
static const Key Period {".", X11Scancode::PERIOD, WindowsScancode::PERIOD};
static const Key ForwardSlash {"/", X11Scancode::SLASH, WindowsScancode::SLASH};
static const Key BackSlash {"\\", X11Scancode::BACKSLASH, WindowsScancode::BACKSLASH};
static const Key LeftControl {"LCTRL", X11Scancode::LEFT_CTRL, WindowsScancode::CTRL};
static const Key Super {"", X11Scancode::SUPER, WindowsScancode::SUPER};
static const Key LeftAlt {"🄰", X11Scancode::LEFT_ALT, WindowsScancode::ALT};
static const Key Space {" ", X11Scancode::SPACE, WindowsScancode::SPACE};
static const Key RightAlt {"R🄰", X11Scancode::RIGHT_ALT, WindowsScancode::ALT};
static const Key Menu = {"", X11Scancode::MENU, WindowsScancode::MENU};
static const Key RightControl {"RCTRL", X11Scancode::RIGHT_CONTROL, WindowsScancode::CTRL};
static const Key RightShift {"R⇧", X11Scancode::RIGHT_SHIFT, WindowsScancode::RIGHT_SHIFT};
static const Key LControl {"\\", X11Scancode::LEFT_CTRL, WindowsScancode::CTRL};
static const Key RControl {"\\", X11Scancode::RIGHT_CONTROL, WindowsScancode::CTRL};
static const Key Insert {"INSERT", X11Scancode::INSERT, WindowsScancode::INSERT};
static const Key Home {"HOME", X11Scancode::HOME, WindowsScancode::HOME};
static const Key PageUp {"PAGEUP", X11Scancode::PAGEUP, WindowsScancode::PAGE_UP};
static const Key Delete {"DELETE", X11Scancode::DEL, WindowsScancode::DEL};
static const Key End {"End", X11Scancode::END, WindowsScancode::END};
static const Key PageDown {"PAGEDOWN", X11Scancode::PAGEDOWN, WindowsScancode::PAGE_DOWN};
static const Key LAlt {"\\", X11Scancode::LEFT_ALT, WindowsScancode::ALT};
static const Key RAlt {"\\", X11Scancode::RIGHT_ALT, WindowsScancode::ALT};
static const Key UpArrow {reinterpret_cast<const char*>(u8""), X11Scancode::UP, WindowsScancode::UP_ARROW};
static const Key DownArrow {reinterpret_cast<const char*>(u8""), X11Scancode::DOWN, WindowsScancode::DOWN_ARROW};
static const Key LeftArrow {reinterpret_cast<const char*>(u8""), X11Scancode::LEFT, WindowsScancode::LEFT_ARROW};
static const Key RightArrow {reinterpret_cast<const char*>(u8""), X11Scancode::RIGHT, WindowsScancode::RIGHT_ARROW};
static const Key LShift {"\\", X11Scancode::LEFT_SHIFT, WindowsScancode::LEFT_SHIFT};
static const Key RShift {"\\", X11Scancode::RIGHT_SHIFT, WindowsScancode::RIGHT_SHIFT};
// TODO: Get the right character codes for these
static const Key UpArrow { "\u2191", X11Scancode::UP, WindowsScancode::UP_ARROW};
static const Key DownArrow {"\u2193", X11Scancode::DOWN, WindowsScancode::DOWN_ARROW};
static const Key LeftArrow {"\u2190", X11Scancode::LEFT, WindowsScancode::LEFT_ARROW};
static const Key RightArrow {"\u2192", X11Scancode::RIGHT, WindowsScancode::RIGHT_ARROW};
static const Key Super {"\000a", X11Scancode::SUPER, WindowsScancode::Nothing};
static const Key Backspace {"\b", X11Scancode::BACKSPACE, WindowsScancode::BACKSPACE};
static const Key LeftBracket {"[", X11Scancode::OPENING_SQUARE_BRACKET, WindowsScancode::LEFT_BRACKET};
static const Key RightBracket {"]", X11Scancode::CLOSING_SQUARE_BRACKET, WindowsScancode::RIGHT_BRACKET};
}
class GamepadButton {};
class MouseButton {
public:
MouseButton();
explicit MouseButton(const char* charcode);
const char* CharCode;
};
using J3ML::LinearAlgebra::Vector2;
class InputService {
public:
static Vector2 GetLeftJoystick();
static Vector2 GetRightJoystick();
};
namespace GamepadButtons {
static const GamepadButton X;
static const GamepadButton Y;
static const GamepadButton A;
static const GamepadButton B;
}
namespace MouseButtons
{
static const MouseButton Left {"l"};
static const MouseButton Right {"r"};
static const MouseButton Middle {"m"};
static const MouseButton MWheelUp {"1"};
static const MouseButton MWheelDown {"2"};
static const MouseButton Mouse4 {"4"};
static const MouseButton Mouse5 {"5"};
static const MouseButton Unimplemented {"u"};
}
#if __linux__
static MouseButton GetMouseButtonFromXButton(unsigned int button) {
switch(button) {
case Button1: return MouseButtons::Left;
case Button2: return MouseButtons::Middle;
case Button3: return MouseButtons::Right;
case Button4: return MouseButtons::MWheelUp;
case Button5: 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))
return MouseButtons::Unimplemented;
break;
}
}
}
static Key GetKeyFromX11Scancode(X11Scancode code) {
for (auto& key : Key::GetKeyboard())
if (key.x11ScanCode == code)
return key;
std::cout << "Unavaliable Scancode: " + std::to_string((int)code) << std::endl;
return Keys::Space;
}
#endif
#if _WIN32
static Key GetKeyFromWindowsScancode(WindowsScancode code) {
for (const auto& key : Key::GetKeyboard())
if (key.winScanCode == code)
return key;
std::cout << "Unavaliable Scancode: " + std::to_string((int) code) << std::endl;
return Keys::Space;
}
#endif
Key GetKeyFromX11Scancode(X11Scancode code);
Key GetKeyFromWindowsScancode(WindowsScancode code);

View File

@@ -0,0 +1,22 @@
/// 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 keyboard.h
/// @desc A class that models the functionality of a keyboard device.
/// @edit 2024-07-29
#pragma once
namespace ReWindow
{
class InputDevice {};
class Keyboard : public InputDevice
{
};
}

View File

@@ -0,0 +1,26 @@
/// 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 keyboard.h
/// @desc A class that models the functionality of a mouse / pointer device.
/// @edit 2024-07-29
#pragma once
namespace ReWindow
{
class InputDevice {}; // TODO: Remember to break InputDevice into it's own file and not define it twice!!!
class Pointer : public InputDevice {};
class Mouse : public Pointer
{
};
}

View File

@@ -0,0 +1,38 @@
/// 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 mousebutton.hpp
/// @desc MouseButton class and defined MouseButtons.
/// @edit 2024-07-29
#pragma once
class MouseButton {
public:
MouseButton();
explicit MouseButton(const char* charcode, unsigned int index);
const char* CharCode;
unsigned int ButtonIndex;
bool operator == (const MouseButton& mb) const;
};
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};
}
MouseButton GetMouseButtonFromXButton(unsigned int button);

View File

@@ -2,25 +2,13 @@
#include <cstdint>
#include <vector>
#include <Event.h>
#include <functional>
#include <map>
#include <thread>
#include <rewindow/types/key.h>
#if WIN32
#include <windows.h>
#include <windowsx.h>
#include <wingdi.h>
#endif
#if __linux__
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <GL/gl.h>
#include <GL/glx.h>
#endif
#include <rewindow/types/cursors.h>
#include <rewindow/types/mousebutton.h>
#include <rewindow/types/gamepadbutton.h>
#include <J3ML/LinearAlgebra.hpp>
using namespace std::chrono_literals;
using precision_clock = std::chrono::high_resolution_clock;
@@ -132,12 +120,23 @@ namespace ReWindow
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.
///
class RWindow {
public:
/// We keep and support both mechanisms for extending behavior to suit:
/// 1. Derived windows with added functionality.
/// 2. Binding functions to a pre-existing window.
#pragma region Callbacks
/// Bindable Non-intrusive event handlers
/// Use these when you can't override the base window class
Event<> OnOpenEvent;
Event<> OnClosingEvent;
Event<RWindowEvent> OnFocusLostEvent;
Event<RWindowEvent> OnFocusGainEvent;
Event<float> OnRefreshEvent;
@@ -148,8 +147,14 @@ namespace ReWindow
Event<MouseButtonDownEvent> OnMouseButtonDownEvent;
Event<MouseButtonUpEvent> OnMouseButtonUpEvent;
#pragma endregion
#pragma region Overrides
/// Intrusive virtual methods intended to be overridden in a derived class.
/// Do not stuff any logic into these. Someone WILL override it and forget to call the base.
/// Called upon the window requesting to open.
virtual void OnOpen() {}
/// Called right before the window closes.
virtual void OnClosing() {}
virtual void OnFocusLost(const RWindowEvent& e) {}
virtual void OnFocusGain(const RWindowEvent& e) {}
virtual void OnRefresh(float elapsed) {}
@@ -162,47 +167,44 @@ namespace ReWindow
virtual void OnMouseButtonUp(const MouseButtonUpEvent&) {}
#pragma endregion
/// The default constructor sets a default size and window title.
RWindow();
/// Constructs a window by explicitly setting title, width and height.
RWindow(const std::string& title, int width, int height);
/// Constructs a window as above with the additional argument of explicitly setting which render API is to be used.
RWindow(const std::string& title, int width, int height, RenderingAPI renderer);
Vector2 GetMouseCoordinates() const {
return getCursorPos();
}
/// Returns a Vector2 representing mouse coordinates relative to the top-left corner of the window.
Vector2 GetMouseCoordinates() const;
void liftKey (Key key) {
currentKeyboard.PressedKeys[key] = false;
auto event = ReWindow::WindowEvents::KeyUpEvent(key);
OnKeyUp(event);
}
// TODO: Is this part of the API interface? Can it be moved to protected?
void liftKey (Key key);
void pressKey (Key key) {
currentKeyboard.PressedKeys[key] = true;
auto eventData = KeyDownEvent(key);
OnKeyDown(eventData);
}
#ifndef __linux___
void setRect (int nx, int ny, int nw, int nh) {
setPos(nx, ny);
width = nw;
height = nh;
}
#endif
// TODO: Is this part of the API interface? Can it be moved to protected?
void pressKey (Key key);
/// Sets which rendering API is to be used with this window.
void setRenderer(RenderingAPI api);
/// Initializes all state with the window manager and rendering API, then opens the window.
void Open();
/// Cleans up and closes the window without destroying the handle.
void Close();
void CloseAndReopenInPlace();
// TODO: Must be implemented from scratch as a Motif Window in x11
void MessageBox();
void MessageBox(); // TODO: Must be implemented from scratch as a Motif Window in x11
/// Returns whether the window currently has mouse and/or keyboard focus.
bool isFocused() const;
bool isFullscreen() const;
bool isResizable() const;
bool isVsyncEnabled() const;
bool isAlive() const;
bool isAlive() const; // TODO: Always returns true.
/// Sets whether the mouse is visible when inside the window.
void setMouseVisible(bool visible);
void setMouseLocked();
void setMouseCenter();
@@ -218,28 +220,57 @@ namespace ReWindow
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
// TODO: Move out of public API, consumers should use setFullscreen()
void fullscreen();
// TODO: Move out of public API, consumers should use setFullscreen()
void restoreFromFullscreen();
// TODO: Josh hates parameter-flags, it's not 1995 :/
bool getFlag(RWindowFlags flag) const;
// TODO: Josh hates parameter-flags, it's not 1995 :/
void setFlag(RWindowFlags flag, bool state);
//void init(RenderingAPI api, const char* title, int width, int height, bool vsync);
/// Tells the underlying window manager to destroy this window and drop the handle.
/// The window, in theory, can not be re-opened after this.
/// TODO: Create a destructor and move to there?
/// TODO: What's the semantic difference between this and Close()?
void destroyWindow();
/// Reads events from the underlying window manager.
/// TODO: Move out of public API, consumers should call refresh or ideally an update() call.
void pollEvents();
/// Updates the window and handles timing internally.
void refresh();
void setSize(int width, int height);
void setSize(const Vector2& size);
/// Returns the position of the window's top-left corner relative to the display
Vector2 getPos() const;
Vector2 getSize() const; // I want to know why this is made platform specific. Is that even necessary? -maxine
// 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;
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;
/// 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];
@@ -248,14 +279,12 @@ namespace ReWindow
KeyboardState previousKeyboard; // Previous Frame's Keyboard State
bool fullscreenmode = false;
std::string title;
#ifndef __linux__
int x;
int y;
#endif
int width;
int height;
RenderingAPI renderer;
bool open = false;
bool resizable;
};
};
}

View File

@@ -2,6 +2,12 @@
#include <rewindow/types/window.h>
#include <jlog/jlog.hpp>
#if _WIN32
#include <windows.h>
#endif
#include <GL/gl.h>
Vector2 mouse_pos;
@@ -19,6 +25,8 @@ class MyWindow : public ReWindow::RWindow {
void OnKeyDown(const ReWindow::KeyDownEvent& e) override {}
void OnRefresh(float elapsed) override {
glClearColor(255, 0, 0, 255);
glClear(GL_COLOR_BUFFER_BIT);
glSwapBuffers();
auto pos = getCursorPos();
//std::cout << pos.x << ", " << pos.y << std::endl;
@@ -45,7 +53,7 @@ int main() {
DEBUG("TODO: Cannot set flags until after window is open")
window->setFullscreen(false);
window->setVsyncEnabled(false);
window->setResizable(false);
window->setResizable(true);
DEBUG(std::format("Window '{}' flags: IN_FOCUS={} FULLSCREEN={} RESIZEABLE={} VSYNC={} QUIT={}",
window->getTitle(),
window->getFlag(RWindowFlags::IN_FOCUS),

View File

@@ -0,0 +1 @@
Main:new("Install build dependencies", "apt-get install -yq libx11-6 libx11-dev libgl-dev")

View File

@@ -1,13 +1,13 @@
#include <rewindow/types/window.h>
#include <rewindow/types/cursors.h>
#include <J3ML/J3ML.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <GL/glx.h>
#include <cstdlib>
#include <cstring>
#include <thread>
#include <rewindow/types/window.h>
#include <rewindow/types/cursors.h>
#include <J3ML/J3ML.hpp>
#include <jlog/jlog.hpp>
@@ -31,19 +31,6 @@ Atom windowTypeUtilityAtom;
XSizeHints hints;
GLXContext glContext;
std::string RWindowFlagToStr(RWindowFlags flag)
{
switch (flag)
{
case RWindowFlags::IN_FOCUS: return "IN_FOCUS";
case RWindowFlags::FULLSCREEN: return "FULLSCREEN";
case RWindowFlags::RESIZABLE: return "RESIZEABLE";
case RWindowFlags::VSYNC: return "VSYNC";
case RWindowFlags::QUIT: return "QUIT";
case RWindowFlags::MAX_FLAG: return "MAX_FLAG";
}
};
using namespace ReWindow;
void RWindow::raise() const {
@@ -60,9 +47,9 @@ void RWindow::lower() const
void RWindow::destroyWindow() {
DEBUG(std::format("Destroying window '{}'", this->title));
XDestroySubwindows(display, window);
DEBUG(std::format("Destroyed window '{}'", this->title));
XDestroyWindow(display, window);
delete this;
DEBUG(std::format("Destroyed window '{}'", this->title));
}
void RWindow::refresh() {
@@ -74,7 +61,8 @@ void RWindow::refresh() {
auto eventData = MouseMoveEvent(mouse_coords);
OnMouseMove(eventData);
std::this_thread::sleep_for(1ms);
OnMouseMoveEvent(eventData);
//std::this_thread::sleep_for(1ms);
}
void RWindow::setFlag(RWindowFlags flag, bool state) {
@@ -97,11 +85,11 @@ void RWindow::pollEvents() {
if (xev.type == ClientMessage)
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();
system("xset r on");
exit(0);
}
if (xev.xclient.message_type == XInternAtom(display, "WM_PROTOCOLS", False) && static_cast<Atom>(xev.xclient.data.l[0]) == wmDeleteWindow) {
destroyWindow();
system("xset r on");
exit(0);
}
if (xev.type == FocusIn) {
DEBUG(std::format("Recieved event '{}'", "FocusIn"));
@@ -109,250 +97,264 @@ void RWindow::pollEvents() {
//focusGained.Invoke();
RWindowEvent event {};
OnFocusGain(event);
OnFocusGainEvent(event);
setFlag(RWindowFlags::IN_FOCUS, true);
}
if (xev.type == FocusOut) {
DEBUG(std::format("Recieved event '{}'", "FocusOut"));
XAutoRepeatOn(display);
//focusLost.Invoke();
RWindowEvent event {};
OnFocusLost(event);
setFlag(RWindowFlags::IN_FOCUS, false);
}
if (xev.type == KeyRelease) {
DEBUG(std::format("Recieved event '{}'", "KeyRelease"));
auto scancode = (X11Scancode) xev.xkey.keycode;
auto key = GetKeyFromX11Scancode(scancode);
OnKeyUpEvent(key);
OnKeyUpEvent(key);
liftKey(key);
}
if (xev.type == KeyPress) {
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);
}
if (xev.type == ButtonRelease) {
DEBUG(std::format("Recieved event '{}'", "ButtonRelease"));
MouseButton button = GetMouseButtonFromXButton(xev.xbutton.button);
auto eventData = MouseButtonUpEvent();
eventData.Button = button;
OnMouseButtonUp(eventData);
}
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);
}
if (xev.type == Expose)
{
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("NOTE: This event is functionally useless, as it only informs of the very beginning and end of a mouse movement.");
DEBUG(std::format("Recieved event '{}'", "MotionNotify"));
}
if (xev.type == ResizeRequest) {
DEBUG(std::format("Recieved event '{}'", "ResizeRequest"));
auto eventData = WindowResizeRequestEvent();
lastKnownWindowSize = eventData.Size;
eventData.Size = {(float)xev.xresizerequest.width, (float)xev.xresizerequest.height};
lastKnownWindowSize = eventData.Size;
OnResizeRequest(eventData);
}
XAutoRepeatOn(display);
//focusLost.Invoke();
RWindowEvent event {};
OnFocusLost(event);
OnFocusLostEvent(event);
setFlag(RWindowFlags::IN_FOCUS, false);
}
previousKeyboard = currentKeyboard;
}
// Might make the window go off the screen on some window managers.
void RWindow::setSize(int newWidth, int newHeight) {
if (!getFlag(RWindowFlags::RESIZABLE)) return;
this->width = newWidth;
this->height = newHeight;
XResizeWindow(display, window, width, height);
DEBUG("Might make the window go off the screen on some window managers.");
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;
uint32_t mask_return;
unsigned m;
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;
}
return Vector2::Zero;
}
// TODO: implement integer vector2/3 types
Vector2 RWindow::getSize() const {
XGetWindowAttributes(display,window,&windowAttributes);
return {(float)windowAttributes.width, (float)windowAttributes.height};
}
// TODO: implement integer vector2/3 types
Vector2 RWindow::getPos() const {
XGetWindowAttributes(display,window,&windowAttributes);
return {(float)windowAttributes.x, (float)windowAttributes.y};
}
void RWindow::setPos(int x, int y) {
XMoveWindow(display, window, x, y);
}
void RWindow::setPos(const Vector2& pos) {
this->setPos(pos.x, pos.y);
}
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;
XEvent xev;
Atom wm_state = XInternAtom(display, "_NET_WM_STATE", true);
Atom wm_fullscreen = XInternAtom(display, "_NET_WM_STATE_FULLSCREEN", true);
XChangeProperty(display, window, wm_state, XA_ATOM, 32, PropModeReplace, (unsigned char *)&wm_fullscreen, 1);
memset(&xev, 0, sizeof(xev));
xev.type = ClientMessage;
xev.xclient.window = window;
xev.xclient.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[2] = 0;
XSendEvent(display, DefaultRootWindow(display), False, SubstructureNotifyMask | SubstructureRedirectMask, &xev);
DEBUG(std::format("Fullscreened window '{}'", this->title));
}
void RWindow::restoreFromFullscreen() {
DEBUG(std::format("Restoring window '{}' from fullscreen", this->title));
fullscreenmode = false;
XEvent xev;
Atom wm_state = XInternAtom(display, "_NET_WM_STATE", False);
Atom fullscreen = XInternAtom(display, "_NET_WM_STATE_FULLSCREEN", False);
memset(&xev, 0, sizeof(xev));
xev.type = ClientMessage;
xev.xclient.window = window;
xev.xclient.message_type = wm_state;
xev.xclient.format = 32;
xev.xclient.data.l[0] = 0; // _NET_WM_STATE_REMOVE
xev.xclient.data.l[1] = fullscreenmode;
xev.xclient.data.l[2] = 0;
XSendEvent(display, DefaultRootWindow(display), False, SubstructureNotifyMask | SubstructureRedirectMask, &xev);
DEBUG(std::format("Restored window '{}' from fullscreen", this->title));
}
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 {
u32 x11_cursor_resolved_enum = static_cast<u32>(style.X11Cursor);
Cursor c = XCreateFontCursor(display, x11_cursor_resolved_enum);
XDefineCursor(display, window, c);
}
void RWindow::Open() {
xSetWindowAttributes.border_pixel = BlackPixel(display, defaultScreen);
xSetWindowAttributes.background_pixel = BlackPixel(display, defaultScreen);
xSetWindowAttributes.override_redirect = True;
xSetWindowAttributes.event_mask = ExposureMask;
//setVsyncEnabled(vsync);
if (renderer == RenderingAPI::OPENGL) {
GLint glAttributes[] = {GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None};
visual = glXChooseVisual(display, defaultScreen, glAttributes);
glContext = glXCreateContext(display, visual, nullptr, GL_TRUE);
if (xev.type == KeyRelease) {
DEBUG(std::format("Recieved event '{}'", "KeyRelease"));
auto scancode = (X11Scancode) xev.xkey.keycode;
auto key = GetKeyFromX11Scancode(scancode);
OnKeyUpEvent(key);
OnKeyUp(key);
liftKey(key);
}
xSetWindowAttributes.colormap = XCreateColormap(display, RootWindow(display, defaultScreen), visual->visual, AllocNone);
if (xev.type == KeyPress) {
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);
}
window = XCreateWindow(display, RootWindow(display, defaultScreen), 0, 0, width, height, 0, visual->depth,
InputOutput, visual->visual, CWBackPixel | CWColormap | CWBorderPixel | NoEventMask,
&xSetWindowAttributes);
// Set window to floating because fucking tiling WMs
windowTypeAtom = XInternAtom(display, "_NET_WM_WINDOW_TYPE", False);
windowTypeUtilityAtom = XInternAtom(display, "_NET_WM_WINDOW_TYPE_UTILITY", False);
XChangeProperty(display, window, windowTypeAtom, XA_ATOM, 32, PropModeReplace,
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);
}
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);
}
if (xev.type == Expose)
{
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"));
}
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);
}
}
previousKeyboard = currentKeyboard;
}
// Might make the window go off the screen on some window managers.
void RWindow::setSize(int newWidth, int newHeight) {
if (!getFlag(RWindowFlags::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));
}
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;
uint32_t mask_return;
unsigned m;
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;
}
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::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};
}
void RWindow::setPos(int x, int y) {
XMoveWindow(display, window, x, y);
}
void RWindow::setPos(const Vector2& pos) {
this->setPos(pos.x, pos.y);
}
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;
XEvent xev;
Atom wm_state = XInternAtom(display, "_NET_WM_STATE", true);
Atom wm_fullscreen = XInternAtom(display, "_NET_WM_STATE_FULLSCREEN", true);
XChangeProperty(display, window, wm_state, XA_ATOM, 32, PropModeReplace, (unsigned char *)&wm_fullscreen, 1);
memset(&xev, 0, sizeof(xev));
xev.type = ClientMessage;
xev.xclient.window = window;
xev.xclient.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[2] = 0;
XSendEvent(display, DefaultRootWindow(display), False, SubstructureNotifyMask | SubstructureRedirectMask, &xev);
DEBUG(std::format("Fullscreened window '{}'", this->title));
}
void RWindow::restoreFromFullscreen() {
DEBUG(std::format("Restoring window '{}' from fullscreen", this->title));
fullscreenmode = false;
XEvent xev;
Atom wm_state = XInternAtom(display, "_NET_WM_STATE", False);
Atom fullscreen = XInternAtom(display, "_NET_WM_STATE_FULLSCREEN", False);
memset(&xev, 0, sizeof(xev));
xev.type = ClientMessage;
xev.xclient.window = window;
xev.xclient.message_type = wm_state;
xev.xclient.format = 32;
xev.xclient.data.l[0] = 0; // _NET_WM_STATE_REMOVE
xev.xclient.data.l[1] = fullscreenmode;
xev.xclient.data.l[2] = 0;
XSendEvent(display, DefaultRootWindow(display), False, SubstructureNotifyMask | SubstructureRedirectMask, &xev);
DEBUG(std::format("Restored window '{}' from fullscreen", this->title));
}
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 {
u32 x11_cursor_resolved_enum = static_cast<u32>(style.X11Cursor);
Cursor c = XCreateFontCursor(display, x11_cursor_resolved_enum);
XDefineCursor(display, window, c);
}
void RWindow::Open() {
xSetWindowAttributes.border_pixel = BlackPixel(display, defaultScreen);
xSetWindowAttributes.background_pixel = BlackPixel(display, defaultScreen);
xSetWindowAttributes.override_redirect = True;
xSetWindowAttributes.event_mask = ExposureMask;
//setVsyncEnabled(vsync);
if (renderer == RenderingAPI::OPENGL) {
GLint glAttributes[] = {GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None};
visual = glXChooseVisual(display, defaultScreen, glAttributes);
glContext = glXCreateContext(display, visual, nullptr, GL_TRUE);
}
xSetWindowAttributes.colormap = XCreateColormap(display, RootWindow(display, defaultScreen), visual->visual, AllocNone);
window = XCreateWindow(display, RootWindow(display, defaultScreen), 0, 0, width, height, 0, visual->depth,
InputOutput, visual->visual, CWBackPixel | CWColormap | CWBorderPixel | NoEventMask,
&xSetWindowAttributes);
// Set window to floating because fucking tiling WMs
windowTypeAtom = XInternAtom(display, "_NET_WM_WINDOW_TYPE", False);
windowTypeUtilityAtom = XInternAtom(display, "_NET_WM_WINDOW_TYPE_UTILITY", False);
XChangeProperty(display, window, windowTypeAtom, XA_ATOM, 32, PropModeReplace,
(unsigned char *)&windowTypeUtilityAtom, 1);
//
XSelectInput(display, window,
ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
PointerMotionMask |
PointerMotionHintMask | FocusChangeMask | StructureNotifyMask | SubstructureRedirectMask |
SubstructureNotifyMask | CWColormap | ResizeRequest | ResizeRedirectMask);
XMapWindow(display, window);
XStoreName(display, window, title.c_str());
wmDeleteWindow = XInternAtom(display, "WM_DELETE_WINDOW", False);
XSetWMProtocols(display, window, &wmDeleteWindow, 1);
//
XSelectInput(display, window,
ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
PointerMotionMask |
PointerMotionHintMask | FocusChangeMask | StructureNotifyMask | SubstructureRedirectMask |
SubstructureNotifyMask | CWColormap | ResizeRequest);
XMapWindow(display, window);
XStoreName(display, window, title.c_str());
wmDeleteWindow = XInternAtom(display, "WM_DELETE_WINDOW", False);
XSetWMProtocols(display, window, &wmDeleteWindow, 1);
if (renderer == RenderingAPI::OPENGL)
glXMakeCurrent(display, window, glContext);
open = true;
}
if (renderer == RenderingAPI::OPENGL)
glXMakeCurrent(display, window, glContext);
open = true;
}
void RWindow::setTitle(const std::string &title) {
this->title = title;
XStoreName(display, window, title.c_str());
}
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;
}
// TODO: Implement MouseButton map
bool RWindow::isMouseButtonDown(MouseButton button) const {
return false;
}
// TODO: Implement ControllerButton map
// TODO: Implement ControllerButton map

View File

@@ -1,4 +1,18 @@
#include <rewindow/types/window.h>
#include <jlog/jlog.hpp>
std::string RWindowFlagToStr(RWindowFlags flag) {
switch (flag) {
case RWindowFlags::IN_FOCUS: return "IN_FOCUS";
case RWindowFlags::FULLSCREEN: return "FULLSCREEN";
case RWindowFlags::RESIZABLE: return "RESIZEABLE";
case RWindowFlags::VSYNC: return "VSYNC";
case RWindowFlags::QUIT: return "QUIT";
case RWindowFlags::MAX_FLAG: return "MAX_FLAG";
default:
FATAL("")
return "unimplemented flag";
}
};
using namespace ReWindow;
@@ -19,6 +33,10 @@ RWindow::RWindow(const std::string& title, int width, int height) : flags{false,
RWindow::RWindow(const std::string& title, int width, int height, RenderingAPI renderer) :
title(title), width(width), height(height), renderer(renderer) {}
Vector2 RWindow::GetMouseCoordinates() const {
return getCursorPos();
}
bool RWindow::getFlag(RWindowFlags flag) const {return flags[(int)flag];}
bool RWindow::isAlive() const {
@@ -37,6 +55,13 @@ void RWindow::setFullscreen(bool fs) {
restoreFromFullscreen();
}
void RWindow::liftKey(Key key) {
currentKeyboard.PressedKeys[key] = false;
auto event = ReWindow::WindowEvents::KeyUpEvent(key);
OnKeyUp(event);
}
std::string RWindow::getTitle() const {
return this->title;
}
@@ -74,4 +99,16 @@ bool RWindow::isKeyDown(Key key) const {
if (pair.first == key && pair.second)
return true;
return false;
}
void RWindow::pressKey(Key key) {
currentKeyboard.PressedKeys[key] = true;
auto eventData = KeyDownEvent(key);
OnKeyDown(eventData);
}
void RWindow::Close()
{
}

View File

@@ -1,5 +1,6 @@
#include <Windows.h>
//#include <gl/GL.h>
#include <rewindow/types/window.h>
#include <gl/GL.h>
using namespace ReWindow;
@@ -18,7 +19,9 @@ void RWindow::setFlag(RWindowFlags flag, bool state) {
if (flag == RWindowFlags::RESIZABLE && !state) {
RECT rect;
GetWindowRect(hwnd, &rect);
SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_THICKFRAME);
LONG style = GetWindowLong(hwnd, GWL_STYLE);
style &= ~(WS_THICKFRAME | WS_MAXIMIZEBOX);
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);
}
}
@@ -33,7 +36,6 @@ void RWindow::pollEvents() {
void RWindow::setSize(int newWidth, int newHeight) {
if (!getFlag(RWindowFlags::RESIZABLE)) return;
this->width = newWidth;
this->height = newHeight;
SetWindowPos(hwnd, nullptr, 0, 0, width, height, SWP_NOMOVE | SWP_NOZORDER);
@@ -138,7 +140,6 @@ void RWindow::Open() {
wc.hInstance = hInstance;
wc.lpszClassName = "RWindowClass";
RegisterClass(&wc);
hwnd = CreateWindowEx(
0,
"RWindowClass",

View File

@@ -0,0 +1 @@
// Base Window Implementation - Anything that is consistent across Windows, Linux, etc

View File

@@ -0,0 +1,9 @@
#include <rewindow/types/gamepadbutton.h>
bool GamepadButton::operator==(const GamepadButton &rhs) const {
return this->GetMnemonicButtonCode() == rhs.GetMnemonicButtonCode();
}
void GamepadThumbstick::SetDeadzone(float minimum) const {
}

View File

@@ -1,5 +1,5 @@
#include <rewindow/types/key.h>
#include <memory>
#include <jlog/jlog.hpp>
//std::vector<Key> Key::keyboard = {};
std::vector<Key> Key::GetKeyboard() { return keyboard; }
@@ -15,12 +15,7 @@ Key::Key(const char* charcode, X11Scancode scancode, WindowsScancode sc)
keyboard.push_back(*this);
}
MouseButton::MouseButton() {
}
MouseButton::MouseButton(const char* charcode) {
this->CharCode = charcode;
}
bool Key::operator==(const Key &rhs) const {
//This is not a good workaround.
@@ -29,4 +24,23 @@ bool Key::operator==(const Key &rhs) const {
bool Key::operator<(const Key &rhs) const {
return (this->CharCode < rhs.CharCode);
}
}
Key GetKeyFromX11Scancode(X11Scancode code) {
for (auto& key : Key::GetKeyboard())
if (key.x11ScanCode == code)
return key;
std::cout << "Unavaliable Scancode: " + std::to_string((int)code) << std::endl;
return Keys::Space;
}
Key GetKeyFromWindowsScancode(WindowsScancode code) {
for (const auto& key : Key::GetKeyboard())
if (key.winScanCode == code)
return key;
std::cout << "Unavaliable Scancode: " + std::to_string((int) code) << std::endl;
return Keys::Space;
}

33
src/types/mousebutton.cpp Normal file
View File

@@ -0,0 +1,33 @@
#include <rewindow/types/mousebutton.h>
#include <string>
#include <jlog/jlog.hpp>
MouseButton::MouseButton() {
}
MouseButton::MouseButton(const char* charcode, unsigned int index) {
this->CharCode = charcode;
this->ButtonIndex = index;
}
bool MouseButton::operator==(const MouseButton &mb) const {
return (mb.ButtonIndex == this->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;
//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));
return MouseButtons::Unimplemented;
}
}
}