Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. ////////////////////////////////////////////////////////////////////////////////////////////////////
  2. // NoesisGUI - http://www.noesisengine.com
  3. // Copyright (c) 2013 Noesis Technologies S.L. All Rights Reserved.
  4. ////////////////////////////////////////////////////////////////////////////////////////////////////
  5.  
  6.  
  7. // This is a minimal integration example. For simplification purposes only basic input events are
  8. // handled, no resource providers are used and the shutdown procedure is omitted. A more complete
  9. // multiplatform integration sample with step by step comments can be found at 'Samples/Integration'
  10.  
  11.  
  12. #ifdef _WIN32
  13. #include "glut.h"
  14. #pragma comment(linker,"/SUBSYSTEM:CONSOLE")
  15. #endif
  16.  
  17. #ifdef __APPLE__
  18. #include <GLUT/glut.h>
  19. #endif
  20.  
  21. #ifdef __EMSCRIPTEN__
  22. #include <GL/glut.h>
  23. #include <GLES3/gl32.h>
  24. #include <emscripten/html5.h>
  25. #endif
  26.  
  27. #ifdef __linux__
  28. #define GL_GLEXT_PROTOTYPES
  29. #include <GL/gl.h>
  30. #include <GL/glut.h>
  31. #endif
  32.  
  33. #ifdef _MSC_VER
  34. #define UNUSED_ARGS(...) (void)(true ? (void)0 : ((void)(__VA_ARGS__)))
  35. #else
  36. #define UNUSED_ARGS(...)
  37. #endif
  38.  
  39.  
  40. #include <NsApp/ThemeProviders.h>
  41. #include <NsRender/GLFactory.h>
  42. #include <NsGui/FontProperties.h>
  43. #include <NsGui/IntegrationAPI.h>
  44. #include <NsGui/IRenderer.h>
  45. #include <NsGui/IView.h>
  46. #include <NsGui/Grid.h>
  47. #include <NsGui/Uri.h>
  48.  
  49. #include <NsApp/LocalXamlProvider.h>
  50. #include <NsApp/LocalTextureProvider.h>
  51. #include <NsApp/LocalFontProvider.h>
  52.  
  53. static Noesis::IView* _view;
  54.  
  55. constexpr int Width = 1366;
  56. constexpr int Height = 768;
  57.  
  58. ///////////////////////////////////////////////////////////////////////////////////////////////////
  59. static void NoesisInit()
  60. {
  61.     // A logging handler is installed here. You can also install a custom error handler and memory
  62.     // allocator. By default errors are redirected to the logging handler
  63.     Noesis::GUI::SetLogHandler([](const char*, uint32_t, uint32_t level, const char*, const char* msg)
  64.     {
  65.         // [TRACE] [DEBUG] [INFO] [WARNING] [ERROR]
  66.         const char* prefixes[] = { "T", "D", "I", "W", "E" };
  67.         printf("[NOESIS/%s] %s\n", prefixes[level], msg);
  68.     });
  69.  
  70.     // https://www.noesisengine.com/docs/Gui.Core.Licensing.html
  71.     Noesis::GUI::SetLicense(NS_LICENSE_NAME, NS_LICENSE_KEY);
  72.  
  73.     // Noesis initialization. This must be the first step before using any NoesisGUI functionality
  74.     Noesis::GUI::Init();
  75.  
  76.     // Setup theme
  77.     Noesis::Ptr<Noesis::XamlProvider> xamlProvider = *new NoesisApp::LocalXamlProvider("UI\\");
  78.     Noesis::Ptr<Noesis::FontProvider> fontProvider = *new NoesisApp::LocalFontProvider("UI\\");
  79.     Noesis::Ptr<Noesis::TextureProvider> textureProvider = *new NoesisApp::LocalTextureProvider("UI\\");
  80.  
  81.     NoesisApp::SetThemeProviders(xamlProvider, fontProvider, textureProvider);
  82.     Noesis::GUI::LoadApplicationResources("Theme/NoesisTheme.DarkBlue.xaml");
  83.  
  84.     // For simplicity purposes we are not using resource providers in this sample. ParseXaml() is
  85.     // enough if there is no extra XAML dependencies
  86.     Noesis::Ptr<Noesis::Grid> xaml(Noesis::GUI::LoadXaml<Noesis::Grid>("Example.xaml"));
  87.  
  88.     // View creation to render and interact with the user interface
  89.     // We transfer the ownership to a global pointer instead of a Ptr<> because there is no way
  90.     // in GLUT to do shutdown and we don't want the Ptr<> to be released at global time
  91.     _view = Noesis::GUI::CreateView(xaml).GiveOwnership();
  92.     _view->SetFlags(Noesis::RenderFlags_PPAA | Noesis::RenderFlags_LCD);
  93.  
  94.     // Renderer initialization with an OpenGL device
  95.     _view->GetRenderer()->Init(NoesisApp::GLFactory::CreateDevice(false));
  96.  
  97.     // Virtual screen resize for smooth scaling
  98.     auto aspect = static_cast<float>(Width) / static_cast<float>(Height);
  99.     const auto base_screen = _view->GetContent()->FindName<Noesis::Grid>("BaseScreen");
  100.  
  101.     // 4:3
  102.     if (aspect == 1.3f) {
  103.         base_screen->SetWidth(1440);
  104.         base_screen->SetHeight(1080);
  105.     }
  106.     // 16:10
  107.     else if (aspect == 1.6f) {
  108.         base_screen->SetWidth(1680);
  109.         base_screen->SetHeight(1050);
  110.     }
  111.     // 16:9
  112.     else if (aspect >= 1.7f) {
  113.         base_screen->SetWidth(1920 * 0.9f);
  114.         base_screen->SetHeight(1080 * 0.9f);
  115.     }
  116.     // default
  117.     else {
  118.         base_screen->SetWidth(1440);
  119.         base_screen->SetHeight(1080);
  120.     }
  121. }
  122.  
  123. ///////////////////////////////////////////////////////////////////////////////////////////////////
  124. static void DisplayFunc(void)
  125. {
  126.     // Update view (layout, animations, ...)
  127.     _view->Update(glutGet(GLUT_ELAPSED_TIME) / 1000.0);
  128.  
  129.     // Offscreen rendering phase populates textures needed by the on-screen rendering
  130.     _view->GetRenderer()->UpdateRenderTree();
  131.     _view->GetRenderer()->RenderOffscreen();
  132.  
  133.     // If you are going to render here with your own engine you need to restore the GPU state
  134.     // because noesis changes it. In this case only framebuffer and viewport need to be restored
  135.     glBindFramebuffer(GL_FRAMEBUFFER, 0);
  136.     glViewport(0, 0, glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
  137.  
  138.     glClearColor(0.0f, 0.0f, 0.25f, 0.0f);
  139.  
  140.     glDisable(GL_SCISSOR_TEST);
  141.     glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  142.     glClearStencil(0);
  143.     glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  144.  
  145.     // Rendering is done in the active framebuffer
  146.     _view->GetRenderer()->Render();
  147.  
  148.     glutSwapBuffers();
  149.     glutPostRedisplay();
  150. }
  151.  
  152. ////////////////////////////////////////////////////////////////////////////////////////////////////
  153. static void ReshapeFunc(int width, int height)
  154. {
  155.     _view->SetSize(width, height);
  156. }
  157.  
  158. ////////////////////////////////////////////////////////////////////////////////////////////////////
  159. static void MouseMoveFunc(int x, int y)
  160. {
  161.     _view->MouseMove(x, y);
  162. }
  163.  
  164. ////////////////////////////////////////////////////////////////////////////////////////////////////
  165. static void MouseFunc(int button, int state, int x, int y)
  166. {
  167.     if (button == GLUT_LEFT_BUTTON)
  168.     {
  169.         if (state == GLUT_DOWN)
  170.         {
  171.             _view->MouseButtonDown(x, y, Noesis::MouseButton_Left);
  172.         }
  173.         else
  174.         {
  175.             _view->MouseButtonUp(x, y, Noesis::MouseButton_Left);
  176.         }
  177.     }
  178. }
  179.  
  180. ////////////////////////////////////////////////////////////////////////////////////////////////////
  181. int main(int argc, char **argv)
  182. {
  183.     glutInit(&argc, argv);
  184.     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_STENCIL);
  185.     glutInitWindowSize(Width, Height);
  186.  
  187. #ifdef __EMSCRIPTEN__
  188.     double width, height;
  189.     emscripten_get_element_css_size("#canvas", &width, &height);
  190.     emscripten_set_canvas_element_size("#canvas", (uint32_t)width, (uint32_t)height);
  191.     glutInitWindowSize((uint32_t)width, (uint32_t)height);
  192. #endif
  193.  
  194.     glutCreateWindow("NoesisGUI - GLUT integration");
  195.     NoesisInit();
  196.  
  197.     glutDisplayFunc(&DisplayFunc);
  198.     glutReshapeFunc(&ReshapeFunc);
  199.     glutPassiveMotionFunc(&MouseMoveFunc);
  200.     glutMouseFunc(&MouseFunc);
  201.  
  202.     glutMainLoop();
  203.     return 0;
  204. }
  205.