SDL Game Development-第3章-1.实现游戏对象管理器(1)

[复制链接]

该用户从未签到

2380

主题

2433

帖子

9139

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
9139
QQ
跳转到指定楼层
楼主
发表于 2017-12-19 09:12:52 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

想要查看内容赶紧注册登陆吧!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
1.使用继承
2.实现多态性
3.使用抽象基类
4.有效地继承关系设计

一、抽象基类GameObject实现//GameObject.h
  1. #include "LoaderParams.h"   //专门用于填充参数的简单类
  2. //注:没有了load函数,原因是不想为每个新项目创建一个新的load()
  3. //不同的游戏load的值不同
  4. //因此,专门定义一个填充参数的类LoaderParams

  5. //所有游戏中的对象将从这个基类派生
  6. class GameObject
  7. {
  8. public:
  9.     virtual void draw() = 0;    //三个纯虚函数,强制派生类必须声明并实现它们
  10.     virtual void update() = 0;
  11.     virtual void clean() = 0;
  12. protected:
  13.     GameObject(const LoaderParams* pParams) {}    //继承后成为私有
  14.     virtual ~GameObject() {}
  15. };
复制代码

二、参数填充类的实现//LoaderParams.h
  1. //专门用于填充参数的简单类
  2. #include <string>

  3. class LoaderParams
  4. {
  5. public:
  6.      LoaderParams(int x, int y,
  7.         int width, int height,
  8.         std::string textureID)
  9.         : m_x(x), m_y(y),
  10.         m_width(width), m_height(height),
  11.         m_textureID(textureID)
  12.         { }
  13.      int getX() const { return m_x; }
  14.      int getY() const { return m_y; }
  15.      int getWidth() const { return m_width; }
  16.      int getHeight() const { return m_height; }
  17.      std::string getTextureID() const { return m_textureID; }

  18. private:
  19.      int m_x;
  20.      int m_y;
  21.      int m_width;
  22.      int m_height;
  23.      std::string m_textureID;
  24. };
复制代码

三、将Game类实现为单例,方法同TextureManager类1.public中添加
  1.     static Game* Instance()
  2.     {
  3.        if(s_pInstance == 0)
  4.        {
  5.           s_pInstance = new Game();
  6.           return s_pInstance;
  7.        }
  8.        return s_pInstance;
  9.     }
复制代码
2.将构造、析构放到private中,并添加静态实例指针,以及类体外,加上类型定义
  1. private:
  2.     Game() {};
  3.     ~Game() {};
  4.     static Game* s_pInstance;   
  5. ...
  6. };

  7. typedef Game TheGame;
复制代码
3.Game.cpp中初始化全局Game实例指针
  1. #include "Game.h"

  2. Game* Game::s_pInstance = 0;
复制代码
4.为Game.h定义一个返回渲染器指针的内联函数(公有)
  1.     //由于渲染器指针会被外部使用,因此定义一个get方法
  2.     SDL_Renderer* getRenderer() const { return m_pRenderer; }
复制代码
5.在main.cpp中修改如下:
  1. #include <iostream>
  2. #include "Game.h"

  3. //Game* g_game = 0;   //全局实例

  4. //这里演示了基本框架结构
  5. int main(int argc, char* argv[])
  6. {
  7.     std::cout << "game init attempt...\n";
  8.     if(TheGame::Instance()->init("Charpter 3", 100, 100, 640, 480, false))
  9.     {
  10.         std::cout << "game init success!\n";
  11.         while(TheGame::Instance()->running())    //开始主循环
  12.         {
  13.             TheGame::Instance()->handleEvents(); //处理输入
  14.             TheGame::Instance()->update();   //计算时间和碰撞
  15.             TheGame::Instance()->render();   //渲染到屏幕

  16.             SDL_Delay(10);  //SDL函数,延时10毫秒。有它,可以让cpu占用率低很多
  17.         }
  18.     }else
  19.     {
  20.         std::cout << "game init failure - " << SDL_GetError() << "\n";
  21.         return -1;
  22.     }
  23.     std::cout << "game closing...\n";
  24.     TheGame::Instance()->clean();    //清理资源,退出

  25.     return 0;
  26. }
复制代码

运行测试:

分享到:  QQ好友和群QQ好友和群
收藏收藏
回复

使用道具 举报

快速回复高级模式
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表