// Regulating game speed #include #include #include #include #include "0943.h" #include "0943i.h" int actual_cycle; int end_game; int pixels; fixed circle_pos; fixed add_pos; BITMAP *dblbuffer; void game_loop(void) { do { get_keyboard_input(); draw_one_frame(); /* First do some graphics, */ while (target_cycle > actual_cycle) /* then until up-to-date... */ do_one_game_cycle(); /* ... do game cycles. */ } while (!end_game); } int main(void) { init_example(); game_loop(); shutdown(); return 0; } END_OF_MAIN() void draw_one_frame(void) { int f; clear(dblbuffer); for (f = 0; f < pixels; f++) dblbuffer->line[rand() % SCREEN_H][rand() % SCREEN_W] = rand() % 256; circlefill(dblbuffer, fixtoi(circle_pos), SCREEN_H / 2, 10, 13); textout(dblbuffer, font, "Press ESC to exit", 0, 0, 15); textout(dblbuffer, font, "+/- to increase/decrease pixels", 0, 10, 15); textprintf(dblbuffer, font, 0, 20, 15, "Number of pixels: %d", pixels); textprintf(dblbuffer, font, 0, 30, 15, "Frames per second: %d", last_fps); vsync(); blit(dblbuffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); frame_counter++; } void do_one_game_cycle(void) { circle_pos += add_pos; if (fixtoi(circle_pos) < 10 || fixtoi(circle_pos) > 310) add_pos *= -1; actual_cycle++; } void get_keyboard_input(void) { if (key[KEY_ESC]) end_game++; if (key[KEY_PLUS_PAD]) pixels += 100; if (key[KEY_MINUS_PAD] && pixels > 100) pixels -= 100; }