• About
    • Resume
A Game Developer also plays some guitar

Tag Archives: Game Engine

在Moai Forum的问题帖

August 21, 2011 10:26 am / Leave a Comment / Benny Chen

(关于Moai,见我的上一篇文章。)

下面贴出在Moai Forum上我最近的两个问题帖。回答者都是Patrick,他是Moai引擎的领导者,同时也是’Zipline Games’的CTO。他似乎是个不错的人,在我每次贴出问题后,总是在最快的时间内解答。

1. Does MOAILayer2D only support 512 props maximumly?

http://getmoai.com/forums/moai-sdk-developer-support/does-moailayer2d-only-support-512-props-maximumly/

Me:

I was trying to put lots of props onto a layer with function MOAILayer2D:insertProp, but found that if I inserted more than 512 props, all the props will fail to render on the layer. Is 512 the threshold for a layer’s prop count? Why is it a restriction? 512 doesn’t suffice my need. If there did exist a restriction, Moai even didn’t tell me any info or warning about the number 512, it took me lots of time to detect the problem and the number… 🙁

Patrick:

Sorry you had to spend your time on that. I agree that we could emit better warnings for situations like that.

The 512 restriction is arbitrary. It’s the size of the buffer created on the stack to sort the prims. I can fix it for you in the next release – will grow the buffer dynamically instead. When I have time to add an option to disable sorting by MOAILayer2D, the buffer won’t be needed at all.

If you need a fix immediately and are building from source, you can go into MOAILayer2D and just change it to something else.

(512 props is a lot! Is this for a mobile game? Mind if I ask what you’re displaying? Depending on what it is, may be other ways I can help.)

Patrick:

OK. I removed MAX_RENDERABLES and now use a flexible size buffer to gather and sort the props returned from the partition. That will be in the upcoming release. Try it out and let me know.

If you run into stuff like this in the future (any kind of odd or mysterious behavior) please let me know and I’ll hop right on it. Would much rather deal with a few false positives than have you waste your time!

Me:

Thanks so much for your reply and help, Patrick.

I’m just new to Moai. I was generating and displaying a map (31×28 grids) with each grid empty or filled with a type of sprite (like a the Pacman game, each grid could be filled with a Wall or a Bean or …). So this is why I used so MANY props. Do you have some other way or suggestions for generating such kind of game maps, so that not that many props will be used?

As you’ve fixed this issue, so I’m looking forward to the next release. Thanks a bunch!

Patrick:

Absolutely. Check out MOAITileDeck2D and MOAIGrid. Any of the deck types can be used in conjunction with a grid, but MOAITileDeck2D is ideally suited. The grid draws with one transform (from the prop it’s attached to) and also gets viewport culled on a per tile basis. It’s ideally suited to tiled backgrounds. You will get way better performance and can also update the grid values at runtime or animate them using a MOAIDeckRemapper. Check out tilemap-animated for a sample. In the games we are working on in-house we have multiple layers of tilemap parallax drawing hundreds and hundred of tiles at really good frame rates. If you need to animate a tile flipping or rotating (like what happens to a block in a Mario game when Mario hits it with his head), just hide the original tile in the grid and replace it with a prop – you can use the same tile deck as the source for the prop.

Also, can’t say enough good things about Tiled (mapeditor.org). The latest version supports flip flags and can output its data as Lua tables you can then load in to Moai (with a little bit of work).

Always a pleasure to help out, so let me know if you have more questions as you go.

Me:

Oh, how stupid I was that I didn’t find these classes, I will try these MOAI classes later. THANK YOU!

2. Questions about MOAIAction:stop

http://getmoai.com/forums/moai-sdk-developer-support/questions-about-moaiactionstop/

Me:

I have some questions regarding the usage of MOAIThread and MOAIAction

Firstly check out the code snippets below.

MOAISim.openWindow ( "test", 320, 480 )</span>
viewport = MOAIViewport.new ()
viewport:setSize ( 320, 480 )
viewport:setScale ( 320, 480 )

layer = MOAILayer2D.new ()
layer:setViewport ( viewport )
MOAISim.pushRenderPass ( layer )

gfxQuad = MOAIGfxQuad2D.new ()
gfxQuad:setTexture ( "cathead.png" )
gfxQuad:setRect ( -64, -64, 64, 64 )

prop = MOAIProp2D.new ()
prop:setDeck ( gfxQuad )
layer:insertProp ( prop )

function moveThreadMain()
	movingAction = prop:moveLoc( 1000, 0, 100, MOAIEaseType.LINEAR )
	local x
	local y
	while ( movingAction:isBusy() )
	do
		x, y = prop:getLoc()
		print( "current x - " .. x )
		coroutine.yield()
	end
end

isCrossBound = false
function checkThreadMain()
	while ( true )
	do
		local x
		local y
		x, y = prop:getLoc()
		if ( isCrossBound == false )
		then
			if ( x > 20 )
			then
				isCrossBound = true
				movingAction:stop()
				print( "x after stop current frame - " .. x )
			end
		else
			print( "x after stop next frame - " .. x )
			break
		end
		coroutine.yield()
	end
end

moveThread = MOAIThread.new()
checkThread = MOAIThread.new()
moveThread:run( moveThreadMain )
checkThread:run( checkThreadMain )

I start and run 2 threads. The ‘moveThread’ uses MOAIAction to move the prop continuously and print its location each frame, while another ‘checkThread’ checks if the prop crosses some bound and if yes stops the moving animation with function MOAIAction:stop

But the result of MOAIAction:stop differs based on the launching sequence of those above 2 threads.

If I run ‘checkThread’ before ‘moveThread’, here is the result. MOAIAction::stop successfully stopped moving prop.

x after stop current frame – 20.099973678589
x after stop next frame – 20.099973678589

But if I run ‘movingThread’ before ‘checkThread’, I will get the following result, seems that animation will continue running for another frame

x after stop current frame – 20.099973678589
x after stop next frame – 20.199974060059

This really confuses me. Can anyone do me a favor to explain at which condition MOAIAction will run an additional frame after stopped, and when not? Is there a way to ensure that MOAIAction:stop will run no more frame no matter the running sequence of MOAIThread?

Patrick:

Just ran your code. It’s a legitimate bug in the way the MOAIAction update loop works. I’ve fixed it and checked it in to ‘master’ on moai-dev. We should have the next binary release out early next week.
Thanks for finding this!

Posted in: Game Programming / Tagged: 2d, forum, game engine, Moai, MOAIAction, MOAILayer2D, MOAIThread

Moai

August 21, 2011 10:24 am / Leave a Comment / Benny Chen

最近通过澳洲朋友Andrew的介绍,开始使用了一款正在开发中的开源游戏引擎Moai – the mobile platform for pro game developers.

Moai是一款由’Zipline Games’公司开发的2D游戏引擎,我写这篇文章时的版本是0.5 Beta。它最大的特点就是集成了Lua脚本语言,它提供了一系列class-based的Lua API。开发者一般只需要通过写Lua脚本,即可实现一款游戏。而引擎本身解决了跨平台的问题,开发完的游戏可以顺利的发布到iOS和Android平台。而引擎本身提供的功能上,也基本上覆盖了需要开发一款2D游戏所有的元素,设备和输入,2D Sprite,动画,字体,粒子系统,物理,声音等。Moai的另外一个重要特色是提供了一个它称之为Moai Cloud的云服务,对于这个我还没有深研究,不过据称它可以让需要后端server的游戏变得简单,用户同样只需要用Lua来编写server的逻辑代码,而至于像scale-up这样的问题完全可以交给Moai引擎来处理。

然而开源引擎,尤其是处于Beta测试中的开源引擎,想用于开发正式的游戏确实有些不可靠。我最近在把之前写过的一个Pacman 2D移植到Moai上,但是遇到了很多的问题,并且一些问题最终通过层层纠结后证实是引擎本身的问题。虽然过程坎坷,但因为参与开源引擎项目,并有了自己的contribution,这本身着实是一件令人欣慰而振奋的事情。

但是希望Moai的开发者们work harder,让Moai尽快变得更好 😀

注册并获取Moai:http://dashboard.moaicloud.com/

Moai入门文章:
第一部分:http://getmoai.com/2011/04/moai-basics-part-1/
第二部分: http://getmoai.com/2011/04/moai-basics-part-2-2/

Moai API Documentation:http://getmoai.com/docs/

Posted in: Game Programming / Tagged: 2d, beta, cloud, game engine, lua, Moai, mobile, zipline games, 开源

关于游戏引擎多线程的一些整理和思考

January 5, 2011 9:05 pm / 2 Comments / Benny Chen

在最近的项目中,由于时间紧任务重,我果断放弃了引擎原来有点畸形的多线程设计,退回到单线程。最后事实证明,这是一个明智的决定,不但效率没有降低,同时那些永无止境的多线程bug也一并被消除,系统稳定了很多,最终项目得以在deadline之前有一个安全的交付。

1.是否多线程

去掉多线程的主要原因是,原来的多线程设计本身就是一个未经过深思熟虑的设计,它几乎是在项目开发的中后期突然插入的。整个引擎的最初设计没有考虑多线程,现在突然引入了多线程,而又没有经过精心的设计,那结果就是各种多线程bug扑面而来,让开发人员焦头烂额。事实是,如果需要多线程,那最好应该从项目设计架构的第一天开始就仔细考虑。

直观上感觉,好像使用多线程就会带来性能的提升。其实不然,如果没有经过精心的设计,随便引入多线程,只会适得其反。不但性能没有得到提升,而且引入多线程后带来的庞大开发代价和潜在的多线程bug,会让你和团队崩溃到吐血,典型的吃力不讨好的事情。相比于单线程,引入多线程的设计需要带来2到3倍的开发和测试代价,在这么一个时间紧急的项目上,在项目的中后期突然引入多线程真是个大错特错的决策,应该果断拿掉。在这篇博客文章“一种3D引擎的多线程设计方案”里,作者就提到,“多线程的引入使引擎变得更加复杂,不良的设计带来的性能提升非常有限,甚至在单核环境下还会出现明显的性能下降”。

从这篇Unreal引擎工程师Tim Sweeney的访谈文章中,我找到了在我的情况中应该拒绝多线程的另一个重要原因。

Tim提到:

For multithreading optimizations, we’re focusing on physics, animation updates, the renderer’s scene traversal loop, sound updates, and content streaming. We are not attempting to multithread systems that are highly sequential and object-oriented, such as the gameplay.

This is also why it’s especially important to focus multithreading efforts on the self-contained and performance-critical subsystems in an engine that offer the most potential performance gain. You definitely don’t want to execute your 150,000 lines of object-oriented gameplay logic across multiple threads – the combinatorical complexity of all of the interactions is beyond what a team can economically manage. But if you’re looking at handing off physics calculations or animation updates to threads, that becomes a more tractable problem.

Tim的主要思想是,游戏引擎多线程应该关注在“独立自主且性能关键的子系统”上,比如物理系统或者是动画系统,这会带来最大的性能提升。而在我的项目中,多线程是放在了脚本(scripting)层,这正中了上面Tim所提到的多线程的雷区——gameplay。脚本是处理gameplay层次的逻辑的,它是高度顺序且面向对象的,并且与其他各个子系统有着千丝万缕的关联。正如Tim所说的,将脚本逻辑放到多线程来执行所带来的交互的复杂度已经超过一个团队的承受范围了。

从这篇文章中我们也可以获知,像Unreal这样知名的商业引擎,也是到最新的第3版(这种称为下一代的游戏引擎)中才开始支持了多线程,这从一个侧面反映了多线程技术在游戏引擎中是一项高级而复杂的技术。主要原因这受制于硬件的发展,多核CPU的PC是到近些年才开始在市场中变为主流,在单核CPU中使用多线程会带来额外的开销,所以在传统的游戏开发中,往往都是单线程的。(这里是一个很好的关于用户软硬件使用情况统计的网站,不过遗憾的是统计数据基本都是来源于国外,不带中国人玩的,对于国内没什么参考价值)

另外对于游戏引擎引入多线程的考虑,还有很重要的一点是,我们需要认真的考虑当前引擎的性能具体是受限在哪里,是CPU还是GPU,我们该在哪里进行优化。因为GPU渲染在游戏中是如此的重要,所以在大多数情况下,很可能GPU才是主要的性能瓶颈。只有真正的当物理,AI等这些模块变得越来越重要,并且约束系统性能时,我们才需要仔细考虑是否需要为游戏引擎带来多线程的可能。

总之,多线程就是一把双刃剑,在决定使用它之前,一定要经过明智而审慎的思考和设计。

2.如何多线程

对于游戏引擎的多线程设计,最近读到了一些相当不错的文章,在这里作个整理。

Gamasutra上的Multithreaded Game Engine Architectures是一篇非常基础且有意思的文章。

该文章首先指出,程序并行有两种主流的方式,第一种称为函数并行(functional parallelism),另一种称为数据并行(data parallelism)。所谓functional parallelism,即将程序中不同的任务分配给不同的线程并行执行,而data parallelism则是将数据划分到不同的线程上并行执行,但每一个线程执行的是相同的任务。这样的分类比较利于我们更好的理解程序并行的行为,我们可以在很多地方看到有这样的分类,比如在Intel的这篇关于并行引擎架构的文章中,提到了类似的functional decomposition和data decomposition,而John Owens在Siggraph 2008的talk “Parallel Programming Models Overview”中,也提到了这样的分类,他称之为task parallelism和data parallelism.

基于这些并行方式,该文章提出了游戏引擎多线程的3种架构。

前两种架构都是基于functional parallelism的方式,第一种架构称为同步的函数并行模型(synchronous function parallel model),是最常见的一种游戏引擎多线程方式。该方式保留传统的game loop,将完全独立的子系统的任务分离到辅线程,并与主线程并行执行,每帧作同步。Intel的Multi-threaded Rendering and Physics Simulation这篇文章就是这种方式的典型。

第二种架构称为异步的函数并行模型 (asynchronous function parallel model),是一种非常新颖而前瞻的架构,它脱离了传统的game loop,各个子系统跑在各自的线程上,并按自己的节奏拥有独立的game loop,该架构拥有非常好的可扩展性,但难点就是不同子系统之间的数据同步问题。对于这种架构方式,Intel的这篇Designing the Framework of a Parallel Game Engine是一篇非常棒的文章,极力推荐,该文章提出了Free Step Mode和Lock Step Mode两种并行执行的模式,不仅仅是并行架构,从这篇文章中还能学到其他很多关于游戏架构的知识。

第三种架构是基于data parallelism的方式,文章中称之为数据并行模型(data parallel model)。这种方式我们并不陌生,比如我们经常听到的GPGPU就是这种方式在GPU上的体现,另外我们所熟知的SIMD(Single Instruction Multiple Data),SPMD(Single Program Multiple Data)技术都是这种方式的典型。该方式可以获得最大的并行性,但缺点是采用数据并行的系统往往非常难于设计,通常需要修改原来的面向对象的设计。可以看看Intel的这篇TickerTape的文章,利用SIMD技术提升性能,但却需要破坏原有的类的设计,将结构数组(Array of Structures)改为数组结构(Structure of Arrays)。

关于更多的游戏引擎和多线程的资源,可以参见这里。

Posted in: Game Programming / Tagged: architecture, data decomposition, function decomposition, game engine, multithread, 函数并行, 多线程, 数据并行, 架构, 游戏引擎

Game Engine Framework

February 4, 2010 1:44 pm / 2 Comments / Benny Chen
FoC of Game

在这次找工作的过程中,有一道笔试题让我印象深刻,题目只有简单的一句话:please use pseudo code to write a game engine framework(请用伪代码写出一个游戏引擎的框架)

当时看到这道题,完全是一种头皮发麻的感觉,虽然我对游戏引擎还算比较熟悉,但它可是个“巨象”般的庞大结构,要在短短的90分钟的笔试时间内把它“摸”完(而且90分钟也不只这一道题),那可真是天方夜谭。所以我当时在试卷上留下的结果就是,草草的画了几个模块图了事。

之后我才意识到,我根本就完全没有理解这道题,或者说,是完全没有理解一个词的意义——framework。受游戏引擎庞大印象的牵连,我把framework想得太大了。

Framework对于IT人士来说貌似是个挺时髦的词,这个词也经常被我们挂在嘴边,我也如此。但问题是,我几乎从来没有去好好的留心过或者深究过,到底什么才是framework,什么样一个东西才能被称为framework,它的准确定义又是什么呢。

看一般英汉字典里对于framework的解释:

structure giving shape and support  框架; 结构

这只是给了我们framework的中文翻译而已,几乎还是没有给我们什么有用的信息。

记住一句话:有困难,找Wiki。当我看到Wiki上对software framework精确的定义时,突然间,一切都明白了。

原文URL:http://en.wikipedia.org/wiki/Software_framework

A software framework, in computer programming, is an abstraction in which common code providing generic functionality can be selectively overridden or specialized by user code providing specific functionality. Frameworks are a special case of software libraries in that they are reusable abstractions of code wrapped in a well-defined API, yet they contain some key distinguishing features that separate them from normal libraries.

Software frameworks have these distinguishing features that separate them from libraries or normal user applications:

1. inversion of control – In a framework, unlike in libraries or normal user applications, the overall program’s flow of control is not dictated by the caller, but by the framework.
2. default behavior – A framework has a default behavior. This default behavior must actually be some useful behavior and not a series of no-ops.
3. extensibility – A framework can be extended by the user usually by selective overriding or specialized by user code providing specific functionality
4. non-modifiable framework code – The framework code, in general, is not allowed to be modified. Users can extend the framework, but not modify its code.

这段话的大概意思是这样,首先表明了software framework是一种抽象体,这个抽象体通过一些公共代码提供了通用的系统功能,使用framework的用户可以在此基础上进行编码,以覆盖或者特化该系统中的相关功能。接着,这段话还说明framework也是一种库,但跟传统的软件库又不同。framework的特点被总结为以下4点:

  1. framework完全决定了程序的控制流,该控制流绝对不会被调用framework的用户(callee)改变,这就是framework跟传统的软件库不同的地方;
  2. framework提供默认行为,且这些默认行为绝对不是无意义的行为;
  3. 用户可以扩展framework的行为;
  4. framework本身的代码不允许被用户改变。

看到这里,我几乎是只有抱头痛哭的份了。因为,从开始学最基础的DirectX技术起,那每一个Demo中,不管多小的Demo,几乎就包含了这样的一种框架,抑或是说,一种游戏引擎框架。只要理解了framework的意思,这道题就根本不是什么问题。我是完全被“游戏引擎”这4个字吓住了,这道题的目的根本不是要你真正的写一个正规的游戏引擎(那是扯蛋),只要弄出一个基本架子即可,达到“麻雀虽小五脏俱全”的效果。

几乎所有的游戏或者说所有的图形渲染的程序,都是遵循下面这样一个过程,一个再熟悉不过的控制流。

FoC of Game

游戏的整个控制流就可以到这么简单,除了图上所描述的东西之外,再加上一系列的消息处理,就可以组成一个基本的游戏框架了。按照wiki上所描述的基本软件framework的4大特点,再来看看游戏引擎framework是如何体现的。

  1. 很显然,游戏的控制流如上图所示,使用framework的用户无法更改;
  2. 游戏引擎framework当然提供默认的行为,比如Initialize部分封装默认的窗口初始化和一系列的设备初始化等过程;
  3. Update和Render,是framework使用者真正进行艺术创造的地方,是游戏开发者的画板。
  4. 游戏引擎framework的代码同样不允许用户修改。

用代码来表现framework是很简单的,在面向对象的语言中一般用抽象类,既提供一些默认的实现,又提供抽象接口供用户扩展。而用户要使用这个framework,只需自定义一个类继承该抽象类,并提供各个抽象方法的实现。用户所需做的只是做一系列的填空题,当然填的如何精彩这就完全取决于用户了。

写这篇文章的真正目的不是为了说明一个游戏引擎的framework是怎么样的,只是借此来强化framework的概念,这样当我们下次再说出这个词的时候,能够知其所以然,而不是自欺欺人。

如果对游戏引擎framework感兴趣,这里有一个简单的游戏引擎framework的代码可供参考: http://gpwiki.org/index.php/SDL:Tutorials:Simple_Engine_Framework

Posted in: Game Programming, Some Experiences / Tagged: framework, game engine, 框架, 游戏引擎

LinkedIn

Milan Petrovic

Categories

  • In My Life (25)
    • A Day in the Life (8)
    • English Learning (2)
    • Learn a Word (7)
    • Something In The Way (8)
  • Music Heaven (8)
    • Guitar (1)
    • In Concert (1)
    • Lyrics (3)
  • OK Computer (54)
    • 3D (3)
    • C++ (10)
    • Computer Graphics (15)
    • Game Programming (23)
    • iOS (6)
    • Linux (1)
    • Lua (9)
    • My Projects (3)
    • Some Experiences (9)
    • Talking in Code (2)
    • Unity (2)
  • Quotations (2)
  • Uncategorized (1)
  • Visca Barça (24)
    • FCB BJ (5)

Recent Posts

  • [译]优化你的手机游戏(没有延迟的,才是健康的)- 一篇给游戏美术设计师读的文章
  • 新浪微博API for MOAI
  • 稍后继续
  • Unity Developer ++
  • Another Thread @ Moai Forum
  • 1st Day of Golden Week
  • 为SyntaxHighlighter添加新语言
  • 基于Lua的State Pattern
  • Class Diagram of Pacman
  • 基于Moai的Pacman

Recent Comments

  • 约修亚_RK on 为SyntaxHighlighter添加新语言
  • 爱装的小男孩 on 小心DLL链接静态库时的内存错误
  • happyfire on Game Loop的几种实现方式
  • William on 新浪微博API for MOAI
  • Benny Chen on 新浪微博API for MOAI
  • your man on 新浪微博API for MOAI
  • 你家男人 on 稍后继续
  • 逍遥 on 关于对std::vector的遍历
  • papa on Unity Developer ++
  • T客网 ︱ Techpot » Blog Archive » iOS开发与OpenGL ES相关问题整理(1) on iOS开发与OpenGL ES相关问题整理(1)

Tags

2d 3D 3dsmax 3ds max air Apply architecture Asia tour barca Beijing bilbao binary search blocked bob boost bruce springsteen C++ capo CGContextDrawImage Champions League Change DLL DX10 eval exporter flash framework frustum culling game game engine iniesta ios linux lua Moai opengles pacman plug-in plugin 北京 导出插件 崩溃 巴萨 游戏引擎 踢球
© Copyright 2026 - A Game Developer
Infinity Theme by DesignCoral / WordPress