Skip to content

Commit

Permalink
Hide glfw
Browse files Browse the repository at this point in the history
  • Loading branch information
kunitoki committed Apr 28, 2024
1 parent 4668e0a commit 083eec6
Show file tree
Hide file tree
Showing 23 changed files with 1,215 additions and 105 deletions.
21 changes: 20 additions & 1 deletion examples/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,23 @@ cmake_minimum_required (VERSION 3.28)

set (target_name example_app)

# ==== Find dependencies
include (FetchContent)

FetchContent_Declare(glfw GIT_REPOSITORY https://github.com/glfw/glfw.git GIT_TAG master)
set (GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set (GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set (GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
set (GLFW_BUILD_WAYLAND OFF CACHE BOOL "" FORCE)
FetchContent_MakeAvailable (glfw)

# ==== Prepare sources
file (GLOB_RECURSE sources
"${CMAKE_CURRENT_LIST_DIR}/source/*.cpp")

source_group (TREE ${CMAKE_CURRENT_LIST_DIR}/ FILES ${sources})

# ==== Setup executable
add_executable (${target_name})

target_compile_features (${target_name} PRIVATE cxx_std_17)
Expand Down Expand Up @@ -66,4 +78,11 @@ target_link_libraries (${target_name} PRIVATE
juce_core
juce_events
juce_audio_basics
juce_audio_devices)
juce_audio_devices
yup_graphics
yup_gui
harfbuzz
sheenbidi
rive
rive_pls_renderer
glfw)
1 change: 1 addition & 0 deletions examples/app/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

class Application : public juce::JUCEApplication, public juce::Timer
{
public:
Application() = default;

const juce::String getApplicationName() override
Expand Down
1 change: 0 additions & 1 deletion examples/graphics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ cmake_minimum_required(VERSION 3.28)
set (target_name example_graphics)

# ==== Find dependencies
# find_package (GLFW3)
include (FetchContent)

FetchContent_Declare(glfw GIT_REPOSITORY https://github.com/glfw/glfw.git GIT_TAG master)
Expand Down
58 changes: 28 additions & 30 deletions examples/graphics/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@

#include "rive/math/simd.hpp"

#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>

#include <memory>

enum class API
Expand Down Expand Up @@ -76,14 +73,16 @@ class CustomWindow : public juce::DocumentWindow, public juce::Timer
startTimerHz (static_cast<int> (framerate));
}

void mouseDown(int button, int mods, double x, double y) override
void mouseDown (const juce::MouseEvent& event) override
{
auto [x, y] = event.getPosition();

float dpiScale = fiddleContext->dpiScale (nativeHandle());
x *= dpiScale;
y *= dpiScale;

dragLastPos = rive::float2 { (float)x, (float)y };
if (button == GLFW_MOUSE_BUTTON_LEFT)
dragLastPos = rive::float2 { x, y };
if (event.isLeftButtoDown())
{
dragIdx = -1;
for (int i = 0; i < kNumInteractivePts; ++i)
Expand All @@ -97,49 +96,50 @@ class CustomWindow : public juce::DocumentWindow, public juce::Timer
}
}

void mouseUp (int button, int mods, double x, double y) override
void mouseUp (const juce::MouseEvent& event) override
{
}

void mouseMove (int button, int mods, double x, double y) override
void mouseMove (const juce::MouseEvent& event) override
{
}

void mouseDrag (int button, int mods, double x, double y) override
void mouseDrag (const juce::MouseEvent& event) override
{
auto [x, y] = event.getPosition();

float dpiScale = fiddleContext->dpiScale (nativeHandle());
x *= dpiScale;
y *= dpiScale;

if (button == GLFW_MOUSE_BUTTON_LEFT)
if (event.isLeftButtoDown())
{
rive::float2 pos = rive::float2 { (float)x, (float)y };
if (dragIdx >= 0)
pts[dragIdx] += (pos - dragLastPos);
else
translate += (pos - dragLastPos);

dragLastPos = pos;
}
}

void keyDown (int key, int scancode, int mods, double x, double y) override
void keyDown (const juce::KeyPress& keys, double x, double y) override
{
const bool shift = mods & GLFW_MOD_SHIFT;

switch (key)
switch (keys.getKey())
{
case GLFW_KEY_ESCAPE:
case juce::KeyPress::escapeKey:
close();
break;

case GLFW_KEY_A:
case juce::KeyPress::textAKey:
forceAtomicMode = !forceAtomicMode;
fpsLastTime = 0;
fpsFrames = 0;
needsTitleUpdate = true;
break;

case GLFW_KEY_D:
case juce::KeyPress::textDKey:
printf ("static float scale = %f;\n", scale);
printf ("static float2 translate = {%f, %f};\n", translate.x, translate.y);
printf ("static float2 pts[] = {");
Expand All @@ -154,39 +154,39 @@ class CustomWindow : public juce::DocumentWindow, public juce::Timer
fflush(stdout);
break;

case GLFW_KEY_1:
case juce::KeyPress::number1Key:
strokeWidth /= 1.5f;
break;

case GLFW_KEY_2:
case juce::KeyPress::number2Key:
strokeWidth *= 1.5f;
break;

case GLFW_KEY_W:
case juce::KeyPress::textWKey:
wireframe = !wireframe;
break;

case GLFW_KEY_C:
case juce::KeyPress::textCKey:
cap = static_cast<rive::StrokeCap> ((static_cast<int> (cap) + 1) % 3);
break;

case GLFW_KEY_O:
case juce::KeyPress::textOKey:
doClose = !doClose;
break;

case GLFW_KEY_S:
case juce::KeyPress::textSKey:
disableStroke = !disableStroke;
break;

case GLFW_KEY_F:
case juce::KeyPress::textFKey:
disableFill = !disableFill;
break;

case GLFW_KEY_P:
case juce::KeyPress::textPKey:
paused = !paused;
break;

case GLFW_KEY_UP:
case juce::KeyPress::upKey:
{
float oldScale = scale;
scale *= 1.25;
Expand All @@ -195,7 +195,7 @@ class CustomWindow : public juce::DocumentWindow, public juce::Timer
break;
}

case GLFW_KEY_DOWN:
case juce::KeyPress::downKey:
{
float oldScale = scale;
scale /= 1.25;
Expand Down Expand Up @@ -234,8 +234,6 @@ class CustomWindow : public juce::DocumentWindow, public juce::Timer
return;
}

glfwPollEvents(); // TODO - remove

mainLoop (juce::Time::getMillisecondCounterHiRes() / 1000.0);

fiddleContext->tick();
Expand Down Expand Up @@ -342,7 +340,7 @@ class CustomWindow : public juce::DocumentWindow, public juce::Timer
}
}

juce::LowLevelRenderContextOptions options;
juce::LowLevelRenderContext::Options options;
bool forceAtomicMode = false;
bool wireframe = false;
bool disableFill = false;
Expand Down
55 changes: 29 additions & 26 deletions examples/render/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
#include "rive/animation/state_machine_instance.hpp"
#include "rive/static_scene.hpp"

#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>

#include <memory>

enum class API
Expand Down Expand Up @@ -99,11 +96,13 @@ class CustomWindow : public juce::DocumentWindow, public juce::Timer
startTimerHz (static_cast<int> (framerate));
}

void mouseDown(int button, int mods, double x, double y) override
void mouseDown(const juce::MouseEvent& event) override
{
if (scenes.empty())
if (scenes.empty() || ! event.isLeftButtoDown())
return;

auto [x, y] = event.getPosition();

float dpiScale = fiddleContext->dpiScale (nativeHandle());
x *= dpiScale;
y *= dpiScale;
Expand All @@ -113,11 +112,13 @@ class CustomWindow : public juce::DocumentWindow, public juce::Timer
scene->pointerDown (xy);
}

void mouseUp (int button, int mods, double x, double y) override
void mouseUp (const juce::MouseEvent& event) override
{
if (scenes.empty())
if (scenes.empty() || ! event.isLeftButtoDown())
return;

auto [x, y] = event.getPosition();

float dpiScale = fiddleContext->dpiScale (nativeHandle());
x *= dpiScale;
y *= dpiScale;
Expand All @@ -127,11 +128,13 @@ class CustomWindow : public juce::DocumentWindow, public juce::Timer
scene->pointerUp (xy);
}

void mouseMove (int button, int mods, double x, double y) override
void mouseMove (const juce::MouseEvent& event) override
{
if (scenes.empty())
return;

auto [x, y] = event.getPosition();

float dpiScale = fiddleContext->dpiScale (nativeHandle());
x *= dpiScale;
y *= dpiScale;
Expand All @@ -141,11 +144,13 @@ class CustomWindow : public juce::DocumentWindow, public juce::Timer
scene->pointerMove (xy);
}

void mouseDrag (int button, int mods, double x, double y) override
void mouseDrag (const juce::MouseEvent& event) override
{
if (scenes.empty())
if (scenes.empty() || ! event.isLeftButtoDown())
return;

auto [x, y] = event.getPosition();

float dpiScale = fiddleContext->dpiScale (nativeHandle());
x *= dpiScale;
y *= dpiScale;
Expand All @@ -155,59 +160,59 @@ class CustomWindow : public juce::DocumentWindow, public juce::Timer
scene->pointerMove (xy);
}

void keyDown (int key, int scancode, int mods, double x, double y) override
void keyDown (const juce::KeyPress& keys, double x, double y) override
{
const bool shift = mods & GLFW_MOD_SHIFT;
const bool shift = keys.getModifiers().isShiftDown();

switch (key)
switch (keys.getKey())
{
case GLFW_KEY_ESCAPE:
case juce::KeyPress::escapeKey:
close();
break;

case GLFW_KEY_A:
case juce::KeyPress::textAKey:
forceAtomicMode = !forceAtomicMode;
fpsLastTime = 0;
fpsFrames = 0;
needsTitleUpdate = true;
break;

case GLFW_KEY_D:
case juce::KeyPress::textDKey:
printf ("static float scale = %f;\n", scale);
printf ("static float2 translate = {%f, %f};\n", translate.x, translate.y);
fflush(stdout);
break;

case GLFW_KEY_W:
case juce::KeyPress::textWKey:
wireframe = !wireframe;
break;

case GLFW_KEY_P:
case juce::KeyPress::textPKey:
paused = !paused;
break;

case GLFW_KEY_H:
case juce::KeyPress::textHKey:
if (!shift)
++horzRepeat;
else if (horzRepeat > 0)
--horzRepeat;
break;

case GLFW_KEY_K:
case juce::KeyPress::textKKey:
if (!shift)
++upRepeat;
else if (upRepeat > 0)
--upRepeat;
break;

case GLFW_KEY_J:
case juce::KeyPress::textJKey:
if (!shift)
++downRepeat;
else if (downRepeat > 0)
--downRepeat;
break;

case GLFW_KEY_UP:
case juce::KeyPress::upKey:
{
float oldScale = scale;
scale *= 1.25;
Expand All @@ -216,7 +221,7 @@ class CustomWindow : public juce::DocumentWindow, public juce::Timer
break;
}

case GLFW_KEY_DOWN:
case juce::KeyPress::downKey:
{
float oldScale = scale;
scale /= 1.25;
Expand Down Expand Up @@ -258,8 +263,6 @@ class CustomWindow : public juce::DocumentWindow, public juce::Timer
return;
}

glfwPollEvents(); // TODO - remove

mainLoop (juce::Time::getMillisecondCounterHiRes() / 1000.0);

fiddleContext->tick();
Expand Down Expand Up @@ -409,7 +412,7 @@ class CustomWindow : public juce::DocumentWindow, public juce::Timer
}
}

juce::LowLevelRenderContextOptions options;
juce::LowLevelRenderContext::Options options;
bool forceAtomicMode = false;
bool wireframe = false;
bool disableFill = false;
Expand Down
Loading

0 comments on commit 083eec6

Please sign in to comment.