main.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 <RmlUi/Lua.h>
  31. #include <Input.h>
  32. #include <Shell.h>
  33. #include <ShellRenderInterfaceOpenGL.h>
  34. #include "DecoratorInstancerDefender.h"
  35. #include "DecoratorInstancerStarfield.h"
  36. #include "ElementGame.h"
  37. #include "HighScores.h"
  38. #include "LuaInterface.h"
  39. Rml::Context* context = nullptr;
  40. void DoAllocConsole();
  41. ShellRenderInterfaceExtensions *shell_renderer;
  42. void GameLoop()
  43. {
  44. context->Update();
  45. shell_renderer->PrepareRenderBuffer();
  46. context->Render();
  47. shell_renderer->PresentRenderBuffer();
  48. }
  49. #if defined RMLUI_PLATFORM_WIN32
  50. #include <windows.h>
  51. int APIENTRY WinMain(HINSTANCE, HINSTANCE, char*, int)
  52. #else
  53. int main(int, char**)
  54. #endif
  55. {
  56. #ifdef RMLUI_PLATFORM_WIN32
  57. DoAllocConsole();
  58. #endif
  59. int window_width = 1024;
  60. int window_height = 768;
  61. ShellRenderInterfaceOpenGL opengl_renderer;
  62. shell_renderer = &opengl_renderer;
  63. // Generic OS initialisation, creates a window and attaches OpenGL.
  64. if (!Shell::Initialise() ||
  65. !Shell::OpenWindow("RmlUi Invaders from Mars (Lua Powered)", shell_renderer, window_width, window_height, false))
  66. {
  67. Shell::Shutdown();
  68. return -1;
  69. }
  70. // RmlUi initialisation.
  71. Rml::SetRenderInterface(&opengl_renderer);
  72. opengl_renderer.SetViewport(window_width, window_height);
  73. ShellSystemInterface system_interface;
  74. Rml::SetSystemInterface(&system_interface);
  75. Rml::Initialise();
  76. // Initialise the Lua interface
  77. Rml::Lua::Initialise();
  78. // Create the main RmlUi context and set it on the shell's input layer.
  79. context = Rml::CreateContext("main", Rml::Vector2i(window_width, window_height));
  80. if (context == nullptr)
  81. {
  82. Rml::Shutdown();
  83. Shell::Shutdown();
  84. return -1;
  85. }
  86. Rml::Debugger::Initialise(context);
  87. Input::SetContext(context);
  88. Shell::SetContext(context);
  89. // Load the font faces required for Invaders.
  90. Shell::LoadFonts("assets/");
  91. // Register Invader's custom decorator instancers.
  92. DecoratorInstancerStarfield decorator_starfield;
  93. DecoratorInstancerDefender decorator_defender;
  94. Rml::Factory::RegisterDecoratorInstancer("starfield", &decorator_starfield);
  95. Rml::Factory::RegisterDecoratorInstancer("defender", &decorator_defender);
  96. // Construct the game singletons.
  97. HighScores::Initialise(context);
  98. // Fire off the startup script.
  99. LuaInterface::Initialise(Rml::Lua::Interpreter::GetLuaState()); //the tables/functions defined in the samples
  100. Rml::Lua::Interpreter::LoadFile(Rml::String("luainvaders/lua/start.lua"));
  101. Shell::EventLoop(GameLoop);
  102. // Shut down the game singletons.
  103. HighScores::Shutdown();
  104. // Shutdown RmlUi.
  105. Rml::Shutdown();
  106. Shell::CloseWindow();
  107. Shell::Shutdown();
  108. return 0;
  109. }
  110. #ifdef RMLUI_PLATFORM_WIN32
  111. #include <windows.h>
  112. #include <fcntl.h>
  113. #include <io.h>
  114. #include <process.h>
  115. void DoAllocConsole()
  116. {
  117. static const WORD MAX_CONSOLE_LINES = 500;
  118. int hConHandle;
  119. HANDLE lStdHandle;
  120. CONSOLE_SCREEN_BUFFER_INFO coninfo;
  121. FILE *fp;
  122. // allocate a console for this app
  123. AllocConsole();
  124. // set the screen buffer to be big enough to let us scroll text
  125. GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
  126. coninfo.dwSize.Y = MAX_CONSOLE_LINES;
  127. SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);
  128. // redirect unbuffered STDOUT to the console
  129. lStdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
  130. hConHandle = _open_osfhandle((intptr_t)lStdHandle, _O_TEXT);
  131. fp = _fdopen( hConHandle, "w" );
  132. *stdout = *fp;
  133. setvbuf( stdout, nullptr, _IONBF, 0 );
  134. // redirect unbuffered STDIN to the console
  135. lStdHandle = GetStdHandle(STD_INPUT_HANDLE);
  136. hConHandle = _open_osfhandle((intptr_t)lStdHandle, _O_TEXT);
  137. fp = _fdopen( hConHandle, "r" );
  138. *stdin = *fp;
  139. setvbuf( stdin, nullptr, _IONBF, 0 );
  140. // redirect unbuffered STDERR to the console
  141. lStdHandle = GetStdHandle(STD_ERROR_HANDLE);
  142. hConHandle = _open_osfhandle((intptr_t)lStdHandle, _O_TEXT);
  143. fp = _fdopen( hConHandle, "w" );
  144. *stderr = *fp;
  145. setvbuf( stderr, nullptr, _IONBF, 0 );
  146. ShowWindow(GetConsoleWindow(), SW_SHOW);
  147. }
  148. #endif