Skip to content

Commit

Permalink
Work on nanovg demos
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Jan 13, 2024
1 parent 24c3f53 commit eb29390
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct AppState
std::unique_ptr<MyNvgDemo> myNvgDemo;
NVGcontext * vg;

NvgImgui::NvgFramebufferPtr myFramebuffer;
std::unique_ptr<NvgImgui::NvgFramebuffer> myFramebuffer;

ImVec4 ClearColor = ImVec4(0.2f, 0.2f, 0.2f, 1.f);
bool DisplayInFrameBuffer = false;
Expand All @@ -62,14 +62,14 @@ int main(int, char**)
runnerParams.appWindowParams.windowGeometry.size = {1200, 900};
ImmApp::AddOnsParams addons;

runnerParams.callbacks.CallPostInit([&]()
runnerParams.callbacks.EnqueuePostInit([&]()
{
appState.vg = NvgImgui::CreateNvgContext(NvgImgui::NVG_ANTIALIAS | NvgImgui::NVG_STENCIL_STROKES | NvgImgui::NVG_DEBUG);
appState.myNvgDemo = std::make_unique<MyNvgDemo>(appState.vg);
int nvgImageFlags = 0; //NVG_IMAGE_FLIPY | NVG_IMAGE_PREMULTIPLIED;
appState.myFramebuffer = NvgImgui::CreateNvgFramebuffer(appState.vg, 1000, 600, nvgImageFlags);
appState.myFramebuffer = std::make_unique<NvgImgui::NvgFramebuffer>(appState.vg, 1000, 600, nvgImageFlags);
});
runnerParams.callbacks.CallBeforeExit([&]()
runnerParams.callbacks.EnqueueBeforeExit([&]()
{
appState.myNvgDemo.reset();
appState.myFramebuffer.reset();
Expand Down Expand Up @@ -98,7 +98,7 @@ int main(int, char**)

if (appState.DisplayInFrameBuffer)
{
NvgImgui::RenderNvgToFrameBuffer(appState.vg, appState.myFramebuffer, nvgDrawingFunction, appState.ClearColor);
NvgImgui::RenderNvgToFrameBuffer(appState.vg, *appState.myFramebuffer, nvgDrawingFunction, appState.ClearColor);
ImGui::Image(appState.myFramebuffer->TextureId, ImVec2(1000, 600));
}

Expand Down
138 changes: 27 additions & 111 deletions bindings/imgui_bundle/demos_cpp/demos_nanovg/demo_nanovg_heart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
#include "imgui.h"
#include "nanovg.h"
#include "nvg_imgui/nvg_imgui.h"

#include "nanovg_demo/nanovg_demo.h"
#include "hello_imgui/internal/functional_utils.h"
#include <math.h>

// On peut modifier CustomBackground durant l'execution

Expand Down Expand Up @@ -95,18 +93,19 @@ void DrawTextWithGradient(NVGcontext* vg, ImVec2 position, int fontId)
//nvgStroke(vg); // Apply the stroke
}

using NvgFramebufferPtr = std::unique_ptr<NvgImgui::NvgFramebuffer>;

void demo_nanovg_heart()
struct AppStateNvgHeart
{
static NvgImgui::NvgFramebufferPtr nvgFramebuffer;
static NVGcontext *vg = nullptr;

NVGcontext *vg = nullptr;
std::unique_ptr<NvgImgui::NvgFramebuffer> nvgFramebuffer;
int fontId = 0;
if (vg == nullptr)

void Init()
{
vg = NvgImgui::CreateNvgContext(NvgImgui::NVG_ANTIALIAS | NvgImgui::NVG_STENCIL_STROKES);
int nvgImageFlags = 0; //NVG_IMAGE_FLIPY | NVG_IMAGE_PREMULTIPLIED;
nvgFramebuffer = NvgImgui::CreateNvgFramebuffer(vg, 1000, 600, nvgImageFlags);
nvgFramebuffer = std::make_unique<NvgImgui::NvgFramebuffer>(vg, 1000, 600, nvgImageFlags);

// Load the font. You should do this only once and store the font handle if you're calling this multiple times.
auto fontPath = HelloImGui::AssetFileFullPath("fonts/Roboto/Roboto-Regular.ttf");
Expand All @@ -116,115 +115,32 @@ void demo_nanovg_heart()
return; // Exit if the font cannot be added.
}
}
HelloImGui::GetRunnerParams()->callbacks.CallBeforeExit([]() {

void Release()
{
nvgFramebuffer.reset();
NvgImgui::DeleteNvgContext(vg);
}
);
};


NvgImgui::RenderNvgToFrameBuffer(vg, nvgFramebuffer, [&](float width, float height)
void demo_nanovg_heart()
{
static AppStateNvgHeart appState;

if (appState.vg == nullptr)
{
appState.Init();
HelloImGui::GetRunnerParams()->callbacks.EnqueueBeforeExit([&]() { appState.Release(); });
}


NvgImgui::RenderNvgToFrameBuffer(appState.vg, *appState.nvgFramebuffer, [&](float width, float height)
{
//DrawHeart(ImmApp::NanoVGContext(), {width / 2.f, height / 2.f}, 200.f, ImGui::GetTime());
DrawTextWithGradient(vg, {width / 2.f, height / 2.f}, fontId);
DrawTextWithGradient(appState.vg, {width / 2.f, height / 2.f}, appState.fontId);
});

ImGui::Text("Hello, world!");
ImGui::Image(nvgFramebuffer->TextureId, ImVec2(1000, 600));
ImGui::Image(appState.nvgFramebuffer->TextureId, ImVec2(1000, 600));
}

//struct MyNvgDemo
//{
// bool Blowup = false;
// DemoData nvgDemoData;
// NVGcontext* vg;
//
// MyNvgDemo(NVGcontext* _vg)
// : vg(_vg)
// {
// int status = loadDemoData(vg, &nvgDemoData);
// IM_ASSERT((status == 0) && "Could not load demo data!");
// }
//
// ~MyNvgDemo()
// {
// freeDemoData(vg, &nvgDemoData);
// }
//
// void Render(float width, float height, int mousex, int mousey, float t)
// {
// renderDemo(vg, mousex, mousey, width, height, t, Blowup, &nvgDemoData);
// }
//
//};



//struct AppState
//{
// std::unique_ptr<MyNvgDemo> myNvgDemo;
//
// NvgImgui::NvgFramebufferPtr myFramebuffer;
//
// ImVec4 ClearColor = ImVec4(0.2f, 0.2f, 0.2f, 1.f);
// bool DisplayInFrameBuffer = false;
//};



//int kkmain(int, char**)
//{
// ChdirBesideAssetsFolder();
//
// AppState appState;
//
// HelloImGui::RunnerParams runnerParams;
// runnerParams.imGuiWindowParams.defaultImGuiWindowType = HelloImGui::DefaultImGuiWindowType::NoDefaultWindow;
// runnerParams.appWindowParams.windowGeometry.size = {1200, 900};
// ImmApp::AddOnsParams addons;
// addons.withNanoVG = true;
//
// runnerParams.callbacks.PostInit = [&]()
// {
// auto vg = ImmApp::NanoVGContext();
// appState.myNvgDemo = std::make_unique<MyNvgDemo>(vg);
// int nvgImageFlags = 0; //NVG_IMAGE_FLIPY | NVG_IMAGE_PREMULTIPLIED;
// appState.myFramebuffer = NvgImgui::CreateNvgFramebuffer(vg, 1000, 600, nvgImageFlags);
// };
// runnerParams.callbacks.BeforeExit = [&]()
// {
// appState.myNvgDemo.reset();
// appState.myFramebuffer.reset();
// };
//
// auto nvgDrawingFunction = [&](float width, float height)
// {
// double now = ImGui::GetTime();
// auto mousePos = ImGui::GetMousePos() - ImGui::GetMainViewport()->Pos;
// appState.myNvgDemo->Render(width, height, (int)mousePos.x, (int)mousePos.y, (float)now);
// };
//
// runnerParams.callbacks.CustomBackground = [&]()
// {
// NvgImgui::RenderNvgToBackground(ImmApp::NanoVGContext(), nvgDrawingFunction, appState.ClearColor);
// };
//
// runnerParams.callbacks.ShowGui = [&]()
// {
// ImGui::Begin("My Window!", NULL, ImGuiWindowFlags_AlwaysAutoResize);
// ImGui::Checkbox("Display in FrameBuffer", &appState.DisplayInFrameBuffer);
// ImGui::Checkbox("Blowup", &appState.myNvgDemo->Blowup);
// ImGui::SetNextItemWidth(HelloImGui::EmSize(15.f));
// ImGui::ColorEdit4("Clear color", &appState.ClearColor.x);
//
// if (appState.DisplayInFrameBuffer)
// {
// NvgImgui::RenderNvgToFrameBuffer(ImmApp::NanoVGContext(), appState.myFramebuffer, nvgDrawingFunction, appState.ClearColor);
// ImGui::Image(appState.myFramebuffer->TextureId, ImVec2(1000, 600));
// }
//
// ImGui::End();
// };
//
// ImmApp::Run(runnerParams, addons);
// return 0;
//}
59 changes: 39 additions & 20 deletions external/nanovg/nvg_imgui/nvg_imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,46 +21,53 @@
namespace NvgImgui
{
#ifdef HELLOIMGUI_HAS_OPENGL
struct NvgFramebufferGl: public NvgFramebuffer
struct NvgFramebuffer::PImpl
{
NVGLUframebuffer* fb = nullptr;
GLint defaultViewport[4]; // To store the default viewport dimensions
NvgFramebuffer * _parent = nullptr;

NvgFramebufferGl(NVGcontext* vg, int width, int height, int nvgImageFlags)
: NvgFramebuffer(width, height, nvgImageFlags)
PImpl(NvgFramebuffer* parent) : _parent(parent)
{
fb = nvgluCreateFramebuffer(vg, width, height, nvgImageFlags);
AcquireResource();
}

~PImpl()
{
ReleaseResource();
}

void AcquireResource()
{
if (_parent->vg == nullptr)
return;
fb = nvgluCreateFramebuffer(_parent->vg, _parent->Width, _parent->Height, _parent->NvgImageFlags);
IM_ASSERT(fb && "Failed to create NVGLU framebuffer");
TextureId = (ImTextureID)(intptr_t)fb->texture;
_parent->TextureId = (ImTextureID)(intptr_t)fb->texture;
}

~NvgFramebufferGl() override
void ReleaseResource()
{
if (fb) {
nvgluDeleteFramebuffer(fb);
fb = nullptr;
}
}

void Bind() override
void Bind()
{
nvgluBindFramebuffer(fb);
glGetIntegerv(GL_VIEWPORT, defaultViewport);
glViewport(0, 0, Width, Height);
glViewport(0, 0, _parent->Width, _parent->Height);
}

void Unbind() override
void Unbind()
{
nvgluBindFramebuffer(nullptr);
glViewport(defaultViewport[0], defaultViewport[1], defaultViewport[2], defaultViewport[3]);
}
};

NvgFramebufferPtr CreateNvgFramebuffer(NVGcontext* vg, int width, int height, int nvImageFlags)
{
return std::make_shared<NvgFramebufferGl>(vg, width, height, nvImageFlags);
}

static void FillClearColor(ImVec4 clearColor)
{
glClearColor(clearColor.x, clearColor.y, clearColor.z, clearColor.w);
Expand Down Expand Up @@ -114,6 +121,18 @@ namespace NvgImgui
#endif // HELLOIMGUI_HAS_OPENGL


NvgFramebuffer::NvgFramebuffer(NVGcontext* vg, int width, int height, int nvgImageFlags) // See NVGimageFlags
: vg(vg), Width(width), Height(height), NvgImageFlags(nvgImageFlags)
{
pImpl = new PImpl(this);
}

NvgFramebuffer::~NvgFramebuffer() { delete pImpl; }

void NvgFramebuffer::Bind() { pImpl->Bind(); }
void NvgFramebuffer::Unbind() { pImpl->Unbind(); }


void RenderNvgToBackground(NVGcontext* vg, NvgDrawingFunction nvgDrawingFunction, ImVec4 clearColor)
{
if (clearColor.w > 0.f)
Expand All @@ -127,9 +146,9 @@ namespace NvgImgui
nvgEndFrame(vg);
}

void RenderNvgToFrameBuffer(NVGcontext* vg, NvgFramebufferPtr texture, NvgDrawingFunction drawFunc, ImVec4 clearColor)
void RenderNvgToFrameBuffer(NVGcontext* vg, NvgFramebuffer& texture, NvgDrawingFunction drawFunc, ImVec4 clearColor)
{
texture->Bind();
texture.Bind();
if (clearColor.w > 0.f)
FillClearColor(clearColor);

Expand All @@ -142,21 +161,21 @@ namespace NvgImgui

// float pixelRatio = ImGui::GetIO().DisplayFramebufferScale.x;
float pixelRatio = 1.f;
nvgBeginFrame(vg, texture->Width, texture->Height, pixelRatio);
nvgBeginFrame(vg, texture.Width, texture.Height, pixelRatio);

// Flip the y-axis
nvgSave(vg); // Save the current state
nvgTranslate(vg, 0, texture->Height); // Move the origin to the bottom-left
nvgTranslate(vg, 0, texture.Height); // Move the origin to the bottom-left
nvgScale(vg, 1, -1); // Flip the y-axis

// Perform drawing operations
drawFunc(texture->Width, texture->Height);
drawFunc(texture.Width, texture.Height);

nvgRestore(vg); // Restore the original state
nvgEndFrame(vg);
nvgReset(vg); // Reset any temporary state changes that may have been made

texture->Unbind();
texture.Unbind();
}


Expand Down
Loading

0 comments on commit eb29390

Please sign in to comment.