# Problem with C++ tutorial 13 Data Structures

4 replies to this topic

### #1Rimevan

Member

• Members
• 11 posts

Posted 20 February 2012 - 02:07 PM

Unfortunately, another problem popped up in the next tutorial..If I just use this simple code it should draw a snake, consisting of
8 white squares. This code does draw the snake, though it is flickering. This flickering causes problem in another code I want to
use, because it looks like it is skipping 1 step every 2 steps. Does anyone know what causes this?

// Template, major revision 3
// IGAD/NHTV - Jacco Bikker - 2006-2009

#include "string.h"
#include "surface.h"
#include "stdlib.h"
#include "template.h"
#include "game.h"

using namespace Tmpl8;

int x[8] = { 5, 6, 7, 8, 9, 10, 11, 12 };
int y[8] = { 8, 8, 8, 8, 8, 8, 8, 8 };
int heading = 0, tail = 0;

void Game::Init()
{
for ( int i = 0; i < 8; i++ )
{
m_Screen->Box( x[i] * 8, y[i] * 8, x[i] * 8 + 6, y[i] * 8 + 6, 0xffffff );
}

}

void Game::Tick( float a_DT )
{

}


Rimevan

### #2jari

New Member

• Members
• 18 posts

Posted 20 February 2012 - 03:20 PM

If you want to draw something on the screen then you should put draw code in the Game::Tick function. This function is called every frame, while the Game::Init just once, after application start.

### #3Rimevan

Member

• Members
• 11 posts

Posted 20 February 2012 - 03:32 PM

jari, on 20 February 2012 - 03:20 PM, said:

If you want to draw something on the screen then you should put draw code in the Game::Tick function. This function is called every frame, while the Game::Init just once, after application start.

I know it is only called at the application start, but I thought this shouldn't be a problem as long as I don't refresh the screen?

Rimevan

### #4Reedbeta

DevMaster Staff

• 5308 posts
• LocationSanta Clara, CA

Posted 20 February 2012 - 05:13 PM

The application is probably double-buffered, and since you're only drawing your snake once, it only gets into one of the two buffers, causing the flickering. I assume you'll eventually want your snake to be dynamic anyway, so best to just go ahead and draw it each frame.
reedbeta.com - developer blog, OpenGL demos, and other projects

### #5Rimevan

Member

• Members
• 11 posts

Posted 20 February 2012 - 08:16 PM

Reedbeta, on 20 February 2012 - 05:13 PM, said:

The application is probably double-buffered, and since you're only drawing your snake once, it only gets into one of the two buffers, causing the flickering. I assume you'll eventually want your snake to be dynamic anyway, so best to just go ahead and draw it each frame.

It needs to be dynamic indeed, but the purpose of this tutorial is to minimalize the amount of drawing. What I wanted to do is as follows:
-draw the whole snake at the start
-draw a black box at the tail every step( so it isn't visible anymore )

I don't exactly know what that 'double-buffered'-thing is, but it causes a problem with the drawing. Because of its flickering, every 2 steps one tail won't
be drawn black and the head isn't drawn. This is what I see:

The right part is moving, but the left part is staying where it is and it's flickering. The code I use is this:
// Template, major revision 3
// IGAD/NHTV - Jacco Bikker - 2006-2009

#include "string.h"
#include "surface.h"
#include "stdlib.h"
#include "template.h"
#include "game.h"

using namespace Tmpl8;

int x[8] = { 5, 6, 7, 8, 9, 10, 11, 12 };
int y[8] = { 8, 8, 8, 8, 8, 8, 8, 8 };
int heading = 0, tail = 0;

void Game::Init()
{
for ( int i = 0; i < 8; i++ )
{
m_Screen->Box( x[i] * 8, y[i] * 8, x[i] * 8 + 6, y[i] * 8 + 6, 0xffffff );
start = false;
}
}

void Game::Tick( float a_DT )
{
m_Screen->Box( x[tail] * 8, y[tail] * 8, x[tail] * 8 + 6, y[tail] * 8 + 6, 0x000000 );
x[tail] = x[7];
y[tail] = y[7];
tail = ( tail + 1 ) % 7;
if ( heading == 0 ) x[7]++;
if ( heading == 1 ) y[7]++;
if ( heading == 2 ) x[7]--;
if ( heading == 3 ) y[7]--;
m_Screen->Box( x[7] * 8, y[7] * 8, x[7] * 8 + 6, y[7] * 8 + 6, 0xffffff );
if ( GetAsyncKeyState( VK_LEFT ) ) heading = ( heading + 3 ) % 4;
if ( GetAsyncKeyState( VK_RIGHT ) ) heading = ( heading + 1 ) % 4;

Sleep( 100 );

}


Rimevan

#### 1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users