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" );
576 float r1
= (float)vg_window_y
/ (float)vg_window_x
,
577 r2
= (float)world
.h
/ (float)world
.w
,
580 size
= ( r2
< r1
? (float)world
.w
* 0.5f
: ((float)world
.h
* 0.5f
) / r1
) + 2.5f
;
581 m3x3_projection( m_projection
, -size
, size
, -size
*r1
, size
*r1
);
584 origin
[0] = floorf( -0.5f
* world
.w
);
585 origin
[1] = floorf( -0.5f
* world
.h
);
588 m3x3_identity( m_view
);
589 m3x3_translate( m_view
, origin
);
590 m3x3_mul( m_projection
, m_view
, vg_pv
);
591 vg_projection_update();
594 v2_copy( vg_mouse_ws
, world
.tile_pos
);
596 world
.tile_x
= floorf( world
.tile_pos
[0] );
597 world
.tile_y
= floorf( world
.tile_pos
[1] );
600 if( !world
.simulating
)
602 if( cell_interactive( (v2i
){ world
.tile_x
, world
.tile_y
} ))
604 world
.selected
= world
.tile_y
* world
.w
+ world
.tile_x
;
606 if( vg_get_button_down("primary") )
608 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
610 if( world
.data
[ world
.selected
].state
& FLAG_CANAL
)
611 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 3, 6 );
613 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 0, 3 );
615 map_reclassify( (v2i
){ world
.tile_x
-2, world
.tile_y
-2 },
616 (v2i
){ world
.tile_x
+2, world
.tile_y
+2 } );
622 else world
.selected
= -1;
624 // Simulation stop/start
625 if( vg_get_button_down("go") )
627 if( world
.simulating
)
629 world
.simulating
= 0;
630 world
.num_fishes
= 0;
633 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
634 world
.io
[i
].recv_count
= 0;
636 vg_info( "Stopping simulation!\n" );
638 sfx_system_fadeout( &audio_system_balls_rolling
, 44100 );
642 vg_success( "Starting simulation!\n" );
644 sfx_set_playrnd( &audio_rolls
, &audio_system_balls_rolling
, 0, 1 );
646 world
.simulating
= 1;
647 world
.num_fishes
= 0;
649 world
.sim_start
= vg_time
;
651 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
653 world
.data
[ i
].state
&= ~FLAG_FLIP_FLOP
;
656 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
657 world
.io
[i
].recv_count
= 0;
662 if( world
.simulating
)
664 while( world
.sim_frame
< (int)((vg_time
-world
.sim_start
)*2.0f
) )
666 //vg_info( "frame: %u\n", world.sim_frame );
667 sfx_set_playrnd( &audio_random
, &audio_system_balls_switching
, 0, 9 );
669 // Update splitter deltas
670 for( int i
= 0; i
< world
.h
*world
.w
; i
++ )
672 struct cell
*cell
= &world
.data
[i
];
673 if( cell
->config
== k_cell_type_split
)
675 cell
->state
&= ~FLAG_FLIP_ROTATING
;
679 // Update fish positions
680 for( int i
= 0; i
< world
.num_fishes
; i
++ )
682 struct fish
*fish
= &world
.fishes
[i
];
683 struct cell
*cell_current
= pcell( fish
->pos
);
685 if( fish
->alive
== -1 )
688 if( fish
->alive
!= 1 )
692 if( cell_current
->state
& FLAG_OUTPUT
)
694 for( int j
= 0; j
< arrlen( world
.io
); j
++ )
696 struct cell_terminal
*term
= &world
.io
[j
];
698 if( term
->id
== fish
->pos
[1]*world
.w
+ fish
->pos
[0] )
700 term
->recv
[ term
->recv_count
++ ] = fish
->payload
;
709 if( cell_current
->config
== k_cell_type_split
)
712 fish
->dir
[0] = cell_current
->state
&FLAG_FLIP_FLOP
?1:-1;
715 cell_current
->state
^= FLAG_FLIP_FLOP
;
717 else if( cell_current
->config
== k_cell_type_merge
)
725 struct cell
*cell_next
= pcell( (v2i
){ fish
->pos
[0]+fish
->dir
[0], fish
->pos
[1]+fish
->dir
[1] } );
726 if( !(cell_next
->state
& (FLAG_CANAL
|FLAG_OUTPUT
)) )
728 // Try other directions for valid, so down, left, right..
729 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
730 vg_info( "Trying some other directions...\n" );
732 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
734 if( (dirs
[j
][0] == -fish
->dir
[0]) && (dirs
[j
][1] == -fish
->dir
[1]) )
737 if( pcell( (v2i
){ fish
->pos
[0]+dirs
[j
][0], fish
->pos
[1]+dirs
[j
][1] } )->state
& (FLAG_CANAL
|FLAG_OUTPUT
) )
739 fish
->dir
[0] = dirs
[j
][0];
740 fish
->dir
[1] = dirs
[j
][1];
746 fish
->pos
[0] += fish
->dir
[0];
747 fish
->pos
[1] += fish
->dir
[1];
749 struct cell
*cell_entry
= pcell( fish
->pos
);
751 if( !(cell_entry
->state
& (FLAG_INPUT
|FLAG_CANAL
|FLAG_OUTPUT
) ))
757 if( cell_entry
->config
== k_cell_type_split
||
758 cell_entry
->config
== k_cell_type_ramp_right
||
759 cell_entry
->config
== k_cell_type_ramp_left
)
761 // Special death (FALL)
762 v2_sub( fish
->physics_co
, fish
->physics_v
, fish
->physics_v
);
763 v2_divs( fish
->physics_v
, vg_time_delta
, fish
->physics_v
);
766 vg_warn( "Special death (fall)\n" );
771 if( cell_entry
->config
== k_cell_type_split
)
773 sfx_set_playrnd( &audio_splitter
, &audio_system_balls_important
, 0, 1 );
774 cell_entry
->state
|= FLAG_FLIP_ROTATING
;
779 // Check for collisions
780 for( int i
= 0; i
< world
.num_fishes
; i
++ )
782 if( world
.fishes
[i
].alive
== 1 )
784 for( int j
= i
+1; j
< world
.num_fishes
; j
++ )
786 if( (world
.fishes
[j
].alive
== 1) && (world
.fishes
[i
].pos
[0] == world
.fishes
[j
].pos
[0]) &&
787 (world
.fishes
[i
].pos
[1] == world
.fishes
[j
].pos
[1]) )
789 // Shatter death (+0.5s)
790 world
.fishes
[i
].alive
= -1;
791 world
.fishes
[j
].alive
= -1;
792 world
.fishes
[i
].death_time
= 0.5f
;
793 world
.fishes
[j
].death_time
= 0.5f
;
800 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
802 struct cell_terminal
*term
= &world
.io
[ i
];
803 int posx
= term
->id
% world
.w
;
804 int posy
= (term
->id
- posx
)/world
.w
;
805 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
809 if( world
.sim_frame
< arrlen( term
->conditions
) )
811 struct fish
*fish
= &world
.fishes
[world
.num_fishes
++];
815 fish
->payload
= term
->conditions
[world
.sim_frame
];
819 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
820 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
821 if( pcell( (v2i
){ posx
+dirs
[j
][0], posy
+dirs
[j
][1] } )->state
& FLAG_CANAL
)
823 fish
->dir
[0] = dirs
[j
][0];
824 fish
->dir
[1] = dirs
[j
][1];
838 float scaled_time
= 0.0f
;
839 scaled_time
= (vg_time
-world
.sim_start
)*2.0f
;
840 world
.frame_lerp
= scaled_time
- (float)world
.sim_frame
;
843 for( int i
= 0; i
< world
.num_fishes
; i
++ )
845 struct fish
*fish
= &world
.fishes
[i
];
847 if( fish
->alive
== 0 )
850 if( fish
->alive
== -1 && (world
.frame_lerp
> fish
->death_time
) )
851 continue; // Todo: particle thing?
853 if( fish
->alive
== -2 )
855 v2_muladds( fish
->physics_v
, (v2f
){ 0.0, -9.8f
}, vg_time_delta
, fish
->physics_v
);
856 v2_muladds( fish
->physics_co
, fish
->physics_v
, vg_time_delta
, fish
->physics_co
);
860 struct cell
*cell
= pcell(fish
->pos
);
863 float t
= world
.frame_lerp
;
865 v2_copy( fish
->physics_co
, fish
->physics_v
);
867 switch( cell
->config
)
870 if( fish
->dir
[0] == 1 )
875 case 3: curve
= curve_3
; break;
876 case 6: curve
= curve_6
; break;
877 case 9: curve
= curve_9
; break;
878 case 12: curve
= curve_12
; break;
880 if( t
> curve_7_linear_section
)
882 t
-= curve_7_linear_section
;
883 t
*= (1.0f
/(1.0f
-curve_7_linear_section
));
885 curve
= cell
->state
& FLAG_FLIP_FLOP
? curve_7
: curve_7_1
;
889 default: curve
= NULL
; break;
895 float t3
= t
* t
* t
;
897 float cA
= 3.0f
*t2
- 3.0f
*t3
;
898 float cB
= 3.0f
*t3
- 6.0f
*t2
+ 3.0f
*t
;
899 float cC
= 3.0f
*t2
- t3
- 3.0f
*t
+ 1.0f
;
901 fish
->physics_co
[0] = t3
*curve
[3][0] + cA
*curve
[2][0] + cB
*curve
[1][0] + cC
*curve
[0][0];
902 fish
->physics_co
[1] = t3
*curve
[3][1] + cA
*curve
[2][1] + cB
*curve
[1][1] + cC
*curve
[0][1];
903 fish
->physics_co
[0] += (float)fish
->pos
[0];
904 fish
->physics_co
[1] += (float)fish
->pos
[1];
909 origin
[0] = (float)fish
->pos
[0] + (float)fish
->dir
[0]*-0.5f
+ 0.5f
;
910 origin
[1] = (float)fish
->pos
[1] + (float)fish
->dir
[1]*-0.5f
+ 0.5f
;
912 fish
->physics_co
[0] = origin
[0] + (float)fish
->dir
[0]*t
;
913 fish
->physics_co
[1] = origin
[1] + (float)fish
->dir
[1]*t
;
920 static void render_tiles( v2i start
, v2i end
, v4f
const regular_colour
, v4f
const selected_colour
)
922 v2i full_start
= { 0,0 };
923 v2i full_end
= { world
.w
, world
.h
};
931 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
933 for( int y
= start
[1]; y
< end
[1]; y
++ )
935 for( int x
= start
[0]; x
< end
[0]; x
++ )
937 struct cell
*cell
= pcell((v2i
){x
,y
});
938 int selected
= world
.selected
== y
*world
.w
+ x
;
940 int tile_offsets
[][2] =
942 {2, 0}, {0, 3}, {0, 2}, {2, 2},
943 {1, 0}, {2, 3}, {3, 2}, {1, 3},
944 {3, 1}, {0, 1}, {1, 2}, {2, 1},
945 {1, 1}, {3, 3}, {2, 1}, {2, 1}
948 int uv
[2] = { 3, 0 };
950 if( cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
952 uv
[0] = tile_offsets
[ cell
->config
][0];
953 uv
[1] = tile_offsets
[ cell
->config
][1];
956 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
, uv
[0], uv
[1] );
959 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, selected_colour
);
961 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
971 glViewport( 0,0, vg_window_x
, vg_window_y
);
973 glDisable( GL_DEPTH_TEST
);
974 glClearColor( 0.369768f
, 0.3654f
, 0.42f
, 1.0f
);
975 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
977 v4f
const colour_default
= {1.0f
, 1.0f
, 1.0f
, 1.0f
};
978 v4f
const colour_selected
= {0.90f
, 0.92f
, 1.0f
, 1.0f
};
980 // TILE SET RENDERING
981 // todo: just slam everything into a mesh...
982 // when user modifies a tile the neighbours can be easily uploaded to gpu mem
984 // Currently we're uploading a fair amount of data every frame anyway.
985 // NOTE: this is for final optimisations ONLY!
986 // ======================================================================
988 use_mesh( &world
.tile
);
994 SHADER_USE( shader_background
);
995 glUniformMatrix3fv( SHADER_UNIFORM( shader_background
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
997 vg_tex2d_bind( &tex_background
, 0 );
998 glUniform1i( SHADER_UNIFORM( shader_background
, "uTexMain" ), 0 );
1000 glUniform3f( SHADER_UNIFORM( shader_background
, "uOffset" ), -50, -50, 100 );
1007 SHADER_USE( shader_tile_main
);
1010 m2x2_identity( subtransform
);
1011 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1012 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_main
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1013 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 0.0f
);
1014 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uForeground" ), 0.0f
);
1017 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1018 glBlendEquation(GL_FUNC_ADD
);
1021 vg_tex2d_bind( &tex_tile_data
, 0 );
1022 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
1024 vg_tex2d_bind( &tex_wood
, 1 );
1025 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
1027 render_tiles( NULL
, NULL
, colour_default
, colour_default
);
1031 SHADER_USE( shader_ball
);
1032 glUniformMatrix3fv( SHADER_UNIFORM( shader_ball
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1034 vg_tex2d_bind( &tex_ball
, 0 );
1035 glUniform1i( SHADER_UNIFORM( shader_ball
, "uTexMain" ), 0 );
1038 if( world
.simulating
)
1040 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1042 struct fish
*fish
= &world
.fishes
[i
];
1044 if( fish
->alive
== 0 )
1047 if( fish
->alive
== -1 && (world
.frame_lerp
> fish
->death_time
) )
1050 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
1051 colour_code_v3( fish
->payload
, dot_colour
);
1053 glUniform3fv( SHADER_UNIFORM( shader_ball
, "uColour" ), 1, dot_colour
);
1054 glUniform2fv( SHADER_UNIFORM( shader_ball
, "uOffset" ), 1, fish
->physics_co
);
1059 SHADER_USE( shader_tile_main
);
1062 vg_tex2d_bind( &tex_tile_data
, 0 );
1063 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
1065 vg_tex2d_bind( &tex_wood
, 1 );
1066 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
1068 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uForeground" ), 1.0f
);
1069 render_tiles( NULL
, NULL
, colour_default
, colour_selected
);
1073 for( int y
= 0; y
< world
.h
; y
++ )
1075 for( int x
= 0; x
< world
.w
; x
++ )
1077 struct cell
*cell
= pcell((v2i
){x
,y
});
1079 if( cell
->config
== k_cell_type_split
)
1081 float rotation
= cell
->state
& FLAG_FLIP_FLOP
? vg_rad( -45.0f
): vg_rad( 45.0f
);
1083 if( cell
->state
& FLAG_FLIP_ROTATING
)
1085 if( (world
.frame_lerp
> curve_7_linear_section
) )
1087 float const rotation_speed
= 0.4f
;
1088 if( (world
.frame_lerp
< 1.0f
-rotation_speed
) )
1090 float t
= world
.frame_lerp
- curve_7_linear_section
;
1091 t
*= -2.0f
* (1.0f
/(1.0f
-(curve_7_linear_section
+rotation_speed
)));
1101 m2x2_create_rotation( subtransform
, rotation
);
1103 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1104 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
+ 0.125f
, 0.0f
, 0.0f
);
1111 if( world
.selected
!= -1 && !(world
.data
[ world
.selected
].state
& FLAG_CANAL
) )
1113 v2i new_begin
= { world
.tile_x
- 2, world
.tile_y
- 2 };
1114 v2i new_end
= { world
.tile_x
+ 2, world
.tile_y
+ 2 };
1116 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
1117 map_reclassify( new_begin
, new_end
);
1119 m2x2_identity( subtransform
);
1120 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 1.0f
);
1121 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1122 glUniform2fv( SHADER_UNIFORM( shader_tile_main
, "uMousePos" ), 1, world
.tile_pos
);
1124 render_tiles( new_begin
, new_end
, colour_default
, colour_default
);
1126 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
1127 map_reclassify( new_begin
, new_end
);
1130 //glDisable(GL_BLEND);
1132 glDisable(GL_BLEND
);
1134 SHADER_USE( shader_tile_colour
);
1135 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1136 use_mesh( &world
.circle
);
1139 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1141 struct cell_terminal
*term
= &world
.io
[ i
];
1142 int posx
= term
->id
% world
.w
;
1143 int posy
= (term
->id
- posx
)/world
.w
;
1144 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1146 int const filled_start
= 0;
1147 int const filled_count
= 32;
1148 int const empty_start
= 32;
1149 int const empty_count
= 32*2;
1151 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
1153 for( int j
= 0; j
< arrlen( term
->conditions
); j
++ )
1155 float y_offset
= is_input
? 0.8f
: 0.2f
;
1156 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)posx
+ 0.2f
+ 0.2f
* (float)j
, (float)posy
+ y_offset
, 0.1f
);
1160 colour_code_v3( term
->conditions
[j
], dot_colour
);
1161 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1163 // Draw filled if tick not passed, draw empty if empty
1164 if( world
.sim_frame
> j
)
1165 draw_mesh( empty_start
, empty_count
);
1167 draw_mesh( filled_start
, filled_count
);
1171 if( term
->recv_count
> j
)
1173 colour_code_v3( term
->recv
[j
], dot_colour
);
1174 v3_muls( dot_colour
, 0.8f
, dot_colour
);
1175 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1177 draw_mesh( filled_start
, filled_count
);
1180 colour_code_v3( term
->conditions
[j
], dot_colour
);
1181 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1183 draw_mesh( empty_start
, empty_count
);
1188 if( world
.simulating
)
1190 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.0f
, 0.0f
, 0.0f
, 1.0f
);
1191 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
);
1199 sfx_internal_debug_overlay();