Main.cpp 2.9 KB

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