basic selection code
[fishladder.git] / fishladder.c
1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
2
3 //#define VG_STEAM
4 #include "vg/vg.h"
5
6 SHADER_DEFINE( colour_shader,
7
8 // VERTEX
9 "layout (location=0) in vec3 a_co;"
10 "uniform mat4 uPv;"
11 "uniform mat4 uMdl;"
12 ""
13 "void main()"
14 "{"
15 " vec4 vert_pos = uPv * uMdl * vec4( a_co, 1.0 );"
16 " gl_Position = vert_pos;"
17 "}",
18
19 // FRAGMENT
20 "out vec4 FragColor;"
21 "uniform vec4 uColour;"
22 ""
23 "void main()"
24 "{"
25 " FragColor = uColour;"
26 "}"
27 ,
28 UNIFORMS({ "uPv", "uMdl", "uColour" })
29 )
30
31 mat4 m_projection;
32 mat4 m_view;
33 mat4 m_pv;
34 mat4 m_mdl;
35
36 int main( int argc, char *argv[] )
37 {
38 vg_init( argc, argv, "FishLadder" );
39 }
40
41 #define CELL_FLAG_INPUT 0x1
42 #define CELL_FLAG_OUTPUT 0x2
43 #define CELL_FLAG_IO (CELL_FLAG_INPUT|CELL_FLAG_OUTPUT)
44 #define CELL_FLAG_WALL 0x4
45 #define CELL_FLAG_HOVER 0x8
46 #define CELL_FLAG_ITER 0x10
47 #define CELL_FLAG_CANAL 0x20
48
49 static struct
50 {
51 u32 x,y;
52
53 struct cell
54 {
55 u32 flags;
56 u32 model_id;
57
58 char *conditions;
59 }
60 * cells;
61
62 vec3 origin;
63 struct cell *selected;
64 int select_valid;
65
66 u32 *io;
67
68 struct vstack
69 {
70 struct vframe
71 {
72 int x, y;
73 int i;
74 }
75 frames[ 64 ];
76
77 int level;
78 u32 flags;
79 }
80 stack;
81 }
82 map;
83
84 static void map_free(void)
85 {
86 for( int i = 0; i < arrlen( map.io ); i ++ )
87 {
88 arrfree( map.cells[ map.io[i] ].conditions );
89 }
90
91 arrfree( map.cells );
92 arrfree( map.io );
93 map.x = 0;
94 map.y = 0;
95 map.cells = NULL;
96 map.io = NULL;
97 }
98
99 static struct cell *map_tile_at( int pos[2] )
100 {
101 if( pos[0] >= 0 && pos[0] < map.x && pos[1] >= 0 && pos[1] < map.y )
102 return map.cells + pos[1]*map.x + pos[0];
103 return NULL;
104 }
105
106 static int map_load( const char *str )
107 {
108 map_free();
109
110 char *c = str;
111
112 // Scan for width
113 for(;; map.x ++)
114 {
115 if( str[map.x] == ';' )
116 break;
117 else if( !str[map.x] )
118 {
119 vg_error( "Unexpected EOF when parsing level!\n" );
120 return 0;
121 }
122 }
123
124 struct cell *row = arraddnptr( map.cells, map.x );
125 int cx = 0;
126 int reg_start = 0, reg_end = 0;
127
128 for(;;)
129 {
130 if( !*c )
131 break;
132
133 if( *c == ';' )
134 {
135 c ++;
136
137 // Parse attribs
138 if( *c != '\n' )
139 {
140 while( *c )
141 {
142 if( reg_start < reg_end )
143 {
144 if( *c >= 'a' && *c <= 'z' )
145 {
146 arrpush( map.cells[ map.io[ reg_start ] ].conditions, *c );
147 }
148 else
149 {
150 if( *c == ',' || *c == '\n' )
151 {
152 reg_start ++;
153
154 if( *c == '\n' )
155 break;
156 }
157 else
158 {
159 vg_error( "Unkown attrib '%c' (row: %u)\n", *c, map.y );
160 return 0;
161 }
162 }
163 }
164 else
165 {
166 vg_error( "Over-assigned values (row: %u)\n", map.y );
167 return 0;
168 }
169
170 c ++;
171 }
172 }
173
174 if( reg_start != reg_end )
175 {
176 vg_error( "Not enough values assigned (row: %u, %u of %u)\n", map.y, reg_start, reg_end );
177 return 0;
178 }
179
180 if( cx != map.x )
181 {
182 vg_error( "Map row underflow (row: %u, %u<%u)\n", map.y, cx, map.x );
183 return 0;
184 }
185
186 row = arraddnptr( map.cells, map.x );
187 cx = 0;
188 map.y ++;
189 reg_end = reg_start = arrlen( map.io );
190 }
191 else
192 {
193 if( cx == map.x )
194 {
195 vg_error( "Map row overflow (row: %u, %u>%u)\n", map.y, cx, map.x );
196 return 0;
197 }
198
199 row[ cx ].conditions = NULL;
200
201 // Parse the various cell types
202 if( *c == '+' || *c == '-' )
203 {
204 arrpush( map.io, cx + map.y*map.x );
205 row[ cx ++ ].flags = *c == '+'? CELL_FLAG_INPUT: CELL_FLAG_OUTPUT;
206 reg_end ++;
207 }
208 else if( *c == '#' )
209 {
210 row[ cx ++ ].flags = CELL_FLAG_WALL;
211 }
212 else
213 {
214 row[ cx ++ ].flags = 0x00;
215 }
216 }
217
218 c ++;
219 }
220
221 // Origin top left corner
222 map.origin[0] = -((float)map.x) * 0.5f;
223 map.origin[2] = -((float)map.y) * 0.5f;
224
225 vg_success( "Map loaded! (%u:%u)\n", map.x, map.y );
226 return 1;
227 }
228
229 void vg_update(void)
230 {
231 // Update camera
232 float ratio = (float)vg_window_y / (float)vg_window_x;
233 float const size = 7.5f;
234 glm_ortho( -size, size, -size*ratio, size*ratio, 0.1f, 100.f, m_projection );
235
236 glm_mat4_identity( m_view );
237 glm_translate_z( m_view, -10.f );
238 glm_rotate_x( m_view, 1.0f, m_view );
239
240 glm_mat4_mul( m_projection, m_view, m_pv );
241
242 // Get mouse ray
243 vec3 ray_origin;
244 vec3 ray_dir;
245
246 mat4 pv_inverse;
247 vec4 vp = { 0.f, 0.f, vg_window_x, vg_window_y };
248 glm_mat4_inv( m_pv, pv_inverse );
249 glm_unprojecti( (vec3){ vg_mouse_x, vg_window_y-vg_mouse_y, -1.f }, pv_inverse, vp, ray_dir );
250 glm_unprojecti( (vec3){ vg_mouse_x, vg_window_y-vg_mouse_y, 0.f }, pv_inverse, vp, ray_origin );
251 glm_vec3_sub( ray_dir, ray_origin, ray_dir );
252
253 // Get floor tile intersection
254 float ray_t = -ray_origin[1] / ray_dir[1];
255
256 vec3 tile_pos;
257 glm_vec3_copy( ray_origin, tile_pos );
258 glm_vec3_muladds( ray_dir, ray_t, tile_pos );
259 glm_vec3_sub( tile_pos, map.origin, tile_pos );
260
261 int tile_x = floorf( tile_pos[0] );
262 int tile_y = floorf( tile_pos[2] );
263
264 map.selected = map_tile_at( (int [2]){tile_x, tile_y} );
265 }
266
267 GLuint tile_vao;
268 GLuint tile_vbo;
269
270 void vg_render(void)
271 {
272 glViewport( 0,0, vg_window_x, vg_window_y );
273
274 glEnable( GL_DEPTH_TEST );
275 glClearColor( 0.94f, 0.94f, 0.94f, 1.0f );
276 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
277
278 SHADER_USE( colour_shader );
279 glUniformMatrix4fv( SHADER_UNIFORM( colour_shader, "uPv" ), 1, GL_FALSE, (float *)m_pv );
280
281 for( int y = 0; y < map.y; y ++ )
282 {
283 for( int x = 0; x < map.x; x ++ )
284 {
285 glm_mat4_identity( m_mdl );
286 glm_translate( m_mdl,
287 (vec3){
288 map.origin[0] + (float)x + 0.5f,
289 0.f,
290 map.origin[2] + (float)y + 0.5f
291 }
292 );
293 glUniformMatrix4fv( SHADER_UNIFORM( colour_shader, "uMdl" ), 1, GL_FALSE, (float *)m_mdl );
294
295 struct cell *cell = &map.cells[ y*map.x+x ];
296
297 if( map.selected != cell )
298 {
299 if( cell->flags & CELL_FLAG_INPUT )
300 glUniform4f( SHADER_UNIFORM( colour_shader, "uColour" ), 0.9f, 0.5f, 0.5f, 1.0f );
301 else if( cell->flags & CELL_FLAG_OUTPUT )
302 glUniform4f( SHADER_UNIFORM( colour_shader, "uColour" ), 0.5f, 0.9f, 0.5f, 1.0f );
303 else if( cell->flags & CELL_FLAG_WALL )
304 glUniform4f( SHADER_UNIFORM( colour_shader, "uColour" ), 0.1f, 0.1f, 0.1f, 1.0f );
305 else
306 glUniform4f( SHADER_UNIFORM( colour_shader, "uColour" ), 0.7f, 0.7f, 0.7f, 1.0f );
307 }
308 else
309 {
310 float flash = sinf( vg_time*2.5f ) * 0.25f + 0.75f;
311 glUniform4f( SHADER_UNIFORM( colour_shader, "uColour" ), flash,flash,flash, 1.0f );
312 }
313
314 glDrawArrays( GL_TRIANGLES, 0, 6 );
315 }
316 }
317 }
318
319 void vg_start(void)
320 {
321 SHADER_INIT( colour_shader );
322
323 glGenVertexArrays( 1, &tile_vao );
324 glGenBuffers( 1, &tile_vbo );
325
326 float quad_mesh[] =
327 {
328 -0.5f, 0.f, -0.5f,
329 -0.5f, 0.f, 0.5f,
330 0.5f, 0.f, 0.5f,
331 -0.5f, 0.f, -0.5f,
332 0.5f, 0.f, 0.5f,
333 0.5f, 0.f, -0.5f
334 };
335
336 glBindVertexArray( tile_vao );
337 glBindBuffer( GL_ARRAY_BUFFER, tile_vbo );
338 glBufferData
339 (
340 GL_ARRAY_BUFFER,
341 sizeof( quad_mesh ),
342 quad_mesh,
343 GL_STATIC_DRAW
344 );
345
346 glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0 );
347 glEnableVertexAttribArray( 0 );
348
349 VG_CHECK_GL();
350
351 map_load
352 (
353 "#####-#####;aa\n"
354 "# #;\n"
355 "# #;\n"
356 "# -;bb\n"
357 "# #;\n"
358 "# #;\n"
359 "#####+#####;abab\n"
360 );
361 }
362
363 void vg_free(void)
364 {
365 map_free();
366 }
367
368 void vg_ui(void)
369 {
370
371 }