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 int selected
, tile_x
, tile_y
;
211 static void map_free(void)
213 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
214 arrfree( world
.io
[ i
].conditions
);
216 arrfree( world
.data
);
225 static void map_reclassify( v2i start
, v2i end
);
226 static int map_load( const char *str
)
235 if( str
[world
.w
] == ';' )
237 else if( !str
[world
.w
] )
239 vg_error( "Unexpected EOF when parsing level\n" );
244 struct cell
*row
= arraddnptr( world
.data
, world
.w
);
246 int reg_start
= 0, reg_end
= 0;
262 if( reg_start
< reg_end
)
264 if( *c
>= 'a' && *c
<= 'z' )
266 arrpush( world
.io
[ reg_start
].conditions
, *c
);
270 if( *c
== ',' || *c
== '\n' )
279 vg_error( "Unkown attribute '%c' (row: %u)\n", *c
, world
.h
);
286 vg_error( "Too many values to assign (row: %u)\n", world
.h
);
294 if( reg_start
!= reg_end
)
296 vg_error( "Not enough values assigned (row: %u, %u of %u)\n", world
.h
, reg_start
, reg_end
);
302 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world
.h
, cx
, world
.w
);
306 row
= arraddnptr( world
.data
, world
.w
);
309 reg_end
= reg_start
= arrlen( world
.io
);
315 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world
.h
, cx
, world
.w
);
319 // Tile initialization
322 if( *c
== '+' || *c
== '-' )
324 struct cell_terminal term
= { .id
= cx
+ world
.h
*world
.w
};
325 arrpush( world
.io
, term
);
326 row
[ cx
++ ].state
= *c
== '+'? FLAG_INPUT
: FLAG_OUTPUT
;
331 row
[ cx
++ ].state
= FLAG_WALL
;
335 row
[ cx
++ ].state
= 0x00;
342 map_reclassify( NULL
, NULL
);
343 vg_success( "Map loaded! (%u:%u)\n", world
.w
, world
.h
);
347 static struct cell
*pcell( v2i pos
)
349 return &world
.data
[ pos
[1]*world
.w
+ pos
[0] ];
352 int main( int argc
, char *argv
[] )
354 vg_init( argc
, argv
, "Fish (Marbles Computer) Ladder Simulator 2022 | N,M: change level | SPACE: Test | LeftClick: Toggle tile" );
363 0.0f
, 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
,
364 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
, 0.0f
,
366 0.48f
, 0.48f
, 0.5f
, 0.52f
, 0.52f
, 0.52f
, // Static dot
367 0.375f
, 0.25f
, 0.5f
, 0.75f
, 0.625f
, 0.25f
, // Downwards pointing arrow
368 0.25f
, 0.625f
, 0.75f
, 0.5f
, 0.25f
, 0.375f
, // Left
369 0.625f
, 0.75f
, 0.5f
, 0.25f
, 0.375f
, 0.75f
, // up
370 0.75f
, 0.375f
, 0.25f
, 0.5f
, 0.75f
, 0.625f
373 init_mesh( &world
.tile
, quad_mesh
, vg_list_size(quad_mesh
) );
378 float circle_mesh
[32*6*3];
379 int res
= vg_list_size( circle_mesh
) / (6*3);
381 for( int i
= 0; i
< res
; i
++ )
383 v2f v0
= { sinf( ((float)i
/(float)res
)*VG_TAUf
), cosf( ((float)i
/(float)res
)*VG_TAUf
) };
384 v2f v1
= { sinf( ((float)(i
+1)/(float)res
)*VG_TAUf
), cosf( ((float)(i
+1)/(float)res
)*VG_TAUf
) };
386 circle_mesh
[ i
*6+0 ] = 0.0f
;
387 circle_mesh
[ i
*6+1 ] = 0.0f
;
389 v2_copy( v0
, circle_mesh
+ 32*6 + i
*12 );
390 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+2 );
391 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+4 );
393 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+6 );
394 v2_muls( v1
, 0.8f
, circle_mesh
+ 32*6 + i
*12+8 );
395 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+10 );
397 v2_copy( v0
, circle_mesh
+ i
*6+4 );
398 v2_copy( v1
, circle_mesh
+ i
*6+2 );
399 v2_copy( v0
, circle_mesh
+i
*6+4 );
400 v2_copy( v1
, circle_mesh
+i
*6+2 );
403 init_mesh( &world
.circle
, circle_mesh
, vg_list_size( circle_mesh
) );
406 resource_load_main();
408 map_load( level_pack
[ 0 ] );
413 resource_free_main();
415 free_mesh( &world
.tile
);
416 free_mesh( &world
.circle
);
421 static int cell_interactive( v2i co
)
424 if( co
[0] < 2 || co
[0] >= world
.w
-2 || co
[1] < 2 || co
[1] >= world
.h
-2 )
428 if( world
.data
[ world
.w
*co
[1] + co
[0] ].state
& (FLAG_WALL
|FLAG_INPUT
|FLAG_OUTPUT
) )
431 // List of 3x3 configurations that we do not allow
432 static u32 invalid_src
[][9] =
464 // Statically compile invalid configurations into bitmasks
465 static u32 invalid
[ vg_list_size(invalid_src
) ];
467 for( int i
= 0; i
< vg_list_size(invalid_src
); i
++ )
471 for( int j
= 0; j
< 3; j
++ )
472 for( int k
= 0; k
< 3; k
++ )
473 comped
|= invalid_src
[i
][ j
*3+k
] << ((j
*5)+k
);
478 // Extract 5x5 grid surrounding tile
480 for( int y
= co
[1]-2; y
< co
[1]+3; y
++ )
481 for( int x
= co
[0]-2; x
< co
[0]+3; x
++ )
483 struct cell
*cell
= pcell((v2i
){x
,y
});
485 if( cell
&& (cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
)) )
486 blob
|= 0x1 << ((y
-(co
[1]-2))*5 + x
-(co
[0]-2));
489 // Run filter over center 3x3 grid to check for invalid configurations
490 int kernel
[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
491 for( int i
= 0; i
< vg_list_size(kernel
); i
++ )
493 if( blob
& (0x1 << (6+kernel
[i
])) )
495 u32 window
= blob
>> kernel
[i
];
497 for( int j
= 0; j
< vg_list_size(invalid
); j
++ )
498 if((window
& invalid
[j
]) == invalid
[j
])
506 static void map_reclassify( v2i start
, v2i end
)
508 v2i full_start
= { 1,1 };
509 v2i full_end
= { world
.w
-1, world
.h
-1 };
517 for( int y
= vg_max( start
[1], full_start
[1] ); y
< vg_min( end
[1], full_end
[1] ); y
++ )
519 for( int x
= vg_max( start
[0], full_start
[0] ); x
< vg_min( end
[0], full_end
[0] ); x
++ )
521 v2i dirs
[] = {{1,0},{0,1},{-1,0},{0,-1}};
525 if( pcell((v2i
){x
,y
})->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
527 for( int i
= 0; i
< vg_list_size( dirs
); i
++ )
529 struct cell
*neighbour
= pcell((v2i
){x
+dirs
[i
][0], y
+dirs
[i
][1]});
530 if( neighbour
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
536 pcell((v2i
){x
,y
})->config
= config
;
541 v2f
const curve_3
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
542 v2f
const curve_6
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.375f
,0.5f
},{0.0f
,0.5f
}};
543 v2f
const curve_9
[] = {{1.0f
,0.5f
},{0.625f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
544 v2f
const curve_12
[]= {{0.0f
,0.5f
},{0.375f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
546 v2f
const curve_7
[] = {{0.5f
,0.8438f
},{0.875f
,0.8438f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
547 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
}};
549 float const curve_7_linear_section
= 0.1562f
;
553 static int curlevel
= 0;
554 int changelvl
= curlevel
;
555 if( vg_get_button_down( "prev" ) ) { if( curlevel
> 0 ) changelvl
--; }
556 else if( vg_get_button_down( "next" ) ) { if( curlevel
< vg_list_size( level_pack
)-1 ) changelvl
++; }
558 if( changelvl
!= curlevel
)
560 map_load( level_pack
[ changelvl
] );
561 curlevel
= changelvl
;
564 world
.simulating
= 0;
565 world
.num_fishes
= 0;
568 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
569 world
.io
[i
].recv_count
= 0;
571 vg_info( "Stopping simulation!\n" );
574 float ratio
= (float)vg_window_y
/ (float)vg_window_x
;
575 float const size
= 9.5f
;
578 origin
[0] = -0.5f
* world
.w
;
579 origin
[1] = -0.5f
* world
.h
;
582 m3x3_projection( m_projection
, -size
, size
, -size
*ratio
, size
*ratio
);
583 m3x3_identity( m_view
);
584 m3x3_translate( m_view
, origin
);
585 m3x3_mul( m_projection
, m_view
, vg_pv
);
586 vg_projection_update();
589 v2_copy( vg_mouse_ws
, world
.tile_pos
);
591 world
.tile_x
= floorf( world
.tile_pos
[0] );
592 world
.tile_y
= floorf( world
.tile_pos
[1] );
595 if( !world
.simulating
)
597 if( cell_interactive( (v2i
){ world
.tile_x
, world
.tile_y
} ))
599 world
.selected
= world
.tile_y
* world
.w
+ world
.tile_x
;
601 if( vg_get_button_down("primary") )
603 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
605 if( world
.data
[ world
.selected
].state
& FLAG_CANAL
)
606 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 3, 6 );
608 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 0, 3 );
610 map_reclassify( (v2i
){ world
.tile_x
-2, world
.tile_y
-2 },
611 (v2i
){ world
.tile_x
+2, world
.tile_y
+2 } );
617 else world
.selected
= -1;
619 // Simulation stop/start
620 if( vg_get_button_down("go") )
622 if( world
.simulating
)
624 world
.simulating
= 0;
625 world
.num_fishes
= 0;
628 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
629 world
.io
[i
].recv_count
= 0;
631 vg_info( "Stopping simulation!\n" );
633 sfx_system_fadeout( &audio_system_balls_rolling
, 44100 );
637 vg_success( "Starting simulation!\n" );
639 sfx_set_playrnd( &audio_rolls
, &audio_system_balls_rolling
, 0, 1 );
641 world
.simulating
= 1;
642 world
.num_fishes
= 0;
644 world
.sim_start
= vg_time
;
646 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
648 world
.data
[ i
].state
&= ~FLAG_FLIP_FLOP
;
651 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
652 world
.io
[i
].recv_count
= 0;
657 if( world
.simulating
)
659 while( world
.sim_frame
< (int)((vg_time
-world
.sim_start
)*2.0f
) )
661 //vg_info( "frame: %u\n", world.sim_frame );
662 sfx_set_playrnd( &audio_random
, &audio_system_balls_switching
, 0, 9 );
664 // Update splitter deltas
665 for( int i
= 0; i
< world
.h
*world
.w
; i
++ )
667 struct cell
*cell
= &world
.data
[i
];
668 if( cell
->config
== k_cell_type_split
)
670 cell
->state
&= ~FLAG_FLIP_ROTATING
;
674 // Update fish positions
675 for( int i
= 0; i
< world
.num_fishes
; i
++ )
677 struct fish
*fish
= &world
.fishes
[i
];
678 struct cell
*cell_current
= pcell( fish
->pos
);
680 if( fish
->alive
== -1 )
683 if( fish
->alive
!= 1 )
687 if( cell_current
->state
& FLAG_OUTPUT
)
689 for( int j
= 0; j
< arrlen( world
.io
); j
++ )
691 struct cell_terminal
*term
= &world
.io
[j
];
693 if( term
->id
== fish
->pos
[1]*world
.w
+ fish
->pos
[0] )
695 term
->recv
[ term
->recv_count
++ ] = fish
->payload
;
704 if( cell_current
->config
== k_cell_type_split
)
707 fish
->dir
[0] = cell_current
->state
&FLAG_FLIP_FLOP
?1:-1;
710 cell_current
->state
^= FLAG_FLIP_FLOP
;
712 else if( cell_current
->config
== k_cell_type_merge
)
720 struct cell
*cell_next
= pcell( (v2i
){ fish
->pos
[0]+fish
->dir
[0], fish
->pos
[1]+fish
->dir
[1] } );
721 if( !(cell_next
->state
& (FLAG_CANAL
|FLAG_OUTPUT
)) )
723 // Try other directions for valid, so down, left, right..
724 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
725 vg_info( "Trying some other directions...\n" );
727 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
729 if( (dirs
[j
][0] == -fish
->dir
[0]) && (dirs
[j
][1] == -fish
->dir
[1]) )
732 if( pcell( (v2i
){ fish
->pos
[0]+dirs
[j
][0], fish
->pos
[1]+dirs
[j
][1] } )->state
& (FLAG_CANAL
|FLAG_OUTPUT
) )
734 fish
->dir
[0] = dirs
[j
][0];
735 fish
->dir
[1] = dirs
[j
][1];
741 fish
->pos
[0] += fish
->dir
[0];
742 fish
->pos
[1] += fish
->dir
[1];
744 struct cell
*cell_entry
= pcell( fish
->pos
);
746 if( !(cell_entry
->state
& (FLAG_INPUT
|FLAG_CANAL
|FLAG_OUTPUT
) ))
752 if( cell_entry
->config
== k_cell_type_split
||
753 cell_entry
->config
== k_cell_type_ramp_right
||
754 cell_entry
->config
== k_cell_type_ramp_left
)
756 // Special death (FALL)
757 v2_sub( fish
->physics_co
, fish
->physics_v
, fish
->physics_v
);
758 v2_divs( fish
->physics_v
, vg_time_delta
, fish
->physics_v
);
761 vg_warn( "Special death (fall)\n" );
766 if( cell_entry
->config
== k_cell_type_split
)
768 sfx_set_playrnd( &audio_splitter
, &audio_system_balls_important
, 0, 1 );
769 cell_entry
->state
|= FLAG_FLIP_ROTATING
;
774 // Check for collisions
775 for( int i
= 0; i
< world
.num_fishes
; i
++ )
777 if( world
.fishes
[i
].alive
== 1 )
779 for( int j
= i
+1; j
< world
.num_fishes
; j
++ )
781 if( (world
.fishes
[j
].alive
== 1) && (world
.fishes
[i
].pos
[0] == world
.fishes
[j
].pos
[0]) &&
782 (world
.fishes
[i
].pos
[1] == world
.fishes
[j
].pos
[1]) )
784 // Shatter death (+0.5s)
785 world
.fishes
[i
].alive
= -1;
786 world
.fishes
[j
].alive
= -1;
787 world
.fishes
[i
].death_time
= 0.5f
;
788 world
.fishes
[j
].death_time
= 0.5f
;
795 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
797 struct cell_terminal
*term
= &world
.io
[ i
];
798 int posx
= term
->id
% world
.w
;
799 int posy
= (term
->id
- posx
)/world
.w
;
800 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
804 if( world
.sim_frame
< arrlen( term
->conditions
) )
806 struct fish
*fish
= &world
.fishes
[world
.num_fishes
++];
810 fish
->payload
= term
->conditions
[world
.sim_frame
];
814 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
815 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
816 if( pcell( (v2i
){ posx
+dirs
[j
][0], posy
+dirs
[j
][1] } )->state
& FLAG_CANAL
)
818 fish
->dir
[0] = dirs
[j
][0];
819 fish
->dir
[1] = dirs
[j
][1];
833 float scaled_time
= 0.0f
;
834 scaled_time
= (vg_time
-world
.sim_start
)*2.0f
;
835 world
.frame_lerp
= scaled_time
- (float)world
.sim_frame
;
838 for( int i
= 0; i
< world
.num_fishes
; i
++ )
840 struct fish
*fish
= &world
.fishes
[i
];
842 if( fish
->alive
== 0 )
845 if( fish
->alive
== -1 && (world
.frame_lerp
> fish
->death_time
) )
846 continue; // Todo: particle thing?
848 if( fish
->alive
== -2 )
850 v2_muladds( fish
->physics_v
, (v2f
){ 0.0, -9.8f
}, vg_time_delta
, fish
->physics_v
);
851 v2_muladds( fish
->physics_co
, fish
->physics_v
, vg_time_delta
, fish
->physics_co
);
855 struct cell
*cell
= pcell(fish
->pos
);
858 float t
= world
.frame_lerp
;
860 v2_copy( fish
->physics_co
, fish
->physics_v
);
862 switch( cell
->config
)
865 if( fish
->dir
[0] == 1 )
870 case 3: curve
= curve_3
; break;
871 case 6: curve
= curve_6
; break;
872 case 9: curve
= curve_9
; break;
873 case 12: curve
= curve_12
; break;
875 if( t
> curve_7_linear_section
)
877 t
-= curve_7_linear_section
;
878 t
*= (1.0f
/(1.0f
-curve_7_linear_section
));
880 curve
= cell
->state
& FLAG_FLIP_FLOP
? curve_7
: curve_7_1
;
884 default: curve
= NULL
; break;
890 float t3
= t
* t
* t
;
892 float cA
= 3.0f
*t2
- 3.0f
*t3
;
893 float cB
= 3.0f
*t3
- 6.0f
*t2
+ 3.0f
*t
;
894 float cC
= 3.0f
*t2
- t3
- 3.0f
*t
+ 1.0f
;
896 fish
->physics_co
[0] = t3
*curve
[3][0] + cA
*curve
[2][0] + cB
*curve
[1][0] + cC
*curve
[0][0];
897 fish
->physics_co
[1] = t3
*curve
[3][1] + cA
*curve
[2][1] + cB
*curve
[1][1] + cC
*curve
[0][1];
898 fish
->physics_co
[0] += (float)fish
->pos
[0];
899 fish
->physics_co
[1] += (float)fish
->pos
[1];
904 origin
[0] = (float)fish
->pos
[0] + (float)fish
->dir
[0]*-0.5f
+ 0.5f
;
905 origin
[1] = (float)fish
->pos
[1] + (float)fish
->dir
[1]*-0.5f
+ 0.5f
;
907 fish
->physics_co
[0] = origin
[0] + (float)fish
->dir
[0]*t
;
908 fish
->physics_co
[1] = origin
[1] + (float)fish
->dir
[1]*t
;
915 static void render_tiles( v2i start
, v2i end
, v4f
const regular_colour
, v4f
const selected_colour
)
917 v2i full_start
= { 0,0 };
918 v2i full_end
= { world
.w
, world
.h
};
926 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
928 for( int y
= start
[1]; y
< end
[1]; y
++ )
930 for( int x
= start
[0]; x
< end
[0]; 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
|FLAG_INPUT
|FLAG_OUTPUT
) )
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] );
954 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, selected_colour
);
956 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
966 glViewport( 0,0, vg_window_x
, vg_window_y
);
968 glDisable( GL_DEPTH_TEST
);
969 glClearColor( 0.8f
, 0.8f
, 0.8f
, 1.0f
);
970 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
972 v4f
const colour_default
= {1.0f
, 1.0f
, 1.0f
, 1.0f
};
973 v4f
const colour_selected
= {0.90f
, 0.92f
, 1.0f
, 1.0f
};
975 // TILE SET RENDERING
976 // todo: just slam everything into a mesh...
977 // when user modifies a tile the neighbours can be easily uploaded to gpu mem
979 // Currently we're uploading a fair amount of data every frame anyway.
980 // NOTE: this is for final optimisations ONLY!
981 // ======================================================================
983 use_mesh( &world
.tile
);
985 SHADER_USE( shader_tile_main
);
988 m2x2_identity( subtransform
);
989 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
990 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_main
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
991 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 0.0f
);
994 vg_tex2d_bind( &tex_tile_data
, 0 );
995 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
997 vg_tex2d_bind( &tex_wood
, 1 );
998 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
1000 render_tiles( NULL
, NULL
, colour_default
, colour_default
);
1003 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1004 glBlendEquation(GL_FUNC_ADD
);
1006 SHADER_USE( shader_ball
);
1007 glUniformMatrix3fv( SHADER_UNIFORM( shader_ball
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1009 vg_tex2d_bind( &tex_ball
, 0 );
1010 glUniform1i( SHADER_UNIFORM( shader_ball
, "uTexMain" ), 0 );
1013 if( world
.simulating
)
1015 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1017 struct fish
*fish
= &world
.fishes
[i
];
1019 if( fish
->alive
== 0 )
1022 if( fish
->alive
== -1 && (world
.frame_lerp
> fish
->death_time
) )
1025 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
1026 colour_code_v3( fish
->payload
, dot_colour
);
1028 glUniform3fv( SHADER_UNIFORM( shader_ball
, "uColour" ), 1, dot_colour
);
1029 glUniform2fv( SHADER_UNIFORM( shader_ball
, "uOffset" ), 1, fish
->physics_co
);
1034 SHADER_USE( shader_tile_main
);
1037 vg_tex2d_bind( &tex_tile_data
, 0 );
1038 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
1040 vg_tex2d_bind( &tex_wood
, 1 );
1041 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
1043 render_tiles( NULL
, NULL
, colour_default
, colour_selected
);
1047 for( int y
= 0; y
< world
.h
; y
++ )
1049 for( int x
= 0; x
< world
.w
; x
++ )
1051 struct cell
*cell
= pcell((v2i
){x
,y
});
1053 if( cell
->config
== k_cell_type_split
)
1055 float rotation
= cell
->state
& FLAG_FLIP_FLOP
? vg_rad( -45.0f
): vg_rad( 45.0f
);
1057 if( cell
->state
& FLAG_FLIP_ROTATING
)
1059 if( (world
.frame_lerp
> curve_7_linear_section
) )
1061 float const rotation_speed
= 0.4f
;
1062 if( (world
.frame_lerp
< 1.0f
-rotation_speed
) )
1064 float t
= world
.frame_lerp
- curve_7_linear_section
;
1065 t
*= -2.0f
* (1.0f
/(1.0f
-(curve_7_linear_section
+rotation_speed
)));
1075 m2x2_create_rotation( subtransform
, rotation
);
1077 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1078 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
+ 0.125f
, 0.0f
, 0.0f
);
1085 if( world
.selected
!= -1 && !(world
.data
[ world
.selected
].state
& FLAG_CANAL
) )
1087 v2i new_begin
= { world
.tile_x
- 2, world
.tile_y
- 2 };
1088 v2i new_end
= { world
.tile_x
+ 2, world
.tile_y
+ 2 };
1090 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
1091 map_reclassify( new_begin
, new_end
);
1093 m2x2_identity( subtransform
);
1094 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 1.0f
);
1095 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1096 glUniform2fv( SHADER_UNIFORM( shader_tile_main
, "uMousePos" ), 1, world
.tile_pos
);
1098 render_tiles( new_begin
, new_end
, colour_default
, colour_default
);
1100 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
1101 map_reclassify( new_begin
, new_end
);
1104 //glDisable(GL_BLEND);
1106 glDisable(GL_BLEND
);
1108 SHADER_USE( shader_tile_colour
);
1109 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1110 use_mesh( &world
.circle
);
1113 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1115 struct cell_terminal
*term
= &world
.io
[ i
];
1116 int posx
= term
->id
% world
.w
;
1117 int posy
= (term
->id
- posx
)/world
.w
;
1118 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1120 int const filled_start
= 0;
1121 int const filled_count
= 32;
1122 int const empty_start
= 32;
1123 int const empty_count
= 32*2;
1125 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
1127 for( int j
= 0; j
< arrlen( term
->conditions
); j
++ )
1129 float y_offset
= is_input
? 0.8f
: 0.2f
;
1130 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)posx
+ 0.2f
+ 0.2f
* (float)j
, (float)posy
+ y_offset
, 0.1f
);
1134 colour_code_v3( term
->conditions
[j
], dot_colour
);
1135 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1137 // Draw filled if tick not passed, draw empty if empty
1138 if( world
.sim_frame
> j
)
1139 draw_mesh( empty_start
, empty_count
);
1141 draw_mesh( filled_start
, filled_count
);
1145 if( term
->recv_count
> j
)
1147 colour_code_v3( term
->recv
[j
], dot_colour
);
1148 v3_muls( dot_colour
, 0.8f
, dot_colour
);
1149 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1151 draw_mesh( filled_start
, filled_count
);
1154 colour_code_v3( term
->conditions
[j
], dot_colour
);
1155 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1157 draw_mesh( empty_start
, empty_count
);
1162 if( world
.simulating
)
1164 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.0f
, 0.0f
, 0.0f
, 1.0f
);
1165 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
);
1173 sfx_internal_debug_overlay();