aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-10-16 08:54:21 +0200
committerdec05eba <dec05eba@protonmail.com>2021-10-16 08:54:21 +0200
commit5cbff06ff9153f7a7958202a777d98ebeae59393 (patch)
treee472c74f486e9d3c877a2fb8044ca781b05396b8
parent1952897a8cb411d9b10d75ac6a8c223924c07e09 (diff)
Cursor motion
-rw-r--r--include/mgl/graphics/rectangle.h2
-rw-r--r--include/mgl/graphics/sprite.h2
-rw-r--r--include/mgl/system/vec.h (renamed from include/mgl/graphics/vec.h)4
-rw-r--r--include/mgl/window.h6
-rw-r--r--src/window.c48
-rw-r--r--tests/main.c2
6 files changed, 46 insertions, 18 deletions
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/system/vec.h
index 562f560..8b87376 100644
--- a/include/mgl/graphics/vec.h
+++ b/include/mgl/system/vec.h
@@ -5,4 +5,8 @@ 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);