diff options
author | David Robillard <d@drobilla.net> | 2022-05-20 19:44:20 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2022-05-21 16:49:47 -0400 |
commit | a136586a339032126540ceb24f7c3f15eaba68c8 (patch) | |
tree | cd3e35e2cab5c71517c691cfad6dbeac8664b945 /examples/pugl_window_demo.c | |
parent | 1cd37cad0a06fbb15c44fd59dd6b2c12a0812a76 (diff) | |
download | pugl-a136586a339032126540ceb24f7c3f15eaba68c8.tar.gz pugl-a136586a339032126540ceb24f7c3f15eaba68c8.tar.bz2 pugl-a136586a339032126540ceb24f7c3f15eaba68c8.zip |
Use consistent integer types for view positions and sizes
Actual window sizes and positions fit easily in a 16-bit integer. So, we use
that in "representation contexts" like events. This makes structures smaller,
and allows the values to be converted to float, double, or integer without
casting (since any int16_t or uint16_t value can fit in them without loss).
Setter APIs use native integers for convenience, to avoid casting hassles when
doing arithmetic. Ranges are checked at runtime.
Diffstat (limited to 'examples/pugl_window_demo.c')
-rw-r--r-- | examples/pugl_window_demo.c | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/examples/pugl_window_demo.c b/examples/pugl_window_demo.c index 7cb3722..8d79f00 100644 --- a/examples/pugl_window_demo.c +++ b/examples/pugl_window_demo.c @@ -14,6 +14,7 @@ #include <math.h> #include <stdbool.h> +#include <stdint.h> #include <string.h> typedef struct { @@ -35,7 +36,7 @@ typedef struct { bool verbose; } PuglTestApp; -static const double pad = 64.0; +static const uint8_t pad = 64u; static void onDisplay(PuglView* view) @@ -69,26 +70,26 @@ onKeyPress(PuglView* view, const PuglKeyEvent* event) app->quit = 1; } else if (event->state & PUGL_MOD_SHIFT) { if (event->key == PUGL_KEY_UP) { - frame.height += 10; + frame.height = (PuglSpan)(frame.height + 10u); } else if (event->key == PUGL_KEY_DOWN) { - frame.height -= 10; + frame.height = (PuglSpan)(frame.height - 10u); } else if (event->key == PUGL_KEY_LEFT) { - frame.width -= 10; + frame.width = (PuglSpan)(frame.width - 10u); } else if (event->key == PUGL_KEY_RIGHT) { - frame.width += 10; + frame.width = (PuglSpan)(frame.width + 10u); } else { return; } puglSetFrame(view, frame); } else { if (event->key == PUGL_KEY_UP) { - frame.y -= 10; + frame.y = (PuglCoord)(frame.y - 10); } else if (event->key == PUGL_KEY_DOWN) { - frame.y += 10; + frame.y = (PuglCoord)(frame.y + 10); } else if (event->key == PUGL_KEY_LEFT) { - frame.x -= 10; + frame.x = (PuglCoord)(frame.x - 10); } else if (event->key == PUGL_KEY_RIGHT) { - frame.x += 10; + frame.x = (PuglCoord)(frame.x + 10); } else { return; } @@ -187,8 +188,10 @@ main(int argc, char** argv) for (unsigned i = 0; i < 2; ++i) { CubeView* cube = &app.cubes[i]; PuglView* view = cube->view; - const PuglRect frame = { - pad + (128.0 + pad) * i, pad + (128.0 + pad) * i, 512.0, 512.0}; + const PuglRect frame = {(PuglCoord)(pad + (128.0 + pad) * i), + (PuglCoord)(pad + (128.0 + pad) * i), + 512u, + 512u}; cube->dist = 10; |