#include #include class Game : public Application { URHO3D_OBJECT(Game, Application); public: SharedPtr scene_; SharedPtr cameraNode_; float yaw_ = 0.0f; float pitch_ = 0.0f; SharedPtr worldSpaceSpriteBatch_; SharedPtr screenSpaceSpriteBatch_; SharedPtr virtualSpriteBatch_; float fpsTimeCounter_ = 0.0f; i32 fpsFrameCounter_ = 0; i32 fpsValue_ = 0; i32 currentTest_ = 1; float angle_ = 0.0f; float scale_ = 0.0f; Game(Context* context) : Application(context) { } void Setup() { engineParameters_[EP_WINDOW_RESIZABLE] = true; engineParameters_[EP_FULL_SCREEN] = false; engineParameters_[EP_WINDOW_WIDTH] = 1200; engineParameters_[EP_WINDOW_HEIGHT] = 900; engineParameters_[EP_FRAME_LIMITER] = false; engineParameters_[EP_LOG_NAME] = GetSubsystem()->GetAppPreferencesDir("urho3d", "logs") + "56_SpriteBatch.log"; } void Start() { //GetSubsystem()->SetMaxFps(10); CreateScene(); SetupViewport(); SubscribeToEvents(); XMLFile* xmlFile = GetSubsystem()->GetResource("UI/DefaultStyle.xml"); DebugHud* debugHud = engine_->CreateDebugHud(); debugHud->SetDefaultStyle(xmlFile); screenSpaceSpriteBatch_ = new SpriteBatch(context_); worldSpaceSpriteBatch_ = new SpriteBatch(context_); worldSpaceSpriteBatch_->camera_ = cameraNode_->GetComponent(); worldSpaceSpriteBatch_->compareMode_ = CMP_LESSEQUAL; virtualSpriteBatch_ = new SpriteBatch(context_); virtualSpriteBatch_->virtualScreenSize_ = IntVector2(700, 600); } void SetupViewport() { SharedPtr viewport(new Viewport(context_, scene_, cameraNode_->GetComponent())); GetSubsystem()->SetViewport(0, viewport); } void CreateScene() { ResourceCache* cache = GetSubsystem(); scene_ = new Scene(context_); scene_->CreateComponent(); Node* planeNode = scene_->CreateChild("Plane"); planeNode->SetScale(Vector3(100.0f, 1.0f, 100.0f)); StaticModel* planeObject = planeNode->CreateComponent(); planeObject->SetModel(cache->GetResource("Models/Plane.mdl")); planeObject->SetMaterial(cache->GetResource("Materials/StoneTiled.xml")); Node* lightNode = scene_->CreateChild("DirectionalLight"); lightNode->SetDirection(Vector3(0.6f, -1.0f, 0.8f)); Light* light = lightNode->CreateComponent(); light->SetColor(Color(0.5f, 0.5f, 0.5f)); light->SetLightType(LIGHT_DIRECTIONAL); light->SetCastShadows(true); light->SetShadowBias(BiasParameters(0.00025f, 0.5f)); light->SetShadowCascade(CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f)); //light->SetShadowIntensity(0.5f); Node* zoneNode = scene_->CreateChild("Zone"); Zone* zone = zoneNode->CreateComponent(); zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f)); zone->SetAmbientColor(Color(0.5f, 0.5f, 0.5f)); zone->SetFogColor(Color(0.4f, 0.5f, 0.8f)); zone->SetFogStart(100.0f); zone->SetFogEnd(300.0f); constexpr i32 NUM_OBJECTS = 20; for (i32 i = 0; i < NUM_OBJECTS; ++i) { Node* mushroomNode = scene_->CreateChild("Mushroom"); mushroomNode->SetPosition(Vector3(Random(90.0f) - 45.0f, 0.0f, Random(90.0f) - 45.0f)); mushroomNode->SetRotation(Quaternion(0.0f, Random(360.0f), 0.0f)); mushroomNode->SetScale(0.5f + Random(2.0f)); StaticModel* mushroomObject = mushroomNode->CreateComponent(); mushroomObject->SetModel(cache->GetResource("Models/Mushroom.mdl")); mushroomObject->SetMaterial(cache->GetResource("Materials/Mushroom.xml")); mushroomObject->SetCastShadows(true); } cameraNode_ = scene_->CreateChild("Camera"); cameraNode_->CreateComponent(); cameraNode_->SetPosition(Vector3(0.0f, 2.0f, -5.0f)); } void MoveCamera(float timeStep) { constexpr float MOVE_SPEED = 20.0f; constexpr float MOUSE_SENSITIVITY = 0.1f; Input* input = GetSubsystem(); IntVector2 mouseMove = input->GetMouseMove(); yaw_ += MOUSE_SENSITIVITY * mouseMove.x_; pitch_ += MOUSE_SENSITIVITY * mouseMove.y_; pitch_ = Clamp(pitch_, -90.0f, 90.0f); cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f)); if (input->GetKeyDown(KEY_W)) cameraNode_->Translate(Vector3::FORWARD * MOVE_SPEED * timeStep); if (input->GetKeyDown(KEY_S)) cameraNode_->Translate(Vector3::BACK * MOVE_SPEED * timeStep); if (input->GetKeyDown(KEY_A)) cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep); if (input->GetKeyDown(KEY_D)) cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep); } void SubscribeToEvents() { SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(Game, HandleUpdate)); SubscribeToEvent(E_ENDVIEWRENDER, URHO3D_HANDLER(Game, HandleEndViewRender)); } void HandleUpdate(StringHash eventType, VariantMap& eventData) { using namespace Update; float timeStep = eventData[P_TIMESTEP].GetFloat(); Input* input = GetSubsystem(); Time* time = GetSubsystem