From 426222903151173637c9a49246c182e79618bf83 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 8 Jan 2023 19:44:41 -0500 Subject: Use ensureHint pattern everywhere --- src/internal.c | 8 ++++++++ src/internal.h | 4 ++++ src/mac.m | 16 ++++------------ src/mac_gl.m | 32 ++++++++++++++------------------ src/win.c | 16 ++++------------ src/win_gl.c | 32 ++++++++++++++------------------ 6 files changed, 48 insertions(+), 60 deletions(-) (limited to 'src') diff --git a/src/internal.c b/src/internal.c index 5549cbc..cfb444e 100644 --- a/src/internal.c +++ b/src/internal.c @@ -12,6 +12,14 @@ #include #include +void +puglEnsureHint(PuglView* const view, const PuglViewHint hint, const int value) +{ + if (view->hints[hint] == PUGL_DONT_CARE) { + view->hints[hint] = value; + } +} + PuglStatus puglSetBlob(PuglBlob* const dest, const void* const data, const size_t len) { diff --git a/src/internal.h b/src/internal.h index 0fb5f65..6400422 100644 --- a/src/internal.h +++ b/src/internal.h @@ -17,6 +17,10 @@ PUGL_BEGIN_DECLS +/// Set hint to a default value if it is unset (PUGL_DONT_CARE) +void +puglEnsureHint(PuglView* view, PuglViewHint hint, int value); + /// Set `blob` to `data` with length `len`, reallocating if necessary PuglStatus puglSetBlob(PuglBlob* dest, const void* data, size_t len); diff --git a/src/mac.m b/src/mac.m index 307a557..3b0896c 100644 --- a/src/mac.m +++ b/src/mac.m @@ -1129,18 +1129,10 @@ puglRealize(PuglView* view) const double scaleFactor = [screen backingScaleFactor]; // Getting depth from the display mode seems tedious, just set usual values - if (view->hints[PUGL_RED_BITS] == PUGL_DONT_CARE) { - view->hints[PUGL_RED_BITS] = 8; - } - if (view->hints[PUGL_BLUE_BITS] == PUGL_DONT_CARE) { - view->hints[PUGL_BLUE_BITS] = 8; - } - if (view->hints[PUGL_GREEN_BITS] == PUGL_DONT_CARE) { - view->hints[PUGL_GREEN_BITS] = 8; - } - if (view->hints[PUGL_ALPHA_BITS] == PUGL_DONT_CARE) { - view->hints[PUGL_ALPHA_BITS] = 8; - } + puglEnsureHint(view, PUGL_RED_BITS, 8); + puglEnsureHint(view, PUGL_GREEN_BITS, 8); + puglEnsureHint(view, PUGL_BLUE_BITS, 8); + puglEnsureHint(view, PUGL_ALPHA_BITS, 8); CGDirectDisplayID displayId = CGMainDisplayID(); CGDisplayModeRef mode = CGDisplayCopyDisplayMode(displayId); diff --git a/src/mac_gl.m b/src/mac_gl.m index 69acde7..b1430d9 100644 --- a/src/mac_gl.m +++ b/src/mac_gl.m @@ -11,6 +11,14 @@ # define NSOpenGLProfileVersion4_1Core NSOpenGLProfileVersion3_2Core #endif +static void +ensureHint(PuglView* const view, const PuglViewHint hint, const int value) +{ + if (view->hints[hint] == PUGL_DONT_CARE) { + view->hints[hint] = value; + } +} + @interface PuglOpenGLView : NSOpenGLView @end @@ -31,24 +39,12 @@ // Set attributes to default if they are unset // (There is no GLX_DONT_CARE equivalent on MacOS) - if (puglview->hints[PUGL_DEPTH_BITS] == PUGL_DONT_CARE) { - puglview->hints[PUGL_DEPTH_BITS] = 0; - } - if (puglview->hints[PUGL_STENCIL_BITS] == PUGL_DONT_CARE) { - puglview->hints[PUGL_STENCIL_BITS] = 0; - } - if (puglview->hints[PUGL_SAMPLES] == PUGL_DONT_CARE) { - puglview->hints[PUGL_SAMPLES] = 1; - } - if (puglview->hints[PUGL_SAMPLE_BUFFERS] == PUGL_DONT_CARE) { - puglview->hints[PUGL_SAMPLE_BUFFERS] = puglview->hints[PUGL_SAMPLES] > 0; - } - if (puglview->hints[PUGL_DOUBLE_BUFFER] == PUGL_DONT_CARE) { - puglview->hints[PUGL_DOUBLE_BUFFER] = 1; - } - if (puglview->hints[PUGL_SWAP_INTERVAL] == PUGL_DONT_CARE) { - puglview->hints[PUGL_SWAP_INTERVAL] = 1; - } + ensureHint(puglview, PUGL_DEPTH_BITS, 0); + ensureHint(puglview, PUGL_STENCIL_BITS, 0); + ensureHint(puglview, PUGL_SAMPLES, 1); + ensureHint(puglview, PUGL_SAMPLE_BUFFERS, puglview->hints[PUGL_SAMPLES] > 0); + ensureHint(puglview, PUGL_DOUBLE_BUFFER, 1); + ensureHint(puglview, PUGL_SWAP_INTERVAL, 1); const unsigned colorSize = (unsigned)(puglview->hints[PUGL_RED_BITS] + puglview->hints[PUGL_BLUE_BITS] + diff --git a/src/win.c b/src/win.c index 80a5baf..5defb8d 100644 --- a/src/win.c +++ b/src/win.c @@ -220,14 +220,6 @@ puglPollWinEvents(PuglWorld* world, const double timeout) return PUGL_SUCCESS; } -static void -ensureHint(PuglView* const view, const PuglViewHint hint, const int value) -{ - if (view->hints[hint] == PUGL_DONT_CARE) { - view->hints[hint] = value; - } -} - PuglStatus puglRealize(PuglView* view) { @@ -245,10 +237,10 @@ puglRealize(PuglView* view) } // Set default depth hints if the user hasn't specified any - ensureHint(view, PUGL_RED_BITS, 8); - ensureHint(view, PUGL_GREEN_BITS, 8); - ensureHint(view, PUGL_BLUE_BITS, 8); - ensureHint(view, PUGL_ALPHA_BITS, 8); + puglEnsureHint(view, PUGL_RED_BITS, 8); + puglEnsureHint(view, PUGL_GREEN_BITS, 8); + puglEnsureHint(view, PUGL_BLUE_BITS, 8); + puglEnsureHint(view, PUGL_ALPHA_BITS, 8); // Get refresh rate for resize draw timer DEVMODEA devMode; diff --git a/src/win_gl.c b/src/win_gl.c index 9850b6f..532fd2b 100644 --- a/src/win_gl.c +++ b/src/win_gl.c @@ -89,6 +89,14 @@ puglWinGlGetProcs(void) return procs; } +static void +ensureHint(PuglView* const view, const PuglViewHint hint, const int value) +{ + if (view->hints[hint] == PUGL_DONT_CARE) { + view->hints[hint] = value; + } +} + static PuglStatus puglWinGlConfigure(PuglView* view) { @@ -96,24 +104,12 @@ puglWinGlConfigure(PuglView* view) // Set attributes to default if they are unset // (There is no GLX_DONT_CARE equivalent on Windows) - if (view->hints[PUGL_DEPTH_BITS] == PUGL_DONT_CARE) { - view->hints[PUGL_DEPTH_BITS] = 0; - } - if (view->hints[PUGL_STENCIL_BITS] == PUGL_DONT_CARE) { - view->hints[PUGL_STENCIL_BITS] = 0; - } - if (view->hints[PUGL_SAMPLES] == PUGL_DONT_CARE) { - view->hints[PUGL_SAMPLES] = 0; - } - if (view->hints[PUGL_SAMPLE_BUFFERS] == PUGL_DONT_CARE) { - view->hints[PUGL_SAMPLE_BUFFERS] = view->hints[PUGL_SAMPLES] > 0; - } - if (view->hints[PUGL_DOUBLE_BUFFER] == PUGL_DONT_CARE) { - view->hints[PUGL_DOUBLE_BUFFER] = 1; - } - if (view->hints[PUGL_SWAP_INTERVAL] == PUGL_DONT_CARE) { - view->hints[PUGL_SWAP_INTERVAL] = 1; - } + ensureHint(view, PUGL_DEPTH_BITS, 0); + ensureHint(view, PUGL_STENCIL_BITS, 0); + ensureHint(view, PUGL_SAMPLES, 0); + ensureHint(view, PUGL_SAMPLE_BUFFERS, view->hints[PUGL_SAMPLES] > 0); + ensureHint(view, PUGL_DOUBLE_BUFFER, 1); + ensureHint(view, PUGL_SWAP_INTERVAL, 1); // clang-format off const int pixelAttrs[] = { -- cgit v1.2.1