main.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. Attention!
  3. This file initializes the Oxygine engine.
  4. If you just started here and don't understand the code completely, feel free to come back later.
  5. You can start from example.cpp and example.h, which main functions are called from here.
  6. */
  7. #include "core/oxygine.h"
  8. #include "Stage.h"
  9. #include "DebugActor.h"
  10. #include "example.h"
  11. using namespace oxygine;
  12. // This function is called each frame
  13. int mainloop()
  14. {
  15. // It gets passed to our example game implementation
  16. example_update();
  17. // Update our stage
  18. // Update all actors. Actor::update will also be called for all its children
  19. getStage()->update();
  20. if (core::beginRendering())
  21. {
  22. Color clearColor(32, 32, 32, 255);
  23. Rect viewport(Point(0, 0), core::getDisplaySize());
  24. // Render all actors inside the stage. Actor::render will also be called for all its children
  25. getStage()->render(clearColor, viewport);
  26. core::swapDisplayBuffers();
  27. }
  28. // Update engine-internal components
  29. // If input events are available, they are passed to Stage::instance.handleEvent
  30. // If the function returns true, it means that the user requested the application to terminate
  31. bool done = core::update();
  32. return done ? 1 : 0;
  33. }
  34. // Application entry point
  35. void run()
  36. {
  37. ObjectBase::__startTracingLeaks();
  38. // Initialize Oxygine's internal stuff
  39. core::init_desc desc;
  40. desc.title = "Oxygine Application";
  41. #if OXYGINE_SDL || OXYGINE_EMSCRIPTEN
  42. // The initial window size can be set up here on SDL builds
  43. desc.w = 960;
  44. desc.h = 640;
  45. // Marmalade settings can be modified from the emulator's menu
  46. #endif
  47. example_preinit();
  48. core::init(&desc);
  49. // Create the stage. Stage is a root node for all updateable and drawable objects
  50. Stage::instance = new Stage(true);
  51. Point size = core::getDisplaySize();
  52. getStage()->setSize(size);
  53. // DebugActor is a helper actor node. It shows FPS, memory usage and other useful stuff
  54. DebugActor::show();
  55. // Initializes our example game. See example.cpp
  56. example_init();
  57. #ifdef EMSCRIPTEN
  58. /*
  59. If you build for Emscripten, mainloop is called automatically and shouldn't be called here.
  60. See emscripten_set_main_loop in the EMSCRIPTEN section below
  61. */
  62. return;
  63. #endif
  64. // This is the main game loop.
  65. while (1)
  66. {
  67. int done = mainloop();
  68. if (done)
  69. break;
  70. }
  71. /*
  72. If we get here, the user has requested the Application to terminate.
  73. We dump and log all our created objects that have not been freed yet
  74. */
  75. ObjectBase::dumpCreatedObjects();
  76. /*
  77. Let's clean up everything right now and call ObjectBase::dumpObjects() again.
  78. We need to free all allocated resources and delete all created actors.
  79. All actors/sprites are smart-pointer objects and don't need to be removed by hand.
  80. But now we want to delete it by hand.
  81. */
  82. // See example.cpp for the shutdown function implementation
  83. example_destroy();
  84. //renderer.cleanup();
  85. // Releases all internal components and the stage
  86. core::release();
  87. // The dump list should be empty by now,
  88. // we want to make sure that there aren't any memory leaks, so we call it again.
  89. ObjectBase::dumpCreatedObjects();
  90. ObjectBase::__stopTracingLeaks();
  91. //end
  92. }
  93. #ifdef __S3E__
  94. int main(int argc, char* argv[])
  95. {
  96. run();
  97. return 0;
  98. }
  99. #endif
  100. #ifdef OXYGINE_SDL
  101. #include "SDL_main.h"
  102. extern "C"
  103. {
  104. int main(int argc, char* argv[])
  105. {
  106. run();
  107. return 0;
  108. }
  109. };
  110. #endif
  111. #ifdef EMSCRIPTEN
  112. #include <emscripten.h>
  113. void one() { mainloop(); }
  114. int main(int argc, char* argv[])
  115. {
  116. run();
  117. emscripten_set_main_loop(one, 0, 0);
  118. return 0;
  119. }
  120. #endif