Debian Codegraph @

Tests/SpriteTest/main.cpp

raw
1 #include <ClanLib/application.h>
2 #include <ClanLib/core.h>
3 #include <ClanLib/display.h>
4 #include <ClanLib/gl.h>
5 #include <GL/gl.h>
6 #include <GL/glu.h>
7
8 /////////////////////////////////////////////////////////////////////////////
9 // Application class
10
11 class Application : public CL_ClanApplication
12 {
13 public:
14 int main(int argc, char** argv);
15
16 void sprite_test_manual(CL_DisplayWindow &window);
17 void sprite_test_resources(CL_DisplayWindow &window);
18 } app;
19
20 /////////////////////////////////////////////////////////////////////////////
21 // Application main entry point:
22
23 int Application::main(int argc, char** argv)
24 {
25 CL_ConsoleWindow debug_console("Debug console");
26 debug_console.redirect_stdio();
27 try
28 {
29 CL_SetupCore::init();
30 CL_SetupDisplay::init();
31 CL_SetupGL::init();
32
33 CL_DisplayWindow window("ClanLib Sprite Test", 1024, 768, false, true);
34
35 // sprite_test_manual(window);
36 sprite_test_resources(window);
37
38 CL_SetupGL::deinit();
39 CL_SetupDisplay::deinit();
40 CL_SetupCore::deinit();
41 }
42 catch (CL_Error error)
43 {
44 std::cout << "Uncaught error: " << error.message.c_str() << std::endl;
45 debug_console.wait_for_key();
46 }
47
48 return 0;
49 }
50
51 void Application::sprite_test_manual(CL_DisplayWindow &window)
52 {
53 /* CL_SpriteDescription desc;
54
55 desc.add_frame(new CL_TargaProvider("images/manwalk_0001.tga"), true);
56 desc.add_frame(new CL_TargaProvider("images/manwalk_0002.tga"), true);
57 desc.add_frame(new CL_TargaProvider("images/manwalk_0003.tga"), true);
58 desc.add_frame(new CL_TargaProvider("images/manwalk_0004.tga"), true);
59 desc.add_frame(new CL_TargaProvider("images/manwalk_0005.tga"), true);
60 desc.add_frame(new CL_TargaProvider("images/manwalk_0006.tga"), true);
61 desc.add_frame(new CL_TargaProvider("images/manwalk_0007.tga"), true);
62 desc.add_frame(new CL_TargaProvider("images/manwalk_0008.tga"), true);
63 desc.add_frame(new CL_TargaProvider("images/manwalk_0009.tga"), true);
64 desc.add_frame(new CL_TargaProvider("images/manwalk_0010.tga"), true);
65 desc.add_frame(new CL_TargaProvider("images/manwalk_0011.tga"), true);
66 desc.add_frame(new CL_TargaProvider("images/manwalk_0012.tga"), true);
67 desc.add_frame(new CL_TargaProvider("images/manwalk_0013.tga"), true);
68 desc.add_frame(new CL_TargaProvider("images/manwalk_0014.tga"), true);
69 desc.add_frame(new CL_TargaProvider("images/manwalk_0015.tga"), true);
70 desc.add_frame(new CL_TargaProvider("images/manwalk_0016.tga"), true);
71 desc.add_frame(new CL_TargaProvider("images/manwalk_0017.tga"), true);
72 desc.add_frame(new CL_TargaProvider("images/manwalk_0018.tga"), true);
73 desc.add_frame(new CL_TargaProvider("images/manwalk_0019.tga"), true);
74 desc.add_frame(new CL_TargaProvider("images/manwalk_0020.tga"), true);
75 desc.add_frame(new CL_TargaProvider("images/manwalk_0021.tga"), true);
76 desc.add_frame(new CL_TargaProvider("images/manwalk_0022.tga"), true);
77 desc.add_frame(new CL_TargaProvider("images/manwalk_0023.tga"), true);
78 desc.add_frame(new CL_TargaProvider("images/manwalk_0024.tga"), true);
79 desc.add_frame(new CL_TargaProvider("images/manwalk_0025.tga"), true);
80 desc.add_frame(new CL_TargaProvider("images/manwalk_0026.tga"), true);
81 desc.add_frame(new CL_TargaProvider("images/manwalk_0027.tga"), true);
82 desc.add_frame(new CL_TargaProvider("images/manwalk_0028.tga"), true);
83 desc.add_frame(new CL_TargaProvider("images/manwalk_0029.tga"), true);
84
85 CL_Sprite sprite(desc);
86
87 CL_GraphicContext *gc = window.get_gc();
88
89 float last_time = (float)CL_System::get_time();
90
91 float colors[26][20][3];
92 for(int y=0; y<19; y++)
93 {
94 for(int x=0; x<26; x++)
95 {
96 colors[x][y][0] = (rand() % 100) / 100.0f;
97 colors[x][y][1] = (rand() % 100) / 100.0f;
98 colors[x][y][2] = (rand() % 100) / 100.0f;
99 }
100 }
101
102 while (!CL_Keyboard::get_keycode(CL_KEY_ESCAPE))
103 {
104 // Calculate amount of time since last frame
105 float delta_time = (CL_System::get_time() - last_time) / 1000.0f;
106 last_time = (float)CL_System::get_time();
107
108 // Rotate sprite based on time
109 sprite.rotate(100.0f * delta_time);
110 sprite.set_scale(1.0f, 1.0f);
111 sprite.set_alignment(origin_top_left);
112
113 // Update sprite animation based on time
114 sprite.update(delta_time);
115
116 gc->clear();
117
118 for(int y=0; y<19; y++)
119 {
120 for(int x=0; x<26; x++)
121 {
122 sprite.set_color(colors[x][y][0], colors[x][y][1], colors[x][y][2]);
123 sprite.draw(x * 40, y * 40, gc);
124 }
125 }
126
127 sprite.set_color(1.0f, 1.0f, 1.0f);
128 sprite.set_alignment(origin_center);
129 sprite.set_scale(2.0f, 2.0f);
130 sprite.draw(gc->get_width()/2-100, gc->get_height()/2, gc);
131 sprite.draw(gc->get_width()/2+100, gc->get_height()/2, gc);
132
133 window.flip();
134 CL_System::keep_alive(10);
135 }
136 */}
137
138 void Application::sprite_test_resources(CL_DisplayWindow &window)
139 {
140 CL_ResourceManager resources("resources.xml", false);
141 CL_Font font("font_gray", &resources);
142
143 CL_Sprite spr1("spr1", &resources);
144 CL_Sprite spr2("spr2", &resources);
145 CL_Sprite spr3("spr3", &resources);
146 CL_Sprite spr4("spr4", &resources);
147 CL_Sprite spr5("spr5", &resources);
148 CL_Sprite spr6("spr6", &resources);
149 CL_Sprite spr7("spr7", &resources);
150 CL_Sprite spr8("spr8", &resources);
151 CL_Sprite spr9("spr9", &resources);
152 CL_Sprite spr10("spr10", &resources);
153 CL_Sprite spr10b("spr10", &resources);
154 CL_Sprite spr10c("spr10", &resources);
155 CL_Sprite spr11("spr11", &resources);
156 CL_Sprite spr12("spr12", &resources);
157 CL_Sprite spr13("spr13", &resources);
158 CL_Sprite spr14("spr14", &resources);
159 CL_Sprite spr15("spr15", &resources);
160 CL_Sprite spr16("spr16", &resources);
161 CL_Sprite spr17("spr17", &resources);
162 CL_Sprite spr18("spr18", &resources);
163 CL_Sprite spr19("spr19", &resources);
164 CL_Sprite spr20("spr20", &resources);
165 CL_Sprite spr21("spr21", &resources);
166 CL_Sprite spr22("spr22", &resources);
167 CL_Sprite spr23("spr23", &resources);
168 CL_Sprite spr24("spr24", &resources);
169 CL_Sprite spr25("spr25", &resources);
170 CL_Sprite spr26("spr26", &resources);
171 CL_Sprite spr27("spr27", &resources);
172
173 CL_GraphicContext *gc = window.get_gc();
174
175 float last_time = (float)CL_System::get_time();
176 while (!CL_Keyboard::get_keycode(CL_KEY_ESCAPE))
177 {
178 CL_Size s = spr1.get_frame_size(0);
179
180 // Calculate amount of time since last frame
181 float delta_time = (CL_System::get_time() - last_time) / 1000.0f;
182 last_time = (float)CL_System::get_time();
183
184 gc->clear(CL_Color(255, 0, 0));
185
186 font.draw(CL_Rect(160*0, 0, 160*1-10, 1000), "1: Non-animated, single frame");
187 spr1.draw(10+160*0, 20);
188
189 font.draw(CL_Rect(160*1, 0, 160*2-10, 1000), "2: 180 base-angle");
190 spr2.draw(10+160*1, 20);
191 font.draw(CL_Rect(160*2, 0, 160*3-10, 1000), "3: 180 base-angle, 2x1 scale");
192 spr3.draw(10+160*2, 20);
193 font.draw(CL_Rect(160*3, 0, 160*4-10, 1000), "4: 1x0.5 scale");
194 spr4.draw(10+160*3, 20);
195 font.draw(CL_Rect(160*4, 0, 160*5-10, 1000), "5: 2x2 scale");
196 spr5.draw(10+160*4, 20);
197
198 font.draw(CL_Rect(160*1, 130, 160*2-10, 1000), "6: Colour red");
199 spr6.draw(10+160*1, 150);
200 font.draw(CL_Rect(160*2, 130, 160*3-10, 1000), "7: Colour greenish");
201 spr7.draw(10+160*2, 150);
202 font.draw(CL_Rect(160*3, 130, 160*4-10, 1000), "8: Half-transparent");
203 spr8.draw(10+160*3, 150);
204 font.draw(CL_Rect(160*4, 130, 160*5-10, 1000), "9: Half-transparent, blue");
205 spr9.draw(10+160*4, 150);
206
207 font.draw(CL_Rect(160*0, 130*2, 160*1-10, 1000), "10: Simple animation");
208 spr10.draw(10+160*0, 130*2+20);
209 spr10.update();
210 font.draw(CL_Rect(160*1, 130*2, 160*2-10, 1000), "11: Fast animation");
211 spr11.draw(10+160*1, 130*2+20);
212 spr11.update();
213 font.draw(CL_Rect(160*2, 130*2, 160*3-10, 1000), "12: Ping-pong animation");
214 spr12.draw(10+160*2, 130*2+20);
215 spr12.update();
216 font.draw(CL_Rect(160*3, 130*2, 160*4-10, 1000), "13: Backward animation");
217 spr13.draw(10+160*3, 130*2+20);
218 spr13.update();
219 font.draw(CL_Rect(160*4, 130*2, 160*5-10, 1000), "10b: Rotating animation");
220 spr10b.draw(10+160*4, 130*2+20);
221 spr10b.rotate(1.0f);
222 spr10b.update();
223 font.draw(CL_Rect(160*5, 130*2, 160*6-10, 1000), "10c: Rotating, 2x scaled animation");
224 spr10c.set_scale(2.0f, 2.0f);
225 spr10c.draw(10+160*5, 130*2+20);
226 spr10c.rotate(1.0f);
227 spr10c.update();
228 font.draw(CL_Rect(160*0, 130*3, 160*1-10, 1000), "14: No-loop animation");
229 spr14.draw(10+160*0, 130*3+20);
230 spr14.update();
231 font.draw(CL_Rect(160*1, 130*3, 160*2-10, 1000), "15: No-loop animation, last_frame on_finish");
232 spr15.draw(10+160*1, 130*3+20);
233 spr15.update();
234 font.draw(CL_Rect(160*2, 130*3, 160*3-10, 1000), "16: No-loop animation, first_frame on_finish");
235 spr16.draw(10+160*2, 130*3+20);
236 spr16.update();
237
238 font.draw(CL_Rect(160*3, 130*3, 160*4-10, 1000), "21: Frame-modifications, first 15 frames fast");
239 spr21.draw(10+160*3, 130*3+20);
240 spr21.update();
241 font.draw(CL_Rect(160*4, 130*3, 160*5-10, 1000), "22: Frame-modifications, first 15 frames with position-offset (not supported yet)");
242 spr22.draw(10+160*4, 130*3+20);
243 spr22.update();
244
245 font.draw(CL_Rect(160*0, 130*4, 160*1-10, 1000), "17: Center translation");
246 spr17.draw(10+160*0, 130*4+20);
247 CL_Display::draw_rect(CL_Rect(9+160*0, 130*4+19, 11+160*0, 130*4+21), CL_Color::white);
248 spr17.update();
249 font.draw(CL_Rect(160*1, 130*4, 160*2-10, 1000), "18: Top-left translation");
250 spr18.draw(10+160*1, 130*4+20);
251 CL_Display::draw_rect(CL_Rect(9+160*1, 130*4+19, 11+160*1, 130*4+21), CL_Color::white);
252 spr18.update();
253 font.draw(CL_Rect(160*2, 130*4, 160*3-10, 1000), "19: Bottom-right translation");
254 spr19.draw(10+160*2, 130*4+20);
255 CL_Display::draw_rect(CL_Rect(9+160*2, 130*4+19, 11+160*2, 130*4+21), CL_Color::white);
256 spr19.update();
257 font.draw(CL_Rect(160*3, 130*4, 160*4-10, 1000), "20: Center +10,+10 translation, animated");
258 spr20.draw(10+160*3, 130*4+20);
259 CL_Display::draw_rect(CL_Rect(9+160*3, 130*4+19, 11+160*3, 130*4+21), CL_Color::white);
260 spr20.update();
261
262 font.draw(CL_Rect(160*1, 130*5, 160*2-10, 1000), "23: Top-left rotation");
263 spr23.draw(10+160*1, 130*5+20);
264 CL_Display::draw_rect(CL_Rect(9+160*1, 130*5+19, 11+160*1, 130*5+21), CL_Color::white);
265 spr23.rotate(1.0f);
266
267 font.draw(CL_Rect(160*2, 130*5, 160*3-10, 1000), "24: Bottom-right rotation, animated");
268 spr24.draw(10+160*2, 130*5+20);
269 CL_Display::draw_rect(CL_Rect(9+160*2+s.width, 130*5+19+s.height, 11+160*2+s.width, 130*5+21+s.height), CL_Color::white);
270 spr24.rotate(1.0f);
271 spr24.update();
272
273 font.draw(CL_Rect(160*3, 130*5, 160*4-10, 1000), "25: Top-left +10,+10 rotation, 2x scaled, animated");
274 spr25.draw(10+160*3, 130*5+20);
275 CL_Display::draw_rect(CL_Rect(9+160*3+10, 130*5+19+10, 11+160*3+10, 130*5+21+10), CL_Color::white);
276 spr25.rotate(1.0f);
277 spr25.update();
278
279 font.draw(CL_Rect(160*4, 130*5, 160*5-10, 1000), "25: Animated sprite using description");
280 spr26.draw(10+160*4, 130*5+20);
281 spr26.update();
282 font.draw(CL_Rect(160*5, 130*5, 160*6-10, 1000), "25: Animated sprite using description plus extra frames");
283 spr27.draw(10+160*5, 130*5+20);
284 spr27.update();
285
286 window.flip();
287 CL_System::keep_alive(10);
288 }
289 }