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
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
. However for my first project I'm making and standar MSX2 game, I'll not use extra speed of Z80B or R800
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
;-)
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.
You can remove GOTOs.
100 TIME=0 ... 1000 IF TIME<2 THEN 1000 ELSE 100
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!
