How to make frame skiping in Turbo Basic?

Page 2/2
1 |

By ARTRAG

Enlighted (6923)

ARTRAG's picture

01-02-2015, 10:43

Anyway I must add that using Oscar's solution you become almost independent by the cpu speed.
This means that turbo machines could go 60 fps and standard z80 would go 30 fps.
This is how modern PC games work

By AxelStone

Prophet (3189)

AxelStone's picture

01-02-2015, 11:28

ARTRAG wrote:

Anyway I must add that using Oscar's solution you become almost independent by the cpu speed.
This means that turbo machines could go 60 fps and standard z80 would go 30 fps.
This is how modern PC games work

A very interesting question Smile . However for my first project I'm making and standar MSX2 game, I'll not use extra speed of Z80B or R800 Wink

By ARTRAG

Enlighted (6923)

ARTRAG's picture

01-02-2015, 11:50

I used the KAI's variable approach in the maze3d demo in order to get a constant speed of the enemy robot independently by the frame rate (than can vary from about 10 fps to 50 fps according to the scene).
You find the binaries and code here and the video here.

The key passages are in NPCAI.C. Here follow the most valuable lines:

/*----------------------------------------------------------------------------------*\
| Name: GetKoef
| Desc: Get the amount to move at the corrent frame
\*----------------------------------------------------------------------------------*/
#define UNIT_GetKoef() ((timeGetTime-UNIT_CTick));
/*----------------------------------------------------------------------------------*\
| Name: UpdateMove
| Desc: Update the move for the frame
\*----------------------------------------------------------------------------------*/
bool UNIT_UpdateMove() {
	uint Koef;
	if (UNIT_CTick==timeGetTime) return false;	// wait
	
	if (UNIT_IsAtNode()) UNIT_NewMove();

	if (UNIT_cPath>UNIT_nPath-1) return true; // End of the path
	
	Koef = UNIT_GetKoef();
	UNIT_PosX += (int)(UNIT_MoveX*Koef);
	UNIT_PosY += (int)(UNIT_MoveY*Koef);
	UNIT_CTick = timeGetTime;
	
	return false;
}

Koef = (timeGetTime-UNIT_CTick) is the difference between external timer and the frame count of the enemy robot.

The X position of the enemy is updated by:
UNIT_PosX += (int)(UNIT_MoveX*Koef);
where UNIT_MoveX is the X speed (defined elsewhere accordingly to a precomputed path).

If you call UNIT_UpdateMove once per frame, Koef = 1, so you move at normal speed, but if you call UNIT_UpdateMove once each two frames, Koef = 2, so you move at double speed and if you call it once each 3 frames, you move 3 times faster and so on.

As Koef is computed at the time the UNIT_UpdateMove function is called, the speed varies according to the "delay" between external counter and frame update. The more the CPU is late, the bigger will be the step of the enemy in this loop iteration.

I hope this helps to do CPU independent games
;-)

By AxelStone

Prophet (3189)

AxelStone's picture

03-02-2015, 20:16

I have finally used the sollution based in TIME, since I want to fix the game speed at 30fps and it'll run on normal MSX2.

100 TIME=0
...
1000 IF TIME<2 THEN GOTO 1000 ELSE GOTO 100

It works fine.

By gdx

Enlighted (6071)

gdx's picture

04-02-2015, 02:25

You can remove GOTOs.

100 TIME=0
...
1000 IF TIME<2 THEN 1000 ELSE 100

By AxelStone

Prophet (3189)

AxelStone's picture

06-02-2015, 20:56

gdx wrote:

You can remove GOTOs.

100 TIME=0
...
1000 IF TIME<2 THEN 1000 ELSE 100

I didn't know it, I'm going to save memory removing all GOTOs in my code. Thanks!

Page 2/2
1 |