main.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. // The initial window size can be set up here on SDL builds, ignored on Mobile devices
  42. desc.w = 960;
  43. desc.h = 640;
  44. example_preinit();
  45. core::init(&desc);
  46. // Create the stage. Stage is a root node for all updateable and drawable objects
  47. Stage::instance = new Stage();
  48. Point size = core::getDisplaySize();
  49. getStage()->setSize(size);
  50. // DebugActor is a helper actor node. It shows FPS, memory usage and other useful stuff
  51. DebugActor::show();
  52. // Initializes our example game. See example.cpp
  53. example_init();
  54. #ifdef EMSCRIPTEN
  55. /*
  56. If you build for Emscripten, mainloop is called automatically and shouldn't be called here.
  57. See emscripten_set_main_loop in the EMSCRIPTEN section below
  58. */
  59. return;
  60. #endif
  61. #if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
  62. // On iPhone mainloop is called automatically by CADisplayLink, see int main() below
  63. //return;
  64. #endif
  65. // This is the main game loop.
  66. while (1)
  67. {
  68. int done = mainloop();
  69. if (done)
  70. break;
  71. }
  72. /*
  73. If we get here, the user has requested the Application to terminate.
  74. We dump and log all our created objects that have not been freed yet
  75. */
  76. ObjectBase::dumpCreatedObjects();
  77. /*
  78. Let's clean up everything right now and call ObjectBase::dumpObjects() again.
  79. We need to free all allocated resources and delete all created actors.
  80. All actors/sprites are smart-pointer objects and don't need to be removed by hand.
  81. But now we want to delete it by hand.
  82. */
  83. // See example.cpp for the shutdown function implementation
  84. example_destroy();
  85. //renderer.cleanup();
  86. // Releases all internal components and the stage
  87. core::release();
  88. // The dump list should be empty by now,
  89. // we want to make sure that there aren't any memory leaks, so we call it again.
  90. ObjectBase::dumpCreatedObjects();
  91. ObjectBase::__stopTracingLeaks();
  92. //end
  93. }
  94. #ifdef OXYGINE_SDL
  95. #include "SDL_main.h"
  96. #include "SDL.h"
  97. extern "C"
  98. {
  99. void one(void* param) { mainloop(); }
  100. void oneEmsc() { mainloop(); }
  101. int main(int argc, char* argv[])
  102. {
  103. run();
  104. #if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
  105. // If parameter 2 is set to 1, refresh rate will be 60 fps, 2 - 30 fps, 3 - 15 fps.
  106. //SDL_iPhoneSetAnimationCallback(core::getWindow(), 1, one, nullptr);
  107. #endif
  108. #if EMSCRIPTEN
  109. emscripten_set_main_loop(oneEmsc, 0, 0);
  110. #endif
  111. return 0;
  112. }
  113. };
  114. #endif