experiments/tinypaint/window.c
2024-05-27 21:30:27 +12:00

70 lines
1.9 KiB
C

#include <X11/Xlib.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
int main() {
Display *display;
Window window;
int screen;
GC gc;
XEvent event;
display = XOpenDisplay(NULL);
if (display == NULL) {
fprintf(stderr, "Unable to open X display\n");
exit(1);
}
screen = DefaultScreen(display);
window = XCreateSimpleWindow(display, RootWindow(display, screen), 10, 10, 800, 600, 1,
BlackPixel(display, screen), WhitePixel(display, screen));
XSelectInput(display, window, ExposureMask | KeyPressMask);
XMapWindow(display, window);
gc = XCreateGC(display, window, 0, NULL);
XSetForeground(display, gc, BlackPixel(display, screen));
XGetWindowAttributes(display, window, &attributes);
*width = attributes.width;
*height = attributes.height;
int width = 800;
int height = 600;
int depth = DefaultDepth(display, screen);
// Create a pixel buffer
uint32_t* pixel_buffer = (uint32_t*)malloc(width * height * sizeof(uint32_t));
if (pixel_buffer == NULL) {
fprintf(stderr, "Unable to allocate pixel buffer\n");
exit(1);
}
// Initialize pixel buffer (example: fill with blue color)
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
pixel_buffer[y * width + x] = 0x0000FF; // Blue color
}
}
// Create an XImage
XImage *image = XCreateImage(display, DefaultVisual(display, screen), depth, ZPixmap, 0,
(char *)pixel_buffer, width, height, 32, 0);
while(1) {
XNextEvent(display, &event);
if (event.type == Expose) {
XPutImage(display, window, gc, image, 0, 0, 0, 0, width, height);
}
}
XDestroyImage(image);
XFreeGC(display, gc);
XDestroyWindow(display, window);
XCloseDisplay(display);
return 0;
}