Refactoring

This commit is contained in:
2024-02-06 23:37:46 -05:00
parent 983428f69d
commit 432d1818f3
5 changed files with 420 additions and 392 deletions

View File

@@ -22,7 +22,7 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
CPMAddPackage(
NAME J3ML
URL https://git.redacted.cc/josh/j3ml/archive/Prerelease-7.zip
URL https://git.redacted.cc/josh/j3ml/archive/Prerelease-14.zip
)
find_package(OpenGL REQUIRED)

View File

@@ -131,7 +131,7 @@ static Key GetKeyFromX11Scancode(X11Scancode code)
if (key.x11ScanCode == code)
return key;
}
throw "Unavaliable Scancode: " + std::to_string((int)code);
throw std::runtime_error("Unavaliable Scancode: " + std::to_string((int)code));
}
static Key GetKeyFromWindowsScancode(WindowsScancode code)
{
@@ -139,5 +139,5 @@ static Key GetKeyFromWindowsScancode(WindowsScancode code)
if (key.winScanCode == code)
return key;
}
throw "Unavaliable Scancode: " + std::to_string((int)code);
throw std::runtime_error("Unavaliable Scancode: " + std::to_string((int)code));
}

View File

@@ -3,20 +3,23 @@
#include <vector>
#include <memory>
#include <rewindow/types/event.h>
#include <functional>
#include <map>
#include <rewindow/types/key.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
using namespace std::chrono_literals;
using precision_clock = std::chrono::high_resolution_clock;
using precision_timestamp = precision_clock::time_point;
enum class RWindowFlags: uint8_t {
IN_FOCUS = 0,
FULLSCREEN = 1,
@@ -48,146 +51,167 @@ enum class CursorStyle
RightSide,
};
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 };
namespace ReWindow
{
using LinearAlgebra::Vector2;
class KeyboardState {
public:
std::map<Key, bool> PressedKeys;
};
class TimestampedEvent {
private:
precision_timestamp timestamp;
public:
TimestampedEvent() : timestamp(precision_clock::now())
{ }
precision_timestamp GetTimestamp() const { return timestamp;}
};
class GamepadState {
public:
std::map<GamepadButton, bool> PressedButtons;
};
class RWindowEvent : public TimestampedEvent {
public:
RWindowEvent() : TimestampedEvent() { }
};
const RWindowEvent EmptyRWindowEvent;
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
bool fullscreenmode = false;
public:
Event<> focusGained;
Event<> focusLost;
// TODO: Integrate J3ML
Event<LinearAlgebra::Vector2> mouseMoved;
Event<MouseButton> mouseBtnPressed;
Event<MouseButton> mouseBtnReleased;
Event<KeyDownEvent> onKeyboardPress;
Event<KeyUpEvent> onKeyboardRelease;
virtual void OnFocusLost(const RWindowEvent& e) {}
virtual void OnFocusGain(const RWindowEvent& e) {}
virtual void OnMouseMove(const MouseEvent& e) {}
virtual void OnRefresh(float elapsed) {}
virtual void OnResizeSuccess() {}
virtual bool OnResizeRequest() {}
// TODO: Must be implemented from scratch as a Motif Window in x11
void MessageBox();
bool isFocused() const;
bool isFullscreen() const
namespace WindowEvents
{
return fullscreenmode;
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:
MouseMoveEvent(const Vector2 &pos) : MouseEvent()
{}
};
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{};
}
bool isResizable() const;
bool isVsyncEnabled() const;
bool isAlive() const;
using namespace WindowEvents;
void setMouseVisible(bool visible);
void setMouseLocked();
void setMouseCenter();
void restoreMouseFromLastCenter(); // Feels nicer for users
class RWindow {
private:
bool flags[4];
std::vector<RWindowEvent> eventLog;
KeyboardState currentKeyboard; // Current Frame's Keyboard State
KeyboardState previousKeyboard; // Previous Frame's Keyboard State
bool fullscreenmode = false;
public:
Event<> focusGained;
Event<> focusLost;
// TODO: Integrate J3ML
Event<Vector2> mouseMoved;
Event<MouseButton> mouseBtnPressed;
Event<MouseButton> mouseBtnReleased;
Event<KeyDownEvent> onKeyboardPress;
Event<KeyUpEvent> onKeyboardRelease;
virtual void OnFocusLost(const RWindowEvent& e) {}
virtual void OnFocusGain(const RWindowEvent& e) {}
virtual void OnMouseMove(const MouseEvent& e) {}
virtual void OnRefresh(float elapsed) {}
virtual void OnResizeSuccess() {}
virtual bool OnResizeRequest() {}
virtual void OnKeyDownEvent(const KeyDownEvent&) {}
virtual void OnKeyUpEvent(const KeyUpEvent&) {}
virtual void OnMouseMoveEvent(const MouseMoveEvent&) {}
// TODO: Must be implemented from scratch as a Motif Window in x11
void MessageBox();
bool isFocused() const;
bool isFullscreen() const
{
return fullscreenmode;
}
bool isResizable() const;
bool isVsyncEnabled() const;
bool isAlive() const;
void setMouseVisible(bool visible);
void setMouseLocked();
void setMouseCenter();
void restoreMouseFromLastCenter(); // Feels nicer for users
void setFullscreen(bool fs);
void setResizable(bool resizable);
void setVsyncEnabled(bool);
void setTitle(const std::string& title);
void setFullscreen(bool fs);
void setResizable(bool resizable);
void setVsyncEnabled(bool);
void setTitle(const std::string& title);
void fullscreen();
void restoreFromFullscreen();
void fullscreen();
void restoreFromFullscreen();
bool getFlag(RWindowFlags flag) const;
void setFlag(RWindowFlags flag, bool state);
void init(RenderingAPI api, const char* title, int width, int height, bool vsync);
void destroyWindow();
void pollEvents();
void refresh();
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 getFlag(RWindowFlags flag) const;
void setFlag(RWindowFlags flag, bool state);
void init(RenderingAPI api, const char* title, int width, int height, bool vsync);
void destroyWindow();
void pollEvents();
void refresh();
void setSize(int width, int height);
Vector2 getPos();
Vector2 getSize();
void setPos(int x, int y);
bool keyDown(X11Scancode scancode);
bool mouseButtonDown(MouseButton buttoncode);
KeyDownEvent getEvent(X11Scancode scancode);
MouseButtonDownEvent getEvent(MouseButton buttoncode);
void raise() const;
void lower() const;
void setCursor(CursorStyle style) const;
// TODO: Implement
// void setCursor(Icon* icon) const;
static void glSwapBuffers();
//Initialize to false because it's not guaranteed that they will be cleared first
RWindow();
};
}
void raise() const;
void lower() const;
void setCursor(CursorStyle style) const;
// TODO: Implement
// void setCursor(Icon* icon) const;
static void glSwapBuffers();
//Initialize to false because it's not guaranteed that they will be cleared first
RWindow();
};

View File

@@ -3,23 +3,24 @@
#if __linux__
void KeyDown(KeyDownEvent e)
void KeyDown(const ReWindow::WindowEvents::KeyDownEvent& e)
{
std::cout << e.key.CharCode << " pressed" << std::endl;
}
int main() {
auto* window = new RWindow();
auto* window = new ReWindow::RWindow();
window->init(RenderingAPI::OPENGL, "name",100,100, true);
window->setFlag(RWindowFlags::RESIZABLE, false);
window->onKeyboardPress += KeyDown;
window->onKeyboardRelease += [&](KeyUpEvent e)
window->onKeyboardRelease += [&](ReWindow::WindowEvents::KeyUpEvent e)
{
if (e.key == Keys::F)
window->setFullscreen(!window->isFullscreen());
};
while (window->isAlive()) {
window->pollEvents();
window->refresh();

View File

@@ -44,287 +44,290 @@ bool Key::operator<(const Key &rhs) const {
return (this->CharCode < rhs.CharCode);
}
void RWindow::raise() const { XRaiseWindow(display, window); }
void RWindow::lower() const { XLowerWindow(display, window); }
namespace ReWindow {
void RWindow::init(RenderingAPI api, const char* title, int width, int height, bool vsync) {
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};
visual = glXChooseVisual(display, defaultScreen, glAttributes);
glContext = glXCreateContext(display, visual, nullptr, GL_TRUE);
vsync = sync;
xSetWindowAttributes.colormap = XCreateColormap(display, RootWindow(display, defaultScreen), visual->visual,
AllocNone);
void RWindow::raise() const { XRaiseWindow(display, window); }
void RWindow::lower() const { XLowerWindow(display, window); }
window = XCreateWindow(display, RootWindow(display, defaultScreen), 0, 0, width, height, 0, visual->depth,
InputOutput, visual->visual, CWBackPixel | CWColormap | CWBorderPixel | NoEventMask,
&xSetWindowAttributes);
XSelectInput(display, window,
ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
Button1MotionMask | Button2MotionMask | Button3MotionMask | Button4MotionMask | Button5MotionMask |
PointerMotionHintMask | FocusChangeMask | StructureNotifyMask | SubstructureRedirectMask |
SubstructureNotifyMask | CWColormap);
XMapWindow(display, window);
XStoreName(display, window, title);
wmDeleteWindow = XInternAtom(display, "WM_DELETE_WINDOW", False);
XSetWMProtocols(display, window, &wmDeleteWindow, 1);
glXMakeCurrent(display, window, glContext);
setVsyncEnabled(vsync);
} else {exit(0);}
}
void RWindow::init(RenderingAPI api, const char* title, int width, int height, bool vsync) {
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};
visual = glXChooseVisual(display, defaultScreen, glAttributes);
glContext = glXCreateContext(display, visual, nullptr, GL_TRUE);
vsync = sync;
xSetWindowAttributes.colormap = XCreateColormap(display, RootWindow(display, defaultScreen), visual->visual,
AllocNone);
void RWindow::destroyWindow() {
XDestroySubwindows(display, window);
XAutoRepeatOn(display);
XDestroyWindow(display, window);
delete this;
}
void RWindow::refresh() {
}
bool RWindow::getFlag(RWindowFlags flag) const
{
if (flags[(int)flag])
return true;
return false;
}
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) {
hints.flags = PMinSize | PMaxSize;
hints.min_width = hints.max_width = windowAttributes.width;
hints.min_height = hints.max_height = windowAttributes.height;
XSetWMNormalHints(display, window, &hints);
window = XCreateWindow(display, RootWindow(display, defaultScreen), 0, 0, width, height, 0, visual->depth,
InputOutput, visual->visual, CWBackPixel | CWColormap | CWBorderPixel | NoEventMask,
&xSetWindowAttributes);
XSelectInput(display, window,
ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
Button1MotionMask | Button2MotionMask | Button3MotionMask | Button4MotionMask | Button5MotionMask |
PointerMotionHintMask | FocusChangeMask | StructureNotifyMask | SubstructureRedirectMask |
SubstructureNotifyMask | CWColormap);
XMapWindow(display, window);
XStoreName(display, window, title);
wmDeleteWindow = XInternAtom(display, "WM_DELETE_WINDOW", False);
XSetWMProtocols(display, window, &wmDeleteWindow, 1);
glXMakeCurrent(display, window, glContext);
setVsyncEnabled(vsync);
} else {exit(0);}
}
}
void RWindow::pollEvents() {
while(XPending(display)) {
XNextEvent(display, &xev);
void RWindow::destroyWindow() {
XDestroySubwindows(display, window);
XAutoRepeatOn(display);
XDestroyWindow(display, window);
delete this;
}
if (xev.type == ClientMessage) {
if (xev.xclient.message_type == XInternAtom(display, "WM_PROTOCOLS", False) &&
static_cast<Atom>(xev.xclient.data.l[0]) == wmDeleteWindow) {
destroyWindow();
exit(0);
void RWindow::refresh() {
}
bool RWindow::getFlag(RWindowFlags flag) const
{
if (flags[(int)flag])
return true;
return false;
}
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) {
hints.flags = PMinSize | PMaxSize;
hints.min_width = hints.max_width = windowAttributes.width;
hints.min_height = hints.max_height = windowAttributes.height;
XSetWMNormalHints(display, window, &hints);
}
}
void RWindow::pollEvents() {
while(XPending(display)) {
XNextEvent(display, &xev);
if (xev.type == ClientMessage) {
if (xev.xclient.message_type == XInternAtom(display, "WM_PROTOCOLS", False) &&
static_cast<Atom>(xev.xclient.data.l[0]) == wmDeleteWindow) {
destroyWindow();
exit(0);
}
}
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;
auto key = GetKeyFromX11Scancode(scancode);
currentKeyboard.PressedKeys[key] = false;
KeyUpEvent eventData = KeyUpEvent(key);
onKeyboardRelease.Invoke(eventData);
/*for (unsigned int i = 0; i < eventLog.size(); i++) {
if (auto *e = dynamic_cast<KeyDownEvent *>(eventLog[i])) {
if ((int) e->key == (int) xev.xkey.keycode) {
delete eventLog[i];
eventLog.erase(eventLog.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[key] = true;
KeyDownEvent eventData = KeyDownEvent(key);
onKeyboardPress.Invoke(eventData);
eventLog.push_back(eventData);
}
if (xev.type == ButtonRelease) {
/*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);
}
}
}*/
}
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);*/
}
if (xev.type == Expose) {
}
}
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;
auto key = GetKeyFromX11Scancode(scancode);
currentKeyboard.PressedKeys[key] = false;
KeyUpEvent eventData = KeyUpEvent(key);
onKeyboardRelease.Invoke(eventData);
/*for (unsigned int i = 0; i < eventLog.size(); i++) {
if (auto *e = dynamic_cast<KeyDownEvent *>(eventLog[i])) {
if ((int) e->key == (int) xev.xkey.keycode) {
delete eventLog[i];
eventLog.erase(eventLog.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[key] = true;
KeyDownEvent eventData = KeyDownEvent(key);
onKeyboardPress.Invoke(eventData);
eventLog.push_back(eventData);
}
if (xev.type == ButtonRelease) {
/*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);
}
}
}*/
}
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);*/
}
if (xev.type == Expose) {
}
previousKeyboard = currentKeyboard;
}
previousKeyboard = currentKeyboard;
}
//Might make the window go off the screen on some window managers.
void RWindow::setSize(int width, int height) {
if (!getFlag(RWindowFlags::RESIZABLE))
return;
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);
size[0] = windowAttributes.width;
size[1] = windowAttributes.height;
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);
pos[0] = windowAttributes.x;
pos[1] = windowAttributes.y;
return pos;
}
void RWindow::setPos(int x, int y) {
XMoveWindow(display, window, x, y);
}
bool RWindow::keyDown(X11Scancode scancode) {
return currentKeyboard.PressedKeys[GetKeyFromX11Scancode(scancode)];
/*for (auto & ev : eventLog) {
if (auto *e = dynamic_cast<KeyDownEvent *>(ev)) {
if (e->key == scancode) { return true;}
}
//Might make the window go off the screen on some window managers.
void RWindow::setSize(int width, int height) {
if (!getFlag(RWindowFlags::RESIZABLE))
return;
XResizeWindow(display, window, width, height);
}
return false;*/
}
KeyDownEvent RWindow::getEvent(X11Scancode scancode) {
/*for (auto & ev : eventLog) {
if (auto *e = dynamic_cast<KeyDownEvent *>(ev)) {
if (e->key == scancode) { return *e;}
}
}*/
//return EmptyKeyDownEvent;
}
bool RWindow::mouseButtonDown(MouseButton buttoncode) {
// TODO: Implement MouseButtonState
/*for (auto & ev : eventLog) {
if (auto *e = dynamic_cast<MouseButtonDownEvent *>(ev)) {
if (e->button == buttoncode) { return true;}
}
// 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);
size[0] = windowAttributes.width;
size[1] = windowAttributes.height;
return size;
}
return false;
}
MouseButtonDownEvent RWindow::getEvent(MouseButton buttoncode) {
/*for (auto & ev : eventLog) {
if (auto *e = dynamic_cast<MouseButtonDownEvent *>(ev)) {
if (e->button == buttoncode) { return *e;}
}
// 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);
pos[0] = windowAttributes.x;
pos[1] = windowAttributes.y;
return pos;
}
return EmptyMouseButtonDownEvent;*/
}
void RWindow::glSwapBuffers() {
glXSwapBuffers(display,window);
}
void RWindow::setPos(int x, int y) {
XMoveWindow(display, window, x, y);
}
bool RWindow::isResizable() const {
return this->getFlag(RWindowFlags::RESIZABLE);
}
bool RWindow::keyDown(X11Scancode scancode) {
return currentKeyboard.PressedKeys[GetKeyFromX11Scancode(scancode)];
/*for (auto & ev : eventLog) {
if (auto *e = dynamic_cast<KeyDownEvent *>(ev)) {
if (e->key == scancode) { return true;}
}
}
return false;*/
}
bool RWindow::isAlive() const {
return true;
}
KeyDownEvent RWindow::getEvent(X11Scancode scancode) {
/*for (auto & ev : eventLog) {
if (auto *e = dynamic_cast<KeyDownEvent *>(ev)) {
if (e->key == scancode) { return *e;}
}
}*/
//return EmptyKeyDownEvent;
}
void RWindow::setResizable(bool resizable) {
this->setFlag(RWindowFlags::RESIZABLE, resizable);
}
bool RWindow::mouseButtonDown(MouseButton buttoncode) {
// TODO: Implement MouseButtonState
/*for (auto & ev : eventLog) {
if (auto *e = dynamic_cast<MouseButtonDownEvent *>(ev)) {
if (e->button == buttoncode) { return true;}
}
}
return false;
}
RWindow::RWindow() : flags(false,false,false,false) {}
MouseButtonDownEvent RWindow::getEvent(MouseButton buttoncode) {
/*for (auto & ev : eventLog) {
if (auto *e = dynamic_cast<MouseButtonDownEvent *>(ev)) {
if (e->button == buttoncode) { return *e;}
}
}
return EmptyMouseButtonDownEvent;*/
}
void RWindow::setFullscreen(bool fs) {
if (fs)
fullscreen();
else
restoreFromFullscreen();
}
void RWindow::glSwapBuffers() {
glXSwapBuffers(display,window);
}
void RWindow::fullscreen() {
fullscreenmode = true;
bool RWindow::isResizable() const {
return this->getFlag(RWindowFlags::RESIZABLE);
}
XEvent xev;
Atom wm_state = XInternAtom (display, "_NET_WM_STATE", true );
Atom wm_fullscreen = XInternAtom (display, "_NET_WM_STATE_FULLSCREEN", true );
bool RWindow::isAlive() const {
return 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);
}
void RWindow::setResizable(bool resizable) {
this->setFlag(RWindowFlags::RESIZABLE, resizable);
}
void RWindow::restoreFromFullscreen() {
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);
}
RWindow::RWindow() : flags(false,false,false,false) {}
void RWindow::setVsyncEnabled(bool b) {
PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXT = nullptr;
glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddressARB((const GLubyte*)"glXSwapIntervalEXT");
glXSwapIntervalEXT(display, None, b);
}
void RWindow::setFullscreen(bool fs) {
if (fs)
fullscreen();
else
restoreFromFullscreen();
}
void RWindow::fullscreen() {
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);
}
void RWindow::restoreFromFullscreen() {
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);
}
void RWindow::setVsyncEnabled(bool b) {
PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXT = nullptr;
glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddressARB((const GLubyte*)"glXSwapIntervalEXT");
glXSwapIntervalEXT(display, None, b);
}
}