main.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #define _WIN32_WINNT 0x0500
  28. #include <Rocket/Core.h>
  29. #include <Rocket/Controls.h>
  30. #include <Rocket/Debugger.h>
  31. #include <Input.h>
  32. #include <Shell.h>
  33. #include "DecoratorInstancerDefender.h"
  34. #include "DecoratorInstancerStarfield.h"
  35. #include "ElementGame.h"
  36. #include "HighScores.h"
  37. #include <Rocket/Core/Lua/Interpreter.h>
  38. #include <Rocket/Controls/Lua/Controls.h>
  39. #include "LuaInterface.h"
  40. Rocket::Core::Context* context = NULL;
  41. void DoAllocConsole();
  42. ShellRenderInterfaceExtensions *shell_renderer;
  43. void GameLoop()
  44. {
  45. context->Update();
  46. shell_renderer->PrepareRenderBuffer();
  47. context->Render();
  48. shell_renderer->PresentRenderBuffer();
  49. }
  50. #if defined ROCKET_PLATFORM_WIN32
  51. #include <windows.h>
  52. int APIENTRY WinMain(HINSTANCE, HINSTANCE, char*, int)
  53. #else
  54. int main(int, char**)
  55. #endif
  56. {
  57. #ifdef ROCKET_PLATFORM_LINUX
  58. #define APP_PATH "../Samples/luainvaders/"
  59. #else
  60. #define APP_PATH "../../Samples/luainvaders/"
  61. #endif
  62. #ifdef ROCKET_PLATFORM_WIN32
  63. DoAllocConsole();
  64. #endif
  65. int window_width = 1024;
  66. int window_height = 768;
  67. ShellRenderInterfaceOpenGL opengl_renderer;
  68. shell_renderer = &opengl_renderer;
  69. // Generic OS initialisation, creates a window and attaches OpenGL.
  70. if (!Shell::Initialise(APP_PATH) ||
  71. !Shell::OpenWindow("Rocket Invaders from Mars (Lua Powered)", shell_renderer, window_width, window_height, false))
  72. {
  73. Shell::Shutdown();
  74. return -1;
  75. }
  76. // Rocket initialisation.
  77. Rocket::Core::SetRenderInterface(&opengl_renderer);
  78. opengl_renderer.SetViewport(window_width, window_height);
  79. ShellSystemInterface system_interface;
  80. Rocket::Core::SetSystemInterface(&system_interface);
  81. Rocket::Core::Initialise();
  82. // Initialise the Rocket Controls library.
  83. Rocket::Controls::Initialise();
  84. // Initialise the Lua interface
  85. Rocket::Core::Lua::Interpreter::Initialise();
  86. Rocket::Controls::Lua::RegisterTypes(Rocket::Core::Lua::Interpreter::GetLuaState());
  87. // Create the main Rocket context and set it on the shell's input layer.
  88. context = Rocket::Core::CreateContext("main", Rocket::Core::Vector2i(window_width, window_height));
  89. if (context == NULL)
  90. {
  91. Rocket::Core::Shutdown();
  92. Shell::Shutdown();
  93. return -1;
  94. }
  95. Rocket::Debugger::Initialise(context);
  96. Input::SetContext(context);
  97. shell_renderer->SetContext(context);
  98. // Load the font faces required for Invaders.
  99. Shell::LoadFonts("../assets/");
  100. // Register Invader's custom decorator instancers.
  101. Rocket::Core::DecoratorInstancer* decorator_instancer = new DecoratorInstancerStarfield();
  102. Rocket::Core::Factory::RegisterDecoratorInstancer("starfield", decorator_instancer);
  103. decorator_instancer->RemoveReference();
  104. decorator_instancer = new DecoratorInstancerDefender();
  105. Rocket::Core::Factory::RegisterDecoratorInstancer("defender", decorator_instancer);
  106. decorator_instancer->RemoveReference();
  107. // Construct the game singletons.
  108. HighScores::Initialise();
  109. // Fire off the startup script.
  110. LuaInterface::Initialise(Rocket::Core::Lua::Interpreter::GetLuaState()); //the tables/functions defined in the samples
  111. Rocket::Core::Lua::Interpreter::LoadFile(Rocket::Core::String("lua/start.lua"));
  112. Shell::EventLoop(GameLoop);
  113. // Shutdown the Rocket contexts.
  114. context->RemoveReference();
  115. // Shutdown Lua before we shut down Rocket.
  116. Rocket::Core::Lua::Interpreter::Shutdown();
  117. // Shut down the game singletons.
  118. HighScores::Shutdown();
  119. // Shutdown Rocket.
  120. Rocket::Core::Shutdown();
  121. Shell::CloseWindow();
  122. Shell::Shutdown();
  123. return 0;
  124. }
  125. #ifdef ROCKET_PLATFORM_WIN32
  126. #include <windows.h>
  127. #include <fcntl.h>
  128. #include <io.h>
  129. #include <process.h>
  130. void DoAllocConsole()
  131. {
  132. static const WORD MAX_CONSOLE_LINES = 500;
  133. int hConHandle;
  134. long lStdHandle;
  135. CONSOLE_SCREEN_BUFFER_INFO coninfo;
  136. FILE *fp;
  137. // allocate a console for this app
  138. AllocConsole();
  139. // set the screen buffer to be big enough to let us scroll text
  140. GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
  141. coninfo.dwSize.Y = MAX_CONSOLE_LINES;
  142. SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);
  143. // redirect unbuffered STDOUT to the console
  144. lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
  145. hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
  146. fp = _fdopen( hConHandle, "w" );
  147. *stdout = *fp;
  148. setvbuf( stdout, NULL, _IONBF, 0 );
  149. // redirect unbuffered STDIN to the console
  150. lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);
  151. hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
  152. fp = _fdopen( hConHandle, "r" );
  153. *stdin = *fp;
  154. setvbuf( stdin, NULL, _IONBF, 0 );
  155. // redirect unbuffered STDERR to the console
  156. lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);
  157. hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
  158. fp = _fdopen( hConHandle, "w" );
  159. *stderr = *fp;
  160. setvbuf( stderr, NULL, _IONBF, 0 );
  161. ShowWindow(GetConsoleWindow(), SW_SHOW);
  162. }
  163. #endif