Cpper
C/C++高级工程师 Android高级软件工程师 IT集成工程师 音频工程师 熟悉c,c++,java,c#,py,js,asp等多种语言 程序猿
今天突然想给引擎加入一个新的功能
那就是使用多线程载入资源
如果游戏资源过多,而只采用一个线程载入资源显然是不够的

 

所以就加入这个功能吧
设计代码有:
1.资源基类

 1 ////////////////////////////////////////////////////////////
 2 /// 定义资源基类
 3 //////////////////////////////////////////////////////////// 
 4 class  Resource : public Object
 5 {
 6 public:
 7     virtual ~Resource(){}
 8     virtual bool Load(const engine_string& file) = 0
 9     
10     DECLARE_OBJECT(Resource)    
11 };

这是引擎所有资源的基类

2.具体的资源
例如图形:

  1 //////////////////////////////////////////////////////// 
  2 /// 定义引擎图形基类
  3 ////////////////////////////////////////////////////////
  4 class Image : public Resource
  5 {
  6 public:
  7     //////////////////////////////////////////////////////// 
  8     /// 图形构造函数
  9     ////////////////////////////////////////////////////////
 10     Image(){}
 11     
 12     //////////////////////////////////////////////////////// 
 13     /// 图形析构函数
 14     ////////////////////////////////////////////////////////
 15     virtual ~Image(){} 
 16 
 17     //////////////////////////////////////////////////////// 
 18     /// 获取图形文件类型
 19     ////////////////////////////////////////////////////////    
 20     virtual const ImageType& GetFileType() const = 0
 21      
 22     //////////////////////////////////////////////////////// 
 23     /// 获取图形数据指针
 24     ////////////////////////////////////////////////////////    
 25     virtual  uint8* GetData()const = 0;  
 26  
 27     //////////////////////////////////////////////////////// 
 28     /// 获取图形宽高
 29     ////////////////////////////////////////////////////////
 30     virtual const int GetWidth() const = 0;
 31     virtual const int GetHeight() const = 0;
 32     
 33     //////////////////////////////////////////////////////// 
 34     /// 获取图形BPP
 35     ////////////////////////////////////////////////////////
 36     virtual const int GetBPP() const = 0
 37     
 38     //////////////////////////////////////////////////////// 
 39     /// 检测图形是否具有alpha通道
 40     ////////////////////////////////////////////////////////    
 41     virtual const bool HasAlpha() const = 0;
 42     
 43     //////////////////////////////////////////////////////// 
 44     /// 获取图形状态
 45     ////////////////////////////////////////////////////////     
 46     virtual const bool IsValid() = 0
 47     
 48     //////////////////////////////////////////////////////// 
 49     /// 获取指定像素颜色值
 50     ////////////////////////////////////////////////////////
 51     virtual const Color GetPixel(int x, int y) const = 0;
 52 
 53     //////////////////////////////////////////////////////// 
 54     /// 设置指定像素颜色值
 55     ////////////////////////////////////////////////////////
 56     virtual bool SetPixel(int x, int y, const Color &color) = 0;
 57  
 58     //////////////////////////////////////////////////////// 
 59     /// 获取红色掩码
 60     ////////////////////////////////////////////////////////
 61     virtual int GetRedMask() const = 0;
 62 
 63     //////////////////////////////////////////////////////// 
 64     /// 获取绿色掩码
 65     ////////////////////////////////////////////////////////
 66     virtual int GetGreenMask() const = 0;
 67 
 68     //////////////////////////////////////////////////////// 
 69     /// 获取蓝色掩码
 70     ////////////////////////////////////////////////////////
 71     virtual int GetBlueMask() const = 0;
 72  
 73     //////////////////////////////////////////////////////// 
 74     /// 获取扫描宽度
 75     ////////////////////////////////////////////////////////
 76     virtual int GetPitch() const =0;
 77     
 78     //////////////////////////////////////////////////////// 
 79     /// 获取图形背景色
 80     //////////////////////////////////////////////////////// 
 81     virtual const Color GetKeyColor() const = 0
 82     
 83     //////////////////////////////////////////////////////// 
 84     /// 设置图形背景色
 85     ////////////////////////////////////////////////////////     
 86     virtual bool SetKeyColor(const Color& color) = 0;
 87     
 88     //////////////////////////////////////////////////////// 
 89     /// 图形拉伸
 90     //////////////////////////////////////////////////////// 
 91     virtual bool RescaleImage(int w, int h) = 0;
 92     
 93     //////////////////////////////////////////////////////// 
 94     /// 图形切割(获取指定区域图形)
 95     //////////////////////////////////////////////////////// 
 96     virtual bool SplitImage(int x, int y, int w, int h) = 0;
 97      
 98     //////////////////////////////////////////////////////// 
 99     /// 图形反色
100     //////////////////////////////////////////////////////// 
101     virtual bool GetInvertImage() = 0;   
102     
103     //////////////////////////////////////////////////////// 
104     /// 图形翻转(做水平或者竖直翻转)
105     //////////////////////////////////////////////////////// 
106     virtual bool FlipImage(ImageFlip type) = 0;
107     
108     //////////////////////////////////////////////////////// 
109     /// 图形旋转
110     //////////////////////////////////////////////////////// 
111     virtual bool RotatedImage(ImageRotation type)= 0 ;
112     
113     //////////////////////////////////////////////////////// 
114     /// 使用指定颜色填充图形
115     ////////////////////////////////////////////////////////
116     virtual bool FillImage(const Color &color) =0;
117     
118     //////////////////////////////////////////////////////// 
119     /// 图形载入
120     ////////////////////////////////////////////////////////
121     virtual bool Load(const engine_string& file) = 0;
122  
123     //////////////////////////////////////////////////////// 
124     /// 图形保存(可保存格式bmp,gif,png,jpg) 
125     ////////////////////////////////////////////////////////
126     virtual bool Save(const engine_string& file) = 0;
127     
128     //////////////////////////////////////////////////////// 
129     /// 调整图形gamma值
130     ////////////////////////////////////////////////////////
131     virtual bool AdjustGamma(float gamma) = 0
132   
133     //////////////////////////////////////////////////////// 
134     /// 调整图像亮度
135     ////////////////////////////////////////////////////////
136     virtual bool AdjustBrightness(float percent) = 0
137  
138     //////////////////////////////////////////////////////// 
139     /// 调整图像对比度
140     ////////////////////////////////////////////////////////
141     virtual bool AdjustContrast(float percent) = 0;
142     
143     //////////////////////////////////////////////////////// 
144     /// 调整图像深度
145     ////////////////////////////////////////////////////////    
146     virtual bool ConvertTo4Bits() = 0;
147     virtual bool ConvertTo8Bits() = 0;
148     virtual bool ConvertTo16Bits555() = 0;
149     virtual bool ConvertTo16Bits565() = 0;
150     virtual bool ConvertTo24Bits() = 0;
151     virtual bool ConvertTo32Bits() = 0;
152     
153     //////////////////////////////////////////////////////// 
154     /// 图形复制函数
155     ////////////////////////////////////////////////////////    
156     virtual RefPtr<Image> CopyImage() = 0
157     
158     DECLARE_OBJECT(Image)    
159 };


3.线程资源载入类

 1 ////////////////////////////////////////////////////////////
 2 /// 定义资源线程载入器O(∩_∩)O~
 3 ////////////////////////////////////////////////////////////'
 4 class ResThreadLoader : public Object
 5 {   
 6 public:
 7     ////////////////////////////////////////////////////////
 8     /// 构造,析构资源线程载入器 
 9     ////////////////////////////////////////////////////////'
10     ResThreadLoader(){}
11     virtual ~ResThreadLoader(){}
12  
13     ////////////////////////////////////////////////////////
14     /// 资源的绑定(绑定成功则返回真)
15     ////////////////////////////////////////////////////////' 
16     virtual bool AttachResource(const engine_string& name,Resource* resource) = 0
17 
18     ////////////////////////////////////////////////////////
19     /// 调用线程载入资源(一个资源对应一个线程)
20     ////////////////////////////////////////////////////////'     
21     virtual void Excute() = 0
22     
23     DECLARE_OBJECT(ResThreadLoader)     
24 };


 

在使用的时候首先从资源管理器中获取全局线程资源载入器
然后绑定要加载的资源
其后调用Excute(函数名写错了!)
其后资源就被载入

下面是具体的例子:

 1     float time = device->GetTime(); 
 2     
 3     //! 单线程载入
 4     //for(int i = 0; i < 12 ; i ++)
 5     //   image[i] = resource_manager->GetImageManager()->CreateObject(logoindex[i]);"..\\image/logo.jpg");
 6      
 7     //! 多线程载入 
 8     for(int i = 0; i < 12 ; i ++)
 9        image[i] = resource_manager->GetImageManager()->CreateObject(logoindex[i]);
10     RefPtr<ResThreadLoader> loader = device->GetResourceManager()->GetResLoader();
11     for(int i = 0; i < 12; i++)
12        loader->AttachResource("..\\image/logo.jpg",image[i].get());  
13      
14     //! 多线程载入 
15     loader->Excute(); 
16     
17     std::cout<<device->GetTime()- time<<std::endl; 
18      
19     //! 反色    
20     image[0]->GetInvertImage();
21     //! 水平翻转 
22     image[1]->FlipImage(core::ImageFlip_Horizontal); 
23     //! 竖直翻转 
24     image[2]->FlipImage(core::ImageFlip_Vertical);   
25     //! Gamma调整
26     image[3]->AdjustGamma(0.4); 
27     //! 亮度调整
28     image[4]->AdjustBrightness(0.4); 
29     //! 对比度调整
30     image[5]->AdjustContrast(0.6); 
31     //! 水平翻转 
32     image[6]->FlipImage(core::ImageFlip_Horizontal); 
33     //! 竖直翻转 
34     image[7]->FlipImage(core::ImageFlip_Vertical);    
35     //! Gamma调整
36     image[8]->AdjustGamma(0.1); 
37     //! 亮度调整
38     image[9]->AdjustBrightness(0.8); 
39     //! 对比度调整
40     image[10]->AdjustContrast(0.3);  
41     
42     RefPtr<TextureManager> texturemanager = resource_manager->GetTextureManager();
43         
44     for(int i = 0; i < 12; i++)
45     {       
46         texture[i] = texturemanager->CreateObject(logoindex[i].c_str(),image[i]);
47         texture[i]->Generate();
48     }        

具体线程是采用的ZThread库
其实一想起来资源多线程加载我首先想到的就是ZThread中的ThreadExcutor
当然具体使用的是PoolExecutor.
posted on 2010-04-06 21:35 ccsdu2009 阅读(1517) 评论(5)  编辑 收藏 引用 所属分类: Game引擎
Comments
  • # re: 盖莫游戏引擎2.1.1-多线程资源载入类
    expter
    Posted @ 2010-04-06 22:29
    可以用来资源打包么?  回复  更多评论   
  • # re: 盖莫游戏引擎2.1.1-多线程资源载入类
    ccsdu2009
    Posted @ 2010-04-07 08:43
    @expter
    no
    当前只处理资源"解包"
    这个以后会加入的  回复  更多评论   
  • # re: 盖莫游戏引擎2.1.1-多线程资源载入类
    ccsdu2009
    Posted @ 2010-04-07 18:31
    @expter
    从理论上讲多线程打包似乎不可行
    因为这是个写操作  回复  更多评论   
  • # re: 盖莫游戏引擎2.1.1-多线程资源载入类
    funcman
    Posted @ 2010-11-25 16:15
    “ /// 调用线程载入资源(一个资源对应一个线程)”
    意思是说要载入12个资源,就会开12个线程?
      回复  更多评论   
  • # re: 盖莫游戏引擎2.1.1-多线程资源载入类
    gaimor
    Posted @ 2010-11-29 13:37
    @funcman
    当初设计失误
    可以忽略之  回复  更多评论   

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理