bf877e33b19e04f21bf4102ea50a8171f77a7381
1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
6 SHADER_DEFINE( shader_tile_colour
,
9 "layout (location=0) in vec2 a_co;"
11 "uniform vec3 uOffset;"
15 "gl_Position = vec4( uPv * vec3( a_co * uOffset.z + uOffset.xy, 1.0 ), 1.0 );"
20 "uniform vec4 uColour;"
24 "FragColor = uColour;"
27 UNIFORMS({ "uPv", "uOffset", "uColour" })
30 SHADER_DEFINE( shader_tile_main
,
32 "layout (location=0) in vec2 a_co;"
33 "uniform vec4 uOffset;" // Tile x/y, uv x/y
36 "out vec4 aTexCoords;"
40 "vec3 p3 = fract(vec3(p.xyx) * vec3(.1031, .1030, .0973));"
41 "p3 += dot(p3, p3.yzx+33.33);"
42 "return fract((p3.xx+p3.yz)*p3.zy);"
47 "vec2 random_offset = floor(hash22(uOffset.xy) * 4.0) * 0.25;"
48 "vec2 edge_safe_coords = a_co * 0.98 + 0.01;"
49 "aTexCoords = vec4((edge_safe_coords + uOffset.zw) * 0.25, edge_safe_coords * 0.25 + random_offset );"
50 "gl_Position = vec4( uPv * vec3( a_co + uOffset.xy, 1.0 ), 1.0 );"
56 "uniform sampler2D uTexGlyphs;"
57 "uniform sampler2D uTexWood;"
63 "vec3 shadowing_colour = vec3( 0.93, 0.88536, 0.8184 );"
64 "vec4 glyph = texture( uTexGlyphs, aTexCoords.xy );"
65 "vec4 wood = texture( uTexWood, aTexCoords.zw );"
66 "vec4 wood_secondary = texture( uTexWood, aTexCoords.zw + 0.25 );"
67 "vec3 wood_comp = mix( wood_secondary.rgb * shadowing_colour, wood.rgb, clamp( glyph.b * 2.0 - 1.0, 0.0, 1.0 ) );"
69 "vec3 shadows = mix( vec3( 0.85, 0.7344, 0.561 ), vec3(1.0,1.0,1.0), glyph.r );"
71 "FragColor = vec4( wood_comp * shadows, 1.0 );"
74 UNIFORMS({ "uPv", "uOffset", "uTexGlyphs", "uTexWood" })
77 const char *level_pack
[] =
102 "#####-#####;bbbbb\n"
107 "###+##+####;bbb,bb\n"
112 "###-#####-###;a,aaa\n"
119 "######+######;aaaa\n"
124 "###-#####-###;aaa,aa\n"
131 "###+#####+###;aa,aaa\n"
135 GLuint tex_tile_data
;
136 GLuint tex_tile_detail
;
143 #define FLAG_INPUT 0x1
144 #define FLAG_OUTPUT 0x2
145 #define FLAG_CANAL 0x4
146 #define FLAG_WALL 0x8
147 #define FLAG_DROP_L 0x10
148 #define FLAG_SPLIT 0x20
149 #define FLAG_MERGER 0x40
150 #define FLAG_DROP_R 0x80
151 #define FLAG_FLIP_FLOP 0x100
154 { { 0.9f
, 0.2f
, 0.01f
},
155 { 0.2f
, 0.9f
, 0.14f
},
156 { 0.1f
, 0.3f
, 0.85f
} };
158 static void colour_code_v3( char cc
, v3f target
)
160 if( cc
>= 'a' && cc
<= 'z' )
164 if( id
< vg_list_size( colour_sets
) )
166 v3_copy( colour_sets
[ id
], target
);
171 v3_copy( (v3f
){0.0f
,0.0f
,0.0f
}, target
);
180 static void init_mesh( struct mesh
*m
, float *tris
, u32 length
)
182 m
->elements
= length
/3;
183 glGenVertexArrays( 1, &m
->vao
);
184 glGenBuffers( 1, &m
->vbo
);
186 glBindVertexArray( m
->vao
);
187 glBindBuffer( GL_ARRAY_BUFFER
, m
->vbo
);
188 glBufferData( GL_ARRAY_BUFFER
, length
*sizeof(float), tris
, GL_STATIC_DRAW
);
190 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2 * sizeof(float), (void*)0 );
191 glEnableVertexAttribArray( 0 );
196 static void free_mesh( struct mesh
*m
)
198 glDeleteVertexArrays( 1, &m
->vao
);
199 glDeleteBuffers( 1, &m
->vbo
);
202 static void draw_mesh( int const start
, int const count
)
204 glDrawArrays( GL_TRIANGLES
, start
*3, count
*3 );
207 static void use_mesh( struct mesh
*m
)
209 glBindVertexArray( m
->vao
);
229 // TODO: Split into input/output structures
239 struct mesh tile
, circle
, splitter_l
, splitter_r
;
255 static void map_free(void)
257 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
258 arrfree( world
.io
[ i
].conditions
);
260 arrfree( world
.data
);
269 static int map_load( const char *str
)
278 if( str
[world
.w
] == ';' )
280 else if( !str
[world
.w
] )
282 vg_error( "Unexpected EOF when parsing level\n" );
287 struct cell
*row
= arraddnptr( world
.data
, world
.w
);
289 int reg_start
= 0, reg_end
= 0;
305 if( reg_start
< reg_end
)
307 if( *c
>= 'a' && *c
<= 'z' )
309 arrpush( world
.io
[ reg_start
].conditions
, *c
);
313 if( *c
== ',' || *c
== '\n' )
322 vg_error( "Unkown attribute '%c' (row: %u)\n", *c
, world
.h
);
329 vg_error( "Too many values to assign (row: %u)\n", world
.h
);
337 if( reg_start
!= reg_end
)
339 vg_error( "Not enough values assigned (row: %u, %u of %u)\n", world
.h
, reg_start
, reg_end
);
345 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world
.h
, cx
, world
.w
);
349 row
= arraddnptr( world
.data
, world
.w
);
352 reg_end
= reg_start
= arrlen( world
.io
);
358 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world
.h
, cx
, world
.w
);
362 // Tile initialization
365 if( *c
== '+' || *c
== '-' )
367 struct cell_terminal term
= { .id
= cx
+ world
.h
*world
.w
};
368 arrpush( world
.io
, term
);
369 row
[ cx
++ ].state
= *c
== '+'? FLAG_INPUT
: FLAG_OUTPUT
;
374 row
[ cx
++ ].state
= FLAG_WALL
;
378 row
[ cx
++ ].state
= 0x00;
385 vg_success( "Map loaded! (%u:%u)\n", world
.w
, world
.h
);
389 static struct cell
*pcell( v2i pos
)
391 return &world
.data
[ pos
[1]*world
.w
+ pos
[0] ];
394 int main( int argc
, char *argv
[] )
396 vg_init( argc
, argv
, "Fish (Marbles Computer) Ladder Simulator 2022 | N,M: change level | SPACE: Test | LeftClick: Toggle tile" );
399 void vg_register(void)
401 SHADER_INIT( shader_tile_colour
);
402 SHADER_INIT( shader_tile_main
);
411 0.0f
, 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
,
412 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
, 0.0f
,
414 0.48f
, 0.48f
, 0.5f
, 0.52f
, 0.52f
, 0.52f
, // Static dot
415 0.375f
, 0.25f
, 0.5f
, 0.75f
, 0.625f
, 0.25f
, // Downwards pointing arrow
416 0.25f
, 0.625f
, 0.75f
, 0.5f
, 0.25f
, 0.375f
, // Left
417 0.625f
, 0.75f
, 0.5f
, 0.25f
, 0.375f
, 0.75f
, // up
418 0.75f
, 0.375f
, 0.25f
, 0.5f
, 0.75f
, 0.625f
421 init_mesh( &world
.tile
, quad_mesh
, vg_list_size(quad_mesh
) );
426 float circle_mesh
[32*6*3];
427 int res
= vg_list_size( circle_mesh
) / (6*3);
429 for( int i
= 0; i
< res
; i
++ )
431 v2f v0
= { sinf( ((float)i
/(float)res
)*VG_TAUf
), cosf( ((float)i
/(float)res
)*VG_TAUf
) };
432 v2f v1
= { sinf( ((float)(i
+1)/(float)res
)*VG_TAUf
), cosf( ((float)(i
+1)/(float)res
)*VG_TAUf
) };
434 circle_mesh
[ i
*6+0 ] = 0.0f
;
435 circle_mesh
[ i
*6+1 ] = 0.0f
;
437 v2_copy( v0
, circle_mesh
+ 32*6 + i
*12 );
438 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+2 );
439 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+4 );
441 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+6 );
442 v2_muls( v1
, 0.8f
, circle_mesh
+ 32*6 + i
*12+8 );
443 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+10 );
445 v2_copy( v0
, circle_mesh
+ i
*6+4 );
446 v2_copy( v1
, circle_mesh
+ i
*6+2 );
447 v2_copy( v0
, circle_mesh
+i
*6+4 );
448 v2_copy( v1
, circle_mesh
+i
*6+2 );
451 init_mesh( &world
.circle
, circle_mesh
, vg_list_size( circle_mesh
) );
458 #include "models/splitter_l.obj.h"
462 #include "models/splitter_r.obj.h"
465 init_mesh( &world
.splitter_l
, splitter_l
, vg_list_size( splitter_l
) );
466 init_mesh( &world
.splitter_r
, splitter_r
, vg_list_size( splitter_r
) );
471 tex_tile_detail
= vg_tex2d_rgba( "textures/tile_overlays.png" );
473 vg_tex2d_linear_mipmap();
476 tex_tile_data
= vg_tex2d_rgba( "textures/tileset.png" );
478 vg_tex2d_linear_mipmap();
481 tex_wood
= vg_tex2d_rgba( "textures/wood.png" );
483 vg_tex2d_linear_mipmap();
487 map_load( level_pack
[ 0 ] );
492 free_mesh( &world
.tile
);
493 free_mesh( &world
.circle
);
494 free_mesh( &world
.splitter_l
);
495 free_mesh( &world
.splitter_r
);
499 glDeleteTextures( 1, &tex_tile_data
);
500 glDeleteTextures( 1, &tex_tile_detail
);
501 glDeleteTextures( 1, &tex_wood
);
504 static int cell_interactive( v2i co
)
507 if( co
[0] < 2 || co
[0] >= world
.w
-2 || co
[1] < 2 || co
[1] >= world
.h
-2 )
511 if( world
.data
[ world
.w
*co
[1] + co
[0] ].state
& (FLAG_WALL
|FLAG_INPUT
|FLAG_OUTPUT
) )
514 // List of 3x3 configurations that we do not allow
515 static u32 invalid_src
[][9] =
549 // Statically compile invalid configurations into bitmasks
550 static u32 invalid
[ vg_list_size(invalid_src
) ];
552 for( int i
= 0; i
< vg_list_size(invalid_src
); i
++ )
556 for( int j
= 0; j
< 3; j
++ )
557 for( int k
= 0; k
< 3; k
++ )
558 comped
|= invalid_src
[i
][ j
*3+k
] << ((j
*5)+k
);
563 // Extract 5x5 grid surrounding tile
565 for( int y
= co
[1]-2; y
< co
[1]+3; y
++ )
566 for( int x
= co
[0]-2; x
< co
[0]+3; x
++ )
568 struct cell
*cell
= pcell((v2i
){x
,y
});
570 if( cell
&& (cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
)) )
571 blob
|= 0x1 << ((y
-(co
[1]-2))*5 + x
-(co
[0]-2));
574 // Run filter over center 3x3 grid to check for invalid configurations
575 int kernel
[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
576 for( int i
= 0; i
< vg_list_size(kernel
); i
++ )
578 if( blob
& (0x1 << (6+kernel
[i
])) )
580 u32 window
= blob
>> kernel
[i
];
582 for( int j
= 0; j
< vg_list_size(invalid
); j
++ )
583 if((window
& invalid
[j
]) == invalid
[j
])
593 static int curlevel
= 0;
594 int changelvl
= curlevel
;
595 if( vg_get_button_down( "prev" ) ) { if( curlevel
> 0 ) changelvl
--; }
596 else if( vg_get_button_down( "next" ) ) { if( curlevel
< vg_list_size( level_pack
)-1 ) changelvl
++; }
598 if( changelvl
!= curlevel
)
600 map_load( level_pack
[ changelvl
] );
601 curlevel
= changelvl
;
604 world
.simulating
= 0;
605 world
.num_fishes
= 0;
608 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
609 world
.io
[i
].recv_count
= 0;
611 vg_info( "Stopping simulation!\n" );
614 float ratio
= (float)vg_window_y
/ (float)vg_window_x
;
615 float const size
= 9.5f
;
618 origin
[0] = -0.5f
* world
.w
;
619 origin
[1] = -0.5f
* world
.h
;
622 m3x3_projection( m_projection
, -size
, size
, -size
*ratio
, size
*ratio
);
623 m3x3_identity( m_view
);
624 m3x3_translate( m_view
, origin
);
625 m3x3_mul( m_projection
, m_view
, vg_pv
);
626 vg_projection_update();
631 v2_copy( vg_mouse_ws
, tile_pos
);
633 int tile_x
= floorf( tile_pos
[0] );
634 int tile_y
= floorf( tile_pos
[1] );
637 if( !world
.simulating
)
639 if( cell_interactive( (v2i
){ tile_x
, tile_y
} ))
641 world
.selected
= tile_y
* world
.w
+ tile_x
;
643 if( vg_get_button_down("primary") )
645 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
652 // Simulation stop/start
653 if( vg_get_button_down("go") )
655 if( world
.simulating
)
657 world
.simulating
= 0;
658 world
.num_fishes
= 0;
661 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
662 world
.io
[i
].recv_count
= 0;
664 vg_info( "Stopping simulation!\n" );
668 vg_success( "Starting simulation!\n" );
670 world
.simulating
= 1;
671 world
.num_fishes
= 0;
673 world
.sim_start
= vg_time
;
675 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
677 world
.data
[ i
].state
&= ~FLAG_FLIP_FLOP
;
680 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
681 world
.io
[i
].recv_count
= 0;
686 // ========================================================
688 for( int y
= 2; y
< world
.h
-2; y
++ )
690 for( int x
= 2; x
< world
.w
-2; x
++ )
692 v2i dirs
[] = {{1,0},{0,1},{-1,0},{0,-1}};
696 if( pcell((v2i
){x
,y
})->state
& FLAG_CANAL
)
698 for( int i
= 0; i
< vg_list_size( dirs
); i
++ )
700 struct cell
*neighbour
= pcell((v2i
){x
+dirs
[i
][0], y
+dirs
[i
][1]});
701 if( neighbour
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
706 pcell((v2i
){x
,y
})->config
= config
;
707 pcell((v2i
){x
,y
})->state
&= ~(FLAG_DROP_L
|FLAG_DROP_R
|FLAG_SPLIT
|FLAG_MERGER
);
711 for( int y
= 2; y
< world
.h
-2; y
++ )
712 for( int x
= 2; x
< world
.w
-2; x
++ )
714 // R,D,L,- 1110 (splitter, 1 drop created)
716 // R,-,L,U - 1011 (merger, 2 drop created)
718 u8 config
= pcell((v2i
){x
,y
})->config
;
720 if( config
== 0x7 ) // splitter
722 world
.data
[y
*world
.w
+x
].state
|= (FLAG_SPLIT
| FLAG_DROP_L
| FLAG_DROP_R
);
724 else if( config
== 0xD )
726 world
.data
[y
*world
.w
+x
-1].state
|= FLAG_DROP_R
;
727 world
.data
[y
*world
.w
+x
+1].state
|= FLAG_DROP_L
;
728 world
.data
[y
*world
.w
+x
].state
|= FLAG_MERGER
;
733 if( world
.simulating
)
735 while( world
.sim_frame
< (int)((vg_time
-world
.sim_start
)*2.0f
) )
737 vg_info( "frame: %u\n", world
.sim_frame
);
739 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
741 struct cell_terminal
*term
= &world
.io
[ i
];
742 int posx
= term
->id
% world
.w
;
743 int posy
= (term
->id
- posx
)/world
.w
;
744 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
748 if( world
.sim_frame
< arrlen( term
->conditions
) )
750 struct fish
*fish
= &world
.fishes
[world
.num_fishes
++];
754 fish
->payload
= term
->conditions
[world
.sim_frame
];
758 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
759 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
760 if( pcell( (v2i
){ posx
+dirs
[j
][0], posy
+dirs
[j
][1] } )->state
& FLAG_CANAL
)
762 fish
->dir
[0] = dirs
[j
][0];
763 fish
->dir
[1] = dirs
[j
][1];
774 for( int i
= 0; i
< world
.num_fishes
; i
++ )
776 struct fish
*fish
= &world
.fishes
[i
];
777 struct cell
*cell_current
= pcell( fish
->pos
);
783 if( cell_current
->state
& FLAG_OUTPUT
)
785 for( int j
= 0; j
< arrlen( world
.io
); j
++ )
787 struct cell_terminal
*term
= &world
.io
[j
];
789 if( term
->id
== fish
->pos
[1]*world
.w
+ fish
->pos
[0] )
791 term
->recv
[ term
->recv_count
++ ] = fish
->payload
;
800 if( !(cell_current
->state
& (FLAG_INPUT
|FLAG_CANAL
)) )
806 if( cell_current
->state
& FLAG_SPLIT
)
809 fish
->dir
[0] = cell_current
->state
&FLAG_FLIP_FLOP
?1:-1;
812 cell_current
->state
^= FLAG_FLIP_FLOP
;
814 else if( cell_current
->state
& FLAG_MERGER
)
822 struct cell
*cell_next
= pcell( (v2i
){ fish
->pos
[0]+fish
->dir
[0], fish
->pos
[1]+fish
->dir
[1] } );
823 if( !(cell_next
->state
& (FLAG_CANAL
|FLAG_OUTPUT
)) )
825 // Try other directions for valid, so down, left, right..
826 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
827 vg_info( "Trying some other directions...\n" );
829 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
831 if( (dirs
[j
][0] == -fish
->dir
[0]) && (dirs
[j
][1] == -fish
->dir
[1]) )
834 if( pcell( (v2i
){ fish
->pos
[0]+dirs
[j
][0], fish
->pos
[1]+dirs
[j
][1] } )->state
& (FLAG_CANAL
|FLAG_OUTPUT
) )
836 fish
->dir
[0] = dirs
[j
][0];
837 fish
->dir
[1] = dirs
[j
][1];
843 fish
->pos
[0] += fish
->dir
[0];
844 fish
->pos
[1] += fish
->dir
[1];
855 glViewport( 0,0, vg_window_x
, vg_window_y
);
857 glDisable( GL_DEPTH_TEST
);
858 glClearColor( 0.8f
, 0.8f
, 0.8f
, 1.0f
);
859 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
863 glUniform4f( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 0.5f, 0.5f, 0.5f, 1.0f );
864 for( int y = 0; y < world.h; y ++ )
865 for( int x = 0; x < world.w; x ++ )
867 struct cell *cell = pcell((v2i){x,y});
869 if( cell->state & FLAG_CANAL )
874 glUniform3f( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), (float)x - 0.2f, (float)y - 0.15f, 1.0f );
878 for( int y = 0; y < world.h; y ++ )
880 for( int x = 0; x < world.w; x ++ )
882 struct cell *cell = pcell((v2i){x,y});
883 int selected = world.selected == y*world.w + x;
885 if( cell->state & FLAG_SPLIT )
887 glUniform4f( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 0.9f, 0.9f, 0.9f, 1.0f );
888 glUniform3f( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), (float)x, (float)y, 1.0f );
890 struct mesh *splitter = cell->state & FLAG_FLIP_FLOP? &world.splitter_r: &world.splitter_l;
892 use_mesh( splitter );
893 draw_mesh( 0, splitter->elements );
894 use_mesh( &world.tile );
897 if( (cell->state & FLAG_CANAL) && !selected )
900 glUniform3f( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), (float)x, (float)y, 1.0f );
904 if( cell->state & FLAG_WALL ) { v4_copy( (v4f){ 0.2f, 0.2f, 0.2f, 1.0f }, colour ); }
905 else if( cell->state & FLAG_CANAL ) { v4_copy( (v4f){ 0.6f, 0.6f, 0.6f, 1.0f }, colour ); }
906 else if( cell->state & FLAG_INPUT ) { v4_copy( (v4f){ 0.5f, 0.5f, 0.5f, 1.0f }, colour ); }
907 else if( cell->state & FLAG_OUTPUT ) { v4_copy( (v4f){ 0.4f, 0.4f, 0.4f, 1.0f }, colour ); }
908 else v4_copy( (v4f){ 0.9f, 0.9f, 0.9f, 1.0f }, colour );
910 //if( cell->water[world.frame&0x1] )
911 // v4_copy( (v4f){ 0.2f, 0.3f, 0.7f * (float)(cell->water[world.frame&0x1]) * (1.0f/16.0f), 1.0f }, colour );
914 v3_muls( colour, sinf( vg_time )*0.25f + 0.5f, colour );
916 //if( cell->state & (FLAG_SPLIT) )
917 // v4_copy( (v4f){ 0.75f, 0.75f, 0.02f, 1.0f }, colour );
918 //if( cell->state & (FLAG_MERGER) )
919 // v4_copy( (v4f){ 0.75f, 0.02f, 0.75f, 1.0f }, colour );
921 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, colour );
929 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
930 glBlendEquation(GL_FUNC_ADD
);
932 use_mesh( &world
.tile
);
933 SHADER_USE( shader_tile_main
);
934 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_main
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
937 glActiveTexture( GL_TEXTURE0
);
938 glBindTexture( GL_TEXTURE_2D
, tex_tile_data
);
939 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
941 glActiveTexture( GL_TEXTURE1
);
942 glBindTexture( GL_TEXTURE_2D
, tex_wood
);
943 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
945 for( int y
= 0; y
< world
.h
; y
++ )
947 for( int x
= 0; x
< world
.w
; x
++ )
949 struct cell
*cell
= pcell((v2i
){x
,y
});
950 int selected
= world
.selected
== y
*world
.w
+ x
;
953 0000 0 | 0001 1 | 0010 2 | 0011 3
957 0100 4 | 0101 5 | 0110 6 | 0111 7
961 1000 8 | 1001 9 | 1010 10 | 1011 11
965 1100 12 | 1101 13 | 1110 14 | 1111 15
971 int tile_offsets
[][2] =
973 {2, 0}, {0, 3}, {0, 2}, {2, 2},
974 {1, 0}, {2, 3}, {3, 2}, {1, 3},
975 {3, 1}, {0, 1}, {1, 2}, {2, 1},
976 {1, 1}, {3, 3}, {2, 1}, {2, 1}
979 int uv
[2] = { 3, 0 };
981 if( cell
->state
& FLAG_CANAL
)
983 uv
[0] = tile_offsets
[ cell
->config
][0];
984 uv
[1] = tile_offsets
[ cell
->config
][1];
987 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
, uv
[0], uv
[1] ); // TODO: PICK GLYPH
992 SHADER_USE( shader_tile_colour
);
993 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
999 use_mesh( &world
.circle
);
1002 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1004 struct cell_terminal
*term
= &world
.io
[ i
];
1005 int posx
= term
->id
% world
.w
;
1006 int posy
= (term
->id
- posx
)/world
.w
;
1007 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1009 int const filled_start
= 0;
1010 int const filled_count
= 32;
1011 int const empty_start
= 32;
1012 int const empty_count
= 32*2;
1014 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
1016 for( int j
= 0; j
< arrlen( term
->conditions
); j
++ )
1018 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)posx
+ 0.2f
+ 0.2f
* (float)j
, (float)posy
+ 0.2f
, 0.1f
);
1022 colour_code_v3( term
->conditions
[j
], dot_colour
);
1023 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1025 // Draw filled if tick not passed, draw empty if empty
1026 if( world
.sim_frame
> j
)
1027 draw_mesh( empty_start
, empty_count
);
1029 draw_mesh( filled_start
, filled_count
);
1033 if( term
->recv_count
> j
)
1035 colour_code_v3( term
->recv
[j
], dot_colour
);
1036 v3_muls( dot_colour
, 0.8f
, dot_colour
);
1037 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1039 draw_mesh( filled_start
, filled_count
);
1042 colour_code_v3( term
->conditions
[j
], dot_colour
);
1043 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1045 draw_mesh( empty_start
, empty_count
);
1051 if( world
.simulating
)
1053 float scaled_time
= (vg_time
-world
.sim_start
)*2.0f
;
1054 float lerp
= scaled_time
- (float)world
.sim_frame
;
1056 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
1058 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1060 struct fish
*fish
= &world
.fishes
[i
];
1065 colour_code_v3( fish
->payload
, dot_colour
);
1066 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1068 // Evaluate position
1069 struct cell
*cell
= pcell(fish
->pos
);
1072 v2f
const curve_3
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
1073 v2f
const curve_6
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.375f
,0.5f
},{0.0f
,0.5f
}};
1074 v2f
const curve_9
[] = {{1.0f
,0.5f
},{0.625f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
1075 v2f
const curve_12
[]= {{0.0f
,0.5f
},{0.375f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
1077 v2f
const curve_7
[] = {{0.5f
,0.8438f
},{0.875f
,0.8438f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
1078 v2f
const curve_7_1
[] = {{0.5f
,0.8438f
},{1.0f
-0.875f
,0.8438f
},{1.0-0.625f
,0.5f
},{0.0f
,0.5f
}};
1080 float const linear_section
= 0.1562f
;
1086 switch( cell
->config
)
1089 if( fish
->dir
[0] == 1 )
1094 case 3: curve
= curve_3
; break;
1095 case 6: curve
= curve_6
; break;
1096 case 9: curve
= curve_9
; break;
1097 case 12: curve
= curve_12
; break;
1099 if( t
> linear_section
)
1101 t
-= linear_section
;
1102 t
*= (1.0f
/(1.0f
-linear_section
));
1104 curve
= cell
->state
& FLAG_FLIP_FLOP
? curve_7
: curve_7_1
;
1108 default: curve
= NULL
; break;
1115 float t3
= t
* t
* t
;
1117 float cA
= 3.0f
*t2
- 3.0f
*t3
;
1118 float cB
= 3.0f
*t3
- 6.0f
*t2
+ 3.0f
*t
;
1119 float cC
= 3.0f
*t2
- t3
- 3.0f
*t
+ 1.0f
;
1121 fish_pos
[0] = t3
*curve
[3][0] + cA
*curve
[2][0] + cB
*curve
[1][0] + cC
*curve
[0][0];
1122 fish_pos
[1] = t3
*curve
[3][1] + cA
*curve
[2][1] + cB
*curve
[1][1] + cC
*curve
[0][1];
1123 fish_pos
[0] += (float)fish
->pos
[0];
1124 fish_pos
[1] += (float)fish
->pos
[1];
1129 origin
[0] = (float)fish
->pos
[0] + (float)fish
->dir
[0]*-0.5f
+ 0.5f
;
1130 origin
[1] = (float)fish
->pos
[1] + (float)fish
->dir
[1]*-0.5f
+ 0.5f
;
1132 fish_pos
[0] = origin
[0] + (float)fish
->dir
[0]*t
;
1133 fish_pos
[1] = origin
[1] + (float)fish
->dir
[1]*t
;
1136 vg_line_box( (v2f
){fish
->pos
[0],fish
->pos
[1]},
1137 (v2f
){ (float)fish
->pos
[0]+1.0f
, (float)fish
->pos
[1]+1.0f
}, 0xffffffff );
1139 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), fish_pos
[0], fish_pos
[1], 0.125f
);
1144 if( world
.simulating
)
1146 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.0f
, 0.0f
, 0.0f
, 1.0f
);
1147 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), -0.5f
+ cosf( vg_time
* 4.0f
) * 0.2f
, sinf( vg_time
* 4.0f
) * 0.2f
+ (float)world
.h
* 0.5f
, 0.05f
);