From 5cbff06ff9153f7a7958202a777d98ebeae59393 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 16 Oct 2021 08:54:21 +0200 Subject: Cursor motion --- include/mgl/graphics/rectangle.h | 2 +- include/mgl/graphics/sprite.h | 2 +- include/mgl/graphics/vec.h | 8 ------- include/mgl/system/vec.h | 12 ++++++++++ include/mgl/window.h | 6 +++-- src/window.c | 48 +++++++++++++++++++++++++++++----------- tests/main.c | 2 +- 7 files changed, 54 insertions(+), 26 deletions(-) delete mode 100644 include/mgl/graphics/vec.h create mode 100644 include/mgl/system/vec.h diff --git a/include/mgl/graphics/rectangle.h b/include/mgl/graphics/rectangle.h index 7c2403b..5bcc521 100644 --- a/include/mgl/graphics/rectangle.h +++ b/include/mgl/graphics/rectangle.h @@ -1,8 +1,8 @@ #ifndef MGL_RECTANGLE_H #define MGL_RECTANGLE_H +#include "../system/vec.h" #include "color.h" -#include "vec.h" typedef struct mgl_context mgl_context; diff --git a/include/mgl/graphics/sprite.h b/include/mgl/graphics/sprite.h index 045da19..ea40b21 100644 --- a/include/mgl/graphics/sprite.h +++ b/include/mgl/graphics/sprite.h @@ -1,8 +1,8 @@ #ifndef MGL_SPRITE_H #define MGL_SPRITE_H +#include "../system/vec.h" #include "color.h" -#include "vec.h" typedef struct mgl_context mgl_context; typedef struct mgl_texture mgl_texture; diff --git a/include/mgl/graphics/vec.h b/include/mgl/graphics/vec.h deleted file mode 100644 index 562f560..0000000 --- a/include/mgl/graphics/vec.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef MGL_VEC_H -#define MGL_VEC_H - -typedef struct { - float x, y; -} mgl_vec2f; - -#endif /* MGL_VEC_H */ diff --git a/include/mgl/system/vec.h b/include/mgl/system/vec.h new file mode 100644 index 0000000..8b87376 --- /dev/null +++ b/include/mgl/system/vec.h @@ -0,0 +1,12 @@ +#ifndef MGL_VEC_H +#define MGL_VEC_H + +typedef struct { + float x, y; +} mgl_vec2f; + +typedef struct { + int x, y; +} mgl_vec2i; + +#endif /* MGL_VEC_H */ diff --git a/include/mgl/window.h b/include/mgl/window.h index 34920a8..0184577 100644 --- a/include/mgl/window.h +++ b/include/mgl/window.h @@ -1,6 +1,8 @@ #ifndef MGL_WINDOW_H #define MGL_WINDOW_H +#include "system/vec.h" + typedef struct mgl_window mgl_window; typedef struct { @@ -11,8 +13,8 @@ typedef struct { struct mgl_window { unsigned long window; mgl_window_callback callback; - int width; - int height; + mgl_vec2i size; + mgl_vec2i cursor_position; }; int mgl_window_create(mgl_window *self, const char *title, int width, int height, mgl_window_callback *callback); diff --git a/src/window.c b/src/window.c index 62f7e88..253ea3a 100644 --- a/src/window.c +++ b/src/window.c @@ -29,9 +29,9 @@ static void set_vertical_sync_enabled(Window window, int enabled) { 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); + self->size.x = width; + self->size.y = height; + context->gl.glViewport(0, 0, self->size.x, self->size.y); context->gl.glMatrixMode(GL_PROJECTION); context->gl.glLoadIdentity(); context->gl.glOrtho(0.0, width, height, 0.0, -1.0, 1.0); @@ -55,10 +55,16 @@ 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 | StructureNotifyMask; + window_attr.event_mask = + KeyPressMask | KeyReleaseMask | + ButtonPressMask | ButtonReleaseMask | + PointerMotionMask | Button1MotionMask | Button2MotionMask | Button3MotionMask | Button4MotionMask | Button5MotionMask | ButtonMotionMask | + StructureNotifyMask; window_attr.bit_gravity = NorthWestGravity; - 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 | CWBitGravity, &window_attr); + 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 | CWBitGravity, &window_attr); XFreeColormap(context->connection, color_map); if(!self->window) { fprintf(stderr, "XCreateWindow failed\n"); @@ -83,6 +89,11 @@ int mgl_window_create_with_params(mgl_window *self, const char *title, int width gwa.height = 0; XGetWindowAttributes(context->connection, self->window, &gwa); + Window dummy_w; + int dummy_i; + unsigned int dummy_u; + XQueryPointer(context->connection, self->window, &dummy_w, &dummy_w, &dummy_i, &dummy_i, &self->cursor_position.x, &self->cursor_position.y, &dummy_u); + mgl_window_on_resize(self, gwa.width, gwa.height); mgl_window_draw(self); return 0; @@ -96,11 +107,21 @@ 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");*/ +static void mgl_window_on_receive_event(mgl_window *self, XEvent *xev) { + switch(xev->type) { + case ConfigureNotify: { + while(XCheckTypedWindowEvent(mgl_get_context()->connection, self->window, ConfigureNotify, xev)) {} + if(xev->xconfigure.width != self->size.x || xev->xconfigure.height != self->size.x) { + mgl_window_on_resize(self, xev->xconfigure.width, xev->xconfigure.height); + /*fprintf(stderr, "resize!\n");*/ + } + break; + } + case MotionNotify: { + while(XCheckTypedWindowEvent(mgl_get_context()->connection, self->window, MotionNotify, xev)) {} + self->cursor_position.x = xev->xmotion.x; + self->cursor_position.y = xev->xmotion.y; + break; } } } @@ -120,12 +141,13 @@ void mgl_window_events_poll(mgl_window *self) { tv.tv_usec = (timeout_ms * 1000) - (tv.tv_sec * 1000 * 1000);*/ /* TODO: Is this needed when using XPending? */ - const int num_ready_fds = select(1 + x11_fd, &in_fds, NULL, NULL, &tv); + /*const int num_ready_fds = select(1 + x11_fd, &in_fds, NULL, NULL, &tv);*/ + const int num_ready_fds = 1; if(num_ready_fds > 0) { XEvent xev; - while(XPending(display)) { + while(self->window && XPending(display)) { XNextEvent(display, &xev); - on_receive_x11_event(self, &xev); + mgl_window_on_receive_event(self, &xev); } } else if(num_ready_fds == -1 && errno != EINTR) { /* TODO: */ diff --git a/tests/main.c b/tests/main.c index 2154b2b..478e432 100644 --- a/tests/main.c +++ b/tests/main.c @@ -15,7 +15,7 @@ static void draw(mgl_window *window, void *userdata) { mgl_rectangle rect = { .color = { 1.0f, 0.0f, 0.0f, 1.0f }, - .position = { 0.0f, 0.0f }, + .position = { window->cursor_position.x, window->cursor_position.y }, .size = { 100.0f, 500.0f } }; mgl_rectangle_draw(context, &rect); -- cgit v1.2.3