87 lines
2.4 KiB
C++
87 lines
2.4 KiB
C++
|
/*
|
||
|
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;
|
||
|
}
|