caff54ab70a9e07ff317c51f0f08c7ed2f0844e4
[raydungeon] / src / game.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <GL/gl.h>
4 #include "game.h"
5
6 int mouse_x, mouse_y, mouse_state[3];
7 int win_width, win_height;
8 float win_aspect;
9
10 struct game_screen *cur_scr;
11
12 /* available screens */
13 extern struct game_screen scr_menu, scr_game, scr_map;
14 #define MAX_SCREENS     4
15 static struct game_screen *screens[MAX_SCREENS];
16 static int num_screens;
17
18
19 int game_init(void)
20 {
21         int i;
22         char *start_scr_name;
23
24         /* initialize screens */
25         screens[num_screens++] = &scr_menu;
26         screens[num_screens++] = &scr_game;
27         screens[num_screens++] = &scr_map;
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.3, 0.3, 0.3, 1);
44         return 0;
45 }
46
47 void game_shutdown(void)
48 {
49         int i;
50
51         for(i=0; i<num_screens; i++) {
52                 if(screens[i]->destroy) {
53                         screens[i]->destroy();
54                 }
55         }
56 }
57
58 void game_display(void)
59 {
60         glClear(GL_COLOR_BUFFER_BIT);
61
62         if(cur_scr) {
63                 cur_scr->display();
64         }
65
66         game_swap_buffers();
67 }
68
69 void game_reshape(int x, int y)
70 {
71         win_width = x;
72         win_height = y;
73         win_aspect = (float)x / (float)y;
74         glViewport(0, 0, x, y);
75
76         if(cur_scr && cur_scr->reshape) {
77                 cur_scr->reshape(x, y);
78         }
79 }
80
81 void game_keyboard(int key, int press)
82 {
83         if(press) {
84                 switch(key) {
85                 case 27:
86                         game_quit();
87                         break;
88                 }
89         }
90
91         if(cur_scr && cur_scr->keyboard) {
92                 cur_scr->keyboard(key, press);
93         }
94 }
95
96 void game_mouse(int bn, int st, int x, int y)
97 {
98         mouse_x = x;
99         mouse_y = y;
100         if(bn < 3) {
101                 mouse_state[bn] = st;
102         }
103
104         if(cur_scr && cur_scr->mouse) {
105                 cur_scr->mouse(bn, st, x, y);
106         }
107 }
108
109 void game_motion(int x, int y)
110 {
111         mouse_x = x;
112         mouse_y = y;
113
114         if(cur_scr && cur_scr->motion) {
115                 cur_scr->motion(x, y);
116         }
117 }
118
119 void game_chscr(struct game_screen *scr)
120 {
121         if(!scr) return;
122
123         if(scr->start && scr->start() == -1) {
124                 return;
125         }
126
127         if(cur_scr && cur_scr->stop) {
128                 cur_scr->stop();
129         }
130         cur_scr = scr;
131 }