aboutsummaryrefslogtreecommitdiff
path: root/src/pong.c
diff options
context:
space:
mode:
authorAtsutane <atsutane@freethoughts.de>2009-05-07 10:54:35 +0200
committerAtsutane <atsutane@freethoughts.de>2009-05-07 10:54:35 +0200
commitf17d67390ab9ab69d83a7be1102fc46e91afe6f5 (patch)
tree7b3ce4860b486989c08443c2aab98135f1ea1156 /src/pong.c
parent76fd2acbbd5558d9222f8f860b3a045370494f30 (diff)
downloadpong-f17d67390ab9ab69d83a7be1102fc46e91afe6f5.tar.gz
pong-f17d67390ab9ab69d83a7be1102fc46e91afe6f5.tar.bz2
game(): Improved performance by switching to usleep() instead of using clock() and CLOCKS_PER_SEC.
Diffstat (limited to 'src/pong.c')
-rw-r--r--src/pong.c102
1 files changed, 49 insertions, 53 deletions
diff --git a/src/pong.c b/src/pong.c
index b462e2c..9ce24a3 100644
--- a/src/pong.c
+++ b/src/pong.c
@@ -30,7 +30,9 @@
#include <stdlib.h>
#include <time.h>
#include <ncurses.h>
+#include <unistd.h>
+#define GAME_LOOP_DIVISOR 12
struct player_data {
unsigned int x;
@@ -356,8 +358,6 @@ void p2_ai(struct game_data *gd) {
* the ncurses handling in the main function.
*/
int game(void) {
- clock_t clocks, /* contains clock since the start of the program */
- interval;
int p, /* Time for player input y/n? */
player_input,
r; /* return value */
@@ -371,9 +371,6 @@ int game(void) {
}
p=0;
- clocks = clock();
- interval = CLOCKS_PER_SEC/12;
-
gd->ball->mv_right = TRUE;
ball_movement(gd);
@@ -383,62 +380,61 @@ int game(void) {
while ((gd->p1->score != 21) &&
(gd->p2->score != 21)) {
- if (clock() > clocks + interval) {
- if (p == 4) {
- /* Input Handling for P1 goes here.
- * I don't have the knowledge for
- * a well working one yet, but it
- * should be relatively easy.
- * man 3 ncurses
- * man 3 curs_getch
- * man 3 curs_inopts
- */
- mvaddch(gd->p1->y-2, 0, ' ');
- mvaddch(gd->p1->y-1, 0, ' ');
- mvaddch(gd->p1->y, 0, ' ');
- mvaddch(gd->p1->y+1, 0, ' ');
- mvaddch(gd->p1->y+2, 0, ' ');
+ if (p == 4) {
- /* Correct the position if the size of
- * the terminal was changed.
- */
- while((gd->p1->y+2) > (gd->max_field_y-1)) {
- gd->p1->y--;
- }
+ /* Input Handling for P1 goes here.
+ * I don't have the knowledge for
+ * a well working one yet, but it
+ * should be relatively easy.
+ * man 3 ncurses
+ * man 3 curs_getch
+ * man 3 curs_inopts
+ */
+ mvaddch(gd->p1->y-2, 0, ' ');
+ mvaddch(gd->p1->y-1, 0, ' ');
+ mvaddch(gd->p1->y, 0, ' ');
+ mvaddch(gd->p1->y+1, 0, ' ');
+ mvaddch(gd->p1->y+2, 0, ' ');
- player_input = getch();
- if ((player_input == KEY_UP) &&
- ((gd->p1->y-2) > 0)) {
- gd->p1->y--;
- }
- else if ((player_input == KEY_DOWN) &&
- ((gd->p1->y+2) < (gd->max_field_y-1))) {
- gd->p1->y++;
- }
-
- mvaddch(gd->p1->y-2, 0, '|');
- mvaddch(gd->p1->y-1, 0, '|');
- mvaddch(gd->p1->y, 0, '|');
- mvaddch(gd->p1->y+1, 0, '|');
- mvaddch(gd->p1->y+2, 0, '|');
-
- p2_ai(gd);
- p=0;
+ /* Correct the position if the size of
+ * the terminal was changed.
+ */
+ while((gd->p1->y+2) > (gd->max_field_y-1)) {
+ gd->p1->y--;
}
- ball_movement(gd);
- if ((gd->ball->mv_left == FALSE) &&
- (gd->ball->mv_right == FALSE)) {
- ball_launch(gd);
+ player_input = getch();
+ if ((player_input == KEY_UP) &&
+ ((gd->p1->y-2) > 0)) {
+ gd->p1->y--;
}
- check_field_size(gd);
- draw_statusbar(gd);
+ else if ((player_input == KEY_DOWN) &&
+ ((gd->p1->y+2) < (gd->max_field_y-1))) {
+ gd->p1->y++;
+ }
+
+ mvaddch(gd->p1->y-2, 0, '|');
+ mvaddch(gd->p1->y-1, 0, '|');
+ mvaddch(gd->p1->y, 0, '|');
+ mvaddch(gd->p1->y+1, 0, '|');
+ mvaddch(gd->p1->y+2, 0, '|');
- p++;
- clocks = clock();
- refresh();
+ p2_ai(gd);
+ p=0;
}
+
+ ball_movement(gd);
+ if ((gd->ball->mv_left == FALSE) &&
+ (gd->ball->mv_right == FALSE)) {
+ ball_launch(gd);
+ }
+ check_field_size(gd);
+ draw_statusbar(gd);
+
+ p++;
+ refresh();
+ usleep(1000000 / GAME_LOOP_DIVISOR);
}
r = (gd->p1->score == 21) ? 1 : 2;