Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Urho3D/Engine/Application.h>
- using namespace std::placeholders;
- #ifdef WIN32
- #include <windows.h>
- #endif
- #include <Urho3D/Engine/EngineDefs.h>
- #include <Urho3D/Engine/EngineEvents.h>
- #include <Urho3D/Core/CoreEvents.h>
- #include <Urho3D/Core/WorkQueue.h>
- #include <Urho3D/Graphics/Graphics.h>
- #include <Urho3D/IO/FileSystem.h>
- #include <Urho3D/IO/Log.h>
- #include <Urho3D/Resource/ResourceCache.h>
- #include <Urho3D/SystemUI/SystemUI.h>
- #include <Urho3D/SystemUI/Console.h>
- #include <Urho3D/LibraryInfo.h>
- #include <Urho3D/Core/CommandLine.h>
- #include <Urho3D/Input/Input.h>
- #include <Urho3D/Scene/Scene.h>
- #include <Urho3D/Scene/SceneManager.h>
- #include <Urho3D/Graphics/Renderer.h>
- #include <Urho3D/Graphics/Camera.h>
- #include <Urho3D/Graphics/Viewport.h>
- #include <Urho3D/Scene/Node.h>
- using namespace Urho3D;
- class AwesomeGameApplication : public Application
- {
- // This macro defines some methods that every `Urho3D::Object` descendant should have.
- URHO3D_OBJECT(AwesomeGameApplication, Application);
- public:
- SharedPtr<Scene> scene;
- SharedPtr<Node> camNode;
- SharedPtr<Camera> camera;
- SharedPtr<Viewport> vp;
- ea::vector<SharedPtr<Node>> goats;
- ea::vector<Vector3> goatsRotations;
- float yaw = 0;
- float pitch = 0;
- // Likewise every `Urho3D::Object` descendant should implement constructor with single `Context*` parameter.
- AwesomeGameApplication(Context* context)
- : Application(context)
- {
- }
- void Setup() override
- {
- #ifdef _WIN32
- // Required until SDL supports hdpi on windows
- if (HMODULE hLibrary = LoadLibraryA("Shcore.dll"))
- {
- typedef HRESULT(WINAPI*SetProcessDpiAwarenessType)(size_t value);
- if (auto fn = GetProcAddress(hLibrary, "SetProcessDpiAwareness"))
- ((SetProcessDpiAwarenessType)fn)(2); // PROCESS_PER_MONITOR_DPI_AWARE
- FreeLibrary(hLibrary);
- }
- #endif
- // Engine is not initialized yet. Set up all the parameters now.
- engineParameters_[EP_FULL_SCREEN] = false;
- engineParameters_[EP_WINDOW_WIDTH] = 1280;
- engineParameters_[EP_WINDOW_HEIGHT] = 760;
- engineParameters_[EP_LOG_LEVEL] = LOG_DEBUG;
- engineParameters_[EP_AUTOLOAD_PATHS] = "";
- #if DESKTOP
- FileSystem* fs = GetFileSystem();
- engineParameters_[EP_RESOURCE_PREFIX_PATHS] = fs->GetProgramDir() + ";" + fs->GetCurrentDir();
- #endif
- engineParameters_[EP_RESOURCE_PATHS] = "Cache;Resources";
- }
- void Start() override
- {
- // Continue with normal application initialization
- context_->RegisterSubsystem(new SceneManager(context_));
- // Load main scene.
- //SceneManager* manager = GetSubsystem<SceneManager>();
- //Scene* scene = manager->CreateScene();
- //bool ret = scene->LoadFile("New Scene.xml");
- //manager->SetActiveScene(scene);
- LoadScene("New Scene.xml");
- FindSceneView("MainCamera");
- CollectGoats("goat");
- // 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.
- GetInput()->SetMouseVisible(true);
- SubscribeToEvent(E_UPDATE, std::bind(&AwesomeGameApplication::OnUpdate, this, _2));
- }
- void OnUpdate(VariantMap& args)
- {
- using namespace Update;
- float timeStep = args[P_TIMESTEP].GetFloat();
- ViewRotate(timeStep);
- ViewMove(timeStep);
- UpdateGoats(timeStep);
- }
- void Stop() override
- {
- // This step is executed when application is closing. No more frames will be rendered after this method is invoked.
- }
- bool LoadScene(ea::string fileName)
- {
- scene = SharedPtr<Scene>(new Scene(context_));
- ea::string path = GetSubsystem<FileSystem>()->GetProgramDir();
- File sceneFile(context_, path + "Resources/" + fileName, FILE_READ);
- return scene->LoadXML(sceneFile);
- }
- bool FindSceneView(ea::string camName)
- {
- camNode = scene->GetChild(camName);
- camera = camNode->GetComponent<Camera>();
- vp = SharedPtr<Viewport>(new Viewport(context_, scene, camera));
- GetRenderer()->SetViewport(0, vp);
- return vp != nullptr;
- }
- bool CollectGoats(ea::string goatsParentNode)
- {
- Node* goatsCollection = scene->GetChild(goatsParentNode);
- goats = goatsCollection->GetChildren();
- goatsRotations.resize(goats.size());
- for (auto& rot : goatsRotations)
- {
- rot = Vector3(Urho3D::Random(-180.0f, 180.0f), Urho3D::Random(-180.0f, 180.0f), Urho3D::Random(-180.0f, 180.0f));
- }
- return goats.size() > 0;
- }
- void ViewRotate(float timeStep)
- {
- const float MOUSE_SENSITIVITY = 0.1f;
- IntVector2 mouseMove = GetInput()->GetMouseMove();
- yaw += MOUSE_SENSITIVITY * mouseMove.x_;
- pitch += MOUSE_SENSITIVITY * mouseMove.y_;
- pitch = Clamp(pitch, -90.0f, 90.0f);
- camNode->SetWorldRotation(Quaternion(pitch, yaw, 0.0f));
- }
- void ViewMove(float timeStep)
- {
- if (camNode)
- {
- const float MOVE_SPEED = 2.0f;
- static Input* input = GetSubsystem<Input>();
- Quaternion rotation = camNode->GetWorldRotation();
- if (input->GetKeyDown(Urho3D::Key::KEY_W))
- camNode->Translate((rotation * Vector3::FORWARD) * MOVE_SPEED * timeStep, TS_WORLD);
- if (input->GetKeyDown(Urho3D::Key::KEY_S))
- camNode->Translate((rotation * Vector3::BACK) * MOVE_SPEED * timeStep, TS_WORLD);
- if (input->GetKeyDown(Urho3D::Key::KEY_A))
- camNode->Translate((rotation * Vector3::LEFT) * MOVE_SPEED * timeStep, TS_WORLD);
- if (input->GetKeyDown(Urho3D::Key::KEY_D))
- camNode->Translate((rotation * Vector3::RIGHT) * MOVE_SPEED * timeStep, TS_WORLD);
- if (input->GetKeyDown(Urho3D::Key::KEY_SPACE))
- camNode->Translate(Vector3::UP * MOVE_SPEED * timeStep, TS_WORLD);
- if (input->GetKeyDown(Urho3D::Key::KEY_Z))
- camNode->Translate(Vector3::DOWN * MOVE_SPEED * timeStep, TS_WORLD);
- }
- }
- void UpdateGoats(float timeStep)
- {
- for (int i=0; i< goats.size(); ++i)
- {
- Quaternion q = goats[i]->GetWorldRotation();
- Quaternion nr = Quaternion(goatsRotations[i] * timeStep);
- goats[i]->SetWorldRotation(q*nr);
- }
- }
- };
- // A helper macro which defines main function. Forgetting it will result in linker errors complaining about missing `_main` or `_WinMain@16`.
- URHO3D_DEFINE_APPLICATION_MAIN(AwesomeGameApplication);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement