Compare commits

...

5 Commits

Author SHA1 Message Date
62568e3252 Update window.cpp 2024-01-24 22:09:58 -05:00
668d7869fe Update window.cpp
Fixed a problem that'd cause setting vsync to behave strangely and overlays to crash.
2024-01-24 06:22:34 -05:00
7320f07d0e Fix 2024-01-22 04:29:50 -05:00
364504ae5c update 2024-01-21 12:58:38 -05:00
0d3e714f52 update 2024-01-21 05:15:24 -05:00
10 changed files with 214 additions and 620 deletions

View File

@@ -18,15 +18,9 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
# Enable Package Managers
include(cmake/CPM.cmake)
CPMAddPackage(
NAME J3ML
URL https://git.redacted.cc/josh/j3ml/archive/Prerelease-7.zip
)
# include(cmake/CPM.cmake)
find_package(OpenGL REQUIRED)
include_directories({$OPENGL_INCLUDE_DIRS} ${J3ML_SOURCE_DIR}/include)
include_directories({$OPENGL_INCLUDE_DIRS})
file(GLOB_RECURSE HEADERS "include/*.h" "include/*.hpp")
if(UNIX AND NOT APPLE)
@@ -38,14 +32,12 @@ if(WIN32)
endif()
include_directories("include")
add_library(ReWindowLibrary SHARED ${SOURCES}
include/rewindow/data/X11Scancodes.h
include/rewindow/types/key.h)
add_library(ReWindowLibrary SHARED ${SOURCES})
# Why god???
set_target_properties(ReWindowLibrary PROPERTIES LINKER_LANGUAGE CXX)
if(UNIX AND NOT APPLE)
target_link_libraries(ReWindowLibrary PUBLIC X11 J3ML ${OPENGL_LIBRARIES})
target_link_libraries(ReWindowLibrary PUBLIC X11 ${OPENGL_LIBRARIES})
add_executable(ReWindowLibraryDemo main.cpp)
target_link_libraries(ReWindowLibraryDemo PUBLIC ReWindowLibrary)
endif()

View File

@@ -2,7 +2,7 @@
# ReWindow
[![License: Unlicense](https://img.shields.io/badge/license-Unlicense-blue.svg)](http://unlicense.org/)
A library which allows easily creating and managing a window and it's eventLog across multiple platforms *primarily for games*.
A library which allows easily creating and managing a window and it's events across multiple platforms *primarily for games*.
## Run Demo

View File

@@ -1,24 +0,0 @@
# SPDX-License-Identifier: MIT
#
# SPDX-FileCopyrightText: Copyright (c) 2019-2023 Lars Melchior and contributors
set(CPM_DOWNLOAD_VERSION 0.38.7)
set(CPM_HASH_SUM "83e5eb71b2bbb8b1f2ad38f1950287a057624e385c238f6087f94cdfc44af9c5")
if(CPM_SOURCE_CACHE)
set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
elseif(DEFINED ENV{CPM_SOURCE_CACHE})
set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
else()
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
endif()
# Expand relative path. This is important if the provided path contains a tilde (~)
get_filename_component(CPM_DOWNLOAD_LOCATION ${CPM_DOWNLOAD_LOCATION} ABSOLUTE)
file(DOWNLOAD
https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
${CPM_DOWNLOAD_LOCATION} EXPECTED_HASH SHA256=${CPM_HASH_SUM}
)
include(${CPM_DOWNLOAD_LOCATION})

View File

@@ -1,106 +0,0 @@
#pragma once
enum class X11Scancode {
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,
END = 115,
INSERT = 118,
DELETE = 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,
FUNCTION_ONE = 67,
FUNCTION_TWO = 68,
FUNCTION_THREE = 69,
FUNCTION_FOUR = 70,
FUNCTION_FIVE = 71,
FUNCTION_SIX = 72,
FUNCTION_SEVEN = 73,
FUNCTION_EIGHT = 74,
FUNCTION_NINE = 75,
FUNCTION_TEN = 76,
FUNCTION_ELEVEN = 77,
FUNCTION_TWELVE = 78,
PRINT = 107,
SCROLL_LOCK = 78,
BREAK = 127,
};

View File

@@ -1,92 +1,146 @@
#pragma once
#include <chrono>
#include <functional>
#if __linux__
#include <X11/Xlib.h>
#include <chrono>
#endif
//This is also the x11 scancodes.
template <typename ... Args>
class Event;
template <typename ... Args>
class EventConnection {
private:
using delegate = std::function<void(Args...)>;
public:
EventConnection(Event<Args...> *creator, delegate cb) : owner(creator), callback(std::move(cb)) {}
bool Disconnect(); // Breaks the event connection, but does not destroy the instance
void Invoke(Args... e);
private:
Event<Args...> * owner;
delegate callback;
bool active = true;
enum class SCANCODE {
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,
END = 115,
INSERT = 118,
DELETE = 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,
FUNCTION_ONE = 67,
FUNCTION_TWO = 68,
FUNCTION_THREE = 69,
FUNCTION_FOUR = 70,
FUNCTION_FIVE = 71,
FUNCTION_SIX = 72,
FUNCTION_SEVEN = 73,
FUNCTION_EIGHT = 74,
FUNCTION_NINE = 75,
FUNCTION_TEN = 76,
FUNCTION_ELEVEN = 77,
FUNCTION_TWELVE = 78,
PRINT = 107,
SCROLL_LOCK = 78,
BREAK = 127,
};
template <typename ... Args>
class Event {
public:
using delegate = std::function<void(Args...)>;
using connection = EventConnection<Args ...>;
using event_ptr = std::shared_ptr<connection>;
public:
void Await(Args& ... arg);
void Invoke(Args... args);
void operator()(Args... args);
connection Connect(delegate callback);
void Disconnect(connection &conn);
connection operator+=(delegate callback);
private:
std::vector<event_ptr> listeners;
uint64_t listenerCounter = 0;
enum class MOUSEBUTTONCODE {
LEFT_CLICK = 1,
MIDDLE_CLICK = 2,
RIGHT_CLICK = 3,
SCROLL_UP = 4,
SCROLL_DOWN = 5,
SIDE_FORWARD = 9,
SIDE_BACK = 8
};
template<typename... Args>
EventConnection<Args...> Event<Args...>::operator+=(Event::delegate callback) { return Connect(callback); }
template<typename... Args>
void Event<Args...>::operator()(Args... args) { Invoke(args...);}
template <typename... Args>
void EventConnection<Args...>::Invoke(Args... e) { callback(e...); }
template <typename ... Args>
bool EventConnection<Args...>::Disconnect() {
if (active) {
owner->Disconnect(this);
active = false;
return true;
class RWindowEvent {
private:
std::chrono::high_resolution_clock::time_point timePoint;
public:
std::chrono::high_resolution_clock::time_point timeStamp();
virtual ~RWindowEvent() = default;
virtual bool empty();
RWindowEvent() {
timePoint = std::chrono::high_resolution_clock::now();
}
return false;
}
};
const RWindowEvent EmptyRWindowEvent;
template <typename ... Args>
void Event<Args...>::Invoke(Args... args) {
for (event_ptr &connection_ptr: this->listeners) {
connection_ptr->Invoke(args...);
}
}
template <typename ... Args>
EventConnection<Args...> Event<Args...>::Connect(delegate callback)
{
event_ptr retval(new connection(this, callback));
this->listeners.push_back(retval);
return *retval;
}
template <typename ... Args>
void Event<Args...>::Disconnect(connection &conn) {
listeners.erase(std::remove(listeners.begin(), listeners.end(), 99), listeners.end());
}
class KeyDownEvent : public RWindowEvent {
public:
SCANCODE key;
};
const KeyDownEvent EmptyKeyDownEvent{};
class MouseButtonDownEvent : public RWindowEvent {
public:
MOUSEBUTTONCODE button;
};
const MouseButtonDownEvent EmptyMouseButtonDownEvent{};

View File

@@ -1,125 +0,0 @@
//
// ~DAWSH
#pragma once
#include <vector>
#include <J3ML/LinearAlgebra/Vector2.h>
#include "rewindow/data/X11Scancodes.h"
using Vector2 = LinearAlgebra::Vector2;
class Key
{
private:
static std::vector<Key> keyboard;
public:
inline static std::vector<Key> GetKeyboard();
Key();
Key(char charcode, X11Scancode scancode);
char CharCode;
X11Scancode x11ScanCode;
};
namespace Keys {
static const Key Escape;
static const Key F1;
static const Key F2;
static const Key F3;
static const Key F4;
static const Key F5;
static const Key F6;
static const Key F7;
static const Key F8;
static const Key F9;
static const Key F10;
static const Key F11;
static const Key F12;
static const Key NumPad1;
static const Key NumPad2;
static const Key NumPad3;
static const Key NumPad4;
static const Key NumPad5;
static const Key NumPad6;
static const Key NumPad7;
static const Key NumPad8;
static const Key NumPad9;
static const Key NumPad0;
static const Key Q {'Q', X11Scancode::Q};
static const Key W {'W', X11Scancode::W};
static const Key E {'E', X11Scancode::E};
static const Key R {'R', X11Scancode::R};
static const Key T;
static const Key Y;
static const Key U;
static const Key I;
static const Key O;
static const Key P;
static const Key A;
static const Key S;
static const Key D;
static const Key F;
static const Key G;
static const Key H;
static const Key J;
static const Key K;
static const Key L;
static const Key Semicolon;
static const Key Comma;
static const Key Enter;
static const Key Minus;
static const Key Equals;
static const Key Tilde;
static const Key Z;
static const Key X;
static const Key C;
static const Key V;
static const Key B;
static const Key N;
static const Key M;
static const Key Period;
static const Key Slash;
static const Key LShift;
static const Key RShift;
}
class GamepadButton {};
class MouseButton {};
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;
static const MouseButton Right;
static const MouseButton Middle;
static const MouseButton Side1;
static const MouseButton Side2;
}
static Key GetKeyFromX11Scancode(X11Scancode code)
{
for (auto& key : Key::GetKeyboard()) {
if (key.x11ScanCode == code)
return key;
}
}

View File

@@ -2,26 +2,20 @@
#include <cstdint>
#include <vector>
#include <memory>
#include <rewindow/types/event.h>
#include "event.h"
#if __linux__
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <GL/gl.h>
#include <GL/glx.h>
#include <functional>
#include <rewindow/data/X11Scancodes.h>
#include <map>
#include "key.h"
#endif
enum class RWindowFlags: uint8_t {
IN_FOCUS = 0,
FULLSCREEN = 1,
RESIZABLE = 2,
VSYNC = 4
VSYNC = 3
};
enum class RenderingAPI: uint8_t {
@@ -30,117 +24,25 @@ enum class RenderingAPI: uint8_t {
VULKAN = 1,
};
using namespace std::chrono_literals;
using precision_clock = std::chrono::high_resolution_clock;
using precision_timestamp = precision_clock::time_point;
class TimestampedEvent {
private:
precision_timestamp timestamp;
public:
TimestampedEvent() : timestamp(precision_clock::now())
{ }
precision_timestamp GetTimestamp() const { return timestamp;}
};
class RWindowEvent : public TimestampedEvent {
public:
RWindowEvent() : TimestampedEvent() { }
};
const RWindowEvent EmptyRWindowEvent;
enum class KeyState { Pressed, Released };
class KeyboardState {
public:
std::map<X11Scancode, 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 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 RWindowEvent {
public:
MouseButton button;
};
const MouseButtonDownEvent EmptyMouseButtonDownEvent{};
class RWindow {
private:
bool flags[4];
std::vector<RWindowEvent> eventLog;
KeyboardState currentKeyboard; // Current Frame's Keyboard State
KeyboardState previousKeyboard; // Previous Frame's Keyboard State
std::vector<RWindowEvent*> events;
public:
Event<> focusGained;
Event<> focusLost;
// TODO: Integrate J3ML
Event<LinearAlgebra::Vector2> mouseMoved;
Event<MouseButton> mouseBtnPressed;
Event<MouseButton> mouseBtnReleased;
Event<KeyDownEvent> onKeyboardPress;
Event<KeyUpEvent> onKeyboardRelease;
bool isFocused() const;
bool isFullscreen() const;
bool isResizable() const;
bool isVsyncEnabled() const;
void setMouseVisible(bool visible);
void setMouseLocked();
void setMouseCenter();
void restoreMouseFromLastCenter(); // Feels nicer for users
void setFocused(bool);
void setFullscreen(bool);
void setResizable(bool);
static void setVsyncEnabled(bool b);
bool getFlag(RWindowFlags flag);
void setFlag(RWindowFlags flag, bool state);
void init(RenderingAPI api, const char* title, int width, int height, bool vsync);
static void setVsyncEnabled(bool b);
void destroyWindow();
void pollEvents();
void setSize(int width, int height);
std::unique_ptr<int[]> getPos();
std::unique_ptr<int[]> getSize();
void setPos(int x, int y);
bool keyDown(X11Scancode scancode);
bool mouseButtonDown(MouseButton buttoncode);
KeyDownEvent getEvent(X11Scancode scancode);
MouseButtonDownEvent getEvent(MouseButton buttoncode);
bool keyDown(SCANCODE scancode);
bool mouseButtonDown(MOUSEBUTTONCODE buttoncode);
KeyDownEvent getEvent(SCANCODE scancode);
MouseButtonDownEvent getEvent(MOUSEBUTTONCODE buttoncode);
static void glSwapBuffers();
//Initialize to false because it's not guaranteed that they will be cleared first
RWindow() : flags(false,false,false,false) {}

View File

@@ -1,95 +1,27 @@
#include <iostream>
#include "include/rewindow/types/window.h"
#if __linux__
int main() {
auto* window = new(RWindow);
window->init(RenderingAPI::OPENGL, "name",1152,864, true);
window->init(RenderingAPI::OPENGL, "name",1152,864, false);
window->setFlag(RWindowFlags::RESIZABLE, false);
int i;
while (true) {
if (i <= 10)
window->pollEvents();
if (window->keyDown(X11Scancode::A)) {
window->glSwapBuffers();
i++;
std::cout << i << std::endl;
if (window->keyDown(SCANCODE::A)) {
std::cout << "A" << std::endl;
//std::cout << (int64_t) window->getEvent(X11Scancode::A).empty() << std::endl;
std::cout << (int64_t) window->getEvent(SCANCODE::A).empty() << std::endl;
}
//if (window->mouseButtonDown(MOUSEBUTTONCODE::LEFT_CLICK))
//std::cout << window->mouseButtonDown(MOUSEBUTTONCODE::LEFT_CLICK) << std::endl;
//std::cout << window->eventLog.size() << std::endl;
if (window->mouseButtonDown(MOUSEBUTTONCODE::LEFT_CLICK))
std::cout << window->mouseButtonDown(MOUSEBUTTONCODE::LEFT_CLICK) << std::endl;
//std::cout << window->events.size() << std::endl;
//window->resize(800,600);
//std::cout << window->getPos()[1] << std::endl;
//auto* d = new(KeyDownEvent);
//d->key = SCANCODE::A;
//window->eventLog.push_back(d);
//window->events.push_back(d);
}
}
#endif
#ifdef WIN32
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
// Register the window class.
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
// Create the window.
HWND hwnd = CreateWindowEx(
0,
CLASS_NAME,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, // Parent Window
NULL, // Menu
hInstance, // Instancehandle
NULL
);
if (hwnd == NULL)
return 0;
ShowWindow(hwnd, nCmdShow);
// Run the message loop
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg) {
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
// All painting occurs here, between BeginPaint and EndPaint
FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));
EndPaint(hwnd, &ps);
}
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
#endif
}

View File

@@ -1 +1,10 @@
#include <rewindow/types/event.h>
#include "../include/rewindow/types/event.h"
bool RWindowEvent::empty() {
if (timePoint == EmptyRWindowEvent.timePoint || this->timePoint == EmptyKeyDownEvent.timePoint || this->timePoint == EmptyMouseButtonDownEvent.timePoint)
return true;
return false;
}
std::chrono::high_resolution_clock::time_point RWindowEvent::timeStamp() {
return timePoint;
}

View File

@@ -1,45 +1,29 @@
#include <iostream>
#include <rewindow/types/window.h>
#include "rewindow/types/window.h"
bool vsync = false;
Window window;
XEvent xev;
Display* display = XOpenDisplay(nullptr);
int defaultScreen = DefaultScreen(display);
//Visual* visual = DefaultVisual(display,defaultScreen);
XVisualInfo* visual;
//int depth = DefaultDepth(display, defaultScreen);
XSetWindowAttributes xSetWindowAttributes;
XWindowAttributes windowAttributes;
Atom wmDeleteWindow;
XSizeHints hints;
GLXContext glContext;
PFNGLXSWAPINTERVALEXTPROC _glXSwapIntervalEXT = nullptr; //It causes issues if it's named just "glXSwapIntervalEXT".
std::vector<Key> Key::keyboard = {};
std::vector<Key> Key::GetKeyboard() { return keyboard; }
Key::Key() {
keyboard.push_back(*this);
}
Key::Key(char charcode, X11Scancode scancode)
: CharCode(charcode), x11ScanCode(scancode)
{
keyboard.push_back(*this);
}
void RWindow::init(RenderingAPI api, const char* title, int width, int height, bool vsync) {
void RWindow::init(RenderingAPI api, const char* title, int width, int height, bool sync) {
if (api == RenderingAPI::OPENGL) {
xSetWindowAttributes.border_pixel = BlackPixel(display, defaultScreen);
xSetWindowAttributes.background_pixel = BlackPixel(display, defaultScreen);
xSetWindowAttributes.override_redirect = True;
xSetWindowAttributes.event_mask = ExposureMask;
setVsyncEnabled(vsync);
GLint glAttributes[] = {GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None};
xSetWindowAttributes.border_pixel = BlackPixel(display, defaultScreen);
xSetWindowAttributes.background_pixel = BlackPixel(display, defaultScreen);
xSetWindowAttributes.override_redirect = True;
xSetWindowAttributes.event_mask = ExposureMask;
GLint glAttributes[] = {GLX_CONTEXT_MAJOR_VERSION_ARB, 1, GLX_CONTEXT_MINOR_VERSION_ARB, 4, GL_CONTEXT_COMPATIBILITY_PROFILE_BIT, GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None};
visual = glXChooseVisual(display, defaultScreen, glAttributes);
glContext = glXCreateContext(display, visual, nullptr, GL_TRUE);
vsync = sync;
xSetWindowAttributes.colormap = XCreateColormap(display, RootWindow(display, defaultScreen), visual->visual,
AllocNone);
@@ -56,12 +40,13 @@ void RWindow::init(RenderingAPI api, const char* title, int width, int height, b
wmDeleteWindow = XInternAtom(display, "WM_DELETE_WINDOW", False);
XSetWMProtocols(display, window, &wmDeleteWindow, 1);
glXMakeCurrent(display, window, glContext);
setVsyncEnabled(vsync);
} else {exit(0);}
}
void RWindow::destroyWindow() {
XDestroySubwindows(display, window);
XAutoRepeatOn(display);
XAutoRepeatOn(XOpenDisplay(nullptr));
XDestroyWindow(display, window);
delete this;
}
@@ -97,71 +82,54 @@ void RWindow::pollEvents() {
}
if (xev.type == FocusIn) {
XAutoRepeatOff(display);
focusGained.Invoke();
setFlag(RWindowFlags::IN_FOCUS, true);
}
if (xev.type == FocusOut) {
XAutoRepeatOn(display);
focusLost.Invoke();
setFlag(RWindowFlags::IN_FOCUS, false);
}
if (xev.type == KeyRelease) {
auto scancode = (X11Scancode) xev.xkey.keycode;
currentKeyboard.PressedKeys[scancode] = false;
auto key = GetKeyFromX11Scancode(scancode);
KeyUpEvent eventData = KeyUpEvent(key);
onKeyboardRelease.Invoke(eventData);
/*for (unsigned int i = 0; i < eventLog.size(); i++) {
if (auto *e = dynamic_cast<KeyDownEvent *>(eventLog[i])) {
for (unsigned int i = 0; i < events.size(); i++) {
if (auto *e = dynamic_cast<KeyDownEvent *>(events[i])) {
if ((int) e->key == (int) xev.xkey.keycode) {
delete eventLog[i];
eventLog.erase(eventLog.begin() + i);
delete events[i];
events.erase(events.begin() + i);
}
}
}*/
}
}
if (xev.type == KeyPress) {
//onKeyboardPress(xev.xkey.keycode);
//On Windows you'll have to do this the long way.
//The keycodes won't be the same :shrug:
auto scancode = (X11Scancode) xev.xkey.keycode;
auto key = GetKeyFromX11Scancode(scancode);
currentKeyboard.PressedKeys[scancode] = true;
KeyDownEvent eventData = KeyDownEvent(key);
onKeyboardPress.Invoke(eventData);
eventLog.push_back(eventData);
auto *kD = new(KeyDownEvent);
kD->key = (SCANCODE) xev.xkey.keycode;
events.push_back(kD);
}
if (xev.type == ButtonRelease) {
//auto scancode = (X11Scancode) xev.xkey.keycode;
/*for (unsigned int i = 0; i < eventLog.size(); i++) {
if (auto *e = dynamic_cast<MouseButtonDownEvent *>(eventLog[i])) {
for (unsigned int i = 0; i < events.size(); i++) {
if (auto *e = dynamic_cast<MouseButtonDownEvent *>(events[i])) {
if ((int) e->button == (int) xev.xbutton.button) {
delete eventLog[i];
eventLog.erase(eventLog.begin() + i);
delete events[i];
events.erase(events.begin() + i);
}
}
}*/
}
}
if (xev.type == ButtonPress) {
/*std::cout << (int) xev.xbutton.button << std::endl;
auto eventData = MouseButtonDownEvent(xev.xbutton.button);
//mBD->button = (MOUSEBUTTONCODE) xev.xbutton.button;
mouseBtnPressed.Invoke(eventData);
eventLog.push_back(eventData);*/
std::cout << (int) xev.xbutton.button << std::endl;
auto *mBD = new(MouseButtonDownEvent);
mBD->button = (MOUSEBUTTONCODE) xev.xbutton.button;
events.push_back(mBD);
}
if (xev.type == Expose) {
}
}
previousKeyboard = currentKeyboard;
}
//Might make the window go off the screen on some window managers.
@@ -171,10 +139,6 @@ void RWindow::setSize(int width, int height) {
XResizeWindow(display, window, width, height);
}
// TODO: use int2D, std::pair<int, int>, or vector2 instead!!!
// Prefer a simple, non-heap-allocated type
// TODO: Const-qualify this function
std::unique_ptr<int[]> RWindow::getSize() {
XGetWindowAttributes(display,window,&windowAttributes);
std::unique_ptr<int[]> size = std::make_unique<int[]>(2);
@@ -183,9 +147,7 @@ std::unique_ptr<int[]> RWindow::getSize() {
return size;
}
// TODO: use int2D, std::pair<int, int>, or vector2 instead!!!
//I'm unsure why this doesn't work as you'd expect.
// TODO: Const-qualify this function
std::unique_ptr<int[]> RWindow::getPos() {
XGetWindowAttributes(display,window,&windowAttributes);
std::unique_ptr<int[]> pos = std::make_unique<int[]>(2);
@@ -198,42 +160,40 @@ void RWindow::setPos(int x, int y) {
XMoveWindow(display, window, x, y);
}
bool RWindow::keyDown(X11Scancode scancode) {
return currentKeyboard.PressedKeys[scancode];
/*for (auto & ev : eventLog) {
bool RWindow::keyDown(SCANCODE scancode) {
for (auto & ev : events) {
if (auto *e = dynamic_cast<KeyDownEvent *>(ev)) {
if (e->key == scancode) { return true;}
}
}
return false;*/
return false;
}
KeyDownEvent RWindow::getEvent(X11Scancode scancode) {
/*for (auto & ev : eventLog) {
KeyDownEvent RWindow::getEvent(SCANCODE scancode) {
for (auto & ev : events) {
if (auto *e = dynamic_cast<KeyDownEvent *>(ev)) {
if (e->key == scancode) { return *e;}
}
}*/
//return EmptyKeyDownEvent;
}
return EmptyKeyDownEvent;
}
bool RWindow::mouseButtonDown(MouseButton buttoncode) {
// TODO: Implement MouseButtonState
/*for (auto & ev : eventLog) {
bool RWindow::mouseButtonDown(MOUSEBUTTONCODE buttoncode) {
for (auto & ev : events) {
if (auto *e = dynamic_cast<MouseButtonDownEvent *>(ev)) {
if (e->button == buttoncode) { return true;}
}
}
return false;*/
return false;
}
MouseButtonDownEvent RWindow::getEvent(MouseButton buttoncode) {
/*for (auto & ev : eventLog) {
MouseButtonDownEvent RWindow::getEvent(MOUSEBUTTONCODE buttoncode) {
for (auto & ev : events) {
if (auto *e = dynamic_cast<MouseButtonDownEvent *>(ev)) {
if (e->button == buttoncode) { return *e;}
}
}
return EmptyMouseButtonDownEvent;*/
return EmptyMouseButtonDownEvent;
}
void RWindow::glSwapBuffers() {
@@ -241,8 +201,8 @@ void RWindow::glSwapBuffers() {
}
void RWindow::setVsyncEnabled(bool b) {
PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXT = nullptr;
glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddressARB((const GLubyte*)"glXSwapIntervalEXT");
glXSwapIntervalEXT(display, None, b);
vsync = b;
_glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddressARB((const GLubyte*)"glXSwapIntervalEXT");
_glXSwapIntervalEXT(display, window, vsync);
}