From 5d6503e80488c705ff1a1d9dee0c2a916a9d002a Mon Sep 17 00:00:00 2001 From: Maciej Samborski Date: Fri, 1 Aug 2025 13:24:33 +0200 Subject: [PATCH] added windowGetSize --- Makefile | 2 +- main.c | 28 +++++++++++++++++----------- src/openwindow.c | 13 +++++++++++-- src/openwindow.h | 5 +++++ 4 files changed, 34 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index 5ec3fbd..5e7ebfe 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ all: libopenwindow.so libopenwindow.dll libopenwindow.so: glad_linux.a mkdir -p ${OUTPUT} - gcc ${FLAGS} -o ${OUTPUT}libopenwindow.so ${LINUX_FILES} -lX11 -lGLX -lOpenGL + gcc ${FLAGS} -o ${OUTPUT}libopenwindow.so ${LINUX_FILES} -lX11 -lGLX -lOpenGL -lgallium libopenwindow.dll: glad_windows.a mkdir -p ${OUTPUT} diff --git a/main.c b/main.c index 39ba4f2..ce2f313 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,6 @@ #include +#define GL_GLEXT_PROTOTYPES #include "GL/gl.h" #include "src/openwindow.h" @@ -17,22 +18,27 @@ void handleInput(Window * window) { int main(void) { - Window * w = openWindow("Window", 800, 600); + Window * window = openWindow("Window", 800, 600); - windowSetFps(w, 300); + windowSetFps(window, 300); - while (!windowKeyPressed(w, WINDOW_KEY_ESC)) { - windowHandleEvents(w); + while (!windowKeyPressed(window, WINDOW_KEY_ESC)) { + windowHandleEvents(window); - handleInput(w); + handleInput(window); - //printf("fps: %lf\n", 1 / windowGetDeltaTime(w)); + //printf("fps: %lf\n", 1 / windowGetDeltaTime(window)); - int x = 0; - int y = 0; - windowGetMousePosition(w, &x, &y); + //int x = 0; + //int y = 0; + //windowGetMousePosition(window, &x, &y); //printf("x: %d, y: %d\n", x, y); + int w = 0; + int h = 0; + windowGetSize(window, &w, &h); + printf("%dx%d\n", w, h); + glClearColor(0.1, 0.2, 0.3, 1); glClear(GL_COLOR_BUFFER_BIT); glViewport(0, 0, 800, 600); @@ -46,10 +52,10 @@ int main(void) glEnd(); glFlush(); - windowDraw(w); + windowDraw(window); } - closeWindow(w); + closeWindow(window); return 0; } diff --git a/src/openwindow.c b/src/openwindow.c index a81d03a..a73d3f4 100644 --- a/src/openwindow.c +++ b/src/openwindow.c @@ -41,6 +41,10 @@ void windowDefaultEventHandler(Window * w, XEvent xev) { case ButtonRelease: w->io.mouse.current[xev.xbutton.button - 1] = false; break; + case ConfigureNotify: + w->width = xev.xconfigure.width; + w->height = xev.xconfigure.height; + break; } } @@ -223,10 +227,15 @@ double windowGetDeltaTime(Window *window) { return window->dt.dt; } +void windowGetSize(Window * window, int * width, int * height) { + *width = window->width; + *height = window->height; +} + void windowDraw(Window * window) { glXSwapBuffers(window->x.display, window->x.window); - usleep(((float)1 / (window->fps + 1)) * 1e6); + usleep(((float)1 / window->fps) * 1e6); clock_gettime(CLOCK_MONOTONIC, window->dt.t2); window->dt.dt = (double) (window->dt.t2->tv_sec - window->dt.t1->tv_sec) + (window->dt.t2->tv_nsec - window->dt.t1->tv_nsec) / 1e9; @@ -504,7 +513,7 @@ double windowGetDeltaTime(Window * window) { void windowDraw(Window * window) { SwapBuffers(window->wgl.display); - Sleep((uint32_t)(((double)1 / (window->fps + 1)) * 1e3)); + Sleep((uint32_t)(((double)1 / window->fps) * 1e3)); QueryPerformanceFrequency(&window->dt.freq); QueryPerformanceCounter(&window->dt.t2); diff --git a/src/openwindow.h b/src/openwindow.h index 4c8e1b1..9e4181b 100644 --- a/src/openwindow.h +++ b/src/openwindow.h @@ -71,6 +71,8 @@ typedef struct Window { X11 x; WindowDeltaTime dt; uint32_t fps; + int width; + int height; } Window; #define WINDOW_KEY_Q ("q") @@ -156,6 +158,8 @@ typedef struct Window { WGL wgl; WindowDeltaTime dt; uint32_t fps; + int width; + int height; } Window; #define WINDOW_KEY_Q ('Q') @@ -212,6 +216,7 @@ bool windowKeyAlt(Window * window); Window * openWindow(const char * name, size_t width, size_t height); void windowSetFps(Window * window, uint32_t fps); double windowGetDeltaTime(Window * window); +void windowGetSize(Window * window, int * width, int * height); void windowGetMousePosition(Window * window, int * x, int * y); void windowDraw(Window * window); void windowHandleEvents(Window * window);