main.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 "ox/oxygine.hpp"
  8. #include "ox/Stage.hpp"
  9. #include "ox/DebugActor.hpp"
  10. #include "example.h"
  11. using namespace oxygine;
  12. // This function is called each frame
  13. int mainloop()
  14. {
  15. // Update engine-internal components
  16. // If input events are available, they are passed to Stage::instance.handleEvent
  17. // If the function returns true, it means that the user requested the application to terminate
  18. bool done = core::update();
  19. // It gets passed to our example game implementation
  20. example_update();
  21. // Update our stage
  22. // Update all actors. Actor::update will also be called for all its children
  23. getStage()->update();
  24. if (core::beginRendering())
  25. {
  26. Color clearColor(32, 32, 32, 255);
  27. Rect viewport(Point(0, 0), core::getDisplaySize());
  28. // Render all actors inside the stage. Actor::render will also be called for all its children
  29. getStage()->render(clearColor, viewport);
  30. core::swapDisplayBuffers();
  31. }
  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();
  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. #if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
  65. // On iPhone mainloop is called automatically by CADisplayLink, see int main() below
  66. //return;
  67. #endif
  68. // This is the main game loop.
  69. while (1)
  70. {
  71. int done = mainloop();
  72. if (done)
  73. break;
  74. }
  75. /*
  76. If we get here, the user has requested the Application to terminate.
  77. We dump and log all our created objects that have not been freed yet
  78. */
  79. ObjectBase::dumpCreatedObjects();
  80. /*
  81. Let's clean up everything right now and call ObjectBase::dumpObjects() again.
  82. We need to free all allocated resources and delete all created actors.
  83. All actors/sprites are smart-pointer objects and don't need to be removed by hand.
  84. But now we want to delete it by hand.
  85. */
  86. // See example.cpp for the shutdown function implementation
  87. example_destroy();
  88. //renderer.cleanup();
  89. // Releases all internal components and the stage
  90. core::release();
  91. // The dump list should be empty by now,
  92. // we want to make sure that there aren't any memory leaks, so we call it again.
  93. ObjectBase::dumpCreatedObjects();
  94. ObjectBase::__stopTracingLeaks();
  95. //end
  96. }
  97. #ifdef __S3E__
  98. int main(int argc, char* argv[])
  99. {
  100. run();
  101. return 0;
  102. }
  103. #endif
  104. #ifdef OXYGINE_SDL
  105. #include "SDL_main.h"
  106. #include "SDL.h"
  107. extern "C"
  108. {
  109. void one(void* param) { mainloop(); }
  110. void oneEmsc() { mainloop(); }
  111. int main(int argc, char* argv[])
  112. {
  113. run();
  114. #if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
  115. // If parameter 2 is set to 1, refresh rate will be 60 fps, 2 - 30 fps, 3 - 15 fps.
  116. //SDL_iPhoneSetAnimationCallback(core::getWindow(), 1, one, nullptr);
  117. #endif
  118. #if EMSCRIPTEN
  119. emscripten_set_main_loop(oneEmsc, 0, 0);
  120. #endif
  121. return 0;
  122. }
  123. };
  124. #endif