Skip to content

Commit

Permalink
fenster_sync, array on mouse pos, array on modkey
Browse files Browse the repository at this point in the history
  • Loading branch information
CardealRusso committed Oct 18, 2024
1 parent efc299b commit 2c51590
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 20 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Compile example

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
workflow_dispatch:

jobs:
build:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v4
with:
submodules: 'recursive'

- name: Compile
run: |
cd examples/mousebuttons-c/
make
shell: bash
3 changes: 2 additions & 1 deletion examples/mousebuttons-c/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
CFLAGS ?= -Wall -Wextra -std=c99

ifeq ($(OS),Windows_NT)
CC = gcc
LDFLAGS = -lgdi32
else
UNAME_S := $(shell uname -s)
Expand All @@ -12,4 +13,4 @@ else
endif

main: main.c ../../src/fenster/fenster.h
$(CC) main.c -I../../src/fenster -o $@ $(CFLAGS) $(LDFLAGS)
$(CC) main.c -I../../src/fenster/ -o $@ $(CFLAGS) $(LDFLAGS)
3 changes: 2 additions & 1 deletion examples/mousebuttons-c/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ static int run() {
while (fenster_loop(&f) == 0) {
printf("Click: L:%d R:%d M:%d SU:%d SD:%d | \n",
f.mclick[0], f.mclick[1], f.mclick[2], f.mclick[3], f.mclick[4]);
fenster_sync(&f, 60);
}

fenster_close(&f);
return 0;
}
Expand Down
25 changes: 18 additions & 7 deletions src/fenster/fenster.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@
#endif

#include <stdint.h>
#include <stdlib.h>
//#include <stdlib.h>
#include <string.h>

struct fenster {
const char *title;
const int width;
const int height;
uint32_t *buf;
int keys[256]; /* keys are mostly ASCII, but arrows are 17..20 */
int mod; /* mod is 4 bits mask, ctrl=1, shift=2, alt=4, meta=8 */
int x;
int y;
int mclick[5]; // left, right, middle, scroll up, scroll down (cleared after read)
int mhold[3]; // left, right, middle (persistent until release)
int keys[256]; // keys are mostly ASCII, but arrows are 17..20
int modkeys[4]; // ctrl, shift, alt, meta
int mpos[2]; // mouse x, y
int mclick[5]; // left, right, middle, scroll up, scroll down (cleared after read)
int mhold[3]; // left, right, middle (persistent until release)
int64_t lastsync; // last sync time
#if defined(__APPLE__)
id wnd;
#elif defined(_WIN32)
Expand All @@ -51,6 +51,7 @@ FENSTER_API int fenster_loop(struct fenster *f);
FENSTER_API void fenster_close(struct fenster *f);
FENSTER_API void fenster_sleep(int64_t ms);
FENSTER_API int64_t fenster_time(void);
FENSTER_API void fenster_sync(struct fenster *f, int fps);

#define fenster_pixel(f, x, y) ((f)->buf[((y) * (f)->width) + (x)])

Expand All @@ -62,4 +63,14 @@ FENSTER_API int64_t fenster_time(void);
#include "fenster_linux.h"
#endif

FENSTER_API void fenster_sync(struct fenster *f, int fps) {
int64_t frame_time = 1000 / fps;
int64_t elapsed = fenster_time() - f->lastsync;
if (elapsed < frame_time) {
fenster_sleep(frame_time - elapsed);
}

f->lastsync = fenster_time();
}

#endif /* FENSTER_H */
9 changes: 6 additions & 3 deletions src/fenster/fenster_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ FENSTER_API int fenster_loop(struct fenster *f) {
}
break;
case MotionNotify:
f->x = ev.xmotion.x, f->y = ev.xmotion.y;
f->mpos[0] = ev.xmotion.x;
f->mpos[1] = ev.xmotion.y;
break;
case KeyPress:
case KeyRelease: {
Expand All @@ -60,8 +61,10 @@ FENSTER_API int fenster_loop(struct fenster *f) {
break;
}
}
f->mod = (!!(m & ControlMask)) | (!!(m & ShiftMask) << 1) |
(!!(m & Mod1Mask) << 2) | (!!(m & Mod4Mask) << 3);
f->modkeys[0] = !!(m & ControlMask);
f->modkeys[1] = !!(m & ShiftMask);
f->modkeys[2] = !!(m & Mod1Mask);
f->modkeys[3] = !!(m & Mod4Mask);
} break;
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/fenster/fenster_mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,19 @@ FENSTER_API int fenster_loop(struct fenster *f) {
case 5:
case 6: { /* NSEventTypeMouseMoved */
CGPoint xy = msg(CGPoint, ev, "locationInWindow");
f->x = (int)xy.x;
f->y = (int)(f->height - xy.y);
f->mpos[0] = (int)xy.x;
f->mpos[1] = (int)(f->height - xy.y);
return 0;
}
case 10: /*NSEventTypeKeyDown*/
case 11: /*NSEventTypeKeyUp:*/ {
NSUInteger k = msg(NSUInteger, ev, "keyCode");
f->keys[k < 127 ? FENSTER_KEYCODES[k] : 0] = evtype == 10;
NSUInteger mod = msg(NSUInteger, ev, "modifierFlags") >> 17;
f->mod = (mod & 0xc) | ((mod & 1) << 1) | ((mod >> 1) & 1);
f->modkeys[0] = (mod & 1) ? 1 : 0; // Shift
f->modkeys[1] = (mod & 2) ? 1 : 0; // Control
f->modkeys[2] = (mod & 4) ? 1 : 0; // Alt/Option
f->modkeys[3] = (mod & 8) ? 1 : 0; // Command
return 0;
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/fenster/fenster_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ static LRESULT CALLBACK fenster_wndproc(HWND hwnd, UINT msg, WPARAM wParam,
}
break;
case WM_MOUSEMOVE:
f->y = HIWORD(lParam), f->x = LOWORD(lParam);
f->mpos[1] = HIWORD(lParam), f->mpos[0] = LOWORD(lParam);
break;
case WM_KEYDOWN:
case WM_KEYUP: {
f->mod = ((GetKeyState(VK_CONTROL) & 0x8000) >> 15) |
((GetKeyState(VK_SHIFT) & 0x8000) >> 14) |
((GetKeyState(VK_MENU) & 0x8000) >> 13) |
(((GetKeyState(VK_LWIN) | GetKeyState(VK_RWIN)) & 0x8000) >> 12);
f->modkeys[0] = (GetKeyState(VK_CONTROL) & 0x8000) ? 1 : 0;
f->modkeys[1] = (GetKeyState(VK_SHIFT) & 0x8000) ? 1 : 0;
f->modkeys[2] = (GetKeyState(VK_MENU) & 0x8000) ? 1 : 0;
f->modkeys[3] = ((GetKeyState(VK_LWIN) | GetKeyState(VK_RWIN)) & 0x8000) ? 1 : 0;
f->keys[FENSTER_KEYCODES[HIWORD(lParam) & 0x1ff]] = !((lParam >> 31) & 1);
} break;
case WM_DESTROY:
Expand Down

0 comments on commit 2c51590

Please sign in to comment.