main.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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-2023 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 "DecoratorDefender.h"
  29. #include "DecoratorStarfield.h"
  30. #include "ElementGame.h"
  31. #include "HighScores.h"
  32. #include "LuaInterface.h"
  33. #include <RmlUi/Core.h>
  34. #include <RmlUi/Debugger.h>
  35. #include <RmlUi/Lua.h>
  36. #include <RmlUi_Backend.h>
  37. #include <Shell.h>
  38. Rml::Context* context = nullptr;
  39. #if defined RMLUI_PLATFORM_WIN32
  40. #include <RmlUi_Include_Windows.h>
  41. int APIENTRY WinMain(HINSTANCE /*instance_handle*/, HINSTANCE /*previous_instance_handle*/, char* /*command_line*/, int /*command_show*/)
  42. #else
  43. int main(int /*argc*/, char** /*argv*/)
  44. #endif
  45. {
  46. int window_width = 1024;
  47. int window_height = 768;
  48. // Initializes the shell which provides common functionality used by the included samples.
  49. if (!Shell::Initialize())
  50. return -1;
  51. // Constructs the system and render interfaces, creates a window, and attaches the renderer.
  52. if (!Backend::Initialize("RmlUi Invaders from Mars (Lua Powered)", window_width, window_height, false))
  53. {
  54. Shell::Shutdown();
  55. return -1;
  56. }
  57. // Install the custom interfaces constructed by the backend before initializing RmlUi.
  58. Rml::SetSystemInterface(Backend::GetSystemInterface());
  59. Rml::SetRenderInterface(Backend::GetRenderInterface());
  60. // RmlUi initialisation.
  61. Rml::Initialise();
  62. // Initialise the Lua interface
  63. Rml::Lua::Initialise();
  64. // Create the main RmlUi context.
  65. context = Rml::CreateContext("main", Rml::Vector2i(window_width, window_height));
  66. if (context == nullptr)
  67. {
  68. Rml::Shutdown();
  69. Backend::Shutdown();
  70. Shell::Shutdown();
  71. return -1;
  72. }
  73. // Initialise the RmlUi debugger.
  74. Rml::Debugger::Initialise(context);
  75. // Load the font faces required for Invaders.
  76. Shell::LoadFonts();
  77. // Register Invader's custom decorator instancers.
  78. DecoratorInstancerStarfield decorator_starfield;
  79. DecoratorInstancerDefender decorator_defender;
  80. Rml::Factory::RegisterDecoratorInstancer("starfield", &decorator_starfield);
  81. Rml::Factory::RegisterDecoratorInstancer("defender", &decorator_defender);
  82. // Construct the game singletons.
  83. HighScores::Initialise(context);
  84. // Fire off the startup script.
  85. LuaInterface::Initialise(Rml::Lua::Interpreter::GetLuaState()); // the tables/functions defined in the samples
  86. Rml::Lua::Interpreter::LoadFile(Rml::String("lua_invaders/lua/start.lua"));
  87. bool running = true;
  88. while (running)
  89. {
  90. running = Backend::ProcessEvents(context, &Shell::ProcessKeyDownShortcuts);
  91. context->Update();
  92. Backend::BeginFrame();
  93. context->Render();
  94. Backend::PresentFrame();
  95. }
  96. // Shut down the game singletons.
  97. HighScores::Shutdown();
  98. // Shutdown RmlUi.
  99. Rml::Shutdown();
  100. Backend::Shutdown();
  101. Shell::Shutdown();
  102. return 0;
  103. }