main.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 "PythonInterface.h"
  38. Rocket::Core::Context* context = NULL;
  39. void DoAllocConsole();
  40. ShellRenderInterfaceExtensions *shell_renderer;
  41. void GameLoop()
  42. {
  43. context->Update();
  44. shell_renderer->PrepareRenderBuffer();
  45. context->Render();
  46. shell_renderer->PresentRenderBuffer();
  47. }
  48. #if defined ROCKET_PLATFORM_WIN32
  49. #include <windows.h>
  50. int APIENTRY WinMain(HINSTANCE ROCKET_UNUSED_PARAMETER(instance_handle), HINSTANCE ROCKET_UNUSED_PARAMETER(previous_instance_handle), char* ROCKET_UNUSED_PARAMETER(command_line), int ROCKET_UNUSED_PARAMETER(command_show))
  51. #else
  52. int main(int ROCKET_UNUSED_PARAMETER(argc), char** ROCKET_UNUSED_PARAMETER(argv))
  53. #endif
  54. {
  55. #ifdef ROCKET_PLATFORM_WIN32
  56. ROCKET_UNUSED(instance_handle);
  57. ROCKET_UNUSED(previous_instance_handle);
  58. ROCKET_UNUSED(command_line);
  59. ROCKET_UNUSED(command_show);
  60. #else
  61. ROCKET_UNUSED(argc);
  62. ROCKET_UNUSED(argv);
  63. #endif
  64. #ifdef ROCKET_PLATFORM_MACOSX
  65. #define APP_PATH "../"
  66. #define ROCKET_PATH "../../bin/"
  67. #else
  68. #define APP_PATH "../Samples/pyinvaders/"
  69. #define ROCKET_PATH "."
  70. #endif
  71. #ifdef ROCKET_PLATFORM_WIN32
  72. DoAllocConsole();
  73. #endif
  74. ShellRenderInterfaceOpenGL opengl_renderer;
  75. shell_renderer = &opengl_renderer;
  76. // Generic OS initialisation, creates a window and attaches OpenGL.
  77. if (!Shell::Initialise("../Samples/pyinvaders/") ||
  78. !Shell::OpenWindow("Rocket Invaders from Mars (Python Powered)", shell_renderer, 1024, 768, false))
  79. {
  80. Shell::Shutdown();
  81. return -1;
  82. }
  83. // Rocket initialisation.
  84. Rocket::Core::SetRenderInterface(&opengl_renderer);
  85. opengl_renderer.SetViewport(1024,768);
  86. ShellSystemInterface system_interface;
  87. Rocket::Core::SetSystemInterface(&system_interface);
  88. Rocket::Core::Initialise();
  89. // Initialise the Rocket Controls library.
  90. Rocket::Controls::Initialise();
  91. // Initialise the Python interface.
  92. PythonInterface::Initialise((Shell::GetExecutablePath() + (APP_PATH "python") + PATH_SEPARATOR + Shell::GetExecutablePath() + ROCKET_PATH).CString());
  93. // Create the main Rocket context and set it on the shell's input layer.
  94. context = Rocket::Core::CreateContext("main", Rocket::Core::Vector2i(1024, 768));
  95. if (context == NULL)
  96. {
  97. Rocket::Core::Shutdown();
  98. Shell::Shutdown();
  99. return -1;
  100. }
  101. Rocket::Debugger::Initialise(context);
  102. Input::SetContext(context);
  103. // Load the font faces required for Invaders.
  104. Shell::LoadFonts("../assets/");
  105. // Register Invader's custom decorator instancers.
  106. Rocket::Core::DecoratorInstancer* decorator_instancer = new DecoratorInstancerStarfield();
  107. Rocket::Core::Factory::RegisterDecoratorInstancer("starfield", decorator_instancer);
  108. decorator_instancer->RemoveReference();
  109. decorator_instancer = new DecoratorInstancerDefender();
  110. Rocket::Core::Factory::RegisterDecoratorInstancer("defender", decorator_instancer);
  111. decorator_instancer->RemoveReference();
  112. // Construct the game singletons.
  113. HighScores::Initialise();
  114. // Fire off the startup script.
  115. PythonInterface::Import("autoexec");
  116. Shell::EventLoop(GameLoop);
  117. // Shutdown the Rocket contexts.
  118. context->RemoveReference();
  119. // Shutdown Python before we shut down Rocket.
  120. PythonInterface::Shutdown();
  121. // Shut down the game singletons.
  122. HighScores::Shutdown();
  123. // Shutdown Rocket.
  124. Rocket::Core::Shutdown();
  125. Shell::CloseWindow();
  126. Shell::Shutdown();
  127. return 0;
  128. }
  129. #ifdef ROCKET_PLATFORM_WIN32
  130. #include <windows.h>
  131. #include <fcntl.h>
  132. #include <io.h>
  133. #include <process.h>
  134. void DoAllocConsole()
  135. {
  136. static const WORD MAX_CONSOLE_LINES = 500;
  137. int hConHandle;
  138. long lStdHandle;
  139. CONSOLE_SCREEN_BUFFER_INFO coninfo;
  140. FILE *fp;
  141. // allocate a console for this app
  142. AllocConsole();
  143. // set the screen buffer to be big enough to let us scroll text
  144. GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
  145. coninfo.dwSize.Y = MAX_CONSOLE_LINES;
  146. SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);
  147. // redirect unbuffered STDOUT to the console
  148. lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
  149. hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
  150. fp = _fdopen( hConHandle, "w" );
  151. *stdout = *fp;
  152. setvbuf( stdout, NULL, _IONBF, 0 );
  153. // redirect unbuffered STDIN to the console
  154. lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);
  155. hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
  156. fp = _fdopen( hConHandle, "r" );
  157. *stdin = *fp;
  158. setvbuf( stdin, NULL, _IONBF, 0 );
  159. // redirect unbuffered STDERR to the console
  160. lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);
  161. hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
  162. fp = _fdopen( hConHandle, "w" );
  163. *stderr = *fp;
  164. setvbuf( stderr, NULL, _IONBF, 0 );
  165. ShowWindow(GetConsoleWindow(), SW_SHOW);
  166. }
  167. #endif