1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
5 #include "fishladder_resources.h"
7 const char *level_pack
[] =
37 "###+##+####;bbb,bb\n"
42 "###-#####-###;a,aaa\n"
49 "######+######;aaaa\n"
54 "###-#####-###;aaa,aa\n"
61 "###+#####+###;aa,aaa\n"
69 #define FLAG_INPUT 0x1
70 #define FLAG_OUTPUT 0x2
71 #define FLAG_CANAL 0x4
73 #define FLAG_FLIP_FLOP 0x100
74 #define FLAG_FLIP_ROTATING 0x200
77 0000 0 | 0001 1 | 0010 2 | 0011 3
81 0100 4 | 0101 5 | 0110 6 | 0111 7
85 1000 8 | 1001 9 | 1010 10 | 1011 11
89 1100 12 | 1101 13 | 1110 14 | 1111 15
97 k_cell_type_ramp_right
= 3,
98 k_cell_type_ramp_left
= 6,
99 k_cell_type_split
= 7,
100 k_cell_type_merge
= 13
104 { { 0.9f
, 0.2f
, 0.01f
},
105 { 0.2f
, 0.9f
, 0.14f
},
106 { 0.1f
, 0.3f
, 0.85f
} };
108 static void colour_code_v3( char cc
, v3f target
)
110 if( cc
>= 'a' && cc
<= 'z' )
114 if( id
< vg_list_size( colour_sets
) )
116 v3_copy( colour_sets
[ id
], target
);
121 v3_copy( (v3f
){0.0f
,0.0f
,0.0f
}, target
);
130 static void init_mesh( struct mesh
*m
, float *tris
, u32 length
)
132 m
->elements
= length
/3;
133 glGenVertexArrays( 1, &m
->vao
);
134 glGenBuffers( 1, &m
->vbo
);
136 glBindVertexArray( m
->vao
);
137 glBindBuffer( GL_ARRAY_BUFFER
, m
->vbo
);
138 glBufferData( GL_ARRAY_BUFFER
, length
*sizeof(float), tris
, GL_STATIC_DRAW
);
140 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2 * sizeof(float), (void*)0 );
141 glEnableVertexAttribArray( 0 );
146 static void free_mesh( struct mesh
*m
)
148 glDeleteVertexArrays( 1, &m
->vao
);
149 glDeleteBuffers( 1, &m
->vbo
);
152 static void draw_mesh( int const start
, int const count
)
154 glDrawArrays( GL_TRIANGLES
, start
*3, count
*3 );
157 static void use_mesh( struct mesh
*m
)
159 glBindVertexArray( m
->vao
);
181 // TODO: Split into input/output structures
191 struct mesh tile
, circle
;
193 GLuint background_data
;
195 int selected
, tile_x
, tile_y
;
213 static void map_free(void)
215 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
216 arrfree( world
.io
[ i
].conditions
);
218 arrfree( world
.data
);
227 static void map_reclassify( v2i start
, v2i end
);
228 static int map_load( const char *str
)
237 if( str
[world
.w
] == ';' )
239 else if( !str
[world
.w
] )
241 vg_error( "Unexpected EOF when parsing level\n" );
246 struct cell
*row
= arraddnptr( world
.data
, world
.w
);
248 int reg_start
= 0, reg_end
= 0;
264 if( reg_start
< reg_end
)
266 if( *c
>= 'a' && *c
<= 'z' )
268 arrpush( world
.io
[ reg_start
].conditions
, *c
);
272 if( *c
== ',' || *c
== '\n' )
281 vg_error( "Unkown attribute '%c' (row: %u)\n", *c
, world
.h
);
288 vg_error( "Too many values to assign (row: %u)\n", world
.h
);
296 if( reg_start
!= reg_end
)
298 vg_error( "Not enough values assigned (row: %u, %u of %u)\n", world
.h
, reg_start
, reg_end
);
304 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world
.h
, cx
, world
.w
);
308 row
= arraddnptr( world
.data
, world
.w
);
311 reg_end
= reg_start
= arrlen( world
.io
);
317 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world
.h
, cx
, world
.w
);
321 // Tile initialization
324 if( *c
== '+' || *c
== '-' )
326 struct cell_terminal term
= { .id
= cx
+ world
.h
*world
.w
};
327 arrpush( world
.io
, term
);
328 row
[ cx
++ ].state
= *c
== '+'? FLAG_INPUT
: FLAG_OUTPUT
;
333 row
[ cx
++ ].state
= FLAG_WALL
;
337 row
[ cx
++ ].state
= 0x00;
344 map_reclassify( NULL
, NULL
);
345 vg_success( "Map loaded! (%u:%u)\n", world
.w
, world
.h
);
349 static struct cell
*pcell( v2i pos
)
351 return &world
.data
[ pos
[1]*world
.w
+ pos
[0] ];
354 int main( int argc
, char *argv
[] )
356 vg_init( argc
, argv
, "Fish (Marbles Computer) Ladder Simulator 2022 | N,M: change level | SPACE: Test | LeftClick: Toggle tile" );
365 0.0f
, 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
,
366 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
, 0.0f
,
368 0.48f
, 0.48f
, 0.5f
, 0.52f
, 0.52f
, 0.52f
, // Static dot
369 0.375f
, 0.25f
, 0.5f
, 0.75f
, 0.625f
, 0.25f
, // Downwards pointing arrow
370 0.25f
, 0.625f
, 0.75f
, 0.5f
, 0.25f
, 0.375f
, // Left
371 0.625f
, 0.75f
, 0.5f
, 0.25f
, 0.375f
, 0.75f
, // up
372 0.75f
, 0.375f
, 0.25f
, 0.5f
, 0.75f
, 0.625f
375 init_mesh( &world
.tile
, quad_mesh
, vg_list_size(quad_mesh
) );
380 float circle_mesh
[32*6*3];
381 int res
= vg_list_size( circle_mesh
) / (6*3);
383 for( int i
= 0; i
< res
; i
++ )
385 v2f v0
= { sinf( ((float)i
/(float)res
)*VG_TAUf
), cosf( ((float)i
/(float)res
)*VG_TAUf
) };
386 v2f v1
= { sinf( ((float)(i
+1)/(float)res
)*VG_TAUf
), cosf( ((float)(i
+1)/(float)res
)*VG_TAUf
) };
388 circle_mesh
[ i
*6+0 ] = 0.0f
;
389 circle_mesh
[ i
*6+1 ] = 0.0f
;
391 v2_copy( v0
, circle_mesh
+ 32*6 + i
*12 );
392 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+2 );
393 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+4 );
395 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+6 );
396 v2_muls( v1
, 0.8f
, circle_mesh
+ 32*6 + i
*12+8 );
397 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+10 );
399 v2_copy( v0
, circle_mesh
+ i
*6+4 );
400 v2_copy( v1
, circle_mesh
+ i
*6+2 );
401 v2_copy( v0
, circle_mesh
+i
*6+4 );
402 v2_copy( v1
, circle_mesh
+i
*6+2 );
405 init_mesh( &world
.circle
, circle_mesh
, vg_list_size( circle_mesh
) );
408 // Create info data texture
409 glGenTextures( &world
.background_data
);
410 glBindTexture( GL_TEXTURE_2D
, texture_name
);
411 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RGBA
, 64, 64, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
414 resource_load_main();
416 map_load( level_pack
[ 0 ] );
421 resource_free_main();
423 free_mesh( &world
.tile
);
424 free_mesh( &world
.circle
);
429 static int cell_interactive( v2i co
)
432 if( co
[0] < 2 || co
[0] >= world
.w
-2 || co
[1] < 2 || co
[1] >= world
.h
-2 )
436 if( world
.data
[ world
.w
*co
[1] + co
[0] ].state
& (FLAG_WALL
|FLAG_INPUT
|FLAG_OUTPUT
) )
439 // List of 3x3 configurations that we do not allow
440 static u32 invalid_src
[][9] =
472 // Statically compile invalid configurations into bitmasks
473 static u32 invalid
[ vg_list_size(invalid_src
) ];
475 for( int i
= 0; i
< vg_list_size(invalid_src
); i
++ )
479 for( int j
= 0; j
< 3; j
++ )
480 for( int k
= 0; k
< 3; k
++ )
481 comped
|= invalid_src
[i
][ j
*3+k
] << ((j
*5)+k
);
486 // Extract 5x5 grid surrounding tile
488 for( int y
= co
[1]-2; y
< co
[1]+3; y
++ )
489 for( int x
= co
[0]-2; x
< co
[0]+3; x
++ )
491 struct cell
*cell
= pcell((v2i
){x
,y
});
493 if( cell
&& (cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
)) )
494 blob
|= 0x1 << ((y
-(co
[1]-2))*5 + x
-(co
[0]-2));
497 // Run filter over center 3x3 grid to check for invalid configurations
498 int kernel
[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
499 for( int i
= 0; i
< vg_list_size(kernel
); i
++ )
501 if( blob
& (0x1 << (6+kernel
[i
])) )
503 u32 window
= blob
>> kernel
[i
];
505 for( int j
= 0; j
< vg_list_size(invalid
); j
++ )
506 if((window
& invalid
[j
]) == invalid
[j
])
514 static void map_reclassify( v2i start
, v2i end
)
516 v2i full_start
= { 1,1 };
517 v2i full_end
= { world
.w
-1, world
.h
-1 };
525 for( int y
= vg_max( start
[1], full_start
[1] ); y
< vg_min( end
[1], full_end
[1] ); y
++ )
527 for( int x
= vg_max( start
[0], full_start
[0] ); x
< vg_min( end
[0], full_end
[0] ); x
++ )
529 v2i dirs
[] = {{1,0},{0,1},{-1,0},{0,-1}};
533 if( pcell((v2i
){x
,y
})->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
535 for( int i
= 0; i
< vg_list_size( dirs
); i
++ )
537 struct cell
*neighbour
= pcell((v2i
){x
+dirs
[i
][0], y
+dirs
[i
][1]});
538 if( neighbour
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
544 pcell((v2i
){x
,y
})->config
= config
;
549 v2f
const curve_3
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
550 v2f
const curve_6
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.375f
,0.5f
},{0.0f
,0.5f
}};
551 v2f
const curve_9
[] = {{1.0f
,0.5f
},{0.625f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
552 v2f
const curve_12
[]= {{0.0f
,0.5f
},{0.375f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
554 v2f
const curve_7
[] = {{0.5f
,0.8438f
},{0.875f
,0.8438f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
555 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
}};
557 float const curve_7_linear_section
= 0.1562f
;
561 static int curlevel
= 0;
562 int changelvl
= curlevel
;
563 if( vg_get_button_down( "prev" ) ) { if( curlevel
> 0 ) changelvl
--; }
564 else if( vg_get_button_down( "next" ) ) { if( curlevel
< vg_list_size( level_pack
)-1 ) changelvl
++; }
566 if( changelvl
!= curlevel
)
568 map_load( level_pack
[ changelvl
] );
569 curlevel
= changelvl
;
572 world
.simulating
= 0;
573 world
.num_fishes
= 0;
576 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
577 world
.io
[i
].recv_count
= 0;
579 vg_info( "Stopping simulation!\n" );
584 float r1
= (float)vg_window_y
/ (float)vg_window_x
,
585 r2
= (float)world
.h
/ (float)world
.w
,
588 size
= ( r2
< r1
? (float)world
.w
* 0.5f
: ((float)world
.h
* 0.5f
) / r1
) + 2.5f
;
589 m3x3_projection( m_projection
, -size
, size
, -size
*r1
, size
*r1
);
592 origin
[0] = floorf( -0.5f
* world
.w
);
593 origin
[1] = floorf( -0.5f
* world
.h
);
596 m3x3_identity( m_view
);
597 m3x3_translate( m_view
, origin
);
598 m3x3_mul( m_projection
, m_view
, vg_pv
);
599 vg_projection_update();
602 v2_copy( vg_mouse_ws
, world
.tile_pos
);
604 world
.tile_x
= floorf( world
.tile_pos
[0] );
605 world
.tile_y
= floorf( world
.tile_pos
[1] );
608 if( !world
.simulating
)
610 if( cell_interactive( (v2i
){ world
.tile_x
, world
.tile_y
} ))
612 world
.selected
= world
.tile_y
* world
.w
+ world
.tile_x
;
614 if( vg_get_button_down("primary") )
616 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
618 if( world
.data
[ world
.selected
].state
& FLAG_CANAL
)
619 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 3, 6 );
621 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 0, 3 );
623 map_reclassify( (v2i
){ world
.tile_x
-2, world
.tile_y
-2 },
624 (v2i
){ world
.tile_x
+2, world
.tile_y
+2 } );
630 else world
.selected
= -1;
632 // Simulation stop/start
633 if( vg_get_button_down("go") )
635 if( world
.simulating
)
637 world
.simulating
= 0;
638 world
.num_fishes
= 0;
641 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
642 world
.io
[i
].recv_count
= 0;
644 vg_info( "Stopping simulation!\n" );
646 sfx_system_fadeout( &audio_system_balls_rolling
, 44100 );
650 vg_success( "Starting simulation!\n" );
652 sfx_set_playrnd( &audio_rolls
, &audio_system_balls_rolling
, 0, 1 );
654 world
.simulating
= 1;
655 world
.num_fishes
= 0;
657 world
.sim_start
= vg_time
;
659 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
661 world
.data
[ i
].state
&= ~FLAG_FLIP_FLOP
;
664 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
665 world
.io
[i
].recv_count
= 0;
670 if( world
.simulating
)
672 while( world
.sim_frame
< (int)((vg_time
-world
.sim_start
)*2.0f
) )
674 //vg_info( "frame: %u\n", world.sim_frame );
675 sfx_set_playrnd( &audio_random
, &audio_system_balls_switching
, 0, 9 );
677 // Update splitter deltas
678 for( int i
= 0; i
< world
.h
*world
.w
; i
++ )
680 struct cell
*cell
= &world
.data
[i
];
681 if( cell
->config
== k_cell_type_split
)
683 cell
->state
&= ~FLAG_FLIP_ROTATING
;
687 // Update fish positions
688 for( int i
= 0; i
< world
.num_fishes
; i
++ )
690 struct fish
*fish
= &world
.fishes
[i
];
691 struct cell
*cell_current
= pcell( fish
->pos
);
693 if( fish
->alive
== -1 )
696 if( fish
->alive
!= 1 )
700 if( cell_current
->state
& FLAG_OUTPUT
)
702 for( int j
= 0; j
< arrlen( world
.io
); j
++ )
704 struct cell_terminal
*term
= &world
.io
[j
];
706 if( term
->id
== fish
->pos
[1]*world
.w
+ fish
->pos
[0] )
708 term
->recv
[ term
->recv_count
++ ] = fish
->payload
;
717 if( cell_current
->config
== k_cell_type_split
)
720 fish
->dir
[0] = cell_current
->state
&FLAG_FLIP_FLOP
?1:-1;
723 cell_current
->state
^= FLAG_FLIP_FLOP
;
725 else if( cell_current
->config
== k_cell_type_merge
)
733 struct cell
*cell_next
= pcell( (v2i
){ fish
->pos
[0]+fish
->dir
[0], fish
->pos
[1]+fish
->dir
[1] } );
734 if( !(cell_next
->state
& (FLAG_CANAL
|FLAG_OUTPUT
)) )
736 // Try other directions for valid, so down, left, right..
737 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
738 vg_info( "Trying some other directions...\n" );
740 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
742 if( (dirs
[j
][0] == -fish
->dir
[0]) && (dirs
[j
][1] == -fish
->dir
[1]) )
745 if( pcell( (v2i
){ fish
->pos
[0]+dirs
[j
][0], fish
->pos
[1]+dirs
[j
][1] } )->state
& (FLAG_CANAL
|FLAG_OUTPUT
) )
747 fish
->dir
[0] = dirs
[j
][0];
748 fish
->dir
[1] = dirs
[j
][1];
754 fish
->pos
[0] += fish
->dir
[0];
755 fish
->pos
[1] += fish
->dir
[1];
757 struct cell
*cell_entry
= pcell( fish
->pos
);
759 if( !(cell_entry
->state
& (FLAG_INPUT
|FLAG_CANAL
|FLAG_OUTPUT
) ))
765 if( cell_entry
->config
== k_cell_type_split
||
766 cell_entry
->config
== k_cell_type_ramp_right
||
767 cell_entry
->config
== k_cell_type_ramp_left
)
769 // Special death (FALL)
770 v2_sub( fish
->physics_co
, fish
->physics_v
, fish
->physics_v
);
771 v2_divs( fish
->physics_v
, vg_time_delta
, fish
->physics_v
);
774 vg_warn( "Special death (fall)\n" );
779 if( cell_entry
->config
== k_cell_type_split
)
781 sfx_set_playrnd( &audio_splitter
, &audio_system_balls_important
, 0, 1 );
782 cell_entry
->state
|= FLAG_FLIP_ROTATING
;
787 // Check for collisions
788 for( int i
= 0; i
< world
.num_fishes
; i
++ )
790 if( world
.fishes
[i
].alive
== 1 )
792 for( int j
= i
+1; j
< world
.num_fishes
; j
++ )
794 if( (world
.fishes
[j
].alive
== 1) && (world
.fishes
[i
].pos
[0] == world
.fishes
[j
].pos
[0]) &&
795 (world
.fishes
[i
].pos
[1] == world
.fishes
[j
].pos
[1]) )
797 // Shatter death (+0.5s)
798 world
.fishes
[i
].alive
= -1;
799 world
.fishes
[j
].alive
= -1;
800 world
.fishes
[i
].death_time
= 0.5f
;
801 world
.fishes
[j
].death_time
= 0.5f
;
808 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
810 struct cell_terminal
*term
= &world
.io
[ i
];
811 int posx
= term
->id
% world
.w
;
812 int posy
= (term
->id
- posx
)/world
.w
;
813 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
817 if( world
.sim_frame
< arrlen( term
->conditions
) )
819 struct fish
*fish
= &world
.fishes
[world
.num_fishes
++];
823 fish
->payload
= term
->conditions
[world
.sim_frame
];
827 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
828 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
829 if( pcell( (v2i
){ posx
+dirs
[j
][0], posy
+dirs
[j
][1] } )->state
& FLAG_CANAL
)
831 fish
->dir
[0] = dirs
[j
][0];
832 fish
->dir
[1] = dirs
[j
][1];
846 float scaled_time
= 0.0f
;
847 scaled_time
= (vg_time
-world
.sim_start
)*2.0f
;
848 world
.frame_lerp
= scaled_time
- (float)world
.sim_frame
;
851 for( int i
= 0; i
< world
.num_fishes
; i
++ )
853 struct fish
*fish
= &world
.fishes
[i
];
855 if( fish
->alive
== 0 )
858 if( fish
->alive
== -1 && (world
.frame_lerp
> fish
->death_time
) )
859 continue; // Todo: particle thing?
861 if( fish
->alive
== -2 )
863 v2_muladds( fish
->physics_v
, (v2f
){ 0.0, -9.8f
}, vg_time_delta
, fish
->physics_v
);
864 v2_muladds( fish
->physics_co
, fish
->physics_v
, vg_time_delta
, fish
->physics_co
);
868 struct cell
*cell
= pcell(fish
->pos
);
871 float t
= world
.frame_lerp
;
873 v2_copy( fish
->physics_co
, fish
->physics_v
);
875 switch( cell
->config
)
878 if( fish
->dir
[0] == 1 )
883 case 3: curve
= curve_3
; break;
884 case 6: curve
= curve_6
; break;
885 case 9: curve
= curve_9
; break;
886 case 12: curve
= curve_12
; break;
888 if( t
> curve_7_linear_section
)
890 t
-= curve_7_linear_section
;
891 t
*= (1.0f
/(1.0f
-curve_7_linear_section
));
893 curve
= cell
->state
& FLAG_FLIP_FLOP
? curve_7
: curve_7_1
;
897 default: curve
= NULL
; break;
903 float t3
= t
* t
* t
;
905 float cA
= 3.0f
*t2
- 3.0f
*t3
;
906 float cB
= 3.0f
*t3
- 6.0f
*t2
+ 3.0f
*t
;
907 float cC
= 3.0f
*t2
- t3
- 3.0f
*t
+ 1.0f
;
909 fish
->physics_co
[0] = t3
*curve
[3][0] + cA
*curve
[2][0] + cB
*curve
[1][0] + cC
*curve
[0][0];
910 fish
->physics_co
[1] = t3
*curve
[3][1] + cA
*curve
[2][1] + cB
*curve
[1][1] + cC
*curve
[0][1];
911 fish
->physics_co
[0] += (float)fish
->pos
[0];
912 fish
->physics_co
[1] += (float)fish
->pos
[1];
917 origin
[0] = (float)fish
->pos
[0] + (float)fish
->dir
[0]*-0.5f
+ 0.5f
;
918 origin
[1] = (float)fish
->pos
[1] + (float)fish
->dir
[1]*-0.5f
+ 0.5f
;
920 fish
->physics_co
[0] = origin
[0] + (float)fish
->dir
[0]*t
;
921 fish
->physics_co
[1] = origin
[1] + (float)fish
->dir
[1]*t
;
928 static void render_tiles( v2i start
, v2i end
, v4f
const regular_colour
, v4f
const selected_colour
)
930 v2i full_start
= { 0,0 };
931 v2i full_end
= { world
.w
, world
.h
};
939 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
941 for( int y
= start
[1]; y
< end
[1]; y
++ )
943 for( int x
= start
[0]; x
< end
[0]; x
++ )
945 struct cell
*cell
= pcell((v2i
){x
,y
});
946 int selected
= world
.selected
== y
*world
.w
+ x
;
948 int tile_offsets
[][2] =
950 {2, 0}, {0, 3}, {0, 2}, {2, 2},
951 {1, 0}, {2, 3}, {3, 2}, {1, 3},
952 {3, 1}, {0, 1}, {1, 2}, {2, 1},
953 {1, 1}, {3, 3}, {2, 1}, {2, 1}
956 int uv
[2] = { 3, 0 };
958 if( cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
960 uv
[0] = tile_offsets
[ cell
->config
][0];
961 uv
[1] = tile_offsets
[ cell
->config
][1];
964 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
, uv
[0], uv
[1] );
967 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, selected_colour
);
969 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
979 glViewport( 0,0, vg_window_x
, vg_window_y
);
981 glDisable( GL_DEPTH_TEST
);
982 glClearColor( 0.369768f
, 0.3654f
, 0.42f
, 1.0f
);
983 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
985 v4f
const colour_default
= {1.0f
, 1.0f
, 1.0f
, 1.0f
};
986 v4f
const colour_selected
= {0.90f
, 0.92f
, 1.0f
, 1.0f
};
988 // TILE SET RENDERING
989 // todo: just slam everything into a mesh...
990 // when user modifies a tile the neighbours can be easily uploaded to gpu mem
992 // Currently we're uploading a fair amount of data every frame anyway.
993 // NOTE: this is for final optimisations ONLY!
994 // ======================================================================
996 use_mesh( &world
.tile
);
1002 SHADER_USE( shader_background
);
1003 glUniformMatrix3fv( SHADER_UNIFORM( shader_background
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1005 vg_tex2d_bind( &tex_background
, 0 );
1006 glUniform1i( SHADER_UNIFORM( shader_background
, "uTexMain" ), 0 );
1008 glUniform3f( SHADER_UNIFORM( shader_background
, "uOffset" ), -50, -50, 100 );
1015 SHADER_USE( shader_tile_main
);
1018 m2x2_identity( subtransform
);
1019 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1020 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_main
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1021 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 0.0f
);
1022 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uForeground" ), 0.0f
);
1025 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1026 glBlendEquation(GL_FUNC_ADD
);
1029 vg_tex2d_bind( &tex_tile_data
, 0 );
1030 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
1032 vg_tex2d_bind( &tex_wood
, 1 );
1033 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
1035 render_tiles( NULL
, NULL
, colour_default
, colour_default
);
1039 SHADER_USE( shader_ball
);
1040 glUniformMatrix3fv( SHADER_UNIFORM( shader_ball
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1042 vg_tex2d_bind( &tex_ball
, 0 );
1043 glUniform1i( SHADER_UNIFORM( shader_ball
, "uTexMain" ), 0 );
1046 if( world
.simulating
)
1048 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1050 struct fish
*fish
= &world
.fishes
[i
];
1052 if( fish
->alive
== 0 )
1055 if( fish
->alive
== -1 && (world
.frame_lerp
> fish
->death_time
) )
1058 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
1059 colour_code_v3( fish
->payload
, dot_colour
);
1061 glUniform3fv( SHADER_UNIFORM( shader_ball
, "uColour" ), 1, dot_colour
);
1062 glUniform2fv( SHADER_UNIFORM( shader_ball
, "uOffset" ), 1, fish
->physics_co
);
1067 SHADER_USE( shader_tile_main
);
1070 vg_tex2d_bind( &tex_tile_data
, 0 );
1071 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
1073 vg_tex2d_bind( &tex_wood
, 1 );
1074 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
1076 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uForeground" ), 1.0f
);
1077 render_tiles( NULL
, NULL
, colour_default
, colour_selected
);
1081 for( int y
= 0; y
< world
.h
; y
++ )
1083 for( int x
= 0; x
< world
.w
; x
++ )
1085 struct cell
*cell
= pcell((v2i
){x
,y
});
1087 if( cell
->config
== k_cell_type_split
)
1089 float rotation
= cell
->state
& FLAG_FLIP_FLOP
? vg_rad( -45.0f
): vg_rad( 45.0f
);
1091 if( cell
->state
& FLAG_FLIP_ROTATING
)
1093 if( (world
.frame_lerp
> curve_7_linear_section
) )
1095 float const rotation_speed
= 0.4f
;
1096 if( (world
.frame_lerp
< 1.0f
-rotation_speed
) )
1098 float t
= world
.frame_lerp
- curve_7_linear_section
;
1099 t
*= -2.0f
* (1.0f
/(1.0f
-(curve_7_linear_section
+rotation_speed
)));
1109 m2x2_create_rotation( subtransform
, rotation
);
1111 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1112 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
+ 0.125f
, 0.0f
, 0.0f
);
1119 if( world
.selected
!= -1 && !(world
.data
[ world
.selected
].state
& FLAG_CANAL
) )
1121 v2i new_begin
= { world
.tile_x
- 2, world
.tile_y
- 2 };
1122 v2i new_end
= { world
.tile_x
+ 2, world
.tile_y
+ 2 };
1124 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
1125 map_reclassify( new_begin
, new_end
);
1127 m2x2_identity( subtransform
);
1128 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 1.0f
);
1129 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1130 glUniform2fv( SHADER_UNIFORM( shader_tile_main
, "uMousePos" ), 1, world
.tile_pos
);
1132 render_tiles( new_begin
, new_end
, colour_default
, colour_default
);
1134 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
1135 map_reclassify( new_begin
, new_end
);
1138 //glDisable(GL_BLEND);
1140 glDisable(GL_BLEND
);
1142 SHADER_USE( shader_tile_colour
);
1143 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1144 use_mesh( &world
.circle
);
1147 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1149 struct cell_terminal
*term
= &world
.io
[ i
];
1150 int posx
= term
->id
% world
.w
;
1151 int posy
= (term
->id
- posx
)/world
.w
;
1152 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1154 int const filled_start
= 0;
1155 int const filled_count
= 32;
1156 int const empty_start
= 32;
1157 int const empty_count
= 32*2;
1159 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
1161 for( int j
= 0; j
< arrlen( term
->conditions
); j
++ )
1163 float y_offset
= is_input
? 0.8f
: 0.2f
;
1164 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)posx
+ 0.2f
+ 0.2f
* (float)j
, (float)posy
+ y_offset
, 0.1f
);
1168 colour_code_v3( term
->conditions
[j
], dot_colour
);
1169 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1171 // Draw filled if tick not passed, draw empty if empty
1172 if( world
.sim_frame
> j
)
1173 draw_mesh( empty_start
, empty_count
);
1175 draw_mesh( filled_start
, filled_count
);
1179 if( term
->recv_count
> j
)
1181 colour_code_v3( term
->recv
[j
], dot_colour
);
1182 v3_muls( dot_colour
, 0.8f
, dot_colour
);
1183 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1185 draw_mesh( filled_start
, filled_count
);
1188 colour_code_v3( term
->conditions
[j
], dot_colour
);
1189 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1191 draw_mesh( empty_start
, empty_count
);
1196 if( world
.simulating
)
1198 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.0f
, 0.0f
, 0.0f
, 1.0f
);
1199 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
);