added windowGetSize

This commit is contained in:
Maciej Samborski 2025-08-01 13:24:33 +02:00
parent 8b228295b9
commit 5d6503e804
4 changed files with 34 additions and 14 deletions

View File

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

28
main.c
View File

@ -1,5 +1,6 @@
#include <stdio.h>
#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;
}

View File

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

View File

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