Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » flickering image

This thread is locked; no one can reply to it. rss feed Print
flickering image
fatboy2
Member #8,239
January 2007

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(){
2rotate_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 
35tankcenterx=tankx+16;
36tankcentery=tanky+16;
37 
38if (mouse_x>tankcenterx&&mouse_y<tankcentery){
39 area =1;
40 }
41if (mouse_x>tankcenterx&&mouse_y>tankcentery) {
42 area =2;
43 }
44 if (mouse_x<tankcenterx&&mouse_y>tankcentery){
45 area=3;
46 }
47if (mouse_x<tankcenterx&&mouse_y<tankcentery){
48 area=4;
49 }
50if (area==1){
51 rotationangle=atan((mouse_x-tankcenterx)/(tankcentery-mouse_y));
52 // tanrotationangle=(mouse_x-tankcenterx)/(mouse_y-tankcentery);
53 }
54if (area==2){
55 rotationangle=atan((mouse_y-tankcentery)/(mouse_x-tankcenterx))+3.1415926/2;
56 // tanrotationangle=(mouse_y-tankcentery)/(mouse_x-tankcenterx);
57 }
58if(area==3){
59 rotationangle=atan((tankcenterx-mouse_x)/(mouse_y-tankcentery))+3.1415926;
60 // tanrotationangle=(tankcenterx-mouse_x)/(mouse_y-tankcentery);
61 }
62if (area==4){
63 rotationangle=atan((tankcentery-mouse_y)/(tankcenterx-mouse_x))+1.5*3.1415926;
64 // tanrotationangle=(tankcentery-mouse_y)/(tankcenterx-mouse_x);
65 }
66allegrorotangle=rotationangle*128/3.1415926;
67float 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 }
93rotate_sprite(screen,tank,tankx,tanky,itofix(allegrorotangle));
94 blit(tank, screen, 0,0,0,0,640,480);
95 clear_bitmap(buffer);

Albin Engström
Member #8,110
December 2006
avatar

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.

Ceagon Xylas
Member #5,495
February 2005
avatar

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.

Quote:

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.

fatboy2
Member #8,239
January 2007

Thank you for the advice. It seems to work properly now.

blargmob
Member #8,356
February 2007
avatar

Albin Engström said:

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?

---
"No amount of prayer would have produced the computers you use to spread your nonsense." Arthur Kalliokoski

gnolam
Member #2,030
March 2002
avatar

Yes. Read the FAQ. :P

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

HardTranceFan
Member #7,317
June 2006
avatar

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.

--
"Shame your mind don't shine like your possessions do" - Faithless (I want more part 1)

Matthew Dalrymple
Member #7,922
October 2006
avatar

Quote:

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.

http://alleg.sourceforge.net/

Hmm that's odd... I see FAQ right there on the left, on the main page.

=-----===-----===-----=
I like signatures that only the signer would understand. Inside jokes are always the best, because they exclude everyone else.

HardTranceFan
Member #7,317
June 2006
avatar

My bad - I was looking on allegro.cc.

Sarcasim duly noted. :P

--
"Shame your mind don't shine like your possessions do" - Faithless (I want more part 1)

Matthew Dalrymple
Member #7,922
October 2006
avatar

Teehee ::)

=-----===-----===-----=
I like signatures that only the signer would understand. Inside jokes are always the best, because they exclude everyone else.

Go to: