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_ball
,
32 "layout (location=0) in vec2 a_co;"
33 "uniform vec2 uOffset;"
36 "out vec2 aTexCoords;"
40 // Create texture coords
44 "vec3 worldpos = vec3( a_co * 0.5 - 0.25 + uOffset, 1.0 );"
45 "gl_Position = vec4( uPv * worldpos, 1.0 );"
51 "uniform sampler2D uTexMain;"
52 "uniform vec3 uColour;"
58 "vec4 glyph = texture( uTexMain, aTexCoords );"
59 "FragColor = vec4( uColour + glyph.rgb * 0.2, glyph.a );"
62 UNIFORMS({ "uTexMain", "uColour", "uOffset", "uPv" })
65 SHADER_DEFINE( shader_tile_main
,
67 "layout (location=0) in vec2 a_co;"
68 "uniform vec4 uOffset;" // Tile x/y, uv x/y
70 "uniform mat2 uSubTransform;"
72 "out vec4 aTexCoords;"
73 "out vec2 aWorldCoords;"
77 "vec3 p3 = fract(vec3(p.xyx) * vec3(.1031, .1030, .0973));"
78 "p3 += dot(p3, p3.yzx+33.33);"
79 "return fract((p3.xx+p3.yz)*p3.zy);"
85 "vec2 subtransform = uSubTransform * (a_co-0.5) + 0.5;"
86 "vec3 worldpos = vec3( subtransform + uOffset.xy, 1.0 );"
87 "gl_Position = vec4( uPv * worldpos, 1.0 );"
89 // Create texture coords
90 "vec2 random_offset = floor(hash22(uOffset.xy) * 4.0) * 0.25;"
91 "vec2 edge_safe_coords = a_co * 0.98 + 0.01;"
92 "aTexCoords = vec4((edge_safe_coords + uOffset.zw) * 0.25, edge_safe_coords * 0.25 + random_offset );"
93 "aWorldCoords = worldpos.xy;"
99 "uniform sampler2D uTexGlyphs;"
100 "uniform sampler2D uTexWood;"
101 "uniform float uGhost;"
102 "uniform vec2 uMousePos;"
103 "uniform vec4 uColour;"
105 "in vec4 aTexCoords;"
106 "in vec2 aWorldCoords;"
110 "vec3 shadowing_colour = vec3( 0.93, 0.88536, 0.8184 );"
111 "vec4 glyph = texture( uTexGlyphs, aTexCoords.xy );"
112 "vec4 wood = texture( uTexWood, aTexCoords.zw );"
113 "vec4 wood_secondary = texture( uTexWood, aTexCoords.zw + 0.25 );"
114 "vec3 wood_comp = mix( wood_secondary.rgb * shadowing_colour, wood.rgb, clamp( glyph.b * 2.0 - 1.0, 0.0, 1.0 ) );"
116 "vec3 shadows = mix( vec3( 0.85, 0.7344, 0.561 ), vec3(1.0,1.0,1.0), glyph.r );"
118 "vec4 output_regular = vec4( wood_comp * shadows, glyph.b );"
120 "float ghost_dist = clamp( 1.5 - distance(uMousePos, aWorldCoords), 0.0, 1.0 );"
121 "vec4 output_ghost = vec4( 1.0, 1.0, 1.0, glyph.g * ghost_dist );"
123 "FragColor = mix( output_regular, output_ghost, uGhost ) * uColour;"
126 UNIFORMS({ "uPv", "uOffset", "uTexGlyphs", "uTexWood", "uSubTransform", "uGhost", "uMousePos", "uColour" })
129 const char *level_pack
[] =
154 "#####-#####;bbbbb\n"
159 "###+##+####;bbb,bb\n"
164 "###-#####-###;a,aaa\n"
171 "######+######;aaaa\n"
176 "###-#####-###;aaa,aa\n"
183 "###+#####+###;aa,aaa\n"
187 GLuint tex_tile_data
;
188 GLuint tex_tile_detail
;
192 sfx_system_t audio_system_sfx
=
198 .vol_src
= &g_vol_sfx
,
200 .fadeout
= FADEOUT_LENGTH
,
204 sfx_set_t audio_tile_mod
=
220 #define FLAG_INPUT 0x1
221 #define FLAG_OUTPUT 0x2
222 #define FLAG_CANAL 0x4
223 #define FLAG_WALL 0x8
224 #define FLAG_FLIP_FLOP 0x100
225 #define FLAG_FLIP_ROTATING 0x200
228 0000 0 | 0001 1 | 0010 2 | 0011 3
232 0100 4 | 0101 5 | 0110 6 | 0111 7
236 1000 8 | 1001 9 | 1010 10 | 1011 11
240 1100 12 | 1101 13 | 1110 14 | 1111 15
248 k_cell_type_split
= 7,
249 k_cell_type_merge
= 13
253 { { 0.9f
, 0.2f
, 0.01f
},
254 { 0.2f
, 0.9f
, 0.14f
},
255 { 0.1f
, 0.3f
, 0.85f
} };
257 static void colour_code_v3( char cc
, v3f target
)
259 if( cc
>= 'a' && cc
<= 'z' )
263 if( id
< vg_list_size( colour_sets
) )
265 v3_copy( colour_sets
[ id
], target
);
270 v3_copy( (v3f
){0.0f
,0.0f
,0.0f
}, target
);
279 static void init_mesh( struct mesh
*m
, float *tris
, u32 length
)
281 m
->elements
= length
/3;
282 glGenVertexArrays( 1, &m
->vao
);
283 glGenBuffers( 1, &m
->vbo
);
285 glBindVertexArray( m
->vao
);
286 glBindBuffer( GL_ARRAY_BUFFER
, m
->vbo
);
287 glBufferData( GL_ARRAY_BUFFER
, length
*sizeof(float), tris
, GL_STATIC_DRAW
);
289 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2 * sizeof(float), (void*)0 );
290 glEnableVertexAttribArray( 0 );
295 static void free_mesh( struct mesh
*m
)
297 glDeleteVertexArrays( 1, &m
->vao
);
298 glDeleteBuffers( 1, &m
->vbo
);
301 static void draw_mesh( int const start
, int const count
)
303 glDrawArrays( GL_TRIANGLES
, start
*3, count
*3 );
306 static void use_mesh( struct mesh
*m
)
308 glBindVertexArray( m
->vao
);
328 // TODO: Split into input/output structures
338 struct mesh tile
, circle
;
340 int selected
, tile_x
, tile_y
;
355 static void map_free(void)
357 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
358 arrfree( world
.io
[ i
].conditions
);
360 arrfree( world
.data
);
369 static void map_reclassify( v2i start
, v2i end
);
370 static int map_load( const char *str
)
379 if( str
[world
.w
] == ';' )
381 else if( !str
[world
.w
] )
383 vg_error( "Unexpected EOF when parsing level\n" );
388 struct cell
*row
= arraddnptr( world
.data
, world
.w
);
390 int reg_start
= 0, reg_end
= 0;
406 if( reg_start
< reg_end
)
408 if( *c
>= 'a' && *c
<= 'z' )
410 arrpush( world
.io
[ reg_start
].conditions
, *c
);
414 if( *c
== ',' || *c
== '\n' )
423 vg_error( "Unkown attribute '%c' (row: %u)\n", *c
, world
.h
);
430 vg_error( "Too many values to assign (row: %u)\n", world
.h
);
438 if( reg_start
!= reg_end
)
440 vg_error( "Not enough values assigned (row: %u, %u of %u)\n", world
.h
, reg_start
, reg_end
);
446 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world
.h
, cx
, world
.w
);
450 row
= arraddnptr( world
.data
, world
.w
);
453 reg_end
= reg_start
= arrlen( world
.io
);
459 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world
.h
, cx
, world
.w
);
463 // Tile initialization
466 if( *c
== '+' || *c
== '-' )
468 struct cell_terminal term
= { .id
= cx
+ world
.h
*world
.w
};
469 arrpush( world
.io
, term
);
470 row
[ cx
++ ].state
= *c
== '+'? FLAG_INPUT
: FLAG_OUTPUT
;
475 row
[ cx
++ ].state
= FLAG_WALL
;
479 row
[ cx
++ ].state
= 0x00;
486 map_reclassify( NULL
, NULL
);
487 vg_success( "Map loaded! (%u:%u)\n", world
.w
, world
.h
);
491 static struct cell
*pcell( v2i pos
)
493 return &world
.data
[ pos
[1]*world
.w
+ pos
[0] ];
496 int main( int argc
, char *argv
[] )
498 vg_init( argc
, argv
, "Fish (Marbles Computer) Ladder Simulator 2022 | N,M: change level | SPACE: Test | LeftClick: Toggle tile" );
501 void vg_register(void)
503 SHADER_INIT( shader_tile_colour
);
504 SHADER_INIT( shader_tile_main
);
505 SHADER_INIT( shader_ball
);
514 0.0f
, 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
,
515 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
, 0.0f
,
517 0.48f
, 0.48f
, 0.5f
, 0.52f
, 0.52f
, 0.52f
, // Static dot
518 0.375f
, 0.25f
, 0.5f
, 0.75f
, 0.625f
, 0.25f
, // Downwards pointing arrow
519 0.25f
, 0.625f
, 0.75f
, 0.5f
, 0.25f
, 0.375f
, // Left
520 0.625f
, 0.75f
, 0.5f
, 0.25f
, 0.375f
, 0.75f
, // up
521 0.75f
, 0.375f
, 0.25f
, 0.5f
, 0.75f
, 0.625f
524 init_mesh( &world
.tile
, quad_mesh
, vg_list_size(quad_mesh
) );
529 float circle_mesh
[32*6*3];
530 int res
= vg_list_size( circle_mesh
) / (6*3);
532 for( int i
= 0; i
< res
; i
++ )
534 v2f v0
= { sinf( ((float)i
/(float)res
)*VG_TAUf
), cosf( ((float)i
/(float)res
)*VG_TAUf
) };
535 v2f v1
= { sinf( ((float)(i
+1)/(float)res
)*VG_TAUf
), cosf( ((float)(i
+1)/(float)res
)*VG_TAUf
) };
537 circle_mesh
[ i
*6+0 ] = 0.0f
;
538 circle_mesh
[ i
*6+1 ] = 0.0f
;
540 v2_copy( v0
, circle_mesh
+ 32*6 + i
*12 );
541 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+2 );
542 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+4 );
544 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+6 );
545 v2_muls( v1
, 0.8f
, circle_mesh
+ 32*6 + i
*12+8 );
546 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+10 );
548 v2_copy( v0
, circle_mesh
+ i
*6+4 );
549 v2_copy( v1
, circle_mesh
+ i
*6+2 );
550 v2_copy( v0
, circle_mesh
+i
*6+4 );
551 v2_copy( v1
, circle_mesh
+i
*6+2 );
554 init_mesh( &world
.circle
, circle_mesh
, vg_list_size( circle_mesh
) );
559 tex_tile_detail
= vg_tex2d_rgba( "textures/tile_overlays.png" );
561 vg_tex2d_linear_mipmap();
564 tex_tile_data
= vg_tex2d_rgba( "textures/tileset.png" );
566 vg_tex2d_linear_mipmap();
569 tex_wood
= vg_tex2d_rgba( "textures/wood.png" );
571 vg_tex2d_linear_mipmap();
574 tex_ball
= vg_tex2d_rgba( "textures/ball.png" );
576 vg_tex2d_linear_mipmap();
582 sfx_set_init( &audio_tile_mod
, NULL
);
585 map_load( level_pack
[ 0 ] );
590 free_mesh( &world
.tile
);
591 free_mesh( &world
.circle
);
595 glDeleteTextures( 1, &tex_tile_data
);
596 glDeleteTextures( 1, &tex_tile_detail
);
597 glDeleteTextures( 1, &tex_wood
);
598 glDeleteTextures( 1, &tex_ball
);
600 sfx_set_free( &audio_tile_mod
);
603 static int cell_interactive( v2i co
)
606 if( co
[0] < 2 || co
[0] >= world
.w
-2 || co
[1] < 2 || co
[1] >= world
.h
-2 )
610 if( world
.data
[ world
.w
*co
[1] + co
[0] ].state
& (FLAG_WALL
|FLAG_INPUT
|FLAG_OUTPUT
) )
613 // List of 3x3 configurations that we do not allow
614 static u32 invalid_src
[][9] =
646 // Statically compile invalid configurations into bitmasks
647 static u32 invalid
[ vg_list_size(invalid_src
) ];
649 for( int i
= 0; i
< vg_list_size(invalid_src
); i
++ )
653 for( int j
= 0; j
< 3; j
++ )
654 for( int k
= 0; k
< 3; k
++ )
655 comped
|= invalid_src
[i
][ j
*3+k
] << ((j
*5)+k
);
660 // Extract 5x5 grid surrounding tile
662 for( int y
= co
[1]-2; y
< co
[1]+3; y
++ )
663 for( int x
= co
[0]-2; x
< co
[0]+3; x
++ )
665 struct cell
*cell
= pcell((v2i
){x
,y
});
667 if( cell
&& (cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
)) )
668 blob
|= 0x1 << ((y
-(co
[1]-2))*5 + x
-(co
[0]-2));
671 // Run filter over center 3x3 grid to check for invalid configurations
672 int kernel
[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
673 for( int i
= 0; i
< vg_list_size(kernel
); i
++ )
675 if( blob
& (0x1 << (6+kernel
[i
])) )
677 u32 window
= blob
>> kernel
[i
];
679 for( int j
= 0; j
< vg_list_size(invalid
); j
++ )
680 if((window
& invalid
[j
]) == invalid
[j
])
688 static void map_reclassify( v2i start
, v2i end
)
690 v2i full_start
= { 2,2 };
691 v2i full_end
= { world
.w
-2, world
.h
-2 };
699 for( int y
= vg_max( start
[1], full_start
[1] ); y
< vg_min( end
[1], full_end
[1] ); y
++ )
701 for( int x
= vg_max( start
[0], full_start
[0] ); x
< vg_min( end
[0], full_end
[0] ); x
++ )
703 v2i dirs
[] = {{1,0},{0,1},{-1,0},{0,-1}};
707 if( pcell((v2i
){x
,y
})->state
& FLAG_CANAL
)
709 for( int i
= 0; i
< vg_list_size( dirs
); i
++ )
711 struct cell
*neighbour
= pcell((v2i
){x
+dirs
[i
][0], y
+dirs
[i
][1]});
712 if( neighbour
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
718 pcell((v2i
){x
,y
})->config
= config
;
725 static int curlevel
= 0;
726 int changelvl
= curlevel
;
727 if( vg_get_button_down( "prev" ) ) { if( curlevel
> 0 ) changelvl
--; }
728 else if( vg_get_button_down( "next" ) ) { if( curlevel
< vg_list_size( level_pack
)-1 ) changelvl
++; }
730 if( changelvl
!= curlevel
)
732 map_load( level_pack
[ changelvl
] );
733 curlevel
= changelvl
;
736 world
.simulating
= 0;
737 world
.num_fishes
= 0;
740 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
741 world
.io
[i
].recv_count
= 0;
743 vg_info( "Stopping simulation!\n" );
746 float ratio
= (float)vg_window_y
/ (float)vg_window_x
;
747 float const size
= 9.5f
;
750 origin
[0] = -0.5f
* world
.w
;
751 origin
[1] = -0.5f
* world
.h
;
754 m3x3_projection( m_projection
, -size
, size
, -size
*ratio
, size
*ratio
);
755 m3x3_identity( m_view
);
756 m3x3_translate( m_view
, origin
);
757 m3x3_mul( m_projection
, m_view
, vg_pv
);
758 vg_projection_update();
761 v2_copy( vg_mouse_ws
, world
.tile_pos
);
763 world
.tile_x
= floorf( world
.tile_pos
[0] );
764 world
.tile_y
= floorf( world
.tile_pos
[1] );
767 if( !world
.simulating
)
769 if( cell_interactive( (v2i
){ world
.tile_x
, world
.tile_y
} ))
771 world
.selected
= world
.tile_y
* world
.w
+ world
.tile_x
;
773 if( vg_get_button_down("primary") )
775 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
777 if( world
.data
[ world
.selected
].state
& FLAG_CANAL
)
778 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 3, 6 );
780 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 0, 3 );
782 map_reclassify( (v2i
){ world
.tile_x
-2, world
.tile_y
-2 },
783 (v2i
){ world
.tile_x
+2, world
.tile_y
+2 } );
790 // Simulation stop/start
791 if( vg_get_button_down("go") )
793 if( world
.simulating
)
795 world
.simulating
= 0;
796 world
.num_fishes
= 0;
799 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
800 world
.io
[i
].recv_count
= 0;
802 vg_info( "Stopping simulation!\n" );
806 vg_success( "Starting simulation!\n" );
808 world
.simulating
= 1;
809 world
.num_fishes
= 0;
811 world
.sim_start
= vg_time
;
813 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
815 world
.data
[ i
].state
&= ~FLAG_FLIP_FLOP
;
818 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
819 world
.io
[i
].recv_count
= 0;
824 if( world
.simulating
)
826 while( world
.sim_frame
< (int)((vg_time
-world
.sim_start
)*2.0f
) )
828 vg_info( "frame: %u\n", world
.sim_frame
);
830 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
832 struct cell_terminal
*term
= &world
.io
[ i
];
833 int posx
= term
->id
% world
.w
;
834 int posy
= (term
->id
- posx
)/world
.w
;
835 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
839 if( world
.sim_frame
< arrlen( term
->conditions
) )
841 struct fish
*fish
= &world
.fishes
[world
.num_fishes
++];
845 fish
->payload
= term
->conditions
[world
.sim_frame
];
849 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
850 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
851 if( pcell( (v2i
){ posx
+dirs
[j
][0], posy
+dirs
[j
][1] } )->state
& FLAG_CANAL
)
853 fish
->dir
[0] = dirs
[j
][0];
854 fish
->dir
[1] = dirs
[j
][1];
865 // Update splitter deltas
866 for( int i
= 0; i
< world
.h
*world
.w
; i
++ )
868 struct cell
*cell
= &world
.data
[i
];
869 if( cell
->config
== k_cell_type_split
)
871 cell
->state
&= ~FLAG_FLIP_ROTATING
;
875 for( int i
= 0; i
< world
.num_fishes
; i
++ )
877 struct fish
*fish
= &world
.fishes
[i
];
878 struct cell
*cell_current
= pcell( fish
->pos
);
884 if( cell_current
->state
& FLAG_OUTPUT
)
886 for( int j
= 0; j
< arrlen( world
.io
); j
++ )
888 struct cell_terminal
*term
= &world
.io
[j
];
890 if( term
->id
== fish
->pos
[1]*world
.w
+ fish
->pos
[0] )
892 term
->recv
[ term
->recv_count
++ ] = fish
->payload
;
901 if( cell_current
->config
== k_cell_type_split
)
904 fish
->dir
[0] = cell_current
->state
&FLAG_FLIP_FLOP
?1:-1;
907 cell_current
->state
^= FLAG_FLIP_FLOP
;
909 else if( cell_current
->config
== k_cell_type_merge
)
917 struct cell
*cell_next
= pcell( (v2i
){ fish
->pos
[0]+fish
->dir
[0], fish
->pos
[1]+fish
->dir
[1] } );
918 if( !(cell_next
->state
& (FLAG_CANAL
|FLAG_OUTPUT
)) )
920 // Try other directions for valid, so down, left, right..
921 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
922 vg_info( "Trying some other directions...\n" );
924 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
926 if( (dirs
[j
][0] == -fish
->dir
[0]) && (dirs
[j
][1] == -fish
->dir
[1]) )
929 if( pcell( (v2i
){ fish
->pos
[0]+dirs
[j
][0], fish
->pos
[1]+dirs
[j
][1] } )->state
& (FLAG_CANAL
|FLAG_OUTPUT
) )
931 fish
->dir
[0] = dirs
[j
][0];
932 fish
->dir
[1] = dirs
[j
][1];
938 fish
->pos
[0] += fish
->dir
[0];
939 fish
->pos
[1] += fish
->dir
[1];
941 struct cell
*cell_entry
= pcell( fish
->pos
);
943 if( !(cell_entry
->state
& (FLAG_INPUT
|FLAG_CANAL
|FLAG_OUTPUT
) ))
946 if( cell_entry
->config
== k_cell_type_split
)
947 cell_entry
->state
|= FLAG_FLIP_ROTATING
;
955 static void render_tiles( v2i start
, v2i end
, v4f
const regular_colour
, v4f
const selected_colour
)
957 v2i full_start
= { 0,0 };
958 v2i full_end
= { world
.w
, world
.h
};
966 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
968 for( int y
= start
[1]; y
< end
[1]; y
++ )
970 for( int x
= start
[0]; x
< end
[0]; x
++ )
972 struct cell
*cell
= pcell((v2i
){x
,y
});
973 int selected
= world
.selected
== y
*world
.w
+ x
;
975 int tile_offsets
[][2] =
977 {2, 0}, {0, 3}, {0, 2}, {2, 2},
978 {1, 0}, {2, 3}, {3, 2}, {1, 3},
979 {3, 1}, {0, 1}, {1, 2}, {2, 1},
980 {1, 1}, {3, 3}, {2, 1}, {2, 1}
983 int uv
[2] = { 3, 0 };
985 if( cell
->state
& FLAG_CANAL
)
987 uv
[0] = tile_offsets
[ cell
->config
][0];
988 uv
[1] = tile_offsets
[ cell
->config
][1];
991 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
, uv
[0], uv
[1] );
994 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, selected_colour
);
996 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
1004 void vg_render(void)
1006 glViewport( 0,0, vg_window_x
, vg_window_y
);
1008 glDisable( GL_DEPTH_TEST
);
1009 glClearColor( 0.8f
, 0.8f
, 0.8f
, 1.0f
);
1010 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
1012 float scaled_time
= 0.0f
, frame_lerp
= 0.0f
;
1014 if( world
.simulating
)
1016 scaled_time
= (vg_time
-world
.sim_start
)*2.0f
;
1017 frame_lerp
= scaled_time
- (float)world
.sim_frame
;
1020 v4f
const colour_default
= {1.0f
, 1.0f
, 1.0f
, 1.0f
};
1021 v4f
const colour_selected
= {0.90f
, 0.92f
, 1.0f
, 1.0f
};
1023 v2f
const curve_3
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
1024 v2f
const curve_6
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.375f
,0.5f
},{0.0f
,0.5f
}};
1025 v2f
const curve_9
[] = {{1.0f
,0.5f
},{0.625f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
1026 v2f
const curve_12
[]= {{0.0f
,0.5f
},{0.375f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
1028 v2f
const curve_7
[] = {{0.5f
,0.8438f
},{0.875f
,0.8438f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
1029 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
}};
1031 float const curve_7_linear_section
= 0.1562f
;
1033 // TILE SET RENDERING
1034 // todo: just slam everything into a mesh...
1035 // when user modifies a tile the neighbours can be easily uploaded to gpu mem
1037 // Currently we're uploading a fair amount of data every frame anyway.
1038 // NOTE: this is for final optimisations ONLY!
1039 // ======================================================================
1041 use_mesh( &world
.tile
);
1043 SHADER_USE( shader_tile_main
);
1046 m2x2_identity( subtransform
);
1047 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1048 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_main
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1049 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 0.0f
);
1052 glActiveTexture( GL_TEXTURE0
);
1053 glBindTexture( GL_TEXTURE_2D
, tex_tile_data
);
1054 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
1056 glActiveTexture( GL_TEXTURE1
);
1057 glBindTexture( GL_TEXTURE_2D
, tex_wood
);
1058 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
1060 render_tiles( NULL
, NULL
, colour_default
, colour_default
);
1063 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1064 glBlendEquation(GL_FUNC_ADD
);
1066 SHADER_USE( shader_ball
);
1067 glUniformMatrix3fv( SHADER_UNIFORM( shader_ball
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1069 glActiveTexture( GL_TEXTURE0
);
1070 glBindTexture( GL_TEXTURE_2D
, tex_ball
);
1071 glUniform1i( SHADER_UNIFORM( shader_ball
, "uTexMain" ), 0 );
1074 if( world
.simulating
)
1076 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1078 struct fish
*fish
= &world
.fishes
[i
];
1083 // Evaluate position
1084 struct cell
*cell
= pcell(fish
->pos
);
1089 float t
= frame_lerp
;
1091 switch( cell
->config
)
1094 if( fish
->dir
[0] == 1 )
1099 case 3: curve
= curve_3
; break;
1100 case 6: curve
= curve_6
; break;
1101 case 9: curve
= curve_9
; break;
1102 case 12: curve
= curve_12
; break;
1104 if( t
> curve_7_linear_section
)
1106 t
-= curve_7_linear_section
;
1107 t
*= (1.0f
/(1.0f
-curve_7_linear_section
));
1109 curve
= cell
->state
& FLAG_FLIP_FLOP
? curve_7
: curve_7_1
;
1113 default: curve
= NULL
; break;
1119 float t3
= t
* t
* t
;
1121 float cA
= 3.0f
*t2
- 3.0f
*t3
;
1122 float cB
= 3.0f
*t3
- 6.0f
*t2
+ 3.0f
*t
;
1123 float cC
= 3.0f
*t2
- t3
- 3.0f
*t
+ 1.0f
;
1125 fish_pos
[0] = t3
*curve
[3][0] + cA
*curve
[2][0] + cB
*curve
[1][0] + cC
*curve
[0][0];
1126 fish_pos
[1] = t3
*curve
[3][1] + cA
*curve
[2][1] + cB
*curve
[1][1] + cC
*curve
[0][1];
1127 fish_pos
[0] += (float)fish
->pos
[0];
1128 fish_pos
[1] += (float)fish
->pos
[1];
1133 origin
[0] = (float)fish
->pos
[0] + (float)fish
->dir
[0]*-0.5f
+ 0.5f
;
1134 origin
[1] = (float)fish
->pos
[1] + (float)fish
->dir
[1]*-0.5f
+ 0.5f
;
1136 fish_pos
[0] = origin
[0] + (float)fish
->dir
[0]*t
;
1137 fish_pos
[1] = origin
[1] + (float)fish
->dir
[1]*t
;
1140 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
1141 colour_code_v3( fish
->payload
, dot_colour
);
1143 glUniform3fv( SHADER_UNIFORM( shader_ball
, "uColour" ), 1, dot_colour
);
1144 glUniform2f( SHADER_UNIFORM( shader_ball
, "uOffset" ), fish_pos
[0], fish_pos
[1] );
1150 SHADER_USE( shader_tile_main
);
1153 glActiveTexture( GL_TEXTURE0
);
1154 glBindTexture( GL_TEXTURE_2D
, tex_tile_data
);
1155 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
1157 glActiveTexture( GL_TEXTURE1
);
1158 glBindTexture( GL_TEXTURE_2D
, tex_wood
);
1159 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
1161 render_tiles( NULL
, NULL
, colour_default
, colour_selected
);
1165 for( int y
= 0; y
< world
.h
; y
++ )
1167 for( int x
= 0; x
< world
.w
; x
++ )
1169 struct cell
*cell
= pcell((v2i
){x
,y
});
1171 if( cell
->config
== k_cell_type_split
)
1173 float rotation
= cell
->state
& FLAG_FLIP_FLOP
? vg_rad( -45.0f
): vg_rad( 45.0f
);
1175 if( cell
->state
& FLAG_FLIP_ROTATING
)
1177 if( (frame_lerp
> curve_7_linear_section
) )
1179 float const rotation_speed
= 0.4f
;
1180 if( (frame_lerp
< 1.0f
-rotation_speed
) )
1182 float t
= frame_lerp
- curve_7_linear_section
;
1183 t
*= -2.0f
* (1.0f
/(1.0f
-(curve_7_linear_section
+rotation_speed
)));
1193 m2x2_create_rotation( subtransform
, rotation
);
1195 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1196 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
+ 0.125f
, 0.0f
, 0.0f
);
1203 if( world
.selected
!= -1 && !(world
.data
[ world
.selected
].state
& FLAG_CANAL
) )
1205 v2i new_begin
= { world
.tile_x
- 2, world
.tile_y
- 2 };
1206 v2i new_end
= { world
.tile_x
+ 2, world
.tile_y
+ 2 };
1208 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
1209 map_reclassify( new_begin
, new_end
);
1211 m2x2_identity( subtransform
);
1212 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 1.0f
);
1213 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1214 glUniform2fv( SHADER_UNIFORM( shader_tile_main
, "uMousePos" ), 1, world
.tile_pos
);
1216 render_tiles( new_begin
, new_end
, colour_default
, colour_default
);
1218 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
1219 map_reclassify( new_begin
, new_end
);
1222 //glDisable(GL_BLEND);
1224 glDisable(GL_BLEND
);
1226 SHADER_USE( shader_tile_colour
);
1227 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1228 use_mesh( &world
.circle
);
1231 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1233 struct cell_terminal
*term
= &world
.io
[ i
];
1234 int posx
= term
->id
% world
.w
;
1235 int posy
= (term
->id
- posx
)/world
.w
;
1236 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1238 int const filled_start
= 0;
1239 int const filled_count
= 32;
1240 int const empty_start
= 32;
1241 int const empty_count
= 32*2;
1243 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
1245 for( int j
= 0; j
< arrlen( term
->conditions
); j
++ )
1247 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)posx
+ 0.2f
+ 0.2f
* (float)j
, (float)posy
+ 0.2f
, 0.1f
);
1251 colour_code_v3( term
->conditions
[j
], dot_colour
);
1252 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1254 // Draw filled if tick not passed, draw empty if empty
1255 if( world
.sim_frame
> j
)
1256 draw_mesh( empty_start
, empty_count
);
1258 draw_mesh( filled_start
, filled_count
);
1262 if( term
->recv_count
> j
)
1264 colour_code_v3( term
->recv
[j
], dot_colour
);
1265 v3_muls( dot_colour
, 0.8f
, dot_colour
);
1266 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1268 draw_mesh( filled_start
, filled_count
);
1271 colour_code_v3( term
->conditions
[j
], dot_colour
);
1272 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1274 draw_mesh( empty_start
, empty_count
);
1279 if( world
.simulating
)
1281 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.0f
, 0.0f
, 0.0f
, 1.0f
);
1282 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
);