Add C++ helloworld example

Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
This commit is contained in:
Christopher Arndt 2024-04-09 22:12:53 +02:00
parent 2ea0ad23e9
commit e0003dded5
4 changed files with 108 additions and 3 deletions

5
.gitignore vendored
View File

@ -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/

19
CMakeLists.txt Normal file
View File

@ -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})

1
lib/nanogui Symbolic link
View File

@ -0,0 +1 @@
../../nanogui

86
nanogui_helloworld.cpp Normal file
View File

@ -0,0 +1,86 @@
/*
src/nanogui_helloworld.cpp -- C++ version of an helloworld example application
*/
#include <nanogui/opengl.h>
#include <nanogui/screen.h>
#include <nanogui/window.h>
#include <nanogui/layout.h>
#include <nanogui/label.h>
#include <nanogui/button.h>
#include <iostream>
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<HelloWorldApplication> 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;
}