3D游戏引擎学习三(游戏引擎核心类Game)

本文分析了3D游戏引擎的核心类Game,详细介绍了Game类的构造、游戏状态、生命周期方法、游戏循环、以及如何处理触摸事件。Game类还包括了音频、动画、物理引擎和AI控制器的管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 分析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*/
  2. 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);  
        } 
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值