xlib_playground

Xlib playground for experiments.
Log | Files | Refs

commit c798231fd60be26169e3acd89c699967fafcada9
parent e2b496a44519f91fc30472b200b6249ed2736b2f
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Wed, 21 Dec 2022 15:47:14 +0900

make example

Diffstat:
Aexample/Makefile | 14++++++++++++++
Aexample/test | 0
Aexample/test.c | 111+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mmain.c | 32++++++++++++++++----------------
Mnote.html | 7+++++++
5 files changed, 148 insertions(+), 16 deletions(-)

diff --git a/example/Makefile b/example/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=test + +all: $(IN) + $(CC) $(INCS) $(CFLAGS) -o $(OUT) $(IN) $(LIBS) + +run: all + ./$(OUT) + +clean: + rm -f $(OUT) diff --git a/example/test b/example/test Binary files differ. diff --git a/example/test.c b/example/test.c @@ -0,0 +1,111 @@ +#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 + + +/* variables */ +Display *display; +Window window; +unsigned int win_width = INIT_WIDTH, win_height = INIT_HEIGHT; +GC gc; +Atom wm_delete_window; + +/* function prototypes */ +void setup(void); +void cleanup(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, 0x0000FF); + XMapWindow(display, window); +} + +void +cleanup(void) +{ + XCloseDisplay(display); +} + +int +main(void) +{ + int px, py; + int quit; + struct timespec ts; + XEvent event; + + setup(); + quit = 0; + + while (!quit){ + while(XPending(display) > 0){ + XNextEvent(display, &event); + switch (event.type){ + case KeyPress: { + switch (XLookupKeysym(&event.xkey, 0)){ + case 'q': + quit = 1; + break; + default: + break; + } + } break; + case ClientMessage: { + if ((Atom) event.xclient.data.l[0] == wm_delete_window) { + quit = 1; + } + } break; + default: + break; + } + } + clock_gettime(CLOCK_MONOTONIC, &ts); + px = 200 + (int) (100 * sinf(ts.tv_sec + ts.tv_nsec / 1000.0 / 1000 / 1000)); + py = 200 + (int) (100 * cosf(ts.tv_sec + ts.tv_nsec / 1000.0 / 1000 / 1000)); + XClearArea(display, window, + 0, 0, // position + win_width, win_height, // width and height + False); + XFillRectangle(display, window, gc, + px, py, // position + 100, 100); // width and height + + ts.tv_sec = 0; + ts.tv_nsec = 10 * 1000 * 1000; + nanosleep(&ts, NULL); + } + + cleanup(); + return 0; +} diff --git a/main.c b/main.c @@ -61,33 +61,33 @@ void cleanup(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, + 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"); pgc = XCreateGC(display, window, 0, NULL); fgc = XCreateGC(display, window, 0, NULL); bgc = XCreateGC(display, window, 0, NULL); - wm_delete_window = XInternAtom(display, + wm_delete_window = XInternAtom(display, "WM_DELETE_WINDOW", False); - XSetWMProtocols(display, window, &wm_delete_window, 1); + XSetWMProtocols(display, window, &wm_delete_window, 1); - XSelectInput(display, window, + XSelectInput(display, window, ExposureMask|KeyPressMask|KeyReleaseMask); XSetForeground(display, pgc, 0x00FF00); XSetForeground(display, fgc, 0xFFFFFF); XSetForeground(display, bgc, 0x0000FF); - XMapWindow(display, window); + XMapWindow(display, window); } void @@ -367,7 +367,7 @@ handle_collision(void) void cleanup(void) { - XCloseDisplay(display); + XCloseDisplay(display); } int @@ -392,5 +392,5 @@ main(void) } cleanup(); - return 0; + return 0; } diff --git a/note.html b/note.html @@ -23,6 +23,13 @@ <h2>当たり判定</h2> <p>とりあえず適当に実装した。2体間の衝突のみ判定しているので近くに別の物体がいるとめりこむ場合がある。</p> +<h2>改行のテスト</h2> +<p>行末の改行 +は +半角スペース +に変換される。 +</p> + <h2>参考</h2> <ul>