Debian Codegraph @

Contrib/CL_FlexibleMap/FMPExample/FMPExample.cpp

raw
1 /*
2 ** FMP_Example
3 ** Copyright (c) 2005 Erik Ylvisaker
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Erik Ylvisaker
27 ** (if your name is missing here, please add it)
28 */
29
30
31 #include "FMPExample.h"
32
33 #include <string>
34 #include <vector>
35
36 using std::string;
37 using std::vector;
38
39 FMP_Example app;
40
41 FMP_Example::FMP_Example(void)
42 {
43 }
44
45 FMP_Example::~FMP_Example(void)
46 {
47 }
48
49
50 bool done = false;
51 int x = 0, y = 0;
52 bool keyleft = false, keyright = false, keyup = false, keydown = false;
53
54 CL_FlexibleMap map;
55 vector<string> files;
56 int fileindex = 0;
57 CL_DisplayWindow *window_ptr = NULL;
58
59 bool display[4];
60
61 int FMP_Example::main(int argc, char **argv)
62 {
63
64 CL_SetupCore setup_core;
65 CL_SetupDisplay setupdisp;
66 CL_SetupGL setupgl;
67
68 CL_DisplayWindow window("CL_FlexibleMap Example", 700, 512);
69
70 window_ptr = &window;
71
72 // Connect the Window close event
73 CL_Slot slot_quit = window.sig_window_close().connect(this, &FMP_Example::on_window_close);
74
75 // Connect keyboard handlers
76 CL_Slot slot_keyboard_up = CL_Keyboard::sig_key_up().connect(this, &FMP_Example::on_key_up);
77 CL_Slot slot_keyboard_down = CL_Keyboard::sig_key_down().connect(this, &FMP_Example::on_key_down);
78
79 // load up the names of all the map files
80 CL_DirectoryScanner sc;
81
82
83 if (sc.scan(".", "*.fmp"))
84 {
85 while (sc.next())
86 {
87 files.push_back(sc.get_name());
88 }
89
90 }
91
92 if (files.size() == 0)
93 {
94 // error, we need to have FMP files in the same directory for this example.
95 return 0;
96
97 }
98
99 // load the first one. Make load maps only after initializing the display.
100 map.load(files[fileindex]);
101
102 // setup frame-rate-independent animations. For each animating tile, mappy has a parameter
103 // called delay. The parameter to set_frame_independent_animation() tells CL_FlexibleMap to wait
104 // that many milliseconds times the delay parameter before cycling to the next frame.
105 map.set_frame_independent_animation(10);
106
107 window.set_title("CL_FlexibleMap Example - " + files[fileindex]);
108
109 // turn on all the tile-layers
110 for (int i = 0; i < 4; i++)
111 display[i] = true;
112
113 // how many pixels to move each frame if the arrow keys are held down
114 const int stepsize = 3;
115
116 // for demonstrating animate-once tiles
117 unsigned int last_time = CL_System::get_time();
118
119 while (!done)
120 {
121 // Draw a nice blue gradient in the background, so we can see where the transparency works
122 CL_Display::fill_rect(
123 CL_Rect(0, 0, 700, 512),
124 CL_Gradient(CL_Color::navy, CL_Color::navy, CL_Color::black, CL_Color::black));
125
126 // allow for scrolling around the map
127 if (CL_Keyboard::get_keycode(CL_KEY_DOWN))
128 y += 3;
129 if (CL_Keyboard::get_keycode(CL_KEY_LEFT))
130 x -= 3;
131 if (CL_Keyboard::get_keycode(CL_KEY_RIGHT))
132 x += 3;
133 if (CL_Keyboard::get_keycode(CL_KEY_UP))
134 y -= 3;
135
136 // call this everytime you want to update your animating tiles. If you are using
137 // frame-rate-independent animations, just call this every cycle, and the rest will
138 // be taken care of.
139 map.update_anim();
140
141 // this is to demonstrate animate once tiles. It will tell them to animate every
142 // 2 seconds (2000 milliseconds)
143 if (CL_System::get_time() - last_time > 2000)
144 {
145 last_time = CL_System::get_time();
146
147 map.start_animate_once();
148 }
149
150 // modify settings for the base layer
151 map.set_current_layer(0);
152
153 // You can set a smaller display area, and the map will be drawn only within that area
154 // for a nice effect, at the end call CL_Display::draw_rect and pass the same rectangle
155 // to give the map a border.
156 //map.set_display_area(CL_Rect(100, 60, 420, 260));
157
158 // This tells the map how to interpret the draw_point chosen. origin_center
159 // will make it so that when set_draw_point is called with point x,y, that point
160 // will be the center of the display area.
161 // (this does not need to be called every frame)
162 map.set_anchor_point(origin_center);
163
164
165 // tell the layer what point should be the top-left of the screen
166 map.get_current_layer().set_draw_point(x, y);
167
168 // set how the map should display regions outside the map. Choices are
169 // border_none, border_wrap, border_default_tile
170 // (this does not need to be called every frame)
171 map.get_current_layer().set_border_style(CL_FlexibleMap::border_none);
172
173 // if you set the border style to border_default_tile, this line of code will
174 // set the default tile to whatever appears in the top-left cornder of the map.
175 // These can be negative, indicating an animating tile should be used (kinda annoying, but neat that it works.)
176 // (this does not need to be called every frame)
177 map.get_current_layer().set_default_tile(map.get_current_layer().get_map_data(0, 0));
178
179 // each tile structure has a background and three foreground tiles
180 // drawing them is separate, so that you can draw sprites in between each layer
181 if (display[0]) map.draw_background();
182 if (display[1]) map.draw_foreground();
183 if (display[2]) map.draw_foreground2();
184 if (display[3]) map.draw_foreground3();
185
186 // check to see if there's another layer
187 if (map.get_layer_count() > 1)
188 {
189 // ok, draw the second layer
190 map.set_current_layer(1);
191
192 // for parallax, you can to set_draw_point(x * 2, y * 2) for example.
193 map.get_current_layer().set_draw_point(x, y);
194
195
196 //map.get_current_layer().set_border_style(CL_FlexibleMap::border_wrap);
197
198 // set layer 1 to 50% transparency
199 // (this does not need to be called every frame)
200 map.get_current_layer().set_alpha(128);
201
202 map.draw_background();
203 }
204
205
206 // Flip the display, showing on the screen what we have drawed
207 // since last call to flip_display()
208 CL_Display::flip();
209
210 // This call updates input and performs other "housekeeping"
211 // call this each frame
212 CL_System::keep_alive(1);
213
214
215 }
216
217 // it's best to destroy your CL_FlexibleMap objects before Clanlib frees its memory.
218 map.clear();
219
220 return 0;
221 }
222
223 void FMP_Example::on_window_close()
224 {
225 done = true;
226 }
227
228 void FMP_Example::on_key_down(const CL_InputEvent &e)
229 {
230 // key down events for numeric keypad + and - to cycle through maps
231 // keys 1-4 will toggle tile layers
232
233 if (CL_Keyboard::get_keycode(CL_KEY_ADD))
234 {
235 fileindex++;
236
237 if (fileindex >= (signed) files.size())
238 fileindex = 0;
239
240 map.load(files[fileindex]);
241 map.set_frame_independent_animation(10);
242
243 window_ptr->set_title("CL_FlexibleMap Example - " + files[fileindex]);
244 }
245 else if (CL_Keyboard::get_keycode(CL_KEY_SUBTRACT))
246 {
247 fileindex--;
248
249 if (fileindex < 0)
250 fileindex = (signed)files.size() - 1;
251
252 map.load(files[fileindex]);
253 map.set_frame_independent_animation(10);
254
255 window_ptr->set_title("CL_FlexibleMap Example - " + files[fileindex]);
256
257 }
258 else if (CL_Keyboard::get_keycode(CL_KEY_1)) display[0] = !display[0];
259 else if (CL_Keyboard::get_keycode(CL_KEY_2)) display[1] = !display[1];
260 else if (CL_Keyboard::get_keycode(CL_KEY_3)) display[2] = !display[2];
261 else if (CL_Keyboard::get_keycode(CL_KEY_4)) display[3] = !display[3];
262
263 }
264
265 void FMP_Example::on_key_up(const CL_InputEvent &e)
266 {
267
268 }