今天突然想給引擎加入一個新的功能
那就是使用多線程載入資源
如果游戲資源過多,而只采用一個線程載入資源顯然是不夠的
所以就加入這個功能吧
設(shè)計代碼有:
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 /// 圖形構(gòu)造函數(shù)
9 ////////////////////////////////////////////////////////
10 Image(){}
11
12 ////////////////////////////////////////////////////////
13 /// 圖形析構(gòu)函數(shù)
14 ////////////////////////////////////////////////////////
15 virtual ~Image(){}
16
17 ////////////////////////////////////////////////////////
18 /// 獲取圖形文件類型
19 ////////////////////////////////////////////////////////
20 virtual const ImageType& GetFileType() const = 0;
21
22 ////////////////////////////////////////////////////////
23 /// 獲取圖形數(shù)據(jù)指針
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 /// 獲取圖形狀態(tài)
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 /// 設(shè)置指定像素顏色值
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 /// 設(shè)置圖形背景色
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 /// 圖形切割(獲取指定區(qū)域圖形)
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 /// 圖形翻轉(zhuǎn)(做水平或者豎直翻轉(zhuǎn))
105 ////////////////////////////////////////////////////////
106 virtual bool FlipImage(ImageFlip type) = 0;
107
108 ////////////////////////////////////////////////////////
109 /// 圖形旋轉(zhuǎn)
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 /// 調(diào)整圖形gamma值
130 ////////////////////////////////////////////////////////
131 virtual bool AdjustGamma(float gamma) = 0;
132
133 ////////////////////////////////////////////////////////
134 /// 調(diào)整圖像亮度
135 ////////////////////////////////////////////////////////
136 virtual bool AdjustBrightness(float percent) = 0;
137
138 ////////////////////////////////////////////////////////
139 /// 調(diào)整圖像對比度
140 ////////////////////////////////////////////////////////
141 virtual bool AdjustContrast(float percent) = 0;
142
143 ////////////////////////////////////////////////////////
144 /// 調(diào)整圖像深度
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 /// 圖形復制函數(shù)
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 /// 構(gòu)造,析構(gòu)資源線程載入器
9 ////////////////////////////////////////////////////////'
10 ResThreadLoader(){}
11 virtual ~ResThreadLoader(){}
12
13 ////////////////////////////////////////////////////////
14 /// 資源的綁定(綁定成功則返回真)
15 ////////////////////////////////////////////////////////'
16 virtual bool AttachResource(const engine_string& name,Resource* resource) = 0;
17
18 ////////////////////////////////////////////////////////
19 /// 調(diào)用線程載入資源(一個資源對應一個線程)
20 ////////////////////////////////////////////////////////'
21 virtual void Excute() = 0;
22
23 DECLARE_OBJECT(ResThreadLoader)
24 };
在使用的時候首先從資源管理器中獲取全局線程資源載入器
然后綁定要加載的資源
其后調(diào)用Excute(函數(shù)名寫錯了!)
其后資源就被載入
下面是具體的例子:
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 //! 水平翻轉(zhuǎn)
22 image[1]->FlipImage(core::ImageFlip_Horizontal);
23 //! 豎直翻轉(zhuǎn)
24 image[2]->FlipImage(core::ImageFlip_Vertical);
25 //! Gamma調(diào)整
26 image[3]->AdjustGamma(0.4);
27 //! 亮度調(diào)整
28 image[4]->AdjustBrightness(0.4);
29 //! 對比度調(diào)整
30 image[5]->AdjustContrast(0.6);
31 //! 水平翻轉(zhuǎn)
32 image[6]->FlipImage(core::ImageFlip_Horizontal);
33 //! 豎直翻轉(zhuǎn)
34 image[7]->FlipImage(core::ImageFlip_Vertical);
35 //! Gamma調(diào)整
36 image[8]->AdjustGamma(0.1);
37 //! 亮度調(diào)整
38 image[9]->AdjustBrightness(0.8);
39 //! 對比度調(diào)整
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.