static gboolean new_game (gui_world *world) { cpFloat time_step = 1.0/60.0; world_free (world->physics); world->mass = 1; //world->physics = (world_status *)malloc(sizeof(world_status)); world->physics = world_new(world->level, time_step); world->graphics -> space = world->physics -> space; if (world->physics->drawing_box) { world->graphics->x1 = world->physics->drawing_box_x1; world->graphics->y1 = world->physics->drawing_box_y1; world->graphics->x2 = world->physics->drawing_box_x2; world->graphics->y2 = world->physics->drawing_box_y2; world->graphics->display = true; } else { world->graphics->display = false; } world->try_number = 1; return false; }
static void user_clean(User *user) { int j; noisy_lock(&user->mutex, user->name); slog("User '%s' cleaning up.", user->name); for (j = 0; j < MAX_USER_EVENTS; j++) { if (user->events[j]) { event_free(user->events[j]); } } for (j = 0; j < MAX_USER_WORLDS; j++) { if (user->worlds[j].name[0]) { world_free(&user->worlds[j], 0); } } noisy_unlock(&user->mutex, user->name); pthread_cond_broadcast(&(user->evtAlarm)); pthread_cond_destroy(&(user->evtAlarm)); pthread_mutex_destroy(&(user->mutex)); }
void test() { printf("Generating map.\n"); map* m = generate_random_map(10, 10, 1, NULL, NULL, NULL); world* w = world_new(m); w->f = fauna_generate(m, 1); char* output; // set random statuses for ( fauna* f = w->f; f != NULL; f = f->next ) { f->ws = wander_status_random(); } // run at 1hz do { output = world_render(w); printf("\e[1;1H\e[2J"); puts(output); printf("\n"); for ( fauna* f = w->f; f != NULL; f = f->next ) { wander_basic(w->m, f->ws, f); } sleep(1); } while ( true ); free(output); printf("Freeing world.\n"); world_free(w); }
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 destroy() { world_free(); }