diff options
| author | kkard2 <[email protected]> | 2025-09-22 22:13:10 +0200 |
|---|---|---|
| committer | kkard2 <[email protected]> | 2025-09-22 22:13:10 +0200 |
| commit | bcfd31ce9ea9ea067b285a78eb747e3d68561bca (patch) | |
| tree | 6e2ffc638dbd89dae0709bb30f8abfa40188eb98 | |
| parent | 42d3b218942d7ece8a15ef9298c8cd13974aa4c9 (diff) | |
make x11 run again
| -rw-r--r-- | examples/platform/platform_win32.c | 4 | ||||
| -rw-r--r-- | examples/platform/platform_x11.c | 56 | ||||
| -rwxr-xr-x[-rw-r--r--] | run_example | 3 | ||||
| -rw-r--r-- | sponge.h | 2 |
4 files changed, 45 insertions, 20 deletions
diff --git a/examples/platform/platform_win32.c b/examples/platform/platform_win32.c index 5984e75..76cca72 100644 --- a/examples/platform/platform_win32.c +++ b/examples/platform/platform_win32.c @@ -32,7 +32,7 @@ int update_canvas(sponge_Texture *canvas, float **depths, uint32_t new_width, ui float *new_depths = malloc(new_count * sizeof(float)); if (!new_pixels_buffer || !new_depths) { free(new_pixels_buffer); - free(new_pixels_buffer); + free(new_depths); return 0; } free(canvas->pixels); @@ -43,7 +43,7 @@ int update_canvas(sponge_Texture *canvas, float **depths, uint32_t new_width, ui } canvas->width = new_width; canvas->height = new_height; - canvas->stride_pixels = new_width; // TODO(kard): think about this if we want image to stay the same without redraw + canvas->stride_pixels = new_width; ZeroMemory(&bmi, sizeof(bmi)); bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); diff --git a/examples/platform/platform_x11.c b/examples/platform/platform_x11.c index 7887d11..3136e51 100644 --- a/examples/platform/platform_x11.c +++ b/examples/platform/platform_x11.c @@ -3,36 +3,57 @@ #include <X11/Xlib.h> #include <X11/Xutil.h> +#include <stddef.h> #include <stdio.h> #include <stdlib.h> +#include <time.h> #include <unistd.h> -// TODO(kard): we should probably put more thought into this -#include "example.c" +#define SPONGE_IMPLEMENTATION +#include "../../sponge.h" + +#define STB_IMAGE_IMPLEMENTATION +#include "../vendor/stb_image.h" + +#include "../example.h" #define DEFAULT_WIDTH 256 #define DEFAULT_HEIGHT 256 -static sponge_Canvas canvas = { 0 }; +static size_t pixels_buffer_count = 0; +static sponge_Texture canvas = { 0 }; +static float *depths = NULL; static Display *dpy; -static XImage *ximg = 0; +static XImage *ximg = NULL; void init_canvas(uint32_t width, uint32_t height) { if (ximg) { - XDestroyImage(ximg); // also frees data (why??) - // TODO(kard): you can set ximg->data to null before XDestroyImage + ximg->data = NULL; + XDestroyImage(ximg); ximg = NULL; } - canvas.pixels = (uint32_t*)malloc(width * height * 4); - if (!canvas.pixels) { - fprintf(stderr, "failed to allocate pixel buffer\n"); - exit(1); + size_t new_count = width * height; + if (new_count > pixels_buffer_count) { + sponge_Color32 *new_pixels_buffer = malloc(new_count * sizeof(uint32_t)); + float *new_depths = malloc(new_count * sizeof(float)); + if (!new_pixels_buffer || !new_depths) { + free(new_pixels_buffer); + free(new_depths); + exit(1); + fprintf(stderr, "failed to allocate pixel buffer\n"); + } + free(canvas.pixels); + free(depths); + canvas.pixels = new_pixels_buffer; + depths = new_depths; + pixels_buffer_count = new_count; } + canvas.width = width; canvas.height = height; - canvas.stride = width; + canvas.stride_pixels = width; // TODO(kard): i assume this can fail ximg = XCreateImage( @@ -71,13 +92,14 @@ int main() { ); XStoreName(dpy, win, "sponge"); - XSelectInput(dpy, win, ExposureMask | KeyPressMask | StructureNotifyMask); + XSelectInput(dpy, win, ExposureMask | PointerMotionMask | StructureNotifyMask); GC gc = DefaultGC(dpy, screen); XMapWindow(dpy, win); init_canvas(DEFAULT_WIDTH, DEFAULT_HEIGHT); + init(); int running = 1; while (running) { @@ -89,16 +111,18 @@ int main() { //draw_frame(canvas); break; case ConfigureNotify: - // TODO(kard): this is some implicit signed to unsigned conversion init_canvas(ev.xconfigure.width, ev.xconfigure.height); break; - case KeyPress: + case MotionNotify: + mouse_move(ev.xmotion.x, ev.xmotion.y); break; } } - draw_frame(canvas); - XPutImage(dpy, win, gc, ximg, 0, 0, 0, 0, canvas.width, canvas.height); + if (sponge_texture_valid(canvas)) { + draw_frame_3d(canvas, depths); + XPutImage(dpy, win, gc, ximg, 0, 0, 0, 0, canvas.width, canvas.height); + } usleep(16000); } diff --git a/run_example b/run_example index f34e57a..80d6c10 100644..100755 --- a/run_example +++ b/run_example @@ -1,2 +1,3 @@ #!/bin/bash -cc -o platform_x11 platform_x11.c -lX11 -Wall -Wextra -g && ./platform_x11 +cc -o example examples/platform/platform_x11.c examples/$1 \ + -lX11 -lm -Wall -Wextra -Wno-type-limits -Wno-pointer-sign -g && ./example @@ -1,4 +1,5 @@ #include <stdint.h> +#include <stddef.h> #include <math.h> typedef union { @@ -412,7 +413,6 @@ void sponge_draw_line(sponge_Texture c, sponge_Vec2I v0, sponge_Vec2I v1, sponge int32_t dx = SPONGE_ABS(v1.x - v0.x); int32_t dy = SPONGE_ABS(v1.y - v0.y); - int32_t d = (2 * dy) - dx; int32_t sx = (v0.x < v1.x) ? 1 : -1; int32_t sy = (v0.y < v1.y) ? 1 : -1; |
