From e0003dded5a676a2f1d3f80c9df6dbb044176aec Mon Sep 17 00:00:00 2001 From: Christopher Arndt Date: Tue, 9 Apr 2024 22:12:53 +0200 Subject: [PATCH] Add C++ helloworld example Signed-off-by: Christopher Arndt --- .gitignore | 5 +-- CMakeLists.txt | 19 ++++++++++ lib/nanogui | 1 + nanogui_helloworld.cpp | 86 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 CMakeLists.txt create mode 120000 lib/nanogui create mode 100644 nanogui_helloworld.cpp diff --git a/.gitignore b/.gitignore index 68751ca..705b9f0 100644 --- a/.gitignore +++ b/.gitignore @@ -15,11 +15,8 @@ dist/ downloads/ eggs/ .eggs/ -lib/ -lib64/ parts/ sdist/ -var/ wheels/ share/python-wheels/ *.egg-info/ @@ -194,3 +191,5 @@ cython_debug/ *.out *.app +# nanogui lib +lib/nanogui/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..6898337 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 3.13) +project(nanogui_helloworld) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED YES) +set(NANOGUI_BUILD_EXAMPLES OFF) +set(NANOGUI_BUILD_PYTHON OFF) +set(NANOGUI_BUILD_SHARED OFF) + +add_subdirectory(lib/nanogui) +include_directories(lib/nanogui/include) +include_directories(${NANOGUI_EXTRA_INCS}) +add_definitions(${NANOGUI_EXTRA_DEFS}) + +set_property(TARGET glfw glfw_objects nanogui PROPERTY FOLDER "dependencies") + +add_executable(nanogui_helloworld nanogui_helloworld.cpp) +target_link_libraries(nanogui_helloworld nanogui ${NANOGUI_EXTRA_LIBS}) + diff --git a/lib/nanogui b/lib/nanogui new file mode 120000 index 0000000..4183cf3 --- /dev/null +++ b/lib/nanogui @@ -0,0 +1 @@ +../../nanogui \ No newline at end of file diff --git a/nanogui_helloworld.cpp b/nanogui_helloworld.cpp new file mode 100644 index 0000000..1b5083f --- /dev/null +++ b/nanogui_helloworld.cpp @@ -0,0 +1,86 @@ +/* + src/nanogui_helloworld.cpp -- C++ version of an helloworld example application +*/ + +#include +#include +#include +#include +#include +#include + +#include + +using namespace nanogui; + +class HelloWorldApplication : public Screen { +public: + HelloWorldApplication() : Screen(Vector2i(400, 300), "NanoGUI Hello World") { + inc_ref(); + window = new Window(this, "Demo Window"); + window->set_layout(new BoxLayout(Orientation::Vertical, Alignment::Middle, 20, 20)); + resize_event(size()); + + new Label(window, "NanoGUI Demo", "sans-bold"); + + Button *b = new Button(window, "Say hello!"); + b->set_callback([] { std::cout << "Well, 'ello there!" << std::endl; }); + b->set_tooltip("Push me!"); + + perform_layout(); + } + + virtual bool resize_event(const Vector2i &size) { + window->set_fixed_size(size); + window->set_size(size); + window->center(); + window->perform_layout(nvg_context()); + return true; + } + + virtual bool keyboard_event(int key, int scancode, int action, int modifiers) { + if (Screen::keyboard_event(key, scancode, action, modifiers)) + return true; + if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) { + set_visible(false); + return true; + } + return false; + } + + virtual void draw(NVGcontext *ctx) { + /* Draw the user interface */ + Screen::draw(ctx); + } + +private: + Window *window; +}; + +int main(int /* argc */, char ** /* argv */) { + try { + nanogui::init(); + + /* scoped variables */ { + ref app = new HelloWorldApplication(); + app->dec_ref(); + app->draw_all(); + app->set_visible(true); + nanogui::mainloop(1 / 60.f * 1000); + } + + nanogui::shutdown(); + } catch (const std::exception &e) { + std::string error_msg = std::string("Caught a fatal error: ") + std::string(e.what()); + #if defined(_WIN32) + MessageBoxA(nullptr, error_msg.c_str(), NULL, MB_ICONERROR | MB_OK); + #else + std::cerr << error_msg << std::endl; + #endif + return -1; + } catch (...) { + std::cerr << "Caught an unknown error!" << std::endl; + } + + return 0; +}