diff options
author | David Robillard <d@drobilla.net> | 2023-05-12 17:08:59 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-05-12 17:08:59 -0400 |
commit | 5dc502bc458d05e2a5823675ef3469a4181cd7a4 (patch) | |
tree | 2d87385c3debed5ea74a512977c99dd871bd3160 /examples | |
parent | f3c8f336fa6a702dc680769d079aa5c4b4e2a645 (diff) | |
download | pugl-5dc502bc458d05e2a5823675ef3469a4181cd7a4.tar.gz pugl-5dc502bc458d05e2a5823675ef3469a4181cd7a4.tar.bz2 pugl-5dc502bc458d05e2a5823675ef3469a4181cd7a4.zip |
Add mouse cursors to examples for testing input latency
Diffstat (limited to 'examples')
-rw-r--r-- | examples/pugl_shader_demo.c | 47 | ||||
-rw-r--r-- | examples/pugl_vulkan_cpp_demo.cpp | 46 |
2 files changed, 82 insertions, 11 deletions
diff --git a/examples/pugl_shader_demo.c b/examples/pugl_shader_demo.c index 178278b..9ba3da0 100644 --- a/examples/pugl_shader_demo.c +++ b/examples/pugl_shader_demo.c @@ -1,4 +1,4 @@ -// Copyright 2012-2020 David Robillard <d@drobilla.net> +// Copyright 2012-2023 David Robillard <d@drobilla.net> // SPDX-License-Identifier: ISC /* @@ -62,6 +62,8 @@ typedef struct { GLuint ibo; double lastDrawDuration; double lastFrameEndTime; + double mouseX; + double mouseY; unsigned framesDrawn; int quit; } PuglTestApp; @@ -103,17 +105,44 @@ onExpose(PuglView* view) glUseProgram(app->drawRect.program); glBindVertexArray(app->vao); + // 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->size[0] = 16.0f; + mouseH->size[1] = 2.0f; + mouseH->fillColor[0] = 1.0f; + mouseH->fillColor[1] = 1.0f; + mouseH->fillColor[2] = 1.0f; + mouseH->fillColor[3] = 0.5f; + + // 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->size[0] = 2.0f; + mouseV->size[1] = 16.0f; + mouseV->fillColor[0] = 1.0f; + mouseV->fillColor[1] = 1.0f; + mouseV->fillColor[2] = 1.0f; + mouseV->fillColor[3] = 0.5f; + for (size_t i = 0; i < app->numRects; ++i) { moveRect(&app->rects[i], i, app->numRects, width, height, time); } glBufferData(GL_UNIFORM_BUFFER, sizeof(proj), &proj, GL_STREAM_DRAW); - glBufferSubData( - GL_ARRAY_BUFFER, 0, (GLsizeiptr)(app->numRects * sizeof(Rect)), app->rects); + glBufferSubData(GL_ARRAY_BUFFER, + 0, + (GLsizeiptr)((app->numRects + 2) * sizeof(Rect)), + app->rects); - glDrawElementsInstanced( - GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, NULL, (GLsizei)(app->numRects * 4)); + glDrawElementsInstanced(GL_TRIANGLE_STRIP, + 4, + GL_UNSIGNED_INT, + NULL, + (GLsizei)((app->numRects + 2) * 4)); ++app->framesDrawn; @@ -160,6 +189,10 @@ onEvent(PuglView* view, const PuglEvent* event) app->quit = 1; } break; + case PUGL_MOTION: + app->mouseX = event->motion.x; + app->mouseY = event->motion.y; + break; case PUGL_TIMER: if (event->timer.id == resizeTimerId) { puglPostRedisplay(view); @@ -242,7 +275,7 @@ setupPugl(PuglTestApp* app) // Create world, view, and rect data app->world = puglNewWorld(PUGL_PROGRAM, 0); app->view = puglNewView(app->world); - app->rects = makeRects(app->numRects); + app->rects = makeRects(app->numRects + 2); // Set up world and view puglSetWorldString(app->world, PUGL_CLASS_NAME, "PuglShaderDemo"); @@ -360,7 +393,7 @@ setupGl(PuglTestApp* app) glGenBuffers(1, &app->instanceVbo); glBindBuffer(GL_ARRAY_BUFFER, app->instanceVbo); glBufferData(GL_ARRAY_BUFFER, - (GLsizeiptr)(app->numRects * sizeof(Rect)), + (GLsizeiptr)((app->numRects + 2) * sizeof(Rect)), app->rects, GL_STREAM_DRAW); diff --git a/examples/pugl_vulkan_cpp_demo.cpp b/examples/pugl_vulkan_cpp_demo.cpp index eb41b09..2d9739a 100644 --- a/examples/pugl_vulkan_cpp_demo.cpp +++ b/examples/pugl_vulkan_cpp_demo.cpp @@ -1,4 +1,4 @@ -// Copyright 2019-2020 David Robillard <d@drobilla.net> +// Copyright 2019-2023 David Robillard <d@drobilla.net> // SPDX-License-Identifier: ISC /* @@ -1399,6 +1399,7 @@ public: pugl::Status onEvent(const pugl::LoopLeaveEvent& event); pugl::Status onEvent(const pugl::KeyPressEvent& event); pugl::Status onEvent(const pugl::CloseEvent& event); + pugl::Status onEvent(const pugl::MotionEvent& event); private: PuglVulkanDemo& _app; @@ -1424,6 +1425,8 @@ public: uint32_t framesDrawn{0}; VkExtent2D extent{512U, 512U}; std::vector<Rect> rects; + double mouseX{0.0}; + double mouseY{0.0}; bool resizing{false}; bool quit{false}; }; @@ -1447,7 +1450,7 @@ PuglVulkanDemo::PuglVulkanDemo(const char* const executablePath, , world{pugl::WorldType::program, pugl::WorldFlag::threads} , loader{world} , view{world, *this} - , rects{makeRects(numRects, extent.width)} + , rects{makeRects(numRects + 2U, extent.width)} {} VkResult @@ -1567,8 +1570,8 @@ beginFrame(PuglVulkanDemo& app, const sk::Device& device, uint32_t& imageIndex) void update(PuglVulkanDemo& app, const double time) { - // Animate rectangles - for (size_t i = 0; i < app.rects.size(); ++i) { + // Animate rectangles (except mouse cursor) + for (size_t i = 0; i < app.rects.size() - 2U; ++i) { moveRect(&app.rects[i], i, app.rects.size(), @@ -1577,6 +1580,31 @@ update(PuglVulkanDemo& app, const double time) time); } + const auto mouseX = static_cast<float>(app.mouseX); + const auto mouseY = static_cast<float>(app.mouseY); + + // Update horizontal mouse cursor line (last rect) + Rect* const mouseH = &app.rects[app.rects.size() - 1U]; + mouseH->pos[0] = mouseX - 8.0f; + mouseH->pos[1] = mouseY - 1.0f; + mouseH->size[0] = 16.0f; + mouseH->size[1] = 2.0f; + mouseH->fillColor[0] = 1.0f; + mouseH->fillColor[1] = 1.0f; + mouseH->fillColor[2] = 1.0f; + mouseH->fillColor[3] = 0.5f; + + // Update vertical mouse cursor line (second last rect) + Rect* const mouseV = &app.rects[app.rects.size() - 2U]; + mouseV->pos[0] = mouseX - 2.0f; + mouseV->pos[1] = mouseY - 8.0f; + mouseV->size[0] = 2.0f; + mouseV->size[1] = 16.0f; + mouseV->fillColor[0] = 1.0f; + mouseV->fillColor[1] = 1.0f; + mouseV->fillColor[2] = 1.0f; + mouseV->fillColor[3] = 0.5f; + // Update vertex buffer memcpy(app.rectData.vertexData.get(), app.rects.data(), @@ -1658,6 +1686,7 @@ View::onEvent(const pugl::ExposeEvent&) // Submit the frame to the queue and present it endFrame(vk, gpu, _app.renderer, nextImageIndex); + // Update frame counters ++_app.framesDrawn; ++_app.renderer.sync.currentFrame; _app.renderer.sync.currentFrame %= _app.renderer.sync.inFlight.size(); @@ -1702,6 +1731,15 @@ View::onEvent(const pugl::KeyPressEvent& event) } pugl::Status +View::onEvent(const pugl::MotionEvent& event) +{ + _app.mouseX = event.x; + _app.mouseY = event.y; + + return pugl::Status::success; +} + +pugl::Status View::onEvent(const pugl::CloseEvent&) { _app.quit = true; |