rev |
line source |
nuclear@0
|
1 #include <stdio.h>
|
nuclear@0
|
2 #include <stdlib.h>
|
nuclear@0
|
3 #include <math.h>
|
nuclear@11
|
4 #include <assert.h>
|
nuclear@0
|
5 #include "opengl.h"
|
nuclear@0
|
6 #include "game.h"
|
nuclear@7
|
7 #include "camera.h"
|
nuclear@11
|
8 #include "sanegl.h"
|
nuclear@17
|
9 #include "texture.h"
|
nuclear@21
|
10 #include "shader.h"
|
nuclear@22
|
11 #include "text.h"
|
nuclear@7
|
12
|
nuclear@21
|
13 static void draw_quad(float hsz, float vsz);
|
nuclear@0
|
14
|
nuclear@22
|
15 int win_width, win_height;
|
nuclear@22
|
16
|
nuclear@9
|
17 static float win_aspect;
|
nuclear@9
|
18 static int video_width, video_height;
|
nuclear@9
|
19 static float video_aspect;
|
nuclear@17
|
20 static struct texture *test_tex;
|
nuclear@17
|
21
|
nuclear@21
|
22 static SdrProg *sdr_cam, *sdr_tex;
|
nuclear@0
|
23
|
nuclear@20
|
24 extern "C" int game_init(void)
|
nuclear@0
|
25 {
|
nuclear@17
|
26 //glEnable(GL_DEPTH_TEST);
|
nuclear@22
|
27 //glEnable(GL_CULL_FACE);
|
nuclear@0
|
28
|
nuclear@0
|
29 glClearColor(0.4, 0.4, 0.4, 1);
|
nuclear@8
|
30
|
nuclear@21
|
31 if(!(sdr_cam = get_sdrprog("sdr/vertex.glsl", "sdr/android_cam_preview.p.glsl"))) {
|
nuclear@10
|
32 return -1;
|
nuclear@10
|
33 }
|
nuclear@21
|
34 if(!(sdr_tex = get_sdrprog("sdr/vertex.glsl", "sdr/tex.p.glsl"))) {
|
nuclear@17
|
35 return -1;
|
nuclear@17
|
36 }
|
nuclear@17
|
37
|
nuclear@17
|
38 if(!(test_tex = get_texture("data/opengl.png"))) {
|
nuclear@17
|
39 return -1;
|
nuclear@17
|
40 }
|
nuclear@10
|
41
|
nuclear@8
|
42 cam_start_video();
|
nuclear@9
|
43 cam_video_size(&video_width, &video_height);
|
nuclear@9
|
44 if(video_height) {
|
nuclear@9
|
45 video_aspect = (float)video_width / (float)video_height;
|
nuclear@9
|
46 } else {
|
nuclear@9
|
47 video_aspect = 1.0;
|
nuclear@9
|
48 }
|
nuclear@9
|
49
|
nuclear@9
|
50 printf("started video %dx%d (aspect: %g)\n", video_width, video_height, video_aspect);
|
nuclear@0
|
51 return 0;
|
nuclear@0
|
52 }
|
nuclear@0
|
53
|
nuclear@20
|
54 extern "C" void game_shutdown(void)
|
nuclear@0
|
55 {
|
nuclear@8
|
56 cam_shutdown();
|
nuclear@21
|
57 delete sdr_cam;
|
nuclear@21
|
58 delete sdr_tex;
|
nuclear@0
|
59 }
|
nuclear@0
|
60
|
nuclear@20
|
61 extern "C" void game_display(unsigned long msec)
|
nuclear@0
|
62 {
|
nuclear@9
|
63 unsigned int tex;
|
nuclear@9
|
64 const float *tex_matrix;
|
nuclear@9
|
65 float xscale, yscale;
|
nuclear@9
|
66
|
nuclear@8
|
67 cam_update();
|
nuclear@9
|
68 tex = cam_texture();
|
nuclear@9
|
69 tex_matrix = cam_texture_matrix();
|
nuclear@7
|
70
|
nuclear@4
|
71 //float tsec = (float)msec / 1000.0f;
|
nuclear@0
|
72
|
nuclear@0
|
73 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
nuclear@0
|
74
|
nuclear@11
|
75 gl_matrix_mode(GL_MODELVIEW);
|
nuclear@11
|
76 gl_load_identity();
|
nuclear@11
|
77 gl_matrix_mode(GL_TEXTURE);
|
nuclear@11
|
78 gl_load_matrixf(tex_matrix);
|
nuclear@11
|
79
|
nuclear@21
|
80 sdr_cam->bind();
|
nuclear@10
|
81 glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex);
|
nuclear@7
|
82
|
nuclear@9
|
83 if(video_aspect > win_aspect) {
|
nuclear@9
|
84 xscale = 1.0;
|
nuclear@9
|
85 yscale = 1.0 / video_aspect;
|
nuclear@9
|
86 } else {
|
nuclear@9
|
87 xscale = video_aspect;
|
nuclear@9
|
88 yscale = 1.0;
|
nuclear@9
|
89 }
|
nuclear@21
|
90 draw_quad(xscale, yscale);
|
nuclear@17
|
91
|
nuclear@17
|
92 gl_matrix_mode(GL_TEXTURE);
|
nuclear@17
|
93 gl_load_identity();
|
nuclear@17
|
94 gl_translatef(0, 1, 0);
|
nuclear@17
|
95 gl_scalef(1, -1, 1);
|
nuclear@17
|
96 gl_matrix_mode(GL_MODELVIEW);
|
nuclear@17
|
97 gl_load_identity();
|
nuclear@17
|
98 gl_scalef((float)test_tex->width / (float)test_tex->height, 1, 1);
|
nuclear@17
|
99
|
nuclear@21
|
100 sdr_tex->bind();
|
nuclear@17
|
101 glBindTexture(GL_TEXTURE_2D, test_tex->texid);
|
nuclear@17
|
102
|
nuclear@17
|
103 glEnable(GL_BLEND);
|
nuclear@17
|
104 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
nuclear@22
|
105 draw_quad(0.5, 0.5);
|
nuclear@22
|
106 glDisable(GL_BLEND);
|
nuclear@17
|
107
|
nuclear@22
|
108 gl_matrix_mode(GL_TEXTURE);
|
nuclear@22
|
109 gl_load_identity();
|
nuclear@17
|
110
|
nuclear@22
|
111
|
nuclear@22
|
112 // draw some text
|
nuclear@22
|
113 text_color(1, 1, 1, 1);
|
nuclear@22
|
114 for(int i=0; i<25; i++) {
|
nuclear@22
|
115 text_position(0, i);
|
nuclear@22
|
116 text_printf("%d%s line of text", i, i == 1 ? "st" : (i == 2 ? "nd" : (i == 3 ? "rd" : "th")));
|
nuclear@22
|
117 }
|
nuclear@7
|
118 }
|
nuclear@7
|
119
|
nuclear@21
|
120 static void draw_quad(float hsz, float vsz)
|
nuclear@7
|
121 {
|
nuclear@7
|
122 static const float varr[] = {-1, -1, 1, -1, 1, 1, -1, 1};
|
nuclear@9
|
123 static const float tcarr[] = {0, 0, 1, 0, 1, 1, 0, 1};
|
nuclear@7
|
124
|
nuclear@11
|
125 gl_matrix_mode(GL_MODELVIEW);
|
nuclear@11
|
126 gl_push_matrix();
|
nuclear@11
|
127 gl_scalef(hsz, vsz, 1);
|
nuclear@11
|
128
|
nuclear@21
|
129 if(SdrProg::active) {
|
nuclear@21
|
130 gl_apply_xform(SdrProg::active->get_globj());
|
nuclear@21
|
131 }
|
nuclear@7
|
132
|
nuclear@21
|
133 glEnableVertexAttribArray(SDR_ATTR_VERTEX);
|
nuclear@21
|
134 glEnableVertexAttribArray(SDR_ATTR_TEXCOORD);
|
nuclear@21
|
135 glVertexAttribPointer(SDR_ATTR_VERTEX, 2, GL_FLOAT, 0, 0, varr);
|
nuclear@21
|
136 glVertexAttribPointer(SDR_ATTR_TEXCOORD, 2, GL_FLOAT, 0, 0, tcarr);
|
nuclear@7
|
137
|
nuclear@8
|
138 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
|
nuclear@7
|
139
|
nuclear@21
|
140 glDisableVertexAttribArray(SDR_ATTR_VERTEX);
|
nuclear@21
|
141 glDisableVertexAttribArray(SDR_ATTR_TEXCOORD);
|
nuclear@11
|
142
|
nuclear@11
|
143 gl_pop_matrix();
|
nuclear@0
|
144 }
|
nuclear@0
|
145
|
nuclear@20
|
146 extern "C" void game_reshape(int x, int y)
|
nuclear@0
|
147 {
|
nuclear@0
|
148 win_width = x;
|
nuclear@0
|
149 win_height = y;
|
nuclear@9
|
150 win_aspect = y ? (float)x / (float)y : 1.0;
|
nuclear@0
|
151 glViewport(0, 0, x, y);
|
nuclear@0
|
152
|
nuclear@11
|
153 gl_matrix_mode(GL_PROJECTION);
|
nuclear@11
|
154 gl_load_identity();
|
nuclear@11
|
155 gl_scalef((float)win_height / (float)win_width, 1, 1);
|
nuclear@0
|
156 }
|
nuclear@0
|
157
|
nuclear@20
|
158 extern "C" void game_keyboard(int key, int pressed)
|
nuclear@0
|
159 {
|
nuclear@0
|
160 if(!pressed) return;
|
nuclear@0
|
161
|
nuclear@0
|
162 switch(key) {
|
nuclear@0
|
163 case 27:
|
nuclear@0
|
164 exit(0);
|
nuclear@0
|
165
|
nuclear@0
|
166 default:
|
nuclear@0
|
167 break;
|
nuclear@0
|
168 }
|
nuclear@0
|
169 }
|
nuclear@0
|
170
|
nuclear@0
|
171 #define MAX_TOUCH_IDS 16
|
nuclear@0
|
172 static struct {
|
nuclear@0
|
173 int bnstate[8];
|
nuclear@0
|
174 int prev_x, prev_y;
|
nuclear@0
|
175 } mstate[MAX_TOUCH_IDS];
|
nuclear@0
|
176
|
nuclear@20
|
177 extern "C" void game_mouse_button(int id, int bn, int pressed, int x, int y)
|
nuclear@0
|
178 {
|
nuclear@0
|
179 if(id >= MAX_TOUCH_IDS) return;
|
nuclear@0
|
180
|
nuclear@0
|
181 mstate[id].prev_x = x;
|
nuclear@0
|
182 mstate[id].prev_y = y;
|
nuclear@0
|
183 mstate[id].bnstate[bn] = pressed;
|
nuclear@0
|
184 }
|
nuclear@0
|
185
|
nuclear@20
|
186 extern "C" void game_mouse_motion(int id, int x, int y)
|
nuclear@0
|
187 {
|
nuclear@0
|
188 /*
|
nuclear@0
|
189 int dx, dy, cx, cy;
|
nuclear@0
|
190
|
nuclear@0
|
191 if(id >= MAX_TOUCH_IDS) return;
|
nuclear@0
|
192
|
nuclear@0
|
193 cx = win_width / 2;
|
nuclear@0
|
194 cy = win_height / 2;
|
nuclear@0
|
195
|
nuclear@0
|
196 dx = x - mstate[id].prev_x;
|
nuclear@0
|
197 dy = y - mstate[id].prev_y;
|
nuclear@0
|
198 mstate[id].prev_x = x;
|
nuclear@0
|
199 mstate[id].prev_y = y;
|
nuclear@0
|
200
|
nuclear@0
|
201 if(!dx && !dy) return;
|
nuclear@0
|
202
|
nuclear@0
|
203 if(mouselook || mstate[id].bnstate[0]) {
|
nuclear@0
|
204 player_turn(&player, dx * 0.5, dy * 0.5);
|
nuclear@0
|
205 }
|
nuclear@0
|
206 if(mstate[id].bnstate[2]) {
|
nuclear@0
|
207 dbg_cam_dist += 0.1 * dy;
|
nuclear@0
|
208 if(dbg_cam_dist < 0.0) dbg_cam_dist = 0.0;
|
nuclear@0
|
209 }
|
nuclear@0
|
210
|
nuclear@0
|
211 if(mouselook) {
|
nuclear@0
|
212 warping_mouse = 1;
|
nuclear@0
|
213 set_mouse_pos(cx, cy);
|
nuclear@0
|
214 mstate[id].prev_x = cx;
|
nuclear@0
|
215 mstate[id].prev_y = cy;
|
nuclear@0
|
216 }
|
nuclear@0
|
217 */
|
nuclear@0
|
218 }
|
nuclear@0
|
219
|