I'm creating a program which allows a tank image move around. Problem with it is that the image flickers. It would be great if anyone would tell me what is causing this flicker.
1 | int drawtank(){ |
2 | rotate_sprite(screen,tank,tankx,tanky,itofix(allegrorotangle)); |
3 | if(key[KEY_RIGHT] ) |
4 | { |
5 | tankx ++; |
6 | } |
7 | if(key[KEY_LEFT]) |
8 | { |
9 | tankx --; |
10 | } |
11 | if(key[KEY_UP]) |
12 | { |
13 | tanky --; |
14 | } |
15 | if(key[KEY_DOWN]) |
16 | { |
17 | tanky ++; |
18 | } |
19 | |
20 | if (tankx < 1 ) |
21 | { |
22 | tankx=1; |
23 | } |
24 | if ( tankx >608) |
25 | { |
26 | tankx = 608; |
27 | } |
28 | if ( tanky<1){ |
29 | tanky=1; |
30 | } |
31 | if (tanky > 448){ |
32 | tanky = 448; |
33 | } |
34 | |
35 | tankcenterx=tankx+16; |
36 | tankcentery=tanky+16; |
37 | |
38 | if (mouse_x>tankcenterx&&mouse_y<tankcentery){ |
39 | area =1; |
40 | } |
41 | if (mouse_x>tankcenterx&&mouse_y>tankcentery) { |
42 | area =2; |
43 | } |
44 | if (mouse_x<tankcenterx&&mouse_y>tankcentery){ |
45 | area=3; |
46 | } |
47 | if (mouse_x<tankcenterx&&mouse_y<tankcentery){ |
48 | area=4; |
49 | } |
50 | if (area==1){ |
51 | rotationangle=atan((mouse_x-tankcenterx)/(tankcentery-mouse_y)); |
52 | // tanrotationangle=(mouse_x-tankcenterx)/(mouse_y-tankcentery); |
53 | } |
54 | if (area==2){ |
55 | rotationangle=atan((mouse_y-tankcentery)/(mouse_x-tankcenterx))+3.1415926/2; |
56 | // tanrotationangle=(mouse_y-tankcentery)/(mouse_x-tankcenterx); |
57 | } |
58 | if(area==3){ |
59 | rotationangle=atan((tankcenterx-mouse_x)/(mouse_y-tankcentery))+3.1415926; |
60 | // tanrotationangle=(tankcenterx-mouse_x)/(mouse_y-tankcentery); |
61 | } |
62 | if (area==4){ |
63 | rotationangle=atan((tankcentery-mouse_y)/(tankcenterx-mouse_x))+1.5*3.1415926; |
64 | // tanrotationangle=(tankcentery-mouse_y)/(tankcenterx-mouse_x); |
65 | } |
66 | allegrorotangle=rotationangle*128/3.1415926; |
67 | float m; |
68 | //tanrotationangle=tan(rotationangle+3.1415926/2); |
69 | if (key[KEY_W]){ |
70 | if (area==1){ |
71 | m=(mouse_y-tankcentery)/(mouse_x-tankcenterx); |
72 | tankx++; |
73 | tanky=m+tanky; |
74 | } |
75 | if (area==2){ |
76 | m=(mouse_y-tankcentery)/(mouse_x-tankcenterx); |
77 | tankx++; |
78 | tanky=m+tanky; |
79 | } |
80 | if (area==3){ |
81 | m=(mouse_y-tankcentery)/(tankcenterx-mouse_x); |
82 | tankx--; |
83 | tanky=m+tanky; |
84 | } |
85 | if (area==4){ |
86 | m=(mouse_y-tankcentery)/(tankcenterx-mouse_x); |
87 | tankx--; |
88 | tanky=m+tanky; |
89 | } |
90 | rotate_sprite(screen,tank,tankx,tanky,itofix(allegrorotangle)); |
91 | rest(50); |
92 | } |
93 | rotate_sprite(screen,tank,tankx,tanky,itofix(allegrorotangle)); |
94 | blit(tank, screen, 0,0,0,0,640,480); |
95 | clear_bitmap(buffer); |
Please post the rest of the code.
of what i can see is that you should rewrite it...
and why do you have a buffer while drawing directly to the screen?
get some proper timing instead of using rest(50),
use buffering.
triple or double.. (i don't know what you use but you don't seem to do it the right way..)
the newbie has spoken... i hope you get some more detailed answers soon.
You're not drawing to a buffer, but rather to the screen. This causes the 'flickering' to appear.
main() { ... BITMAP *buffer=create_bitmap(SCREEN_W,SCREEN_H); clear_to_color(buffer,makecol(0,0,0)); while(!key[KEY_ESC]) { //or while your program has not ended rotate_sprite(buffer,tank,tankx,tanky,itofix(allegrorotangle); //draw to the buffer blit(buffer,screen,0,0,0,0,buffer->w,buffer->h); //draw the buffer to the screen clear_to_color(buffer,makecol(0,0,0)); //clear the buffer, otherwise you'll get a 'blurred' look } destroy_bitmap(buffer); }
Check out AGDN for some good beginner tutorials.
use buffering. triple or double.. (i don't know what you use but you don't seem to do it the right way..)
Their code uses neither.
Thank you for the advice. It seems to work properly now.
get some proper timing instead of using rest(50)
In all of my programs, I use rest(10); in my while loop as a delay. Is that bad?
Yes. Read the FAQ.
Where does the FAQ mention anything about not using rest() for a delay? The closest to be found is making games run on the same speed on any computer.
Also, the FAQ isn't in an obvious place. Most web sites have the FAQ on the front page. Allegro's is down in the middle of the manual page.
Also, the FAQ isn't in an obvious place. Most web sites have the FAQ on the front page. Allegro's is down in the middle of the manual page.
Hmm that's odd... I see FAQ right there on the left, on the main page.
My bad - I was looking on allegro.cc.
Sarcasim duly noted.
Teehee