xlib_playground

Xlib playground for experiments.
Log | Files | Refs

commit 062ae436f429e77fe0b1a45b043e27f072399f6c
parent b531b9293c5396d1220e52c084455f53aacfc405
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Fri, 23 Dec 2022 09:25:10 +0900

copy ex2

Diffstat:
Aex3/Makefile | 14++++++++++++++
Aex3/ex2.c | 215+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 229 insertions(+), 0 deletions(-)

diff --git a/ex3/Makefile b/ex3/Makefile @@ -0,0 +1,14 @@ +INCS=-I/usr/X11R6/include +CFLAGS=-Wall -Wextra -std=c11 +LIBS=-L/usr/X11R6/lib -lX11 -lXext -lm +IN=*.c +OUT=ex2 + +all: $(IN) + $(CC) $(INCS) $(CFLAGS) -o $(OUT) $(IN) $(LIBS) + +run: all + ./$(OUT) + +clean: + rm -f $(OUT) diff --git a/ex3/ex2.c b/ex3/ex2.c @@ -0,0 +1,215 @@ +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include <unistd.h> +#include <math.h> + +#include <X11/Xlib.h> + +/* macros */ +#define INIT_WIDTH 800 +#define INIT_HEIGHT 600 +#define FPS 60 + +/* +#define COUNT_FPS +*/ + + +enum Keys { + Key_D, + Key_S, + Key_A, + Key_W, + Key_Space, + Num_Key, //number of keys in this enum +}; +enum Key_State { + Key_Up, + Key_Down, +}; + +/* variables */ +Display *display; +Window window; +unsigned int win_width = INIT_WIDTH, win_height = INIT_HEIGHT; +GC gc; +Atom wm_delete_window; +int key_state[Num_Key]; +int quit; +float px = 200, py = 200; +float vx = 0, vy = 0; +int width = 40, height = 40; + + +/* function prototypes */ +void setup(void); +void cleanup(void); +void handle_inputs(void); + + +void +setup(void) +{ + if ((display = XOpenDisplay(NULL)) == NULL){ + fprintf(stderr, "ERROR: could not open display\n"); + exit(1); + } + window = XCreateSimpleWindow( + display, + XDefaultRootWindow(display), + 0, 0, + win_width, win_height, + 0, 0, + 0); + XStoreName(display, window, "UNKO"); + gc = XCreateGC(display, window, 0, NULL); + + wm_delete_window = XInternAtom(display, + "WM_DELETE_WINDOW", False); + XSetWMProtocols(display, window, &wm_delete_window, 1); + + XSelectInput(display, window, + ExposureMask|KeyPressMask|KeyReleaseMask); + + XSetForeground(display, gc, 0x00FFFF); + XMapWindow(display, window); +} + +void +handle_inputs(void) +{ + XEvent event; + while (XPending(display) > 0) { + XNextEvent(display, &event); + switch (event.type) { + case KeyPress: { + switch (XLookupKeysym(&event.xkey, 0)) { + case 'q': + quit = 1; + break; + case 'd': + key_state[Key_D] = Key_Down; + break; + case 'a': + key_state[Key_A] = Key_Down; + break; + case 'w': + key_state[Key_W] = Key_Down; + break; + case 's': + key_state[Key_S] = Key_Down; + break; + default: + break; + } + } break; + case KeyRelease: { + switch (XLookupKeysym(&event.xkey, 0)) { + case 'd': + key_state[Key_D] = Key_Up; + break; + case 'a': + key_state[Key_A] = Key_Up; + break; + case 'w': + key_state[Key_W] = Key_Up; + break; + case 's': + key_state[Key_S] = Key_Up; + break; + default: + break; + } + } break; + case ClientMessage: { + if ((Atom) event.xclient.data.l[0] == wm_delete_window) { + quit = 1; + } + } break; + default: + break; + } + } + + + vx = vy = 0; + if (key_state[Key_D] == Key_Down) + vx += 300; + if (key_state[Key_A] == Key_Down) + vx += -300; + if (key_state[Key_S] == Key_Down) + vy += 300; + if (key_state[Key_W] == Key_Down) + vy += -300; + +} + +void +cleanup(void) +{ + XCloseDisplay(display); +} + +int +main(void) +{ + long t0, t1, dt; +#ifdef COUNT_FPS + int fps_count = 0; +#endif + struct timespec ts; + + setup(); + quit = 0; + clock_gettime(CLOCK_MONOTONIC, &ts); + t0 = ts.tv_nsec; + + while (!quit){ + handle_inputs(); + + // fix fps + dt = 0; + while (dt < 1.0 * 1000 * 1000 * 1000 / FPS){ + clock_gettime(CLOCK_MONOTONIC, &ts); + t1 = ts.tv_nsec; + dt = t1 > t0 ? t1 - t0 : t1 - t0 + 1000 * 1000 * 1000; + } +#ifdef COUNT_FPS + // count fps. + // simple but not precise. + fps_count++; + if (t1 < t0){ + printf("fps: %u\n", fps_count); + fps_count = 0; + } +#endif + + clock_gettime(CLOCK_MONOTONIC, &ts); + t0 = ts.tv_nsec; + + px = px + vx * dt / 1000 / 1000 / 1000; + py = py + vy * dt / 1000 / 1000 / 1000; + // bind within the window + if (px < 0) + px = 0; + if (win_width < px + width) + px = win_width - width; + if (py < 0) + py = 0; + if (win_height < py + height) + py = win_height - height; + + XClearArea(display, window, + 0, 0, // position + win_width, win_height, // width and height + False); + XFillRectangle(display, window, gc, + px, py, // position + width, height); // width and height + + } + + cleanup(); + return 0; +}