这个嘛存粹是抄一下,省的每次还要继承啊,太多东西,不好去修改,一个文件做修改方便点,留这样,以备后面copy用
#pragma once
#include <Urho3D/Engine/Application.h>
#include <Urho3D/Input/Input.h>
#include <Urho3D/Engine/Application.h>
#include <Urho3D/Graphics/Camera.h>
#include <Urho3D/Engine/Console.h>
#include <Urho3D/UI/Cursor.h>
#include <Urho3D/Engine/DebugHud.h>
#include <Urho3D/Engine/Engine.h>
#include <Urho3D/IO/FileSystem.h>
#include <Urho3D/Graphics/Graphics.h>
#include <Urho3D/Input/Input.h>
#include <Urho3D/Input/InputEvents.h>
#include <Urho3D/Graphics/Renderer.h>
#include <Urho3D/Resource/ResourceCache.h>
#include <Urho3D/Scene/Scene.h>
#include <Urho3D/Scene/SceneEvents.h>
#include <Urho3D/UI/Sprite.h>
#include <Urho3D/Graphics/Texture2D.h>
#include <Urho3D/Core/Timer.h>
#include <Urho3D/UI/UI.h>
#include <Urho3D/Resource/XMLFile.h>
#include <Urho3D/IO/Log.h>
#include <Urho3D/Core/CoreEvents.h>
#include <Urho3D/Core/ProcessUtils.h>
#include <Urho3D/Input/Input.h>
#include <Urho3D/UI/Font.h>
#include <Urho3D/UI/Text.h>
#include <Urho3D/UI/UI.h>
#include <Urho3D/DebugNew.h>
namespace Urho3D
{
class Node;
class Scene;
class Sprite;
}
using namespace Urho3D;
const float TOUCH_SENSITIVITY = 2.0f;
class Game : public Application
{
URHO3D_OBJECT(Game, Application);
public:
Game(Context *context);
virtual void Setup();
virtual void Start();
virtual void Stop();
protected:
virtual String GetScreenJoystickPatchString() const { return String::EMPTY; }
void InitTouchInput();
void InitMouseMode(MouseMode mode);
void SetLogoVisible(bool enable);
SharedPtr<Sprite> logoSprite_;
SharedPtr<Scene> scene_;
SharedPtr<Node> cameraNode_;
float yaw_;
float pitch_;
bool touchEnabled_;
MouseMode useMouseMode_;
private:
void HandleMouseModeRequest(StringHash eventType, VariantMap& eventData);
void HandleMouseModeChange(StringHash eventType, VariantMap& eventData);
void HandleKeyDown(StringHash eventType, VariantMap& eventData);
void HandleKeyUp(StringHash eventType, VariantMap& eventData);
void HandleSceneUpdate(StringHash eventType, VariantMap& eventData);
void HandleTouchBegin(StringHash eventType, VariantMap& eventData);
void HandleUpdate(StringHash eventType, VariantMap& eventData);
unsigned screenJoystickIndex_;
unsigned screenJoystickSettingsIndex_;
bool paused_;
};
URHO3D_DEFINE_APPLICATION_MAIN(Game)
Game::Game(Context* context) :
Application(context),
yaw_(0.0f),
pitch_(0.0f),
touchEnabled_(false),
screenJoystickIndex_(M_MAX_UNSIGNED),
screenJoystickSettingsIndex_(M_MAX_UNSIGNED),
paused_(false),
useMouseMode_(MM_ABSOLUTE)
{
}
void Game::Setup()
{
engineParameters_["WindowTitle"] = GetTypeName();
engineParameters_["LogName"] = GetSubsystem<FileSystem>()->GetAppPreferencesDir("urho3d", "logs") + GetTypeName() + ".log";
engineParameters_["FullScreen"] = false;
engineParameters_["Headless"] = false;
engineParameters_["Sound"] = false;
if (!engineParameters_.Contains("ResourcePrefixPaths"))
engineParameters_["ResourcePrefixPaths"] = ";../share/Resources;../share/Urho3D/Resources";
}
void Game::Start()
{
if (GetPlatform() == "Android" || GetPlatform() == "iOS")
InitTouchInput();
else if (GetSubsystem<Input>()->GetNumJoysticks() == 0)
SubscribeToEvent(E_TOUCHBEGIN, URHO3D_HANDLER(Game, HandleTouchBegin));
ResourceCache* cache = GetSubsystem<ResourceCache>();
Texture2D* logoTexture = cache->GetResource<Texture2D>("Textures/LogoLarge.png");
if (!logoTexture)
return;
UI* ui = GetSubsystem<UI>();
logoSprite_ = ui->GetRoot()->CreateChild<Sprite>();
logoSprite_->SetTexture(logoTexture);
int textureWidth = logoTexture->GetWidth();
int textureHeight = logoTexture->GetHeight();
logoSprite_->SetScale(256.0f / textureWidth);
logoSprite_->SetSize(textureWidth, textureHeight);
logoSprite_->SetHotSpot(0, textureHeight);
logoSprite_->SetAlignment(HA_LEFT, VA_BOTTOM);
logoSprite_->SetOpacity(0.75f);
logoSprite_->SetPriority(-100);
Graphics* graphics = GetSubsystem<Graphics>();
Image* icon = cache->GetResource<Image>("Textures/UrhoIcon.png");
graphics->SetWindowIcon(icon);
graphics->SetWindowTitle("Urho3D Game");
XMLFile* xmlFile = cache->GetResource<XMLFile>("UI/DefaultStyle.xml");
Console* console = engine_->CreateConsole();
console->SetDefaultStyle(xmlFile);
console->GetBackground()->SetOpacity(0.8f);
DebugHud* debugHud = engine_->CreateDebugHud();
debugHud->SetDefaultStyle(xmlFile);
SubscribeToEvent(E_KEYDOWN, URHO3D_HANDLER(Game, HandleKeyDown));
SubscribeToEvent(E_KEYUP, URHO3D_HANDLER(Game, HandleKeyUp));
SubscribeToEvent(E_SCENEUPDATE, URHO3D_HANDLER(Game, HandleSceneUpdate));
//========================================================================
// hello world
SharedPtr<Text> helloText(new Text(context_));
helloText->SetText("Hello World from Urho3D!");
helloText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 30);
helloText->SetColor(Color(0.0f, 1.0f, 0.0f));
helloText->SetHorizontalAlignment(HA_CENTER);
helloText->SetVerticalAlignment(VA_CENTER);
ui->GetRoot()->AddChild(helloText);
//========================================================================
SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(Game, HandleUpdate));
InitMouseMode(MM_FREE);
}
void Game::HandleUpdate(StringHash eventType, VariantMap& eventData)
{
}
void Game::Stop()
{
engine_->DumpResources(true);
}
void Game::InitTouchInput()
{
touchEnabled_ = true;
ResourceCache* cache = GetSubsystem<ResourceCache>();
Input* input = GetSubsystem<Input>();
XMLFile* layout = cache->GetResource<XMLFile>("UI/ScreenJoystick_Simple.xml");
const String& patchString = GetScreenJoystickPatchString();
if (!patchString.Empty())
{
SharedPtr<XMLFile> patchFile(new XMLFile(context_));
if (patchFile->FromString(patchString))
layout->Patch(patchFile);
}
screenJoystickIndex_ = input->AddScreenJoystick(layout, cache->GetResource<XMLFile>("UI/DefaultStyle.xml"));
input->SetScreenJoystickVisible(screenJoystickSettingsIndex_, true);
}
void Game::InitMouseMode(MouseMode mode)
{
useMouseMode_ = mode;
Input* input = GetSubsystem<Input>();
if (GetPlatform() != "Web")
{
if (useMouseMode_ == MM_FREE)
input->SetMouseVisible(true);
Console* console = GetSubsystem<Console>();
if (useMouseMode_ != MM_ABSOLUTE)
{
input->SetMouseMode(useMouseMode_);
if (console && console->IsVisible())
input->SetMouseMode(MM_ABSOLUTE, true);
}
}
else
{
input->SetMouseVisible(true);
SubscribeToEvent(E_MOUSEBUTTONDOWN, URHO3D_HANDLER(Game, HandleMouseModeRequest));
SubscribeToEvent(E_MOUSEMODECHANGED, URHO3D_HANDLER(Game, HandleMouseModeChange));
}
}
void Game::SetLogoVisible(bool enable)
{
if (logoSprite_)
logoSprite_->SetVisible(enable);
}
void Game::HandleKeyUp(StringHash eventType, VariantMap& eventData)
{
using namespace KeyUp;
int key = eventData[P_KEY].GetInt();
if (key == KEY_ESCAPE)
{
Console* console = GetSubsystem<Console>();
if (console->IsVisible())
console->SetVisible(false);
else
{
if (GetPlatform() == "Web")
{
GetSubsystem<Input>()->SetMouseVisible(true);
if (useMouseMode_ != MM_ABSOLUTE)
GetSubsystem<Input>()->SetMouseMode(MM_FREE);
}
else
engine_->Exit();
}
}
}
void Game::HandleKeyDown(StringHash eventType, VariantMap& eventData)
{
using namespace KeyDown;
int key = eventData[P_KEY].GetInt();
if (key == KEY_F1)
GetSubsystem<Console>()->Toggle();
else if (key == KEY_F2)
GetSubsystem<DebugHud>()->ToggleAll();
else if (!GetSubsystem<UI>()->GetFocusElement())
{
Renderer* renderer = GetSubsystem<Renderer>();
if (key == KEY_SELECT && touchEnabled_)
{
paused_ = !paused_;
Input* input = GetSubsystem<Input>();
if (screenJoystickSettingsIndex_ == M_MAX_UNSIGNED)
{
ResourceCache* cache = GetSubsystem<ResourceCache>();
screenJoystickSettingsIndex_ = input->AddScreenJoystick(cache->GetResource<XMLFile>("UI/ScreenJoystickSettings_Samples.xml"), cache->GetResource<XMLFile>("UI/DefaultStyle.xml"));
}
else
input->SetScreenJoystickVisible(screenJoystickSettingsIndex_, paused_);
}
else if (key == '1')
{
int quality = renderer->GetTextureQuality();
++quality;
if (quality > QUALITY_HIGH)
quality = QUALITY_LOW;
renderer->SetTextureQuality(quality);
}
else if (key == '2')
{
int quality = renderer->GetMaterialQuality();
++quality;
if (quality > QUALITY_HIGH)
quality = QUALITY_LOW;
renderer->SetMaterialQuality(quality);
}
else if (key == '3')
renderer->SetSpecularLighting(!renderer->GetSpecularLighting());
else if (key == '4')
renderer->SetDrawShadows(!renderer->GetDrawShadows());
else if (key == '5')
{
int shadowMapSize = renderer->GetShadowMapSize();
shadowMapSize *= 2;
if (shadowMapSize > 2048)
shadowMapSize = 512;
renderer->SetShadowMapSize(shadowMapSize);
}
else if (key == '6')
{
ShadowQuality quality = renderer->GetShadowQuality();
quality = (ShadowQuality)(quality + 1);
if (quality > SHADOWQUALITY_BLUR_VSM)
quality = SHADOWQUALITY_SIMPLE_16BIT;
renderer->SetShadowQuality(quality);
}
else if (key == '7')
{
bool occlusion = renderer->GetMaxOccluderTriangles() > 0;
occlusion = !occlusion;
renderer->SetMaxOccluderTriangles(occlusion ? 5000 : 0);
}
else if (key == '8')
renderer->SetDynamicInstancing(!renderer->GetDynamicInstancing());
else if (key == '9')
{
Graphics* graphics = GetSubsystem<Graphics>();
Image screenshot(context_);
graphics->TakeScreenShot(screenshot);
screenshot.SavePNG(GetSubsystem<FileSystem>()->GetProgramDir() + "Data/Screenshot_" +
Time::GetTimeStamp().Replaced(':', '_').Replaced('.', '_').Replaced(' ', '_') + ".png");
}
}
}
void Game::HandleSceneUpdate(StringHash eventType, VariantMap& eventData)
{
if (touchEnabled_ && cameraNode_)
{
Input* input = GetSubsystem<Input>();
for (unsigned i = 0; i < input->GetNumTouches(); ++i)
{
TouchState* state = input->GetTouch(i);
if (!state->touchedElement_) // Touch on empty space
{
if (state->delta_.x_ || state->delta_.y_)
{
Camera* camera = cameraNode_->GetComponent<Camera>();
if (!camera)
return;
Graphics* graphics = GetSubsystem<Graphics>();
yaw_ += TOUCH_SENSITIVITY * camera->GetFov() / graphics->GetHeight() * state->delta_.x_;
pitch_ += TOUCH_SENSITIVITY * camera->GetFov() / graphics->GetHeight() * state->delta_.y_;
cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
}
else
{
Cursor* cursor = GetSubsystem<UI>()->GetCursor();
if (cursor && cursor->IsVisible())
cursor->SetPosition(state->position_);
}
}
}
}
}
void Game::HandleTouchBegin(StringHash eventType, VariantMap& eventData)
{
InitTouchInput();
UnsubscribeFromEvent("TouchBegin");
}
void Game::HandleMouseModeRequest(StringHash eventType, VariantMap& eventData)
{
Console* console = GetSubsystem<Console>();
if (console && console->IsVisible())
return;
Input* input = GetSubsystem<Input>();
if (useMouseMode_ == MM_ABSOLUTE)
input->SetMouseVisible(false);
else if (useMouseMode_ == MM_FREE)
input->SetMouseVisible(true);
input->SetMouseMode(useMouseMode_);
}
void Game::HandleMouseModeChange(StringHash eventType, VariantMap& eventData)
{
Input* input = GetSubsystem<Input>();
bool mouseLocked = eventData[MouseModeChanged::P_MOUSELOCKED].GetBool();
input->SetMouseVisible(!mouseLocked);
}