main.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 "FontEngineInterfaceBitmap.h"
  29. #include <RmlUi/Core.h>
  30. #include <RmlUi/Debugger.h>
  31. #include <RmlUi_Backend.h>
  32. #include <Shell.h>
  33. /*
  34. This demo shows how to create a custom bitmap font engine implementation.
  35. It should work even when RmlUi is compiled without the default font engine (see CMake flag 'NO_FONT_INTERFACE_DEFAULT').
  36. See the interface in 'FontEngineInterfaceBitmap.h' and the implementation in 'FontEngineBitmap.h'.
  37. */
  38. #if defined RMLUI_PLATFORM_WIN32
  39. #include <RmlUi_Include_Windows.h>
  40. int APIENTRY WinMain(HINSTANCE /*instance_handle*/, HINSTANCE /*previous_instance_handle*/, char* /*command_line*/, int /*command_show*/)
  41. #else
  42. int main(int /*argc*/, char** /*argv*/)
  43. #endif
  44. {
  45. int window_width = 1024;
  46. int window_height = 768;
  47. // Initializes the shell which provides common functionality used by the included samples.
  48. if (!Shell::Initialize())
  49. return -1;
  50. // Constructs the system and render interfaces, creates a window, and attaches the renderer.
  51. if (!Backend::Initialize("Bitmap Font Sample", window_width, window_height, true))
  52. {
  53. Shell::Shutdown();
  54. return -1;
  55. }
  56. // Install the custom interfaces constructed by the backend before initializing RmlUi.
  57. Rml::SetSystemInterface(Backend::GetSystemInterface());
  58. Rml::SetRenderInterface(Backend::GetRenderInterface());
  59. // Construct and load the font interface.
  60. auto font_interface = Rml::MakeUnique<FontEngineInterfaceBitmap>();
  61. Rml::SetFontEngineInterface(font_interface.get());
  62. // RmlUi initialisation.
  63. Rml::Initialise();
  64. // Create the main RmlUi context.
  65. Rml::Context* context = Rml::CreateContext("main", Rml::Vector2i(window_width, window_height));
  66. if (!context)
  67. {
  68. Rml::Shutdown();
  69. Backend::Shutdown();
  70. Shell::Shutdown();
  71. return -1;
  72. }
  73. Rml::Debugger::Initialise(context);
  74. // Load bitmap font
  75. if (!Rml::LoadFontFace("basic/bitmapfont/data/Comfortaa_Regular_22.fnt"))
  76. {
  77. Rml::Shutdown();
  78. Backend::Shutdown();
  79. Shell::Shutdown();
  80. return -1;
  81. }
  82. // Load and show the demo document.
  83. if (Rml::ElementDocument * document = context->LoadDocument("basic/bitmapfont/data/bitmapfont.rml"))
  84. {
  85. if (auto el = document->GetElementById("title"))
  86. el->SetInnerRML("Bitmap font");
  87. document->Show();
  88. }
  89. bool running = true;
  90. while (running)
  91. {
  92. running = Backend::ProcessEvents(context, &Shell::ProcessKeyDownShortcuts);
  93. context->Update();
  94. Backend::BeginFrame();
  95. context->Render();
  96. Backend::PresentFrame();
  97. }
  98. // Shutdown RmlUi.
  99. Rml::Shutdown();
  100. // Destroy the font interface before taking down the shell, this way font textures are properly released through the render interface.
  101. font_interface.reset();
  102. Backend::Shutdown();
  103. Shell::Shutdown();
  104. return 0;
  105. }