This commit is contained in:
2024-06-17 21:10:15 -07:00
parent 5b8788f94f
commit adec224701
6 changed files with 213 additions and 57 deletions

View File

@@ -18,7 +18,7 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
CPMAddPackage(
NAME J3ML
URL https://git.redacted.cc/josh/j3ml/archive/Release-3.zip
URL https://git.redacted.cc/josh/j3ml/archive/Release-2.1.zip
)
CPMAddPackage(
@@ -28,7 +28,7 @@ CPMAddPackage(
CPMAddPackage(
NAME jlog
URL https://git.redacted.cc/josh/jlog/archive/Prerelease-4.zip
URL https://git.redacted.cc/josh/jlog/archive/Prerelease-6.zip
)
find_package(OpenGL REQUIRED)
@@ -44,7 +44,7 @@ if(UNIX AND NOT APPLE)
endif()
if(WIN32)
file(GLOB_RECURSE SOURCES "src/rewindow/*.cpp" "src/windows/*.cpp" "src/platform/shared/*.cpp")
file(GLOB_RECURSE SOURCES "src/rewindow/*.cpp" "src/platform/windows/*.cpp" "src/platform/shared/*.cpp")
endif()
include_directories("include")
@@ -71,8 +71,7 @@ if(UNIX AND NOT APPLE)
endif()
if(WIN32)
target_link_libraries(ReWindowLibrary PUBLIC J3ML Event jlog${OPENGL_LIBRARIES})
target_link_libraries(ReWindowLibrary PUBLIC J3ML Event jlog ${OPENGL_LIBRARIES})
add_executable(ReWindowLibraryDemo main.cpp)
target_link_libraries(ReWindowLibraryDemo PUBLIC ReWindowLibrary)

View File

@@ -4,25 +4,24 @@
#include <vector>
#include <J3ML/LinearAlgebra/Vector2.h>
#include <jlog/jlog.hpp>
#include <stdexcept>
#include <iostream>
#if __linux__
#include <X11/X.h>
#endif
#include <string>
#include <rewindow/data/X11Scancodes.h>
#include <rewindow/data/WindowsScancodes.h>
//#include "jlog/jlog.hpp"
using J3ML::LinearAlgebra::Vector2;
class Key
{
private:
//clion on Linux falsely marks this as being wrong.
inline static std::vector<Key> keyboard{};
//CLion on Linux falsely marks this as being wrong.
static inline std::vector<Key> keyboard{};
public:
static std::vector<Key> GetKeyboard();
@@ -163,24 +162,27 @@ namespace MouseButtons
static const MouseButton Left {"l"};
static const MouseButton Right {"r"};
static const MouseButton Middle {"m"};
static const MouseButton Side1 {"1"};
static const MouseButton Side2 {"2"};
static const MouseButton MWheelUp {"1"};
static const MouseButton MWheelDown {"2"};
static const MouseButton Mouse4 {"4"};
static const MouseButton Mouse5 {"5"};
static const MouseButton Unimplemented {"u"};
}
#if __linux__
static MouseButton GetMouseButtonFromXButton(unsigned int button) {
switch(button) {
case Button1: return MouseButtons::Left;
case Button2: return MouseButtons::Right;
case Button3: return MouseButtons::Middle;
case Button4: return MouseButtons::Side1;
case Button5: return MouseButtons::Side2;
default:
{
//throw std::runtime_error("Undefined XButtonCode: " + button);
//FATAL("Undefined XButtonCode:" + button);
std::cout << "Undefined XButtonCode:" + button << std::endl;
case Button2: return MouseButtons::Middle;
case Button3: return MouseButtons::Right;
case Button4: return MouseButtons::MWheelUp;
case Button5: return MouseButtons::MWheelDown;
//For *whatever* reason. These aren't in X.h
case 8: return MouseButtons::Mouse4;
case 9: return MouseButtons::Mouse5;
default: {
FATAL(" Undefined XButtonCode: " + std::to_string((int) button))
return MouseButtons::Unimplemented;
break;
}
}

View File

@@ -12,31 +12,16 @@ std::ostream& operator<<(std::ostream& os, const Vector2& v) {
class MyWindow : public ReWindow::RWindow {
public:
MyWindow(const std::string& title, int w, int h) : ReWindow::RWindow(title, w, h)
{}
MyWindow(const std::string& title, int w, int h) : ReWindow::RWindow(title, w, h) {}
void OnMouseMove(const ReWindow::MouseMoveEvent& e) override {
}
void OnMouseMove(const ReWindow::MouseMoveEvent& e) override {}
void OnKeyDown(const ReWindow::KeyDownEvent& e) override {
}
void OnKeyDown(const ReWindow::KeyDownEvent& e) override {}
void OnRefresh(float elapsed) override {
glSwapBuffers ();
auto pos = getCursorPos ();
glSwapBuffers();
auto pos = getCursorPos();
//std::cout << pos.x << ", " << pos.y << std::endl;
#if __linux__
if (isKeyDown(Keys::L)) {
this->setCursorStyle (ReWindow::Cursors::Pencil);
}
else this->setCursorStyle (ReWindow::Cursors::Default);
#else
if (isKeyDown (Keys::L)) {
this->setCursorStyle (ReWindow::Cursors::Cross);
}
else this->setCursorStyle (ReWindow::Cursors::Arrow);
#endif
}
bool OnResizeRequest(const ReWindow::WindowResizeRequestEvent& e) override {

View File

@@ -1,13 +1,12 @@
#include <iostream>
#include <rewindow/types/window.h>
#include <rewindow/types/cursors.h>
#include <J3ML/J3ML.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <cstdlib>
#include <cstring>
#include <thread>
#include <rewindow/types/cursors.h>
@@ -152,12 +151,6 @@ void RWindow::pollEvents() {
XResizeWindow(display, window, width, height);
}
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;

View File

@@ -9,7 +9,7 @@ RWindow::RWindow() {
//RWindow::singleton = this;
}
RWindow::RWindow(const std::string& title, int width, int height) : flags(false,false,false,false) {
RWindow::RWindow(const std::string& title, int width, int height) : flags{false,false,false,false} {
this->title = title;
this->width = width;
this->height = height;
@@ -43,4 +43,10 @@ std::string RWindow::getTitle() const {
void RWindow::setRenderer(RenderingAPI api) {
renderer = api;
}
void RWindow::setSize(const Vector2& size) {
this->width = size.x;
this->height = size.y;
this->setSize(size.x, size.y);
}

View File

@@ -1,10 +1,181 @@
#include <rewindow/types/window.h>
#include <gl/GL.h>
using namespace ReWindow;
HINSTANCE hInstance = GetModuleHandle(NULL);
RWindow::RWindow() {
title = "ReWindow Application";
width = 640;
height = 480;
bool fullscreenmode = false;
bool open = false;
HINSTANCE hInstance = GetModuleHandle(NULL);
HWND hwnd;
HDC hdc;
HGLRC glContext;
void raise() { SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); }
void lower() { SetWindowPos(hwnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); }
void RWindow::setFlag(RWindowFlags flag, bool state) {
flags[(int) flag] = state;
if (flag == RWindowFlags::RESIZABLE && !state) {
RECT rect;
GetWindowRect(hwnd, &rect);
SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_THICKFRAME);
SetWindowPos(hwnd, nullptr, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOOWNERZORDER);
}
}
void RWindow::pollEvents() {
MSG msg;
while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
void RWindow::setSize(int newWidth, int newHeight) {
if (!getFlag(RWindowFlags::RESIZABLE)) return;
this->width = newWidth;
this->height = newHeight;
SetWindowPos(hwnd, nullptr, 0, 0, width, height, SWP_NOMOVE | SWP_NOZORDER);
}
Vector2 RWindow::getCursorPos() const {
POINT point;
GetCursorPos(&point);
ScreenToClient(hwnd, &point);
return { (float)point.x, (float)point.y };
}
Vector2 RWindow::getSize() const {
RECT rect;
GetClientRect(hwnd, &rect);
return { (float)(rect.right - rect.left), (float)(rect.bottom - rect.top) };
}
void RWindow::setPos(int x, int y) {
SetWindowPos(hwnd, nullptr, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
}
void RWindow::setPos(const Vector2& pos) {
setPos(pos.x, pos.y);
}
void RWindow::fullscreen() {
// Implement fullscreen
SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_OVERLAPPEDWINDOW);
SetWindowPos(hwnd, HWND_TOP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), SWP_FRAMECHANGED | SWP_NOOWNERZORDER);
}
void RWindow::restoreFromFullscreen() {
// Implement restore from fullscreen
SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) | WS_OVERLAPPEDWINDOW);
SetWindowPos(hwnd, nullptr, 0, 0, width, height, SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOOWNERZORDER);
}
void RWindow::setVsyncEnabled(bool b) {
typedef BOOL(WINAPI* PFNWGLSWAPINTERVALEXTPROC)(int interval);
PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = nullptr;
wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC) wglGetProcAddress("wglSwapIntervalEXT");
if (wglSwapIntervalEXT)
wglSwapIntervalEXT(b ? 1 : 0);
}
bool RWindow::isFullscreen() const {
return fullscreenmode;
}
//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: {
if (wParam == 112)
std::cout << "F1 was pressed." << std::endl;
}
case WM_KEYUP:
break;
case WM_LBUTTONDOWN:
break;
case WM_LBUTTONUP:
break;
case WM_MOUSEMOVE:
break;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
void RWindow::setCursorStyle(CursorStyle style) const {}
void RWindow::Open() {
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = "RWindowClass";
RegisterClass(&wc);
hwnd = CreateWindowEx(
0,
"RWindowClass",
title.c_str(),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, width, height,
nullptr,
nullptr,
hInstance,
nullptr
);
if (renderer == RenderingAPI::OPENGL) {
PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
24,
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
24,
8,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0
};
hdc = GetDC(hwnd);
int pixelFormat = ChoosePixelFormat(hdc, &pfd);
SetPixelFormat(hdc, pixelFormat, &pfd);
glContext = wglCreateContext(hdc);
wglMakeCurrent(hdc, glContext);
}
ShowWindow(hwnd, SW_SHOW);
open = true;
}
void RWindow::glSwapBuffers() {
SwapBuffers(hdc);
}
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 eventData = MouseMoveEvent(mouse_coords);
OnMouseMove(eventData);
std::this_thread::sleep_for(1ms);
}