// int main(int argc, char *argv[]) { int main(void) { const char *title = "Small Size League Simulator by RoboIME"; puts(title); { // Handle SIGINT (Ctrl+C) struct sigaction sa; sa.sa_handler = sigint_handler; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sigaction(SIGINT, &sa, NULL); } // Create a world struct World *world = new_world(&FIELD_2015); // Add some robots for (int i = 0; i < 6; i++) { world_add_robot(world, i, TEAM_BLUE); world_add_robot(world, i, TEAM_YELLOW); } // Create and bind socket struct Socket *socket = new_socket(11002, "224.5.23.2"); socket_sender_bind(socket); #define BUFFER_SIZE 10240 char buffer[BUFFER_SIZE]; int send_size; int send_geometry = 0; while (keep_going) { world_step(world, 1.0 / 60, 10, 1.0 / 600); // TODO: log errors send_size = serialize_world(world, buffer, BUFFER_SIZE); if (send_size > 0) socket_send(socket, buffer, send_size); if (send_geometry++ % 120 == 0) { send_size = serialize_field(world_get_field(world), buffer, BUFFER_SIZE); if (send_size > 0) socket_send(socket, buffer, send_size); } // TODO: realtime update // sleep for 16ms, prevents going too fast and intensive nanosleep(&(struct timespec){.tv_nsec=16000000}, NULL); }
void run_game(lua_State *L) { uint32_t now = SDL_GetTicks(); /* Current real time. */ /* * Compute how much time has passed since last time. Watch out for time * wrap-around. */ uint32_t delta_time = (now >= before) ? now - before : (uint32_t)-1 - before + now; before = now; /* * If there was some huge lag, don't make worlds catch * up. Instead, assume last frame took 50ms. */ if (delta_time > 50) delta_time = 50; /* Game speed always normal. */ uint32_t game_delta_time = delta_time; game_time += game_delta_time; /* Advance game time. */ /* Calculate frames per second. */ fps_count++; if (now - fps_time >= config.FPSUpdateInterval && config.debug) { frames_per_second = fps_count*1000.0 / (now - fps_time); fps_time = now; fps_count = 0; extern int total_tile_count; log_msg("FPS: %.2f, tiles=%d", frames_per_second, total_tile_count); } process_events(L); #if ENABLE_AUDIO /* Dynamically adjust volume. */ audio_adjust_volume(); #endif /* Step worlds. */ extern mem_pool mp_world; for (World *world = mp_first(&mp_world); world != NULL; world = mp_next(world)) { if (world->killme) continue; /* We deal with these below. */ /* Bring world up to present game time. */ while (game_time >= world->next_step_time) { world->next_step_time += world->step_ms; /* * Step world -- execute body step functions, timers, * collision handlers. */ world_step(world, L); /* * Handle user input. To be more responsive, we do this * here between steps too. */ process_events(L); if (world->killme) break; /* No need to keep going. */ } } /* * Deal with worlds that have either been destroyed or created in the * loop above. Must do this here so scripts get a chance to set * everything up before a frame is rendered. */ for (World *world = mp_first(&mp_world); world != NULL;) { if (world->killme) { /* * Remove dying worlds. Take care to get next world * pointer before destruction. */ World *tmp = world; world = mp_next(world); world_free(tmp); continue; } /* Perform the initial step on recently created worlds. */ if (world->static_body.step == 0) { /* * We must give scripts control over what the contents * of the world look like before drawing it. Otherwise * we get such artifacts as camera centered on origin * even though it should be tracking a player character. */ world_step(world, L); } world = mp_next(world); } /* * Draw what each camera sees. */ render_clear(); for (Camera *cam = cam_list; cam != NULL; cam = cam->next) { if (!cam->disabled) render(cam); } #ifndef NDEBUG /* Debug stuff is drawn over normal stuff. */ if (debug_cam != NULL && debug_cam->objtype == OBJTYPE_CAMERA) render_debug(debug_cam); #endif }
void start_game(game *g) { _world_vertices(g); _setup_world(g); _reset_camera(g); _setup_camera(g); _update_camera(g); // Start game Uint32 start_loop = SDL_GetTicks(); Uint32 last_ticks = SDL_GetTicks(); SDL_Delay(1); Uint32 cur_ticks = SDL_GetTicks(); SDL_Event e; size_t count = 0; while (g->state != ENDED) { _render_world(g); // Get time since last frame (ms) last_ticks = cur_ticks; cur_ticks = SDL_GetTicks(); if (g->o.enabled) { // Calculate average and instant FPS if (g->o.fps_upd & 1) { g->avg_fps = count / ((cur_ticks - start_loop) / 1000.f); g->fps = 1000.0 / (cur_ticks - last_ticks); g->o.fps_upd = 0; } // Check if we should render FPS (only 2 times per second) if ((cur_ticks / 100) % 5 == 0) { g->o.fps_upd = 2; } else { g->o.fps_upd >>= 1; } _render_overlay(g); } SDL_GL_SwapWindow(g->win); // Events while (SDL_PollEvent(&e)) { _handle_event(g, e); } // Update camera _update_camera(g); // Update the world ++count; if (g->state == RUNNING) { switch (g->step) { case(WHOLE): world_step(g->w); break; case(HALF): world_half_step(g->w); break; } } _update_world_buffer(g); } }