int wt_input(void) { int quit = False; double sin_facing, cos_facing; intent = read_input_devices(); /* This block code is a hack to do acceleration and deceleration. */ if (fabs(vx) > fabs(intent->force_x)) { if (vx < 0.0) vx = MIN(vx + 0.1, intent->force_x); else vx = MAX(vx - 0.1, intent->force_x); } else if (fabs(vx) < fabs(intent->force_x)) { vx += intent->force_x / 5.0; if (fabs(vx) > fabs(intent->force_x)) vx = intent->force_x; } if (fabs(vy) > fabs(intent->force_y)) { if (vy < 0.0) vy = MIN(vy + 0.1, intent->force_y); else vy = MAX(vy - 0.1, intent->force_y); } else if (fabs(vy) < fabs(intent->force_y)) { vy += intent->force_y / 5.0; if (fabs(vy) > fabs(intent->force_y)) vy = intent->force_y; } if (fabs(vy) > fabs(intent->force_y)) { if (vy < 0.0) vy = MIN(vy + 0.1, intent->force_y); else vy = MAX(vy - 0.1, intent->force_y); } else if (fabs(vy) < fabs(intent->force_y)) { vy += intent->force_y / 5.0; if (fabs(vy) > fabs(intent->force_y)) vy = intent->force_y; } /* Angular deceleration here is weird and unrealistic, but it feels ** right to me. */ if (fabs(va) > fabs(intent->force_rotate)) va *= 0.6; else if (fabs(va) < fabs(intent->force_rotate)) { va += intent->force_rotate / 8.0; if (fabs(va) > fabs(intent->force_rotate)) va = intent->force_rotate; } view->angle += FLOAT_TO_FIXED(0.3 * va); sin_facing = sin(FIXED_TO_FLOAT(view->angle)); cos_facing = cos(FIXED_TO_FLOAT(view->angle)); view->x += FLOAT_TO_FIXED(0.8 * vx * cos_facing); view->y += FLOAT_TO_FIXED(0.8 * vx * sin_facing); view->x += FLOAT_TO_FIXED(0.8 * vy * -sin_facing); view->y += FLOAT_TO_FIXED(0.8 * vy * cos_facing); if (view->height > FIXED_ONE) v -= FIXED_ONE / 16; view->height += v; if (view->height < FIXED_ONE) { v = FIXED_ZERO; view->height = FIXED_ONE; } while (intent->n_special--) { if (intent->special[intent->n_special] == INTENT_END_GAME) quit = True; else v = FIXED_ONE / 2; } return quit; }
int main(int argc, char *argv[]) { World *w; FILE *fp; Boolean quit = False; Intent *intent; fixed v = FIXED_ZERO; double vx = 0.0, vy = 0.0, va = 0.0; #ifdef MSDOS long frames; time_t starttime, endtime; #endif if (argc != 2) { fprintf(stderr, "Usage: wt <world file>\n"); exit(EXIT_FAILURE); } if ((fp = fopen(argv[1], "r")) == NULL) { perror(argv[1]); exit(EXIT_FAILURE); } w = read_world_file(fp); fclose(fp); init_graphics(); init_renderer(SCREEN_WIDTH, SCREEN_HEIGHT); init_input_devices(); /* setup view */ view = new_view(fixdiv(FIXED_2PI, INT_TO_FIXED(4))); view->x = FIXED_ZERO; view->y = FIXED_ZERO; view->height = FIXED_ONE; view->angle = FIXED_ZERO; #ifdef MSDOS starttime = time(NULL); frames = 0; #endif while (!quit) { double sin_facing, cos_facing; render(w, view); #ifdef MSDOS frames++; #endif intent = read_input_devices(); /* This block code is a hack to do acceleration and deceleration. */ if (fabs(vx) > fabs(intent->force_x)) { if (vx < 0.0) vx = MIN(vx + 0.1, intent->force_x); else vx = MAX(vx - 0.1, intent->force_x); } else if (fabs(vx) < fabs(intent->force_x)) { vx += intent->force_x / 5.0; if (fabs(vx) > fabs(intent->force_x)) vx = intent->force_x; } if (fabs(vy) > fabs(intent->force_y)) { if (vy < 0.0) vy = MIN(vy + 0.1, intent->force_y); else vy = MAX(vy - 0.1, intent->force_y); } else if (fabs(vy) < fabs(intent->force_y)) { vy += intent->force_y / 5.0; if (fabs(vy) > fabs(intent->force_y)) vy = intent->force_y; } if (fabs(vy) > fabs(intent->force_y)) { if (vy < 0.0) vy = MIN(vy + 0.1, intent->force_y); else vy = MAX(vy - 0.1, intent->force_y); } else if (fabs(vy) < fabs(intent->force_y)) { vy += intent->force_y / 5.0; if (fabs(vy) > fabs(intent->force_y)) vy = intent->force_y; } /* Angular deceleration here is weird and unrealistic, but it feels ** right to me. */ if (fabs(va) > fabs(intent->force_rotate)) va *= 0.6; else if (fabs(va) < fabs(intent->force_rotate)) { va += intent->force_rotate / 8.0; if (fabs(va) > fabs(intent->force_rotate)) va = intent->force_rotate; } view->angle += FLOAT_TO_FIXED(0.3 * va); sin_facing = sin(FIXED_TO_FLOAT(view->angle)); cos_facing = cos(FIXED_TO_FLOAT(view->angle)); view->x += FLOAT_TO_FIXED(0.8 * vx * cos_facing); view->y += FLOAT_TO_FIXED(0.8 * vx * sin_facing); view->x += FLOAT_TO_FIXED(0.8 * vy * -sin_facing); view->y += FLOAT_TO_FIXED(0.8 * vy * cos_facing); if (view->height > FIXED_ONE) v -= FIXED_ONE / 16; view->height += v; if (view->height < FIXED_ONE) { v = FIXED_ZERO; view->height = FIXED_ONE; } while (intent->n_special--) { if (intent->special[intent->n_special] == INTENT_END_GAME) quit = True; else v = FIXED_ONE / 2; } } #ifdef MSDOS endtime = time(NULL); #endif end_input_devices(); end_graphics(); #ifdef MSDOS printf("%li frames in %lu seconds - %.2f frames per second", (long) frames, (long) endtime - starttime, (float) frames / (float) (endtime-starttime)); #endif return EXIT_SUCCESS; }
int main(int argc, char *argv[]) { World *w; View *view; Intent *intent; Graphics_info *ginfo; Tcl_Interp *interp; Object *me; Boolean quit = False; #if PROFILE int fps_count = 0; double total_time = 0.0; double start_time; #endif interp = Tcl_CreateInterp(); updater = updater_create(interp); if (argc != 2) { fprintf(stderr, "Usage: %s <world file>\n", argv[0]); exit(EXIT_FAILURE); } ginfo = init_graphics(); #define TRUECOLOR #ifdef TRUECOLOR set_texture_trans(32, ginfo->palette.color_lookup); #else set_texture_trans(ginfo->palette.rgb_cube_size, ginfo->palette.color_lookup); #endif init_renderer(ginfo->width, ginfo->height); init_input_devices(); /* Initialize the view */ view = new_view(FLOAT_TO_FIXED(3.14159265 / 2.0)); w = new_world(); parser_init(interp, w); if (Tcl_EvalFile(interp, WT_INIT_FILENAME) != TCL_OK) { fprintf(stderr, "%s\n", Tcl_GetVar(interp, "errorCode", 0)); fprintf(stderr, "%s\n", Tcl_GetVar(interp, "errorInfo", 0)); exit(1); } if (Tcl_EvalFile(interp, argv[1]) != TCL_OK) { fprintf(stderr, "%s\n", Tcl_GetVar(interp, "errorCode", 0)); fprintf(stderr, "%s\n", Tcl_GetVar(interp, "errorInfo", 0)); exit(1); } while (!quit) { double sin_facing, cos_facing; double fx, fy, fz, torque; Framebuffer *fb; int i; start_time = current_time(); intent = read_input_devices(); me = get_controlled_object(); if (me == NULL) fatal_error("No controlled object"); for (i = 0; i < intent->n_special; i++) { switch (intent->special[i]) { case INTENT_END_GAME: quit = True; (void) Tcl_Eval(interp, "action_quit"); break; case INTENT_JUMP: (void) Tcl_Eval(interp, "action_jump"); break; case INTENT_ACTION1: (void) Tcl_Eval(interp, "action_1"); break; case INTENT_ACTION2: (void) Tcl_Eval(interp, "action_2"); break; case INTENT_ACTION3: (void) Tcl_Eval(interp, "action_3"); break; case INTENT_ACTION4: (void) (Tcl_Eval(interp, "action_4") != TCL_OK); break; case INTENT_ACTION5: (void) Tcl_Eval(interp, "action_5"); break; default: break; } } /* Determine forces on viewer. */ sin_facing = sin(me->angle); cos_facing = cos(me->angle); fx = cos_facing * intent->force_x - sin_facing * intent->force_y; fy = sin_facing * intent->force_x + cos_facing * intent->force_y; fz = -0.05 * me->mass; /* gravity */ torque = intent->force_rotate; /* Apply the forces. */ object_apply_force(me, fx, fy, fz); object_apply_torque(me, torque); object_update(me); if (me->z <= 0.0 && me->dz <= 0.0) { me->z = 0.0; me->dz = 0.0; } /* Determine the view. */ me = get_viewpoint_object(); if (me == NULL) fatal_error("No viewpoint object"); object_view(me, view); updater_run(updater); /* Display the world. */ fb = render(w, view); update_screen(fb); #if PROFILE fps_count++; total_time += current_time() - start_time; if (fps_count == 100) { printf("fps = %3.2f\n", (double) fps_count / total_time); fps_count = 0; total_time = 0.0; } #endif } end_input_devices(); end_graphics(); return EXIT_SUCCESS; }