zoukankan      html  css  js  c++  java
  • RenderTree渲染树

    RenderTree渲染树对类中的静态成员有很重要的关系,这个和多态是有很重要的关系,举个简单的例子,在游戏中,马里奥需要渲染,蘑菇也需要渲染,怪兽也需要渲染,其是串在一个树上的,但是不同的类型怎么将其挂在一起,在渲染的过程中,马里奥,蘑菇和怪兽都是拥有不同的渲染接口,但是这样渲染器会做的很复杂,并不是我们想要的,简单的方式就是创建一个抽象基类,让马里奥,蘑菇和怪兽都继承同一个抽象基类,但是我们面临的问题是,对于马里奥,蘑菇和怪兽来说,他们的类型都是不一样的,怎么才能够穿在一个链表上

    如上所示,提供一个纯虚函数show,使得下面的函数可以继承,每个子类中的show都是不一样的

    为了将这些东西连起来,需要有个静态的头,这个是没有问题的

    要串成一个链表,我们要求所有的节点都是类型是一样的,所以需要在每个节点加上next,因此这个链表本质上是父类指针next连在一起的

      1 #include <iostream>
      2 using namespace std;
      3 
      4 #include <thread>
      5 #include <unistd.h>
      6 //父类需要提供一个接口
      7 class RenderShape
      8 {
      9 public:
     10     virtual void show()=0;
     11     bool init(int x,int y)
     12     {
     13         _x=x;
     14         _y=y;
     15         return true;
     16     }
     17 
     18     static void RenderShapeList()
     19     {
     20         RenderShape *t=head;
     21         while(t)
     22         {
     23             t->show();//所有的地方可以直接实现覆写
     24             t=t->next;
     25         }
     26     }
     27 
     28 protected:
     29     int _x;
     30     int _y;
     31     static RenderShape *head;//这个是需要共享的
     32            RenderShape *next;
     33 };
     34 
     35 RenderShape *RenderShape::head=nullptr;
     36 
     37 class Rect:public RenderShape
     38 {
     39 public:
     40     Rect *create(int x,int y,int w,int l)
     41     {
     42         Rect *pRet=new Rect;
     43         if(pRet&&pRet->init(x,y,w,l))
     44         {
     45             pRet->autoRelease();
     46         }
     47         else
     48         {
     49             delete pRet;
     50             pRet=nullptr;
     51         }
     52     }
     53 
     54     bool init(int x, int y,int w,int l)
     55     {
     56         RenderShape::init(x,y);
     57         _w=w;
     58         _l=l;
     59         return true;
     60     }
     61 
     62     void autoRelease()
     63     {
     64         this->next=head;
     65         head=this;
     66     }
     67 
     68     virtual void show()
     69     {
     70         cout<<"draw rect from"<<"("<<_x<<","<<")"<<_y
     71            <<"width"<<_w<<"length"<<_l<<endl;
     72     }
     73 protected:
     74     int _w;
     75     int _l;
     76 };
     77 
     78 class Circle:public RenderShape
     79 {
     80 public:
     81     Circle *create(int x,int y,int r)
     82     {
     83         Circle *pRet=new Circle;
     84         if(pRet&&pRet->init(x,y,r))
     85         {
     86             pRet->autoRelease();
     87         }
     88         else
     89         {
     90             delete pRet;
     91             pRet=nullptr;
     92         }
     93     }
     94 
     95     bool init(int x, int y, int r)
     96     {
     97         RenderShape::init(x,y);
     98         _r=r;
     99         return true;
    100     }
    101 
    102     void autoRelease()
    103     {
    104         this->next=head;
    105         head=this;
    106     }
    107 
    108     virtual void show()
    109     {
    110         cout<<"draw circle from"<<"("<<_x<<","<<")"<<_y
    111            <<"radius"<<_r<<endl;
    112     }
    113 
    114 protected:
    115     int _r;
    116 };
    117 
    118 class Ellipse:public RenderShape
    119 {
    120 protected:
    121     int _l;
    122     int _s;
    123 };
    124 
    125 void threadTask()
    126 {
    127     while(1)
    128     {
    129         cout<<"++++++++++++++++++++++++++"<<endl;
    130         RenderShape::RenderShapeList();
    131         sleep(2);
    132         cout<<"--------------------------"<<endl;
    133     }
    134 }
    135 int main()
    136 {
    137     Rect *pr;
    138     Circle *pc;
    139 
    140     thread t(threadTask);
    141     while(1)
    142     {
    143         int choice;
    144         cin>>choice;
    145         switch(choice)
    146         {
    147             case 1:
    148                 pr=Rect::create(1,2,3,4);
    149                 break;
    150             case 2:
    151                 pc=Circle::create(4,5,6);
    152                 break;
    153         }
    154     }
    155     t.join();
    156     return 0;
    157 }
  • 相关阅读:
    Windows快捷键
    visual studio code颜色主题切换
    visual studio code中文语言包安装
    顶点缓存与索引缓存
    程序结构(2)
    ansible常用模块
    ansible常用模块
    ubuntu实用技巧
    ubuntu实用技巧
    Sqoop导出MySQL数据
  • 原文地址:https://www.cnblogs.com/Cucucudeblog/p/10224059.html
Copyright © 2011-2022 走看看