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