2
0

Main.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. ANKI_CHECK(App::init(config, allocAligned, nullptr));
  18. // Load the scene.lua
  19. ScriptResourcePtr script;
  20. ANKI_CHECK(getResourceManager().loadResource("samples/assets/scene.lua", script));
  21. ANKI_CHECK(getScriptManager().evalString(script->getSource()));
  22. // Input
  23. getInput().lockCursor(true);
  24. getInput().hideCursor(true);
  25. getInput().moveCursor(Vec2(0.0));
  26. // Some renderer stuff
  27. getMainRenderer().getOffscreenRenderer().getVolumetric().setFog(Vec3(1.0, 0.9, 0.9), 0.7);
  28. return ErrorCode::NONE;
  29. }
  30. Error MyApp::userMainLoop(Bool& quit)
  31. {
  32. const F32 MOVE_DISTANCE = 0.1;
  33. const F32 ROTATE_ANGLE = toRad(2.5);
  34. const F32 MOUSE_SENSITIVITY = 9.0;
  35. quit = false;
  36. SceneGraph& scene = getSceneGraph();
  37. Input& in = getInput();
  38. if(in.getKey(KeyCode::ESCAPE))
  39. {
  40. quit = true;
  41. return ErrorCode::NONE;
  42. }
  43. // move the camera
  44. static MoveComponent* mover = &scene.getActiveCamera().getComponent<MoveComponent>();
  45. if(in.getKey(KeyCode::UP))
  46. {
  47. mover->rotateLocalX(ROTATE_ANGLE);
  48. }
  49. if(in.getKey(KeyCode::DOWN))
  50. {
  51. mover->rotateLocalX(-ROTATE_ANGLE);
  52. }
  53. if(in.getKey(KeyCode::LEFT))
  54. {
  55. mover->rotateLocalY(ROTATE_ANGLE);
  56. }
  57. if(in.getKey(KeyCode::RIGHT))
  58. {
  59. mover->rotateLocalY(-ROTATE_ANGLE);
  60. }
  61. if(in.getKey(KeyCode::A))
  62. {
  63. mover->moveLocalX(-MOVE_DISTANCE);
  64. }
  65. if(in.getKey(KeyCode::D))
  66. {
  67. mover->moveLocalX(MOVE_DISTANCE);
  68. }
  69. if(in.getKey(KeyCode::Z))
  70. {
  71. mover->moveLocalY(MOVE_DISTANCE);
  72. }
  73. if(in.getKey(KeyCode::SPACE))
  74. {
  75. mover->moveLocalY(-MOVE_DISTANCE);
  76. }
  77. if(in.getKey(KeyCode::W))
  78. {
  79. mover->moveLocalZ(-MOVE_DISTANCE);
  80. }
  81. if(in.getKey(KeyCode::S))
  82. {
  83. mover->moveLocalZ(MOVE_DISTANCE);
  84. }
  85. if(in.getKey(KeyCode::Q))
  86. {
  87. mover->rotateLocalZ(ROTATE_ANGLE);
  88. }
  89. if(in.getKey(KeyCode::E))
  90. {
  91. mover->rotateLocalZ(-ROTATE_ANGLE);
  92. }
  93. if(in.getMousePosition() != Vec2(0.0))
  94. {
  95. F32 angY = -ROTATE_ANGLE * in.getMousePosition().x() * MOUSE_SENSITIVITY * getMainRenderer().getAspectRatio();
  96. mover->rotateLocalY(angY);
  97. mover->rotateLocalX(ROTATE_ANGLE * in.getMousePosition().y() * MOUSE_SENSITIVITY);
  98. }
  99. return ErrorCode::NONE;
  100. }
  101. int main(int argc, char* argv[])
  102. {
  103. Error err = ErrorCode::NONE;
  104. app = new MyApp;
  105. err = app->init();
  106. if(!err)
  107. {
  108. err = app->mainLoop();
  109. }
  110. if(err)
  111. {
  112. ANKI_LOGE("Error reported. To run %s you have to navigate to the /path/to/anki/samples. And then execute it",
  113. argv[0]);
  114. }
  115. else
  116. {
  117. delete app;
  118. ANKI_LOGI("Bye!!");
  119. }
  120. return 0;
  121. }