Can somebody please help me to correct the "flashing" animation. I don't know how to correct this.
sprite.h
1 | |
2 | include <allegro.h> |
3 | |
4 | int spr_w=100; |
5 | |
6 | static volatile int sprite_time=0; |
7 | void STimer(void){ |
8 | sprite_time++; |
9 | } |
10 | END_OF_FUNCTION(STimer); |
11 | |
12 | |
13 | class Sprite{ |
14 | |
15 | public: |
16 | |
17 | |
18 | |
19 | |
20 | BITMAP *sprite_l[5]; |
21 | BITMAP *sprite_r[5]; |
22 | char buffer1[255]; |
23 | char buffer[255]; |
24 | int xpos,ypos,speed; |
25 | |
26 | |
27 | |
28 | |
29 | |
30 | |
31 | Sprite():xpos (0),ypos (SCREEN_H/2),speed(5){ |
32 | |
33 | for (int a=1;a<=2;a++){ |
34 | sprintf(buffer1, "grafik/sprite/hightler_walking_l_%i.bmp",a); |
35 | sprite_l[a]=load_bitmap(buffer1, NULL); |
36 | } |
37 | |
38 | |
39 | for (int i=1;i<=2;i++){ |
40 | sprintf(buffer, "grafik/sprite/hightler_walking_r_%i.bmp",i); |
41 | sprite_r<i>=load_bitmap(buffer, NULL); |
42 | } |
43 | |
44 | |
45 | } |
46 | ~Sprite(){ |
47 | destroy_bitmap(sprite_l[2]); |
48 | destroy_bitmap(sprite_r[2]); |
49 | } |
50 | |
51 | |
52 | void mouse_beweg(int x){ |
53 | if (mouse_b &1){ |
54 | xpos=x-(2/spr_w); |
55 | } |
56 | } |
57 | |
58 | |
59 | |
60 | void control(){ |
61 | |
62 | |
63 | if(key[KEY_LEFT]){ |
64 | if(xpos -speed<=0) xpos=0;//-32; |
65 | else xpos -=speed; |
66 | } |
67 | |
68 | //info 100==sprite_w |
69 | if(key[KEY_RIGHT]&&xpos>0) |
70 | |
71 | { if(xpos + speed +100> SCREEN_W) |
72 | xpos = SCREEN_W - 100; |
73 | else xpos += speed; |
74 | } |
75 | } |
76 | |
77 | void Draw(BITMAP *b){ |
78 | |
79 | LOCK_VARIABLE(sprite_time); |
80 | LOCK_FUNCTION((void*)STimer); |
81 | install_int_ex(STimer, BPS_TO_TIMER(60)); |
82 | |
83 | int x; |
84 | x=mouse_beweg(mouse_x); |
85 | textprintf(b, font, 30, 140,makecol(128,128,266),"Entfernung zur maus: %i", mouse_x-xpos); |
86 | |
87 | |
88 | |
89 | if(sprite_time>=60){ |
90 | sprite_time=0; |
91 | } |
92 | textprintf(b, font, 200,200,makecol(255,0,255),"FPS SPRITE:%i",sprite_time); |
93 | |
94 | |
95 | |
96 | |
97 | stretch_sprite(b,sprite_r[2],xpos,ypos,100,100); |
98 | if(key[KEY_RIGHT]||mouse_x>0){ |
99 | |
100 | |
101 | //here must be the problem |
102 | if (mouse_b &1){ |
103 | xpos=mouse_x-xpos; |
104 | vsync(); |
105 | for(xpos=xpos;xpos<mouse_x;xpos++){ |
106 | do{ |
107 | |
108 | stretch_sprite(b, sprite_r[1], xpos, ypos, 100, 100); |
109 | } |
110 | while(sprite_time%1>0); |
111 | |
112 | |
113 | } |
114 | |
115 | |
116 | } |
117 | } |
118 | } |
119 | int sprite_pos_x(){ |
120 | return xpos; |
121 | } |
122 | |
123 | int sprite_pos_y(){ |
124 | return ypos; |
125 | } |
126 | }; //END_OF_SPRITE_CLASS |
Thank you
1. You should invest some time in improving your coding style. Your code is extremely difficult to read and follow.
2. Your Draw function is completely wrong. You should read again the part of the manual that explains how timers are used.