0

I was trying to make a simple animation of a stickman walking, in graphics library in C.

My code:

#include<stdio.h>
#include<graphics.h>

void swap(int *x, int *y){

    int temp=0;
    temp=*x;
    *x=*y;
    *y=temp;
}


int main(){
    int gd=DETECT,gm=0;
    int i=0;
    int a=30+i;
    int b=50+i;
    initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
    while(i!=600){
    swap(&a,&b);
    circle(40+i,40,30);
    line(40+i,70,a+i,90);
    line(40+i,70,b+i,90);
    i++;
    delay(10);
    cleardevice();
    }
    getch();
    closegraph();
    return 0;
}

The problem is that, the monitor screen keeps flickering constantly while playing the animation and moreover, the leg positions are not swapping according to the swap(&b,&c) function. Where am I going wrong here? Can someone please help me?

Fix: So, I realised that, there's no point in having the swap() function as, swapping the lines will make the alignment same again for both of them (as a result it will look like the legs are not moving), so I decided to tweak some things here:

while(i!=600){
    if(i%2==0){
        circle(40+i,40,30);
        line(40+i,70,(a+b)/2+i,90);
        line(40+i,70,(b+a)/2+i,90);

    }else{
        circle(40+i,40,30);
        line(40+i,70,a+i,90);
        line(40+i,70,b+i,90);
    }
    i++;
    delay(10);
    cleardevice();
    }

Now, it actually looks like the figure is walking. But I still don't know how to fix the screen flickering problem.

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 1
    Have you ever head of [*page flipping*](https://stackoverflow.com/questions/8639408/what-does-it-mean-by-page-flipping-in-computer-graphics-programming) ? That's one way to eliminate flicker, and yes, it can be a pain. If your graphics library supports it, consider it, or something similar. – WhozCraig Oct 26 '19 at 03:55
  • @FMA either use buffering like WhozCraig suggested or render incrementaly (which still flickers but a lot less) ... you main problem now is the `cleardevice()` ... PS buffering is much much easier to implement and does not flicker at all ... IIRC BGI have some functions in it to support buffering – Spektre Oct 26 '19 at 07:32
  • Learn to debug, at least put some `printf()` at strategic places to watch your variables `b` and `c`. This will help you find that issue. BTW, the code of `swap()` has no obvious error. – the busybee Oct 26 '19 at 09:22
  • @thebusybee sorry, I debugged and checked that the `swap()` function was working. But it was pointless in this code. –  Oct 26 '19 at 15:40
  • Did you consider using frame buffers and switch these? You erase a non-visible buffer and draw in it as slow as it goes, and then exchange it in a blink. Visit the given URL and learn about the issue. – the busybee Oct 26 '19 at 15:45
  • 1
    Be sure to hook the vertical retrace interrupt. Then when the interrupt occurs, do the image flip. Then the image flipping will not cause a flickering screen – user3629249 Oct 27 '19 at 00:59
  • regarding: `#include` The `graphics.h` is from the days of DOS. Much better to use a 'modern' graphics library – user3629249 Oct 27 '19 at 01:00

0 Answers0