basic map loader
[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
46 static struct
47 {
48 u32 x,y;
49
50 struct cell
51 {
52 u32 flags;
53 char *conditions;
54 }
55 * cells;
56
57 u32 *io;
58 }
59 map;
60
61 static void map_free(void)
62 {
63 for( int i = 0; i < arrlen( map.io ); i ++ )
64 {
65 arrfree( map.cells[ map.io[i] ].conditions );
66 }
67
68 arrfree( map.cells );
69 arrfree( map.io );
70 map.x = 0;
71 map.y = 0;
72 map.cells = NULL;
73 map.io = NULL;
74 }
75
76 static int map_load( const char *str )
77 {
78 map_free();
79
80 char *c = str;
81
82 // Scan for width
83 for(;; map.x ++)
84 {
85 if( str[map.x] == ';' )
86 break;
87 else if( !str[map.x] )
88 {
89 vg_error( "Unexpected EOF when parsing level!\n" );
90 return 0;
91 }
92 }
93
94 struct cell *row = arraddnptr( map.cells, map.x );
95 int cx = 0;
96 int reg_start = 0, reg_end = 0;
97
98 for(;;)
99 {
100 if( !*c )
101 break;
102
103 if( *c == ';' )
104 {
105 c ++;
106
107 // Parse attribs
108 if( *c != '\n' )
109 {
110 while( *c )
111 {
112 if( reg_start < reg_end )
113 {
114 if( *c >= 'a' && *c <= 'z' )
115 {
116 arrpush( map.cells[ map.io[ reg_start ] ].conditions, *c );
117 }
118 else
119 {
120 if( *c == ',' || *c == '\n' )
121 {
122 reg_start ++;
123
124 if( *c == '\n' )
125 break;
126 }
127 else
128 {
129 vg_error( "Unkown attrib '%c' (row: %u)\n", *c, map.y );
130 return 0;
131 }
132 }
133 }
134 else
135 {
136 vg_error( "Over-assigned values (row: %u)\n", map.y );
137 return 0;
138 }
139
140 c ++;
141 }
142 }
143
144 if( reg_start != reg_end )
145 {
146 vg_error( "Not enough values assigned (row: %u, %u of %u)\n", map.y, reg_start, reg_end );
147 return 0;
148 }
149
150 if( cx != map.x )
151 {
152 vg_error( "Map row underflow (row: %u, %u<%u)\n", map.y, cx, map.x );
153 return 0;
154 }
155
156 row = arraddnptr( map.cells, map.x );
157 cx = 0;
158 map.y ++;
159 reg_end = reg_start = arrlen( map.io );
160 }
161 else
162 {
163 if( cx == map.x )
164 {
165 vg_error( "Map row overflow (row: %u, %u>%u)\n", map.y, cx, map.x );
166 return 0;
167 }
168
169 row[ cx ].conditions = NULL;
170
171 // Parse the various cell types
172 if( *c == '+' || *c == '-' )
173 {
174 arrpush( map.io, cx + map.y*map.x );
175 row[ cx ++ ].flags = *c == '+'? CELL_FLAG_INPUT: CELL_FLAG_OUTPUT;
176 reg_end ++;
177 }
178 else if( *c == '#' )
179 {
180 row[ cx ++ ].flags = CELL_FLAG_WALL;
181 }
182 else
183 {
184 row[ cx ++ ].flags = 0x00;
185 }
186 }
187
188 c ++;
189 }
190
191 vg_success( "Map loaded! (%u:%u)\n", map.x, map.y );
192 return 1;
193 }
194
195 void vg_update(void)
196 {
197 // Update camera
198 float ratio = (float)vg_window_y / (float)vg_window_x;
199 float const size = 7.5f;
200 glm_ortho( -size, size, -size*ratio, size*ratio, 0.1f, 100.f, m_projection );
201
202 glm_mat4_identity( m_view );
203 glm_translate_z( m_view, -10.f );
204 glm_rotate_x( m_view, -1.0f, m_view );
205
206 glm_mat4_mul( m_projection, m_view, m_pv );
207
208 // Get mouse ray
209 vec3 ray_origin;
210 vec3 ray_dir;
211
212 mat4 pv_inverse;
213 vec4 vp = { 0.f, 0.f, vg_window_x, vg_window_y };
214 glm_mat4_inv( m_pv, pv_inverse );
215 glm_unprojecti( (vec3){ vg_mouse_x, vg_window_y-vg_mouse_y, -1.f }, pv_inverse, vp, ray_dir );
216 glm_unprojecti( (vec3){ vg_mouse_x, vg_window_y-vg_mouse_y, 0.f }, pv_inverse, vp, ray_origin );
217 glm_vec3_sub( ray_dir, ray_origin, ray_dir );
218
219 // Get floor tile intersection
220 float ray_t = -ray_origin[1] / ray_dir[1];
221
222 vec3 tile_pos;
223 glm_vec3_copy( ray_origin, tile_pos );
224 glm_vec3_muladds( ray_dir, ray_t, tile_pos );
225
226 tile_pos[0] = floorf( tile_pos[0] + 0.5f );
227 tile_pos[2] = floorf( tile_pos[2] + 0.5f );
228
229 glm_mat4_identity( m_mdl );
230 glm_translate( m_mdl, tile_pos );
231 }
232
233 GLuint tile_vao;
234 GLuint tile_vbo;
235
236 void vg_render(void)
237 {
238 glViewport( 0,0, vg_window_x, vg_window_y );
239
240 glEnable( GL_DEPTH_TEST );
241 glClearColor( 0.94f, 0.94f, 0.94f, 1.0f );
242 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
243
244 SHADER_USE( colour_shader );
245 glUniformMatrix4fv( SHADER_UNIFORM( colour_shader, "uPv" ), 1, GL_FALSE, (float *)m_pv );
246 glUniform4f( SHADER_UNIFORM( colour_shader, "uColour" ), 0.5f, 0.5f, 0.5f, 1.0f );
247
248 glUniformMatrix4fv( SHADER_UNIFORM( colour_shader, "uMdl" ), 1, GL_FALSE, (float *)m_mdl );
249
250 glBindVertexArray( tile_vao );
251 glDrawArrays( GL_TRIANGLES, 0, 6 );
252 }
253
254 void vg_start(void)
255 {
256 SHADER_INIT( colour_shader );
257
258 glGenVertexArrays( 1, &tile_vao );
259 glGenBuffers( 1, &tile_vbo );
260
261 float quad_mesh[] =
262 {
263 -0.5f, 0.f, -0.5f,
264 -0.5f, 0.f, 0.5f,
265 0.5f, 0.f, 0.5f,
266 -0.5f, 0.f, -0.5f,
267 0.5f, 0.f, 0.5f,
268 0.5f, 0.f, -0.5f
269 };
270
271 glBindVertexArray( tile_vao );
272 glBindBuffer( GL_ARRAY_BUFFER, tile_vbo );
273 glBufferData
274 (
275 GL_ARRAY_BUFFER,
276 sizeof( quad_mesh ),
277 quad_mesh,
278 GL_STATIC_DRAW
279 );
280
281 glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0 );
282 glEnableVertexAttribArray( 0 );
283
284 VG_CHECK_GL();
285
286 map_load
287 (
288 "###########;\n"
289 "# + #;abab\n"
290 "# #;\n"
291 "# #;\n"
292 "# - - #;aa,bb\n"
293 "###########;\n"
294 );
295 }
296
297 void vg_free(void)
298 {
299 map_free();
300 }
301
302 void vg_ui(void)
303 {
304
305 }