#include #include #include #include #include #include #include #include #include "../../Common/gem.h" #include "../../Common/lzw.h" #include "../../Common/mat.h" #include "display.h" bool quited = false; #define WIDTH 640 #define HEIGHT 480 // TODO: correct error handling. extern int plr_display_x11_main(int argc, char *argv[]) { char test_string[] = "what is this?"; char *compressed_string = NULL; uint32_t compressed_string_sz = 0; struct com_lzw_table table = com_lzw_infer_table(test_string, sizeof(test_string)); bool test = com_lzw_compress(&table, test_string, sizeof(test_string), &compressed_string, &compressed_string_sz); com_lzw_free_table(&table); assert(test); com_mat_t m0 = com_mat_identity(); com_mat_t m1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; for (int i = 0; i < 16; ++i) m1.a[i] *= COM_FIX_FRACUNIT; com_mat_t t0 = com_mat_mul(m1, m0); com_mat_t t1 = com_mat_mul_reodered(com_mat_reoder(m1), m0); com_fix_print(t0.a[4]); com_fix_print(t1.a[4]); // com_vec_t v0 = com_mat_vec_project(com_mat_reoder(m0), com_vec_identity()); // com_fix_print(v0.a[0]); com_fix_run_tests(); com_fix_run_bench(); com_mat_run_bench(); Display *display = XOpenDisplay(NULL); if (NULL == display) { fprintf(stderr, "Failed to initialize display"); return EXIT_FAILURE; } Window root = DefaultRootWindow(display); if (None == root) { fprintf(stderr, "No root window found"); XCloseDisplay(display); return EXIT_FAILURE; } int screen = DefaultScreen(display); Visual *visual = DefaultVisual(display, screen); int depth = DefaultDepth(display, screen); Window window = XCreateSimpleWindow(display, root, 0, 0, WIDTH, HEIGHT, 0, 0, 0xffffffff); if (None == window) { fprintf(stderr, "Failed to create window"); XCloseDisplay(display); return EXIT_FAILURE; } XSizeHints *hints = XAllocSizeHints(); if (hints == NULL) return EXIT_FAILURE; // Pinning min and max to the same values disables resizing hints->flags = PMinSize | PMaxSize; hints->min_width = hints->max_width = WIDTH; hints->min_height = hints->max_height = HEIGHT; XSetWMNormalHints(display, window, hints); XFree(hints); XSelectInput(display, window, ExposureMask | KeyPressMask); XMapWindow(display, window); GC gc = XCreateGC(display, window, 0, NULL); Atom wm_delete_window = XInternAtom(display, "WM_DELETE_WINDOW", False); XSetWMProtocols(display, window, &wm_delete_window, 1); // TODO: can't be not true int bytes_per_pixel = 4; char *pixel_buffer = (char *)malloc(WIDTH * HEIGHT * bytes_per_pixel); XImage *ximage = XCreateImage(display, visual, depth, ZPixmap, 0, pixel_buffer, WIDTH, HEIGHT, 32, 0); if (!ximage) { fprintf(stderr, "Failed to create XImage\n"); free(pixel_buffer); XCloseDisplay(display); return EXIT_FAILURE; } int frame = 0; XEvent event; while (!quited) { XNextEvent(display, &event); switch (event.type) { case ClientMessage: if (event.xclient.data.l[0] == wm_delete_window) { XDestroyWindow(display, window); quited = true; } break; case Expose: frame += 2; // Fill buffer with a simple color gradient for (int y = 0; y < HEIGHT; y++) { for (int x = 0; x < WIDTH; x++) { int pixel_index = (y * WIDTH + x) * bytes_per_pixel; // Assuming standard Little-Endian 32-bit BGRA format pixel_buffer[pixel_index + 0] = (x + frame) % 256; // Blue pixel_buffer[pixel_index + 1] = (y + frame) % 256; // Green pixel_buffer[pixel_index + 2] = (x + y - frame) % 256; // Red pixel_buffer[pixel_index + 3] = 0; // Alpha/Padding } } com_mat_t proj = com_mat_perspective(640, 480, 1, 100 * COM_FIX_FRACUNIT, COM_FIX_PIHALF); com_mat_t view = com_mat_look_at(com_vec_from(0, 0, 0), com_vec_from(0, COM_FIX_FRACUNIT, 0), com_vec_from(-COM_FIX_FRACUNIT, 0, 0)); com_mat_t mvp = com_mat_reoder(com_mat_mul(proj, view)); com_vec_t v0 = com_vec_from(5 * COM_FIX_FRACUNIT, 0, 2 * COM_FIX_FRACUNIT); com_vec_t v1 = com_vec_from(5 * COM_FIX_FRACUNIT, 1 * COM_FIX_FRACUNIT, 4 * COM_FIX_FRACUNIT); com_vec_t v2 = com_vec_from(5 * COM_FIX_FRACUNIT, 4 * COM_FIX_FRACUNIT, 4 * COM_FIX_FRACUNIT); com_pnt_t uv0 = com_pnt_from(0 * COM_FIX_FRACUNIT, 0 * COM_FIX_FRACUNIT); com_pnt_t uv1 = com_pnt_from(1 * COM_FIX_FRACUNIT, 0 * COM_FIX_FRACUNIT); com_pnt_t uv2 = com_pnt_from(1 * COM_FIX_FRACUNIT, 1 * COM_FIX_FRACUNIT); com_vec_t c0 = com_gem_vec_project_clip(mvp, v0); com_vec_t c1 = com_gem_vec_project_clip(mvp, v1); com_vec_t c2 = com_gem_vec_project_clip(mvp, v2); com_pnt_t p0, p1, p2; uint8_t cc0 = com_gem_clip_vis_project(c0, &p0); uint8_t cc1 = com_gem_clip_vis_project(c1, &p1); uint8_t cc2 = com_gem_clip_vis_project(c2, &p2); com_pnt_print(p0); com_pnt_print(p1); com_pnt_print(p2); uint8_t tex[32 * 32 * 3] = {0}; for (int y = 0; y < 32; ++y) { for (int x = 0; x < 32; ++x) { tex[(y * 32 + x) * 3 + 0] = (x / 2 + y / 2) % 2 == 0 ? 175 : 0; tex[(y * 32 + x) * 3 + 1] = (x / 2 + y / 2) % 2 == 0 ? 0 : 0; tex[(y * 32 + x) * 3 + 2] = (x / 2 + y / 2) % 2 == 0 ? 175 : 0; } } com_gem_draw_triangle_textured(p0, p1, p2, (uint8_t *)pixel_buffer, uv0, uv1, uv2, tex); // com_gem_draw_triangle(p0, p1, p2, (uint8_t *)pixel_buffer); // Draw the complete image onto the window when exposed XPutImage(display, window, // Target drawable gc, // Graphics Context ximage, // Source XImage 0, 0, // Source coordinates (x, y) 0, 0, // Destination coordinates (x, y) WIDTH, HEIGHT // Dimensions to copy ); break; } } XCloseDisplay(display); return 0; }