summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/platform/platform_win32.c4
-rw-r--r--examples/platform/platform_x11.c56
2 files changed, 42 insertions, 18 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);
}