entry_point.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 <stdio.h>
  8. #include "core/Renderer.h"
  9. #include "RootActor.h"
  10. #include "DebugActor.h"
  11. #include "example.h"
  12. using namespace oxygine;
  13. Renderer renderer;
  14. Rect viewport;
  15. class ExampleRootActor: public RootActor
  16. {
  17. public:
  18. ExampleRootActor()
  19. {
  20. //each mobile application should handle focus lost
  21. //and free/restore GPU resources
  22. addEventListener(RootActor::DEACTIVATE, CLOSURE(this, &ExampleRootActor::onDeactivate));
  23. addEventListener(RootActor::ACTIVATE, CLOSURE(this, &ExampleRootActor::onActivate));
  24. }
  25. void onDeactivate(Event *)
  26. {
  27. core::reset();
  28. }
  29. void onActivate(Event *)
  30. {
  31. core::restore();
  32. }
  33. };
  34. //called each frame
  35. int mainloop()
  36. {
  37. example_update();
  38. //update our rootActor
  39. //Actor::update would be called also for children
  40. getRoot()->update();
  41. Color clear(33, 33, 33, 255);
  42. //start rendering and clear viewport
  43. if (renderer.begin(0, viewport, &clear))
  44. {
  45. //begin rendering from RootActor.
  46. getRoot()->render(renderer);
  47. //rendering done
  48. renderer.end();
  49. core::swapDisplayBuffers();
  50. }
  51. //update internal components
  52. //all input events would be passed to RootActor::instance.handleEvent
  53. //if done is true then User requests quit from app.
  54. bool done = core::update();
  55. return done ? 1 : 0;
  56. }
  57. //it is application entry point
  58. void run()
  59. {
  60. //initialize oxygine's internal stuff
  61. core::init_desc desc;
  62. #if OXYGINE_SDL
  63. //we could setup initial window size on SDL builds
  64. //desc.w = 960;
  65. //desc.h = 660;
  66. //marmalade settings could be changed from emulator's menu
  67. #endif
  68. core::init(&desc);
  69. example_preinit();
  70. //create RootActor. RootActor is a root node
  71. RootActor::instance = new ExampleRootActor();
  72. Point size = core::getDisplaySize();
  73. getRoot()->init(size, size);
  74. //DebugActor is a helper node it shows FPS and memory usage and other useful stuff
  75. DebugActor::initialize();
  76. //create and add new DebugActor to root actor as child
  77. getRoot()->addChild(new DebugActor());
  78. Matrix view = makeViewMatrix(size.x, size.y);
  79. viewport = Rect(0, 0, size.x, size.y);
  80. Matrix proj;
  81. //initialize projection matrix
  82. Matrix::orthoLH(proj, (float)size.x, (float)size.y, 0, 1);
  83. //Renderer is class helper for rendering primitives and batching them
  84. //Renderer is lightweight class you could create it many of times
  85. renderer.setDriver(IVideoDriver::instance);
  86. //initialization view and projection matrix
  87. //where Left Top corner is (0, 0), and right bottom is (width, height)
  88. renderer.initCoordinateSystem(size.x, size.y);
  89. //initialize this example stuff. see example.cpp
  90. example_init();
  91. bool done = false;
  92. //here is main game loop
  93. while (1)
  94. {
  95. int done = mainloop();
  96. if (done)
  97. break;
  98. }
  99. //so user want to leave application...
  100. //lets dump all created objects into log
  101. //all created and not freed resources would be displayed
  102. ObjectBase::dumpCreatedObjects();
  103. //lets cleanup everything right now and call ObjectBase::dumpObjects() again
  104. //we need to free all allocated resources and delete all created actors
  105. //all actors/sprites are smart pointer objects and actually you don't need it remove them by hands
  106. //but now we want delete it by hands
  107. //check example.cpp
  108. example_destroy();
  109. renderer.cleanup();
  110. /**releases all internal components and RootActor*/
  111. core::release();
  112. //dump list should be empty now
  113. //we deleted everything and could be sure that there aren't any memory leaks
  114. ObjectBase::dumpCreatedObjects();
  115. //end
  116. }
  117. #ifdef __S3E__
  118. int main(int argc, char* argv[])
  119. {
  120. run();
  121. return 0;
  122. }
  123. #endif
  124. #ifdef OXYGINE_SDL
  125. #include "SDL_main.h"
  126. extern "C"
  127. {
  128. int main(int argc, char* argv[])
  129. {
  130. run();
  131. return 0;
  132. }
  133. };
  134. #endif
  135. #ifdef __FLASHPLAYER__
  136. int main(int argc, char* argv[])
  137. {
  138. printf("test\n");
  139. run();
  140. return 0;
  141. }
  142. #endif