Compare commits

...

12 Commits

Author SHA1 Message Date
ef257765fe Migrate to J3ML v20 2024-03-21 13:05:09 -04:00
5696dd4ed8 Un-break 2024-02-24 08:42:42 -05:00
Redacted
9f0a511022 Update CMakeLists.txt 2024-02-24 07:48:37 -05:00
ffe49e4c67 Implement static GetMouseCoordinates() ? 2024-02-22 00:31:04 -05:00
28f904783f Fix SIGILL 2024-02-21 23:46:42 -05:00
6969568549 Attempt to fix broken integration 2024-02-21 23:38:12 -05:00
bcc74ea3d4 Integrate Event module 2024-02-21 23:12:36 -05:00
ef57fb0732 Implement RWindow::isKeyDown 2024-02-21 20:21:37 -05:00
2930391ee4 Implement CursorStyles, minor refactors 2024-02-21 20:10:06 -05:00
b1dfab70a1 Lil Cleanup 2024-02-21 05:37:12 -05:00
bdc1427626 Clear TODOs 2024-02-21 05:35:20 -05:00
158fafaa79 Implement Window::getCursorPos, also fixed Window::getPos 2024-02-21 05:35:08 -05:00
8 changed files with 365 additions and 267 deletions

View File

@@ -1,55 +1,72 @@
cmake_minimum_required(VERSION 3.20)
project(ReWindowLibrary
VERSION 1.0
LANGUAGES CXX
)
if (PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
message(FATAL_ERROR "In-Source builds are not allowed")
endif()
set(CMAKE_CXX_STANDARD 20)
if (WIN32)
set(CMAKE_CXX_FLAGS "-municode")
endif()
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-18.zip
)
find_package(OpenGL REQUIRED)
include_directories({$OPENGL_INCLUDE_DIRS} ${J3ML_SOURCE_DIR}/include)
file(GLOB_RECURSE HEADERS "include/*.h" "include/*.hpp")
if(UNIX AND NOT APPLE)
file(GLOB_RECURSE SOURCES "src/rewindow/*.cpp" "src/linux/*.cpp")
endif()
if(WIN32)
file(GLOB_RECURSE SOURCES "src/rewindow/*.cpp" "src/windows/*.cpp")
endif()
include_directories("include")
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})
add_executable(ReWindowLibraryDemo main.cpp)
#target_include_directories(ReWindowLibraryDemo PRIVATE ${JGL_SOURCE_DIR}/include)
target_link_libraries(ReWindowLibraryDemo PUBLIC ReWindowLibrary)
#target_link_libraries(ReWindowLibraryDemo PRIVATE JGL)
endif()
if(WIN32)
cmake_minimum_required(VERSION 3.20)
project(ReWindowLibrary
VERSION 1.0
LANGUAGES CXX
)
if (PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
message(FATAL_ERROR "In-Source builds are not allowed")
endif()
set(CMAKE_CXX_STANDARD 20)
if (WIN32)
set(CMAKE_CXX_FLAGS "-municode")
endif()
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-20.zip
)
CPMAddPackage(
NAME Event
URL https://git.redacted.cc/josh/Event/archive/Release-2.zip
)
find_package(OpenGL REQUIRED)
include_directories(${OPENGL_INCLUDE_DIRS})
include_directories(${J3ML_SOURCE_DIR}/include)
file(GLOB_RECURSE HEADERS "include/*.h" "include/*.hpp")
if(UNIX AND NOT APPLE)
file(GLOB_RECURSE SOURCES "src/rewindow/*.cpp" "src/linux/*.cpp")
endif()
if(WIN32)
file(GLOB_RECURSE SOURCES "src/rewindow/*.cpp" "src/windows/*.cpp")
endif()
include_directories("include")
add_library(ReWindowLibrary SHARED ${SOURCES}
include/rewindow/types/cursors.h
)
target_include_directories(ReWindowLibrary PUBLIC ${Event_SOURCE_DIR}/include)
# Why god???
set_target_properties(ReWindowLibrary PROPERTIES LINKER_LANGUAGE CXX)
if(UNIX AND NOT APPLE)
target_link_libraries(ReWindowLibrary PUBLIC X11)
target_link_libraries(ReWindowLibrary PUBLIC ${OPENGL_LIBRARIES})
target_link_libraries(ReWindowLibrary PUBLIC J3ML)
target_link_libraries(ReWindowLibrary PUBLIC Event)
add_executable(ReWindowLibraryDemo main.cpp)
#target_include_directories(ReWindowLibraryDemo PRIVATE ${JGL_SOURCE_DIR}/include)
target_link_libraries(ReWindowLibraryDemo PUBLIC ReWindowLibrary)
#target_link_libraries(ReWindowLibraryDemo PRIVATE JGL)
endif()
if(WIN32)
endif()

View File

@@ -0,0 +1,60 @@
#pragma once
#include <X11/cursorfont.h>
namespace ReWindow
{
enum class X11CursorStyle
{
Default = XC_left_ptr,
X = XC_X_cursor,
Arrow = XC_arrow,
IBeam = XC_xterm,
BottomLeftCorner = XC_bottom_left_corner,
BottomRightCorner = XC_bottom_right_corner,
BottomSide = XC_bottom_side,
Dot = XC_dot,
DoubleArrow = XC_double_arrow,
Exchange = XC_exchange,
Hand = XC_hand2,
LeftSide = XC_left_side,
Plus = XC_plus,
RightSide = XC_right_side,
Pencil = XC_pencil
};
// https://learn.microsoft.com/en-us/windows/win32/menurc/about-cursors
enum class WindowsCursorStyle
{
Arrow,
IBeam,
Wait,
Cross,
Hand,
AppStarting,
};
class CursorStyle
{
public:
X11CursorStyle X11Cursor;
CursorStyle(X11CursorStyle style) : X11Cursor(style) {}
};
namespace Cursors
{
static const CursorStyle Default {X11CursorStyle::Default};
static const CursorStyle X {X11CursorStyle::X};
static const CursorStyle Arrow {X11CursorStyle::Arrow};
static const CursorStyle IBeam {X11CursorStyle::IBeam};
static const CursorStyle BottomLeftCorner {X11CursorStyle::BottomLeftCorner};
static const CursorStyle BottomRightCorner {X11CursorStyle::BottomRightCorner};
static const CursorStyle BottomSide {X11CursorStyle::BottomSide};
static const CursorStyle Dot {X11CursorStyle::Dot};
static const CursorStyle DoubleArrow {X11CursorStyle::DoubleArrow};
static const CursorStyle Exchange {X11CursorStyle::Exchange};
static const CursorStyle Hand {X11CursorStyle::Hand};
static const CursorStyle LeftSide {X11CursorStyle::LeftSide};
static const CursorStyle Plus {X11CursorStyle::Plus};
static const CursorStyle RightSide {X11CursorStyle::RightSide};
static const CursorStyle Pencil {X11CursorStyle::Pencil};
}
}

View File

@@ -1,81 +0,0 @@
#pragma once
#include <chrono>
#include <functional>
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;
};
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;
};
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;
}
return false;
}
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());
}

View File

@@ -2,7 +2,7 @@
#include <cstdint>
#include <vector>
#include <memory>
#include <rewindow/types/event.h>
#include <Event.h>
#include <functional>
#include <map>
#include <rewindow/types/key.h>
@@ -13,6 +13,8 @@
#include <X11/Xutil.h>
#include <GL/gl.h>
#include <GL/glx.h>
#include "cursors.h"
#endif
@@ -33,23 +35,6 @@ enum class RenderingAPI: uint8_t {
VULKAN = 1,
};
enum class CursorStyle
{
Default,
X,
Arrow,
IBeam,
BottomLeftCorner,
BottomRightCorner,
BottomSide,
Dot,
DoubleArrow,
Exchange,
Hand,
LeftSide,
Plus,
RightSide,
};
enum class KeyState { Pressed, Released };
@@ -140,50 +125,55 @@ namespace ReWindow
using namespace WindowEvents;
class RWindow {
private:
Vector2 lastKnownWindowSize {0, 0};
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<MouseMoveEvent&> mouseMoved;
Event<MouseButton> mouseBtnPressed;
Event<MouseButton> mouseBtnReleased;
Event<KeyDownEvent> onKeyboardPress;
Event<KeyUpEvent> onKeyboardRelease;
#pragma region Callbacks
/// Bindable Non-intrusive event handlers
/// Use these when you can't override the base window class
Event<RWindowEvent> OnFocusLostEvent;
Event<RWindowEvent> OnFocusGainEvent;
Event<float> OnRefreshEvent;
Event<WindowResizeRequestEvent> OnResizeRequestEvent;
Event<KeyDownEvent> OnKeyDownEvent;
Event<KeyUpEvent> OnKeyUpEvent;
Event<MouseMoveEvent> OnMouseMoveEvent;
Event<MouseButtonDownEvent> OnMouseButtonDownEvent;
Event<MouseButtonUpEvent> OnMouseButtonUpEvent;
#pragma endregion
#pragma region Overrides
virtual void OnFocusLost(const RWindowEvent& e) {}
virtual void OnFocusGain(const RWindowEvent& e) {}
virtual void OnRefresh(float elapsed) {}
virtual void OnResizeSuccess() {}
virtual bool OnResizeRequest(const WindowResizeRequestEvent& e) {}
virtual void OnKeyDownEvent(const KeyDownEvent&) {}
virtual void OnKeyUpEvent(const KeyUpEvent&) {}
virtual bool OnResizeRequest(const WindowResizeRequestEvent& e) { return true;}
virtual void OnKeyDown(const KeyDownEvent&) {}
virtual void OnKeyUp(const KeyUpEvent&) {}
virtual void OnMouseMove(const MouseMoveEvent&) {}
virtual void OnMouseButtonDown(const MouseButtonDownEvent&) {}
virtual void OnMouseButtonUp(const MouseButtonUpEvent&) {}
#pragma endregion
RWindow();
Vector2 GetSize() const
{
RWindow(const std::string& title, int width, int height);
RWindow(const std::string& title, int width, int height, RenderingAPI renderer);
Vector2 GetMouseCoordinates() const
{
return getCursorPos();
}
void setRenderer(RenderingAPI api);
void Open();
void Close();
void CloseAndReopenInPlace();
// TODO: Must be implemented from scratch as a Motif Window in x11
void MessageBox();
bool isFocused() const;
bool isFullscreen() const
{
return fullscreenmode;
}
bool isFullscreen() const;
bool isResizable() const;
bool isVsyncEnabled() const;
@@ -194,40 +184,53 @@ namespace ReWindow
void setMouseCenter();
void restoreMouseFromLastCenter(); // Feels nicer for users
bool isKeyDown(Key key) const;
bool isMouseButtonDown(MouseButton button) const;
void setFullscreen(bool fs);
void setResizable(bool resizable);
void setVsyncEnabled(bool);
void setTitle(const std::string& title);
std::string getTitle() const;
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 init(RenderingAPI api, const char* title, int width, int height, bool vsync);
void destroyWindow();
void pollEvents();
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;
void setPos(int x, int y);
void setPos(const Vector2& pos);
Vector2 getCursorPos() const;
void raise() const;
void lower() const;
void setCursor(CursorStyle style) const;
// TODO: Implement
// void setCursor(Icon* icon) const;
void setCursorStyle(CursorStyle style) const;
void setCursorCustomIcon() const;
static void glSwapBuffers();
//Initialize to false because it's not guaranteed that they will be cleared first
private:
Vector2 lastKnownWindowSize {0, 0};
bool flags[4];
std::vector<RWindowEvent> eventLog;
KeyboardState currentKeyboard; // Current Frame's Keyboard State
KeyboardState previousKeyboard; // Previous Frame's Keyboard State
bool fullscreenmode = false;
std::string title;
int width;
int height;
RenderingAPI renderer;
bool open = false;
bool resizable;
//You can't do this because you can't initialize a static member inside the class constructor.
//static RWindow* singleton;
};

View File

@@ -3,29 +3,39 @@
#if __linux__
void KeyDown(const ReWindow::WindowEvents::KeyDownEvent& e)
{
//std::cout << e.key.CharCode << " pressed" << std::endl;
}
Vector2 mouse_pos;
// TODO: Move to J3ML::LinearAlgebra::Vector2
std::ostream& operator<<(std::ostream& os, const Vector2& v)
{
return os << "{" << v.x << ", " << v.y << "}";
}
class MyWindow : public ReWindow::RWindow
{
public:
MyWindow(const std::string& title, int w, int h) : ReWindow::RWindow(title, w, h)
{}
void OnMouseMove(const ReWindow::MouseMoveEvent& e) override
{
//std::cout << "Mouse Moved: " << e.Position << " @ " << std::endl;
}
void OnKeyDown(const ReWindow::KeyDownEvent& e) override
{
}
void OnRefresh(float elapsed) override
{
glSwapBuffers();
auto pos = getCursorPos();
//std::cout << pos.x << ", " << pos.y << std::endl;
if (isKeyDown(Keys::L))
this->setCursorStyle(ReWindow::Cursors::Pencil);
else
this->setCursorStyle(ReWindow::Cursors::Default);
}
bool OnResizeRequest(const ReWindow::WindowResizeRequestEvent& e) override
{
@@ -35,15 +45,22 @@ public:
};
int main() {
auto* window = new MyWindow();
window->init(RenderingAPI::OPENGL, "name",100,100, true);
window->setFlag(RWindowFlags::RESIZABLE, true);
auto* window = new MyWindow("Test Window", 600, 480);
window->onKeyboardPress += KeyDown;
window->onKeyboardRelease += [&](ReWindow::WindowEvents::KeyUpEvent e)
window->setRenderer(RenderingAPI::OPENGL);
window->Open();
// TODO: Cannot set flags until after window is open
// Make this work somehow
window->setFullscreen(false);
window->setVsyncEnabled(true);
window->setResizable(true);
window->OnKeyDownEvent += [&] (ReWindow::KeyDownEvent e)
{
if (e.key == Keys::F)
window->setFullscreen(!window->isFullscreen());
if (e.key == Keys::LeftArrow)
{
std::cout << "Left Arrow Hit" << std::endl;
}
};
while (window->isAlive()) {

View File

@@ -1 +0,0 @@
#include "../include/rewindow/types/event.h"

View File

@@ -4,10 +4,12 @@
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/cursorfont.h>
#include <cstdlib>
#include <cstring>
#include <thread>
#include "rewindow/types/cursors.h"
// TODO: Move all "global" members to be instantiated class members of Window
// Doing this would break the intended "Platform-Specific" Encapsulation
@@ -27,43 +29,33 @@ GLXContext glContext;
namespace ReWindow {
RWindow::RWindow() : flags(false,false,false,false) {
RWindow::RWindow() {
title = "ReWindow Application";
width = 640;
height = 480;
//RWindow::singleton = this;
}
RWindow::RWindow(const std::string& title, int width, int height) : flags(false,false,false,false) {
this->title = title;
this->width = width;
this->height = height;
//RWindow::singleton = this;
}
RWindow::RWindow(const std::string& title, int width, int height, RenderingAPI renderer) :
title(title), width(width), height(height), renderer(renderer)
{
//RWindow::singleton = this;
}
void RWindow::raise() const { XRaiseWindow(display, window); }
void RWindow::lower() const { XLowerWindow(display, window); }
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);
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 |
PointerMotionMask |
PointerMotionHintMask | FocusChangeMask | StructureNotifyMask | SubstructureRedirectMask |
SubstructureNotifyMask | CWColormap | ResizeRequest | ResizeRedirectMask);
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::destroyWindow() {
XDestroySubwindows(display, window);
@@ -76,31 +68,11 @@ namespace ReWindow {
// TODO: Implement refresh time keeping
OnRefresh(0.f);
// TODO: Check if mouse coords have changed, only then fire OnMouseMove event
Vector2 mouse_coords = getCursorPos();
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, root, &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;
Vector2 mouse_coords_raw = {(float)win_x_ret, (float)win_y_ret};
auto window_pos = getPos();
Vector2 mouse_coords = mouse_coords_raw - window_pos;
auto eventData = MouseMoveEvent(mouse_coords);
OnMouseMove(eventData);
}
auto eventData = MouseMoveEvent(mouse_coords);
OnMouseMove(eventData);
std::this_thread::sleep_for(1ms);
}
@@ -138,7 +110,7 @@ namespace ReWindow {
if (xev.type == FocusIn) {
XAutoRepeatOff(display);
focusGained.Invoke();
//focusGained.Invoke();
RWindowEvent event {};
OnFocusGain(event);
setFlag(RWindowFlags::IN_FOCUS, true);
@@ -146,7 +118,7 @@ namespace ReWindow {
if (xev.type == FocusOut) {
XAutoRepeatOn(display);
focusLost.Invoke();
//focusLost.Invoke();
RWindowEvent event {};
OnFocusLost(event);
setFlag(RWindowFlags::IN_FOCUS, false);
@@ -157,8 +129,7 @@ namespace ReWindow {
auto key = GetKeyFromX11Scancode(scancode);
currentKeyboard.PressedKeys[key] = false;
KeyUpEvent eventData = KeyUpEvent(key);
OnKeyUpEvent(eventData);
OnKeyUp(eventData);
}
if (xev.type == KeyPress) {
@@ -166,7 +137,7 @@ namespace ReWindow {
auto key = GetKeyFromX11Scancode(scancode);
currentKeyboard.PressedKeys[key] = true;
KeyDownEvent eventData = KeyDownEvent(key);
OnKeyDownEvent(eventData);
OnKeyDown(eventData);
eventLog.push_back(eventData);
}
@@ -190,6 +161,7 @@ namespace ReWindow {
if (xev.type == 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)
{
//auto eventData = MouseMoveEvent(xev.xmotion.x, xev.xmotion.y);
@@ -211,8 +183,10 @@ namespace ReWindow {
}
// Might make the window go off the screen on some window managers.
void RWindow::setSize(int width, int height)
void RWindow::setSize(int newWidth, int newHeight)
{
this->width = newWidth;
this->height = newHeight;
if (!getFlag(RWindowFlags::RESIZABLE))
return;
XResizeWindow(display, window, width, height);
@@ -220,9 +194,39 @@ namespace ReWindow {
void RWindow::setSize(const Vector2& size)
{
this->width = size.x;
this->height = size.y;
this->setSize(size.x, size.y);
}
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
{
@@ -233,8 +237,9 @@ namespace ReWindow {
// TODO: implement integer vector2/3 types
Vector2 RWindow::getPos() const
{
auto root = XDefaultRootWindow(display);
XGetWindowAttributes(display,root,&windowAttributes);
XGetWindowAttributes(display,window,&windowAttributes);
return {(float)windowAttributes.x, (float)windowAttributes.y};
}
@@ -262,6 +267,7 @@ namespace ReWindow {
}
void RWindow::setResizable(bool resizable) {
this->resizable = resizable;
this->setFlag(RWindowFlags::RESIZABLE, resizable);
}
@@ -318,6 +324,83 @@ namespace ReWindow {
glXSwapIntervalEXT(display, None, 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);
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);
if (renderer == RenderingAPI::OPENGL)
{
glXMakeCurrent(display, window, glContext);
}
//setVsyncEnabled(vsync);
open = true;
}
void RWindow::setTitle(const std::string &title) {
this->title = title;
XStoreName(display, window, title.c_str());
}
std::string RWindow::getTitle() const {
return this->title;
}
bool RWindow::isKeyDown(Key key) const {
if (!currentKeyboard.PressedKeys.contains(key))
return false;
return currentKeyboard.PressedKeys.at(key);
}
// TODO: Implement MouseButton map
bool RWindow::isMouseButtonDown(MouseButton button) const
{
return false;
}
void RWindow::setRenderer(RenderingAPI api) {
renderer = api;
}
// TODO: Implement ControllerButton map
}

View File