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
35 "uniform mat2 uSubTransform;"
37 "out vec4 aTexCoords;"
41 "vec3 p3 = fract(vec3(p.xyx) * vec3(.1031, .1030, .0973));"
42 "p3 += dot(p3, p3.yzx+33.33);"
43 "return fract((p3.xx+p3.yz)*p3.zy);"
48 // Create texture coords
49 "vec2 random_offset = floor(hash22(uOffset.xy) * 4.0) * 0.25;"
50 "vec2 edge_safe_coords = a_co * 0.98 + 0.01;"
51 "aTexCoords = vec4((edge_safe_coords + uOffset.zw) * 0.25, edge_safe_coords * 0.25 + random_offset );"
54 "vec2 subtransform = uSubTransform * (a_co-0.5) + 0.5;"
55 "vec3 worldpos = vec3( subtransform + uOffset.xy, 1.0 );"
56 "gl_Position = vec4( uPv * worldpos, 1.0 );"
62 "uniform sampler2D uTexGlyphs;"
63 "uniform sampler2D uTexWood;"
69 "vec3 shadowing_colour = vec3( 0.93, 0.88536, 0.8184 );"
70 "vec4 glyph = texture( uTexGlyphs, aTexCoords.xy );"
71 "vec4 wood = texture( uTexWood, aTexCoords.zw );"
72 "vec4 wood_secondary = texture( uTexWood, aTexCoords.zw + 0.25 );"
73 "vec3 wood_comp = mix( wood_secondary.rgb * shadowing_colour, wood.rgb, clamp( glyph.b * 2.0 - 1.0, 0.0, 1.0 ) );"
75 "vec3 shadows = mix( vec3( 0.85, 0.7344, 0.561 ), vec3(1.0,1.0,1.0), glyph.r );"
77 "FragColor = vec4( wood_comp * shadows, glyph.b );"
80 UNIFORMS({ "uPv", "uOffset", "uTexGlyphs", "uTexWood", "uSubTransform" })
83 const char *level_pack
[] =
108 "#####-#####;bbbbb\n"
113 "###+##+####;bbb,bb\n"
118 "###-#####-###;a,aaa\n"
125 "######+######;aaaa\n"
130 "###-#####-###;aaa,aa\n"
137 "###+#####+###;aa,aaa\n"
141 GLuint tex_tile_data
;
142 GLuint tex_tile_detail
;
149 #define FLAG_INPUT 0x1
150 #define FLAG_OUTPUT 0x2
151 #define FLAG_CANAL 0x4
152 #define FLAG_WALL 0x8
153 #define FLAG_DROP_L 0x10
154 #define FLAG_SPLIT 0x20
155 #define FLAG_MERGER 0x40
156 #define FLAG_DROP_R 0x80
157 #define FLAG_FLIP_FLOP 0x100
158 #define FLAG_FLIP_ROTATING 0x200
161 0000 0 | 0001 1 | 0010 2 | 0011 3
165 0100 4 | 0101 5 | 0110 6 | 0111 7
169 1000 8 | 1001 9 | 1010 10 | 1011 11
173 1100 12 | 1101 13 | 1110 14 | 1111 15
181 k_cell_type_split
= 7,
182 k_cell_type_merge
= 13
186 { { 0.9f
, 0.2f
, 0.01f
},
187 { 0.2f
, 0.9f
, 0.14f
},
188 { 0.1f
, 0.3f
, 0.85f
} };
190 static void colour_code_v3( char cc
, v3f target
)
192 if( cc
>= 'a' && cc
<= 'z' )
196 if( id
< vg_list_size( colour_sets
) )
198 v3_copy( colour_sets
[ id
], target
);
203 v3_copy( (v3f
){0.0f
,0.0f
,0.0f
}, target
);
212 static void init_mesh( struct mesh
*m
, float *tris
, u32 length
)
214 m
->elements
= length
/3;
215 glGenVertexArrays( 1, &m
->vao
);
216 glGenBuffers( 1, &m
->vbo
);
218 glBindVertexArray( m
->vao
);
219 glBindBuffer( GL_ARRAY_BUFFER
, m
->vbo
);
220 glBufferData( GL_ARRAY_BUFFER
, length
*sizeof(float), tris
, GL_STATIC_DRAW
);
222 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2 * sizeof(float), (void*)0 );
223 glEnableVertexAttribArray( 0 );
228 static void free_mesh( struct mesh
*m
)
230 glDeleteVertexArrays( 1, &m
->vao
);
231 glDeleteBuffers( 1, &m
->vbo
);
234 static void draw_mesh( int const start
, int const count
)
236 glDrawArrays( GL_TRIANGLES
, start
*3, count
*3 );
239 static void use_mesh( struct mesh
*m
)
241 glBindVertexArray( m
->vao
);
261 // TODO: Split into input/output structures
271 struct mesh tile
, circle
;
287 static void map_free(void)
289 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
290 arrfree( world
.io
[ i
].conditions
);
292 arrfree( world
.data
);
301 static int map_load( const char *str
)
310 if( str
[world
.w
] == ';' )
312 else if( !str
[world
.w
] )
314 vg_error( "Unexpected EOF when parsing level\n" );
319 struct cell
*row
= arraddnptr( world
.data
, world
.w
);
321 int reg_start
= 0, reg_end
= 0;
337 if( reg_start
< reg_end
)
339 if( *c
>= 'a' && *c
<= 'z' )
341 arrpush( world
.io
[ reg_start
].conditions
, *c
);
345 if( *c
== ',' || *c
== '\n' )
354 vg_error( "Unkown attribute '%c' (row: %u)\n", *c
, world
.h
);
361 vg_error( "Too many values to assign (row: %u)\n", world
.h
);
369 if( reg_start
!= reg_end
)
371 vg_error( "Not enough values assigned (row: %u, %u of %u)\n", world
.h
, reg_start
, reg_end
);
377 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world
.h
, cx
, world
.w
);
381 row
= arraddnptr( world
.data
, world
.w
);
384 reg_end
= reg_start
= arrlen( world
.io
);
390 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world
.h
, cx
, world
.w
);
394 // Tile initialization
397 if( *c
== '+' || *c
== '-' )
399 struct cell_terminal term
= { .id
= cx
+ world
.h
*world
.w
};
400 arrpush( world
.io
, term
);
401 row
[ cx
++ ].state
= *c
== '+'? FLAG_INPUT
: FLAG_OUTPUT
;
406 row
[ cx
++ ].state
= FLAG_WALL
;
410 row
[ cx
++ ].state
= 0x00;
417 vg_success( "Map loaded! (%u:%u)\n", world
.w
, world
.h
);
421 static struct cell
*pcell( v2i pos
)
423 return &world
.data
[ pos
[1]*world
.w
+ pos
[0] ];
426 int main( int argc
, char *argv
[] )
428 vg_init( argc
, argv
, "Fish (Marbles Computer) Ladder Simulator 2022 | N,M: change level | SPACE: Test | LeftClick: Toggle tile" );
431 void vg_register(void)
433 SHADER_INIT( shader_tile_colour
);
434 SHADER_INIT( shader_tile_main
);
443 0.0f
, 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
,
444 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
, 0.0f
,
446 0.48f
, 0.48f
, 0.5f
, 0.52f
, 0.52f
, 0.52f
, // Static dot
447 0.375f
, 0.25f
, 0.5f
, 0.75f
, 0.625f
, 0.25f
, // Downwards pointing arrow
448 0.25f
, 0.625f
, 0.75f
, 0.5f
, 0.25f
, 0.375f
, // Left
449 0.625f
, 0.75f
, 0.5f
, 0.25f
, 0.375f
, 0.75f
, // up
450 0.75f
, 0.375f
, 0.25f
, 0.5f
, 0.75f
, 0.625f
453 init_mesh( &world
.tile
, quad_mesh
, vg_list_size(quad_mesh
) );
458 float circle_mesh
[32*6*3];
459 int res
= vg_list_size( circle_mesh
) / (6*3);
461 for( int i
= 0; i
< res
; i
++ )
463 v2f v0
= { sinf( ((float)i
/(float)res
)*VG_TAUf
), cosf( ((float)i
/(float)res
)*VG_TAUf
) };
464 v2f v1
= { sinf( ((float)(i
+1)/(float)res
)*VG_TAUf
), cosf( ((float)(i
+1)/(float)res
)*VG_TAUf
) };
466 circle_mesh
[ i
*6+0 ] = 0.0f
;
467 circle_mesh
[ i
*6+1 ] = 0.0f
;
469 v2_copy( v0
, circle_mesh
+ 32*6 + i
*12 );
470 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+2 );
471 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+4 );
473 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+6 );
474 v2_muls( v1
, 0.8f
, circle_mesh
+ 32*6 + i
*12+8 );
475 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+10 );
477 v2_copy( v0
, circle_mesh
+ i
*6+4 );
478 v2_copy( v1
, circle_mesh
+ i
*6+2 );
479 v2_copy( v0
, circle_mesh
+i
*6+4 );
480 v2_copy( v1
, circle_mesh
+i
*6+2 );
483 init_mesh( &world
.circle
, circle_mesh
, vg_list_size( circle_mesh
) );
488 tex_tile_detail
= vg_tex2d_rgba( "textures/tile_overlays.png" );
490 vg_tex2d_linear_mipmap();
493 tex_tile_data
= vg_tex2d_rgba( "textures/tileset.png" );
495 vg_tex2d_linear_mipmap();
498 tex_wood
= vg_tex2d_rgba( "textures/wood.png" );
500 vg_tex2d_linear_mipmap();
504 map_load( level_pack
[ 0 ] );
509 free_mesh( &world
.tile
);
510 free_mesh( &world
.circle
);
514 glDeleteTextures( 1, &tex_tile_data
);
515 glDeleteTextures( 1, &tex_tile_detail
);
516 glDeleteTextures( 1, &tex_wood
);
519 static int cell_interactive( v2i co
)
522 if( co
[0] < 2 || co
[0] >= world
.w
-2 || co
[1] < 2 || co
[1] >= world
.h
-2 )
526 if( world
.data
[ world
.w
*co
[1] + co
[0] ].state
& (FLAG_WALL
|FLAG_INPUT
|FLAG_OUTPUT
) )
529 // List of 3x3 configurations that we do not allow
530 static u32 invalid_src
[][9] =
562 // Statically compile invalid configurations into bitmasks
563 static u32 invalid
[ vg_list_size(invalid_src
) ];
565 for( int i
= 0; i
< vg_list_size(invalid_src
); i
++ )
569 for( int j
= 0; j
< 3; j
++ )
570 for( int k
= 0; k
< 3; k
++ )
571 comped
|= invalid_src
[i
][ j
*3+k
] << ((j
*5)+k
);
576 // Extract 5x5 grid surrounding tile
578 for( int y
= co
[1]-2; y
< co
[1]+3; y
++ )
579 for( int x
= co
[0]-2; x
< co
[0]+3; x
++ )
581 struct cell
*cell
= pcell((v2i
){x
,y
});
583 if( cell
&& (cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
)) )
584 blob
|= 0x1 << ((y
-(co
[1]-2))*5 + x
-(co
[0]-2));
587 // Run filter over center 3x3 grid to check for invalid configurations
588 int kernel
[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
589 for( int i
= 0; i
< vg_list_size(kernel
); i
++ )
591 if( blob
& (0x1 << (6+kernel
[i
])) )
593 u32 window
= blob
>> kernel
[i
];
595 for( int j
= 0; j
< vg_list_size(invalid
); j
++ )
596 if((window
& invalid
[j
]) == invalid
[j
])
606 static int curlevel
= 0;
607 int changelvl
= curlevel
;
608 if( vg_get_button_down( "prev" ) ) { if( curlevel
> 0 ) changelvl
--; }
609 else if( vg_get_button_down( "next" ) ) { if( curlevel
< vg_list_size( level_pack
)-1 ) changelvl
++; }
611 if( changelvl
!= curlevel
)
613 map_load( level_pack
[ changelvl
] );
614 curlevel
= changelvl
;
617 world
.simulating
= 0;
618 world
.num_fishes
= 0;
621 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
622 world
.io
[i
].recv_count
= 0;
624 vg_info( "Stopping simulation!\n" );
627 float ratio
= (float)vg_window_y
/ (float)vg_window_x
;
628 float const size
= 9.5f
;
631 origin
[0] = -0.5f
* world
.w
;
632 origin
[1] = -0.5f
* world
.h
;
635 m3x3_projection( m_projection
, -size
, size
, -size
*ratio
, size
*ratio
);
636 m3x3_identity( m_view
);
637 m3x3_translate( m_view
, origin
);
638 m3x3_mul( m_projection
, m_view
, vg_pv
);
639 vg_projection_update();
644 v2_copy( vg_mouse_ws
, tile_pos
);
646 int tile_x
= floorf( tile_pos
[0] );
647 int tile_y
= floorf( tile_pos
[1] );
650 if( !world
.simulating
)
652 if( cell_interactive( (v2i
){ tile_x
, tile_y
} ))
654 world
.selected
= tile_y
* world
.w
+ tile_x
;
656 if( vg_get_button_down("primary") )
658 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
665 // Simulation stop/start
666 if( vg_get_button_down("go") )
668 if( world
.simulating
)
670 world
.simulating
= 0;
671 world
.num_fishes
= 0;
674 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
675 world
.io
[i
].recv_count
= 0;
677 vg_info( "Stopping simulation!\n" );
681 vg_success( "Starting simulation!\n" );
683 world
.simulating
= 1;
684 world
.num_fishes
= 0;
686 world
.sim_start
= vg_time
;
688 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
690 world
.data
[ i
].state
&= ~FLAG_FLIP_FLOP
;
693 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
694 world
.io
[i
].recv_count
= 0;
699 // ========================================================
701 for( int y
= 2; y
< world
.h
-2; y
++ )
703 for( int x
= 2; x
< world
.w
-2; x
++ )
705 v2i dirs
[] = {{1,0},{0,1},{-1,0},{0,-1}};
709 if( pcell((v2i
){x
,y
})->state
& FLAG_CANAL
)
711 for( int i
= 0; i
< vg_list_size( dirs
); i
++ )
713 struct cell
*neighbour
= pcell((v2i
){x
+dirs
[i
][0], y
+dirs
[i
][1]});
714 if( neighbour
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
719 pcell((v2i
){x
,y
})->config
= config
;
720 pcell((v2i
){x
,y
})->state
&= ~(FLAG_DROP_L
|FLAG_DROP_R
|FLAG_SPLIT
|FLAG_MERGER
);
724 for( int y
= 2; y
< world
.h
-2; y
++ )
725 for( int x
= 2; x
< world
.w
-2; x
++ )
727 // R,D,L,- 1110 (splitter, 1 drop created)
729 // R,-,L,U - 1011 (merger, 2 drop created)
731 u8 config
= pcell((v2i
){x
,y
})->config
;
733 if( config
== k_cell_type_split
) // splitter
735 struct cell
*cell
= pcell((v2i
){x
,y
});
737 cell
->state
|= (FLAG_SPLIT
| FLAG_DROP_L
| FLAG_DROP_R
);
739 else if( config
== k_cell_type_merge
)
741 world
.data
[y
*world
.w
+x
-1].state
|= FLAG_DROP_R
;
742 world
.data
[y
*world
.w
+x
+1].state
|= FLAG_DROP_L
;
743 world
.data
[y
*world
.w
+x
].state
|= FLAG_MERGER
;
748 if( world
.simulating
)
750 while( world
.sim_frame
< (int)((vg_time
-world
.sim_start
)*2.0f
) )
752 vg_info( "frame: %u\n", world
.sim_frame
);
754 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
756 struct cell_terminal
*term
= &world
.io
[ i
];
757 int posx
= term
->id
% world
.w
;
758 int posy
= (term
->id
- posx
)/world
.w
;
759 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
763 if( world
.sim_frame
< arrlen( term
->conditions
) )
765 struct fish
*fish
= &world
.fishes
[world
.num_fishes
++];
769 fish
->payload
= term
->conditions
[world
.sim_frame
];
773 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
774 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
775 if( pcell( (v2i
){ posx
+dirs
[j
][0], posy
+dirs
[j
][1] } )->state
& FLAG_CANAL
)
777 fish
->dir
[0] = dirs
[j
][0];
778 fish
->dir
[1] = dirs
[j
][1];
789 // Update splitter deltas
790 for( int i
= 0; i
< world
.h
*world
.w
; i
++ )
792 struct cell
*cell
= &world
.data
[i
];
793 if( cell
->config
== k_cell_type_split
)
795 cell
->state
&= ~FLAG_FLIP_ROTATING
;
799 for( int i
= 0; i
< world
.num_fishes
; i
++ )
801 struct fish
*fish
= &world
.fishes
[i
];
802 struct cell
*cell_current
= pcell( fish
->pos
);
808 if( cell_current
->state
& FLAG_OUTPUT
)
810 for( int j
= 0; j
< arrlen( world
.io
); j
++ )
812 struct cell_terminal
*term
= &world
.io
[j
];
814 if( term
->id
== fish
->pos
[1]*world
.w
+ fish
->pos
[0] )
816 term
->recv
[ term
->recv_count
++ ] = fish
->payload
;
825 if( !(cell_current
->state
& (FLAG_INPUT
|FLAG_CANAL
)) )
831 if( cell_current
->state
& FLAG_SPLIT
)
834 fish
->dir
[0] = cell_current
->state
&FLAG_FLIP_FLOP
?1:-1;
837 cell_current
->state
^= FLAG_FLIP_FLOP
;
839 else if( cell_current
->state
& FLAG_MERGER
)
847 struct cell
*cell_next
= pcell( (v2i
){ fish
->pos
[0]+fish
->dir
[0], fish
->pos
[1]+fish
->dir
[1] } );
848 if( !(cell_next
->state
& (FLAG_CANAL
|FLAG_OUTPUT
)) )
850 // Try other directions for valid, so down, left, right..
851 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
852 vg_info( "Trying some other directions...\n" );
854 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
856 if( (dirs
[j
][0] == -fish
->dir
[0]) && (dirs
[j
][1] == -fish
->dir
[1]) )
859 if( pcell( (v2i
){ fish
->pos
[0]+dirs
[j
][0], fish
->pos
[1]+dirs
[j
][1] } )->state
& (FLAG_CANAL
|FLAG_OUTPUT
) )
861 fish
->dir
[0] = dirs
[j
][0];
862 fish
->dir
[1] = dirs
[j
][1];
868 fish
->pos
[0] += fish
->dir
[0];
869 fish
->pos
[1] += fish
->dir
[1];
871 struct cell
*cell_entry
= pcell( fish
->pos
);
873 if( cell_entry
->config
== k_cell_type_split
)
874 cell_entry
->state
|= FLAG_FLIP_ROTATING
;
885 glViewport( 0,0, vg_window_x
, vg_window_y
);
887 glDisable( GL_DEPTH_TEST
);
888 glClearColor( 0.8f
, 0.8f
, 0.8f
, 1.0f
);
889 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
891 float scaled_time
= 0.0f
, frame_lerp
= 0.0f
;
893 if( world
.simulating
)
895 scaled_time
= (vg_time
-world
.sim_start
)*2.0f
;
896 frame_lerp
= scaled_time
- (float)world
.sim_frame
;
899 v2f
const curve_3
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
900 v2f
const curve_6
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.375f
,0.5f
},{0.0f
,0.5f
}};
901 v2f
const curve_9
[] = {{1.0f
,0.5f
},{0.625f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
902 v2f
const curve_12
[]= {{0.0f
,0.5f
},{0.375f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
904 v2f
const curve_7
[] = {{0.5f
,0.8438f
},{0.875f
,0.8438f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
905 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
}};
907 float const curve_7_linear_section
= 0.1562f
;
909 // TILE SET RENDERING
910 // ======================================================================
911 use_mesh( &world
.tile
);
912 SHADER_USE( shader_tile_main
);
915 m2x2_identity( subtransform
);
916 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
917 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_main
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
920 glActiveTexture( GL_TEXTURE0
);
921 glBindTexture( GL_TEXTURE_2D
, tex_tile_data
);
922 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
924 glActiveTexture( GL_TEXTURE1
);
925 glBindTexture( GL_TEXTURE_2D
, tex_wood
);
926 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
928 for( int y
= 0; y
< world
.h
; y
++ )
930 for( int x
= 0; x
< world
.w
; x
++ )
932 struct cell
*cell
= pcell((v2i
){x
,y
});
933 int selected
= world
.selected
== y
*world
.w
+ x
;
935 int tile_offsets
[][2] =
937 {2, 0}, {0, 3}, {0, 2}, {2, 2},
938 {1, 0}, {2, 3}, {3, 2}, {1, 3},
939 {3, 1}, {0, 1}, {1, 2}, {2, 1},
940 {1, 1}, {3, 3}, {2, 1}, {2, 1}
943 int uv
[2] = { 3, 0 };
945 if( cell
->state
& FLAG_CANAL
)
947 uv
[0] = tile_offsets
[ cell
->config
][0];
948 uv
[1] = tile_offsets
[ cell
->config
][1];
951 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
, uv
[0], uv
[1] );
958 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
959 glBlendEquation(GL_FUNC_ADD
);
961 for( int y
= 0; y
< world
.h
; y
++ )
963 for( int x
= 0; x
< world
.w
; x
++ )
965 struct cell
*cell
= pcell((v2i
){x
,y
});
967 if( cell
->state
& FLAG_SPLIT
)
969 float rotation
= cell
->state
& FLAG_FLIP_FLOP
? vg_rad( -45.0f
): vg_rad( 45.0f
);
971 if( cell
->state
& FLAG_FLIP_ROTATING
)
973 if( (frame_lerp
> curve_7_linear_section
) )
975 float const rotation_speed
= 0.4f
;
976 if( (frame_lerp
< 1.0f
-rotation_speed
) )
978 float t
= frame_lerp
- curve_7_linear_section
;
979 t
*= -2.0f
* (1.0f
/(1.0f
-(curve_7_linear_section
+rotation_speed
)));
989 m2x2_create_rotation( subtransform
, rotation
);
991 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
992 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
+ 0.125f
, 0.0f
, 0.0f
);
1000 SHADER_USE( shader_tile_colour
);
1001 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1002 use_mesh( &world
.circle
);
1005 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1007 struct cell_terminal
*term
= &world
.io
[ i
];
1008 int posx
= term
->id
% world
.w
;
1009 int posy
= (term
->id
- posx
)/world
.w
;
1010 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1012 int const filled_start
= 0;
1013 int const filled_count
= 32;
1014 int const empty_start
= 32;
1015 int const empty_count
= 32*2;
1017 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
1019 for( int j
= 0; j
< arrlen( term
->conditions
); j
++ )
1021 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)posx
+ 0.2f
+ 0.2f
* (float)j
, (float)posy
+ 0.2f
, 0.1f
);
1025 colour_code_v3( term
->conditions
[j
], dot_colour
);
1026 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1028 // Draw filled if tick not passed, draw empty if empty
1029 if( world
.sim_frame
> j
)
1030 draw_mesh( empty_start
, empty_count
);
1032 draw_mesh( filled_start
, filled_count
);
1036 if( term
->recv_count
> j
)
1038 colour_code_v3( term
->recv
[j
], dot_colour
);
1039 v3_muls( dot_colour
, 0.8f
, dot_colour
);
1040 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1042 draw_mesh( filled_start
, filled_count
);
1045 colour_code_v3( term
->conditions
[j
], dot_colour
);
1046 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1048 draw_mesh( empty_start
, empty_count
);
1054 if( world
.simulating
)
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
);
1074 float t
= frame_lerp
;
1076 switch( cell
->config
)
1079 if( fish
->dir
[0] == 1 )
1084 case 3: curve
= curve_3
; break;
1085 case 6: curve
= curve_6
; break;
1086 case 9: curve
= curve_9
; break;
1087 case 12: curve
= curve_12
; break;
1089 if( t
> curve_7_linear_section
)
1091 t
-= curve_7_linear_section
;
1092 t
*= (1.0f
/(1.0f
-curve_7_linear_section
));
1094 curve
= cell
->state
& FLAG_FLIP_FLOP
? curve_7
: curve_7_1
;
1098 default: curve
= NULL
; break;
1104 float t3
= t
* t
* t
;
1106 float cA
= 3.0f
*t2
- 3.0f
*t3
;
1107 float cB
= 3.0f
*t3
- 6.0f
*t2
+ 3.0f
*t
;
1108 float cC
= 3.0f
*t2
- t3
- 3.0f
*t
+ 1.0f
;
1110 fish_pos
[0] = t3
*curve
[3][0] + cA
*curve
[2][0] + cB
*curve
[1][0] + cC
*curve
[0][0];
1111 fish_pos
[1] = t3
*curve
[3][1] + cA
*curve
[2][1] + cB
*curve
[1][1] + cC
*curve
[0][1];
1112 fish_pos
[0] += (float)fish
->pos
[0];
1113 fish_pos
[1] += (float)fish
->pos
[1];
1118 origin
[0] = (float)fish
->pos
[0] + (float)fish
->dir
[0]*-0.5f
+ 0.5f
;
1119 origin
[1] = (float)fish
->pos
[1] + (float)fish
->dir
[1]*-0.5f
+ 0.5f
;
1121 fish_pos
[0] = origin
[0] + (float)fish
->dir
[0]*t
;
1122 fish_pos
[1] = origin
[1] + (float)fish
->dir
[1]*t
;
1125 //vg_line_box( (v2f){fish->pos[0],fish->pos[1]},
1126 //(v2f){ (float)fish->pos[0]+1.0f, (float)fish->pos[1]+1.0f }, 0xffffffff );
1128 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), fish_pos
[0], fish_pos
[1], 0.125f
);
1133 if( world
.simulating
)
1135 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.0f
, 0.0f
, 0.0f
, 1.0f
);
1136 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
);