2
0

Main.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include <cstdio>
  2. #include <anki/AnKi.h>
  3. using namespace anki;
  4. class MyApp : public App
  5. {
  6. public:
  7. Error init();
  8. Error userMainLoop(Bool& quit) override;
  9. };
  10. MyApp* app;
  11. Error MyApp::init()
  12. {
  13. // Init the super class
  14. Config config;
  15. config.set("fullscreenDesktopResolution", true);
  16. config.set("dataPaths", ".:..");
  17. config.set("debugContext", 0);
  18. ANKI_CHECK(App::init(config, allocAligned, nullptr));
  19. // Load the scene.lua
  20. ScriptResourcePtr script;
  21. ANKI_CHECK(getResourceManager().loadResource("samples/assets/scene.lua", script));
  22. ANKI_CHECK(getScriptManager().evalString(script->getSource()));
  23. // Input
  24. getInput().lockCursor(true);
  25. getInput().hideCursor(true);
  26. getInput().moveCursor(Vec2(0.0));
  27. // Some renderer stuff
  28. getMainRenderer().getOffscreenRenderer().getVolumetric().setFog(Vec3(1.0, 0.9, 0.9), 0.7);
  29. return ErrorCode::NONE;
  30. }
  31. Error MyApp::userMainLoop(Bool& quit)
  32. {
  33. const F32 MOVE_DISTANCE = 0.1;
  34. const F32 ROTATE_ANGLE = toRad(2.5);
  35. const F32 MOUSE_SENSITIVITY = 9.0;
  36. quit = false;
  37. SceneGraph& scene = getSceneGraph();
  38. Renderer& renderer = getMainRenderer().getOffscreenRenderer();
  39. Input& in = getInput();
  40. if(in.getKey(KeyCode::ESCAPE))
  41. {
  42. quit = true;
  43. return ErrorCode::NONE;
  44. }
  45. // move the camera
  46. static MoveComponent* mover = &scene.getActiveCamera().getComponent<MoveComponent>();
  47. if(in.getKey(KeyCode::_1) == 1)
  48. {
  49. mover = &scene.getActiveCamera().getComponent<MoveComponent>();
  50. }
  51. if(in.getKey(KeyCode::F1) == 1)
  52. {
  53. renderer.getDbg().setEnabled(!renderer.getDbg().getEnabled());
  54. }
  55. if(in.getKey(KeyCode::F2) == 1)
  56. {
  57. renderer.getDbg().flipFlags(DbgFlag::SPATIAL_COMPONENT);
  58. }
  59. if(in.getKey(KeyCode::F6) == 1)
  60. {
  61. renderer.getDbg().switchDepthTestEnabled();
  62. }
  63. if(in.getKey(KeyCode::UP))
  64. {
  65. mover->rotateLocalX(ROTATE_ANGLE);
  66. }
  67. if(in.getKey(KeyCode::DOWN))
  68. {
  69. mover->rotateLocalX(-ROTATE_ANGLE);
  70. }
  71. if(in.getKey(KeyCode::LEFT))
  72. {
  73. mover->rotateLocalY(ROTATE_ANGLE);
  74. }
  75. if(in.getKey(KeyCode::RIGHT))
  76. {
  77. mover->rotateLocalY(-ROTATE_ANGLE);
  78. }
  79. if(in.getKey(KeyCode::A))
  80. {
  81. mover->moveLocalX(-MOVE_DISTANCE);
  82. }
  83. if(in.getKey(KeyCode::D))
  84. {
  85. mover->moveLocalX(MOVE_DISTANCE);
  86. }
  87. if(in.getKey(KeyCode::Z))
  88. {
  89. mover->moveLocalY(MOVE_DISTANCE);
  90. }
  91. if(in.getKey(KeyCode::SPACE))
  92. {
  93. mover->moveLocalY(-MOVE_DISTANCE);
  94. }
  95. if(in.getKey(KeyCode::W))
  96. {
  97. mover->moveLocalZ(-MOVE_DISTANCE);
  98. }
  99. if(in.getKey(KeyCode::S))
  100. {
  101. mover->moveLocalZ(MOVE_DISTANCE);
  102. }
  103. if(in.getKey(KeyCode::Q))
  104. {
  105. mover->rotateLocalZ(ROTATE_ANGLE);
  106. }
  107. if(in.getKey(KeyCode::E))
  108. {
  109. mover->rotateLocalZ(-ROTATE_ANGLE);
  110. }
  111. if(in.getMousePosition() != Vec2(0.0))
  112. {
  113. F32 angY = -ROTATE_ANGLE * in.getMousePosition().x() * MOUSE_SENSITIVITY * getMainRenderer().getAspectRatio();
  114. mover->rotateLocalY(angY);
  115. mover->rotateLocalX(ROTATE_ANGLE * in.getMousePosition().y() * MOUSE_SENSITIVITY);
  116. }
  117. return ErrorCode::NONE;
  118. }
  119. int main(int argc, char* argv[])
  120. {
  121. Error err = ErrorCode::NONE;
  122. app = new MyApp;
  123. err = app->init();
  124. if(!err)
  125. {
  126. err = app->mainLoop();
  127. }
  128. if(err)
  129. {
  130. ANKI_LOGE("Error reported. To run %s you have to navigate to the /path/to/anki/samples. And then execute it",
  131. argv[0]);
  132. }
  133. else
  134. {
  135. delete app;
  136. ANKI_LOGI("Bye!!");
  137. }
  138. return 0;
  139. }