0b5f2b966cc57822f36da71bba9c1a44d0f56d57
[deeprace] / src / scr_game.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <GL/gl.h>
5 #include "miniglut.h"
6 #include "game.h"
7 #include "util.h"
8
9 static int ginit(void);
10 static void gdestroy(void);
11 static int gstart(void);
12 static void gstop(void);
13 static void gdisplay(void);
14 static void greshape(int x, int y);
15 static void gkeyb(int key, int press);
16 static void gmouse(int bn, int press, int x, int y);
17 static void gmotion(int x, int y);
18
19 struct game_screen scr_game = {
20         "game",
21         ginit, gdestroy,
22         gstart, gstop,
23         gdisplay, greshape,
24         gkeyb, gmouse, gmotion
25 };
26
27 static float cam_theta, cam_phi = 20, cam_dist = 10;
28 static float cam_pan[3];
29
30 static int ginit(void)
31 {
32         return 0;
33 }
34
35 static void gdestroy(void)
36 {
37 }
38
39 static int gstart(void)
40 {
41         return 0;
42 }
43
44 static void gstop(void)
45 {
46 }
47
48 static void gdisplay(void)
49 {
50         static int dlist;
51         glMatrixMode(GL_MODELVIEW);
52         glLoadIdentity();
53         glTranslatef(0, 0, -cam_dist);
54         glRotatef(cam_phi, 1, 0, 0);
55         glRotatef(cam_theta, 0, 1, 0);
56         glTranslatef(cam_pan[0], cam_pan[1], cam_pan[2]);
57
58         glColor3f(1, 1, 1);
59         glFrontFace(GL_CW);
60         if(!dlist) {
61                 dlist = glGenLists(1);
62                 glNewList(dlist, GL_COMPILE);
63                 glutSolidTeapot(1);
64                 glEndList();
65         }
66         glCallList(dlist);
67         glFrontFace(GL_CCW);
68 }
69
70 static void greshape(int x, int y)
71 {
72 }
73
74 static void gkeyb(int key, int press)
75 {
76 }
77
78 static void gmouse(int bn, int press, int x, int y)
79 {
80 }
81
82 static void gmotion(int x, int y)
83 {
84         int dx = x - mouse_x;
85         int dy = y - mouse_y;
86
87         if(!(dx | dy)) return;
88
89         if(mouse_state[0]) {
90                 cam_theta += dx * 0.5;
91                 cam_phi += dy * 0.5;
92                 if(cam_phi < -90) cam_phi = -90;
93                 if(cam_phi > 90) cam_phi = 90;
94         }
95         if(mouse_state[1]) {
96                 float up[3], right[3];
97                 float theta = cam_theta * M_PI / 180.0f;
98                 float phi = cam_phi * M_PI / 180.0f;
99
100                 up[0] = -sin(theta) * sin(phi);
101                 up[1] = -cos(phi);
102                 up[2] = cos(theta) * sin(phi);
103                 right[0] = cos(theta);
104                 right[1] = 0;
105                 right[2] = sin(theta);
106
107                 cam_pan[0] += (right[0] * dx + up[0] * dy) * 0.01;
108                 cam_pan[1] += up[1] * dy * 0.01;
109                 cam_pan[2] += (right[2] * dx + up[2] * dy) * 0.01;
110         }
111         if(mouse_state[2]) {
112                 cam_dist += dy * 0.1;
113                 if(cam_dist < 0) cam_dist = 0;
114         }
115 }