Skip to content

Frame Skipping

Zal0 edited this page Sep 13, 2021 · 4 revisions

If you add too many enemies on your map you might notice a big slowdown that at some points can even make the game unplayable. This can be improved a little bit using the var delta_time that:

  • Will be 0 when the frame rate is ~60fps
  • Will be 1 otherwise

You can use this var to multiply the current amount of movement of your sprites (x << 0 is the same as multiplying by 1 and x << 1 is the same as multiplying by 2). This is how your update method should look like on your SpritePlayer.c

void UPDATE() {
	...
	if(KEY_PRESSED(J_UP)) {
		TranslateSprite(THIS, 0, -1 << delta_time);
		SetSpriteAnim(THIS, anim_walk, 15);
	} 
	if(KEY_PRESSED(J_DOWN)) {
		TranslateSprite(THIS, 0, 1 << delta_time);
		SetSpriteAnim(THIS, anim_walk, 15);
	}
	if(KEY_PRESSED(J_LEFT)) {
		TranslateSprite(THIS, -1 << delta_time, 0);
		SetSpriteAnim(THIS, anim_walk, 15);
	}
	if(KEY_PRESSED(J_RIGHT)) {
		TranslateSprite(THIS, 1 << delta_time, 0);
		SetSpriteAnim(THIS, anim_walk, 15);
	}
	...
}

and here is the same change on SpriteEnemy.c

void UPDATE() {
	struct EnemyInfo* data = (struct EnemyInfo*)THIS->custom_data;
	if(TranslateSprite(THIS, 0, data->vy << delta_time)) {
		data->vy = -data->vy;
	}
}

For more info about frame skipping check this post on my blog Game Boy Development Tips and Tricks (II)

Clone this wiki locally