main.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. void GameLoop()
  45. {
  46. context->Update();
  47. glClear(GL_COLOR_BUFFER_BIT);
  48. context->Render();
  49. Shell::FlipBuffers();
  50. }
  51. #if defined ROCKET_PLATFORM_WIN32
  52. #include <windows.h>
  53. int APIENTRY WinMain(HINSTANCE, HINSTANCE, char*, int)
  54. #else
  55. int main(int, char**)
  56. #endif
  57. {
  58. // Generic OS initialisation, creates a window and attaches OpenGL.
  59. if (!Shell::Initialise("../Samples/invaders/") ||
  60. !Shell::OpenWindow("Rocket Invaders from Mars", true))
  61. {
  62. Shell::Shutdown();
  63. return -1;
  64. }
  65. // Rocket initialisation.
  66. ShellRenderInterfaceOpenGL opengl_renderer;
  67. Rocket::Core::SetRenderInterface(&opengl_renderer);
  68. opengl_renderer.SetViewport(1024,768);
  69. ShellSystemInterface system_interface;
  70. Rocket::Core::SetSystemInterface(&system_interface);
  71. Rocket::Core::Initialise();
  72. // Initialise the Rocket Controls library.
  73. Rocket::Controls::Initialise();
  74. // Create the main Rocket context and set it on the shell's input layer.
  75. context = Rocket::Core::CreateContext("main", Rocket::Core::Vector2i(1024, 768));
  76. if (context == NULL)
  77. {
  78. Rocket::Core::Shutdown();
  79. Shell::Shutdown();
  80. return -1;
  81. }
  82. // Initialise the Rocket debugger.
  83. Rocket::Debugger::Initialise(context);
  84. Input::SetContext(context);
  85. // Load the font faces required for Invaders.
  86. Shell::LoadFonts("../assets/");
  87. // Register Invader's custom element and decorator instancers.
  88. Rocket::Core::ElementInstancer* element_instancer = new Rocket::Core::ElementInstancerGeneric< ElementGame >();
  89. Rocket::Core::Factory::RegisterElementInstancer("game", element_instancer);
  90. element_instancer->RemoveReference();
  91. Rocket::Core::DecoratorInstancer* decorator_instancer = new DecoratorInstancerStarfield();
  92. Rocket::Core::Factory::RegisterDecoratorInstancer("starfield", decorator_instancer);
  93. decorator_instancer->RemoveReference();
  94. decorator_instancer = new DecoratorInstancerDefender();
  95. Rocket::Core::Factory::RegisterDecoratorInstancer("defender", decorator_instancer);
  96. decorator_instancer->RemoveReference();
  97. // Register Invader's data formatters
  98. HighScoresNameFormatter name_formatter;
  99. HighScoresShipFormatter ship_formatter;
  100. // Construct the game singletons.
  101. HighScores::Initialise();
  102. // Initialise the event instancer and handlers.
  103. EventInstancer* event_instancer = new EventInstancer();
  104. Rocket::Core::Factory::RegisterEventListenerInstancer(event_instancer);
  105. event_instancer->RemoveReference();
  106. EventManager::RegisterEventHandler("start_game", new EventHandlerStartGame());
  107. EventManager::RegisterEventHandler("high_score", new EventHandlerHighScore());
  108. EventManager::RegisterEventHandler("options", new EventHandlerOptions());
  109. // Start the game.
  110. if (EventManager::LoadWindow("background") &&
  111. EventManager::LoadWindow("main_menu"))
  112. Shell::EventLoop(GameLoop);
  113. // Shut down the game singletons.
  114. HighScores::Shutdown();
  115. // Release the event handlers.
  116. EventManager::Shutdown();
  117. // Shutdown Rocket.
  118. context->RemoveReference();
  119. Rocket::Core::Shutdown();
  120. Shell::CloseWindow();
  121. Shell::Shutdown();
  122. return 0;
  123. }