|
[a5] A doubt about move of image with keyboard |
Jefferson Almeida
Member #12,659
March 2011
|
Hi, I changed for "anime[0].Reallocate(4 , 0.1);" and stayed a little better... Thanks a lot! 1...
2bool key[NUMKEYS];
3 for (int i = 0 ; i < NUMKEYS ; ++i) {key[i] = false;}
4
5 Animation anime[4];
6 anime[0].Reallocate(4 , 0.1);
7 anime[1].Reallocate(4 , 0.1);
8 anime[2].Reallocate(4, 0.1);
9 anime[3].Reallocate(4, 0.1);
10 // load bitmaps and use SetBitmap
11 /* Left Images */
12 ALLEGRO_BITMAP* aphonsol0 = al_load_bitmap("Sprites/FFT1/aphonsol0.png");
13 ALLEGRO_BITMAP* aphonsol1 = al_load_bitmap("Sprites/FFT1/aphonsol1.png");
14 ALLEGRO_BITMAP* aphonsol2 = al_load_bitmap("Sprites/FFT1/aphonsol2.png");
15 ALLEGRO_BITMAP* aphonsol3 = al_load_bitmap("Sprites/FFT1/aphonsol3.png");
16 /* Right Images */
17 ALLEGRO_BITMAP* aphonsor0 = al_load_bitmap("Sprites/FFT1/aphonsor0.png");
18 ALLEGRO_BITMAP* aphonsor1 = al_load_bitmap("Sprites/FFT1/aphonsor1.png");
19 ALLEGRO_BITMAP* aphonsor2 = al_load_bitmap("Sprites/FFT1/aphonsor2.png");
20 ALLEGRO_BITMAP* aphonsor3 = al_load_bitmap("Sprites/FFT1/aphonsor3.png");
21 /* Up Images */
22 ALLEGRO_BITMAP* aphonsou0 = al_load_bitmap("Sprites/FFT1/aphonsou0.png");
23 ALLEGRO_BITMAP* aphonsou1 = al_load_bitmap("Sprites/FFT1/aphonsou1.png");
24 ALLEGRO_BITMAP* aphonsou2 = al_load_bitmap("Sprites/FFT1/aphonsou2.png");
25 ALLEGRO_BITMAP* aphonsou3 = al_load_bitmap("Sprites/FFT1/aphonsou3.png");
26 /* Down Images */
27 ALLEGRO_BITMAP* aphonsod0 = al_load_bitmap("Sprites/FFT1/aphonsod0.png");
28 ALLEGRO_BITMAP* aphonsod1 = al_load_bitmap("Sprites/FFT1/aphonsod1.png");
29 ALLEGRO_BITMAP* aphonsod2 = al_load_bitmap("Sprites/FFT1/aphonsod2.png");
30 ALLEGRO_BITMAP* aphonsod3 = al_load_bitmap("Sprites/FFT1/aphonsod3.png");
31 if(!aphonsol0 && aphonsol1 && aphonsol2 && aphonsol3 && aphonsor0 && aphonsor1 && aphonsor2 && aphonsor3 && aphonsou0 && aphonsou1 && aphonsou2 && aphonsou3 && aphonsod0 && aphonsod1 && aphonsod2 && aphonsod3) {
32 fprintf(stderr, "Failed to load up, dowm, left and right images");
33 return -1;
34 }
35 //For clear the background images al_convert_mask_to_alpha(bmp, (al_map_rgb(255, 0, 255)));
36 /* Left */
37 anime[0].SetBitmap(0 , aphonsol0);
38 anime[0].SetBitmap(1 , aphonsol1);
39 anime[0].SetBitmap(2 , aphonsol2);
40 anime[0].SetBitmap(3 , aphonsol3);
41 /* Right */
42 anime[1].SetBitmap(0 , aphonsor0);
43 anime[1].SetBitmap(1 , aphonsor1);
44 anime[1].SetBitmap(2 , aphonsor2);
45 anime[1].SetBitmap(3 , aphonsor3);
46 /* Up */
47 anime[2].SetBitmap(0 , aphonsou0);
48 anime[2].SetBitmap(1 , aphonsou1);
49 anime[2].SetBitmap(2 , aphonsou2);
50 anime[2].SetBitmap(3 , aphonsou3);
51 /* Down */
52 anime[3].SetBitmap(0 , aphonsod0);
53 anime[3].SetBitmap(1 , aphonsod1);
54 anime[3].SetBitmap(2 , aphonsod2);
55 anime[3].SetBitmap(3 , aphonsod3);
56
57 map<string , Animation*> amap;
58 amap["left"] = &(anime[0]);
59 amap["right"] = &(anime[1]);
60 amap["up"] = &(anime[2]);
61 amap["down"] = &(anime[3]);
62
63 Animation* current = &(anime[2]);
64
65 al_register_event_source(event_queue, al_get_display_event_source(display));
66
67 al_register_event_source(event_queue, al_get_timer_event_source(timer));
68
69 al_register_event_source(event_queue, al_get_keyboard_event_source());
70
71 al_register_event_source(event_queue, al_get_mouse_event_source());
72
73 al_get_timer_count(timer);
74
75 al_start_timer(timer);
76
77 bool quit;
78 bool redraw;
79 while (!quit) {
80 while (1) {
81 ALLEGRO_EVENT ev;
82 al_wait_for_event(event_queue , &ev);
83 if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
84 if (ev.keyboard.keycode == ALLEGRO_KEY_LEFT) {
85 current = amap["left"];
86 current->SetFrameTime(0.0);
87 key[LEFT] = true;
88 break;
89 }
90 if (ev.keyboard.keycode == ALLEGRO_KEY_RIGHT) {
91 current = amap["right"];
92 current->SetFrameTime(0.0);
93 key[RIGHT] = true;
94 break;
95 }
96 if (ev.keyboard.keycode == ALLEGRO_KEY_UP) {
97 current = amap["up"];
98 current->SetFrameTime(0.0);
99 key[UP] = true;
100 break;
101 }
102 if (ev.keyboard.keycode == ALLEGRO_KEY_DOWN) {
103 current = amap["down"];
104 current->SetFrameTime(0.0);
105 key[DOWN] = true;
106 break;
107 }
108 }
109 else if (ev.type == ALLEGRO_EVENT_KEY_UP) {
110 if (ev.keyboard.keycode == ALLEGRO_KEY_LEFT) {
111 key[LEFT] = false;
112 break;
113 }
114 if (ev.keyboard.keycode == ALLEGRO_KEY_RIGHT) {
115 key[RIGHT] = false;
116 break;
117 }
118 if (ev.keyboard.keycode == ALLEGRO_KEY_UP) {
119 key[UP] = false;
120 break;
121 }
122 if (ev.keyboard.keycode == ALLEGRO_KEY_DOWN) {
123 key[DOWN] = false;
124 break;
125 }
126 }
127 else if (ev.type == ALLEGRO_EVENT_TIMER) {
128 if (key[LEFT] || key[RIGHT] || key[UP] || key[DOWN]) {
129 current->AdvanceFrameTime(1.0/FPS);
130 if (key[LEFT]) {
131 cx -= 1;
132 cy --;
133 }
134 if (key[RIGHT]) {
135 cx += 1;
136 cy ++;
137 }
138 if (key[UP]) {
139 cx += 1;
140 cy --;
141 }
142 if (key[DOWN]) {
143 cx -= 1;
144 cy ++;
145 }
146 } else {
147 current->SetFrameTime(0.0);
148 }
149 redraw = true;
150 }
151 if (redraw) {
152 al_clear_to_color(al_map_rgb(0,0,0));
153 current->Draw(cx, cy);
154 al_flip_display();
155 redraw = false;
156 }
157 }
158 }
159};
Now I'm beginning to do a isometric map with some tiles that I have! Edit1: I can add my "map_draw();" in the "if(redraw){" or I have to create a class for him and to do it work with the "current->" ? Edit2: Hi... I removed the "if(redraw) {" and add the "if (al_event_queue_is_empty(event_queue)) {" in your place for put the "map_draw();" , but I think this did not the right way! ... while (!quit) { while (1) { ALLEGRO_EVENT ev; if (al_event_queue_is_empty(event_queue)) { current->Draw(cx, cy); al_flip_display(); al_clear_to_color(al_map_rgb(0,0,0)); map_draw(); } al_wait_for_event(event_queue , &ev); if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { ...
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
Jefferson Almeida said: Edit1: I can add my "map_draw();" in the "if(redraw){" or I have to create a class for him and to do it work with the "current->" ? Yes, just add map_draw() to the 'if (redraw)' block. Jefferson Almeida said: I removed the "if(redraw) {" and add the "if (al_event_queue_is_empty(event_queue)) {" in your place for put the "map_draw();" , but I think this did not the right way! Think about it - do you want to redraw when the timer goes off, or do you want to redraw when the event queue has been fully processed? My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Jefferson Almeida
Member #12,659
March 2011
|
When I used the "if(redraw) {" and add "map_draw();" the movement of the character stayed with a imense lag. I think with the time will to give bug! |
Edgar Reynaldo
Major Reynaldo
May 2007
|
If the character movement is lagging behind, you are either not altering it's position often enough, or you are not redrawing enough. Jefferson Almeida said: I think with the time will to give bug! What? My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Jefferson Almeida
Member #12,659
March 2011
|
Hi, I thought in to create an animation for the character when I press the key F1, but I remember what has many positions: RIGHT, LEFT, UP... has some event that check the last key press for I can to make the animation from him when not in motion. 1#include <map>
2using std::map;
3
4#include <string>
5using std::string;
6
7
8/* Test */
9int cx = 10;
10int cy = 10;
11
12
13
14class Animation {
15private :
16 ALLEGRO_BITMAP** frames;
17 double duration;
18 double time_passed;
19 int frame_number;
20 int num_frames;
21public :
22 Animation() :
23 frames(0),
24 duration(0.0),
25 time_passed(0.0),
26 frame_number(0),
27 num_frames(0)
28 {}
29 Animation(int framecount , double time_length) :
30 frames(0),
31 duration(0.0),
32 time_passed(0.0),
33 frame_number(0),
34 num_frames(0)
35 {
36 Reallocate(framecount , time_length);
37 }
38 ~Animation() {Free();}
39 void Free() {
40 if (frames) {delete [] frames;frames = 0;}
41 num_frames = 0;
42 }
43 bool Reallocate(int framecount , double time_length) {
44 if (framecount < 1) {return false;}
45 if (time_length <= 0.0) {return false;}
46 Free();
47 duration = time_length;
48 time_passed = 0.0;
49 frame_number = 0;
50 try {
51 frames = new ALLEGRO_BITMAP*[framecount];
52 }
53 catch(...) {
54 frames = 0;
55 return false;
56 }
57 num_frames = framecount;
58 return true;
59 }
60
61 void SetBitmap(int index , ALLEGRO_BITMAP* bmp) {
62 assert((index >= 0) && (index < num_frames));
63 frames[index] = bmp;
64 }
65
66 void AdvanceFrameTime(double delta_time) {
67 SetFrameTime(time_passed + delta_time);
68 }
69 void SetFrameTime(double frame_time) {
70 // simple forward repeating animation
71 time_passed = frame_time;
72 while (time_passed >= duration) {time_passed -= duration;}
73 while (time_passed < 0.0) {time_passed += duration;}
74 frame_number = (int)((float)num_frames*(time_passed / duration));
75 // shouldn't be necessary, but just in case
76 if (frame_number < 0) {frame_number = 0;}
77 if (frame_number >= num_frames) {frame_number = num_frames - 1;}
78 }
79
80 void Draw(int cx , int cy) {
81 al_draw_bitmap(frames[frame_number], cx , cy , 0);
82 }
83
84 double FrameTime() {return time_passed;}
85 int FrameNum() {return frame_number;}
86
87};
88
89enum KEYS {
90 LEFT = 0,
91 RIGHT = 1,
92 UP = 2,
93 DOWN = 3,
94 F1 = 4,
95 NUMKEYS = 5
96};
97
98 // Here my main
99 ...
100
101 bool key[NUMKEYS];
102 for (int i = 0 ; i < NUMKEYS ; ++i) {key[i] = false;}
103
104 Animation anime[5];
105 anime[0].Reallocate(4 , 0.1);
106 anime[1].Reallocate(4 , 0.1);
107 anime[2].Reallocate(4 , 0.1);
108 anime[3].Reallocate(4 , 0.1);
109 anime[4].Reallocate(4 , 0.1);
110 // Here Load images...
111 ...
112 // Here Setbitmap
113 /* Left */
114 anime[0].SetBitmap(0 , aphonsol0);
115 anime[0].SetBitmap(1 , aphonsol1);
116 anime[0].SetBitmap(2 , aphonsol2);
117 anime[0].SetBitmap(3 , aphonsol3);
118 /* Right */
119 anime[1].SetBitmap(0 , aphonsor0);
120 anime[1].SetBitmap(1 , aphonsor1);
121 anime[1].SetBitmap(2 , aphonsor2);
122 anime[1].SetBitmap(3 , aphonsor3);
123 /* Up */
124 anime[2].SetBitmap(0 , aphonsou0);
125 anime[2].SetBitmap(1 , aphonsou1);
126 anime[2].SetBitmap(2 , aphonsou2);
127 anime[2].SetBitmap(3 , aphonsou3);
128 /* Down */
129 anime[3].SetBitmap(0 , aphonsod0);
130 anime[3].SetBitmap(1 , aphonsod1);
131 anime[3].SetBitmap(2 , aphonsod2);
132 anime[3].SetBitmap(3 , aphonsod3);
133 /* F1 */
134 //anime[4].
135
136 map<string , Animation*> amap;
137 amap["left"] = &(anime[0]);
138 amap["right"] = &(anime[1]);
139 amap["up"] = &(anime[2]);
140 amap["down"] = &(anime[3]);
141 amap["f1"] = &(anime[4]);
142
143 Animation* current = &(anime[2]);
144
145 al_register_event_source(event_queue, al_get_display_event_source(display));
146
147 al_register_event_source(event_queue, al_get_timer_event_source(timer));
148
149 al_register_event_source(event_queue, al_get_keyboard_event_source());
150
151 al_register_event_source(event_queue, al_get_mouse_event_source());
152
153 al_get_timer_count(timer);
154
155 al_start_timer(timer);
156
157 al_flip_display();
158
159 bool quit;
160 bool redraw;
161 while (!quit) {
162 while (1) {
163 ALLEGRO_EVENT ev;
164 if(redraw && al_is_event_queue_empty(event_queue)) {
165 current->Draw(cx, cy);
166 al_flip_display();
167 al_clear_to_color(al_map_rgb(0,0,0));
168 map_draw();
169 redraw = false;
170 }
171 al_wait_for_event(event_queue , &ev);
172 if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
173 quit = true;
174 break;
175 }
176 else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
177 if (ev.keyboard.keycode == ALLEGRO_KEY_LEFT) {
178 current = amap["left"];
179 current->SetFrameTime(0.0);
180 key[LEFT] = true;
181 break;
182 }
183 if (ev.keyboard.keycode == ALLEGRO_KEY_RIGHT) {
184 current = amap["right"];
185 current->SetFrameTime(0.0);
186 key[RIGHT] = true;
187 break;
188 }
189 if (ev.keyboard.keycode == ALLEGRO_KEY_UP) {
190 current = amap["up"];
191 current->SetFrameTime(0.0);
192 key[UP] = true;
193 break;
194 }
195 if (ev.keyboard.keycode == ALLEGRO_KEY_DOWN) {
196 current = amap["down"];
197 current->SetFrameTime(0.0);
198 key[DOWN] = true;
199 break;
200 }
201 if (ev.keyboard.keycode == ALLEGRO_KEY_F1) {
202 //current = amap["f1"];
203 //current->SetFrameTime(0.0);
204 //key[F1] = true;
205 al_draw_bitmap(menuedit, 500, 80, 0);
206 break;
207 }
208 if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) {
209 quit = true;
210 break;
211 }
212 }
213 else if (ev.type == ALLEGRO_EVENT_KEY_UP) {
214 if (ev.keyboard.keycode == ALLEGRO_KEY_LEFT) {
215 key[LEFT] = false;
216 break;
217 }
218 if (ev.keyboard.keycode == ALLEGRO_KEY_RIGHT) {
219 key[RIGHT] = false;
220 break;
221
222 }
223 if (ev.keyboard.keycode == ALLEGRO_KEY_UP) {
224 key[UP] = false;
225 break;
226 }
227 if (ev.keyboard.keycode == ALLEGRO_KEY_DOWN) {
228 key[DOWN] = false;
229 break;
230 }
231 if (ev.keyboard.keycode == ALLEGRO_KEY_F1) {
232 key[F1] = false;
233 break;
234 }
235 if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) {
236 quit = false;
237 break;
238 }
239 }
240 else if (ev.type == ALLEGRO_EVENT_TIMER) {
241 if (key[LEFT] || key[RIGHT] || key[UP] || key[DOWN] || key[F1]) {
242 current->AdvanceFrameTime(1.0/FPS);
243 if (key[LEFT]) {
244 cx -= 1;
245 cy --;
246 }
247 if (key[RIGHT]) {
248 cx += 1;
249 cy ++;
250 }
251 if (key[UP]) {
252 cx += 1;
253 //cy --;
254 }
255 if (key[DOWN]) {
256 cx -= 1;
257 //cy ++;
258 }
259 if (key[F1]) {
260 //
261 }
262 } else {
263 current->SetFrameTime(0.0);
264 }
265 redraw = true;
266
267 }
268 }
269 }
270
271 al_destroy_timer(timer);
272 al_destroy_display(display);
273 al_destroy_event_queue(event_queue);
274 return 0;
275};
|
|
|