commit 82d0689a7959e25611d5dfcc0d894e4fdb231212
parent 47ae560e0671f0ceb3aeec8ded95c76db6374596
Author: Matsuda Kenji <info@mtkn.jp>
Date: Wed, 17 Dec 2025 12:58:17 +0900
allocate picture
Diffstat:
| M | Makefile | | | 2 | +- |
| M | win32.c | | | 66 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- |
2 files changed, 63 insertions(+), 5 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,5 +1,5 @@
all:
- $(CC) -Wall -Wpedantic -Wextra -I/usr/X11R6/include win32.c -L/usr/X11R6/lib -lX11 -o win32
+ $(CC) -Wall -Wpedantic -Wextra -I/usr/X11R6/include win32.c -L/usr/X11R6/lib -lX11 -lXrender -o win32
clean:
rm -f win32
diff --git a/win32.c b/win32.c
@@ -2,9 +2,11 @@
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
+#include <X11/extensions/Xrender.h>
void fatal(char *msg);
int findVisual32(Display *display, XVisualInfo *return_visual_info);
+int findPictFormat(Display *display, int depth, XRenderPictFormat *return_pictformat);
int main(void) {
Display *display = XOpenDisplay(NULL);
@@ -25,13 +27,34 @@ int main(void) {
&attr);
XMapWindow(display, window);
+ XRenderPictFormat pictformat, pictformat24, pictformat32;
+ if (findPictFormat(display, 24, &pictformat24) < 0) {
+ fatal("24bit-deep pictformat not found");
+ }
+ if (findPictFormat(display, 32, &pictformat32) < 0) {
+ fatal("32bit-deep pictformat not found");
+ }
+ switch (DefaultDepth(display, screen)) {
+ case 24:
+ pictformat = pictformat24;
+ break;
+ case 32:
+ pictformat = pictformat32;
+ break;
+ default:
+ fatal("unsupported screen depth");
+ }
+
+ Picture picture = XRenderCreatePicture(display, window, &pictformat, 0, NULL);
+ XRenderColor color = { .red = 0xffff, .green = 0xffff, .blue = 0xeaea, .alpha = 0xffff };
+ XRectangle rectangle = { .x = 0, .y = 0, .width = 800, .height = 600 };
+
+/*
XVisualInfo vi32;
if (findVisual32(display, &vi32) < 0) {
fatal("32bit-deep visual info not found");
}
-
Colormap colormap32 = XCreateColormap(display, DefaultRootWindow(display), vi32.visual, AllocNone);
-
XSetWindowAttributes attr32 = {
.border_pixel = 0,
.colormap = colormap32,
@@ -43,8 +66,8 @@ int main(void) {
vi32.visual,
CWBorderPixel|CWColormap,
&attr32);
-
- XSync(display, window);
+*/
+ XSync(display, 0);
int quit = 0;
XEvent event;
while (!quit) {
@@ -52,6 +75,7 @@ int main(void) {
if (event.type == KeyPress && event.xkey.keycode == 49) {
quit = 1;
}
+ XRenderFillRectangles(display, PictOpSrc, picture, &color, &rectangle, 1);
}
XCloseDisplay(display);
@@ -79,3 +103,37 @@ int findVisual32(Display *display, XVisualInfo *return_visual_info) {
XFree(vinfos);
return 0;
}
+
+XRenderDirectFormat directformat24 = {
+ .red = 16,
+ .redMask = 0xff,
+ .green = 8,
+ .greenMask = 0xff,
+ .blue = 0,
+ .blueMask = 0xff,
+};
+
+XRenderDirectFormat directformat32 = {
+ .alpha = 24,
+ .alphaMask = 0xff,
+ .red = 16,
+ .redMask = 0xff,
+ .green = 8,
+ .greenMask = 0xff,
+ .blue = 0,
+ .blueMask = 0xff,
+};
+
+int findPictFormat(Display *display, int depth, XRenderPictFormat *return_pictformat) {
+ long mask = PictFormatType | PictFormatDepth;
+ XRenderPictFormat template = {
+ .type = PictTypeDirect,
+ .depth = depth,
+ };
+ XRenderPictFormat *f = XRenderFindFormat(display, mask, &template, 0);
+ if (!f) {
+ return -1;
+ }
+ *return_pictformat = *f;
+ return 0;
+}