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. --- include/pugl/pugl.h | 50 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 17 deletions(-) (limited to 'include/pugl') diff --git a/include/pugl/pugl.h b/include/pugl/pugl.h index 2c00a30..3fc124e 100644 --- a/include/pugl/pugl.h +++ b/include/pugl/pugl.h @@ -59,6 +59,18 @@ PUGL_BEGIN_DECLS @{ */ +/** + A pixel coordinate within/of a view. + + This is relative to the top left corner of the view's parent, or to the top + left corner of the view itself, depending on the context. + + There are platform-imposed limits on window positions. For portability, + applications should keep coordinates between -16000 and 16000. Note that + negative frame coordinates are possible, for example with multiple screens. +*/ +typedef int16_t PuglCoord; + /** A pixel span (width or height) within/of a view. @@ -68,16 +80,20 @@ PUGL_BEGIN_DECLS typedef uint16_t PuglSpan; /** - A rectangle. + A rectangle in a view or on the screen. + + This type is used to describe two things: the position and size of a view + (for configuring), or a rectangle within a view (for exposing). - This is used to describe things like view position and size. Pugl generally - uses coordinates where the top left corner is 0,0. + The coordinate (0, 0) represents the top-left pixel of the parent window (or + display if there isn't one), or the top-left pixel of the view, + respectively. */ typedef struct { - double x; - double y; - double width; - double height; + PuglCoord x; + PuglCoord y; + PuglSpan width; + PuglSpan height; } PuglRect; /** @@ -267,10 +283,10 @@ typedef PuglAnyEvent PuglDestroyEvent; typedef struct { PuglEventType type; ///< #PUGL_CONFIGURE PuglEventFlags flags; ///< Bitwise OR of #PuglEventFlag values - double x; ///< New parent-relative X coordinate - double y; ///< New parent-relative Y coordinate - double width; ///< New width - double height; ///< New height + PuglCoord x; ///< Parent-relative X coordinate of view + PuglCoord y; ///< Parent-relative Y coordinate of view + PuglSpan width; ///< Width of view + PuglSpan height; ///< Height of view } PuglConfigureEvent; /** @@ -313,10 +329,10 @@ typedef PuglAnyEvent PuglUpdateEvent; typedef struct { PuglEventType type; ///< #PUGL_EXPOSE PuglEventFlags flags; ///< Bitwise OR of #PuglEventFlag values - double x; ///< View-relative X coordinate - double y; ///< View-relative Y coordinate - double width; ///< Width of exposed region - double height; ///< Height of exposed region + PuglCoord x; ///< View-relative top-left X coordinate of region + PuglCoord y; ///< View-relative top-left Y coordinate of region + PuglSpan width; ///< Width of exposed region + PuglSpan height; ///< Height of exposed region } PuglExposeEvent; /** @@ -1451,8 +1467,8 @@ puglInitWindowSize(PuglView* view, int width, int height) { PuglRect frame = puglGetFrame(view); - frame.width = width; - frame.height = height; + frame.width = (PuglSpan)width; + frame.height = (PuglSpan)height; puglSetFrame(view, frame); } -- cgit v1.2.1