Compare commits

...

12 Commits

Author SHA1 Message Date
maxine
3617d610ea Merge pull request 'brokenbullshit' (#12) from brokenbullshit into main
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m56s
Reviewed-on: #12
2024-08-23 11:01:00 -04:00
9500681420 fixed to use new JLog and Event APIs. New ReWindow specific logger too. Should be able to toggle it on and off in programs that use ReWindow as a dependency and still have other loggers working.
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 6m48s
2024-08-23 11:02:03 -04:00
bf41652336 Update CMakeLists.txt
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 5m39s
Still broken but closer I guess.
2024-08-22 12:45:54 -04:00
68ae72e6be broken SHIT
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 6m13s
2024-08-22 12:00:26 -04:00
a4b68a5abc Update display.cpp
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 6m21s
both displays
2024-08-13 13:21:22 -04:00
1345669c32 Add Aspect ratio output
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 6m31s
2024-08-13 12:52:57 -04:00
9d51ac0026 Test Info Output Format
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Has been cancelled
2024-08-13 12:48:27 -04:00
edb2e29246 poll for graphics modes.
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 4m48s
2024-08-13 12:41:29 -04:00
bed600d4d4 Make resizing on windows actually send the event.
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m36s
2024-08-10 23:43:15 -04:00
acf66ae909 When the cursor is hidden, Still show it on the title bar (WINDOWS)
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m25s
2024-08-09 19:01:38 -04:00
c319acfffb Set cursor visible (Linux)
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 24m43s
2024-08-08 20:43:27 -04:00
9b810101a4 Set cursor visible (windows)
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m32s
2024-08-08 20:26:25 -04:00
15 changed files with 471 additions and 188 deletions

View File

@@ -18,17 +18,17 @@ include(cmake/CPM.cmake)
CPMAddPackage(
NAME J3ML
URL https://git.redacted.cc/josh/j3ml/archive/Release-3.0.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-10.zip
)
CPMAddPackage(
NAME jlog
URL https://git.redacted.cc/josh/jlog/archive/Prerelease-12.zip
URL https://git.redacted.cc/josh/jlog/archive/Prerelease-15.zip
)
find_package(OpenGL REQUIRED)
@@ -39,12 +39,14 @@ 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 +65,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)

View File

@@ -12,7 +12,7 @@ Install dependencies
```bash
dnf install cmake make gcc-g++ libX11 libX11-devel
apt-get install cmake make gcc g++ libx11-6 libx11-dev libgl-dev
apt-get install cmake make gcc g++ libx11-6 libx11-dev libgl-dev libxrandr-dev
```
Clone the repository

View File

@@ -0,0 +1,10 @@
#pragma once
#include "jlog/Logger.hpp"
namespace ReWindow::Logger {
using namespace jlog;
extern GenericLogger Fatal;
extern GenericLogger Debug;
}

View 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();
};

View File

@@ -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>
@@ -139,13 +140,13 @@ namespace ReWindow
#pragma region Callbacks
/// Bindable Non-intrusive event handlers
/// Use these when you can't override the base window class
Event<> OnOpenEvent;
Event<> OnClosingEvent;
Event<int> OnOpenEvent;
Event<int> OnClosingEvent;
Event<RWindowEvent> OnFocusLostEvent;
Event<RWindowEvent> OnFocusGainEvent;
Event<float> OnRefreshEvent;
Event<WindowResizeRequestEvent> OnResizeRequestEvent;
Event<KeyDownEvent> OnKeyDownEvent;
Event<KeyDownEvent> OnKeyDownEvent;
Event<KeyUpEvent> OnKeyUpEvent;
Event<MouseMoveEvent> OnMouseMoveEvent;
Event<MouseButtonDownEvent> OnMouseButtonDownEvent;
@@ -201,30 +202,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()
@@ -271,11 +266,22 @@ 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);
private:
bool cursor_visible = true;
Vector2 lastKnownWindowSize {0, 0};
bool flags[5];
std::vector<RWindowEvent> eventLog;
@@ -288,7 +294,5 @@ namespace ReWindow
RenderingAPI renderer;
bool open = false;
bool resizable;
};
}

View File

@@ -1,6 +1,7 @@
#include <iostream>
#include <rewindow/types/window.h>
#include <jlog/jlog.hpp>
#include <rewindow/types/display.h>
#include <rewindow/logger/logger.h>
//aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa Windows :/
#if _WIN32
@@ -42,21 +43,46 @@ 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),
@@ -64,12 +90,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()) {

7
src/logger/logger.cpp Normal file
View 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};
}

View 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;
}

View File

@@ -8,7 +8,7 @@
#include <rewindow/types/window.h>
#include <rewindow/types/cursors.h>
#include <J3ML/J3ML.hpp>
#include <jlog/jlog.hpp>
#include "rewindow/logger/logger.h"
@@ -30,24 +30,25 @@ Atom windowTypeAtom;
Atom windowTypeUtilityAtom;
XSizeHints hints;
GLXContext glContext;
Cursor invisible_cursor = 0;
using namespace ReWindow;
void RWindow::raise() const {
DEBUG(std::format("Raising window '{}'", this->title));
Logger::Debug(std::format("Raising window '{}'", this->title));
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;
}
@@ -65,18 +66,35 @@ void RWindow::refresh() {
//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,7 +102,8 @@ 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) {
destroyWindow();
system("xset r on");
@@ -92,27 +111,33 @@ void RWindow::pollEvents() {
}
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);
}
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);
}
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);
@@ -121,7 +146,7 @@ void RWindow::pollEvents() {
}
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);
@@ -131,7 +156,7 @@ void RWindow::pollEvents() {
}
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;
@@ -141,7 +166,7 @@ void RWindow::pollEvents() {
}
if (xev.type == ButtonPress) {
DEBUG(std::format("Recieved event '{}'", "ButtonPress"));
Logger::Debug(std::format("Recieved event '{}'", "ButtonPress"));
MouseButton button = GetMouseButtonFromXButton(xev.xbutton.button);
auto eventData = MouseButtonDownEvent(button);
eventData.Button = button;
@@ -152,17 +177,17 @@ void RWindow::pollEvents() {
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"));
Logger::Debug(std::format("Recieved event '{}'", "ResizeRequest"));
this->width = xev.xresizerequest.width;
this->height = xev.xresizerequest.height;
auto eventData = WindowResizeRequestEvent();
@@ -186,7 +211,7 @@ void RWindow::setSize(int newWidth, int 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 {
@@ -251,7 +276,7 @@ bool RWindow::isResizable() const {
}
void RWindow::fullscreen() {
DEBUG(std::format("Fullscreening window '{}'", this->title));
Logger::Debug(std::format("Fullscreening window '{}'", this->title));
fullscreenmode = true;
XEvent xev;
@@ -268,11 +293,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 +311,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) {

View 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;
}

View File

@@ -1,5 +1,5 @@
#include <rewindow/types/window.h>
#include <jlog/jlog.hpp>
#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";
}
};
@@ -37,7 +36,9 @@ Vector2 RWindow::GetMouseCoordinates() const {
return getCursorPos();
}
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;
@@ -57,7 +58,7 @@ void RWindow::setFullscreen(bool fs) {
void RWindow::liftKey(Key key) {
currentKeyboard.PressedKeys[key] = false;
auto event = ReWindow::WindowEvents::KeyUpEvent(key);
auto event = KeyUpEvent(key);
OnKeyUp(event);
}
@@ -83,6 +84,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;
@@ -108,7 +117,4 @@ void RWindow::pressKey(Key key) {
}
void RWindow::Close()
{
}
void RWindow::Close() {}

View File

@@ -1,5 +1,5 @@
#include <Windows.h>
//#include <gl/GL.h>
#include <gl/GL.h>
#include <rewindow/types/window.h>
using namespace ReWindow;
@@ -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,131 +98,179 @@ 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);
}
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;
}
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();
case WM_SETFOCUS: {
RWindowEvent event {};
eWindow->OnFocusGain(event);
eWindow->OnFocusGainEvent(event);
//If the wheel delta is positive, That's MWheelUp, Negative is MWheelDown.
int wheel_delta = GET_WHEEL_DELTA_WPARAM(wParam);
if (wheel_delta > 0)
eventData.Button = MouseButtons::MWheelUp,
eWindow->OnMouseButtonDownEvent(eventData),
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;
}
else
eventData.Button = MouseButtons::MWheelDown,
eWindow->OnMouseButtonDownEvent(eventData),
eWindow->OnMouseButtonDown(eventData);
break;
}
case WM_LBUTTONUP: {
auto eventData = MouseButtonUpEvent();
eventData.Button = MouseButtons::Left;
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->OnMouseButtonUpEvent(eventData);
eWindow->OnMouseButtonUp(eventData);
break;
}
if (button == XBUTTON2)
eventData.Button = MouseButtons::Mouse5,
eWindow->OnMouseButtonUpEvent(eventData),
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;
}
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:
//This is the same as "Motion Notify" in the X Window System.
break;
}
@@ -291,5 +347,4 @@ void RWindow::refresh() {
auto eventData = MouseMoveEvent(mouse_coords);
OnMouseMove(eventData);
std::this_thread::sleep_for(1ms);
}

View File

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

View File

@@ -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; }

View File

@@ -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;
}
}