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 | |
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')
-rw-r--r-- | examples/pugl_cairo_demo.c | 8 | ||||
-rw-r--r-- | examples/pugl_cpp_demo.cpp | 4 | ||||
-rw-r--r-- | examples/pugl_embed_demo.c | 27 | ||||
-rw-r--r-- | examples/pugl_window_demo.c | 25 |
4 files changed, 34 insertions, 30 deletions
diff --git a/examples/pugl_cairo_demo.c b/examples/pugl_cairo_demo.c index cf34158..c0e89c7 100644 --- a/examples/pugl_cairo_demo.c +++ b/examples/pugl_cairo_demo.c @@ -101,10 +101,10 @@ postButtonRedisplay(PuglView* view) for (const Button* b = buttons; b->label; ++b) { const double span = sqrt(b->w * b->w + b->h * b->h); - const PuglRect rect = {(b->x - span) * scaleX, - (b->y - span) * scaleY, - span * 2.0 * scaleX, - span * 2.0 * scaleY}; + const PuglRect rect = {(PuglCoord)((b->x - span) * scaleX), + (PuglCoord)((b->y - span) * scaleY), + (PuglSpan)ceil(span * 2.0 * scaleX), + (PuglSpan)ceil(span * 2.0 * scaleY)}; puglPostRedisplayRect(view, rect); } diff --git a/examples/pugl_cpp_demo.cpp b/examples/pugl_cpp_demo.cpp index 015e7aa..19b8065 100644 --- a/examples/pugl_cpp_demo.cpp +++ b/examples/pugl_cpp_demo.cpp @@ -57,8 +57,8 @@ CubeView::onEvent(const pugl::UpdateEvent&) noexcept // return postRedisplay(); // But for testing, use sendEvent() instead: - return sendEvent( - pugl::ExposeEvent{0u, 0.0, 0.0, frame().width, frame().height}); + return sendEvent(pugl::ExposeEvent{ + 0u, PuglCoord{0}, PuglCoord{0}, frame().width, frame().height}); } pugl::Status diff --git a/examples/pugl_embed_demo.c b/examples/pugl_embed_demo.c index 66605b7..ae3956a 100644 --- a/examples/pugl_embed_demo.c +++ b/examples/pugl_embed_demo.c @@ -14,7 +14,7 @@ #include <stdio.h> #include <string.h> -static const int borderWidth = 64; +static const uint8_t borderWidth = 64u; static const uintptr_t reverseTimerId = 1u; typedef struct { @@ -55,10 +55,11 @@ static const float backgroundColorVertices[] = { static PuglRect getChildFrame(const PuglRect parentFrame) { - const PuglRect childFrame = {borderWidth, - borderWidth, - parentFrame.width - 2 * borderWidth, - parentFrame.height - 2 * borderWidth}; + const PuglRect childFrame = { + borderWidth, + borderWidth, + (PuglSpan)(parentFrame.width - 2 * borderWidth), + (PuglSpan)(parentFrame.height - 2 * borderWidth)}; return childFrame; } @@ -118,26 +119,26 @@ onKeyPress(PuglView* view, const PuglKeyEvent* event, const char* prefix) fprintf(stderr, "%sPaste \"%s\"\n", prefix, text); } 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; } 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; |