added windowMouse* functions

This commit is contained in:
Maciej Samborski 2025-07-29 13:35:58 +02:00
parent 34cf3bde86
commit 8b228295b9
3 changed files with 67 additions and 11 deletions

7
main.c
View File

@ -26,7 +26,12 @@ int main(void)
handleInput(w);
printf("fps: %lf\n", 1 / windowGetDeltaTime(w));
//printf("fps: %lf\n", 1 / windowGetDeltaTime(w));
int x = 0;
int y = 0;
windowGetMousePosition(w, &x, &y);
//printf("x: %d, y: %d\n", x, y);
glClearColor(0.1, 0.2, 0.3, 1);
glClear(GL_COLOR_BUFFER_BIT);

View File

@ -29,7 +29,17 @@ void windowDefaultEventHandler(Window * w, XEvent xev) {
{
case KeyPress:
case KeyRelease:
XQueryKeymap(w->x.display, w->io.current);
XQueryKeymap(w->x.display, w->io.keyboard.current);
break;
case MotionNotify:
w->io.mouse.x = xev.xmotion.x;
w->io.mouse.y = xev.xmotion.y;
break;
case ButtonPress:
w->io.mouse.current[xev.xbutton.button - 1] = true;
break;
case ButtonRelease:
w->io.mouse.current[xev.xbutton.button - 1] = false;
break;
}
}
@ -225,7 +235,8 @@ void windowDraw(Window * window) {
}
void resetIOState(Window * window) {
memcpy(window->io.last, window->io.current, WINDOW_KEY_COUNT);
memcpy(window->io.keyboard.last, window->io.keyboard.current, WINDOW_KEY_COUNT);
memcpy(window->io.mouse.last, window->io.mouse.current, WINDOW_MOUSE_BUTTON_COUNT);
}
void windowHandleEvents(Window * window) {
@ -255,13 +266,32 @@ void closeWindow(Window * window) {
free(window);
}
bool windowMousePressed(Window * window, int button) {
return !window->io.mouse.last[button - 1] && window->io.mouse.current[button - 1];
}
bool windowMouseHeld(Window * window, int button) {
return window->io.mouse.last[button - 1] && window->io.mouse.current[button - 1];
}
bool windowMouseReleased(Window * window, int button) {
return window->io.mouse.last[button - 1] && !window->io.mouse.current[button - 1];
}
void windowGetMousePosition(Window * window, int * x, int * y) {
if (x == NULL || y == NULL) return;
*x = window->io.mouse.x;
*y = window->io.mouse.y;
}
bool windowKeyPressed(Window * window, char * key) {
KeySym keysym = XStringToKeysym(key);
if (keysym == NoSymbol) return false;
KeyCode keycode = XKeysymToKeycode(window->x.display, keysym);
return !(window->io.last[keycode >> 3] & (1 << (keycode & 7))) && (window->io.current[keycode >> 3] & (1 << (keycode & 7)));
return !(window->io.keyboard.last[keycode >> 3] & (1 << (keycode & 7))) && (window->io.keyboard.current[keycode >> 3] & (1 << (keycode & 7)));
}
bool windowKeyHeld(Window * window, char * key) {
@ -270,7 +300,7 @@ bool windowKeyHeld(Window * window, char * key) {
KeyCode keycode = XKeysymToKeycode(window->x.display, keysym);
return (window->io.last[keycode >> 3] & (1 << (keycode & 7))) && (window->io.current[keycode >> 3] & (1 << (keycode & 7)));
return (window->io.keyboard.last[keycode >> 3] & (1 << (keycode & 7))) && (window->io.keyboard.current[keycode >> 3] & (1 << (keycode & 7)));
}
bool windowKeyReleased(Window * window, char * key) {
@ -279,28 +309,28 @@ bool windowKeyReleased(Window * window, char * key) {
KeyCode keycode = XKeysymToKeycode(window->x.display, keysym);
return (window->io.last[keycode >> 3] & (1 << (keycode & 7))) && !(window->io.current[keycode >> 3] & (1 << (keycode & 7)));
return (window->io.keyboard.last[keycode >> 3] & (1 << (keycode & 7))) && !(window->io.keyboard.current[keycode >> 3] & (1 << (keycode & 7)));
}
bool windowKeyShift(Window * window) {
KeyCode sl = XKeysymToKeycode(window->x.display, XK_Shift_L);
KeyCode sr = XKeysymToKeycode(window->x.display, XK_Shift_R);
return (window->io.last[sl >> 3] & (1 << (sl & 7))) || (window->io.current[sr >> 3] & (1 << (sr & 7)));
return (window->io.keyboard.last[sl >> 3] & (1 << (sl & 7))) || (window->io.keyboard.current[sr >> 3] & (1 << (sr & 7)));
}
bool windowKeyCtrl(Window * window) {
KeyCode cl = XKeysymToKeycode(window->x.display, XK_Control_L);
KeyCode cr = XKeysymToKeycode(window->x.display, XK_Control_R);
return (window->io.last[cl >> 3] & (1 << (cl & 7))) || (window->io.current[cr >> 3] & (1 << (cr & 7)));
return (window->io.keyboard.last[cl >> 3] & (1 << (cl & 7))) || (window->io.keyboard.current[cr >> 3] & (1 << (cr & 7)));
}
bool windowKeyAlt(Window * window) {
KeyCode al = XKeysymToKeycode(window->x.display, XK_Alt_L);
KeyCode ar = XKeysymToKeycode(window->x.display, XK_Alt_R);
return (window->io.last[al >> 3] & (1 << (al & 7))) || (window->io.current[ar >> 3] & (1 << (ar & 7)));
return (window->io.keyboard.last[al >> 3] & (1 << (al & 7))) || (window->io.keyboard.current[ar >> 3] & (1 << (ar & 7)));
}
#else

View File

@ -5,6 +5,13 @@
#define WINDOW_KEY_COUNT 256
#define WINDOW_MOUSE_BUTTON_COUNT 5
#define WINDOW_MOUSE_BUTTON1 (Button1)
#define WINDOW_MOUSE_BUTTON2 (Button2)
#define WINDOW_MOUSE_BUTTON3 (Button3)
#define WINDOW_MOUSE_BUTTON4 (Button4)
#define WINDOW_MOUSE_BUTTON5 (Button5)
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
@ -37,8 +44,17 @@ typedef struct {
} X11;
typedef struct {
struct {
char current[WINDOW_KEY_COUNT];
char last[WINDOW_KEY_COUNT];
} keyboard;
struct {
bool current[WINDOW_MOUSE_BUTTON_COUNT];
bool last[WINDOW_MOUSE_BUTTON_COUNT];
int x;
int y;
} mouse;
} WindowIOState;
typedef struct {
@ -106,6 +122,10 @@ bool windowKeyShift(Window * window);
bool windowKeyCtrl(Window * window);
bool windowKeyAlt(Window * window);
bool windowMousePressed(Window * window, int button);
bool windowMouseHeld(Window * window, int button);
bool windowMouseReleased(Window * window, int button);
#else
#include <windows.h>
@ -192,6 +212,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 windowGetMousePosition(Window * window, int * x, int * y);
void windowDraw(Window * window);
void windowHandleEvents(Window * window);
void closeWindow(Window * window);