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