昨天写了一篇urho3D的博文,但觉得有些不够,再写这篇博文补充一些东西吧。

Urho3D web版编译

       Urho3D web版编译还是挺简单的,只需要emscripten

# cd to emsdk path and active emsdk
source emsdk_env.sh
# cd to urho3D path
mkdir build
./cmake_emscripten.sh ./build -DURHO3D_TESTING=1
cd build
make -j 7

# run demos
cd bin
python -m SimpleHTTPServer

URHO3D_TESTING开启,emscripten才会编译html文件。否则只生成js和data文件

       Urho3D官方也提供了web demos展示页面,链接在这里。可以直接打开实验,不过emscripten编译出的urho3D的js经常有超过10m接近20m的情况,有时浏览器会因为超时而中断素材文件的下载。

Urho3D hello world

       Urho3D默认集成了渲染相关的全部组件,因此渲染一个hello world出来真不是问题。

搭建Urho3D开发环境

       这步可以参考我昨天的博文

初始化引擎,依旧主渲染循环格式

       这里使用一个最简单的模板,来初始化引擎,并声明游戏循环,同时绑定按esc退出游戏。

#include <Urho3D/Engine/Application.h>
#include <Urho3D/Engine/Engine.h>
#include <Urho3D/Input/InputEvents.h>
using namespace Urho3D;
class MyApp : public Application
{
public:
    MyApp(Context* context) :
        Application(context)
    {
    }
    virtual void Setup()
    {
        // Called before engine initialization. engineParameters_ member variable can be modified here
    }
    virtual void Start()
    {
        // Called after engine initialization. Setup application & subscribe to events here
        SubscribeToEvent(E_KEYDOWN, URHO3D_HANDLER(MyApp, HandleKeyDown));
    }
    virtual void Stop()
    {
        // Perform optional cleanup after main loop has terminated
    }
    void HandleKeyDown(StringHash eventType, VariantMap& eventData)
    {
        using namespace KeyDown;
        // Check for pressing ESC. Note the engine_ member variable for convenience access to the Engine object
        int key = eventData[P_KEY].GetInt();
        if (key == KEY_ESCAPE)
            engine_->Exit();
    }
};
URHO3D_DEFINE_APPLICATION_MAIN(MyApp)

设置使用窗口化运行游戏,并设置窗口标题

       添加头文件#include <Urho3D/Engine/EngineDefs.h>,然后修改Setup函数

    virtual void Setup()
    {
        engineParameters_[Urho3D::EP_WINDOW_TITLE] = "TTTTTEEEESSSTT";
        engineParameters_[Urho3D::EP_FULL_SCREEN]  = false;
    }

创建字体渲染函数

       在MyApp类中新建一个CreateText()函数

    void CreateText()
    {
        Urho3D::ResourceCache* cache = GetSubsystem<Urho3D::ResourceCache>();

        // Construct new Text object
        Urho3D::SharedPtr<Urho3D::Text> helloText(new Urho3D::Text(context_));

//        // Set String to display
//        helloText->SetText("Hello World from Urho3D!");
//
//        // Set font and text color
//        helloText->SetFont(cache->GetResource<Urho3D::Font>("Fonts/Anonymous Pro.ttf"), 30);
//        helloText->SetFont(cache->GetResource<Urho3D::Font>("Fonts/BlueHighway.ttf"), 30);
//        helloText->SetColor(Urho3D::Color(0.0f, 1.0f, 0.0f));

        // Set String to display
        helloText->SetText("你好微软雅黑 from Urho3D!");

        // Set font and text color
        helloText->SetFont(cache->GetResource<Urho3D::Font>("Fonts/Msyh.ttf"), 30);
        helloText->SetColor(Urho3D::Color(0.0f, 1.0f, 0.0f));

        // Align Text center-screen
        helloText->SetHorizontalAlignment(Urho3D::HA_CENTER);
        helloText->SetVerticalAlignment(Urho3D::VA_CENTER);

        // Add Text instance to the UI root element
        GetSubsystem<Urho3D::UI>()->GetRoot()->AddChild(helloText);
    }

Anonymous Pro.ttf和BlueHighway.ttf是urho3D自带的,微软雅黑字体是我自己主动放到Fonts文件夹下的

修改start函数

       在start函数中调用CreateText函数,就能显示helloworld

    virtual void Start()
    {
        // Create "Hello World" Text
        CreateText();

        SubscribeToEvent(Urho3D::E_KEYDOWN, URHO3D_HANDLER(MyApp, HandleKeyDown));
    }