troubleshooting Question

Need help....Double Buffering?

Avatar of Punkman
Punkman asked on
C++
15 Comments2 Solutions886 ViewsLast Modified:
Hmm...not sure if this is a newbie question or not. I am making a game in C++ (Turbo C++, for DOS so no windows stuff) and i have made a simple test to see if i could get the background to not be drawn over by my character. Here is my program:

---------------------------------------------------------------------------
#include<conio.h>
#include<iostream.h>
#include<dos.h>
#include<graphics.h>
#include<useful4.h> //programmer made one, don't worry about it, lets me just use gfxinit which initilizes graphics

void main()
{ gfxinit();
cleardevice();
setbkcolor(2);
bar(0,0,40,20); //just background stuff
bar(200,250,240,260);
setfillstyle(3,5);
fillellipse(300,300,20,30);
fillellipse(100,100,10,10);
int w=19;
int ship[49][21]={ //my character, a space ship
{w,w,w,w,w,w,w,w,w,w,w,w,w,w,w,w,w,w,w,w,w},
{w,w,w,w,w,w,w,w,8,8,4,8,8,w,w,w,w,w,w,w,w},
{w,w,w,w,w,w,w,8,7,7,4,7,7,8,w,w,w,w,w,w,w},
{w,w,w,w,w,w,8,7,7,7,4,7,7,7,8,w,w,w,w,w,w},
{w,w,w,w,w,8,7,7,7,7,7,7,7,7,7,8,w,w,w,w,w},
{w,w,w,w,8,7,7,7,7,7,7,7,7,7,7,7,8,w,w,w,w},
{w,w,w,8,7,7,7,7,7,7,8,7,7,7,7,7,7,8,w,w,w},
{w,w,w,8,7,7,7,7,7,8,8,8,7,7,7,7,7,8,w,w,w},
{w,w,w,8,7,7,7,7,8,7,8,7,8,7,7,7,7,8,w,w,w},
{w,w,8,7,7,7,7,8,7,7,8,7,7,8,7,7,7,7,8,w,w},
{w,w,8,7,7,7,8,7,7,7,7,7,7,7,8,7,7,7,8,w,w},
{w,w,8,7,7,7,8,7,8,8,8,8,8,7,8,7,7,7,8,w,w},
{w,w,8,7,7,8,7,7,7,7,7,7,7,7,7,8,7,7,8,w,w},
{w,8,7,7,7,8,7,7,7,8,8,8,7,7,7,8,7,7,7,8,w},
{w,8,7,7,7,8,7,7,8,7,7,7,8,7,7,8,7,7,7,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7,7,7,w,7,7,8,7,8,7,7,w,7,7,7,8,7,w},
{w,7,8,7,7,7,8,7,7,8,7,8,7,7,8,7,7,7,8,7,w},
{w,7,8,7,7,7,7,8,7,7,8,7,7,8,7,7,7,7,8,7,w},
{w,7,8,7,7,7,7,8,7,7,8,7,7,8,7,7,7,7,8,7,w},
{w,7,8,7,7,7,7,8,7,7,7,7,7,8,7,7,7,7,8,7,w},
{w,7,8,7,7,7,7,7,8,7,7,7,8,7,7,7,7,7,8,7,w},
{w,7,8,7,w,7,7,7,8,7,7,7,8,7,7,7,w,7,8,7,w},
{w,7,7,7,w,w,7,7,8,8,8,8,8,7,7,w,w,7,7,7,w},
{w,w,w,w,w,w,w,7,7,14,7,14,7,7,w,w,w,w,w,w,w},
{w,w,w,w,w,w,w,w,7,7,7,7,7,w,w,w,w,w,w,w,w},
{w,w,w,w,w,w,w,w,w,w,w,w,w,w,w,w,w,w,w,w,w}};

int writeover[49][21];

int x=100,y=100;
char move;


for( int r=0; r<49; r++) //gets screen
for( int c=0; c<21; c++)
writeover[r][c]=getpixel(x+c,y+r);


do
{



move='x';
if(kbhit())
{
for( r=0; r<49; r++) //over writes
for( c=0; c<21; c++)
putpixel(x+c,y+r,writeover[r][c]);

move=getch();

if(move=='6')
{ x=x+4; }

if(move=='4')
{ x=x-4; }

if(move=='8')
{ y=y-4; }

if(move=='2')
{ y=y+4; }




for( int r=0; r<49; r++) //gets screen
for( int c=0; c<21; c++)
writeover[r][c]=getpixel(x+c,y+r);

for( r=0; r<49; r++) //outputs guy
for( c=0; c<21; c++)
{ if(ship[r][c]!=19)
putpixel(x+c,y+r,ship[r][c]);
}




}

}
while(move!='q');


getch();
}

--------------------------------------------------------------------------------

Sorry if the indenting is screwed up and stuff, didn't transpher well from the txt. Anyway, this pretty much works, only it gives me a huge flicker when i move the ship. Copy it and paste it int C++ and see what i mean. My teacher said that the only way to make it not flicker is to use double buffering. He says that we don't learn how to do double buffering in this class. Well i want to learn how so that i can make my game better! I know that it has to deal with storing information in "invisible" memory. But other then that im lost. Could anyone help me with this? THANKS!!!!!!!

PS: Or is there another way to make it not flicker?
PSS: This is a really simple program, and i did it in like 5 minutes so that i could just see if it works, sorry if its a little slopy!
ASKER CERTIFIED SOLUTION
ambience

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 15 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 15 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros