static void update(struct game_state *gs) { if (inner_state.state != IS_LEVEL_CLEARED && inner_state.state != IS_LEVEL_TRANSITION) update_eye(); update_crosshair(); update_background(); update_water(); update_particles(); update_explosions(); if (ship_is_visible()) { update_ship(); update_missiles(); update_bombs(); update_lasers(); update_foes(); } update_powerups(); update_in_game_texts(); if (inner_state.state == IS_IN_GAME) { update_multiplier(); update_combo(); } if (inner_state.state == IS_GAME_OVER) { if (inner_state.tics >= GAME_OVER_FADE_IN_TICS) { if (!update_stats_table()) { gc.score += game_over_stats.bonus; set_inner_state(IS_RANK); } } } if (inner_state.state == IS_LEVEL_CLEARED) { if (inner_state.tics >= LEVEL_CLEARED_FADE_IN_TICS) { if (!update_stats_table()) { gc.score += game_over_stats.bonus; set_inner_state(IS_LEVEL_TRANSITION); } } } update_level_tics(); update_inner_state(); if (serializing) serialize(); }
void loop(s4 &game_state) { ICameraSceneNode* camera = irrlicht->smgr->addCameraSceneNode(); matrix4 ortho; ortho.buildProjectionMatrixOrthoLH( irrlicht->driver->getScreenSize().Width/ortho_scale, irrlicht->driver->getScreenSize().Height/ortho_scale,-1.0,1000.0); camera->setProjectionMatrix(ortho); camera->setPosition({0,0,-100}); camera->setTarget({0,0,0}); irrlicht->hud->setActiveCamera(camera); IBillboardSceneNode* qnode = irrlicht->smgr->addBillboardSceneNode(); qnode->setMaterialFlag(EMF_WIREFRAME,true); // -------------------------------------- p("---- Game loop start ----"); d32 dt = 0.0f; const d32 maxDelta = 1.0f/60.0f * 5.0f; const d32 tick_ms = 0.01666f; // TODO: change this back to 30ms? d32 render_dt = 0.0f; const d32 render_ms = RENDER_TIME_STEP; uint32 time_physics_prev = btclock->getTimeMicroseconds(); while(irrlicht->device->run() && game_state == GAME_STATE_PLAY) { const uint32 time_physics_curr = btclock->getTimeMicroseconds(); const d32 frameTime = ((d32)(time_physics_curr - time_physics_prev)) / 1000000.0; // todo: is this truncated correctly? time_physics_prev = time_physics_curr; d32 capped_dt = frameTime; if (capped_dt > maxDelta) { capped_dt = maxDelta; } render_dt += capped_dt; if ( render_dt >= render_ms ) { render_dt = 0.0f; update_player(); update_enemies(); update_missiles(ply->missiles,ply->num_missile); irrlicht->hud->getRootSceneNode()->setPosition(camera->getPosition() + vector3df(0,0,100)); irrlicht->driver->beginScene(true, true, SColor(255,59,120,140)); irrlicht->smgr->drawAll(); irrlicht->driver->clearZBuffer(); irrlicht->hud->drawAll(); irrlicht->driver->endScene(); } dt += capped_dt; while( dt >= tick_ms ) { dt -= tick_ms; receiver->input(); clear_quads(quadtree); step_player(); step_enemies(); step_missiles(ply->missiles, ply->num_missile); QuadTree::Quad* fq = get_quad_from_pos(quadtree,ply->curr_pos); qnode->setSize(dimension2d<f32>(1,1)); qnode->setPosition(fq->pos.irr()); qnode->setSize(dimension2d<f32>(fq->width,fq->width)); if (receiver->debug_key.state) { for (s4 i = 0; i < fleet->num_squad; i++) { if (fleet->squads[i].mode != scatter) fleet->squads[i].mode = scatter; else fleet->squads[i].mode = to_positions; } } if (receiver->restart.state) { game_state = GAME_STATE_RESET; return; } if (receiver->QUIT) { game_state = GAME_STATE_QUIT; return; } empty_transient_soft(memory); } alpha = dt / tick_ms; core::stringw str = L"Coquelicot // score: "; str += score; str += L" // FPS: "; str += (s32)irrlicht->driver->getFPS(); irrlicht->device->setWindowCaption(str.c_str()); } }