Advertisement
Guest User

Untitled

a guest
Aug 9th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.26 KB | None | 0 0
  1. #include <Urho3D/Engine/Application.h>
  2.  
  3. using namespace std::placeholders;
  4.  
  5. #ifdef WIN32
  6. #include <windows.h>
  7. #endif
  8.  
  9. #include <Urho3D/Engine/EngineDefs.h>
  10. #include <Urho3D/Engine/EngineEvents.h>
  11. #include <Urho3D/Core/CoreEvents.h>
  12. #include <Urho3D/Core/WorkQueue.h>
  13. #include <Urho3D/Graphics/Graphics.h>
  14. #include <Urho3D/IO/FileSystem.h>
  15. #include <Urho3D/IO/Log.h>
  16. #include <Urho3D/Resource/ResourceCache.h>
  17. #include <Urho3D/SystemUI/SystemUI.h>
  18. #include <Urho3D/SystemUI/Console.h>
  19. #include <Urho3D/LibraryInfo.h>
  20. #include <Urho3D/Core/CommandLine.h>
  21. #include <Urho3D/Input/Input.h>
  22. #include <Urho3D/Scene/Scene.h>
  23. #include <Urho3D/Scene/SceneManager.h>
  24. #include <Urho3D/Graphics/Renderer.h>
  25. #include <Urho3D/Graphics/Camera.h>
  26. #include <Urho3D/Graphics/Viewport.h>
  27. #include <Urho3D/Scene/Node.h>
  28.  
  29. using namespace Urho3D;
  30.  
  31.  
  32. class AwesomeGameApplication : public Application
  33. {
  34.     // This macro defines some methods that every `Urho3D::Object` descendant should have.
  35.     URHO3D_OBJECT(AwesomeGameApplication, Application);
  36. public:
  37.  
  38.     SharedPtr<Scene> scene;
  39.     SharedPtr<Node> camNode;
  40.     SharedPtr<Camera> camera;
  41.     SharedPtr<Viewport> vp;
  42.     ea::vector<SharedPtr<Node>> goats;
  43.     ea::vector<Vector3> goatsRotations;
  44.  
  45.     float yaw = 0;
  46.     float pitch = 0;
  47.  
  48.  
  49.     // Likewise every `Urho3D::Object` descendant should implement constructor with single `Context*` parameter.
  50.     AwesomeGameApplication(Context* context)
  51.         : Application(context)
  52.     {
  53.     }
  54.  
  55.     void Setup() override
  56.     {
  57. #ifdef _WIN32
  58.     // Required until SDL supports hdpi on windows
  59.     if (HMODULE hLibrary = LoadLibraryA("Shcore.dll"))
  60.     {
  61.         typedef HRESULT(WINAPI*SetProcessDpiAwarenessType)(size_t value);
  62.         if (auto fn = GetProcAddress(hLibrary, "SetProcessDpiAwareness"))
  63.             ((SetProcessDpiAwarenessType)fn)(2);    // PROCESS_PER_MONITOR_DPI_AWARE
  64.         FreeLibrary(hLibrary);
  65.     }
  66. #endif
  67.         // Engine is not initialized yet. Set up all the parameters now.
  68.         engineParameters_[EP_FULL_SCREEN] = false;
  69.         engineParameters_[EP_WINDOW_WIDTH] = 1280;
  70.         engineParameters_[EP_WINDOW_HEIGHT] = 760;
  71.         engineParameters_[EP_LOG_LEVEL] = LOG_DEBUG;
  72.         engineParameters_[EP_AUTOLOAD_PATHS] = "";
  73.  
  74. #if DESKTOP
  75.     FileSystem* fs = GetFileSystem();
  76.     engineParameters_[EP_RESOURCE_PREFIX_PATHS] = fs->GetProgramDir() + ";" + fs->GetCurrentDir();
  77. #endif
  78.     engineParameters_[EP_RESOURCE_PATHS] = "Cache;Resources";
  79.     }
  80.  
  81.     void Start() override
  82.     {
  83.         // Continue with normal application initialization
  84.         context_->RegisterSubsystem(new SceneManager(context_));
  85.  
  86.  
  87.         // Load main scene.
  88.         //SceneManager* manager = GetSubsystem<SceneManager>();
  89.         //Scene* scene = manager->CreateScene();
  90.         //bool ret = scene->LoadFile("New Scene.xml");
  91.         //manager->SetActiveScene(scene);
  92.  
  93.         LoadScene("New Scene.xml");
  94.         FindSceneView("MainCamera");
  95.         CollectGoats("goat");
  96.  
  97.        
  98.        
  99.         // At this point engine is initialized, but first frame was not rendered yet. Further setup should be done here. To make sample a little bit user friendly show mouse cursor here.
  100.         GetInput()->SetMouseVisible(true);
  101.  
  102.         SubscribeToEvent(E_UPDATE, std::bind(&AwesomeGameApplication::OnUpdate, this, _2));
  103.     }
  104.  
  105.     void OnUpdate(VariantMap& args)
  106.     {
  107.         using namespace Update;
  108.         float timeStep = args[P_TIMESTEP].GetFloat();
  109.  
  110.         ViewRotate(timeStep);
  111.         ViewMove(timeStep);
  112.         UpdateGoats(timeStep);
  113.     }
  114.  
  115.     void Stop() override
  116.     {
  117.         // This step is executed when application is closing. No more frames will be rendered after this method is invoked.
  118.     }
  119.  
  120.     bool LoadScene(ea::string fileName)
  121.     {
  122.         scene = SharedPtr<Scene>(new Scene(context_));
  123.         ea::string path = GetSubsystem<FileSystem>()->GetProgramDir();
  124.         File sceneFile(context_, path + "Resources/" + fileName, FILE_READ);
  125.         return scene->LoadXML(sceneFile);
  126.     }
  127.  
  128.     bool FindSceneView(ea::string camName)
  129.     {
  130.         camNode = scene->GetChild(camName);
  131.         camera = camNode->GetComponent<Camera>();
  132.         vp = SharedPtr<Viewport>(new Viewport(context_, scene, camera));
  133.         GetRenderer()->SetViewport(0, vp);
  134.         return vp != nullptr;
  135.     }
  136.  
  137.     bool CollectGoats(ea::string goatsParentNode)
  138.     {
  139.         Node* goatsCollection = scene->GetChild(goatsParentNode);
  140.         goats = goatsCollection->GetChildren();
  141.         goatsRotations.resize(goats.size());
  142.  
  143.         for (auto& rot : goatsRotations)
  144.         {
  145.             rot = Vector3(Urho3D::Random(-180.0f, 180.0f), Urho3D::Random(-180.0f, 180.0f), Urho3D::Random(-180.0f, 180.0f));
  146.         }
  147.  
  148.  
  149.         return goats.size() > 0;
  150.     }
  151.  
  152.     void ViewRotate(float timeStep)
  153.     {
  154.         const float MOUSE_SENSITIVITY = 0.1f;
  155.         IntVector2 mouseMove = GetInput()->GetMouseMove();
  156.  
  157.         yaw += MOUSE_SENSITIVITY * mouseMove.x_;
  158.         pitch += MOUSE_SENSITIVITY * mouseMove.y_;
  159.         pitch = Clamp(pitch, -90.0f, 90.0f);
  160.  
  161.         camNode->SetWorldRotation(Quaternion(pitch, yaw, 0.0f));
  162.     }
  163.  
  164.     void ViewMove(float timeStep)
  165.     {
  166.         if (camNode)
  167.         {
  168.             const float MOVE_SPEED = 2.0f;
  169.  
  170.             static Input* input = GetSubsystem<Input>();
  171.  
  172.             Quaternion rotation = camNode->GetWorldRotation();
  173.  
  174.            
  175.             if (input->GetKeyDown(Urho3D::Key::KEY_W))
  176.                 camNode->Translate((rotation * Vector3::FORWARD) * MOVE_SPEED * timeStep, TS_WORLD);
  177.  
  178.             if (input->GetKeyDown(Urho3D::Key::KEY_S))
  179.                 camNode->Translate((rotation * Vector3::BACK) * MOVE_SPEED * timeStep, TS_WORLD);
  180.  
  181.             if (input->GetKeyDown(Urho3D::Key::KEY_A))
  182.                 camNode->Translate((rotation * Vector3::LEFT) * MOVE_SPEED * timeStep, TS_WORLD);
  183.  
  184.             if (input->GetKeyDown(Urho3D::Key::KEY_D))
  185.                 camNode->Translate((rotation * Vector3::RIGHT) * MOVE_SPEED * timeStep, TS_WORLD);
  186.  
  187.             if (input->GetKeyDown(Urho3D::Key::KEY_SPACE))
  188.                 camNode->Translate(Vector3::UP * MOVE_SPEED * timeStep, TS_WORLD);
  189.  
  190.             if (input->GetKeyDown(Urho3D::Key::KEY_Z))
  191.                 camNode->Translate(Vector3::DOWN * MOVE_SPEED * timeStep, TS_WORLD);
  192.  
  193.         }
  194.     }
  195.  
  196.     void UpdateGoats(float timeStep)
  197.     {
  198.         for (int i=0; i< goats.size(); ++i)
  199.         {
  200.             Quaternion q = goats[i]->GetWorldRotation();
  201.             Quaternion nr = Quaternion(goatsRotations[i] * timeStep);
  202.             goats[i]->SetWorldRotation(q*nr);
  203.         }
  204.     }
  205. };
  206.  
  207. // A helper macro which defines main function. Forgetting it will result in linker errors complaining about missing `_main` or `_WinMain@16`.
  208. URHO3D_DEFINE_APPLICATION_MAIN(AwesomeGameApplication);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement