main.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. shell_renderer->SetContext(context);
  104. // Load the font faces required for Invaders.
  105. Shell::LoadFonts("../assets/");
  106. // Register Invader's custom decorator instancers.
  107. Rocket::Core::DecoratorInstancer* decorator_instancer = new DecoratorInstancerStarfield();
  108. Rocket::Core::Factory::RegisterDecoratorInstancer("starfield", decorator_instancer);
  109. decorator_instancer->RemoveReference();
  110. decorator_instancer = new DecoratorInstancerDefender();
  111. Rocket::Core::Factory::RegisterDecoratorInstancer("defender", decorator_instancer);
  112. decorator_instancer->RemoveReference();
  113. // Construct the game singletons.
  114. HighScores::Initialise();
  115. // Fire off the startup script.
  116. PythonInterface::Import("autoexec");
  117. Shell::EventLoop(GameLoop);
  118. // Shutdown the Rocket contexts.
  119. context->RemoveReference();
  120. // Shutdown Python before we shut down Rocket.
  121. PythonInterface::Shutdown();
  122. // Shut down the game singletons.
  123. HighScores::Shutdown();
  124. // Shutdown Rocket.
  125. Rocket::Core::Shutdown();
  126. Shell::CloseWindow();
  127. Shell::Shutdown();
  128. return 0;
  129. }
  130. #ifdef ROCKET_PLATFORM_WIN32
  131. #include <windows.h>
  132. #include <fcntl.h>
  133. #include <io.h>
  134. #include <process.h>
  135. void DoAllocConsole()
  136. {
  137. static const WORD MAX_CONSOLE_LINES = 500;
  138. int hConHandle;
  139. long lStdHandle;
  140. CONSOLE_SCREEN_BUFFER_INFO coninfo;
  141. FILE *fp;
  142. // allocate a console for this app
  143. AllocConsole();
  144. // set the screen buffer to be big enough to let us scroll text
  145. GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
  146. coninfo.dwSize.Y = MAX_CONSOLE_LINES;
  147. SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);
  148. // redirect unbuffered STDOUT to the console
  149. lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
  150. hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
  151. fp = _fdopen( hConHandle, "w" );
  152. *stdout = *fp;
  153. setvbuf( stdout, NULL, _IONBF, 0 );
  154. // redirect unbuffered STDIN to the console
  155. lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);
  156. hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
  157. fp = _fdopen( hConHandle, "r" );
  158. *stdin = *fp;
  159. setvbuf( stdin, NULL, _IONBF, 0 );
  160. // redirect unbuffered STDERR to the console
  161. lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);
  162. hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
  163. fp = _fdopen( hConHandle, "w" );
  164. *stderr = *fp;
  165. setvbuf( stderr, NULL, _IONBF, 0 );
  166. ShowWindow(GetConsoleWindow(), SW_SHOW);
  167. }
  168. #endif