entry_point.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. Attention!
  3. This file has Oxygine initialization stuff.
  4. If you just started you don't need to understand it exactly you could check it later.
  5. You could start from example.cpp and example.h it has main functions being called from there
  6. */
  7. #include "core/oxygine.h"
  8. #include "Stage.h"
  9. #include "DebugActor.h"
  10. #include "example.h"
  11. using namespace oxygine;
  12. //called each frame
  13. int mainloop()
  14. {
  15. example_update();
  16. //update our stage
  17. //update all actors. Actor::update would be called also for all children
  18. getStage()->update();
  19. if (core::beginRendering())
  20. {
  21. Color clearColor(32, 32, 32, 255);
  22. Rect viewport(Point(0, 0), core::getDisplaySize());
  23. //render all actors. Actor::render would be called also for all children
  24. getStage()->render(clearColor, viewport);
  25. core::swapDisplayBuffers();
  26. }
  27. //update internal components
  28. //all input events would be passed to Stage::instance.handleEvent
  29. //if done is true then User requests quit from app.
  30. bool done = core::update();
  31. return done ? 1 : 0;
  32. }
  33. //it is application entry point
  34. void run()
  35. {
  36. ObjectBase::__startTracingLeaks();
  37. //initialize Oxygine's internal stuff
  38. core::init_desc desc;
  39. desc.title = "Oxygine Application";
  40. #if OXYGINE_SDL || OXYGINE_EMSCRIPTEN
  41. //we could setup initial window size on SDL builds
  42. desc.w = 960;
  43. desc.h = 640;
  44. //marmalade settings could be changed from emulator's menu
  45. #endif
  46. example_preinit();
  47. core::init(&desc);
  48. //create Stage. Stage is a root node
  49. Stage::instance = new Stage(true);
  50. Point size = core::getDisplaySize();
  51. getStage()->setSize(size);
  52. //DebugActor is a helper actor node. It shows FPS, memory usage and other useful stuff
  53. DebugActor::show();
  54. //initialize this example stuff. see example.cpp
  55. example_init();
  56. #ifdef EMSCRIPTEN
  57. /*
  58. if you build for Emscripten mainloop would be called automatically outside.
  59. see emscripten_set_main_loop below
  60. */
  61. return;
  62. #endif
  63. //here is main game loop
  64. while (1)
  65. {
  66. int done = mainloop();
  67. if (done)
  68. break;
  69. }
  70. //user wants to leave application...
  71. //lets dump all created objects into log
  72. //all created and not freed resources would be displayed
  73. ObjectBase::dumpCreatedObjects();
  74. //lets cleanup everything right now and call ObjectBase::dumpObjects() again
  75. //we need to free all allocated resources and delete all created actors
  76. //all actors/sprites are smart pointer objects and actually you don't need it remove them by hands
  77. //but now we want delete it by hands
  78. //check example.cpp
  79. example_destroy();
  80. //renderer.cleanup();
  81. /**releases all internal components and Stage*/
  82. core::release();
  83. //dump list should be empty now
  84. //we deleted everything and could be sure that there aren't any memory leaks
  85. ObjectBase::dumpCreatedObjects();
  86. ObjectBase::__stopTracingLeaks();
  87. //end
  88. }
  89. #ifdef __S3E__
  90. int main(int argc, char* argv[])
  91. {
  92. run();
  93. return 0;
  94. }
  95. #endif
  96. #ifdef OXYGINE_SDL
  97. #include "SDL_main.h"
  98. extern "C"
  99. {
  100. int main(int argc, char* argv[])
  101. {
  102. run();
  103. return 0;
  104. }
  105. };
  106. #endif
  107. #ifdef EMSCRIPTEN
  108. #include <emscripten.h>
  109. void one() { mainloop(); }
  110. int main(int argc, char* argv[])
  111. {
  112. run();
  113. emscripten_set_main_loop(one, 0, 0);
  114. return 0;
  115. }
  116. #endif