summaryrefslogtreecommitdiff
path: root/chapter31/src/main/c
diff options
context:
space:
mode:
Diffstat (limited to 'chapter31/src/main/c')
-rw-r--r--chapter31/src/main/c/simple_crt.c10
-rw-r--r--chapter31/src/main/c/simple_graphics.c8
-rw-r--r--chapter31/src/main/c/simple_graphics.h5
-rw-r--r--chapter31/src/main/c/simple_minifb.c39
4 files changed, 54 insertions, 8 deletions
diff --git a/chapter31/src/main/c/simple_crt.c b/chapter31/src/main/c/simple_crt.c
index 39708629..e50042c2 100644
--- a/chapter31/src/main/c/simple_crt.c
+++ b/chapter31/src/main/c/simple_crt.c
@@ -3,6 +3,7 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
+#include <math.h>
#include <termios.h>
#include <unistd.h>
@@ -19,6 +20,15 @@ double simple_f64_from_bits(int64_t bits) {
return value;
}
+// Floating remainder's machine instruction uses this memory-preserving helper.
+double simple_f64_mod(double x, double y) {
+ // fmod can set errno for NaN-producing inputs; arithmetic leaves it alone.
+ int saved_errno = errno;
+ double result = fmod(x, y);
+ errno = saved_errno;
+ return result;
+}
+
// errno may be thread-local or a macro calling a platform accessor.
int32_t simple_errno(void) { return errno; }
int32_t simple_set_errno(int32_t error) { errno = error; return -1; }
diff --git a/chapter31/src/main/c/simple_graphics.c b/chapter31/src/main/c/simple_graphics.c
index 40d66ba9..a35c28a6 100644
--- a/chapter31/src/main/c/simple_graphics.c
+++ b/chapter31/src/main/c/simple_graphics.c
@@ -74,7 +74,8 @@ static int load_graphics(void) {
if (!candidate || candidate->version != SIMPLE_GRAPHICS_VERSION ||
!candidate->open || !candidate->present || !candidate->poll ||
!candidate->mouse_x || !candidate->mouse_y || !candidate->button ||
- !candidate->key || !candidate->close || !candidate->wait) {
+ !candidate->key || !candidate->close || !candidate->wait ||
+ !candidate->width || !candidate->height || !candidate->present_size) {
fprintf(stderr, "Simple graphics: incompatible MiniFB backend '%s'. Rebuild with make graphics.\n", path);
dlclose(library);
library = NULL;
@@ -93,6 +94,11 @@ int32_t simple_gfx_open(const char *title, uint32_t width, uint32_t height) {
int32_t simple_gfx_present(const uint32_t *pixels, uint32_t count) {
return api ? api->present(pixels, count) : -1;
}
+int32_t simple_gfx_present_size(const uint32_t *pixels, uint32_t count, uint32_t width, uint32_t height) {
+ return api ? api->present_size(pixels, count, width, height) : -1;
+}
+uint32_t simple_gfx_width(void) { return api ? api->width() : 0; }
+uint32_t simple_gfx_height(void) { return api ? api->height() : 0; }
int32_t simple_gfx_poll(int32_t wait) { return api ? api->poll(wait) : -1; }
int32_t simple_gfx_mouse_x(void) { return api ? api->mouse_x() : 0; }
int32_t simple_gfx_mouse_y(void) { return api ? api->mouse_y() : 0; }
diff --git a/chapter31/src/main/c/simple_graphics.h b/chapter31/src/main/c/simple_graphics.h
index 370256be..d19cac46 100644
--- a/chapter31/src/main/c/simple_graphics.h
+++ b/chapter31/src/main/c/simple_graphics.h
@@ -15,6 +15,9 @@ struct simple_graphics_api {
int32_t (*key)(int32_t);
void (*close)(void);
int32_t (*wait)(int64_t);
+ uint32_t (*width)(void);
+ uint32_t (*height)(void);
+ int32_t (*present_size)(const uint32_t *, uint32_t, uint32_t, uint32_t);
};
-#define SIMPLE_GRAPHICS_VERSION 2
+#define SIMPLE_GRAPHICS_VERSION 3
#endif
diff --git a/chapter31/src/main/c/simple_minifb.c b/chapter31/src/main/c/simple_minifb.c
index 2117f0d2..86f110c3 100644
--- a/chapter31/src/main/c/simple_minifb.c
+++ b/chapter31/src/main/c/simple_minifb.c
@@ -14,11 +14,19 @@
#include "x11/WindowData_X11.h"
#endif
-// One fixed-size window for now. No Simple pointer survives a native call:
+// One resizable window. No Simple pointer survives a native call:
// MiniFB retains its last draw buffer for OS repaint events, so copy it here.
static struct mfb_window *window;
static uint32_t *frame;
static uint32_t width, height;
+static size_t capacity;
+
+static uint32_t window_width(void) { return window ? mfb_get_window_width(window) : 0; }
+static uint32_t window_height(void) { return window ? mfb_get_window_height(window) : 0; }
+
+static void resize_window(struct mfb_window *w, int x, int y) {
+ if (x > 0 && y > 0) mfb_set_viewport(w, 0, 0, (unsigned)x, (unsigned)y);
+}
// The C-only build never installs the upstream C++ callback wrappers.
void release_cpp_stub(struct mfb_window *unused) { (void)unused; }
@@ -31,6 +39,7 @@ static void close_window(void) {
}
free(frame);
frame = NULL;
+ capacity = 0;
}
static int32_t open_window(const char *title, uint32_t w, uint32_t h) {
@@ -44,7 +53,8 @@ static int32_t open_window(const char *title, uint32_t w, uint32_t h) {
fprintf(stderr, "Simple graphics: cannot allocate the display buffer.\n");
return -1;
}
- window = mfb_open(title, w, h);
+ capacity = (size_t)w * h;
+ window = mfb_open_ex(title, w, h, MFB_WF_RESIZABLE);
if (!window) {
fprintf(stderr, "Simple graphics: MiniFB could not open a window (is a graphical display available?).\n");
close_window();
@@ -52,6 +62,7 @@ static int32_t open_window(const char *title, uint32_t w, uint32_t h) {
}
width = w;
height = h;
+ mfb_set_resize_callback(window, resize_window);
return 0;
}
@@ -67,14 +78,29 @@ static int32_t status(mfb_update_state state) {
return -1;
}
-static int32_t present(const uint32_t *pixels, uint32_t count) {
+static int32_t present_size(const uint32_t *pixels, uint32_t count, uint32_t w, uint32_t h) {
if (!window) return 1;
- if (!pixels || count != (uint64_t)width * height) {
+ if (!pixels || !w || !h || w > UINT32_MAX / 4 ||
+ count != (uint64_t)w * h || (uint64_t)w * h > SIZE_MAX / 4) {
fprintf(stderr, "Simple graphics: pixel array length must equal width * height.\n");
return -1;
}
+ if (count > capacity) {
+ uint32_t *larger = realloc(frame, (size_t)count * sizeof(*frame));
+ if (!larger) {
+ fprintf(stderr, "Simple graphics: cannot resize the display buffer.\n");
+ return -1;
+ }
+ frame = larger;
+ capacity = count;
+ }
memcpy(frame, pixels, (size_t)count * sizeof(*frame));
- return status(mfb_update_ex(window, frame, width, height));
+ return status(mfb_update_ex(window, frame, w, h));
+}
+
+// Compatibility for clients drawing at their original resolution.
+static int32_t present(const uint32_t *pixels, uint32_t count) {
+ return present_size(pixels, count, width, height);
}
static int32_t wait_events(int64_t timeout) {
@@ -109,7 +135,8 @@ static int32_t key(int32_t k) {
static const struct simple_graphics_api api = {
SIMPLE_GRAPHICS_VERSION, open_window, present, poll_events,
- mouse_x, mouse_y, button, key, close_window, wait_events
+ mouse_x, mouse_y, button, key, close_window, wait_events,
+ window_width, window_height, present_size
};
#ifdef _WIN32
__declspec(dllexport)