main.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include <RmlUi/Core.h>
  29. #include <RmlUi/Debugger.h>
  30. #include <Input.h>
  31. #include <Shell.h>
  32. #include <ShellRenderInterfaceOpenGL.h>
  33. #include "SystemInterface.h"
  34. Rml::Context* context = nullptr;
  35. ShellRenderInterfaceExtensions *shell_renderer;
  36. void GameLoop()
  37. {
  38. context->Update();
  39. shell_renderer->PrepareRenderBuffer();
  40. context->Render();
  41. shell_renderer->PresentRenderBuffer();
  42. }
  43. #if defined RMLUI_PLATFORM_WIN32
  44. #include <windows.h>
  45. int APIENTRY WinMain(HINSTANCE RMLUI_UNUSED_PARAMETER(instance_handle), HINSTANCE RMLUI_UNUSED_PARAMETER(previous_instance_handle), char* RMLUI_UNUSED_PARAMETER(command_line), int RMLUI_UNUSED_PARAMETER(command_show))
  46. #else
  47. int main(int RMLUI_UNUSED_PARAMETER(argc), char** RMLUI_UNUSED_PARAMETER(argv))
  48. #endif
  49. {
  50. #ifdef RMLUI_PLATFORM_WIN32
  51. RMLUI_UNUSED(instance_handle);
  52. RMLUI_UNUSED(previous_instance_handle);
  53. RMLUI_UNUSED(command_line);
  54. RMLUI_UNUSED(command_show);
  55. #else
  56. RMLUI_UNUSED(argc);
  57. RMLUI_UNUSED(argv);
  58. #endif
  59. #ifdef RMLUI_PLATFORM_WIN32
  60. AllocConsole();
  61. #endif
  62. int window_width = 1024;
  63. int window_height = 768;
  64. ShellRenderInterfaceOpenGL opengl_renderer;
  65. shell_renderer = &opengl_renderer;
  66. // Generic OS initialisation, creates a window and attaches OpenGL.
  67. if (!Shell::Initialise() ||
  68. !Shell::OpenWindow("Custom File Handler Sample", shell_renderer, window_width, window_height, true))
  69. {
  70. Shell::Shutdown();
  71. return -1;
  72. }
  73. // RmlUi initialisation.
  74. Rml::SetRenderInterface(&opengl_renderer);
  75. opengl_renderer.SetViewport(window_width, window_height);
  76. // Initialise our system interface to write the log messages to file.
  77. ShellSystemInterface system_interface;
  78. Rml::SetSystemInterface(&system_interface);
  79. Rml::Initialise();
  80. // Create the main RmlUi context and set it on the shell's input layer.
  81. context = Rml::CreateContext("main", Rml::Vector2i(window_width, window_height));
  82. if (context == nullptr)
  83. {
  84. Rml::Shutdown();
  85. Shell::Shutdown();
  86. return -1;
  87. }
  88. Rml::Debugger::Initialise(context);
  89. Input::SetContext(context);
  90. shell_renderer->SetContext(context);
  91. Shell::LoadFonts("assets/");
  92. // Load a non-existent document to spawn an error message.
  93. Rml::ElementDocument* invalid_document = context->LoadDocument("assets/invalid.rml");
  94. RMLUI_ASSERTMSG(invalid_document != nullptr, "Testing ASSERT logging.");
  95. if (invalid_document != nullptr)
  96. {
  97. invalid_document->Close();
  98. }
  99. // Load and show the demo document.
  100. Rml::ElementDocument* document = context->LoadDocument("assets/demo.rml");
  101. if (document != nullptr)
  102. {
  103. document->Show();
  104. }
  105. Shell::EventLoop(GameLoop);
  106. // Shutdown RmlUi.
  107. Rml::Shutdown();
  108. Shell::CloseWindow();
  109. Shell::Shutdown();
  110. return 0;
  111. }