冰球游戏项目开发日志(1)

  • 决定不再使用OpenGL。

  同样的程序在Windows下完美运行在xenomai下无法渲染,很有可能是虚拟机虚拟显卡不支持的原因,试图寻找解决方案短时间内收效不高,于是决定直接使用QWidget和QPainter进行绘制。

  • 各种杂事耽误了一星期,今晚花了点时间完成了基本的Gui渲染框架,已经可以通过方向键控制机械手上下左右移动,效果如下:

  • 渲染器设计如下:
 1 namespace Hockey
 2 {
 3     class Render
 4     {
 5     public:
 6 
 7         Render();
 8 
 9     public:
10 
11         void render(QPainter* painter,QPaintEvent* event);
12 
13         void Initialize();
14 
15         void AddGameObject(GameObject* obj);
16 
17     private:
18 
19         std::vector<GameObject*> RenderList;
20     };
21 }
  • Gui管理器
 1 namespace Hockey
 2 {
 3     class GuiManager : public QWidget
 4     {
 5         Q_OBJECT
 6 
 7     public:
 8 
 9         GuiManager(Render* render,QWidget* parent);
10 
11     public slots:
12 
13         void Update();
14 
15     protected:
16 
17         void paintEvent(QPaintEvent* event) Q_DECL_OVERRIDE;
18 
19     private:
20 
21         Render* pRender;
22     };
23 }
  • 程序入口,偷了个懒直接把按键控制写在窗口里了,后面会分离出来。
 1 namespace Hockey
 2 {
 3     class GuiMain : public QWidget
 4     {
 5     public:
 6 
 7         GuiMain();
 8 
 9     public slots:
10 
11         void keyPressEvent(QKeyEvent *event);
12 
13     private:
14 
15         Render render;
16     };
17 }
  • 游戏对象接口:
 1 namespace Hockey
 2 {
 3     class GameObject
 4     {
 5 
 6     public:
 7 
 8         virtual void Initialize() = 0;
 9         virtual void Draw(QPainter* painter) = 0;
10         virtual void Update() = 0;
11     };
12 }
  • 下一步将资源数据保存在xml中,与队友写的其他模块进行对接。
  • 附部分函数实现,主要是贴图渲染部分:
 1 class RedRobot : public GameObject
 2     {
 3     public:
 4 
 5         void Initialize();
 6         void Draw(QPainter* painter);
 7         void Update();
 8 
 9         void Move_x(int step);
10         void Move_y(int step);
11 
12         static RedRobot* GetInstance();
13 
14     private:
15 
16         QPen    edge;
17         QBrush  texture;
18         int     radius;
19         int     pos_x;
20         int     pos_y;
21 
22         static RedRobot* pInstance;
23 
24     };
 1 void RedRobot::Initialize()
 2{
 3  QPixmap pixmap(":/picture/red.png");
 4  radius = pixmap.height();
 5  pos_x = 150;
 6  pos_y = 60;
 7  pixmap.scaled(radius,radius,Qt::IgnoreAspectRatio,Qt::SmoothTransformation);
8  texture.setTexture(pixmap);
9  edge.setColor(QColor(255,0,255));
10  edge.setWidth(1);
11}
12
13 void RedRobot::Draw(QPainter *painter)
14 {
15   painter->save();
16   painter->translate(pos_x,pos_y);
17   painter->setBrush(texture);
18   painter->setPen(edge);
19   painter->drawEllipse(0,0,radius,radius);
20   painter->restore();
21 }
1 void GuiManager::paintEvent(QPaintEvent* event)
2     {
3         QPainter painter;
4         painter.begin(this);
5         painter.setRenderHint(QPainter::Antialiasing);
6         pRender->render(&painter,event);
7         painter.end();
8     }
1 void Render::render(QPainter* painter,QPaintEvent* event)
2     {
3         for(GameObject* object : RenderList)
4         {
5             object->Draw(painter);
6         }
7     }
  • QWidget背景的设置,用QOpenGLWidget在linux虚拟机下背景贴图无法显示,但纯色背景可以使用,原因不明:
1 QPixmap background(":/picture/table.png");
2 
3 setFixedSize(300,500);
4 setAutoFillBackground(true);
5 
6 QPalette palette = this->palette();
7 palette.setBrush(QPalette::Background,QBrush(background));
8 this->setPalette(palette);
原文地址:https://www.cnblogs.com/leafwaltz/p/6241904.html