Compare commits
27 Commits
Prerelease
...
Prerelease
Author | SHA1 | Date | |
---|---|---|---|
48091496d5 | |||
c202ca5c84 | |||
c5f94b7c49 | |||
b351577a20 | |||
fb3a77e2e3 | |||
b96dd03af1 | |||
3e99394b6f | |||
5b10e55f83 | |||
c0dc56bc8c | |||
a62c576d14 | |||
92b567e48f | |||
|
3617d610ea | ||
9500681420 | |||
bf41652336 | |||
68ae72e6be | |||
a4b68a5abc | |||
1345669c32 | |||
9d51ac0026 | |||
edb2e29246 | |||
bed600d4d4 | |||
acf66ae909 | |||
c319acfffb | |||
9b810101a4 | |||
4c616d68af | |||
4571016c4c | |||
a8c85a29dd | |||
483b5dadad |
@@ -1,4 +1,4 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
cmake_minimum_required(VERSION 3.18..3.27)
|
||||
project(ReWindowLibrary
|
||||
VERSION 1.0
|
||||
LANGUAGES CXX
|
||||
@@ -18,17 +18,17 @@ include(cmake/CPM.cmake)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME J3ML
|
||||
URL https://git.redacted.cc/josh/j3ml/archive/Release-2.4.zip
|
||||
URL https://git.redacted.cc/josh/j3ml/archive/Release-3.1.zip
|
||||
)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME Event
|
||||
URL https://git.redacted.cc/josh/Event/archive/Release-6.zip
|
||||
URL https://git.redacted.cc/josh/Event/archive/Release-12.zip
|
||||
)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME jlog
|
||||
URL https://git.redacted.cc/josh/jlog/archive/Prerelease-9.zip
|
||||
URL https://git.redacted.cc/josh/jlog/archive/Prerelease-16.zip
|
||||
)
|
||||
|
||||
find_package(OpenGL REQUIRED)
|
||||
@@ -39,12 +39,15 @@ include_directories(${jlog_SOURCE_DIR}/include)
|
||||
|
||||
file(GLOB_RECURSE HEADERS "include/*.h" "include/*.hpp")
|
||||
|
||||
file(GLOB_RECURSE HEADERS "include/logger/*.h" "include/logger/*.hpp")
|
||||
|
||||
if(UNIX AND NOT APPLE)
|
||||
file(GLOB_RECURSE SOURCES "src/types/*.cpp" "src/platform/linux/*.cpp" "src/platform/shared/*.cpp")
|
||||
|
||||
file(GLOB_RECURSE SOURCES "src/types/*.cpp" "src/platform/linux/*.cpp" "src/platform/shared/*.cpp" "src/logger/*.cpp" )
|
||||
endif()
|
||||
|
||||
if(WIN32)
|
||||
file(GLOB_RECURSE SOURCES "src/types/*.cpp" "src/platform/windows/*.cpp" "src/platform/shared/*.cpp")
|
||||
file(GLOB_RECURSE SOURCES "src/types/*.cpp" "src/platform/windows/*.cpp" "src/platform/shared/*.cpp" "src/logger/*.cpp")
|
||||
endif()
|
||||
|
||||
include_directories("include")
|
||||
@@ -63,7 +66,7 @@ target_include_directories(ReWindowLibrary PUBLIC ${Event_SOURCE_DIR}/include)
|
||||
set_target_properties(ReWindowLibrary PROPERTIES LINKER_LANGUAGE CXX)
|
||||
|
||||
if(UNIX AND NOT APPLE)
|
||||
target_link_libraries(ReWindowLibrary PUBLIC X11 J3ML Event jlog ${OPENGL_LIBRARIES})
|
||||
target_link_libraries(ReWindowLibrary PUBLIC X11 Xrandr J3ML Event jlog ${OPENGL_LIBRARIES})
|
||||
target_link_libraries(ReWindowLibrary PUBLIC)
|
||||
|
||||
add_executable(ReWindowLibraryDemo main.cpp)
|
||||
@@ -75,4 +78,4 @@ if(WIN32)
|
||||
target_link_libraries(ReWindowLibrary PUBLIC J3ML Event jlog ${OPENGL_LIBRARIES})
|
||||
add_executable(ReWindowLibraryDemo main.cpp)
|
||||
target_link_libraries(ReWindowLibraryDemo PUBLIC ReWindowLibrary)
|
||||
endif()
|
||||
endif()
|
||||
|
@@ -11,8 +11,8 @@ A library which allows easily creating and managing a window and it's events ac
|
||||
Install dependencies
|
||||
|
||||
```bash
|
||||
dnf install cmake make gcc-g++ libX11 libX11-devel
|
||||
apt-get install cmake make gcc g++ libx11-6 libx11-dev libgl-dev
|
||||
Fedora/RHEL: dnf install cmake make gcc-g++ libX11 libX11-devel mesa-libGL-devel
|
||||
Ubuntu/Debian: apt-get install cmake make gcc g++ libx11-6 libx11-dev libgl-dev libxrandr-dev
|
||||
```
|
||||
|
||||
Clone the repository
|
||||
|
10
include/rewindow/logger/logger.h
Normal file
10
include/rewindow/logger/logger.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "jlog/Logger.hpp"
|
||||
|
||||
namespace ReWindow::Logger {
|
||||
using namespace jlog;
|
||||
|
||||
extern GenericLogger Fatal;
|
||||
extern GenericLogger Debug;
|
||||
}
|
39
include/rewindow/types/display.h
Normal file
39
include/rewindow/types/display.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include <J3ML/J3ML.hpp>
|
||||
|
||||
namespace ReWindow {
|
||||
class FullscreenGraphicsMode;
|
||||
class Display;
|
||||
}
|
||||
|
||||
///A given graphics mode that a display supports.
|
||||
///Upon startup, We'll poll the display server for this.
|
||||
class ReWindow::FullscreenGraphicsMode {
|
||||
private:
|
||||
u32 width;
|
||||
u32 height;
|
||||
short refresh_rate;
|
||||
public:
|
||||
FullscreenGraphicsMode() = default;
|
||||
FullscreenGraphicsMode(u32 width, u32 height, short refresh_rate);
|
||||
[[nodiscard]] u32 getWidth() const;
|
||||
[[nodiscard]] u32 getHeight() const;
|
||||
[[nodiscard]] float getRefreshRate() const;
|
||||
};
|
||||
|
||||
///Refers to a particular monitor the user has.
|
||||
class ReWindow::Display {
|
||||
private:
|
||||
///* The graphics mode of the display when our program was started. The primary use for this
|
||||
///* is so that if the user has selected a different mode in-game we can restore the old mode upon exit. *///
|
||||
FullscreenGraphicsMode default_graphics_mode{};
|
||||
///All graphics modes that a given display supports.
|
||||
std::vector<FullscreenGraphicsMode> graphics_modes;
|
||||
public:
|
||||
Display() = default;
|
||||
Display(const FullscreenGraphicsMode& default_graphics_mode, const std::vector<FullscreenGraphicsMode>& graphics_modes);
|
||||
FullscreenGraphicsMode getDefaultGraphicsMode();
|
||||
std::vector<FullscreenGraphicsMode> getGraphicsModes();
|
||||
static std::vector<Display> getDisplaysFromWindowManager();
|
||||
};
|
@@ -2,6 +2,7 @@
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#include <Event.h>
|
||||
#include <EventConnection.h> // TODO: Fix this shit to where we don't need to include both files they should just be bundled together
|
||||
#include <map>
|
||||
#include <thread>
|
||||
#include <rewindow/types/key.h>
|
||||
@@ -103,12 +104,16 @@ namespace ReWindow
|
||||
class MouseButtonDownEvent : public MouseEvent {
|
||||
public:
|
||||
MouseButton Button;
|
||||
|
||||
MouseButtonDownEvent() = default;
|
||||
MouseButtonDownEvent(MouseButton Button) : MouseEvent() {}
|
||||
};
|
||||
|
||||
class MouseButtonUpEvent : public MouseEvent {
|
||||
public:
|
||||
MouseButton Button;
|
||||
MouseButtonUpEvent() = default;
|
||||
MouseButtonUpEvent(MouseButton Button) : MouseEvent() {}
|
||||
};
|
||||
|
||||
class WindowResizeRequestEvent : public RWindowEvent
|
||||
@@ -122,7 +127,6 @@ namespace ReWindow
|
||||
|
||||
|
||||
/// 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.
|
||||
///
|
||||
|
||||
@@ -141,7 +145,7 @@ namespace ReWindow
|
||||
Event<RWindowEvent> OnFocusGainEvent;
|
||||
Event<float> OnRefreshEvent;
|
||||
Event<WindowResizeRequestEvent> OnResizeRequestEvent;
|
||||
Event<KeyDownEvent> OnKeyDownEvent;
|
||||
Event<KeyDownEvent> OnKeyDownEvent;
|
||||
Event<KeyUpEvent> OnKeyUpEvent;
|
||||
Event<MouseMoveEvent> OnMouseMoveEvent;
|
||||
Event<MouseButtonDownEvent> OnMouseButtonDownEvent;
|
||||
@@ -177,12 +181,6 @@ namespace ReWindow
|
||||
/// Returns a Vector2 representing mouse coordinates relative to the top-left corner of the window.
|
||||
Vector2 GetMouseCoordinates() const;
|
||||
|
||||
// TODO: Is this part of the API interface? Can it be moved to protected?
|
||||
void liftKey (Key key);
|
||||
|
||||
// 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);
|
||||
|
||||
@@ -197,30 +195,24 @@ namespace ReWindow
|
||||
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;
|
||||
[[nodiscard]] bool isFocused() const;
|
||||
[[nodiscard]] bool isFullscreen() const;
|
||||
[[nodiscard]] bool isResizable() const;
|
||||
[[nodiscard]] bool isVsyncEnabled() const;
|
||||
|
||||
bool isAlive() const; // TODO: Always returns true.
|
||||
[[nodiscard]] 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();
|
||||
void restoreMouseFromLastCenter(); // Feels nicer for users
|
||||
|
||||
bool isKeyDown(Key key) const;
|
||||
bool isMouseButtonDown(MouseButton button) const;
|
||||
[[nodiscard]] bool isKeyDown(Key key) const;
|
||||
[[nodiscard]] 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;
|
||||
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
|
||||
|
||||
[[nodiscard]] std::string getTitle() const;
|
||||
[[nodiscard]] int getWidth() const; // Honestly no idea if we'll keep these or still go with getSize.
|
||||
[[nodiscard]] int getHeight() const; // getSize wasn't working for me for logging. -maxine
|
||||
void setSizeWithoutEvent(const Vector2& size); //AAAAAHHHHHHHHH WINDOZE MAKING THINGS DIFFICULT :/ - Redacted.
|
||||
// TODO: Move out of public API, consumers should use setFullscreen()
|
||||
void fullscreen();
|
||||
// TODO: Move out of public API, consumers should use setFullscreen()
|
||||
@@ -254,6 +246,10 @@ namespace ReWindow
|
||||
// But this is at BEST, unreliable.
|
||||
Vector2 getSize() const;
|
||||
|
||||
/// Returns the position of the "renderable area" of the window relative to it's top left corner.
|
||||
/// (used to account for the width or the border & title bar).
|
||||
Vector2 getPositionOfRenderableArea() const;
|
||||
|
||||
void setPos(int x, int y);
|
||||
void setPos(const Vector2& pos);
|
||||
Vector2 getCursorPos() const;
|
||||
@@ -267,23 +263,71 @@ namespace ReWindow
|
||||
void setCursorStyle(CursorStyle style) const;
|
||||
void setCursorCustomIcon() const;
|
||||
|
||||
void setCursorLocked();
|
||||
void setCursorCenter();
|
||||
void restoreCursorFromLastCenter(); // Feels nicer for users
|
||||
|
||||
///Hides the cursor when it's inside of our window.
|
||||
///Useful for 3D game camera.
|
||||
void setCursorVisible(bool cursor_enable);
|
||||
bool getCursorVisible();
|
||||
|
||||
/// 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;
|
||||
void setLastKnownWindowSize(const Vector2& size);
|
||||
protected:
|
||||
float delta_time;
|
||||
float refresh_rate;
|
||||
int refresh_count;
|
||||
bool cursor_visible = true;
|
||||
Vector2 lastKnownWindowSize {0, 0};
|
||||
bool flags[5];
|
||||
std::vector<RWindowEvent> eventLog;
|
||||
KeyboardState currentKeyboard; // Current Frame's Keyboard State
|
||||
KeyboardState previousKeyboard; // Previous Frame's Keyboard State
|
||||
bool fullscreenmode = false;
|
||||
std::string title;
|
||||
int width;
|
||||
int height;
|
||||
RenderingAPI renderer;
|
||||
bool open = false;
|
||||
bool resizable;
|
||||
|
||||
Vector2 mouse_coords = {0, 0};
|
||||
Vector2 last_mouse_coords = {0, 0};
|
||||
private:
|
||||
Vector2 lastKnownWindowSize {0, 0};
|
||||
bool flags[5];
|
||||
std::vector<RWindowEvent> eventLog;
|
||||
KeyboardState currentKeyboard; // Current Frame's Keyboard State
|
||||
KeyboardState previousKeyboard; // Previous Frame's Keyboard State
|
||||
bool fullscreenmode = false;
|
||||
std::string title;
|
||||
int width;
|
||||
int height;
|
||||
RenderingAPI renderer;
|
||||
bool open = false;
|
||||
bool resizable;
|
||||
|
||||
|
||||
/// Returns the most accurate and recent available mouse coordinates.
|
||||
/// @note Call this version at most **once** per-frame. It polls the X-Window server and therefore is quite slow.
|
||||
/// @see getCursorPos();
|
||||
Vector2 GetAccurateMouseCoordinates() const;
|
||||
|
||||
/// Executes event handlers for keyboard rele;ase events.
|
||||
void processKeyRelease (Key key);
|
||||
/// Executes event handlers for keyboard press events.
|
||||
void processKeyPress (Key key);
|
||||
/// Executes event handlers for window close events.
|
||||
/// @note This will be invoked **before** the window-close procedure begins.
|
||||
void processOnClose();
|
||||
/// Executes event handlers for window open events.
|
||||
/// @note This will be invoked **after** the window-open procedure completes.
|
||||
void processOnOpen();
|
||||
/// Executes event handlers for mouse press events.
|
||||
void processMousePress(MouseButton btn);
|
||||
/// Executes event handlers for mouse release events.
|
||||
void processMouseRelease(MouseButton btn);
|
||||
/// Executes event handlers for window focus events.
|
||||
void processFocusIn();
|
||||
/// Executes event handlers for window unfocus events.
|
||||
void processFocusOut();
|
||||
|
||||
void processMouseMove(Vector2 last_pos, Vector2 new_pos);
|
||||
|
||||
private:
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
63
main.cpp
63
main.cpp
@@ -1,8 +1,14 @@
|
||||
#include <iostream>
|
||||
#include <rewindow/types/window.h>
|
||||
#include <jlog/jlog.hpp>
|
||||
#include <GL/gl.h>
|
||||
#include <rewindow/types/display.h>
|
||||
#include <rewindow/logger/logger.h>
|
||||
|
||||
//aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa Windows :/
|
||||
#if _WIN32
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#include <GL/gl.h>
|
||||
|
||||
Vector2 mouse_pos;
|
||||
|
||||
@@ -23,8 +29,10 @@ class MyWindow : public ReWindow::RWindow {
|
||||
glClearColor(255, 0, 0, 255);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glSwapBuffers();
|
||||
auto pos = getCursorPos();
|
||||
auto pos = GetMouseCoordinates();
|
||||
//std::cout << pos.x << ", " << pos.y << std::endl;
|
||||
|
||||
RWindow::OnRefresh(elapsed);
|
||||
}
|
||||
|
||||
bool OnResizeRequest(const ReWindow::WindowResizeRequestEvent& e) override {
|
||||
@@ -35,21 +43,48 @@ class MyWindow : public ReWindow::RWindow {
|
||||
|
||||
int main() {
|
||||
auto* window = new MyWindow("Test Window", 600, 480);
|
||||
DEBUG(std::format("New window '{}' created. width={} height={}", window->getTitle(), window->getWidth(), window->getHeight()));
|
||||
jlog::Debug(std::format("New window '{}' created. width={} height={}", window->getTitle(), window->getWidth(), window->getHeight()));
|
||||
|
||||
window->setRenderer(RenderingAPI::OPENGL);
|
||||
DEBUG(std::format("Rendering API OPENGL set for window '{}'", window->getTitle()));
|
||||
jlog::Debug(std::format("Rendering API OPENGL set for window '{}'", window->getTitle()));
|
||||
|
||||
window->Open();
|
||||
DEBUG(std::format("Opened window '{}'", window->getTitle()));
|
||||
jlog::Debug(std::format("Opened window '{}'", window->getTitle()));
|
||||
|
||||
// TODO: Cannot set flags until after window is open
|
||||
// Make this work somehow
|
||||
DEBUG("TODO: Cannot set flags until after window is open")
|
||||
jlog::Debug("TODO: Cannot set flags until after window is open");
|
||||
window->setFullscreen(false);
|
||||
window->setVsyncEnabled(false);
|
||||
window->setVsyncEnabled(true);
|
||||
window->setResizable(true);
|
||||
DEBUG(std::format("Window '{}' flags: IN_FOCUS={} FULLSCREEN={} RESIZEABLE={} VSYNC={} QUIT={}",
|
||||
window->setCursorVisible(false);
|
||||
|
||||
/*
|
||||
std::vector<ReWindow::Display> d = ReWindow::Display::getDisplaysFromWindowManager();
|
||||
for (ReWindow::Display& display : d) {
|
||||
|
||||
auto w = display.getDefaultGraphicsMode().getWidth();
|
||||
auto h = display.getDefaultGraphicsMode().getHeight();
|
||||
auto r = display.getDefaultGraphicsMode().getRefreshRate();
|
||||
|
||||
auto aspect = (float)w/(float)h;
|
||||
|
||||
std::cout << "Default: ";
|
||||
std::cout << std::format("Mode ({},{}) @ {}hz Ratio {}", w, h, r, aspect) << std::endl;
|
||||
|
||||
for (ReWindow::FullscreenGraphicsMode& fgm : display.getGraphicsModes()) {
|
||||
w = fgm.getWidth();
|
||||
h = fgm.getHeight();
|
||||
r = fgm.getRefreshRate();
|
||||
|
||||
auto aspect = (float)w/(float)h;
|
||||
|
||||
std::cout << std::format("Mode ({},{}) @ {}hz Ratio {}", w, h, r, aspect) << std::endl;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
ReWindow::Logger::Debug(std::format("Window '{}' flags: IN_FOCUS={} FULLSCREEN={} RESIZEABLE={} VSYNC={} QUIT={}",
|
||||
window->getTitle(),
|
||||
window->getFlag(RWindowFlags::IN_FOCUS),
|
||||
window->getFlag(RWindowFlags::FULLSCREEN),
|
||||
@@ -57,12 +92,14 @@ int main() {
|
||||
window->getFlag(RWindowFlags::VSYNC),
|
||||
window->getFlag(RWindowFlags::QUIT)));
|
||||
|
||||
|
||||
window->OnKeyDownEvent += [&] (ReWindow::KeyDownEvent e) {
|
||||
DEBUG(e.key.CharCode);
|
||||
jlog::Debug(e.key.CharCode);
|
||||
};
|
||||
|
||||
|
||||
window->OnMouseButtonDownEvent += [&] (ReWindow::MouseButtonDownEvent e) {
|
||||
DEBUG(e.Button.CharCode);
|
||||
jlog::Debug(e.Button.CharCode);
|
||||
};
|
||||
|
||||
while (window->isAlive()) {
|
||||
@@ -71,6 +108,7 @@ int main() {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
//Windows :(
|
||||
#ifdef _WIN32
|
||||
#ifndef UNICODE
|
||||
@@ -82,4 +120,5 @@ extern "C" {
|
||||
return main();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
*/
|
7
src/logger/logger.cpp
Normal file
7
src/logger/logger.cpp
Normal file
@@ -0,0 +1,7 @@
|
||||
#include "rewindow/logger/logger.h"
|
||||
|
||||
namespace ReWindow::Logger {
|
||||
using namespace jlog;
|
||||
GenericLogger Fatal {"ReWindow::fatal", GlobalLogFile, Colors::Reds::Crimson, Colors::Gray, Colors::Gray, Colors::Reds::Crimson, Colors::White};
|
||||
GenericLogger Debug {"ReWindow::debug", GlobalLogFile, Colors::Purples::Purple, Colors::Gray, Colors::Gray, Colors::Purples::Purple, Colors::White};
|
||||
}
|
74
src/platform/linux/display.cpp
Normal file
74
src/platform/linux/display.cpp
Normal file
@@ -0,0 +1,74 @@
|
||||
#include <rewindow/types/display.h>
|
||||
#include <X11/X.h>
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/extensions/Xrandr.h>
|
||||
#include <stdexcept>
|
||||
|
||||
using namespace ReWindow;
|
||||
|
||||
std::vector<ReWindow::Display> ReWindow::Display::getDisplaysFromWindowManager() {
|
||||
std::vector<ReWindow::Display> result;
|
||||
_XDisplay* display = XOpenDisplay(nullptr);
|
||||
|
||||
if (!display)
|
||||
throw std::runtime_error("There isn't an X11 root display????");
|
||||
|
||||
Window root_window = DefaultRootWindow(display);
|
||||
XRRScreenResources* screen_resources = XRRGetScreenResources(display, root_window);
|
||||
|
||||
if (!screen_resources)
|
||||
throw std::runtime_error("Failed to get screen resources.");
|
||||
|
||||
for (int i = 0; i < screen_resources->noutput; i++) {
|
||||
RROutput output = screen_resources->outputs[i];
|
||||
XRROutputInfo* output_info = XRRGetOutputInfo(display, screen_resources, output);
|
||||
|
||||
if (output_info->connection == RR_Connected) {
|
||||
ReWindow::Display d;
|
||||
XRRCrtcInfo* crtc_info = XRRGetCrtcInfo(display, screen_resources, output_info->crtc);
|
||||
|
||||
if (!crtc_info)
|
||||
throw std::runtime_error("Failed to get CRT info.");
|
||||
|
||||
u32 default_width = (u32) crtc_info->width;
|
||||
u32 default_height = (u32) crtc_info->height;
|
||||
short default_refresh_rate = 0;
|
||||
|
||||
for (int k = 0; k < screen_resources->nmode; k++) {
|
||||
if (screen_resources->modes[k].id == crtc_info->mode) {
|
||||
XRRModeInfo mode_info = screen_resources->modes[k];
|
||||
double refresh_rate;
|
||||
|
||||
if (mode_info.hTotal > 0 && mode_info.vTotal > 0)
|
||||
refresh_rate = mode_info.dotClock / (double)(mode_info.hTotal * mode_info.vTotal),
|
||||
default_refresh_rate = (short)refresh_rate;
|
||||
|
||||
d.default_graphics_mode = {default_width, default_height, default_refresh_rate};
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (int j = 0; j < output_info->nmode; j++) {
|
||||
RRMode mode = output_info->modes[j];
|
||||
for (int k = 0; k < screen_resources->nmode; k++) {
|
||||
if (screen_resources->modes[k].id == mode) {
|
||||
XRRModeInfo mode_info = screen_resources->modes[k];
|
||||
// Calculate refresh rate from the mode's dot clock
|
||||
double refresh_rate = 0.0;
|
||||
if (mode_info.hTotal > 0 && mode_info.vTotal > 0)
|
||||
refresh_rate = mode_info.dotClock / (double)(mode_info.hTotal * mode_info.vTotal);
|
||||
|
||||
d.graphics_modes.emplace_back((u32) mode_info.width,(u32) mode_info.height,(short) refresh_rate);
|
||||
}
|
||||
}
|
||||
}
|
||||
XRRFreeCrtcInfo(crtc_info);
|
||||
result.push_back(d);
|
||||
}
|
||||
XRRFreeOutputInfo(output_info);
|
||||
}
|
||||
|
||||
XRRFreeScreenResources(screen_resources);
|
||||
XCloseDisplay(display);
|
||||
return result;
|
||||
}
|
@@ -4,11 +4,10 @@
|
||||
#include <GL/glx.h>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <thread>
|
||||
#include <rewindow/types/window.h>
|
||||
#include <rewindow/types/cursors.h>
|
||||
#include <J3ML/J3ML.hpp>
|
||||
#include <jlog/jlog.hpp>
|
||||
#include <rewindow/logger/logger.h>
|
||||
|
||||
|
||||
|
||||
@@ -30,53 +29,100 @@ Atom windowTypeAtom;
|
||||
Atom windowTypeUtilityAtom;
|
||||
XSizeHints hints;
|
||||
GLXContext glContext;
|
||||
Cursor invisible_cursor = 0;
|
||||
|
||||
Vector2 render_area_position = {0, 0};
|
||||
Vector2 position = {0, 0};
|
||||
bool should_poll_x_for_mouse_pos = true;
|
||||
|
||||
|
||||
using namespace ReWindow;
|
||||
|
||||
void RWindow::raise() const {
|
||||
DEBUG(std::format("Raising window '{}'", this->title));
|
||||
Logger::Debug(std::format("Raising window '{}'", this->title));
|
||||
|
||||
// Get the position of the renderable area relative to the rest of the window.
|
||||
XGetWindowAttributes(display, window, &windowAttributes);
|
||||
render_area_position = { (float) windowAttributes.x, (float) windowAttributes.y };
|
||||
|
||||
XRaiseWindow(display, window);
|
||||
}
|
||||
|
||||
void RWindow::lower() const
|
||||
{
|
||||
DEBUG(std::format("Lowering window '{}'", this->title));
|
||||
Logger::Debug(std::format("Lowering window '{}'", this->title));
|
||||
XLowerWindow(display, window);
|
||||
}
|
||||
|
||||
void RWindow::destroyWindow() {
|
||||
DEBUG(std::format("Destroying window '{}'", this->title));
|
||||
Logger::Debug(std::format("Destroying window '{}'", this->title));
|
||||
XDestroySubwindows(display, window);
|
||||
DEBUG(std::format("Destroyed window '{}'", this->title));
|
||||
Logger::Debug(std::format("Destroyed window '{}'", this->title));
|
||||
XDestroyWindow(display, window);
|
||||
delete this;
|
||||
}
|
||||
|
||||
void RWindow::refresh() {
|
||||
// TODO: Implement refresh time keeping
|
||||
OnRefresh(0.f);
|
||||
|
||||
// TODO: Check if mouse coords have changed, only then fire OnMouseMove event
|
||||
Vector2 mouse_coords = getCursorPos();
|
||||
auto begin_frame = std::chrono::high_resolution_clock::now();
|
||||
|
||||
|
||||
OnRefresh(delta_time);
|
||||
|
||||
// Only call once and cache the result.
|
||||
mouse_coords = GetAccurateMouseCoordinates();
|
||||
|
||||
/// TODO: Implement optional minimum epsilon to trigger a Mouse Update.
|
||||
if (mouse_coords != last_mouse_coords) {
|
||||
processMouseMove(last_mouse_coords, mouse_coords);
|
||||
last_mouse_coords = mouse_coords;
|
||||
}
|
||||
|
||||
auto end_frame = std::chrono::high_resolution_clock::now();
|
||||
|
||||
auto frame_time = end_frame - begin_frame;
|
||||
int frame_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(frame_time).count();
|
||||
float frame_time_s = frame_time_ms / 1000.f;
|
||||
delta_time = frame_time_ms;
|
||||
|
||||
refresh_rate = 1.f / delta_time;
|
||||
|
||||
|
||||
refresh_count++;
|
||||
|
||||
|
||||
auto eventData = MouseMoveEvent(mouse_coords);
|
||||
OnMouseMove(eventData);
|
||||
OnMouseMoveEvent(eventData);
|
||||
//std::this_thread::sleep_for(1ms);
|
||||
}
|
||||
|
||||
void RWindow::setCursorVisible(bool cursor_enable) {
|
||||
cursor_visible = cursor_enable;
|
||||
|
||||
if (invisible_cursor == 0) {
|
||||
Pixmap blank_pixmap = XCreatePixmap(display, window, 1, 1, 1);
|
||||
XColor dummy; dummy.pixel = 0; dummy.red = 0; dummy.flags = 0;
|
||||
invisible_cursor = XCreatePixmapCursor(display, blank_pixmap, blank_pixmap, &dummy, &dummy, 0, 0);
|
||||
XFreePixmap(display, blank_pixmap);
|
||||
}
|
||||
|
||||
if (!cursor_enable)
|
||||
XDefineCursor(display, window, invisible_cursor);
|
||||
|
||||
if (cursor_enable)
|
||||
XUndefineCursor(display, window);
|
||||
}
|
||||
|
||||
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) {
|
||||
DEBUG("Once you've done this you cannot make it resizable again.");
|
||||
Logger::Debug("Once you've done this you cannot make it resizable again.");
|
||||
hints.flags = PMinSize | PMaxSize;
|
||||
hints.min_width = hints.max_width = windowAttributes.width;
|
||||
hints.min_height = hints.max_height = windowAttributes.height;
|
||||
XSetWMNormalHints(display, window, &hints);
|
||||
}
|
||||
DEBUG(std::format("Set flag '{}' to state '{}' for window '{}'", RWindowFlagToStr(flag), state, this->title));
|
||||
Logger::Debug(std::format("Set flag '{}' to state '{}' for window '{}'", RWindowFlagToStr(flag), state, this->title));
|
||||
}
|
||||
|
||||
void RWindow::pollEvents() {
|
||||
@@ -84,94 +130,101 @@ void RWindow::pollEvents() {
|
||||
XNextEvent(display, &xev);
|
||||
|
||||
if (xev.type == ClientMessage)
|
||||
DEBUG(std::format("Recieved event '{}'", "ClientMessage"));
|
||||
Logger::Debug(std::format("Recieved event '{}'", "ClientMessage"));
|
||||
|
||||
if (xev.xclient.message_type == XInternAtom(display, "WM_PROTOCOLS", False) && static_cast<Atom>(xev.xclient.data.l[0]) == wmDeleteWindow) {
|
||||
|
||||
processOnClose();
|
||||
destroyWindow();
|
||||
system("xset r on");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (xev.type == FocusIn) {
|
||||
DEBUG(std::format("Recieved event '{}'", "FocusIn"));
|
||||
Logger::Debug(std::format("Recieved event '{}'", "FocusIn"));
|
||||
XAutoRepeatOff(display);
|
||||
//focusGained.Invoke();
|
||||
RWindowEvent event {};
|
||||
OnFocusGain(event);
|
||||
OnFocusGainEvent(event);
|
||||
setFlag(RWindowFlags::IN_FOCUS, true);
|
||||
|
||||
if (!cursor_visible)
|
||||
XDefineCursor(display, window, invisible_cursor);
|
||||
|
||||
// Get the position of the renderable area relative to the rest of the window.
|
||||
XGetWindowAttributes(display, window, &windowAttributes);
|
||||
render_area_position = { (float) windowAttributes.x, (float) windowAttributes.y };
|
||||
processFocusIn();
|
||||
}
|
||||
|
||||
if (xev.type == FocusOut) {
|
||||
DEBUG(std::format("Recieved event '{}'", "FocusOut"));
|
||||
Logger::Debug(std::format("Recieved event '{}'", "FocusOut"));
|
||||
XAutoRepeatOn(display);
|
||||
//focusLost.Invoke();
|
||||
RWindowEvent event {};
|
||||
OnFocusLost(event);
|
||||
OnFocusLostEvent(event);
|
||||
|
||||
setFlag(RWindowFlags::IN_FOCUS, false);
|
||||
if (!cursor_visible)
|
||||
XUndefineCursor(display, window);
|
||||
|
||||
processFocusOut();
|
||||
}
|
||||
|
||||
if (xev.type == KeyRelease) {
|
||||
DEBUG(std::format("Recieved event '{}'", "KeyRelease"));
|
||||
Logger::Debug(std::format("Recieved event '{}'", "KeyRelease"));
|
||||
auto scancode = (X11Scancode) xev.xkey.keycode;
|
||||
auto key = GetKeyFromX11Scancode(scancode);
|
||||
OnKeyUpEvent(key);
|
||||
OnKeyUp(key);
|
||||
liftKey(key);
|
||||
processKeyRelease(key);
|
||||
}
|
||||
|
||||
if (xev.type == KeyPress) {
|
||||
DEBUG(std::format("Recieved event '{}'", "KeyPress"));
|
||||
Logger::Debug(std::format("Recieved event '{}'", "KeyPress"));
|
||||
auto scancode = (X11Scancode) xev.xkey.keycode;
|
||||
auto key = GetKeyFromX11Scancode(scancode);
|
||||
OnKeyDownEvent(key);
|
||||
OnKeyDown(key);
|
||||
pressKey(key);
|
||||
//eventLog.push_back(eventData);
|
||||
processKeyPress(key);
|
||||
}
|
||||
|
||||
if (xev.type == ButtonRelease) {
|
||||
DEBUG(std::format("Recieved event '{}'", "ButtonRelease"));
|
||||
Logger::Debug(std::format("Recieved event '{}'", "ButtonRelease"));
|
||||
MouseButton button = GetMouseButtonFromXButton(xev.xbutton.button);
|
||||
auto eventData = MouseButtonUpEvent();
|
||||
eventData.Button = button;
|
||||
|
||||
OnMouseButtonUpEvent(eventData);
|
||||
OnMouseButtonUp(eventData);
|
||||
processMouseRelease(button);
|
||||
}
|
||||
|
||||
if (xev.type == ButtonPress) {
|
||||
DEBUG(std::format("Recieved event '{}'", "ButtonPress"));
|
||||
MouseButton button = GetMouseButtonFromXButton(xev.xbutton.button);
|
||||
auto eventData = MouseButtonDownEvent(button);
|
||||
eventData.Button = button;
|
||||
|
||||
OnMouseButtonDownEvent(eventData);
|
||||
OnMouseButtonDown(eventData);
|
||||
Logger::Debug(std::format("Window event: MouseButtonPress {}", button.CharCode));
|
||||
|
||||
processMousePress(button);
|
||||
}
|
||||
|
||||
if (xev.type == Expose)
|
||||
{
|
||||
DEBUG(std::format("Recieved event '{}'", "Expose"));
|
||||
Logger::Debug(std::format("Recieved event '{}'", "Expose"));
|
||||
}
|
||||
|
||||
// NOTE: This event is functionally useless, as it only informs of the very beginning and end of a mouse movement.
|
||||
if (xev.type == MotionNotify)
|
||||
{
|
||||
DEBUG(std::format("Recieved event '{}'", "MotionNotify"));
|
||||
Logger::Debug(std::format("Recieved event '{}'", "MotionNotify"));
|
||||
}
|
||||
|
||||
if (xev.type == ResizeRequest) {
|
||||
DEBUG(std::format("Recieved event '{}'", "ResizeRequest"));
|
||||
this->width = xev.xresizerequest.width;
|
||||
this->height = xev.xresizerequest.height;
|
||||
auto eventData = WindowResizeRequestEvent();
|
||||
eventData.Size = {(float)xev.xresizerequest.width, (float)xev.xresizerequest.height};
|
||||
lastKnownWindowSize = eventData.Size;
|
||||
OnResizeRequest(eventData);
|
||||
OnResizeRequestEvent(eventData);
|
||||
glViewport(0, 0, (GLsizei)xev.xresizerequest.width, (GLsizei)xev.xresizerequest.height);
|
||||
if (xev.type == ConfigureNotify) {
|
||||
if (this->width != xev.xconfigurerequest.width || this->height != xev.xconfigurerequest.height) {
|
||||
Logger::Debug(std::format("Recieved event '{}'", "ResizeRequest"));
|
||||
|
||||
this->width = xev.xconfigurerequest.width;
|
||||
this->height = xev.xconfigurerequest.height;
|
||||
|
||||
auto eventData = WindowResizeRequestEvent();
|
||||
eventData.Size = { (float) xev.xconfigurerequest.width, (float) xev.xconfigurerequest.height };
|
||||
lastKnownWindowSize = eventData.Size;
|
||||
|
||||
OnResizeRequest(eventData);
|
||||
OnResizeRequestEvent(eventData);
|
||||
}
|
||||
|
||||
//Window Moved.
|
||||
if (position.x != xev.xconfigurerequest.x || position.y != xev.xconfigurerequest.y)
|
||||
position = { (float) xev.xconfigurerequest.x, (float) xev.xconfigurerequest.y };
|
||||
}
|
||||
|
||||
}
|
||||
previousKeyboard = currentKeyboard;
|
||||
}
|
||||
@@ -185,41 +238,31 @@ void RWindow::setSize(int newWidth, int newHeight) {
|
||||
this->height = newHeight;
|
||||
XResizeWindow(display, window, newWidth, newHeight);
|
||||
XFlush(display);
|
||||
glViewport(0, 0, width, height);
|
||||
DEBUG(std::format("Set size for window '{}'. width={} height={}", this->title, newWidth, newHeight));
|
||||
Logger::Debug(std::format("Set size for window '{}'. width={} height={}", this->title, newWidth, newHeight));
|
||||
}
|
||||
|
||||
Vector2 RWindow::getCursorPos() const {
|
||||
Window root = XDefaultRootWindow(display);
|
||||
Window root_return;
|
||||
Window child_return;
|
||||
int root_x_ret;
|
||||
int root_y_ret;
|
||||
int win_x_ret;
|
||||
int win_y_ret;
|
||||
Vector2 RWindow::GetAccurateMouseCoordinates() const {
|
||||
|
||||
Window root_return, child_return;
|
||||
int root_x_ret, root_y_ret;
|
||||
int win_x_ret, win_y_ret;
|
||||
uint32_t mask_return;
|
||||
unsigned m;
|
||||
|
||||
// This seems to be relative to the top left corner of the renderable area.
|
||||
bool mouseAvailable = XQueryPointer(display, window, &root_return, &child_return, &root_x_ret, &root_y_ret, &win_x_ret, &win_y_ret, &mask_return);
|
||||
|
||||
if (mouseAvailable) {
|
||||
// TODO: process retrieved mouse coordinates
|
||||
// TODO: normalize coordinates from displaySpace to windowSpace
|
||||
// TODO: fire mouse movement event
|
||||
//std::cout << win_x_ret << ", " << win_y_ret << std::endl;
|
||||
// TODO: Compensate for height of window TitleBar + window border width
|
||||
float window_border_width = 2;
|
||||
float window_titlebar_height = 18;
|
||||
Vector2 mouse_coords_raw = {(float)win_x_ret+window_border_width, (float)win_y_ret+window_titlebar_height};
|
||||
auto window_pos = getPos();
|
||||
return mouse_coords_raw - window_pos;
|
||||
Vector2 m_coords = { (float) win_x_ret, (float) win_y_ret };
|
||||
return m_coords;
|
||||
}
|
||||
return Vector2::Zero;
|
||||
}
|
||||
|
||||
// TODO: implement integer vector2/3 types
|
||||
Vector2 RWindow::getSize() const {
|
||||
XGetWindowAttributes(display,window,&windowAttributes);
|
||||
return {(float)windowAttributes.width, (float)windowAttributes.height};
|
||||
return {(float) this->width, (float) this->height};
|
||||
}
|
||||
|
||||
Vector2 RWindow::getLastKnownResize() const
|
||||
@@ -229,16 +272,16 @@ Vector2 RWindow::getLastKnownResize() const
|
||||
|
||||
// TODO: implement integer vector2/3 types
|
||||
Vector2 RWindow::getPos() const {
|
||||
XGetWindowAttributes(display,window,&windowAttributes);
|
||||
return {(float)windowAttributes.x, (float)windowAttributes.y};
|
||||
return position;
|
||||
}
|
||||
|
||||
void RWindow::setPos(int x, int y) {
|
||||
XMoveWindow(display, window, x, y);
|
||||
position = { (float) x, (float) y };
|
||||
}
|
||||
|
||||
void RWindow::setPos(const Vector2& pos) {
|
||||
this->setPos(pos.x, pos.y);
|
||||
setPos(pos.x, pos.y);
|
||||
}
|
||||
|
||||
|
||||
@@ -247,11 +290,11 @@ void RWindow::glSwapBuffers() {
|
||||
}
|
||||
|
||||
bool RWindow::isResizable() const {
|
||||
return this->getFlag(RWindowFlags::RESIZABLE);
|
||||
return getFlag(RWindowFlags::RESIZABLE);
|
||||
}
|
||||
|
||||
void RWindow::fullscreen() {
|
||||
DEBUG(std::format("Fullscreening window '{}'", this->title));
|
||||
Logger::Debug(std::format("Fullscreening window '{}'", this->title));
|
||||
fullscreenmode = true;
|
||||
|
||||
XEvent xev;
|
||||
@@ -268,11 +311,11 @@ void RWindow::fullscreen() {
|
||||
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));
|
||||
Logger::Debug(std::format("Fullscreened window '{}'", this->title));
|
||||
}
|
||||
|
||||
void RWindow::restoreFromFullscreen() {
|
||||
DEBUG(std::format("Restoring window '{}' from fullscreen", this->title));
|
||||
Logger::Debug(std::format("Restoring window '{}' from fullscreen", this->title));
|
||||
fullscreenmode = false;
|
||||
XEvent xev;
|
||||
Atom wm_state = XInternAtom(display, "_NET_WM_STATE", False);
|
||||
@@ -286,7 +329,7 @@ void RWindow::restoreFromFullscreen() {
|
||||
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));
|
||||
Logger::Debug(std::format("Restored window '{}' from fullscreen", this->title));
|
||||
}
|
||||
|
||||
void RWindow::setVsyncEnabled(bool b) {
|
||||
@@ -332,7 +375,7 @@ void RWindow::Open() {
|
||||
ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
|
||||
PointerMotionMask |
|
||||
PointerMotionHintMask | FocusChangeMask | StructureNotifyMask | SubstructureRedirectMask |
|
||||
SubstructureNotifyMask | CWColormap | ResizeRequest);
|
||||
SubstructureNotifyMask | CWColormap);
|
||||
XMapWindow(display, window);
|
||||
XStoreName(display, window, title.c_str());
|
||||
wmDeleteWindow = XInternAtom(display, "WM_DELETE_WINDOW", False);
|
||||
@@ -340,7 +383,14 @@ void RWindow::Open() {
|
||||
|
||||
if (renderer == RenderingAPI::OPENGL)
|
||||
glXMakeCurrent(display, window, glContext);
|
||||
|
||||
// Get the position of the renderable area relative to the rest of the window.
|
||||
XGetWindowAttributes(display, window, &windowAttributes);
|
||||
render_area_position = { (float) windowAttributes.x, (float) windowAttributes.y };
|
||||
open = true;
|
||||
|
||||
|
||||
processOnOpen();
|
||||
}
|
||||
|
||||
void RWindow::setTitle(const std::string &title) {
|
||||
@@ -353,6 +403,10 @@ bool RWindow::isMouseButtonDown(MouseButton button) const {
|
||||
return false;
|
||||
}
|
||||
|
||||
Vector2 RWindow::getPositionOfRenderableArea() const {
|
||||
return render_area_position;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
34
src/platform/shared/display.cpp
Normal file
34
src/platform/shared/display.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#include <rewindow/types/display.h>
|
||||
|
||||
using namespace ReWindow;
|
||||
|
||||
FullscreenGraphicsMode::FullscreenGraphicsMode(u32 width, u32 height, short refresh_rate) {
|
||||
this->width = width;
|
||||
this->height = height;
|
||||
this->refresh_rate = refresh_rate;
|
||||
}
|
||||
|
||||
u32 FullscreenGraphicsMode::getWidth() const {
|
||||
return width;
|
||||
}
|
||||
|
||||
u32 FullscreenGraphicsMode::getHeight() const {
|
||||
return height;
|
||||
}
|
||||
|
||||
float FullscreenGraphicsMode::getRefreshRate() const {
|
||||
return refresh_rate;
|
||||
}
|
||||
|
||||
FullscreenGraphicsMode Display::getDefaultGraphicsMode() {
|
||||
return default_graphics_mode;
|
||||
}
|
||||
|
||||
std::vector<FullscreenGraphicsMode> Display::getGraphicsModes() {
|
||||
return graphics_modes;
|
||||
}
|
||||
|
||||
Display::Display(const FullscreenGraphicsMode& default_graphics_mode, const std::vector<FullscreenGraphicsMode>& graphics_modes) {
|
||||
this->default_graphics_mode = default_graphics_mode;
|
||||
this->graphics_modes = graphics_modes;
|
||||
}
|
@@ -1,5 +1,5 @@
|
||||
#include <rewindow/types/window.h>
|
||||
#include <jlog/jlog.hpp>
|
||||
#include <rewindow/types/window.h>
|
||||
#include "rewindow/logger/logger.h"
|
||||
std::string RWindowFlagToStr(RWindowFlags flag) {
|
||||
switch (flag) {
|
||||
case RWindowFlags::IN_FOCUS: return "IN_FOCUS";
|
||||
@@ -9,7 +9,6 @@ std::string RWindowFlagToStr(RWindowFlags flag) {
|
||||
case RWindowFlags::QUIT: return "QUIT";
|
||||
case RWindowFlags::MAX_FLAG: return "MAX_FLAG";
|
||||
default:
|
||||
FATAL("")
|
||||
return "unimplemented flag";
|
||||
}
|
||||
};
|
||||
@@ -34,10 +33,12 @@ RWindow::RWindow(const std::string& title, int width, int height, RenderingAPI r
|
||||
title(title), width(width), height(height), renderer(renderer) {}
|
||||
|
||||
Vector2 RWindow::GetMouseCoordinates() const {
|
||||
return getCursorPos();
|
||||
return mouse_coords;
|
||||
}
|
||||
|
||||
bool RWindow::getFlag(RWindowFlags flag) const {return flags[(int)flag];}
|
||||
bool RWindow::getFlag(RWindowFlags flag) const {
|
||||
return flags[(int) flag];
|
||||
}
|
||||
|
||||
bool RWindow::isAlive() const {
|
||||
return true;
|
||||
@@ -55,10 +56,46 @@ void RWindow::setFullscreen(bool fs) {
|
||||
restoreFromFullscreen();
|
||||
}
|
||||
|
||||
void RWindow::liftKey(Key key) {
|
||||
void RWindow::processFocusIn()
|
||||
{
|
||||
RWindowEvent event {};
|
||||
OnFocusGain(event);
|
||||
OnFocusGainEvent(event);
|
||||
}
|
||||
|
||||
void RWindow::processFocusOut()
|
||||
{
|
||||
RWindowEvent event {};
|
||||
OnFocusLost(event);
|
||||
OnFocusLostEvent(event);
|
||||
}
|
||||
|
||||
void RWindow::processMousePress(MouseButton btn)
|
||||
{
|
||||
auto eventData = MouseButtonDownEvent(btn);
|
||||
OnMouseButtonDownEvent(eventData);
|
||||
OnMouseButtonDown(eventData);
|
||||
}
|
||||
|
||||
void RWindow::processMouseMove(Vector2 last_pos, Vector2 new_pos)
|
||||
{
|
||||
auto eventData = MouseMoveEvent(new_pos);
|
||||
OnMouseMove(eventData);
|
||||
OnMouseMoveEvent(eventData);
|
||||
}
|
||||
|
||||
void RWindow::processMouseRelease(MouseButton btn)
|
||||
{
|
||||
auto eventData = MouseButtonUpEvent(btn);
|
||||
OnMouseButtonUpEvent(eventData);
|
||||
OnMouseButtonUp(eventData);
|
||||
}
|
||||
|
||||
void RWindow::processKeyRelease(Key key) {
|
||||
currentKeyboard.PressedKeys[key] = false;
|
||||
auto event = ReWindow::WindowEvents::KeyUpEvent(key);
|
||||
auto event = KeyUpEvent(key);
|
||||
OnKeyUp(event);
|
||||
OnKeyUpEvent(key);
|
||||
}
|
||||
|
||||
|
||||
@@ -83,6 +120,14 @@ int RWindow::getHeight() const
|
||||
return this->height;
|
||||
}
|
||||
|
||||
void RWindow::setSizeWithoutEvent(const Vector2& size) {
|
||||
width = size.x;
|
||||
height = size.y;
|
||||
}
|
||||
|
||||
void RWindow::setLastKnownWindowSize(const Vector2& size) {
|
||||
lastKnownWindowSize = size;
|
||||
}
|
||||
|
||||
void RWindow::setRenderer(RenderingAPI api) {
|
||||
renderer = api;
|
||||
@@ -101,14 +146,27 @@ bool RWindow::isKeyDown(Key key) const {
|
||||
return false;
|
||||
}
|
||||
|
||||
void RWindow::pressKey(Key key) {
|
||||
void RWindow::processKeyPress(Key key) {
|
||||
currentKeyboard.PressedKeys[key] = true;
|
||||
auto eventData = KeyDownEvent(key);
|
||||
OnKeyDown(eventData);
|
||||
OnKeyDownEvent(key);
|
||||
}
|
||||
|
||||
void RWindow::processOnClose()
|
||||
{
|
||||
OnClosing();
|
||||
OnClosingEvent();
|
||||
}
|
||||
|
||||
void RWindow::processOnOpen()
|
||||
{
|
||||
OnOpen();
|
||||
OnOpenEvent();
|
||||
}
|
||||
|
||||
|
||||
void RWindow::Close()
|
||||
{
|
||||
|
||||
void RWindow::Close() {
|
||||
/// TODO: Implement closing the window without destroying the handle.
|
||||
processOnClose();
|
||||
}
|
@@ -6,7 +6,7 @@ using namespace ReWindow;
|
||||
|
||||
bool fullscreenmode = false;
|
||||
bool open = false;
|
||||
HINSTANCE hInstance = GetModuleHandle(NULL);
|
||||
HINSTANCE hInstance = GetModuleHandle(nullptr);
|
||||
HWND hwnd;
|
||||
HDC hdc;
|
||||
HGLRC glContext;
|
||||
@@ -48,6 +48,15 @@ Vector2 RWindow::getCursorPos() const {
|
||||
return { (float)point.x, (float)point.y };
|
||||
}
|
||||
|
||||
void RWindow::setCursorVisible(bool cursor_enable) {
|
||||
cursor_visible = cursor_enable;
|
||||
}
|
||||
|
||||
bool RWindow::getCursorVisible() {
|
||||
return cursor_visible;
|
||||
}
|
||||
|
||||
|
||||
Vector2 RWindow::getSize() const {
|
||||
RECT rect;
|
||||
GetClientRect(hwnd, &rect);
|
||||
@@ -76,8 +85,7 @@ void RWindow::restoreFromFullscreen() {
|
||||
|
||||
void RWindow::setVsyncEnabled(bool b) {
|
||||
typedef BOOL(WINAPI* PFNWGLSWAPINTERVALEXTPROC)(int interval);
|
||||
PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = nullptr;
|
||||
wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC) wglGetProcAddress("wglSwapIntervalEXT");
|
||||
auto wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC) wglGetProcAddress("wglSwapIntervalEXT");
|
||||
|
||||
if (wglSwapIntervalEXT)
|
||||
wglSwapIntervalEXT(b ? 1 : 0);
|
||||
@@ -90,39 +98,182 @@ bool RWindow::isFullscreen() const {
|
||||
RWindow* eWindow = nullptr;
|
||||
KeyboardState* pKeyboard = nullptr;
|
||||
KeyboardState* cKeyboard = nullptr;
|
||||
|
||||
//Event loop.
|
||||
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
|
||||
switch (uMsg) {
|
||||
case WM_CLOSE:
|
||||
DestroyWindow(hwnd);
|
||||
case WM_DESTROY:
|
||||
exit(0);
|
||||
case WM_SIZE:
|
||||
break;
|
||||
case WM_KEYDOWN: {
|
||||
auto key = GetKeyFromWindowsScancode((WindowsScancode) wParam);
|
||||
//Key repeat fix.
|
||||
if (!pKeyboard->PressedKeys[key]) {
|
||||
eWindow->OnKeyDownEvent(key);
|
||||
eWindow->OnKeyDown(key);
|
||||
eWindow->pressKey(key);
|
||||
case WM_CLOSE: {
|
||||
DestroyWindow(hwnd);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case WM_KEYUP: {
|
||||
auto key = GetKeyFromWindowsScancode((WindowsScancode) wParam);
|
||||
eWindow->OnKeyUpEvent(key);
|
||||
eWindow->OnKeyUp(key);
|
||||
eWindow->liftKey(key);
|
||||
break;
|
||||
}
|
||||
case WM_LBUTTONDOWN:
|
||||
break;
|
||||
case WM_LBUTTONUP:
|
||||
|
||||
case WM_DESTROY: {
|
||||
exit(0);
|
||||
}
|
||||
|
||||
case WM_SIZE: {
|
||||
eWindow->setSizeWithoutEvent({(float) LOWORD(lParam), (float) HIWORD(lParam)});
|
||||
auto eventData = WindowResizeRequestEvent();
|
||||
eventData.Size = {(float) eWindow->getWidth(), (float) eWindow->getHeight()};
|
||||
eWindow->setLastKnownWindowSize({(float) eWindow->getWidth(), (float) eWindow->getHeight()});
|
||||
eWindow->OnResizeRequest(eventData);
|
||||
eWindow->OnResizeRequestEvent(eventData);
|
||||
//Just to be absolutely sure the OpenGL viewport resizes along with the window.
|
||||
glViewport(0, 0, eWindow->getWidth(), eWindow->getHeight());
|
||||
break;
|
||||
}
|
||||
|
||||
case WM_SETFOCUS: {
|
||||
RWindowEvent event {};
|
||||
eWindow->OnFocusGain(event);
|
||||
eWindow->OnFocusGainEvent(event);
|
||||
|
||||
eWindow->setFlag(RWindowFlags::IN_FOCUS, true);
|
||||
break;
|
||||
}
|
||||
|
||||
case WM_KILLFOCUS: {
|
||||
RWindowEvent event {};
|
||||
eWindow->OnFocusLost(event);
|
||||
eWindow->OnFocusLostEvent(event);
|
||||
|
||||
eWindow->setFlag(RWindowFlags::IN_FOCUS, false);
|
||||
break;
|
||||
}
|
||||
|
||||
case WM_SETCURSOR: {
|
||||
if (LOWORD(lParam) == HTCLIENT && eWindow->getCursorVisible() == false)
|
||||
SetCursor(nullptr);
|
||||
break;
|
||||
}
|
||||
|
||||
case WM_KEYDOWN: {
|
||||
auto key = GetKeyFromWindowsScancode((WindowsScancode) wParam);
|
||||
//Key repeat fix.
|
||||
if (!pKeyboard->PressedKeys[key]) {
|
||||
eWindow->OnKeyDownEvent(key);
|
||||
eWindow->OnKeyDown(key);
|
||||
eWindow->pressKey(key);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case WM_KEYUP: {
|
||||
auto key = GetKeyFromWindowsScancode((WindowsScancode) wParam);
|
||||
eWindow->OnKeyUpEvent(key);
|
||||
eWindow->OnKeyUp(key);
|
||||
eWindow->liftKey(key);
|
||||
break;
|
||||
}
|
||||
|
||||
//Mouse Buttons.
|
||||
case WM_MOUSEWHEEL: {
|
||||
auto eventData = MouseButtonDownEvent();
|
||||
int wheel_delta = GET_WHEEL_DELTA_WPARAM(wParam);
|
||||
|
||||
//If the wheel delta is positive it's MWheelUp, Negative is MWheelDown.
|
||||
if (wheel_delta > 0)
|
||||
eventData.Button = MouseButtons::MWheelUp,
|
||||
eWindow->OnMouseButtonDownEvent(eventData),
|
||||
eWindow->OnMouseButtonDown(eventData);
|
||||
|
||||
else
|
||||
eventData.Button = MouseButtons::MWheelDown,
|
||||
eWindow->OnMouseButtonDownEvent(eventData),
|
||||
eWindow->OnMouseButtonDown(eventData);
|
||||
break;
|
||||
}
|
||||
|
||||
case WM_LBUTTONDOWN: {
|
||||
auto eventData = MouseButtonDownEvent();
|
||||
eventData.Button = MouseButtons::Left;
|
||||
|
||||
eWindow->OnMouseButtonDownEvent(eventData);
|
||||
eWindow->OnMouseButtonDown(eventData);
|
||||
break;
|
||||
}
|
||||
|
||||
case WM_LBUTTONUP: {
|
||||
auto eventData = MouseButtonUpEvent();
|
||||
eventData.Button = MouseButtons::Left;
|
||||
|
||||
eWindow->OnMouseButtonUpEvent(eventData);
|
||||
eWindow->OnMouseButtonUp(eventData);
|
||||
break;
|
||||
}
|
||||
|
||||
case WM_RBUTTONDOWN: {
|
||||
auto eventData = MouseButtonDownEvent();
|
||||
eventData.Button = MouseButtons::Right;
|
||||
|
||||
eWindow->OnMouseButtonDownEvent(eventData);
|
||||
eWindow->OnMouseButtonDown(eventData);
|
||||
break;
|
||||
}
|
||||
|
||||
case WM_RBUTTONUP: {
|
||||
auto eventData = MouseButtonUpEvent();
|
||||
eventData.Button = MouseButtons::Right;
|
||||
|
||||
eWindow->OnMouseButtonUpEvent(eventData);
|
||||
eWindow->OnMouseButtonUp(eventData);
|
||||
break;
|
||||
}
|
||||
|
||||
case WM_MBUTTONDOWN: {
|
||||
auto eventData = MouseButtonDownEvent();
|
||||
eventData.Button = MouseButtons::Middle;
|
||||
|
||||
eWindow->OnMouseButtonDownEvent(eventData);
|
||||
eWindow->OnMouseButtonDown(eventData);
|
||||
break;
|
||||
}
|
||||
|
||||
case WM_MBUTTONUP: {
|
||||
auto eventData = MouseButtonUpEvent();
|
||||
eventData.Button = MouseButtons::Middle;
|
||||
|
||||
eWindow->OnMouseButtonUpEvent(eventData);
|
||||
eWindow->OnMouseButtonUp(eventData);
|
||||
break;
|
||||
}
|
||||
|
||||
case WM_XBUTTONDOWN: {
|
||||
WORD button = GET_XBUTTON_WPARAM(wParam);
|
||||
auto eventData = MouseButtonDownEvent();
|
||||
|
||||
if (button == XBUTTON1)
|
||||
eventData.Button = MouseButtons::Mouse4,
|
||||
eWindow->OnMouseButtonDownEvent(eventData),
|
||||
eWindow->OnMouseButtonDown(eventData);
|
||||
|
||||
if (button == XBUTTON2)
|
||||
eventData.Button = MouseButtons::Mouse5,
|
||||
eWindow->OnMouseButtonDownEvent(eventData),
|
||||
eWindow->OnMouseButtonDown(eventData);
|
||||
break;
|
||||
}
|
||||
|
||||
case WM_XBUTTONUP: {
|
||||
WORD button = GET_XBUTTON_WPARAM(wParam);
|
||||
auto eventData = MouseButtonUpEvent();
|
||||
|
||||
if (button == XBUTTON1)
|
||||
eventData.Button = MouseButtons::Mouse4,
|
||||
eWindow->OnMouseButtonUpEvent(eventData),
|
||||
eWindow->OnMouseButtonUp(eventData);
|
||||
|
||||
if (button == XBUTTON2)
|
||||
eventData.Button = MouseButtons::Mouse5,
|
||||
eWindow->OnMouseButtonUpEvent(eventData),
|
||||
eWindow->OnMouseButtonUp(eventData);
|
||||
break;
|
||||
}
|
||||
|
||||
//This is the same as "Motion Notify" in the X Window System.
|
||||
case WM_MOUSEMOVE:
|
||||
break;
|
||||
break;
|
||||
}
|
||||
|
||||
if (pKeyboard != nullptr && cKeyboard != nullptr)
|
||||
pKeyboard = cKeyboard;
|
||||
|
||||
@@ -196,5 +347,4 @@ void RWindow::refresh() {
|
||||
|
||||
auto eventData = MouseMoveEvent(mouse_coords);
|
||||
OnMouseMove(eventData);
|
||||
std::this_thread::sleep_for(1ms);
|
||||
}
|
@@ -1 +0,0 @@
|
||||
// Base Window Implementation - Anything that is consistent across Windows, Linux, etc
|
@@ -1,5 +1,5 @@
|
||||
#include <rewindow/types/key.h>
|
||||
#include <jlog/jlog.hpp>
|
||||
//#include <rewindow/logger.h>
|
||||
//std::vector<Key> Key::keyboard = {};
|
||||
|
||||
std::vector<Key> Key::GetKeyboard() { return keyboard; }
|
||||
@@ -29,7 +29,7 @@ bool Key::operator<(const Key &rhs) const {
|
||||
|
||||
|
||||
Key GetKeyFromX11Scancode(X11Scancode code) {
|
||||
for (auto& key : Key::GetKeyboard())
|
||||
for (const auto& key : Key::GetKeyboard())
|
||||
if (key.x11ScanCode == code)
|
||||
return key;
|
||||
std::cout << "Unavaliable Scancode: " + std::to_string((int)code) << std::endl;
|
||||
|
@@ -1,6 +1,6 @@
|
||||
#include <rewindow/types/mousebutton.h>
|
||||
#include <string>
|
||||
#include <jlog/jlog.hpp>
|
||||
#include "rewindow/logger/logger.h"
|
||||
|
||||
MouseButton::MouseButton() {
|
||||
}
|
||||
@@ -26,7 +26,7 @@ MouseButton GetMouseButtonFromXButton(unsigned int button) {
|
||||
case 8: return MouseButtons::Mouse4;
|
||||
case 9: return MouseButtons::Mouse5;
|
||||
default: {
|
||||
FATAL("Undefined XButtonCode: " + std::to_string((int) button));
|
||||
ReWindow::Logger::Fatal("Undefined XButtonCode: " + std::to_string((int) button));
|
||||
return MouseButtons::Unimplemented;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user