diff options
Diffstat (limited to 'examples')
26 files changed, 190 insertions, 163 deletions
diff --git a/examples/.clang-tidy b/examples/.clang-tidy index c9cacb3..bc26cde 100644 --- a/examples/.clang-tidy +++ b/examples/.clang-tidy @@ -6,6 +6,7 @@ Checks: > -*-non-private-member-variables-in-classes, -*avoid-c-arrays, -android-cloexec-fopen, + -boost-*, -bugprone-easily-swappable-parameters, -bugprone-macro-parentheses, -bugprone-multi-level-implicit-pointer-conversion, diff --git a/examples/cube_view.h b/examples/cube_view.h index 6941870..a02154f 100644 --- a/examples/cube_view.h +++ b/examples/cube_view.h @@ -8,8 +8,8 @@ #include "demo_utils.h" -#include "pugl/gl.h" -#include "pugl/pugl.h" +#include <pugl/gl.h> +#include <pugl/pugl.h> #include <stdbool.h> diff --git a/examples/demo_utils.h b/examples/demo_utils.h index da3b2cd..db66b21 100644 --- a/examples/demo_utils.h +++ b/examples/demo_utils.h @@ -4,7 +4,7 @@ #ifndef EXAMPLES_DEMO_UTILS_H #define EXAMPLES_DEMO_UTILS_H -#include "pugl/pugl.h" +#include <pugl/pugl.h> #include <math.h> #include <stdio.h> diff --git a/examples/glad/.clang-format b/examples/glad/.clang-format new file mode 100644 index 0000000..f219d75 --- /dev/null +++ b/examples/glad/.clang-format @@ -0,0 +1,6 @@ +# Copyright 2025 David Robillard <d@drobilla.net> +# SPDX-License-Identifier: 0BSD OR ISC + +--- +DisableFormat: true +... diff --git a/examples/meson.build b/examples/meson.build index 0c4f998..65daa31 100644 --- a/examples/meson.build +++ b/examples/meson.build @@ -1,4 +1,4 @@ -# Copyright 2021-2023 David Robillard <d@drobilla.net> +# Copyright 2021-2025 David Robillard <d@drobilla.net> # SPDX-License-Identifier: 0BSD OR ISC data_dir = get_option('prefix') / get_option('datadir') / 'pugl-0' @@ -27,12 +27,6 @@ vulkan_examples = [ 'pugl_vulkan_demo.c', ] -includes = include_directories( - '..', - '../bindings/cpp/include', - '../include', -) - # Suppress some additional C warnings in examples example_c_args = [] if get_option('warning_level') == 'everything' @@ -120,7 +114,7 @@ else foreach example : stub_examples source = [example] target = example.split('.')[0] - dependencies = [pugl_dep, pugl_stub_dep] + dependencies = [pugl_dep, pugl_stub_dep, puglutil_dep] defines = [] executable( @@ -129,7 +123,7 @@ else c_args: example_defines + example_c_args + defines, cpp_args: example_defines + example_cpp_args + defines, dependencies: dependencies, - include_directories: includes, + implicit_include_directories: false, ) endforeach @@ -138,7 +132,7 @@ else foreach example : gl_examples source = [example] target = example.split('.')[0] - dependencies = [pugl_dep, pugl_gl_dep] + dependencies = [pugl_dep, pugl_gl_dep, puglutil_dep] defines = [] if target == 'pugl_shader_demo' @@ -148,6 +142,10 @@ else elif target == 'pugl_print_events' dependencies += [pugl_stub_dep] elif target == 'pugl_cpp_demo' + if not is_variable('puglpp_dep') + continue + endif + dependencies += [puglpp_dep] endif @@ -157,7 +155,7 @@ else c_args: example_defines + example_c_args + defines, cpp_args: example_defines + example_cpp_args + defines, dependencies: dependencies, - include_directories: includes, + implicit_include_directories: false, ) endforeach endif @@ -170,8 +168,8 @@ else target, example, c_args: example_defines + example_c_args + cairo_args, - dependencies: [pugl_dep, pugl_cairo_dep], - include_directories: includes, + dependencies: [pugl_dep, pugl_cairo_dep, puglutil_dep], + implicit_include_directories: false, ) endforeach endif @@ -181,12 +179,17 @@ else foreach example : vulkan_examples source = [example] target = example.split('.')[0] - dependencies = [dl_dep, pugl_vulkan_dep] + dependencies = [dl_dep, pugl_vulkan_dep, puglutil_dep] defines = [] if target == 'pugl_vulkan_cpp_demo' + if not is_variable('puglpp_dep') + continue + endif + source += ['file_utils.c'] defines += ['-D_POSIX_C_SOURCE=200809L'] + dependencies += [puglpp_dep] endif executable( @@ -195,7 +198,7 @@ else c_args: example_defines + example_c_args + defines, cpp_args: example_defines + example_cpp_args + defines, dependencies: dependencies, - include_directories: includes, + implicit_include_directories: false, ) endforeach endif diff --git a/examples/pugl_cairo_demo.app/MacOS/meson.build b/examples/pugl_cairo_demo.app/MacOS/meson.build index 18e1fcf..e8a5242 100644 --- a/examples/pugl_cairo_demo.app/MacOS/meson.build +++ b/examples/pugl_cairo_demo.app/MacOS/meson.build @@ -6,6 +6,6 @@ executable( ['../../pugl_cairo_demo.c'], c_args: example_defines + example_c_args + cairo_args, cpp_args: example_defines + example_cpp_args, - dependencies: [pugl_dep, pugl_cairo_dep], - include_directories: include_directories('../../..'), + dependencies: [pugl_dep, pugl_cairo_dep, puglutil_dep], + implicit_include_directories: false, ) diff --git a/examples/pugl_cairo_demo.c b/examples/pugl_cairo_demo.c index d973d69..109149d 100644 --- a/examples/pugl_cairo_demo.c +++ b/examples/pugl_cairo_demo.c @@ -2,10 +2,11 @@ // SPDX-License-Identifier: ISC #include "demo_utils.h" -#include "test/test_utils.h" -#include "pugl/cairo.h" -#include "pugl/pugl.h" +#include <puglutil/test_utils.h> + +#include <pugl/cairo.h> +#include <pugl/pugl.h> #include <cairo.h> @@ -49,9 +50,9 @@ static const Button buttons[] = {{128, 128, 64, 64, "1"}, static ViewScale getScale(const PuglView* const view) { - const PuglRect frame = puglGetFrame(view); - const ViewScale scale = {(frame.width - (512.0 / frame.width)) / 512.0, - (frame.height - (512.0 / frame.height)) / 512.0}; + const PuglArea size = puglGetSizeHint(view, PUGL_CURRENT_SIZE); + const ViewScale scale = {(size.width - (512.0 / size.width)) / 512.0, + (size.height - (512.0 / size.height)) / 512.0}; return scale; } @@ -100,8 +101,8 @@ buttonDraw(PuglTestApp* app, cairo_t* cr, const Button* but, const double time) cairo_set_font_size(cr, 32.0); cairo_text_extents(cr, but->label, &extents); cairo_move_to(cr, - (but->w / 2.0) - extents.width / 2, - (but->h / 2.0) + extents.height / 2); + (but->w / 2.0) - (extents.width / 2), + (but->h / 2.0) + (extents.height / 2)); cairo_set_source_rgba(cr, 0, 0, 0, 1); cairo_show_text(cr, but->label); @@ -114,7 +115,7 @@ postButtonRedisplay(PuglView* view) const ViewScale scale = getScale(view); for (const Button* b = buttons; b->label; ++b) { - const double span = sqrt(b->w * b->w + b->h * b->h); + const double span = sqrt((b->w * b->w) + (b->h * b->h)); puglObscureRegion(view, (int)((b->x - span) * scale.x), (int)((b->y - span) * scale.y), diff --git a/examples/pugl_clipboard_demo.app/MacOS/meson.build b/examples/pugl_clipboard_demo.app/MacOS/meson.build index 31ad2f0..f56ae59 100644 --- a/examples/pugl_clipboard_demo.app/MacOS/meson.build +++ b/examples/pugl_clipboard_demo.app/MacOS/meson.build @@ -5,6 +5,6 @@ executable( 'pugl_clipboard_demo', '../../pugl_clipboard_demo.c', c_args: example_defines + example_c_args, - dependencies: [pugl_dep, pugl_gl_dep], - include_directories: include_directories('../../..'), + dependencies: [pugl_dep, pugl_gl_dep, puglutil_dep], + implicit_include_directories: false, ) diff --git a/examples/pugl_clipboard_demo.c b/examples/pugl_clipboard_demo.c index 78ea4e8..04929ab 100644 --- a/examples/pugl_clipboard_demo.c +++ b/examples/pugl_clipboard_demo.c @@ -4,10 +4,11 @@ // A demonstration of using clipboards for copy/paste and drag and drop #include "cube_view.h" -#include "test/test_utils.h" -#include "pugl/gl.h" -#include "pugl/pugl.h" +#include <puglutil/test_utils.h> + +#include <pugl/gl.h> +#include <pugl/pugl.h> #include <math.h> #include <stdbool.h> @@ -44,8 +45,8 @@ onDisplay(PuglView* view) if (app->continuous) { const double dTime = thisTime - cube->lastDrawTime; - cube->xAngle = fmod(cube->xAngle + dTime * 100.0, 360.0); - cube->yAngle = fmod(cube->yAngle + dTime * 100.0, 360.0); + cube->xAngle = fmod(cube->xAngle + (dTime * 100.0), 360.0); + cube->yAngle = fmod(cube->yAngle + (dTime * 100.0), 360.0); } displayCube( diff --git a/examples/pugl_cpp_demo.app/MacOS/meson.build b/examples/pugl_cpp_demo.app/MacOS/meson.build index f5ddd67..891dcd9 100644 --- a/examples/pugl_cpp_demo.app/MacOS/meson.build +++ b/examples/pugl_cpp_demo.app/MacOS/meson.build @@ -6,6 +6,6 @@ executable( '../../pugl_cpp_demo.cpp', c_args: example_defines + example_c_args, cpp_args: example_defines + example_cpp_args, - dependencies: [puglpp_dep, pugl_gl_dep], - include_directories: include_directories('../../..'), + dependencies: [puglpp_dep, pugl_gl_dep, puglutil_dep], + implicit_include_directories: false, ) diff --git a/examples/pugl_cpp_demo.cpp b/examples/pugl_cpp_demo.cpp index 6b52d18..9ba5723 100644 --- a/examples/pugl_cpp_demo.cpp +++ b/examples/pugl_cpp_demo.cpp @@ -3,11 +3,12 @@ #include "cube_view.h" #include "demo_utils.h" -#include "test/test_utils.h" -#include "pugl/gl.hpp" -#include "pugl/pugl.h" -#include "pugl/pugl.hpp" +#include <puglutil/test_utils.h> + +#include <pugl/gl.hpp> +#include <pugl/pugl.h> +#include <pugl/pugl.hpp> #include <cmath> @@ -57,8 +58,9 @@ CubeView::onEvent(const pugl::UpdateEvent&) noexcept // return obscure(); // But for testing, use sendEvent() instead: + const auto currentSize = this->size(pugl::SizeHint::currentSize); return sendEvent(pugl::ExposeEvent{ - 0U, PuglCoord{0}, PuglCoord{0}, frame().width, frame().height}); + 0U, PuglCoord{0}, PuglCoord{0}, currentSize.width, currentSize.height}); } pugl::Status diff --git a/examples/pugl_cursor_demo.app/MacOS/meson.build b/examples/pugl_cursor_demo.app/MacOS/meson.build index 94359cd..0836875 100644 --- a/examples/pugl_cursor_demo.app/MacOS/meson.build +++ b/examples/pugl_cursor_demo.app/MacOS/meson.build @@ -6,6 +6,6 @@ executable( '../../pugl_cursor_demo.c', c_args: example_defines + example_c_args, cpp_args: example_defines + example_cpp_args, - dependencies: [pugl_dep, pugl_gl_dep], - include_directories: include_directories('../../..'), + dependencies: [pugl_dep, pugl_gl_dep, puglutil_dep], + implicit_include_directories: false, ) diff --git a/examples/pugl_cursor_demo.c b/examples/pugl_cursor_demo.c index bc219f0..6ddd5ad 100644 --- a/examples/pugl_cursor_demo.c +++ b/examples/pugl_cursor_demo.c @@ -1,10 +1,10 @@ // Copyright 2012-2020 David Robillard <d@drobilla.net> // SPDX-License-Identifier: ISC -#include "test/test_utils.h" +#include <puglutil/test_utils.h> -#include "pugl/gl.h" -#include "pugl/pugl.h" +#include <pugl/gl.h> +#include <pugl/pugl.h> #include <stdbool.h> @@ -40,7 +40,7 @@ onExpose(void) glColor3f(0.6f, 0.6f, 0.6f); for (int row = 1; row < N_ROWS; ++row) { - const float y = (float)row * (2.0f / (float)N_ROWS) - 1.0f; + const float y = ((float)row * (2.0f / (float)N_ROWS)) - 1.0f; glBegin(GL_LINES); glVertex2f(-1.0f, y); glVertex2f(1.0f, y); @@ -48,7 +48,7 @@ onExpose(void) } for (int col = 1; col < N_COLS; ++col) { - const float x = (float)col * (2.0f / (float)N_COLS) - 1.0f; + const float x = ((float)col * (2.0f / (float)N_COLS)) - 1.0f; glBegin(GL_LINES); glVertex2f(x, -1.0f); glVertex2f(x, 1.0f); @@ -59,9 +59,9 @@ onExpose(void) static void onMotion(PuglView* view, double x, double y) { - const PuglRect frame = puglGetFrame(view); - int row = (int)(y * N_ROWS / frame.height); - int col = (int)(x * N_COLS / frame.width); + const PuglArea size = puglGetSizeHint(view, PUGL_CURRENT_SIZE); + int row = (int)(y * N_ROWS / size.height); + int col = (int)(x * N_COLS / size.width); row = (row < 0) ? 0 : (row >= N_ROWS) ? (N_ROWS - 1) : row; col = (col < 0) ? 0 : (col >= N_COLS) ? (N_COLS - 1) : col; diff --git a/examples/pugl_embed_demo.app/MacOS/meson.build b/examples/pugl_embed_demo.app/MacOS/meson.build index 9e6dd58..34d4ccb 100644 --- a/examples/pugl_embed_demo.app/MacOS/meson.build +++ b/examples/pugl_embed_demo.app/MacOS/meson.build @@ -6,6 +6,6 @@ executable( '../../pugl_embed_demo.c', c_args: example_defines + example_c_args, cpp_args: example_defines + example_cpp_args, - dependencies: [pugl_dep, pugl_gl_dep], - include_directories: include_directories('../../..'), + dependencies: [pugl_dep, pugl_gl_dep, puglutil_dep], + implicit_include_directories: false, ) diff --git a/examples/pugl_embed_demo.c b/examples/pugl_embed_demo.c index c14afde..70e00c2 100644 --- a/examples/pugl_embed_demo.c +++ b/examples/pugl_embed_demo.c @@ -3,10 +3,11 @@ #include "cube_view.h" #include "demo_utils.h" -#include "test/test_utils.h" -#include "pugl/gl.h" -#include "pugl/pugl.h" +#include <puglutil/test_utils.h> + +#include <pugl/gl.h> +#include <pugl/pugl.h> #include <math.h> #include <stdbool.h> @@ -50,18 +51,6 @@ static const float backgroundColorVertices[] = { // clang-format on -static PuglRect -getChildFrame(const PuglRect parentFrame) -{ - const PuglRect childFrame = { - borderWidth, - borderWidth, - (PuglSpan)(parentFrame.width - 2 * borderWidth), - (PuglSpan)(parentFrame.height - 2 * borderWidth)}; - - return childFrame; -} - static void onDisplay(PuglView* view) { @@ -72,8 +61,8 @@ onDisplay(PuglView* view) const double dTime = (thisTime - app->lastDrawTime) * (app->reversing ? -1.0 : 1.0); - app->xAngle = fmod(app->xAngle + dTime * 100.0, 360.0); - app->yAngle = fmod(app->yAngle + dTime * 100.0, 360.0); + app->xAngle = fmod(app->xAngle + (dTime * 100.0), 360.0); + app->yAngle = fmod(app->yAngle + (dTime * 100.0), 360.0); } displayCube( @@ -100,32 +89,33 @@ swapFocus(PuglTestApp* app) static void onKeyPress(PuglView* view, const PuglKeyEvent* event) { - PuglTestApp* app = (PuglTestApp*)puglGetHandle(view); - PuglRect frame = puglGetFrame(view); + PuglTestApp* app = (PuglTestApp*)puglGetHandle(view); if (event->key == '\t') { swapFocus(app); } else if (event->key == 'q' || event->key == PUGL_KEY_ESCAPE) { app->quit = 1; } else if (event->state & PUGL_MOD_SHIFT) { + const PuglArea size = puglGetSizeHint(view, PUGL_CURRENT_SIZE); if (event->key == PUGL_KEY_UP) { - puglSetSize(view, frame.width, frame.height - 10U); + puglSetSizeHint(view, PUGL_CURRENT_SIZE, size.width, size.height - 10U); } else if (event->key == PUGL_KEY_DOWN) { - puglSetSize(view, frame.width, frame.height + 10U); + puglSetSizeHint(view, PUGL_CURRENT_SIZE, size.width, size.height + 10U); } else if (event->key == PUGL_KEY_LEFT) { - puglSetSize(view, frame.width - 10U, frame.height); + puglSetSizeHint(view, PUGL_CURRENT_SIZE, size.width - 10U, size.height); } else if (event->key == PUGL_KEY_RIGHT) { - puglSetSize(view, frame.width + 10U, frame.height); + puglSetSizeHint(view, PUGL_CURRENT_SIZE, size.width + 10U, size.height); } } else { + const PuglPoint pos = puglGetPositionHint(view, PUGL_CURRENT_POSITION); if (event->key == PUGL_KEY_UP) { - puglSetPosition(view, frame.x, frame.y - 10); + puglSetPositionHint(view, PUGL_CURRENT_POSITION, pos.x, pos.y - 10); } else if (event->key == PUGL_KEY_DOWN) { - puglSetPosition(view, frame.x, frame.y + 10); + puglSetPositionHint(view, PUGL_CURRENT_POSITION, pos.x, pos.y + 10); } else if (event->key == PUGL_KEY_LEFT) { - puglSetPosition(view, frame.x - 10, frame.y); + puglSetPositionHint(view, PUGL_CURRENT_POSITION, pos.x - 10, pos.y); } else if (event->key == PUGL_KEY_RIGHT) { - puglSetPosition(view, frame.x + 10, frame.y); + puglSetPositionHint(view, PUGL_CURRENT_POSITION, pos.x + 10, pos.y); } } } @@ -133,16 +123,17 @@ onKeyPress(PuglView* view, const PuglKeyEvent* event) static PuglStatus onParentEvent(PuglView* view, const PuglEvent* event) { - PuglTestApp* app = (PuglTestApp*)puglGetHandle(view); - const PuglRect parentFrame = puglGetFrame(view); + PuglTestApp* const app = (PuglTestApp*)puglGetHandle(view); printEvent(event, "Parent: ", app->verbose); switch (event->type) { case PUGL_CONFIGURE: reshapeCube((float)event->configure.width, (float)event->configure.height); - - puglSetFrame(app->child, getChildFrame(parentFrame)); + puglSetSizeHint(app->child, + PUGL_CURRENT_SIZE, + event->configure.width - (2U * borderWidth), + event->configure.height - (2U * borderWidth)); break; case PUGL_UPDATE: if (app->continuous) { @@ -261,7 +252,6 @@ main(int argc, char** argv) puglSetWorldString(app.world, PUGL_CLASS_NAME, "PuglEmbedDemo"); - const PuglRect parentFrame = {0, 0, 512, 512}; puglSetSizeHint(app.parent, PUGL_DEFAULT_SIZE, 512, 512); puglSetSizeHint(app.parent, PUGL_MIN_SIZE, 192, 192); puglSetSizeHint(app.parent, PUGL_MAX_SIZE, 1024, 1024); @@ -288,8 +278,13 @@ main(int argc, char** argv) return logError("Failed to create parent window (%s)\n", puglStrerror(st)); } - puglSetFrame(app.child, getChildFrame(parentFrame)); puglSetParent(app.child, puglGetNativeView(app.parent)); + puglSetPositionHint( + app.child, PUGL_DEFAULT_POSITION, borderWidth, borderWidth); + puglSetSizeHint(app.child, + PUGL_DEFAULT_SIZE, + 512U - (2U * borderWidth), + 512U - (2U * borderWidth)); puglSetViewHint(app.child, PUGL_CONTEXT_DEBUG, opts.errorChecking); puglSetViewHint(app.child, PUGL_SAMPLES, opts.samples); diff --git a/examples/pugl_management_demo.app/MacOS/meson.build b/examples/pugl_management_demo.app/MacOS/meson.build index 69dafc7..308a88c 100644 --- a/examples/pugl_management_demo.app/MacOS/meson.build +++ b/examples/pugl_management_demo.app/MacOS/meson.build @@ -6,6 +6,6 @@ executable( ['../../pugl_management_demo.c'], c_args: example_defines + example_c_args + cairo_args, cpp_args: example_defines + example_cpp_args, - dependencies: [pugl_dep, pugl_cairo_dep], - include_directories: include_directories('../../..'), + dependencies: [pugl_dep, pugl_cairo_dep, puglutil_dep], + implicit_include_directories: false, ) diff --git a/examples/pugl_management_demo.c b/examples/pugl_management_demo.c index 51d2f43..c9b3f4a 100644 --- a/examples/pugl_management_demo.c +++ b/examples/pugl_management_demo.c @@ -1,14 +1,12 @@ // Copyright 2012-2023 David Robillard <d@drobilla.net> // SPDX-License-Identifier: ISC -/* - A demonstration of window types, states, and management. -*/ +// A demonstration of window types, states, and management -#include "test/test_utils.h" +#include <puglutil/test_utils.h> -#include "pugl/cairo.h" -#include "pugl/pugl.h" +#include <pugl/cairo.h> +#include <pugl/pugl.h> #include <cairo.h> @@ -37,12 +35,13 @@ onMainEvent(PuglView* view, const PuglEvent* event); static PuglStatus onExpose(PuglView* const view, const PuglExposeEvent* const event) { - PuglWorld* const world = puglGetWorld(view); - DemoApp* const app = (DemoApp*)puglGetWorldHandle(world); - const PuglRect frame = puglGetFrame(view); + PuglWorld* const world = puglGetWorld(view); + DemoApp* const app = (DemoApp*)puglGetWorldHandle(world); + const PuglPoint pos = puglGetPositionHint(view, PUGL_CURRENT_POSITION); + const PuglArea size = puglGetSizeHint(view, PUGL_CURRENT_SIZE); const PuglViewStyleFlags style = puglGetViewStyle(view); - const PuglCoord cx = (PuglCoord)(frame.width / 2U); - const PuglCoord cy = (PuglCoord)(frame.height / 2U); + const PuglCoord cx = (PuglCoord)(size.width / 2U); + const PuglCoord cy = (PuglCoord)(size.height / 2U); cairo_t* const cr = (cairo_t*)puglGetContext(view); // Clip to expose region @@ -61,29 +60,31 @@ onExpose(PuglView* const view, const PuglExposeEvent* const event) cairo_set_source_rgb(cr, 1.0, 1.0, 1.0); // Draw position label - snprintf(buf, sizeof(buf), "Position: %5d, %5d", frame.x, frame.y); + snprintf(buf, sizeof(buf), "Position: %5d, %5d", pos.x, pos.y); cairo_text_extents(cr, buf, &extents); cairo_move_to( - cr, cx - extents.width / 2.0, cy + extents.height / 2.0 - 192.0); + cr, cx - (extents.width / 2.0), cy + (extents.height / 2.0) - 192.0); cairo_show_text(cr, buf); // Draw size label - snprintf(buf, sizeof(buf), "Size: %5u, %5u", frame.width, frame.height); + snprintf(buf, sizeof(buf), "Size: %5u, %5u", size.width, size.height); cairo_text_extents(cr, buf, &extents); cairo_move_to( - cr, cx - extents.width / 2.0, cy + extents.height / 2.0 - 144.0); + cr, cx - (extents.width / 2.0), cy + (extents.height / 2.0) - 144.0); cairo_show_text(cr, buf); // Draw scale label snprintf(buf, sizeof(buf), "Scale: %g", puglGetScaleFactor(view)); cairo_text_extents(cr, buf, &extents); - cairo_move_to(cr, cx - extents.width / 2.0, cy + extents.height / 2.0 - 96.0); + cairo_move_to( + cr, cx - (extents.width / 2.0), cy + (extents.height / 2.0) - 96.0); cairo_show_text(cr, buf); // Draw time label snprintf(buf, sizeof(buf), "Draw time: %g", puglGetTime(world)); cairo_text_extents(cr, buf, &extents); - cairo_move_to(cr, cx - extents.width / 2.0, cy + extents.height / 2.0 - 48.0); + cairo_move_to( + cr, cx - (extents.width / 2.0), cy + (extents.height / 2.0) - 48.0); cairo_show_text(cr, buf); // Draw style label @@ -100,7 +101,7 @@ onExpose(PuglView* const view, const PuglExposeEvent* const event) style & PUGL_VIEW_STYLE_DEMANDING ? " demanding" : "", style & PUGL_VIEW_STYLE_RESIZING ? " resizing" : ""); cairo_text_extents(cr, buf, &extents); - cairo_move_to(cr, cx - extents.width / 2.0, cy + extents.height / 2.0); + cairo_move_to(cr, cx - (extents.width / 2.0), cy + (extents.height / 2.0)); cairo_show_text(cr, buf); if (view == app->mainView.view) { @@ -108,7 +109,7 @@ onExpose(PuglView* const view, const PuglExposeEvent* const event) snprintf(buf, sizeof(buf), "Keys: Space T W H M F A B D Q"); cairo_text_extents(cr, buf, &extents); cairo_move_to( - cr, cx - extents.width / 2.0, cy + extents.height / 2.0 + 48.0); + cr, cx - (extents.width / 2.0), cy + (extents.height / 2.0) + 48.0); cairo_show_text(cr, buf); } diff --git a/examples/pugl_print_events.c b/examples/pugl_print_events.c index 96a8889..9445e50 100644 --- a/examples/pugl_print_events.c +++ b/examples/pugl_print_events.c @@ -1,10 +1,10 @@ // Copyright 2012-2020 David Robillard <d@drobilla.net> // SPDX-License-Identifier: ISC -#include "test/test_utils.h" +#include <puglutil/test_utils.h> -#include "pugl/pugl.h" -#include "pugl/stub.h" +#include <pugl/pugl.h> +#include <pugl/stub.h> #include <stdbool.h> #include <stdio.h> diff --git a/examples/pugl_shader_demo.app/MacOS/meson.build b/examples/pugl_shader_demo.app/MacOS/meson.build index d3d0755..afcd532 100644 --- a/examples/pugl_shader_demo.app/MacOS/meson.build +++ b/examples/pugl_shader_demo.app/MacOS/meson.build @@ -10,6 +10,6 @@ executable( ], c_args: example_defines + example_c_args, cpp_args: example_defines + example_cpp_args, - dependencies: [pugl_dep, pugl_gl_dep, dl_dep], - include_directories: include_directories('../../..'), + dependencies: [pugl_dep, pugl_gl_dep, dl_dep, puglutil_dep], + implicit_include_directories: false, ) diff --git a/examples/pugl_shader_demo.c b/examples/pugl_shader_demo.c index 2b122a9..5bef21e 100644 --- a/examples/pugl_shader_demo.c +++ b/examples/pugl_shader_demo.c @@ -21,16 +21,19 @@ about 100000 rectangles. */ +#define PUGL_NO_INCLUDE_GL_H + #include "demo_utils.h" #include "file_utils.h" #include "rects.h" #include "shader_utils.h" -#include "test/test_utils.h" #include "glad/glad.h" -#include "pugl/gl.h" -#include "pugl/pugl.h" +#include <puglutil/test_utils.h> + +#include <pugl/gl.h> +#include <pugl/pugl.h> #include <math.h> #include <stddef.h> @@ -90,15 +93,15 @@ static void onExpose(PuglView* view) { PuglTestApp* app = (PuglTestApp*)puglGetHandle(view); - const PuglRect frame = puglGetFrame(view); - const float width = (float)frame.width; - const float height = (float)frame.height; + const PuglArea size = puglGetSizeHint(view, PUGL_CURRENT_SIZE); + const float width = (float)size.width; + const float height = (float)size.height; const double time = puglGetTime(puglGetWorld(view)); // Construct projection matrix for 2D window surface (in pixels) mat4 proj; mat4Ortho( - proj, 0.0f, (float)frame.width, 0.0f, (float)frame.height, -1.0f, 1.0f); + proj, 0.0f, (float)size.width, 0.0f, (float)size.height, -1.0f, 1.0f); // Clear and bind everything that is the same for every rect glClear(GL_COLOR_BUFFER_BIT); @@ -108,7 +111,7 @@ onExpose(PuglView* view) // Update horizontal mouse cursor line (last rect) Rect* const mouseH = &app->rects[app->numRects]; mouseH->pos[0] = (float)(app->mouseX - 8.0); - mouseH->pos[1] = (float)(frame.height - app->mouseY - 1.0); + mouseH->pos[1] = (float)(size.height - app->mouseY - 1.0); mouseH->size[0] = 16.0f; mouseH->size[1] = 2.0f; mouseH->fillColor[0] = 1.0f; @@ -119,7 +122,7 @@ onExpose(PuglView* view) // Update vertical mouse cursor line (second last rect) Rect* const mouseV = &app->rects[app->numRects + 1]; mouseV->pos[0] = (float)(app->mouseX - 2.0); - mouseV->pos[1] = (float)(frame.height - app->mouseY - 8.0); + mouseV->pos[1] = (float)(size.height - app->mouseY - 8.0); mouseV->size[0] = 2.0f; mouseV->size[1] = 16.0f; mouseV->fillColor[0] = 1.0f; @@ -230,10 +233,15 @@ loadShader(const char* const programPath, const char* const name) free(path); fseek(file, 0, SEEK_END); - const size_t fileSize = (size_t)ftell(file); + const long filePos = ftell(file); + if (filePos <= 0) { + fclose(file); + return NULL; + } fseek(file, 0, SEEK_SET); - char* source = (char*)calloc(1, fileSize + 1U); + const size_t fileSize = (size_t)filePos; + char* source = (char*)calloc(1, fileSize + 1U); if (fread(source, 1, fileSize, file) != fileSize) { free(source); @@ -351,17 +359,16 @@ setupGl(PuglTestApp* app) char* const fragmentSource = loadShader(app->programPath, SHADER_DIR "rect.frag"); - if (!vertexSource || !fragmentSource) { - logError("Failed to load shader sources\n"); - return PUGL_FAILURE; + // Compile rectangle shaders and program + if (headerSource && vertexSource && fragmentSource) { + app->drawRect = compileProgram(headerSource, vertexSource, fragmentSource); } - // Compile rectangle shaders and program - app->drawRect = compileProgram(headerSource, vertexSource, fragmentSource); free(fragmentSource); free(vertexSource); free(headerSource); if (!app->drawRect.program) { + logError("Failed to compile shader program\n"); return PUGL_FAILURE; } diff --git a/examples/pugl_vulkan_cpp_demo.app/MacOS/meson.build b/examples/pugl_vulkan_cpp_demo.app/MacOS/meson.build index d75c75d..7e7556b 100644 --- a/examples/pugl_vulkan_cpp_demo.app/MacOS/meson.build +++ b/examples/pugl_vulkan_cpp_demo.app/MacOS/meson.build @@ -9,6 +9,6 @@ executable( ], c_args: example_defines + example_c_args, cpp_args: example_defines + example_cpp_args, - dependencies: [puglpp_dep, pugl_vulkan_dep], - include_directories: include_directories('../../..'), + dependencies: [puglpp_dep, pugl_vulkan_dep, puglutil_dep], + implicit_include_directories: false, ) diff --git a/examples/pugl_vulkan_cpp_demo.cpp b/examples/pugl_vulkan_cpp_demo.cpp index a1d6954..4a73157 100644 --- a/examples/pugl_vulkan_cpp_demo.cpp +++ b/examples/pugl_vulkan_cpp_demo.cpp @@ -17,13 +17,14 @@ #include "demo_utils.h" #include "file_utils.h" #include "rects.h" -#include "test/test_utils.h" #include "sybok.hpp" -#include "pugl/pugl.h" -#include "pugl/pugl.hpp" -#include "pugl/vulkan.hpp" +#include <puglutil/test_utils.h> + +#include <pugl/pugl.h> +#include <pugl/pugl.hpp> +#include <pugl/vulkan.hpp> #include <vulkan/vk_platform.h> @@ -559,7 +560,7 @@ Swapchain::init(const sk::VulkanApi& vk, const auto minNumImages = (!capabilities.maxImageCount || capabilities.maxImageCount >= 3U) - ? 3U + ? std::max(capabilities.minImageCount, 3U) : capabilities.maxImageCount; const VkSwapchainCreateInfoKHR swapchainCreateInfo{ @@ -576,7 +577,7 @@ Swapchain::init(const sk::VulkanApi& vk, VK_SHARING_MODE_EXCLUSIVE, SK_COUNTED(0, nullptr), capabilities.currentTransform, - VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR, + VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR, mode == RenderMode::resizing ? gpu.resizePresentMode : gpu.presentMode, VK_TRUE, oldSwapchain}; @@ -715,7 +716,12 @@ readFile(const char* const programPath, const std::string& filename) } fseek(file.get(), 0, SEEK_END); - const auto fileSize = static_cast<size_t>(ftell(file.get())); + const auto filePos = ftell(file.get()); + if (filePos <= 0) { + return {}; + } + + const auto fileSize = static_cast<size_t>(filePos); fseek(file.get(), 0, SEEK_SET); const auto numWords = fileSize / sizeof(uint32_t); diff --git a/examples/pugl_vulkan_demo.app/MacOS/meson.build b/examples/pugl_vulkan_demo.app/MacOS/meson.build index 0d07171..0f7f4bf 100644 --- a/examples/pugl_vulkan_demo.app/MacOS/meson.build +++ b/examples/pugl_vulkan_demo.app/MacOS/meson.build @@ -8,6 +8,6 @@ executable( '../../file_utils.c', ], c_args: example_defines + example_c_args, - dependencies: [pugl_dep, pugl_vulkan_dep], - include_directories: include_directories('../../..'), + dependencies: [pugl_dep, pugl_vulkan_dep, puglutil_dep], + implicit_include_directories: false, ) diff --git a/examples/pugl_vulkan_demo.c b/examples/pugl_vulkan_demo.c index 708a28f..75083c4 100644 --- a/examples/pugl_vulkan_demo.c +++ b/examples/pugl_vulkan_demo.c @@ -10,10 +10,11 @@ */ #include "demo_utils.h" -#include "test/test_utils.h" -#include "pugl/pugl.h" -#include "pugl/vulkan.h" +#include <puglutil/test_utils.h> + +#include <pugl/pugl.h> +#include <pugl/vulkan.h> #include <vulkan/vk_platform.h> #include <vulkan/vulkan_core.h> diff --git a/examples/pugl_window_demo.app/MacOS/meson.build b/examples/pugl_window_demo.app/MacOS/meson.build index 7bfc219..f2623f2 100644 --- a/examples/pugl_window_demo.app/MacOS/meson.build +++ b/examples/pugl_window_demo.app/MacOS/meson.build @@ -6,6 +6,6 @@ executable( '../../pugl_window_demo.c', c_args: example_defines + example_c_args, cpp_args: example_defines + example_cpp_args, - dependencies: [pugl_dep, pugl_gl_dep], - include_directories: include_directories('../../..'), + dependencies: [pugl_dep, pugl_gl_dep, puglutil_dep], + implicit_include_directories: false, ) diff --git a/examples/pugl_window_demo.c b/examples/pugl_window_demo.c index cd4fd67..867f856 100644 --- a/examples/pugl_window_demo.c +++ b/examples/pugl_window_demo.c @@ -7,10 +7,11 @@ #include "cube_view.h" #include "demo_utils.h" -#include "test/test_utils.h" -#include "pugl/gl.h" -#include "pugl/pugl.h" +#include <puglutil/test_utils.h> + +#include <pugl/gl.h> +#include <pugl/pugl.h> #include <math.h> #include <stdbool.h> @@ -49,8 +50,8 @@ onDisplay(PuglView* view) if (app->continuous) { const double dTime = thisTime - cube->lastDrawTime; - cube->xAngle = fmod(cube->xAngle + dTime * 100.0, 360.0); - cube->yAngle = fmod(cube->yAngle + dTime * 100.0, 360.0); + cube->xAngle = fmod(cube->xAngle + (dTime * 100.0), 360.0); + cube->yAngle = fmod(cube->yAngle + (dTime * 100.0), 360.0); } displayCube( @@ -64,29 +65,30 @@ onKeyPress(PuglView* view, const PuglKeyEvent* event) { PuglWorld* world = puglGetWorld(view); PuglTestApp* app = (PuglTestApp*)puglGetWorldHandle(world); - PuglRect frame = puglGetFrame(view); if (event->key == 'q' || event->key == PUGL_KEY_ESCAPE) { app->quit = 1; } else if (event->state & PUGL_MOD_SHIFT) { + const PuglArea size = puglGetSizeHint(view, PUGL_CURRENT_SIZE); if (event->key == PUGL_KEY_UP) { - puglSetSize(view, frame.width, frame.height - 10U); + puglSetSizeHint(view, PUGL_CURRENT_SIZE, size.width, size.height - 10U); } else if (event->key == PUGL_KEY_DOWN) { - puglSetSize(view, frame.width, frame.height + 10U); + puglSetSizeHint(view, PUGL_CURRENT_SIZE, size.width, size.height + 10U); } else if (event->key == PUGL_KEY_LEFT) { - puglSetSize(view, frame.width - 10U, frame.height); + puglSetSizeHint(view, PUGL_CURRENT_SIZE, size.width - 10U, size.height); } else if (event->key == PUGL_KEY_RIGHT) { - puglSetSize(view, frame.width + 10U, frame.height); + puglSetSizeHint(view, PUGL_CURRENT_SIZE, size.width + 10U, size.height); } } else { + const PuglPoint pos = puglGetPositionHint(view, PUGL_CURRENT_POSITION); if (event->key == PUGL_KEY_UP) { - puglSetPosition(view, frame.x, frame.y - 10); + puglSetPositionHint(view, PUGL_CURRENT_POSITION, pos.x, pos.y - 10); } else if (event->key == PUGL_KEY_DOWN) { - puglSetPosition(view, frame.x, frame.y + 10); + puglSetPositionHint(view, PUGL_CURRENT_POSITION, pos.x, pos.y + 10); } else if (event->key == PUGL_KEY_LEFT) { - puglSetPosition(view, frame.x - 10, frame.y); + puglSetPositionHint(view, PUGL_CURRENT_POSITION, pos.x - 10, pos.y); } else if (event->key == PUGL_KEY_RIGHT) { - puglSetPosition(view, frame.x + 10, frame.y); + puglSetPositionHint(view, PUGL_CURRENT_POSITION, pos.x + 10, pos.y); } } } @@ -186,9 +188,10 @@ main(int argc, char** argv) cube->dist = 10; puglSetViewString(view, PUGL_WINDOW_TITLE, "Pugl Window Demo"); - puglSetPosition(view, - (PuglCoord)(pad + (128U + pad) * i), - (PuglCoord)(pad + (128U + pad) * i)); + puglSetPositionHint(view, + PUGL_DEFAULT_POSITION, + (PuglCoord)(pad + ((128U + pad) * i)), + (PuglCoord)(pad + ((128U + pad) * i))); puglSetSizeHint(view, PUGL_DEFAULT_SIZE, 512, 512); puglSetSizeHint(view, PUGL_MIN_SIZE, 128, 128); |