gameplay理解

Camera视角:确定显示的视场及视角。

Game:显示的基类。静态单例模式。但是获取方式很奇怪。

Game::getInstance得到的是__gameInstance,但是__gameInstance是在Game()构造函数里=this。因此如果没有Game的实例,this就是NULL。

然而使用时,编写继承自Game的子类,如HomuraGame,并且声明HomuraGame的全局变量,这样在加载该so时子类的实例就会创建,同时Game也就会创建。而后调用时Game::getInstance就能得到Game的指针。

这也意味着同一时间只能存在一个Game的实例进行操作。

 1 Game::Game()
 2     : _initialized(false), _state(UNINITIALIZED), _pausedCount(0),
 3       _frameLastFPS(0), _frameCount(0), _frameRate(0), _width(0), _height(0),
 4       _clearDepth(1.0f), _clearStencil(0), _properties(NULL),
 5       _animationController(NULL), _audioController(NULL),
 6       _physicsController(NULL), _aiController(NULL), _audioListener(NULL),
 7       _timeEvents(NULL), _scriptController(NULL), _scriptTarget(NULL)
 8 {
 9     GP_ASSERT(__gameInstance == NULL);
10 
11     __gameInstance = this;
12     _timeEvents = new std::priority_queue<TimeEvent, std::vector<TimeEvent>, std::less<TimeEvent> >();
13 }
14 
15 Game::~Game()
16 {
17     SAFE_DELETE(_scriptTarget);
18     SAFE_DELETE(_scriptController);
19 
20     // Do not call any virtual functions from the destructor.
21     // Finalization is done from outside this class.
22     SAFE_DELETE(_timeEvents);
23 #ifdef GP_USE_MEM_LEAK_DETECTION
24     Ref::printLeaks();
25     printMemoryLeaks();
26 #endif
27 
28     __gameInstance = NULL;
29 }
30 
31 Game* Game::getInstance()
32 {
33     GP_ASSERT(__gameInstance);
34     return __gameInstance;
35 }
 1     /**
 2      * Constructor.
 3      */
 4     Game();
 5 
 6     /**
 7      * Destructor.
 8      */
 9     virtual ~Game();
10 
11     /**
12      * Gets the single instance of the game.
13      * 
14      * @return The single instance of the game.
15      */
16     static Game* getInstance();
 1 #define DISPATCH_EVENT(dispatch, count, eventName, ...) { \
 2     for (int i = 0; i < count; i++) { \
 3         EXECUTE_FUNCTION(dispatch[i], eventName, __VA_ARGS__); \
 4     } \
 5 }
 6 
 7 HomuraGame game;
 8 
 9 HomuraGame::HomuraGame() : mParticleIndex(3), mGraphicsIndex(1), pFont(NULL) {
10     memset(pParticle, 0, MAX_HOMURA_SIZE * sizeof(Homura*));
11     memset(pGraphics, 0, MAX_HOMURA_SIZE * sizeof(Homura*));
12     memset(pPhysical, 0, MAX_HOMURA_SIZE * sizeof(Homura*));
13 }

而后Game的子类中实现了update和render方法,openGL中每次更新都会调用Game的frame方法,而frame方法会调用update及render方法,从而对动画进行更新。

 1 class HomuraGame : public Game {
 2 public:
 3     HomuraGame();
 4 
 5     /**
 6      * @see Game::initialize
 7      */
 8     void initialize();
 9 
10     /**
11      * @see Game::finalize
12      */
13     void finalize();
14 
15     /**
16      * @see Game::update
17      */
18     void update(float elapsedTime);
19 
20     /**
21      * @see Game::render
22      */
23     void render(float elapsedTime);
24 
25     void resizeEvent(unsigned int width, unsigned int height);
26 
27     void keyEvent(Keyboard::KeyEvent evt, int key);
28 
29     void touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
30 
31     bool mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta);
32 
33     static HomuraGame* getInstance();

 

posted @ 2017-05-26 12:36  鸭子船长  阅读(645)  评论(0编辑  收藏  举报