From 97f1b1c735775d1e22412bbcf98ef403f9ee2275 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 16 Oct 2021 07:04:34 +0200 Subject: Add rectangle and sprite, use pixel coordinates, remote opengl dependency from test --- src/window.c | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) (limited to 'src/window.c') diff --git a/src/window.c b/src/window.c index ccfab93..818805d 100644 --- a/src/window.c +++ b/src/window.c @@ -27,6 +27,16 @@ static void set_vertical_sync_enabled(Window window, int enabled) { fprintf(stderr, "Warning: setting vertical sync failed\n"); } +static void mgl_window_on_resize(mgl_window *self, int width, int height) { + mgl_context *context = mgl_get_context(); + self->width = width; + self->height = height; + context->gl.glViewport(0, 0, self->width, self->height); + context->gl.glMatrixMode(GL_PROJECTION); + context->gl.glLoadIdentity(); + context->gl.glOrtho(0.0, width, height, 0.0, -1.0, 1.0); +} + int mgl_window_create(mgl_window *self, const char *title, int width, int height, mgl_window_callback *callback) { return mgl_window_create_with_params(self, title, width, height, DefaultRootWindow(mgl_get_context()->connection), callback); } @@ -45,7 +55,7 @@ int mgl_window_create_with_params(mgl_window *self, const char *title, int width XSetWindowAttributes window_attr; window_attr.colormap = color_map; - window_attr.event_mask = KeyPressMask; + window_attr.event_mask = KeyPressMask | StructureNotifyMask; self->window = XCreateWindow(context->connection, parent_window, 0, 0, width, height, 0, ((XVisualInfo*)context->visual_info)->depth, InputOutput, ((XVisualInfo*)context->visual_info)->visual, CWColormap | CWEventMask, &window_attr); XFreeColormap(context->connection, color_map); @@ -66,6 +76,13 @@ int mgl_window_create_with_params(mgl_window *self, const char *title, int width context->gl.glEnable(GL_TEXTURE_2D); context->gl.glEnable(GL_BLEND); context->gl.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + XWindowAttributes gwa; + gwa.width = 0; + gwa.height = 0; + XGetWindowAttributes(context->connection, self->window, &gwa); + + mgl_window_on_resize(self, gwa.width, gwa.height); mgl_window_draw(self); return 0; } @@ -79,7 +96,12 @@ void mgl_window_deinit(mgl_window *self) { } static void on_receive_x11_event(mgl_window *window, XEvent *xev) { - + if(xev->type == ConfigureNotify) { + if(xev->xconfigure.width != window->width || xev->xconfigure.height != window->height) { + mgl_window_on_resize(window, xev->xconfigure.width, xev->xconfigure.height); + /*fprintf(stderr, "resize!\n");*/ + } + } } void mgl_window_events_poll(mgl_window *self) { @@ -112,12 +134,6 @@ void mgl_window_events_poll(mgl_window *self) { void mgl_window_draw(mgl_window *self) { mgl_context *context = mgl_get_context(); - - /* TODO: Get window size from window resize event instead */ - XWindowAttributes gwa; - XGetWindowAttributes(context->connection, self->window, &gwa); - - context->gl.glViewport(0, 0, gwa.width, gwa.height); context->gl.glClear(GL_COLOR_BUFFER_BIT); context->gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); if(self->callback.draw) -- cgit v1.2.3