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 { { 0.9f
, 0.2f
, 0.01f
},
162 { 0.2f
, 0.9f
, 0.14f
},
163 { 0.1f
, 0.3f
, 0.85f
} };
165 static void colour_code_v3( char cc
, v3f target
)
167 if( cc
>= 'a' && cc
<= 'z' )
171 if( id
< vg_list_size( colour_sets
) )
173 v3_copy( colour_sets
[ id
], target
);
178 v3_copy( (v3f
){0.0f
,0.0f
,0.0f
}, target
);
187 static void init_mesh( struct mesh
*m
, float *tris
, u32 length
)
189 m
->elements
= length
/3;
190 glGenVertexArrays( 1, &m
->vao
);
191 glGenBuffers( 1, &m
->vbo
);
193 glBindVertexArray( m
->vao
);
194 glBindBuffer( GL_ARRAY_BUFFER
, m
->vbo
);
195 glBufferData( GL_ARRAY_BUFFER
, length
*sizeof(float), tris
, GL_STATIC_DRAW
);
197 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2 * sizeof(float), (void*)0 );
198 glEnableVertexAttribArray( 0 );
203 static void free_mesh( struct mesh
*m
)
205 glDeleteVertexArrays( 1, &m
->vao
);
206 glDeleteBuffers( 1, &m
->vbo
);
209 static void draw_mesh( int const start
, int const count
)
211 glDrawArrays( GL_TRIANGLES
, start
*3, count
*3 );
214 static void use_mesh( struct mesh
*m
)
216 glBindVertexArray( m
->vao
);
236 // TODO: Split into input/output structures
246 struct mesh tile
, circle
, splitter_l
, splitter_r
;
262 static void map_free(void)
264 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
265 arrfree( world
.io
[ i
].conditions
);
267 arrfree( world
.data
);
276 static int map_load( const char *str
)
285 if( str
[world
.w
] == ';' )
287 else if( !str
[world
.w
] )
289 vg_error( "Unexpected EOF when parsing level\n" );
294 struct cell
*row
= arraddnptr( world
.data
, world
.w
);
296 int reg_start
= 0, reg_end
= 0;
312 if( reg_start
< reg_end
)
314 if( *c
>= 'a' && *c
<= 'z' )
316 arrpush( world
.io
[ reg_start
].conditions
, *c
);
320 if( *c
== ',' || *c
== '\n' )
329 vg_error( "Unkown attribute '%c' (row: %u)\n", *c
, world
.h
);
336 vg_error( "Too many values to assign (row: %u)\n", world
.h
);
344 if( reg_start
!= reg_end
)
346 vg_error( "Not enough values assigned (row: %u, %u of %u)\n", world
.h
, reg_start
, reg_end
);
352 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world
.h
, cx
, world
.w
);
356 row
= arraddnptr( world
.data
, world
.w
);
359 reg_end
= reg_start
= arrlen( world
.io
);
365 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world
.h
, cx
, world
.w
);
369 // Tile initialization
372 if( *c
== '+' || *c
== '-' )
374 struct cell_terminal term
= { .id
= cx
+ world
.h
*world
.w
};
375 arrpush( world
.io
, term
);
376 row
[ cx
++ ].state
= *c
== '+'? FLAG_INPUT
: FLAG_OUTPUT
;
381 row
[ cx
++ ].state
= FLAG_WALL
;
385 row
[ cx
++ ].state
= 0x00;
392 vg_success( "Map loaded! (%u:%u)\n", world
.w
, world
.h
);
396 static struct cell
*pcell( v2i pos
)
398 return &world
.data
[ pos
[1]*world
.w
+ pos
[0] ];
401 int main( int argc
, char *argv
[] )
403 vg_init( argc
, argv
, "Fish (Marbles Computer) Ladder Simulator 2022 | N,M: change level | SPACE: Test | LeftClick: Toggle tile" );
406 void vg_register(void)
408 SHADER_INIT( shader_tile_colour
);
409 SHADER_INIT( shader_tile_main
);
418 0.0f
, 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
,
419 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
, 0.0f
,
421 0.48f
, 0.48f
, 0.5f
, 0.52f
, 0.52f
, 0.52f
, // Static dot
422 0.375f
, 0.25f
, 0.5f
, 0.75f
, 0.625f
, 0.25f
, // Downwards pointing arrow
423 0.25f
, 0.625f
, 0.75f
, 0.5f
, 0.25f
, 0.375f
, // Left
424 0.625f
, 0.75f
, 0.5f
, 0.25f
, 0.375f
, 0.75f
, // up
425 0.75f
, 0.375f
, 0.25f
, 0.5f
, 0.75f
, 0.625f
428 init_mesh( &world
.tile
, quad_mesh
, vg_list_size(quad_mesh
) );
433 float circle_mesh
[32*6*3];
434 int res
= vg_list_size( circle_mesh
) / (6*3);
436 for( int i
= 0; i
< res
; i
++ )
438 v2f v0
= { sinf( ((float)i
/(float)res
)*VG_TAUf
), cosf( ((float)i
/(float)res
)*VG_TAUf
) };
439 v2f v1
= { sinf( ((float)(i
+1)/(float)res
)*VG_TAUf
), cosf( ((float)(i
+1)/(float)res
)*VG_TAUf
) };
441 circle_mesh
[ i
*6+0 ] = 0.0f
;
442 circle_mesh
[ i
*6+1 ] = 0.0f
;
444 v2_copy( v0
, circle_mesh
+ 32*6 + i
*12 );
445 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+2 );
446 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+4 );
448 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+6 );
449 v2_muls( v1
, 0.8f
, circle_mesh
+ 32*6 + i
*12+8 );
450 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+10 );
452 v2_copy( v0
, circle_mesh
+ i
*6+4 );
453 v2_copy( v1
, circle_mesh
+ i
*6+2 );
454 v2_copy( v0
, circle_mesh
+i
*6+4 );
455 v2_copy( v1
, circle_mesh
+i
*6+2 );
458 init_mesh( &world
.circle
, circle_mesh
, vg_list_size( circle_mesh
) );
465 #include "models/splitter_l.obj.h"
469 #include "models/splitter_r.obj.h"
472 init_mesh( &world
.splitter_l
, splitter_l
, vg_list_size( splitter_l
) );
473 init_mesh( &world
.splitter_r
, splitter_r
, vg_list_size( splitter_r
) );
478 tex_tile_detail
= vg_tex2d_rgba( "textures/tile_overlays.png" );
480 vg_tex2d_linear_mipmap();
483 tex_tile_data
= vg_tex2d_rgba( "textures/tileset.png" );
485 vg_tex2d_linear_mipmap();
488 tex_wood
= vg_tex2d_rgba( "textures/wood.png" );
490 vg_tex2d_linear_mipmap();
494 map_load( level_pack
[ 0 ] );
499 free_mesh( &world
.tile
);
500 free_mesh( &world
.circle
);
501 free_mesh( &world
.splitter_l
);
502 free_mesh( &world
.splitter_r
);
506 glDeleteTextures( 1, &tex_tile_data
);
507 glDeleteTextures( 1, &tex_tile_detail
);
508 glDeleteTextures( 1, &tex_wood
);
511 static int cell_interactive( v2i co
)
514 if( co
[0] < 2 || co
[0] >= world
.w
-2 || co
[1] < 2 || co
[1] >= world
.h
-2 )
518 if( world
.data
[ world
.w
*co
[1] + co
[0] ].state
& (FLAG_WALL
|FLAG_INPUT
|FLAG_OUTPUT
) )
521 // List of 3x3 configurations that we do not allow
522 static u32 invalid_src
[][9] =
554 // Statically compile invalid configurations into bitmasks
555 static u32 invalid
[ vg_list_size(invalid_src
) ];
557 for( int i
= 0; i
< vg_list_size(invalid_src
); i
++ )
561 for( int j
= 0; j
< 3; j
++ )
562 for( int k
= 0; k
< 3; k
++ )
563 comped
|= invalid_src
[i
][ j
*3+k
] << ((j
*5)+k
);
568 // Extract 5x5 grid surrounding tile
570 for( int y
= co
[1]-2; y
< co
[1]+3; y
++ )
571 for( int x
= co
[0]-2; x
< co
[0]+3; x
++ )
573 struct cell
*cell
= pcell((v2i
){x
,y
});
575 if( cell
&& (cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
)) )
576 blob
|= 0x1 << ((y
-(co
[1]-2))*5 + x
-(co
[0]-2));
579 // Run filter over center 3x3 grid to check for invalid configurations
580 int kernel
[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
581 for( int i
= 0; i
< vg_list_size(kernel
); i
++ )
583 if( blob
& (0x1 << (6+kernel
[i
])) )
585 u32 window
= blob
>> kernel
[i
];
587 for( int j
= 0; j
< vg_list_size(invalid
); j
++ )
588 if((window
& invalid
[j
]) == invalid
[j
])
598 static int curlevel
= 0;
599 int changelvl
= curlevel
;
600 if( vg_get_button_down( "prev" ) ) { if( curlevel
> 0 ) changelvl
--; }
601 else if( vg_get_button_down( "next" ) ) { if( curlevel
< vg_list_size( level_pack
)-1 ) changelvl
++; }
603 if( changelvl
!= curlevel
)
605 map_load( level_pack
[ changelvl
] );
606 curlevel
= changelvl
;
609 world
.simulating
= 0;
610 world
.num_fishes
= 0;
613 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
614 world
.io
[i
].recv_count
= 0;
616 vg_info( "Stopping simulation!\n" );
619 float ratio
= (float)vg_window_y
/ (float)vg_window_x
;
620 float const size
= 9.5f
;
623 origin
[0] = -0.5f
* world
.w
;
624 origin
[1] = -0.5f
* world
.h
;
627 m3x3_projection( m_projection
, -size
, size
, -size
*ratio
, size
*ratio
);
628 m3x3_identity( m_view
);
629 m3x3_translate( m_view
, origin
);
630 m3x3_mul( m_projection
, m_view
, vg_pv
);
631 vg_projection_update();
636 v2_copy( vg_mouse_ws
, tile_pos
);
638 int tile_x
= floorf( tile_pos
[0] );
639 int tile_y
= floorf( tile_pos
[1] );
642 if( !world
.simulating
)
644 if( cell_interactive( (v2i
){ tile_x
, tile_y
} ))
646 world
.selected
= tile_y
* world
.w
+ tile_x
;
648 if( vg_get_button_down("primary") )
650 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
657 // Simulation stop/start
658 if( vg_get_button_down("go") )
660 if( world
.simulating
)
662 world
.simulating
= 0;
663 world
.num_fishes
= 0;
666 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
667 world
.io
[i
].recv_count
= 0;
669 vg_info( "Stopping simulation!\n" );
673 vg_success( "Starting simulation!\n" );
675 world
.simulating
= 1;
676 world
.num_fishes
= 0;
678 world
.sim_start
= vg_time
;
680 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
682 world
.data
[ i
].state
&= ~FLAG_FLIP_FLOP
;
685 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
686 world
.io
[i
].recv_count
= 0;
691 // ========================================================
693 for( int y
= 2; y
< world
.h
-2; y
++ )
695 for( int x
= 2; x
< world
.w
-2; x
++ )
697 v2i dirs
[] = {{1,0},{0,1},{-1,0},{0,-1}};
701 if( pcell((v2i
){x
,y
})->state
& FLAG_CANAL
)
703 for( int i
= 0; i
< vg_list_size( dirs
); i
++ )
705 struct cell
*neighbour
= pcell((v2i
){x
+dirs
[i
][0], y
+dirs
[i
][1]});
706 if( neighbour
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
711 pcell((v2i
){x
,y
})->config
= config
;
712 pcell((v2i
){x
,y
})->state
&= ~(FLAG_DROP_L
|FLAG_DROP_R
|FLAG_SPLIT
|FLAG_MERGER
);
716 for( int y
= 2; y
< world
.h
-2; y
++ )
717 for( int x
= 2; x
< world
.w
-2; x
++ )
719 // R,D,L,- 1110 (splitter, 1 drop created)
721 // R,-,L,U - 1011 (merger, 2 drop created)
723 u8 config
= pcell((v2i
){x
,y
})->config
;
725 if( config
== 0x7 ) // splitter
727 struct cell
*cell
= pcell((v2i
){x
,y
});
729 cell
->state
|= (FLAG_SPLIT
| FLAG_DROP_L
| FLAG_DROP_R
);
731 else if( config
== 0xD )
733 world
.data
[y
*world
.w
+x
-1].state
|= FLAG_DROP_R
;
734 world
.data
[y
*world
.w
+x
+1].state
|= FLAG_DROP_L
;
735 world
.data
[y
*world
.w
+x
].state
|= FLAG_MERGER
;
740 if( world
.simulating
)
742 while( world
.sim_frame
< (int)((vg_time
-world
.sim_start
)*2.0f
) )
744 vg_info( "frame: %u\n", world
.sim_frame
);
746 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
748 struct cell_terminal
*term
= &world
.io
[ i
];
749 int posx
= term
->id
% world
.w
;
750 int posy
= (term
->id
- posx
)/world
.w
;
751 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
755 if( world
.sim_frame
< arrlen( term
->conditions
) )
757 struct fish
*fish
= &world
.fishes
[world
.num_fishes
++];
761 fish
->payload
= term
->conditions
[world
.sim_frame
];
765 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
766 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
767 if( pcell( (v2i
){ posx
+dirs
[j
][0], posy
+dirs
[j
][1] } )->state
& FLAG_CANAL
)
769 fish
->dir
[0] = dirs
[j
][0];
770 fish
->dir
[1] = dirs
[j
][1];
781 // Update splitter deltas
782 for( int i
= 0; i
< world
.h
*world
.w
; i
++ )
784 struct cell
*cell
= &world
.data
[i
];
785 if( cell
->config
== 0x7 )
787 cell
->state
&= ~FLAG_FLIP_ROTATING
;
791 for( int i
= 0; i
< world
.num_fishes
; i
++ )
793 struct fish
*fish
= &world
.fishes
[i
];
794 struct cell
*cell_current
= pcell( fish
->pos
);
800 if( cell_current
->state
& FLAG_OUTPUT
)
802 for( int j
= 0; j
< arrlen( world
.io
); j
++ )
804 struct cell_terminal
*term
= &world
.io
[j
];
806 if( term
->id
== fish
->pos
[1]*world
.w
+ fish
->pos
[0] )
808 term
->recv
[ term
->recv_count
++ ] = fish
->payload
;
817 if( !(cell_current
->state
& (FLAG_INPUT
|FLAG_CANAL
)) )
823 if( cell_current
->state
& FLAG_SPLIT
)
826 fish
->dir
[0] = cell_current
->state
&FLAG_FLIP_FLOP
?1:-1;
829 cell_current
->state
^= FLAG_FLIP_FLOP
;
831 else if( cell_current
->state
& FLAG_MERGER
)
839 struct cell
*cell_next
= pcell( (v2i
){ fish
->pos
[0]+fish
->dir
[0], fish
->pos
[1]+fish
->dir
[1] } );
840 if( !(cell_next
->state
& (FLAG_CANAL
|FLAG_OUTPUT
)) )
842 // Try other directions for valid, so down, left, right..
843 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
844 vg_info( "Trying some other directions...\n" );
846 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
848 if( (dirs
[j
][0] == -fish
->dir
[0]) && (dirs
[j
][1] == -fish
->dir
[1]) )
851 if( pcell( (v2i
){ fish
->pos
[0]+dirs
[j
][0], fish
->pos
[1]+dirs
[j
][1] } )->state
& (FLAG_CANAL
|FLAG_OUTPUT
) )
853 fish
->dir
[0] = dirs
[j
][0];
854 fish
->dir
[1] = dirs
[j
][1];
860 fish
->pos
[0] += fish
->dir
[0];
861 fish
->pos
[1] += fish
->dir
[1];
863 struct cell
*cell_entry
= pcell( fish
->pos
);
864 if( cell_entry
->config
== 0x7 )
865 cell_entry
->state
|= FLAG_FLIP_ROTATING
;
876 glViewport( 0,0, vg_window_x
, vg_window_y
);
878 glDisable( GL_DEPTH_TEST
);
879 glClearColor( 0.8f
, 0.8f
, 0.8f
, 1.0f
);
880 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
882 float scaled_time
= 0.0f
, frame_lerp
= 0.0f
;
884 if( world
.simulating
)
886 scaled_time
= (vg_time
-world
.sim_start
)*2.0f
;
887 frame_lerp
= scaled_time
- (float)world
.sim_frame
;
892 glUniform4f( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 0.5f, 0.5f, 0.5f, 1.0f );
893 for( int y = 0; y < world.h; y ++ )
894 for( int x = 0; x < world.w; x ++ )
896 struct cell *cell = pcell((v2i){x,y});
898 if( cell->state & FLAG_CANAL )
903 glUniform3f( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), (float)x - 0.2f, (float)y - 0.15f, 1.0f );
907 for( int y = 0; y < world.h; y ++ )
909 for( int x = 0; x < world.w; x ++ )
911 struct cell *cell = pcell((v2i){x,y});
912 int selected = world.selected == y*world.w + x;
914 if( cell->state & FLAG_SPLIT )
916 glUniform4f( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 0.9f, 0.9f, 0.9f, 1.0f );
917 glUniform3f( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), (float)x, (float)y, 1.0f );
919 struct mesh *splitter = cell->state & FLAG_FLIP_FLOP? &world.splitter_r: &world.splitter_l;
921 use_mesh( splitter );
922 draw_mesh( 0, splitter->elements );
923 use_mesh( &world.tile );
926 if( (cell->state & FLAG_CANAL) && !selected )
929 glUniform3f( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), (float)x, (float)y, 1.0f );
933 if( cell->state & FLAG_WALL ) { v4_copy( (v4f){ 0.2f, 0.2f, 0.2f, 1.0f }, colour ); }
934 else if( cell->state & FLAG_CANAL ) { v4_copy( (v4f){ 0.6f, 0.6f, 0.6f, 1.0f }, colour ); }
935 else if( cell->state & FLAG_INPUT ) { v4_copy( (v4f){ 0.5f, 0.5f, 0.5f, 1.0f }, colour ); }
936 else if( cell->state & FLAG_OUTPUT ) { v4_copy( (v4f){ 0.4f, 0.4f, 0.4f, 1.0f }, colour ); }
937 else v4_copy( (v4f){ 0.9f, 0.9f, 0.9f, 1.0f }, colour );
939 //if( cell->water[world.frame&0x1] )
940 // v4_copy( (v4f){ 0.2f, 0.3f, 0.7f * (float)(cell->water[world.frame&0x1]) * (1.0f/16.0f), 1.0f }, colour );
943 v3_muls( colour, sinf( vg_time )*0.25f + 0.5f, colour );
945 //if( cell->state & (FLAG_SPLIT) )
946 // v4_copy( (v4f){ 0.75f, 0.75f, 0.02f, 1.0f }, colour );
947 //if( cell->state & (FLAG_MERGER) )
948 // v4_copy( (v4f){ 0.75f, 0.02f, 0.75f, 1.0f }, colour );
950 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, colour );
957 v2f
const curve_3
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
958 v2f
const curve_6
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.375f
,0.5f
},{0.0f
,0.5f
}};
959 v2f
const curve_9
[] = {{1.0f
,0.5f
},{0.625f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
960 v2f
const curve_12
[]= {{0.0f
,0.5f
},{0.375f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
962 v2f
const curve_7
[] = {{0.5f
,0.8438f
},{0.875f
,0.8438f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
963 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
}};
965 float const curve_7_linear_section
= 0.1562f
;
967 // TILE SET RENDERING
968 // ======================================================================
969 use_mesh( &world
.tile
);
970 SHADER_USE( shader_tile_main
);
973 m2x2_identity( subtransform
);
974 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
975 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_main
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
978 glActiveTexture( GL_TEXTURE0
);
979 glBindTexture( GL_TEXTURE_2D
, tex_tile_data
);
980 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
982 glActiveTexture( GL_TEXTURE1
);
983 glBindTexture( GL_TEXTURE_2D
, tex_wood
);
984 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
986 for( int y
= 0; y
< world
.h
; y
++ )
988 for( int x
= 0; x
< world
.w
; x
++ )
990 struct cell
*cell
= pcell((v2i
){x
,y
});
991 int selected
= world
.selected
== y
*world
.w
+ x
;
994 0000 0 | 0001 1 | 0010 2 | 0011 3
998 0100 4 | 0101 5 | 0110 6 | 0111 7
1002 1000 8 | 1001 9 | 1010 10 | 1011 11
1006 1100 12 | 1101 13 | 1110 14 | 1111 15
1012 int tile_offsets
[][2] =
1014 {2, 0}, {0, 3}, {0, 2}, {2, 2},
1015 {1, 0}, {2, 3}, {3, 2}, {1, 3},
1016 {3, 1}, {0, 1}, {1, 2}, {2, 1},
1017 {1, 1}, {3, 3}, {2, 1}, {2, 1}
1020 int uv
[2] = { 3, 0 };
1022 if( cell
->state
& FLAG_CANAL
)
1024 uv
[0] = tile_offsets
[ cell
->config
][0];
1025 uv
[1] = tile_offsets
[ cell
->config
][1];
1028 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
, uv
[0], uv
[1] );
1035 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1036 glBlendEquation(GL_FUNC_ADD
);
1038 for( int y
= 0; y
< world
.h
; y
++ )
1040 for( int x
= 0; x
< world
.w
; x
++ )
1042 struct cell
*cell
= pcell((v2i
){x
,y
});
1044 if( cell
->state
& FLAG_SPLIT
)
1046 float rotation
= cell
->state
& FLAG_FLIP_FLOP
? vg_rad( -45.0f
): vg_rad( 45.0f
);
1048 if( cell
->state
& FLAG_FLIP_ROTATING
)
1050 if( (frame_lerp
> curve_7_linear_section
) )
1052 float const rotation_speed
= 0.4f
;
1053 if( (frame_lerp
< 1.0f
-rotation_speed
) )
1055 float t
= frame_lerp
- curve_7_linear_section
;
1056 t
*= -2.0f
* (1.0f
/(1.0f
-(curve_7_linear_section
+rotation_speed
)));
1066 m2x2_create_rotation( subtransform
, rotation
);
1068 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1069 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
+ 0.125f
, 0.0f
, 0.0f
);
1075 glDisable(GL_BLEND
);
1077 SHADER_USE( shader_tile_colour
);
1078 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1079 use_mesh( &world
.circle
);
1082 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1084 struct cell_terminal
*term
= &world
.io
[ i
];
1085 int posx
= term
->id
% world
.w
;
1086 int posy
= (term
->id
- posx
)/world
.w
;
1087 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1089 int const filled_start
= 0;
1090 int const filled_count
= 32;
1091 int const empty_start
= 32;
1092 int const empty_count
= 32*2;
1094 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
1096 for( int j
= 0; j
< arrlen( term
->conditions
); j
++ )
1098 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)posx
+ 0.2f
+ 0.2f
* (float)j
, (float)posy
+ 0.2f
, 0.1f
);
1102 colour_code_v3( term
->conditions
[j
], dot_colour
);
1103 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1105 // Draw filled if tick not passed, draw empty if empty
1106 if( world
.sim_frame
> j
)
1107 draw_mesh( empty_start
, empty_count
);
1109 draw_mesh( filled_start
, filled_count
);
1113 if( term
->recv_count
> j
)
1115 colour_code_v3( term
->recv
[j
], dot_colour
);
1116 v3_muls( dot_colour
, 0.8f
, dot_colour
);
1117 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1119 draw_mesh( filled_start
, filled_count
);
1122 colour_code_v3( term
->conditions
[j
], dot_colour
);
1123 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1125 draw_mesh( empty_start
, empty_count
);
1131 if( world
.simulating
)
1133 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
1135 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1137 struct fish
*fish
= &world
.fishes
[i
];
1142 colour_code_v3( fish
->payload
, dot_colour
);
1143 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1145 // Evaluate position
1146 struct cell
*cell
= pcell(fish
->pos
);
1151 float t
= frame_lerp
;
1153 switch( cell
->config
)
1156 if( fish
->dir
[0] == 1 )
1161 case 3: curve
= curve_3
; break;
1162 case 6: curve
= curve_6
; break;
1163 case 9: curve
= curve_9
; break;
1164 case 12: curve
= curve_12
; break;
1166 if( t
> curve_7_linear_section
)
1168 t
-= curve_7_linear_section
;
1169 t
*= (1.0f
/(1.0f
-curve_7_linear_section
));
1171 curve
= cell
->state
& FLAG_FLIP_FLOP
? curve_7
: curve_7_1
;
1175 default: curve
= NULL
; break;
1182 float t3
= t
* t
* t
;
1184 float cA
= 3.0f
*t2
- 3.0f
*t3
;
1185 float cB
= 3.0f
*t3
- 6.0f
*t2
+ 3.0f
*t
;
1186 float cC
= 3.0f
*t2
- t3
- 3.0f
*t
+ 1.0f
;
1188 fish_pos
[0] = t3
*curve
[3][0] + cA
*curve
[2][0] + cB
*curve
[1][0] + cC
*curve
[0][0];
1189 fish_pos
[1] = t3
*curve
[3][1] + cA
*curve
[2][1] + cB
*curve
[1][1] + cC
*curve
[0][1];
1190 fish_pos
[0] += (float)fish
->pos
[0];
1191 fish_pos
[1] += (float)fish
->pos
[1];
1196 origin
[0] = (float)fish
->pos
[0] + (float)fish
->dir
[0]*-0.5f
+ 0.5f
;
1197 origin
[1] = (float)fish
->pos
[1] + (float)fish
->dir
[1]*-0.5f
+ 0.5f
;
1199 fish_pos
[0] = origin
[0] + (float)fish
->dir
[0]*t
;
1200 fish_pos
[1] = origin
[1] + (float)fish
->dir
[1]*t
;
1203 vg_line_box( (v2f
){fish
->pos
[0],fish
->pos
[1]},
1204 (v2f
){ (float)fish
->pos
[0]+1.0f
, (float)fish
->pos
[1]+1.0f
}, 0xffffffff );
1206 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), fish_pos
[0], fish_pos
[1], 0.125f
);
1211 if( world
.simulating
)
1213 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.0f
, 0.0f
, 0.0f
, 1.0f
);
1214 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
);