This game I have was originally written as an experiment and used frame based movement and locked at 60fps. Now that the game actually has some favour and has evolved greatly since then, I changed the game's timing system completely since users could take advantage of intense scenes with low framerates. Everything that uses my frame based movement code works just as intended, except for one thing. NPCs spawning way too slow when framerates are significantly above 60fps! This doesn't make any sense, especially since everything else works.
The spawning timer is really simple, decrease the timer until it reaches 0, then reset it. When I was using frame based movement, I would simply decrement the timer by 1. The timer is typically set to 30 or 20, which means 2 or 3 NPCs are spawned every second (60 fps), so -1 every frame. For timer based updates, I subtract the delta time between each frame. This doesn't work, and the higher my framerates go, the slower the spawning speed.
This is the code I'm using:
Delta speed calculation.
static uint64_t last_time = 0;
uint64_t current_time = time_get_time(); //( get_current_time() * 1000.0f );
int fps_limit = 60;
//if( ( current_time - last_time ) < ( 1000 / fps_limit ) )
// return;
if( last_time != 0 )
This->m_delta_speed = float( current_time - last_time ) / ( 1000.0f / 60.0f );
last_time = current_time;
NPC spawning code
if( !m_gameover )
{
update_user();
m_spawn_timer -= m_delta_speed;
if (m_spawn_timer < 0)
{
add_green_square();
m_spawn_speed = (This->get_difficulty() ? 30.0f : 20.0f);
m_spawn_timer = m_spawn_speed;
}
}
Once again, the timing code for calculating the delta time works fine for everything else (or at least it looks fine), but the above code used to spawn enemies doesn't work properly above 60fps. Any ideas? Thanks.
Shogun.