Problem with C++ tutorial 13 Data Structures

529d9efcdb6b447120c5277f52e4384f
0
Rimevan 101 Feb 20, 2012 at 14:07

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

4 Replies

Please log in or register to post a reply.

A1b52ffd884d07e67d76399b59b6cf29
0
jari 101 Feb 20, 2012 at 15:20

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.

529d9efcdb6b447120c5277f52e4384f
0
Rimevan 101 Feb 20, 2012 at 15:32

@jari

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

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 168 Feb 20, 2012 at 17:13

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.

529d9efcdb6b447120c5277f52e4384f
0
Rimevan 101 Feb 20, 2012 at 20:16

@Reedbeta

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 the head every step
-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:

09408619.png

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