Framework.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include "Framework.h"
  6. using namespace anki;
  7. Error SampleApp::init(int argc, char** argv, CString sampleName)
  8. {
  9. if(!directoryExists("assets"))
  10. {
  11. ANKI_LOGE("Cannot find directory \"assets\". YOU ARE RUNNING THE SAMPLE FROM THE WRONG DIRECTORY. "
  12. "To run %s you have to navigate to the /path/to/anki/samples/%s and then execute it",
  13. argv[0],
  14. &sampleName[0]);
  15. return Error::USER_DATA;
  16. }
  17. // Init the super class
  18. Config config;
  19. config.set("window.fullscreenDesktopResolution", true);
  20. config.set("rsrc.dataPaths", ".:../..");
  21. config.set("window.debugContext", 0);
  22. ANKI_CHECK(config.setFromCommandLineArguments(argc, argv));
  23. ANKI_CHECK(App::init(config, allocAligned, nullptr));
  24. // Input
  25. getInput().lockCursor(true);
  26. getInput().hideCursor(true);
  27. getInput().moveCursor(Vec2(0.0f));
  28. // Some renderer stuff
  29. getMainRenderer().getOffscreenRenderer().getVolumetric().setFogParticleColor(Vec3(1.0f, 0.9f, 0.9f) * 0.0001f);
  30. ANKI_CHECK(sampleExtraInit());
  31. return Error::NONE;
  32. }
  33. Error SampleApp::userMainLoop(Bool& quit)
  34. {
  35. const F32 MOVE_DISTANCE = 0.1f;
  36. const F32 ROTATE_ANGLE = toRad(2.5f);
  37. const F32 MOUSE_SENSITIVITY = 9.0f;
  38. quit = false;
  39. SceneGraph& scene = getSceneGraph();
  40. Renderer& renderer = getMainRenderer().getOffscreenRenderer();
  41. Input& in = getInput();
  42. if(in.getKey(KeyCode::ESCAPE))
  43. {
  44. quit = true;
  45. return Error::NONE;
  46. }
  47. // move the camera
  48. static MoveComponent* mover = &scene.getActiveCamera().getComponent<MoveComponent>();
  49. if(in.getKey(KeyCode::_1) == 1)
  50. {
  51. mover = &scene.getActiveCamera().getComponent<MoveComponent>();
  52. }
  53. if(in.getKey(KeyCode::F1) == 1)
  54. {
  55. renderer.getDbg().setEnabled(!renderer.getDbg().getEnabled());
  56. }
  57. if(in.getKey(KeyCode::F2) == 1)
  58. {
  59. renderer.getDbg().flipFlags(DbgFlag::SPATIAL_COMPONENT);
  60. }
  61. if(in.getKey(KeyCode::F6) == 1)
  62. {
  63. renderer.getDbg().switchDepthTestEnabled();
  64. }
  65. if(in.getKey(KeyCode::UP))
  66. {
  67. mover->rotateLocalX(ROTATE_ANGLE);
  68. }
  69. if(in.getKey(KeyCode::DOWN))
  70. {
  71. mover->rotateLocalX(-ROTATE_ANGLE);
  72. }
  73. if(in.getKey(KeyCode::LEFT))
  74. {
  75. mover->rotateLocalY(ROTATE_ANGLE);
  76. }
  77. if(in.getKey(KeyCode::RIGHT))
  78. {
  79. mover->rotateLocalY(-ROTATE_ANGLE);
  80. }
  81. if(in.getKey(KeyCode::A))
  82. {
  83. mover->moveLocalX(-MOVE_DISTANCE);
  84. }
  85. if(in.getKey(KeyCode::D))
  86. {
  87. mover->moveLocalX(MOVE_DISTANCE);
  88. }
  89. if(in.getKey(KeyCode::Z))
  90. {
  91. mover->moveLocalY(MOVE_DISTANCE);
  92. }
  93. if(in.getKey(KeyCode::SPACE))
  94. {
  95. mover->moveLocalY(-MOVE_DISTANCE);
  96. }
  97. if(in.getKey(KeyCode::W))
  98. {
  99. mover->moveLocalZ(-MOVE_DISTANCE);
  100. }
  101. if(in.getKey(KeyCode::S))
  102. {
  103. mover->moveLocalZ(MOVE_DISTANCE);
  104. }
  105. if(in.getKey(KeyCode::Q))
  106. {
  107. mover->rotateLocalZ(ROTATE_ANGLE);
  108. }
  109. if(in.getKey(KeyCode::E))
  110. {
  111. mover->rotateLocalZ(-ROTATE_ANGLE);
  112. }
  113. if(in.getMousePosition() != Vec2(0.0))
  114. {
  115. F32 angY = -ROTATE_ANGLE * in.getMousePosition().x() * MOUSE_SENSITIVITY * getMainRenderer().getAspectRatio();
  116. mover->rotateLocalY(angY);
  117. mover->rotateLocalX(ROTATE_ANGLE * in.getMousePosition().y() * MOUSE_SENSITIVITY);
  118. }
  119. return Error::NONE;
  120. }