Thread: flickering

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    380

    flickering

    Using gotoxy() how would I reduce the flickering in my game? I know that part of my problem is cause by redrawing the whole screen all the time instead parts of it.

  2. #2
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274
    you can make an extra function called updatescreen(); add a xold, yold variable to hold the position pacman is on when the position is changed called updatescreen(); and in there draw the pacman using x and y coordinates and reprint 1 tile of the map which are xold and yold so that everytime you move the whole screen isn't redrawn only 2 tiles.

    so basically like this:
    Code:
    int main()
    {
    	int quit = 0;
    //	loadMap();
    	drawscreen();
    	while(!quit)
    	{	
    //		if(kbhit())
    		player.oldy = player.y;
    		player.oldx = player.x;
    		quit = getInput();
    		collision();
    		updatescreen();
    	}
    	return 0;
    }
    
    void updatescreen(void)
    {
    	gotoxy(player.x,player.y);
    	map[player.y][player.x] = 2;
    	gotoxy(player.xold,player.yold)
    	switch(map[player.yold][player.xold])
    	{
    		case 0: {textcolor(2); cprintf("."); break;}
    		case 1: {textcolor(9); cprintf("\xdb"); break;}
    		case 2: {textcolor(14); cprintf("\x02"); break;}
    		case 3: {textcolor(11); cprintf("\x02"); break; }
    		case 4: {printf(" "); break;}
    	}
    }
    hope that helped.
    ¿Red Baron?

    "Imagination is more important than knowledge"
    -Albert Einstein (1879-1955)

    Check out my games!

    [code] /* dont forget code tags! */ [/code]

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    OK, thanks for your help. So you're suggestion that I repeat the code that I have in drawscreen() in your updatescreen()?

  4. #4
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274
    yes/no call drawscreen before entering your loop, then everytime a key is pressed use updatescreen. drawscreen and updatescreen aren't suppose to have the same stuff, what i put in the example function is all you need dont add anything to it, unless you get an error or mistake.
    ¿Red Baron?

    "Imagination is more important than knowledge"
    -Albert Einstein (1879-1955)

    Check out my games!

    [code] /* dont forget code tags! */ [/code]

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    Okay, I've added your update function and now my drawscreen() contains:

    Code:
    void drawscreen(void)
    {
    	int i,j, x = 1, y = 1;
    	map[player.y][player.x] = 2;
    	for(i =0; i < MAXY; i++)
    	{
    		for(j = 0; j < MAXX; j++)
    		{ gotoxy(x,y);
    			switch(map[i][j])
    				{
    					case 0: {textcolor(2); cprintf("."); break;}
    					case 1: {textcolor(9); cprintf("\xdb"); break;}
    					case 2: {textcolor(14); cprintf("\x02"); break;}
    					case 3: {textcolor(11); cprintf("\x01"); break; }
    					case 4: {printf(" "); break;}
    				}
    			x++;
    		}
    		x = 1;
    		y++;
    	}
    	gotoxy(50,1);
    	cprintf("X: %d Y: %d", player.x, player.y);
    }
    void updatescreen(void)
    {
    	map[player.oldy][player.oldx] = 4;
    	gotoxy(player.oldy,player.oldx);
    	switch(map[player.oldy][player.oldx])
    		{
    			case 0: {textcolor(2); cprintf("."); break;}
    			case 1: {textcolor(9); cprintf("\xdb"); break;}
    			case 2: {textcolor(14); cprintf("\x02"); break;}
    			case 3: {textcolor(11); cprintf("\x02"); break; }
    			case 4: {printf(" "); break;}
    		}
    	gotoxy(50,1);
    	cprintf("X: %d Y: %d", player.x, player.y);
    }
    The problem now is for some reason the cursor gets positioned at 1,1 instead of 1,2 and input response isn't the same.

  6. #6
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274
    can't see anything wrong with your program, put the whole code up and i'll try to help
    ¿Red Baron?

    "Imagination is more important than knowledge"
    -Albert Einstein (1879-1955)

    Check out my games!

    [code] /* dont forget code tags! */ [/code]

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    here's the source

  8. #8
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274
    wow was there a lot of errors, since i'm a nice guy i fixed it all for you...

    Code:
    int main()
    {
    	int quit = 0;
    	drawscreen();
    	while(!quit)
    	{	
    		player.oldy = player.y;
    		player.oldx = player.x;
    		quit = getInput();
    		updatescreen();
    	}
    	return 0;
    }
    
    void drawscreen(void)
    {
    	int i,j, x = 1, y = 1;
    	player.x=1;
    	player.y=1;
    	for(i =0; i < MAXY; i++)
    	{
    		for(j = 0; j < MAXX; j++)
    		{ gotoxy(j,i);
    			switch(map[i][j])
    				{
    					case 0: {textcolor(2); cprintf("."); break;}    //dot
    					case 1: {textcolor(9); cprintf("\xdb"); break;}  //block
    					case 2: {textcolor(14); cprintf("\x02"); break;}  //person
    					case 3: {textcolor(11); cprintf("\x01"); break; }  //nme
    					case 4: {printf(" "); break;}               //empty
    				}
    		}
    		gotoxy(player.x,player.y);
    		textcolor(14); cprintf("\x02");
    	}
    	gotoxy(50,1);
    	cprintf("X: %d Y: %d", player.x, player.y);
    }
    
    
    
    
    void updatescreen(void)
    {
    	map[player.oldy][player.oldx] = 4;
        
    	gotoxy(player.oldx,player.oldy);
    	switch(map[player.oldy][player.oldx])
    		{
    			case 0: {textcolor(2); cprintf("."); break;}      //dot
    			case 1: {textcolor(9); cprintf("\xdb"); break;}    //block
    			case 2: {textcolor(14); cprintf("\x02"); break;}  //person
    			case 3: {textcolor(11); cprintf("\x02"); break; }  //nme
    			case 4: {printf(" "); break;}               //empty
    		}
    	
    	gotoxy(player.x,player.y);
        textcolor(14); cprintf("\x02");
    	
    	gotoxy(50,1);
    	cprintf("X: %d Y: %d", player.x, player.y);
    }
    
    
    
    int getInput(void)
    {
    	int move;
    	int quit = 0;
    	move = getch();
    	if(move==KEY_UP && map[player.y-1][player.x]!=1)
           player.y--;
        else if(move==KEY_DOWN && map[player.y+1][player.x]!=1)
           player.y++;
        else if(move==KEY_LEFT && map[player.y][player.x-1]!=1)
           player.x--;
        else if(move==KEY_RIGHT && map[player.y][player.x+1]!=1)
           player.x++;
        else if(move==KEY_ESC)
           quit=1;
    	return (quit);
    }
    Last edited by red_baron; 07-23-2002 at 08:01 PM.
    ¿Red Baron?

    "Imagination is more important than knowledge"
    -Albert Einstein (1879-1955)

    Check out my games!

    [code] /* dont forget code tags! */ [/code]

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    One more problem. In your code you have gotoxy(j,i) which on first loop produces gotoxy(0,0) while the first on screen coordinate is 1,1. The 0th elements do print it just that they aren't on screen. I was thinking of just ignoring the 0th elements. Do you think thats right? Besides all this I greatly appreciate you taking your time to help me.

  10. #10
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274
    sure i think i missed it didn't notice it started at 0 but go ahead and do that also the colission method you can add again i just had a problem while fixing your code so i added the function to key input but if you want you can put back the function again it willl work, anyway good luck with whatever your gonna do.
    ¿Red Baron?

    "Imagination is more important than knowledge"
    -Albert Einstein (1879-1955)

    Check out my games!

    [code] /* dont forget code tags! */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. flickering
    By ElastoManiac in forum Windows Programming
    Replies: 10
    Last Post: 12-23-2005, 08:26 AM
  2. graphics flickering problem
    By Benzakhar in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 02-23-2004, 05:15 PM
  3. TextOut() flickering
    By Xterria in forum Game Programming
    Replies: 6
    Last Post: 01-22-2004, 09:55 PM
  4. Argh! Flickering!!!
    By SMurf in forum Windows Programming
    Replies: 6
    Last Post: 03-03-2002, 10:43 AM
  5. Flickering
    By Visu A in forum C Programming
    Replies: 1
    Last Post: 02-27-2002, 12:48 AM