xlib_playground

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 60689a306dbcf75ea03418b7893fcf0933ff01d4
parent 986a8323199e0f652863cd109daa6a5f032ae1b8
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Thu, 15 Dec 2022 10:30:41 +0900

disable pixmap

Diffstat:
Mmain.c | 68++++++++++++++++++++++++++++++++++++++++++++++----------------------
1 file changed, 46 insertions(+), 22 deletions(-)

diff --git a/main.c b/main.c @@ -6,23 +6,28 @@ #include "obj.h" +/* macros */ #define INIT_WIDTH 800 #define INIT_HEIGHT 600 #define FPS 60 -#define RECT_WIDTH 300 -#define RECT_HEIGHT 300 -int -main(void) -{ - Display *display; - Window window; - unsigned int win_width = INIT_WIDTH, win_height = INIT_HEIGHT; - GC gc; - Pixmap pm; - Atom wm_delete_window; - Obj square = {1, 1, 100, 100, 20, 20}; +/* variables */ +Display *display; +Window window; +unsigned int win_width = INIT_WIDTH, win_height = INIT_HEIGHT; +GC gc; +Atom wm_delete_window; +Obj square = {1, 1, 100, 100, 20, 20}; +int quit = 0; +/* 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); @@ -36,8 +41,6 @@ main(void) 0); XStoreName(display, window, "UNKO"); gc = XCreateGC(display, window, 0, NULL); - pm = XCreatePixmap(display, window, win_width, win_height, - DefaultDepth(display, DefaultScreen(display))); wm_delete_window = XInternAtom(display, "WM_DELETE_WINDOW", False); XSetWMProtocols(display, window, &wm_delete_window, 1); @@ -45,8 +48,19 @@ main(void) XSelectInput(display, window, KeyPressMask); XMapWindow(display, window); +} + +void +cleanup(void) +{ + XCloseDisplay(display); +} + +int +main(void) +{ + setup(); - int quit = 0; while (!quit) { while (XPending(display) > 0) { XEvent event = {0}; @@ -57,6 +71,18 @@ main(void) case 'q': quit = 1; break; + case 'd': + square.vx += 10; + break; + case 'a': + square.vx -= 10; + break; + case 'w': + square.vy -= 10; + break; + case 's': + square.vy += 10; + break; default: break; } @@ -69,15 +95,12 @@ main(void) } } - XSetForeground(display, gc, 0); - XFillRectangle(display, pm, gc, 0, 0, win_width, win_height); XWindowAttributes window_attributes_return; XGetWindowAttributes(display, window, &window_attributes_return); win_width = window_attributes_return.width; win_height = window_attributes_return.height; - if(square.px <= 0){ square.px = 0; square.vx *= -1; @@ -96,15 +119,16 @@ main(void) } obj_accel(&square, 0, 9.8); obj_next_tick(&square, 1.0/FPS); - XSetForeground(display, gc, 0x00FF00); - XFillRectangle(display, pm, gc, square.px, square.py, square.width, square.height); - XCopyArea(display, pm, window, gc, 0, 0, win_width, win_height, 0, 0); + + XClearArea(display, window, 0, 0, win_width, win_height, False); + XSetForeground(display, gc, 0x00FF00); + XFillRectangle(display, window, gc, square.px, square.py, square.width, square.height); struct timespec timetosleep = {0, 1000*1000*1000/FPS}; //not accurate nanosleep(&timetosleep, NULL); } - XCloseDisplay(display); + cleanup(); return 0; }