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