青瓷引擎 - 游戏主循环

原创 2016年08月31日 15:17:27

游戏主循环

参考:游戏主循环,讲得非常清楚。

青瓷引擎调度器

青瓷引擎启动了个循环定时器来跑游戏主循环:

window.requestAnimationFrame(mainLoop);

游戏主循环的逻辑如下图所示:

Main Loop

这里我们需要先解释下两个概念:

场景(Scene)

1个场景就是一个游戏世界,或者说1个场景=1个关卡。一个场景包含了所有的游戏对象,例如角色、武器、例子系统、声音、敌人等等。在引擎编辑器中,所有的实体对象(或者说node节点)都完整的呈现在Hierarchy面板中:

Scene

更新场景

主循环会递归访问场景所有节点,伪代码如下:

foreach (nodes) {
    node.update();
    foreach (node’s components) {
        component.update();
    }
}

preUpdatepostUpdate的调度,与update类似。下面举个实际例子来加深下理解。

调度例子

  • 创建一个空的工程,名称为:MainLoop;
  • 创建一个JavaScript脚本文件:Test.js,代码如下:
var Test = qc.defineBehaviour('qc.demo.Test', qc.Behaviour, function() {
    console.log('Init');
}, {});

Test.prototype.preUpdate = function() {
    console.log(this.gameObject.name, 'call preUpdate');
};

Test.prototype.update = function() {
    console.log(this.gameObject.name, 'call update');
};

Test.prototype.postUpdate = function() {
    console.log(this.gameObject.name, 'call postUpdate');
};
  • 创建两个空的node节点,重命名为:node1和node2;
  • 将脚本Test.js挂载到node1和node2节点;
  • 运行工程,在浏览器的开发控制台查看输出:
    Call

在场景中有2个节点:node1和node2。游戏主循环将调度这两个节点及其挂载的所有组件:

Call

首先,游戏主循环调度所有nodes的preUpdate函数,然后是update,最后是postUpdate。大部分情况下,我们将游戏逻辑代码置于update中。当用户输入时,onDownonClickonUp等函数将会被主循环调用(后续再详细解释)。

附:Game Main Loop

Video games are simply ordinary software - there’s nothing intrinsically special about them. However, they SEEM to behave in a very different way from your ordinary everyday applications - so much that many experienced programmers are at a complete loss when faced with the task of developing a game from scratch. This difference is mostly caused by game’s close ties to their main loop. In this chapter, we’ll examine what is the main loop, and how it works in QICI.

A simple model

How simply could we model a computer game? Let’s start by separating the game in two parts: the data inside the computer, and everything outside the computer. This might seem like a strange thing to say, but remember that games are, first and foremost, interactive software. So, outside the computer, we have (at the very least) the player. Inside the computer we have all sorts of data, but the most important is what we call the game state: it contains all the dynamic information of the game: the position of the player, monsters; the current time; the score - in essence, everything that changes with time.

A computer game can then be understood as a system in which those two entities - player and game state - interact with each other according to a specific pattern of rules. The player inputs data into the system, and the system users the game state to generate output to display to the player. This is not very different from ordinary applications, in which e.g. a click (input) might cause a dialog to show up (output). The difference is how that behaves in respect with time.

The Game Loop

Most applications are developed in a reactive way - your program just sits around waiting for the user to click something, and then it does something in response. There is a main loop behind your application as well, but it’s not important - it’s abstracted away. With games, your code is constantly being invoked, even in the absence of user input.

The pseudo-code for a video game will often look like this:

function main() {
    init();
    runMainLoop();
    deInit();
}

The main loop looks something like this:

while (running) {
    processInput();
    updateGameState();
    drawScreen();
    waitUntilItsTime();
}

First, it reads input from the user and stores it somewhere. Here, it will check the keyboard, gamepad, plastic guitar - basically, it will collect all information from the outside world.

After that, it will use that information to update the game state - depending on what conditions we have, they will have different results. For example, on the menu screen, detecting that the “down” arrow got pressed might increment the “currently selected menu item” variable, but the same input while on the game might instead trigger the “set player as moving down” flag. Note that the world update is invoked even if the user didn’t perform any input. This is a very important property of most video games.

Lastly, it will collect the current game state data to generate the output - it will figure out where the player and everything else are, and generate an image to display on the screen. A more complicated game will also have to deal with other forms of output - audio and network, for example.

QICI Game Loop

QICI start a loop timer to run game loop:

window.requestAnimationFrame(mainLoop);

The main loop is shown as the following diagram:

Main Loop

There are two concepts must be explained in detail first.

What is a Scene?

A scene is a game world, or 1 scene=1 level. The scene contains all gameObjects, for example, characters, weapons, particle systems, sounds, enemies. In QICI Editor, all entities (or gameObjects, or nodes) in the scene are listed in the Hierarchy views:

Scene

How to Update a Scene?

QICI calls scene’s nodes recursively. The pseudo-code looks like this:

foreach (nodes) {
    node.update();
    foreach (node’s components) {
        component.update();
    }
}

Preupdate and Postupdate is the same to Update a scene. Let’s get to an example to understand it.

Example

  • Create a empty project ‘MainLoop’ first.
  • New a javascript file ‘Test.js’ and type codes:
var Test = qc.defineBehaviour('qc.demo.Test', qc.Behaviour, function() {
    console.log('Init');
}, {});

Test.prototype.preUpdate = function() {
    console.log(this.gameObject.name, 'call preUpdate');
};

Test.prototype.update = function() {
    console.log(this.gameObject.name, 'call update');
};

Test.prototype.postUpdate = function() {
    console.log(this.gameObject.name, 'call postUpdate');
};
  • Create two empty nodes in the scene, and rename them to ‘node1’ and ‘node2’.
  • Link ‘Test.js’ to node1 and node2.
  • Run the project, and see output in the console view:
    Call

What happened inside the engine?

There are 2 nodes in the scenes: node1 and node2. The main loop visits them and calls their components’ function.

Call

The main loop calls all nodes’ preUpdate function first, then update function, and postUpdate function last. Most of the time, we will put our codes in the update function. When the mouse is clicked (or touch), these function will be called by main loop just like update function: onDown, onClick and onUp (Explain later chapter).

版权声明:本文为博主原创文章,未经博主允许不得转载。

开源免费的HTML5游戏引擎——青瓷引擎(QICI Engine) 1.0正式版发布了!

青瓷引擎是一套开源免费的JavaScript游戏引擎类库,其基于开源免费的Phaser游戏引擎,并提供了一套完全基于浏览器的跨平台集成式HTML5游戏编辑器。...
  • QICIEngine
  • QICIEngine
  • 2015年12月15日 13:39
  • 1797

cocos2d-x游戏引擎核心之三——主循环和定时器

cocos2d-x游戏引擎核心之三——主循环和定时器 2014-10-28 11:40 by 小天_y, 107 阅读, 0 评论, 收藏,  编辑 一、游戏主循环 在介绍游戏基本概念的...
  • u012419410
  • u012419410
  • 2016年02月02日 15:04
  • 384

cocos2d-x游戏开发(三)游戏主循环

终于抽时间把这个游戏写完了。由于没有自拍神器,所以把它移植到了Android上,用我的戴妃跑的很欢啊。自此,我算是完成了一个功能比较完善的游戏了。 麻雀虽小,五脏俱全,该有的都有,不该有的估计也...
  • zhanglongit
  • zhanglongit
  • 2013年01月29日 17:09
  • 4425

网络游戏中的主循环

引言: 网络游戏作为一种复杂度较高的软件,从其设计角度还是有一些共性的,比方说几乎所有的网游都会有一个主循环。由于游戏需要根据输入、游戏内状态的改变来不间断地更新游戏画面,所以游戏的主循环往往看起来...
  • u010832643
  • u010832643
  • 2014年11月06日 15:31
  • 1563

【Visual C++】游戏编程学习笔记之三:游戏循环的使用

本系列文章由@二货梦想家张程 所写,转载请注明出处。 本文章链接:http://blog.csdn.net/terence1212/article/details/44163799 作者:Zee...
  • terence1212
  • terence1212
  • 2015年03月11日 23:28
  • 850

4.帧循环(游戏主循环),schedule

 1 概述 游戏乃至图形界面的本质是不断地绘图,然而绘图并不是随意的,任何游戏都需要遵循一定的规则来呈现出来,这些规则就体现为游戏逻辑。游戏逻辑会控制游戏内容,使其根据用户输入和时间...
  • toto1297488504
  • toto1297488504
  • 2014年10月21日 14:38
  • 1206

写一个好的游戏主循环

原文地址:http://www.koonsolo.com/news/dewitters-gameloop/ [转于Oiramario]  游戏主循环是每个游戏的心跳,输送着整个游戏需要的养...
  • tjunxin
  • tjunxin
  • 2013年04月28日 00:31
  • 2071

Mangos源码分析(7):服务器公共组件实现之游戏主循环

当阅读一项工程的源码时,我们大概会选择从main函数开始,而当开始一项新的工程时,第一个写下的函数大多也是main。那我们就先来看看,游戏服务器代码实现中,main函数都做了些什么。  由于我在读技术...
  • u011686361
  • u011686361
  • 2015年09月10日 12:03
  • 918

十大游戏开发引擎优缺点对比

戏引擎作为开发商研发游戏时必备的工具,每一款引擎的运用都关乎自家大作未来的销量,所以游戏商们对引擎的选择也非常小心翼翼。现在,手游、页游等新兴平台的表现又非常强势,所以选择正确的游戏引擎变得尤为重要。...
  • English0523
  • English0523
  • 2017年05月31日 15:01
  • 8814

给立志于游戏引擎研发的新手的一些建议

          经常遇到一些新手,问如果想立志于游戏引擎研发,有一些什么好的建议。另外个人认为游戏程序员即使是在使用商业引擎做项目,也应该至少达到引擎初级水平,这样才能知其然也知其所以...
  • CrazyEngine
  • CrazyEngine
  • 2016年11月18日 02:02
  • 2256
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:青瓷引擎 - 游戏主循环
举报原因:
原因补充:

(最多只允许输入30个字)