no libc, start on socket x11

This commit is contained in:
veclavtlica
2026-10-04 11:13:07 +03:00
parent 1125073568
commit 14062e9d00
21 changed files with 516 additions and 222 deletions
+101 -95
View File
@@ -1,19 +1,20 @@
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "../../Common/lzw.h"
#include "../../Common/mat.h"
#include "../../Common/std.h"
#include "../draw.h"
#include "display.h"
/* https://hereket.com/posts/from-scratch-x11-windowing/ */
bool quited = false;
/* TODO: Have a cfg.h configuration header setting those. */
#define WIDTH 640
#define HEIGHT 480
@@ -27,7 +28,7 @@ extern int plr_display_x11_main(int argc, char *argv[]) {
bool test = com_lzw_compress(&table, test_string, sizeof(test_string),
&compressed_string, &compressed_string_sz);
com_lzw_free_table(&table);
assert(test);
com_std_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};
@@ -46,98 +47,103 @@ extern int plr_display_x11_main(int argc, char *argv[]) {
com_fix_run_bench();
com_mat_run_bench();
Display *display = XOpenDisplay(NULL);
if (NULL == display) {
fprintf(stderr, "Failed to initialize display");
return EXIT_FAILURE;
// Display *display = XOpenDisplay(NULL);
// if (NULL == display) {
// com_std_printf("Failed to initialize display");
// return -1;
// }
// Window root = DefaultRootWindow(display);
// if (None == root) {
// com_std_printf("No root window found");
// XCloseDisplay(display);
// return -1;
// }
// 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) {
// com_std_printf("Failed to create window");
// XCloseDisplay(display);
// return -1;
// }
// XSizeHints *hints = XAllocSizeHints();
// if (hints == NULL)
// return -1;
// // 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);
// char pixel_buffer[WIDTH * HEIGHT * bytes_per_pixel];
// XImage *ximage = XCreateImage(display, visual, depth, ZPixmap, 0,
// pixel_buffer, WIDTH, HEIGHT, 32, 0);
// if (!ximage) {
// com_std_printf("Failed to create XImage\n");
// // com_std_alloc(pixel_buffer, 0);
// XCloseDisplay(display);
// return -1;
// }
// 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;
// plr_draw_screen((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);
while (1) {
}
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;
plr_draw_screen((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;
}
+12 -2
View File
@@ -1,5 +1,15 @@
#include "../Display/display.h"
extern int main(int argc, char *argv[]) {
return plr_display_x11_main(argc, argv);
__attribute__((force_align_arg_pointer)) __attribute__((noreturn)) extern void
_start(void) {
plr_display_x11_main(0, 0);
__asm__ volatile("mov $60, %%rax\n\t"
"xor %%rdi, %%rdi\n\t"
"syscall"
:
:
: "%rax", "%rdi");
__builtin_unreachable();
}
+2 -2
View File
@@ -3,7 +3,7 @@ CC = clang
CFLAGS = -Wall -std=c99 -g3 -O3 -flto=full
DEPS = ../Common/lzw.h ../Common/mat.h ../Common/vec.h ../Common/fix.h ../Common/def.h ../Common/bits.h \
../Common/gem.h \
../Common/Timer/timer.h \
../Common/Timer/time.h \
./Display/display.h \
./draw.h
SRC = ../Common/lzw.c ../Common/fix.c ../Common/mat.c ./draw.c
@@ -15,7 +15,7 @@ DOS = maindos.c Display/dos.c
$(CC) -c -o $@ $< $(CFLAGS)
# TODO: Make it work with no stdlib.
linux: CFLAGS += -msse2 -lm -lX11 # -nostdlib
linux: CFLAGS += -msse2 -mbmi2 -nostdlib
linux: $(LINUX) | $(SRC)
$(CC) -o player $^ $(CFLAGS)
+3 -2
View File
@@ -6,11 +6,12 @@
#include "../Common/fix.c"
#include "../Common/lzw.c"
#include "../Common/mat.c"
#include "../Common/zord.c"
#include "./draw.c"
#if COM_DEF_TARGET == COM_DEF_TARGET_LINUX
#include "../Common/Std/libc.c"
#include "../Common/Timer/posix.c"
#include "../Common/Std/linux.c"
#include "../Common/Timer/stub.c"
#include "./Display/x11.c"
#include "./Main/linux.c"
#elif COM_DEF_TARGET == COM_DEF_TARGET_WASM
+30 -14
View File
@@ -1,6 +1,8 @@
#include "../Common/def.h"
#include "../Common/gem.h"
#include "../Common/std.h"
#include "../Common/time.h"
#include <stdint.h>
#if COM_DEF_TARGET == COM_DEF_TARGET_WASM
@@ -33,38 +35,52 @@ com_def_export_sym("plr_draw_screen") void plr_draw_screen(
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_vec_t v0 = com_vec_from(0 * COM_FIX_FRACUNIT, 0, 1 * COM_FIX_FRACUNIT);
com_vec_t v1 = com_vec_from(1 * COM_FIX_FRACUNIT, 1 * COM_FIX_FRACUNIT,
1 * COM_FIX_FRACUNIT);
com_vec_t v2 = com_vec_from(1 * COM_FIX_FRACUNIT, 1 * COM_FIX_FRACUNIT,
1 * COM_FIX_FRACUNIT);
com_vec_t v3 = com_vec_from(1 * COM_FIX_FRACUNIT, 1 * COM_FIX_FRACUNIT,
-1 * 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_pnt_t uv3 = com_pnt_from(0 * 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_vec_t c3 = com_gem_vec_project_clip(mvp, v3);
com_pnt_t p0, p1, p2;
com_pnt_t p0, p1, p2, p3;
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);
uint8_t cc3 = com_gem_clip_vis_project(c3, &p3);
com_pnt_print(p0);
com_pnt_print(p1);
com_pnt_print(p2);
com_std_printf("%i, %i\n", p0.a[0], p0.a[1]);
com_std_printf("%i, %i\n", p1.a[0], p1.a[1]);
com_std_printf("%i, %i\n", p2.a[0], p2.a[1]);
com_std_printf("%i, %i\n", p3.a[0], p3.a[1]);
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;
// uint32_t idx = com_zord_order(x, y);
uint32_t idx = y * 32 + x;
tex[idx * 3 + 0] = (x / 2 + y / 2) % 2 == 0 ? 175 : 0;
tex[idx * 3 + 1] = (x / 2 + y / 2) % 2 == 0 ? 0 : 0;
tex[idx * 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);
uint64_t time = com_timer_count_ns();
for (int i = 5000; i--;) {
// com_gem_draw_triangle_textured_edge(p0, p1, p2, (uint8_t *)pixel_buffer,
// uv0, uv1, uv2, tex);
// com_gem_draw_triangle_textured_span(p0, p1, p3, (uint8_t *)pixel_buffer,
// uv0, uv1, uv3, tex);
}
com_timer_profile(time, "textured triangle");
}