main.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #include <string>
  12. #include "ox/key.hpp"
  13. /*
  14. #pragma comment(lib,"Version.lib")
  15. #pragma comment(lib,"winmm.lib")
  16. #pragma comment(lib,"imm32.lib")
  17. #pragma comment(lib,"user32.lib")
  18. #pragma comment(lib,"gdi32.lib")
  19. #pragma comment(lib,"ole32.lib")
  20. #pragma comment(lib,"shell32.lib")
  21. #pragma comment(lib,"oleaut32.lib")
  22. */
  23. using namespace oxygine;
  24. using namespace std;
  25. // This function is called each frame
  26. int mainloop()
  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. // It gets passed to our example game implementation
  33. example_update();
  34. // Update our stage
  35. // Update all actors. Actor::update will also be called for all its children
  36. getStage()->update();
  37. if (core::beginRendering())
  38. {
  39. Color clearColor(32, 32, 32, 255);
  40. Rect viewport(Point(0, 0), core::getDisplaySize());
  41. // Render all actors inside the stage. Actor::render will also be called for all its children
  42. getStage()->render(clearColor, viewport);
  43. core::swapDisplayBuffers();
  44. }
  45. return done ? 1 : 0;
  46. }
  47. // Application entry point
  48. void run()
  49. {
  50. ObjectBase::__startTracingLeaks();
  51. // Initialize Oxygine's internal stuff
  52. core::init_desc desc;
  53. desc.title = "Oxygine After Effects Viewer";
  54. #if OXYGINE_SDL || OXYGINE_EMSCRIPTEN
  55. // The initial window size can be set up here on SDL builds
  56. desc.w = 1024;
  57. desc.h = 768;
  58. // Marmalade settings can be modified from the emulator's menu
  59. #endif
  60. example_preinit();
  61. core::init(&desc);
  62. // Create the stage. Stage is a root node for all updateable and drawable objects
  63. Stage::instance = new Stage(true);
  64. Point size = core::getDisplaySize();
  65. getStage()->setSize(size);
  66. // DebugActor is a helper actor node. It shows FPS, memory usage and other useful stuff
  67. DebugActor::show();
  68. // Initializes our example game. See example.cpp
  69. example_init();
  70. #ifdef EMSCRIPTEN
  71. /*
  72. If you build for Emscripten, mainloop is called automatically and shouldn't be called here.
  73. See emscripten_set_main_loop in the EMSCRIPTEN section below
  74. */
  75. return;
  76. #endif
  77. #if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
  78. // On iPhone mainloop is called automatically by CADisplayLink, see int main() below
  79. return;
  80. #endif
  81. // This is the main game loop.
  82. while (1)
  83. {
  84. int done = mainloop();
  85. if (done)
  86. break;
  87. }
  88. /*
  89. If we get here, the user has requested the Application to terminate.
  90. We dump and log all our created objects that have not been freed yet
  91. */
  92. ObjectBase::dumpCreatedObjects();
  93. /*
  94. Let's clean up everything right now and call ObjectBase::dumpObjects() again.
  95. We need to free all allocated resources and delete all created actors.
  96. All actors/sprites are smart-pointer objects and don't need to be removed by hand.
  97. But now we want to delete it by hand.
  98. */
  99. // See example.cpp for the shutdown function implementation
  100. example_destroy();
  101. //renderer.cleanup();
  102. // Releases all internal components and the stage
  103. core::release();
  104. // The dump list should be empty by now,
  105. // we want to make sure that there aren't any memory leaks, so we call it again.
  106. ObjectBase::dumpCreatedObjects();
  107. ObjectBase::__stopTracingLeaks();
  108. }
  109. #ifdef __S3E__
  110. int main(int argc, char* argv[])
  111. {
  112. run();
  113. return 0;
  114. }
  115. #endif
  116. #ifdef OXYGINE_SDL
  117. #include "SDL_main.h"
  118. #include "SDL.h"
  119. std::string aeProject;
  120. std::string aeCurrent;
  121. Vector2 aeWorkArea(0,0);
  122. extern "C"
  123. {
  124. void one(void* param) { mainloop(); }
  125. void oneEmsc() { mainloop(); }
  126. int main(int argc, char* argv[])
  127. {
  128. #ifdef __APPLE__
  129. aeProject = "Knight";
  130. #endif
  131. if (argc > 1)
  132. {
  133. aeProject = argv[1];
  134. if (argc > 2)
  135. {
  136. aeCurrent = argv[2];
  137. if (argc > 3)
  138. sscanf(argv[3], "%f;%f", &aeWorkArea.x, &aeWorkArea.y);
  139. }
  140. }
  141. run();
  142. #if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
  143. // If parameter 2 is set to 1, refresh rate will be 60 fps, 2 - 30 fps, 3 - 15 fps.
  144. SDL_iPhoneSetAnimationCallback(core::getWindow(), 1, one, nullptr);
  145. #endif
  146. #if EMSCRIPTEN
  147. emscripten_set_main_loop(oneEmsc, 0, 0);
  148. #endif
  149. return 0;
  150. }
  151. };
  152. #endif