fps counter
[deeprace] / src / game.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "miniglut.h"
5 #include <GL/glu.h>
6 #include "game.h"
7 #include "input.h"
8
9 int mouse_x, mouse_y, mouse_state[3];
10 int win_width, win_height;
11 float win_aspect;
12
13 struct game_screen *cur_scr;
14
15 /* available screens */
16 extern struct game_screen scr_menu, scr_game;
17 #define MAX_SCREENS     4
18 static struct game_screen *screens[MAX_SCREENS];
19 static int num_screens;
20
21
22 int game_init(void)
23 {
24         int i;
25         char *start_scr_name;
26
27         /* initialize screens */
28         screens[num_screens++] = &scr_menu;
29         screens[num_screens++] = &scr_game;
30
31         start_scr_name = getenv("START_SCREEN");
32
33         for(i=0; i<num_screens; i++) {
34                 if(screens[i]->init() == -1) {
35                         return -1;
36                 }
37                 if(screens[i]->name && start_scr_name && strcmp(screens[i]->name, start_scr_name) == 0) {
38                         game_chscr(screens[i]);
39                 }
40         }
41         if(!cur_scr) {
42                 game_chscr(&scr_game);  /* TODO: scr_menu */
43         }
44
45         init_input();
46
47         glClearColor(0.1, 0.1, 0.1, 1);
48         glEnable(GL_DEPTH_TEST);
49         glEnable(GL_CULL_FACE);
50         glEnable(GL_LIGHTING);
51         glEnable(GL_LIGHT0);
52         return 0;
53 }
54
55 void game_shutdown(void)
56 {
57         int i;
58
59         putchar('\n');
60
61         for(i=0; i<num_screens; i++) {
62                 if(screens[i]->destroy) {
63                         screens[i]->destroy();
64                 }
65         }
66 }
67
68 void game_display(void)
69 {
70         static long nframes, interv, prev_msec;
71         long msec;
72
73         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
74
75         cur_scr->display();
76
77         game_swap_buffers();
78
79
80         msec = glutGet(GLUT_ELAPSED_TIME);
81         interv += msec - prev_msec;
82         prev_msec = msec;
83         if(interv >= 1000) {
84                 float fps = (float)(nframes * 1000) / interv;
85                 printf("\rfps: %.2f    ", fps);
86                 fflush(stdout);
87                 nframes = 0;
88                 interv = 0;
89         }
90         nframes++;
91 }
92
93 void game_reshape(int x, int y)
94 {
95         win_width = x;
96         win_height = y;
97         win_aspect = (float)x / (float)y;
98         glViewport(0, 0, x, y);
99
100         glMatrixMode(GL_PROJECTION);
101         glLoadIdentity();
102         gluPerspective(50, win_aspect, 0.5, 500);
103
104         if(cur_scr && cur_scr->reshape) {
105                 cur_scr->reshape(x, y);
106         }
107 }
108
109 void game_keyboard(int key, int press)
110 {
111         if(press) {
112                 switch(key) {
113                 case 27:
114                         game_quit();
115                         break;
116                 }
117         }
118
119         if(cur_scr && cur_scr->keyboard) {
120                 cur_scr->keyboard(key, press);
121         }
122 }
123
124 void game_mouse(int bn, int st, int x, int y)
125 {
126         mouse_x = x;
127         mouse_y = y;
128         if(bn < 3) {
129                 mouse_state[bn] = st;
130         }
131
132         if(cur_scr && cur_scr->mouse) {
133                 cur_scr->mouse(bn, st, x, y);
134         }
135 }
136
137 void game_motion(int x, int y)
138 {
139         if(cur_scr && cur_scr->motion) {
140                 cur_scr->motion(x, y);
141         }
142         mouse_x = x;
143         mouse_y = y;
144 }
145
146 void game_chscr(struct game_screen *scr)
147 {
148         if(!scr) return;
149
150         if(scr->start && scr->start() == -1) {
151                 return;
152         }
153
154         if(cur_scr && cur_scr->stop) {
155                 cur_scr->stop();
156         }
157         cur_scr = scr;
158 }