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