Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Flicker free animation for Game

noname01noname01 Posts: 1Member
I am trying to make a dartboard game with a moving dartboard. I have animated the dartboard but it flickers like hell. I have been facing this problem from the day i started to animate objects. Can someone please suggest a solution for minimising the flickering without going overboard in Assembly and VGA programming(cos I am still learning Assembly).

Check out the following code in Turbo C++ to animate the dartboard(which flickers)

#include
#include
#include
#include

void driver()
{
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "c:\tc\bgi");
errorcode = graphresult();
if (errorcode != grOk) exit(1);
}

void DrawBoard(int x,int y)
{
setfillstyle(1,14);
bar(x,y-5,x+5,y+5);
setfillstyle(1,13);
bar(x+5,y-10,x+15,y+10);
setfillstyle(1,12);
bar(x+15,y-20,x+25,y+20);
setfillstyle(1,7);
bar(x+25,y-30,x+35,y+30);
}

void EraseBoard(int x,int y)
{
setfillstyle(1,0);
bar(x,y-30,x+40,y+30);
}

void MoveBoard()
{
int y=240;
DrawBoard(450,y);
getch();
EraseBoard(450,y);
while(!kbhit())
{
static flag=1;
DrawBoard(450,y);
EraseBoard(450,y);
delay(10);
y+=flag;
if(y==400) flag*=-1;
if(y==100) flag*=-1;
}
}

void main()
{
driver();
//setcolor(10);
MoveBoard();
getch();
}


Comments

  • 97256449725644 Posts: 32Member
    The secret to removing flickering from amimation is to use virtual screens. Instead of writing directly to the screen, write to a buffer, the copy this to the screen

    declare buffers as follows

    // declare a pointer to the offset of VGA memory
    unsigned char *vga = (unsigned char *) MK_FP(0xA000, 0);
    // declare a pointer to the offset of virtual screen.
    unsigned char *vscreen = (unsigned char *) MK_FP(0x6000 , 0);


    Now in the drawing functions, write to the vscreen

    eg.
    void ghline(int x1,int x2,int y,unsigned char colour)
    {
    memset (vscreen+(y*320)+x1, colour, x2-x1+1);
    }

    instead of

    void ghline(int x1,int x2,int y,unsigned char colour)
    {
    memset (vga+(y*320)+x1, colour, x2-x1+1);
    }

    When you have all the drawing of one frame complete, copy this to the screen

    void memtoscreen()
    {
    memcpy(vga,vscreen,size_t(6400*10));
    }

    Hope this helps. If not just ask again.

    Ciaran

  • gautamgautam Posts: 642Member
    Make use << & >> for * and /, they are faster. With BGI, assembly's vertical blanking doesn't work I think.

    : The secret to removing flickering from amimation is to use virtual screens. Instead of writing directly to the screen, write to a buffer, the copy this to the screen
    :
    : declare buffers as follows
    :
    : // declare a pointer to the offset of VGA memory
    : unsigned char *vga = (unsigned char *) MK_FP(0xA000, 0);
    : // declare a pointer to the offset of virtual screen.
    : unsigned char *vscreen = (unsigned char *) MK_FP(0x6000 , 0);
    :
    :
    : Now in the drawing functions, write to the vscreen
    :
    : eg.
    : void ghline(int x1,int x2,int y,unsigned char colour)
    : {
    : memset (vscreen+(y*320)+x1, colour, x2-x1+1);
    : }
    :
    : instead of
    :
    : void ghline(int x1,int x2,int y,unsigned char colour)
    : {
    : memset (vga+(y*320)+x1, colour, x2-x1+1);
    : }
    :
    : When you have all the drawing of one frame complete, copy this to the screen
    :
    : void memtoscreen()
    : {
    : memcpy(vga,vscreen,size_t(6400*10));
    : }
    :
    : Hope this helps. If not just ask again.
    :
    : Ciaran
    :


Sign In or Register to comment.