Skip to content

Instantly share code, notes, and snippets.

@werto87
Created October 25, 2021 16:38
Show Gist options
  • Save werto87/fc96ed1fb2c549a6021aa72260d51e23 to your computer and use it in GitHub Desktop.
Save werto87/fc96ed1fb2c549a6021aa72260d51e23 to your computer and use it in GitHub Desktop.
cxxopts and magnum possible memory leak small example
/*
This file is part of Magnum.
Original authors — credit is appreciated but not required:
2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021
— Vladimír Vondruš <mosra@centrum.cz>
2018 — ShaddyAQN <ShaddyAQN@gmail.com>
2018 — Jonathan Hale <squareys@googlemail.com>
2018 — Tomáš Skřivan <skrivantomas@seznam.cz>
2018 — Natesh Narain <nnaraindev@gmail.com>
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute
this software, either in source code form or as a compiled binary, for any
purpose, commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of
this software dedicate any and all copyright interest in the software to
the public domain. We make this dedication for the benefit of the public
at large and to the detriment of our heirs and successors. We intend this
dedication to be an overt act of relinquishment in perpetuity of all
present and future rights to this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <Magnum/GL/DefaultFramebuffer.h>
#include <Magnum/GL/Renderer.h>
#include <Magnum/ImGuiIntegration/Context.hpp>
#include <Magnum/Math/Color.h>
#ifdef CORRADE_TARGET_ANDROID
#include <Magnum/Platform/AndroidApplication.h>
#elif defined(CORRADE_TARGET_EMSCRIPTEN)
#include <Magnum/Platform/EmscriptenApplication.h>
#else
#include <Magnum/Platform/Sdl2Application.h>
#endif
#include <cxxopts.hpp>
#include <filesystem>
std::string
from_u8string (const std::u8string &s)
{
return std::string (s.begin (), s.end ());
}
namespace Magnum
{
namespace Examples
{
using namespace Math::Literals;
class ImGuiExample : public Platform::Application
{
public:
explicit ImGuiExample (const Arguments &arguments);
void drawEvent () override;
void viewportEvent (ViewportEvent &event) override;
void keyPressEvent (KeyEvent &event) override;
void keyReleaseEvent (KeyEvent &event) override;
void mousePressEvent (MouseEvent &event) override;
void mouseReleaseEvent (MouseEvent &event) override;
void mouseMoveEvent (MouseMoveEvent &event) override;
void mouseScrollEvent (MouseScrollEvent &event) override;
void textInputEvent (TextInputEvent &event) override;
private:
ImGuiIntegration::Context _imgui{ NoCreate };
ImFont *smallFont{};
bool _showDemoWindow = true;
bool _showAnotherWindow = false;
Color4 _clearColor = 0x72909aff_rgbaf;
Float _floatValue = 0.0f;
};
ImGuiExample::ImGuiExample (const Arguments &arguments) : Platform::Application{ arguments, Configuration{}.setTitle ("Magnum ImGui Example").setWindowFlags (Configuration::WindowFlag::Resizable) }
{
ImGui::CreateContext ();
ImGuiIO &io = ImGui::GetIO ();
std::cout << io.IniFilename << std::endl;
ImVector<ImWchar> ranges;
ImFontGlyphRangesBuilder builder;
builder.AddText (from_u8string (std::u8string{ u8"♠♥♦♣" }).c_str ());
builder.AddRanges (io.Fonts->GetGlyphRangesDefault ());
builder.BuildRanges (&ranges);
auto const fontFile = std::filesystem::path{ "bin/asset/DejaVuSans.ttf" };
if (std::filesystem::exists (fontFile)) std::cout << "file exists" << std::endl;
auto const fontSize = 100.0f;
smallFont = io.Fonts->AddFontFromFileTTF (fontFile.c_str (), fontSize * 0.5f, nullptr, ranges.Data);
io.Fonts->Build ();
_imgui = Magnum::ImGuiIntegration::Context (*ImGui::GetCurrentContext (), windowSize ());
Magnum::GL::Renderer::setBlendEquation (GL::Renderer::BlendEquation::Add, GL::Renderer::BlendEquation::Add);
Magnum::GL::Renderer::setBlendFunction (GL::Renderer::BlendFunction::SourceAlpha, GL::Renderer::BlendFunction::OneMinusSourceAlpha);
}
void
ImGuiExample::drawEvent ()
{
GL::defaultFramebuffer.clear (GL::FramebufferClear::Color);
_imgui.newFrame ();
/* Enable text input, if needed */
if (ImGui::GetIO ().WantTextInput && !isTextInputActive ()) startTextInput ();
else if (!ImGui::GetIO ().WantTextInput && isTextInputActive ())
stopTextInput ();
ImGui::PushFont (smallFont); // use big fonts for smaller window width so text is readable on mobile
/* 1. Show a simple window.
Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appear in
a window called "Debug" automatically */
{
ImGui::Text ("Hello, world!");
ImGui::SliderFloat ("Float", &_floatValue, 0.0f, 1.0f);
if (ImGui::ColorEdit3 ("Clear Color", _clearColor.data ())) GL::Renderer::setClearColor (_clearColor);
if (ImGui::Button ("Test Window")) _showDemoWindow ^= true;
if (ImGui::Button ("Another Window")) _showAnotherWindow ^= true;
ImGui::Text ("Application average %.3f ms/frame (%.1f FPS)", 1000.0 / Double (ImGui::GetIO ().Framerate), Double (ImGui::GetIO ().Framerate));
}
/* 2. Show another simple window, now using an explicit Begin/End pair */
if (_showAnotherWindow)
{
ImGui::SetNextWindowSize (ImVec2 (500, 100), ImGuiCond_FirstUseEver);
ImGui::Begin ("Another Window", &_showAnotherWindow);
ImGui::Text ("Hello");
ImGui::End ();
}
/* 3. Show the ImGui demo window. Most of the sample code is in
ImGui::ShowDemoWindow() */
if (_showDemoWindow)
{
ImGui::SetNextWindowPos (ImVec2 (650, 20), ImGuiCond_FirstUseEver);
ImGui::ShowDemoWindow ();
}
ImGui::PopFont ();
/* Update application cursor */
_imgui.updateApplicationCursor (*this);
/* Set appropriate states. If you only draw ImGui, it is sufficient to
just enable blending and scissor test in the constructor. */
GL::Renderer::enable (GL::Renderer::Feature::Blending);
GL::Renderer::enable (GL::Renderer::Feature::ScissorTest);
GL::Renderer::disable (GL::Renderer::Feature::FaceCulling);
GL::Renderer::disable (GL::Renderer::Feature::DepthTest);
_imgui.drawFrame ();
/* Reset state. Only needed if you want to draw something else with
different state after. */
GL::Renderer::enable (GL::Renderer::Feature::DepthTest);
GL::Renderer::enable (GL::Renderer::Feature::FaceCulling);
GL::Renderer::disable (GL::Renderer::Feature::ScissorTest);
GL::Renderer::disable (GL::Renderer::Feature::Blending);
swapBuffers ();
redraw ();
}
void
ImGuiExample::viewportEvent (ViewportEvent &event)
{
GL::defaultFramebuffer.setViewport ({ {}, event.framebufferSize () });
_imgui.relayout (Vector2{ event.windowSize () } / event.dpiScaling (), event.windowSize (), event.framebufferSize ());
}
void
ImGuiExample::keyPressEvent (KeyEvent &event)
{
if (_imgui.handleKeyPressEvent (event)) return;
}
void
ImGuiExample::keyReleaseEvent (KeyEvent &event)
{
if (_imgui.handleKeyReleaseEvent (event)) return;
}
void
ImGuiExample::mousePressEvent (MouseEvent &event)
{
if (_imgui.handleMousePressEvent (event)) return;
}
void
ImGuiExample::mouseReleaseEvent (MouseEvent &event)
{
if (_imgui.handleMouseReleaseEvent (event)) return;
}
void
ImGuiExample::mouseMoveEvent (MouseMoveEvent &event)
{
if (_imgui.handleMouseMoveEvent (event)) return;
}
void
ImGuiExample::mouseScrollEvent (MouseScrollEvent &event)
{
if (_imgui.handleMouseScrollEvent (event))
{
/* Prevent scrolling the page */
event.setAccepted ();
return;
}
}
void
ImGuiExample::textInputEvent (TextInputEvent &event)
{
if (_imgui.handleTextInputEvent (event)) return;
}
}
}
// MAGNUM_APPLICATION_MAIN (Magnum::Examples::ImGuiExample)
namespace
{
Corrade::Containers::Pointer<Magnum::Examples::ImGuiExample> emscriptenApplicationInstance;
}
int
main (int argc, char **argv)
{
cxxopts::Options options ("modern-durak-client", "A brief description");
options.add_options () ("w,websocket-url", "url to websocket --websocket-url wss://www.example.com/socketserver", cxxopts::value<std::string> ()->default_value ("wss://localhost:55555")) ("t,touch", "set this option if input type is touch and not keyboard") ("h,help", "Print usage");
emscriptenApplicationInstance.reset (new Magnum::Examples::ImGuiExample{ { argc, argv } });
emscriptenApplicationInstance->redraw ();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment