main.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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/Controls.h>
  30. #include <RmlUi/Debugger.h>
  31. #include <Shell.h>
  32. #include <ShellRenderInterfaceOpenGL.h>
  33. #include <Input.h>
  34. #include "DecoratorInstancerDefender.h"
  35. #include "DecoratorInstancerStarfield.h"
  36. #include "ElementGame.h"
  37. #include "EventHandlerHighScore.h"
  38. #include "EventHandlerOptions.h"
  39. #include "EventHandlerStartGame.h"
  40. #include "EventInstancer.h"
  41. #include "EventManager.h"
  42. #include "HighScores.h"
  43. #include "HighScoresNameFormatter.h"
  44. #include "HighScoresShipFormatter.h"
  45. Rml::Core::Context* context = nullptr;
  46. ShellRenderInterfaceExtensions *shell_renderer;
  47. void GameLoop()
  48. {
  49. context->Update();
  50. shell_renderer->PrepareRenderBuffer();
  51. context->Render();
  52. shell_renderer->PresentRenderBuffer();
  53. }
  54. #if defined RMLUI_PLATFORM_WIN32
  55. #include <windows.h>
  56. 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))
  57. #else
  58. int main(int RMLUI_UNUSED_PARAMETER(argc), char** RMLUI_UNUSED_PARAMETER(argv))
  59. #endif
  60. {
  61. #ifdef RMLUI_PLATFORM_WIN32
  62. RMLUI_UNUSED(instance_handle);
  63. RMLUI_UNUSED(previous_instance_handle);
  64. RMLUI_UNUSED(command_line);
  65. RMLUI_UNUSED(command_show);
  66. #else
  67. RMLUI_UNUSED(argc);
  68. RMLUI_UNUSED(argv);
  69. #endif
  70. #ifdef RMLUI_PLATFORM_WIN32
  71. AllocConsole();
  72. #endif
  73. int window_width = 1024;
  74. int window_height = 768;
  75. ShellRenderInterfaceOpenGL opengl_renderer;
  76. shell_renderer = &opengl_renderer;
  77. // Generic OS initialisation, creates a window and attaches OpenGL.
  78. if (!Shell::Initialise() ||
  79. !Shell::OpenWindow("RmlUi Invaders from Mars", shell_renderer, window_width, window_height, false))
  80. {
  81. Shell::Shutdown();
  82. return -1;
  83. }
  84. // RmlUi initialisation.
  85. Rml::Core::SetRenderInterface(&opengl_renderer);
  86. opengl_renderer.SetViewport(window_width, window_height);
  87. ShellSystemInterface system_interface;
  88. Rml::Core::SetSystemInterface(&system_interface);
  89. Rml::Core::Initialise();
  90. // Initialise the RmlUi Controls library.
  91. Rml::Controls::Initialise();
  92. // Create the main RmlUi context and set it on the shell's input layer.
  93. context = Rml::Core::CreateContext("main", Rml::Core::Vector2i(window_width, window_height));
  94. if (context == nullptr)
  95. {
  96. Rml::Core::Shutdown();
  97. Shell::Shutdown();
  98. return -1;
  99. }
  100. // Initialise the RmlUi debugger.
  101. Rml::Debugger::Initialise(context);
  102. Input::SetContext(context);
  103. shell_renderer->SetContext(context);
  104. // Load the font faces required for Invaders.
  105. Shell::LoadFonts("assets/");
  106. // Register Invader's custom element and decorator instancers.
  107. Rml::Core::ElementInstancerGeneric< ElementGame > element_instancer_game;
  108. Rml::Core::Factory::RegisterElementInstancer("game", &element_instancer_game);
  109. DecoratorInstancerStarfield decorator_instancer_starfield;
  110. DecoratorInstancerDefender decorator_instancer_defender;
  111. Rml::Core::Factory::RegisterDecoratorInstancer("starfield", &decorator_instancer_starfield);
  112. Rml::Core::Factory::RegisterDecoratorInstancer("defender", &decorator_instancer_defender);
  113. // Register Invader's data formatters
  114. HighScoresNameFormatter name_formatter;
  115. HighScoresShipFormatter ship_formatter;
  116. // Construct the game singletons.
  117. HighScores::Initialise();
  118. // Initialise the event instancer and handlers.
  119. EventInstancer event_listener_instancer;
  120. Rml::Core::Factory::RegisterEventListenerInstancer(&event_listener_instancer);
  121. EventManager::RegisterEventHandler("start_game", new EventHandlerStartGame());
  122. EventManager::RegisterEventHandler("high_score", new EventHandlerHighScore());
  123. EventManager::RegisterEventHandler("options", new EventHandlerOptions());
  124. // Start the game.
  125. if (EventManager::LoadWindow("background") &&
  126. EventManager::LoadWindow("main_menu"))
  127. Shell::EventLoop(GameLoop);
  128. // Shut down the game singletons.
  129. HighScores::Shutdown();
  130. // Release the event handlers.
  131. EventManager::Shutdown();
  132. // Shutdown RmlUi.
  133. Rml::Core::Shutdown();
  134. Shell::CloseWindow();
  135. Shell::Shutdown();
  136. return 0;
  137. }