Merge pull request 'vsync-test' (#4) from vsync-test into main

Reviewed-on: #4
This commit is contained in:
2024-01-25 20:02:42 -05:00
7 changed files with 32 additions and 37 deletions

View File

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

View File

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

View File

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

View File

@@ -154,7 +154,8 @@ public:
void setMouseCenter();
void restoreMouseFromLastCenter(); // Feels nicer for users
void setFocused(bool);
static void setVsyncEnabled(bool b);
void setFullscreen(bool fs);
void setResizable(bool resizable);
void setVsyncEnabled(bool);
@@ -165,7 +166,7 @@ public:
bool getFlag(RWindowFlags flag) const;
void setFlag(RWindowFlags flag, bool state);
void init(RenderingAPI api, const char* title, int width, int height);
void init(RenderingAPI api, const char* title, int width, int height, bool vsync);
void destroyWindow();
void pollEvents();
void refresh();

View File

@@ -30,8 +30,12 @@ int main() {
#ifdef WIN32
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{

View File

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

View File

@@ -14,9 +14,7 @@ Window window;
XEvent xev;
Display* display = XOpenDisplay(nullptr);
int defaultScreen = DefaultScreen(display);
//Visual* visual = DefaultVisual(display,defaultScreen);
XVisualInfo* visual;
//int depth = DefaultDepth(display, defaultScreen);
XSetWindowAttributes xSetWindowAttributes;
XWindowAttributes windowAttributes;
Atom wmDeleteWindow;
@@ -49,16 +47,17 @@ bool Key::operator<(const Key &rhs) const {
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) {
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);
@@ -75,6 +74,7 @@ void RWindow::init(RenderingAPI api, const char* title, int width, int height) {
wmDeleteWindow = XInternAtom(display, "WM_DELETE_WINDOW", False);
XSetWMProtocols(display, window, &wmDeleteWindow, 1);
glXMakeCurrent(display, window, glContext);
setVsyncEnabled(vsync);
} else {exit(0);}
}
@@ -162,9 +162,8 @@ void RWindow::pollEvents() {
}
if (xev.type == ButtonRelease) {
//auto scancode = (X11Scancode) xev.xkey.keycode;
/*for (unsigned int i = 0; i < eventLog.size(); i++) {
if (auto *e = dynamic_cast<MouseButtonDownEvent *>(eventLog[i])) {
for (unsigned int i = 0; i < events.size(); i++) {
if (auto *e = dynamic_cast<MouseButtonDownEvent *>(events[i])) {
if ((int) e->button == (int) xev.xbutton.button) {
delete eventLog[i];
eventLog.erase(eventLog.begin() + i);
@@ -248,7 +247,7 @@ bool RWindow::mouseButtonDown(MouseButton buttoncode) {
if (e->button == buttoncode) { return true;}
}
}
return false;*/
return false;
}
MouseButtonDownEvent RWindow::getEvent(MouseButton buttoncode) {
@@ -323,3 +322,9 @@ void RWindow::restoreFromFullscreen() {
SubstructureNotifyMask | SubstructureRedirectMask, &xev);
}
void RWindow::setVsyncEnabled(bool b) {
PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXT = nullptr;
glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddressARB((const GLubyte*)"glXSwapIntervalEXT");
glXSwapIntervalEXT(display, None, b);
}