【翻译】Orx官方教程:10.独立程序和本地化 stand alone & localization

本文译自 orx tutorials 的 独立程序与本地化教程 (stand alone & localization),六月流光 译。最新版本见Orx 官方中文Wiki 。 本文转自六月流光的 博客 。原文链接在: http://yatusiter.blogbus.com/logs/68485225.html 。望有新人能够加入这个翻译者的队伍,早日将 Orx的WIKI页中文化。有兴趣的请加入73063577,并与我取得联系,防止重复翻译。

StandAlone tutorial

独立程序与本地化教程(stand alone & localization)

Summary

综述

This is our first basic C++ tutorial. It also shows how to write a stand alone executable using orx and how to use the localization module (orxLOCALE).

这是我们的第一个C++基础教程。它也展示了如何使用orx编写独立的可执行文件和使用本地化模 块(orxLOCALE)。

As we are NOT using the default executable anymore for this tutorial, its code will be directly compiled into the executable and not into an external library.

由 于我们在此教程中不再使用默认的可执行文件,它的代码会被直接编译成可执行文件而不是外部库。

This implies that we will NOT have the default hardcoded behavior we had in the previous tutorials:

这暗示了我们不再有前几个教程中的如下默认行 为:(译注:内置于默认可执行文件,同时下面的几点原本也都是否定句式,但与这里的否定矛盾,原意应该是下面这些特性会在StandAlone版本中失 效)

  • F11 will not affect vertical sync toggler
  • Escape won't automatically exit
  • F12 won't capture a screenshot
  • Backspace won't reload configuration files
  • the [Main] section in the config file won't be used to load a plugin (“GameFile” key)
  • F11 切换垂直同步
  • Escape 退出
  • F12 截屏
  • 退格键(Backspace) 重新载入全部配置文件
  • 配置文件中的[Main]配置段被用于加载一个插件(“GameFile” 键)


A program based directly on orx 1) , by default, will also NOT exit if it receives the orxSYSTEM_EVENT_CLOSE event.To do so, we will either have to use the helper orx_Execute() function ( see below ) or handle it ourselves.

一个直接基于orx的程序(注释1,即不再依靠 ORX 启动器),默认收到orxSYSTEM_EVENT_CLOSE事件后不退出。为了改变这种情况,我们必须使用orx_Execute() 函数(如下link)或者自己处理。

See previous basic tutorials for more info about basic object creation , clock handling , frames hierarchy , animations , cameras & viewports , sounds & musics , FXs , physics and scrolling .

查看之前关于basic object creation , clock handling , frames hierarchy , animations , cameras & viewports , sounds & musics , FXs , physics and scrolling 的基础教程以获得更多的信息。

As we're on our own here, we need to write the main function and initialize orx manually.The good thing is that we can then specify which modules we want to use, and deactivates display or any other module at will, if needed.

我们现在只能靠自己了,所以必须自己写main 函数和手动初始化orx。

好 处是如果需要,我们可以任意指定要使用哪些模块,停用显示模块或其他模块。

If we still want a semi-automated initialization of orx, we can use the orx_Execute() function.This tutorial will cover the use of orx with this helper function, but you can decide not to use it if its behavior doesn't suit your needs.

如果我们还是想要半自动初始化orx,我们可以 使用orx_Execute()函数。

本教程将通过这个辅助函数使用orx,但是如果它的行为不符合你的需求也可以不使用。

This helper function will take care of initializing everything correctly and exiting properly.

It will also make sure the clock module is constantly ticked (as it's part of orx's core) and that we exit if the orxSYSTEM_EVENT_CLOSE event is sent.

This event is sent when closing the windows, for example, but it can also be sent under your own criteria (escape key pressed, for example).

这个辅助函数将会确保所有部分正确初始化和妥善 退出。

它 也将确保clock模块正常运作(作为 orx的核心部分)并且当(备注,一个模块不断被触发,不太好理解,事实上是表示clock一直在正常计时)

orxSYSTEM_EVENT_CLOSE事 件发送时退出。

此 事件在关闭窗口时发送,但是也可以按你的标准发送(例如按下Esc键)。

This code is also a basic C++ example to show how to use orx without having to write C code.

This tutorial could have been architectured in a better way (cutting it into pieces with headers files, for example) but we wanted to keep a single file per *basic* tutorial.

这 些代码同样也是一个基础的C++示例,用来演示如何通过C语言以外的语言来使用orx。

本教程本来应该用另外一种更好的方式组织起来 (例如分割成多个头文件)但是我们希望每个基础教程都只有一个文件。

This stand alone executable also creates a console (as does the default orx executable), but you can have you own console-less program if you wish.

In order to achieve that, you only need to provide an argc/argv style parameter list that contains the executable name.

If you don't, the default loaded config file will be orx.ini instead of being based on our executable name (ie. 10_StandAlone.ini).

这个独立可执行文件会创建一个终端(和默认的 orx可执行文件一样),但是如果你喜欢也可以创建一个没有终端的可执行文件。

为了实现上述目标,你只需要提供一个包含该可执行文件名的argc/argv风格的 参数表即可。

如 果不这样的话,默认加载的配置文件是orx.ini,取代基于可执行文件名的配置文件(例如 10_StandAlone.ini)。

For visual studio users (windows), it can easily be achieved by writing a WinMain() function instead of main(), and by getting the executable name (or hardcoding it, as it's shamelessly done in this tutorial

对visual studio 用户(windows)而言,可以轻松通过撰写WinMain() 替代main() 函数,并且获取可执行文件的名称(或者硬编码实现,正如本教程中可耻地这么做鸟^.^) 。

This tutorial simply display orx's logo and a localized legend. Press space or click left mouse button to cycle through all the availables languages for the legend's text.

本 教程简单的现实了orx的logo和一个本地化的说明。按空格键或者点击鼠标左键以循环显示所有可用语言的说明文本。

Some explanations about core elements that you can find in this tutorial:

下面是一些你可以在本教程中找到的核心元素的解 释:

  • Run function: Don't put *ANY* logic code here, it's only a backbone where you can handle default core behaviors (tracking exit or changing locale, for example) or profile some stuff. As it's directly called from the main loop and not part of the clock system, time consistency can't be enforced. For all your main game execution, please create (or use an existing) clock and register your callback to it.
  • 运行 函数(Run function):不要在这里放置任何逻辑代码,它只是一个处理默认核心行为(例如跟踪退出或者更改locale)或简要描述一些东西的主干。由于它直 接从main循环中调用并且不是clock系统的的一部分,时间一致性无法被强制执行。对你所有的主游戏可执行文件,请在此创建(或者使用已有 的)clock并且注册你的回调函数。
  • Event handlers: When an event handler returns orxSTATUS_SUCCESS, no other handler will be called after it for the same event. On the other hand, if orxSTATUS_FAILURE is returned, event processing will continue for this event if other handlers are listening this event type. We'll monitor locale events to update our legend's text when the selected language is changed.
  • 事件处理器(Event handlers):当一个事件响应函数返回orxSTATUS_SUCCESS后,对此事件就不会再调用其他事件响应函数。另一方面,如果返回的是 orxSTATUS_FAILURE,事件会传递给其他正在监听这个事件的响应函数。我们将监视locale事情以便当选择的语言切换时更新我们的说明文 本。(备注:这个翻译有点乱,主要要了解Orx的事件处理方式)
  • orx_Execute(): Inits and executes orx using our self-defined functions (Init, Run and Exit). We can of course not use this helper and handles everything manually if its behavior doesn't suit our needs. You can have a look at the content of orx_Execute() 2) to have a better idea on how to do this.
  • orx_Execute():通过我们自定义 的函数(Init,Run和Exit)初始化和执行orx。当然,如果它不符合我们的需求,我们可以不使用这个辅助函数并且手动处理所有事情。你可以通过 参考orx_Execute()的内容(注2,在orx.h中实现)了解怎么这样做。(备注,作者的better idea不是说更好的注意,而是对于how to do this的修饰,就是我们不知道时候说,I have no idea一样)



Details
详细说明

Let's start with the includes.

让 我们从包含的文件开始。

#include "orx.h"

That's all you need to include so as to use orx. This include works equally with a C or a C++ compiler 3) .


这就是你要使用orx所需要包含的唯一文件。它在C和C++(注释3,在这种情 况下预编译宏 __orxCPP__ 会被自动定义)编译器下都工作良好。

Let's now have a look at our StandAlone class that contains orx's Init(), Run() and Exit() callbacks.

现在看下我们的StandAlone类,包含orx Init()、Run()和Exit()回调函数。

class StandAlone
{
public:
static orxSTATUS orxFASTCALL EventHandler(const orxEVENT *_pstEvent);
static orxSTATUS orxFASTCALL Init();
static void orxFASTCALL Exit();
static orxSTATUS orxFASTCALL Run();
void SelectNextLanguage();

StandAlone() : m_poLogo(NULL), s32LanguageIndex(0) {};
~StandAlone() {};
private:
orxSTATUS InitGame();
Logo *m_poLogo;
orxS32 s32LanguageIndex;
};

All the callbacks could actually have been defined out of any class. This is done here just to show how to do it if you need it.
We see that our StandAlone class also contains our Logo object and an index to the current selected language.

所有的回调函数实际上都可以定义在任何类之外。这里这么做只是演示当你需要的时候你可以这么做。 我们看到StandAlone类也包含了我们的logo对象和一个当前选中的语言的索引。

Let's now have a look to our Logo class definition.
现在看一下Logo类的定义:

class Logo
{
private:
orxOBJECT *m_pstObject;
orxOBJECT *m_pstLegend;
public:
Logo();
~Logo();
};

Nothing fancy here, we have a reference to an orxOBJECT that will be our logo and another one that will be the displayed localized legend.
As you'll see we won't use the reference at all in this executable, we just keep them so as to show a proper cleaning when our Logo object is destroyed. If we don't do it manually, orx will take care of it when quitting anyway.

这里没有什么特别的,我们用指针指向一个 orxOBJECT作为我们的logo,另一个用来指向显示的本地化说明。如你所见,我们在这个可执行文件里将不会使用这个可执行文件的所有引用,我们只 是保持它们以便显示在销毁Logo对象时被正确地清理。如果我们不手动做,在退出的时候orx会替我们搞定。

Let's now see its constructor.
现在让我们看一下它的构造函数:
Logo::Logo()
{
m_pstObject = orxObject_CreateFromConfig("Logo");
orxObject_SetUserData(m_pstObject, this);
m_pstLegend = orxObject_CreateFromConfig("Legend");
}

As seen in the previous tutorials we create our two objects (Logo and Legend) and we link our Logo C++ object to its orx equivalent using orxObject_SetUserData().

用前面的教程中讲的方法,我们创建了两个对象 (Logo和Legend)并且我们通过
orxObject_SetUserData()把Logo C++对象链接到它对应的orx对象上。

Logo::~Logo()
{
orxObject_Delete(m_pstObject);
orxObject_Delete(m_pstLegend);
}

Simple cleaning here as we only delete our both objects.

这里只是简单的删除了我们的两个对象。

Let's now see our main function.
现 在看看我们的main函数:

int main(int argc, char **argv)
{
orx_Execute(argc, argv, StandAlone::Init, StandAlone::Run, StandAlone::Exit);
return EXIT_SUCCESS;
}

As we can see, we're using the orx_Execute() helper that will initialize and execute orx for us.
In order to do so, we need to provide it our executable name and the command line parameters along with three callbacks: Init(), Run() and Exit().


正如我们看见的,我们使用 orx_Execute() 辅助函数来初始化和执行orx。
这样我们需要提供我们的可执行文件名称和命令行参数和三个回调函数:Init()、Run() 和Exit()。

We will only exit from this helper function when orx quits.

只有当orx退出时我们才从这个辅助函数退出。

Let's have a quick glance at the console-less version for windows.
我们快速浏览一下windows的无终端版本。

#ifdef __orxMSVC__
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE PrevInstance,
LPSTR lpCmdLine, int nCmdShow) {
// Inits and executes orx
orx_WinExecute(StandAlone::Init, StandAlone::Run, StandAlone::Exit);
// Done!
return EXIT_SUCCESS;
} #endif

Same as for the traditional main() version except that we use the orx_WinExecute() helper that will compute the correct command line parameters and use it. 4)(the ones given as parameter don't contain the executable name which is needed to determine the main config file name

This only works for a console-less windows game 5) .(which uses WinMain() instead of main())

和 旧的main() 版本一样除了我们使用orx_WinExecute()辅助函数来计算正确的命令行参数并使用它(注释4,这些给出的参数并没有包含我们需要的用来决定主 配置文件名的可执行文件的名字)。
这只能在一个无命令行(终端)的windows游戏下工作。(注释5,使用WinMain()替代main())

Let's now see how our Init() code looks like.
现在看看我们的Init() 代码是怎么样的:

orxSTATUS StandAlone::Init()
font-size: 11pt; font-family: Arial; color: #000000; background-color: transparent; font-weight: normal; font-style: normal; tex

你可能感兴趣的:(游戏,C++,c,windows,C#)