main.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #include <Rocket/Core.h>
  28. #include <Rocket/Controls.h>
  29. #include <Rocket/Debugger.h>
  30. #include <Shell.h>
  31. #include <Input.h>
  32. #include "DecoratorInstancerDefender.h"
  33. #include "DecoratorInstancerStarfield.h"
  34. #include "ElementGame.h"
  35. #include "EventHandlerHighScore.h"
  36. #include "EventHandlerOptions.h"
  37. #include "EventHandlerStartGame.h"
  38. #include "EventInstancer.h"
  39. #include "EventManager.h"
  40. #include "HighScores.h"
  41. #include "HighScoresNameFormatter.h"
  42. #include "HighScoresShipFormatter.h"
  43. Rocket::Core::Context* context = NULL;
  44. ShellRenderInterfaceExtensions *shell_renderer;
  45. void GameLoop()
  46. {
  47. context->Update();
  48. shell_renderer->PrepareRenderBuffer();
  49. context->Render();
  50. shell_renderer->PresentRenderBuffer();
  51. }
  52. #if defined ROCKET_PLATFORM_WIN32
  53. #include <windows.h>
  54. 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))
  55. #else
  56. int main(int ROCKET_UNUSED_PARAMETER(argc), char** ROCKET_UNUSED_PARAMETER(argv))
  57. #endif
  58. {
  59. #ifdef ROCKET_PLATFORM_WIN32
  60. ROCKET_UNUSED(instance_handle);
  61. ROCKET_UNUSED(previous_instance_handle);
  62. ROCKET_UNUSED(command_line);
  63. ROCKET_UNUSED(command_show);
  64. #else
  65. ROCKET_UNUSED(argc);
  66. ROCKET_UNUSED(argv);
  67. #endif
  68. #ifdef ROCKET_PLATFORM_LINUX
  69. #define APP_PATH "../Samples/invaders/"
  70. #else
  71. #define APP_PATH "../../Samples/invaders/"
  72. #endif
  73. #ifdef ROCKET_PLATFORM_WIN32
  74. AllocConsole();
  75. #endif
  76. int window_width = 1024;
  77. int window_height = 768;
  78. ShellRenderInterfaceOpenGL opengl_renderer;
  79. shell_renderer = &opengl_renderer;
  80. // Generic OS initialisation, creates a window and attaches OpenGL.
  81. if (!Shell::Initialise(APP_PATH) ||
  82. !Shell::OpenWindow("Rocket Invaders from Mars", shell_renderer, window_width, window_height, false))
  83. {
  84. Shell::Shutdown();
  85. return -1;
  86. }
  87. // Rocket initialisation.
  88. Rocket::Core::SetRenderInterface(&opengl_renderer);
  89. opengl_renderer.SetViewport(window_width, window_height);
  90. ShellSystemInterface system_interface;
  91. Rocket::Core::SetSystemInterface(&system_interface);
  92. Rocket::Core::Initialise();
  93. // Initialise the Rocket Controls library.
  94. Rocket::Controls::Initialise();
  95. // Create the main Rocket context and set it on the shell's input layer.
  96. context = Rocket::Core::CreateContext("main", Rocket::Core::Vector2i(window_width, window_height));
  97. if (context == NULL)
  98. {
  99. Rocket::Core::Shutdown();
  100. Shell::Shutdown();
  101. return -1;
  102. }
  103. // Initialise the Rocket debugger.
  104. Rocket::Debugger::Initialise(context);
  105. Input::SetContext(context);
  106. shell_renderer->SetContext(context);
  107. // Load the font faces required for Invaders.
  108. Shell::LoadFonts("../assets/");
  109. // Register Invader's custom element and decorator instancers.
  110. Rocket::Core::ElementInstancer* element_instancer = new Rocket::Core::ElementInstancerGeneric< ElementGame >();
  111. Rocket::Core::Factory::RegisterElementInstancer("game", element_instancer);
  112. element_instancer->RemoveReference();
  113. Rocket::Core::DecoratorInstancer* decorator_instancer = new DecoratorInstancerStarfield();
  114. Rocket::Core::Factory::RegisterDecoratorInstancer("starfield", decorator_instancer);
  115. decorator_instancer->RemoveReference();
  116. decorator_instancer = new DecoratorInstancerDefender();
  117. Rocket::Core::Factory::RegisterDecoratorInstancer("defender", decorator_instancer);
  118. decorator_instancer->RemoveReference();
  119. // Register Invader's data formatters
  120. HighScoresNameFormatter name_formatter;
  121. HighScoresShipFormatter ship_formatter;
  122. // Construct the game singletons.
  123. HighScores::Initialise();
  124. // Initialise the event instancer and handlers.
  125. EventInstancer* event_instancer = new EventInstancer();
  126. Rocket::Core::Factory::RegisterEventListenerInstancer(event_instancer);
  127. event_instancer->RemoveReference();
  128. EventManager::RegisterEventHandler("start_game", new EventHandlerStartGame());
  129. EventManager::RegisterEventHandler("high_score", new EventHandlerHighScore());
  130. EventManager::RegisterEventHandler("options", new EventHandlerOptions());
  131. // Start the game.
  132. if (EventManager::LoadWindow("background") &&
  133. EventManager::LoadWindow("main_menu"))
  134. Shell::EventLoop(GameLoop);
  135. // Shut down the game singletons.
  136. HighScores::Shutdown();
  137. // Release the event handlers.
  138. EventManager::Shutdown();
  139. // Shutdown Rocket.
  140. context->RemoveReference();
  141. Rocket::Core::Shutdown();
  142. Shell::CloseWindow();
  143. Shell::Shutdown();
  144. return 0;
  145. }