3D Game Builder SDK

SDK Functions

gameTick( void )

Description

Get a value that is in sync with time.

Call this function once per frame only.

Specification

float gameTick( void )

return value

= a value in sync with time

Notes

This function allows you to sync your game with time.

Not all computers run at the same speed. As a result, your game may run faster on a faster computer, and slower on a slower computer.

For example, if you have a ball bouncing around the screen, it may appear faster or slower depending on the performance of the host computer.

You can ensure that the ball always appears to move at the same speed if you multiply the movement by a value in sync with time during each frame.

Call this function once per frame only.

For Example

#include "3dgamebuilder.h"


// draw ball
void drawBall( float x, float y, float z, float width, float height )
{ 
	float	w2, h2;

	// get half size so we can draw ball from center
	w2 = width  * 0.5f;
	h2 = height * 0.5f;

	// copy matrix
	glPushMatrix();
	
		// move into position
		glTranslatef( x, y, z );
	
		// draw ball (as a quad)
		glBegin( GL_TRIANGLE_STRIP );
			glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
			glVertex2f( -w2, -h2 );
			glVertex2f( -w2,  h2 );
			glVertex2f(  w2, -h2 );
			glVertex2f(  w2,  h2 );
		glEnd();

	// done with matrix
	glPopMatrix();
}


// exported game function
DLL_EXPORT int gameLoop(void)
{
	float posx  = 0.0f;
	float posy  = 0.0f;

	float stepx = 0.03f;
	float stepy = 0.02f;

	float tick;

	// reset
	gameTick();

	// game loop
	while(1)
	{
		// read the controller
		gamePadRead();

		// start drawing frame
		gameStartRender();
	
		// reset matrix
		glMatrixMode( GL_PROJECTION );
		glLoadIdentity();
		
		// reset matrix
		glMatrixMode( GL_MODELVIEW );
		glLoadIdentity();

		// draw ball
		drawBall( posx, posy, 0.0f, 0.03f, 0.1f );

		// move ball at a speed associated with time
		tick = gameTick() * 30.0f;
		posx += stepx * tick;
		posy += stepy * tick;

		// bounce off right side
		if (posx > 0.985f) {
			posx = 0.985f - (posx - 0.985f);
			stepx = -(stepx);
			gameMessage( "bounce off right side" );
		}

		// bounce off left side
		if (posx < -0.985f) {
			posx = -0.985f - (posx + 0.985f);
			stepx = -(stepx);
			gameMessage( "bounce off left side" );
		}

		// bounce off top side
		if (posy > 0.95f) {
			posy = 0.95f - (posy - 0.95f);
			stepy = -(stepy);
			gameMessage( "bounce off top side" );
		}

		// bounce off bottom side
		if (posy < -0.95f) {
			posy = -0.95f - (posy + 0.95f);
			stepy = -(stepy);
			gameMessage( "bounce off bottom side" );
		}

		// end of drawing
		gameEndRender();
	}

	return 0;
}

See Also

(no related functions)

Go Back

SDK Functions