5fb7b02275284dc5870a39d801e026a732161b0d
[vrtris] / src / gamescr.c
1 #include "opengl.h"
2 #include "screen.h"
3 #include "cmesh.h"
4
5 static int init(void);
6 static void cleanup(void);
7 static void start(void);
8 static void stop(void);
9 static void update(float dt);
10 static void draw(void);
11 static void reshape(int x, int y);
12 static void keyboard(int key, int pressed);
13 static void mouse(int bn, int pressed, int x, int y);
14 static void motion(int x, int y);
15 static void wheel(int dir);
16
17 struct game_screen game_screen = {
18         "game",
19         1,      /* opaque */
20         0,      /* next */
21         init,
22         cleanup,
23         start,
24         stop,
25         update,
26         draw,
27         reshape,
28         keyboard,
29         mouse,
30         motion,
31         wheel
32 };
33
34 static struct cmesh *blkmesh;
35 static float cam_theta, cam_phi, cam_dist = 6;
36 static int bnstate[16];
37 static int prev_mx, prev_my;
38
39 static int init(void)
40 {
41         if(!(blkmesh = cmesh_alloc())) {
42                 return -1;
43         }
44         if(cmesh_load(blkmesh, "data/noisecube.obj") == -1) {
45                 fprintf(stderr, "failed to load block model\n");
46                 return -1;
47         }
48         cmesh_dump(blkmesh, "dump");
49         cmesh_dump_obj(blkmesh, "dump.obj");
50         return 0;
51 }
52
53 static void cleanup(void)
54 {
55         cmesh_free(blkmesh);
56 }
57
58 static void start(void)
59 {
60 }
61
62 static void stop(void)
63 {
64 }
65
66 static void update(float dt)
67 {
68 }
69
70 static void draw(void)
71 {
72         glTranslatef(0, 0, -cam_dist);
73         glRotatef(cam_phi, 1, 0, 0);
74         glRotatef(cam_theta, 0, 1, 0);
75
76         cmesh_draw(blkmesh);
77 }
78
79 static void reshape(int x, int y)
80 {
81 }
82
83 static void keyboard(int key, int pressed)
84 {
85 }
86
87 static void mouse(int bn, int pressed, int x, int y)
88 {
89         bnstate[bn] = pressed;
90         prev_mx = x;
91         prev_my = y;
92 }
93
94 static void motion(int x, int y)
95 {
96         float dx = x - prev_mx;
97         float dy = y - prev_my;
98         prev_mx = x;
99         prev_my = y;
100
101         if(bnstate[0]) {
102                 cam_theta += dx * 0.5;
103                 cam_phi += dy * 0.5;
104
105                 if(cam_phi < -90) cam_phi = -90;
106                 if(cam_phi > 90) cam_phi = 90;
107         }
108         if(bnstate[2]) {
109                 cam_dist += dy * 0.1;
110                 if(cam_dist < 0) cam_dist = 0;
111         }
112 }
113
114 static void wheel(int dir)
115 {
116 }