- 分析Game.h文件
/* * ===================================================================================== * * Filename: Game.h * * Description: 定义游戏的基础类,游戏初始化、启动、消息循环、渲染、触摸处理等方法 * * Version: 1.0 * Created: 2013年12月1日 15时17分31秒 * Revision: none * Compiler: gcc * * Author: caoshun (fly_net_cn), caoshun001@gmail.com * Company: * * ===================================================================================== */ #ifndef _GAME_H #define _GAME_H class Game{ public: /** * 游戏状态 */ enum State { UNINITIALIZED,//未初始化 RUNNING,//正在运行 PAUSED //游戏暂停 }; /** * 析构函数,销毁游戏中的场景等对象 */ virtual ~Game(); /** * 获取当前游戏 * * @return The single instance of the game. */ static Game* getInstance(); /** * 获取游戏的累计运行时间(单位为毫秒,不包括暂停时间) * * @return The total game time (in milliseconds). */ static double getGameTime(); /** * 获取游戏状态 * * @return The current game state. */ inline State getState() const; /** * 游戏启动入口 * * @return Zero for normal termination, or non-zero if an error occurred. */ int run(); /** * 暂停当前游戏 */ void pause(); /** * 唤醒当前游戏 */ void resume(); /** * 退出游戏 */ void exit(); /** * 游戏循环 * 游戏平台每一桢都会调用该函数 */ void frame(); /** * 获取音频控制器 * * @return The audio controller for this game. */ inline AudioController* getAudioController() const; /** * 获取动画控制器 * * @return The animation controller for this game. */ inline AnimationController* getAnimationController() const; /** * 获取物理引擎控制器 * * @return The physics controller for this game. */ inline PhysicsController* getPhysicsController() const; /** * 获取AI控制器 * * @return The AI controller for this game. */ inline AIController* getAIController() const; /** * 触摸事件回调 * * @param evt 触摸事件 * @param x 触摸位置x坐标 * @param y 触摸位置y坐标 * @param 触摸ID * * @see Touch::TouchEvent */ virtual void touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex); protected: /** * 构造函数 */ Game(); /** * 游戏初始化回调 */ virtual void initialize() = 0; /** * 游戏销毁回调 */ virtual void finalize() = 0; /** * 游戏逻辑更新回调 * * @param elapsedTime The elapsed game time. */ virtual void update(float elapsedTime) = 0; /** * 游戏渲染回调 * * @param elapsedTime The elapsed game time. */ virtual void render(float elapsedTime) = 0; }; #endif /*_GAME_H*/
- Game.cpp
#include "Game.h" #include "Platform.h" /** * 游戏实例 **/ static Game* __gameInstance = NULL; double Game::_pausedTimeLast = 0.0; double Game::_pausedTimeTotal = 0.0; Game* Game::getInstance() { assert(__gameInstance); return __gameInstance; } double Game::getGameTime() { return Platform::getAbsoluteTime() - _pausedTimeTotal; } int Game::run() { if (_state != UNINITIALIZED) return -1; _width = Platform::getDisplayWidth(); _height = Platform::getDisplayHeight(); // Start up game systems. if (!startup()) { shutdown(); return -2; } return 0; } bool Game::startup() { if (_state != UNINITIALIZED) return false; // 设置视口位置 setViewport(Rectangle(0.0f, 0.0f, (float)_width, (float)_height)); // 初始化渲染状态 RenderState::initialize(); // 初始化桢缓冲 FrameBuffer::initialize(); //创建并初始化动画控制器 _animationController = new AnimationController(); _animationController->initialize();
//创建并初始化音频控制器 _audioController = new AudioController(); _audioController->initialize();
//创建并初始化物理引擎控制器 _physicsController = new PhysicsController(); _physicsController->initialize();
//创建并初始化AI控制器 _aiController = new AIController(); _aiController->initialize(); //设置游戏状态为运行 _state = RUNNING; return true; } void Game::frame() { //第一次调用时初始化游戏 if (!_initialized) { // Perform lazy first time initialization initialize(); _initialized = true; // Fire first game resize event Platform::resizeEventInternal(_width, _height); } static double lastFrameTime = Game::getGameTime(); double frameTime = getGameTime(); //游戏处理 if (_state == Game::RUNNING) { assert(_animationController); assert(_audioController); assert(_physicsController); assert(_aiController); // 获取时间变化量 float elapsedTime = (frameTime - lastFrameTime); lastFrameTime = frameTime; // 更新动画 _animationController->update(elapsedTime); // 更新物理引擎 _physicsController->update(elapsedTime); // 更新AI _aiController->update(elapsedTime); // 回调游戏逻辑处理 update(elapsedTime); // 渲染声音 _audioController->update(elapsedTime); // 渲染图像 render(elapsedTime); } }