From a136586a339032126540ceb24f7c3f15eaba68c8 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Fri, 20 May 2022 19:44:20 -0400 Subject: 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. --- examples/pugl_embed_demo.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'examples/pugl_embed_demo.c') 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 #include -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; } -- cgit v1.2.1