1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
3 //#define VG_CAPTURE_MODE
5 #define VG_STEAM_APPID 1218140U
10 k_world_button_mode_once
,
11 k_world_button_mode_toggle
18 float light_target
, light
, extra_light
;
21 enum world_button_mode mode
;
24 enum world_button_status
26 k_world_button_on_enable
,
27 k_world_button_on_disable
30 #include "fishladder_resources.h"
32 // #define STEAM_LEADERBOARDS
35 // ===========================================================================================================
40 k_cell_type_ramp_right
= 3,
41 k_cell_type_ramp_left
= 6,
42 k_cell_type_split
= 7,
43 k_cell_type_merge
= 13,
44 k_cell_type_con_r
= 1,
45 k_cell_type_con_u
= 2,
46 k_cell_type_con_l
= 4,
52 k_fish_state_soon_dead
= -1,
53 k_fish_state_dead
= 0,
56 k_fish_state_soon_alive
61 k_world_button_none
= -1,
62 k_world_button_sim
= 0,
63 k_world_button_pause
= 1,
64 k_world_button_speedy
= 2,
65 k_world_button_settings
= 3
74 #define FLAG_CANAL 0x1
75 #define FLAG_IS_TRIGGER 0x2
76 #define FLAG_RESERVED0 0x4
77 #define FLAG_RESERVED1 0x8
79 #define FLAG_INPUT 0x10
80 #define FLAG_OUTPUT 0x20
81 #define FLAG_WALL 0x40
82 #define FLAG_EMITTER 0x80
84 #define FLAG_FLIP_FLOP 0x100
85 #define FLAG_TRIGGERED 0x200
86 #define FLAG_FLIP_ROTATING 0x400
87 #define FLAG_TARGETED 0x800
89 #define FLAG_INPUT_NICE 0x1000
92 0000 0 | 0001 1 | 0010 2 | 0011 3
96 0100 4 | 0101 5 | 0110 6 | 0111 7
100 1000 8 | 1001 9 | 1010 10 | 1011 11
104 1100 12 | 1101 13 | 1110 14 | 1111 15
110 static struct cell_description
119 enum sprites_auto_combine_index trigger_sprite
;
121 cell_descriptions
[] =
125 { .start
= { 1, 0 }, .end
= { -1, 0 }, .trigger_pos
= { 0.5f
, 0.25f
}, .trigger_sprite
= k_sprite_brk_d
},
126 { .start
= { 0, 1 }, .end
= { 0, -1 }, .trigger_pos
= { 0.25f
, 0.5f
}, .trigger_sprite
= k_sprite_brk_l
},
127 { .start
= { 0, 1 }, .end
= { 1, 0 }, .trigger_pos
= { 0.25f
, 0.5f
}, .trigger_sprite
= k_sprite_brk_l
},
129 { .start
= { -1, 0 }, .end
= { 1, 0 }, .trigger_pos
= { 0.5f
, 0.25f
}, .trigger_sprite
= k_sprite_brk_d
},
130 { .start
= { -1, 0 }, .end
= { 1, 0 }, .trigger_pos
= { 0.5f
, 0.25f
}, .trigger_sprite
= k_sprite_brk_d
, .is_linear
= 1 },
131 { .start
= { 0, 1 }, .end
= { -1, 0 }, .trigger_pos
= { 0.5f
, 0.25f
}, .trigger_sprite
= k_sprite_brk_d
},
132 { .start
= { 0, 1 }, .is_special
= 1 },
134 { .start
= { 0, -1 }, .end
= { 0, 1 }, .trigger_pos
= { 0.25f
, 0.5f
}, .trigger_sprite
= k_sprite_brk_l
},
135 { .start
= { 1, 0 }, .end
= { 0, -1 }, .trigger_pos
= { 0.25f
, 0.5f
}, .trigger_sprite
= k_sprite_brk_l
},
136 { .start
= { 0, 1 }, .end
= { 0, -1 }, .trigger_pos
= { 0.25f
, 0.5f
}, .trigger_sprite
= k_sprite_brk_l
, .is_linear
= 1 },
139 { .start
= { -1, 0 }, .end
= { 0, -1 }, .trigger_pos
= { 0.5f
, 0.75f
}, .trigger_sprite
= k_sprite_brk_u
},
140 { .end
= { 0, -1 }, .is_special
= 1, .trigger_pos
= { 0.5f
, 0.75f
}, .trigger_sprite
= k_sprite_brk_u
},
145 v2f
const curve_3
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
146 v2f
const curve_6
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.375f
,0.5f
},{0.0f
,0.5f
}};
147 v2f
const curve_9
[] = {{1.0f
,0.5f
},{0.625f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
148 v2f
const curve_12
[]= {{0.0f
,0.5f
},{0.375f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
150 v2f
const curve_1
[] = {{1.0f
,0.5f
},{0.8f
,0.5f
},{0.3f
,0.5f
},{0.2f
,0.5f
}};
151 v2f
const curve_4
[] = {{0.0f
,0.5f
},{0.3f
,0.5f
},{0.5f
,0.5f
},{0.8f
,0.5f
}};
152 v2f
const curve_2
[] = {{0.5f
,1.0f
},{0.5f
,0.8f
},{0.5f
,0.3f
},{0.5f
,0.2f
}};
153 v2f
const curve_8
[] = {{0.5f
,0.0f
},{0.5f
,0.3f
},{0.5f
,0.5f
},{0.5f
,0.8f
}};
155 v2f
const curve_7
[] = {{0.5f
,0.8438f
},{0.875f
,0.8438f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
156 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
}};
158 float const curve_7_linear_section
= 0.1562f
;
161 // ===========================================================================================================
171 // Things that are 'static', aka, initialized once
174 struct world_button buttons
[4];
176 enum e_game_state state
;
178 struct cmp_level
*lvl_to_load
;
181 float world_transition
;
200 *cmd_buf_tiles
, *cmd_buf_specials
;
202 u32 tile_count
, tile_special_count
, max_commands
;
205 int sim_run
, max_runs
;
207 int sim_frame
, sim_target
;
208 float sim_internal_time
, // current tick-time
209 sim_internal_delta
, // time delta
210 sim_internal_ref
, // Reference point of internal time
211 sim_delta_ref
, // Reference point of vg_time when we started at current sim_speed
212 sim_delta_speed
, // Rate to apply time delta
213 frame_lerp
, // Fractional part of sim_internal_time
214 pause_offset_target
; //
223 int step_count
, recv_count
;
238 GLuint vao
, vbo
, ebo
;
243 GLuint background_data
;
244 GLuint random_samples
;
246 int selected
, tile_x
, tile_y
;
253 enum e_fish_state state
;
265 struct cmp_level
*pCmpLevel
;
279 .buttons
= { { .mode
= k_world_button_mode_toggle
},
280 { .mode
= k_world_button_mode_toggle
},
281 { .mode
= k_world_button_mode_toggle
},
282 { .mode
= k_world_button_mode_toggle
} }
286 // Forward declerations
287 // --------------------
292 static void colour_code_v3( i8 cc
, v3f target
);
293 static int hash21i( v2i p
, u32 umod
);
297 static void init_mesh( struct mesh
*m
, float const *tris
, u32 length
);
298 static void free_mesh( struct mesh
*m
);
299 static void use_mesh( struct mesh
*m
);
300 static void draw_mesh( int const start
, int const count
);
304 static void level_selection_buttons(void);
306 // Map/world interface
307 // -------------------
308 static void map_free(void);
309 static void io_reset(void);
310 static struct cell
*pcell( v2i pos
);
311 static void lcell( int id
, v2i pos
);
312 static void map_reclassify( v2i start
, v2i end
, int update_texbuffer
);
313 static void gen_level_text( struct cmp_level
*pLevel
);
314 static int map_load( const char *str
, const char *name
);
315 static void map_serialize( FILE *stream
);
319 static void career_serialize(void);
320 static void career_unlock_level( struct cmp_level
*lvl
);
321 static void career_unlock_level( struct cmp_level
*lvl
);
322 static void career_pass_level( struct cmp_level
*lvl
, int score
, int upload
);
323 static void career_reset_level( struct cmp_level
*lvl
);
324 static void career_load(void);
325 static void clear_animation_flags(void);
329 static void simulation_stop(void);
330 static void simulation_start(void);
331 static int world_check_pos_ok( v2i co
, int dist
);
332 static int cell_interactive( v2i co
);
334 void vg_update(void);
335 static void render_tiles( v4f
const regular_colour
, v4f
const selected_colour
);
336 static void render_tile_block( v2i start
, v2i end
, v4f
const regular_colour
, v4f
const selected_colour
);
338 void vg_render(void);
343 #ifdef STEAM_LEADERBOARDS
344 void leaderboard_set_score( struct cmp_level
*cmp_level
, u32 score
);
345 void leaderboard_dispatch_score(void);
346 void leaderboard_found( LeaderboardFindResult_t
*pCallback
);
347 void leaderboard_downloaded( LeaderboardScoresDownloaded_t
*pCallback
);
348 void leaderboard_set_score( struct cmp_level
*cmp_level
, u32 score
);
353 static int console_credits( int argc
, char const *argv
[] );
354 static int console_save_map( int argc
, char const *argv
[] );
355 static int console_load_map( int argc
, char const *argv
[] );
356 static int console_changelevel( int argc
, char const *argv
[] );
361 int main( int argc
, char *argv
[] );
364 // ===========================================================================================================
371 // ===========================================================================================================
373 static int colour_set_id
= 0;
374 static int world_theme_id
= 0;
376 static v3f colour_sets
[][4] =
378 { { 1.0f
, 0.9f
, 0.3f
}, // Yellow
379 { 0.4f
, 0.8f
, 1.00f
}, // Blue
380 { 0.2f
, 0.9f
, 0.14f
}, // Green
381 { 0.882f
, 0.204f
, 0.922f
} // Pink
383 { { 1.0f
, 0.9f
, 0.3f
}, // Yellow
384 { 0.4f
, 0.8f
, 1.00f
}, // Blue
385 { 0.85f
, 0.85f
, 0.85f
}, // Weiss
386 { 0.2f
, 0.2f
, 0.2f
} // Black/Gray
388 { { 1.0f
, 0.9f
, 0.3f
}, // Yellow
389 { 0.827f
, 0.373f
, 0.718f
}, // Pink
390 { 0.0f
, 0.353f
, 0.71f
}, // Blue
391 { 0.863f
, 0.196f
, 0.125f
} // Red
395 static struct world_theme
406 { 0.89f
, 0.8f
, 0.7f
},
411 { 0.8f
, 0.8f
, 0.8f
},
416 { 0.7f
, 0.7f
, 0.7f
},
421 static void colour_code_v3( i8 cc
, v3f target
)
423 if( (cc
>= 0) && (cc
< vg_list_size( colour_sets
[0] )) )
425 v3_copy( colour_sets
[colour_set_id
][ cc
], target
);
429 vg_error( "Invalid colour code used '%d'\n", (int)cc
);
430 v3_copy( (v3f
){0.0f
,0.0f
,0.0f
}, target
);
433 static int hash21i( v2i p
, u32 umod
)
435 static const int random_noise
[] =
437 0x46,0xD5,0xB8,0xD3,0xF2,0xE5,0xCC,0x07,0xD0,0xB3,0x7A,0xA2,0xC3,0xDA,0xDC,0x7F,0xE0,0xB7,0x42,0xA0,0xBF,0x41,0x92,0x32,0x6F,0x0D,0x45,0xC7,0x54,0xDB,0x30,0xC2,
438 0xD5,0xDA,0x55,0x09,0xDE,0x74,0x48,0x20,0xE1,0x24,0x5C,0x4D,0x6F,0x36,0xD8,0xE9,0x8D,0x8F,0x54,0x99,0x98,0x51,0xFE,0xDB,0x26,0x04,0x65,0x57,0x56,0xF3,0x53,0x30,
439 0x3D,0x16,0xC0,0xB6,0xF2,0x47,0xCF,0x62,0xB0,0x6C,0x8F,0x4F,0x8C,0x4C,0x17,0xF0,0x19,0x7E,0x2D,0x81,0x8D,0xFB,0x10,0xD3,0x49,0x50,0x60,0xFD,0x38,0x15,0x3B,0xEE,
440 0x05,0xC1,0xCF,0x62,0x97,0x75,0xDF,0x4E,0x4D,0x89,0x5E,0x88,0x5C,0x30,0x8C,0x54,0x1D,0x39,0x41,0xEA,0xA2,0x63,0x12,0x1B,0x8E,0x35,0x22,0x9B,0x98,0xA3,0x7F,0x80,
441 0xD6,0x27,0x94,0x66,0xB5,0x1D,0x7E,0xDF,0x96,0x28,0x38,0x3A,0xA0,0xE8,0x71,0x09,0x62,0x5E,0x9D,0x53,0x58,0x1B,0x7D,0x0D,0x2D,0x99,0x77,0x83,0xC3,0x89,0xC2,0xA2,
442 0xA7,0x1D,0x78,0x80,0x37,0xC1,0x87,0xFF,0x65,0xBF,0x2C,0xF1,0xE5,0xB3,0x09,0xE0,0x25,0x92,0x83,0x0F,0x8A,0x57,0x3C,0x0B,0xC6,0xBC,0x44,0x16,0xE3,0xCE,0xC3,0x0D,
443 0x69,0xD3,0xC6,0x99,0xB8,0x46,0x44,0xC4,0xF3,0x1E,0xBF,0xF5,0xB4,0xDB,0xFB,0x93,0xA1,0x7B,0xC9,0x08,0x77,0x22,0xE5,0x02,0xEF,0x9E,0x90,0x94,0x8A,0xA6,0x3D,0x7E,
444 0xA2,0xA0,0x10,0x82,0x47,0x5C,0xAA,0xF8,0x2F,0x0D,0x9F,0x76,0xDA,0x99,0x0F,0xCB,0xE2,0x02,0x0C,0x75,0xCA,0x35,0x29,0xA6,0x49,0x83,0x6D,0x91,0xB4,0xEC,0x31,0x69,
445 0xBA,0x13,0xF3,0xC7,0x21,0x06,0xC8,0x79,0xEF,0xB1,0x9C,0x6A,0xEE,0x64,0x9A,0xDC,0x1E,0xC6,0x18,0x93,0xA9,0x7E,0x89,0x7D,0x96,0xE5,0x44,0xB8,0x00,0x15,0xAF,0x8C,
446 0x78,0x8F,0xA8,0x05,0xA7,0x07,0x25,0x9A,0xC8,0x5D,0x90,0x1A,0x41,0x53,0x30,0xD3,0x24,0x33,0x71,0xB4,0x50,0x6E,0xE4,0xEA,0x0D,0x2B,0x6D,0xF5,0x17,0x08,0x74,0x49,
447 0x71,0xC2,0xAC,0xF7,0xDC,0xB2,0x7E,0xCC,0xB6,0x1B,0xB8,0xA9,0x52,0xCF,0x6B,0x51,0xD2,0x4E,0xC9,0x43,0xEE,0x2E,0x92,0x24,0xBB,0x47,0x4D,0x0C,0x3E,0x21,0x53,0x19,
448 0xD4,0x82,0xE2,0xC6,0x93,0x85,0x0A,0xF8,0xFA,0x04,0x07,0xD3,0x1D,0xEC,0x03,0x66,0xFD,0xB1,0xFB,0x8F,0xC5,0xDE,0xE8,0x29,0xDF,0x23,0x09,0x9D,0x7C,0x43,0x3D,0x4D,
449 0x89,0xB9,0x6F,0xB4,0x6B,0x4A,0x51,0xC3,0x94,0xF4,0x7C,0x5E,0x19,0x87,0x79,0xC1,0x80,0x0C,0x45,0x12,0xEC,0x95,0xF3,0x31,0x68,0x42,0xE1,0x06,0x57,0x0E,0xA7,0xFB,
450 0x78,0x96,0x87,0x23,0xA5,0x20,0x7A,0x09,0x3A,0x45,0xE6,0xD9,0x5E,0x6A,0xD6,0xAA,0x29,0x50,0x92,0x4E,0xD0,0xB5,0x91,0xC2,0x9A,0xCF,0x07,0xFE,0xB2,0x15,0xEB,0xE4,
451 0x84,0x40,0x14,0x47,0xFA,0x93,0xB9,0x06,0x69,0xDB,0xBD,0x4E,0xEA,0x52,0x9B,0xDE,0x5B,0x50,0x36,0xAB,0xB3,0x1F,0xD2,0xCD,0x9C,0x13,0x07,0x7E,0x8B,0xED,0x72,0x62,
452 0x74,0x77,0x3B,0x88,0xAC,0x5B,0x6A,0xBC,0xDA,0x99,0xE8,0x24,0x90,0x5A,0xCA,0x8D,0x5C,0x2B,0xF8,0xF1,0xE1,0x1D,0x94,0x11,0xEA,0xCC,0x02,0x09,0x1E,0xA2,0x48,0x67,
453 0x87,0x5A,0x7E,0xC6,0xCC,0xA3,0xFB,0xC5,0x36,0xEB,0x5C,0xE1,0xAF,0x1E,0xBE,0xE7,0xD8,0x8F,0x70,0xAE,0x42,0x05,0xF5,0xCD,0x2D,0xA2,0xB0,0xFD,0xEF,0x65,0x2C,0x22,
454 0xCB,0x8C,0x8B,0xAA,0x3D,0x86,0xE2,0xCD,0xBE,0xC3,0x42,0x38,0xE3,0x9C,0x08,0xB5,0xAE,0xBD,0x54,0x73,0x83,0x70,0x24,0x47,0xCA,0x4C,0x04,0xC4,0xE0,0x1D,0x40,0xED,
455 0xF4,0x2B,0x50,0x8E,0x97,0xB3,0xF0,0xA6,0x76,0xDB,0x49,0x30,0xE5,0xD9,0x71,0x07,0xB2,0xF1,0x0F,0xD6,0x77,0xAA,0x72,0xC0,0xAF,0x66,0xD8,0x40,0xC6,0x08,0x19,0x8C,
456 0xD9,0x8F,0x5A,0x75,0xAC,0xBE,0xC2,0x40,0x5B,0xBD,0x0D,0x1D,0x00,0xAF,0x26,0x5E,0x78,0x43,0xAA,0xC6,0x4F,0xF3,0xD8,0xE2,0x7F,0x0C,0x1E,0x77,0x4D,0x35,0x96,0x23,
457 0x32,0x44,0x03,0x8D,0x92,0xE7,0xFD,0x48,0x07,0xD0,0x58,0xFC,0x6D,0xC9,0x91,0x33,0xF0,0x23,0x45,0xA4,0x29,0xB9,0xF5,0xB0,0x68,0x8F,0x7B,0x59,0x15,0x8E,0xA6,0x66,
458 0x15,0xA0,0x76,0x9B,0x69,0xCB,0x38,0xA5,0xF4,0xB4,0x6B,0xDC,0x1F,0xAB,0xAE,0x12,0x77,0xC0,0x8C,0x4A,0x03,0xB9,0x73,0xD3,0x6D,0x52,0xC5,0xF5,0x6E,0x4E,0x4B,0xA3,
459 0x24,0x02,0x58,0xEE,0x5F,0xF9,0xD6,0xD0,0x1D,0xBC,0xF4,0xB8,0x4F,0xFD,0x4B,0x2D,0x34,0x77,0x46,0xE5,0xD4,0x33,0x7B,0x9C,0x35,0xCD,0xB0,0x5D,0x06,0x39,0x99,0xEB,
460 0x0C,0xD0,0x0F,0xF7,0x92,0xB5,0x58,0x5B,0x5E,0x79,0x12,0xF4,0x05,0xF6,0x21,0x07,0x0B,0x49,0x1A,0xFB,0xD4,0x98,0xC4,0xEF,0x7A,0xD6,0xCA,0xA1,0xDA,0xB3,0x51,0x00,
461 0x76,0xEC,0x08,0x48,0x40,0x35,0xD7,0x94,0xBE,0xF5,0x7B,0xA4,0x20,0x81,0x5F,0x82,0xF3,0x6F,0x96,0x24,0x98,0xB6,0x49,0x18,0xC8,0xC5,0x8C,0xD2,0x38,0x7F,0xC4,0xF6,
462 0xAA,0x87,0xDC,0x73,0x5B,0xA1,0xAF,0xE5,0x3D,0x37,0x6B,0x85,0xED,0x38,0x62,0x7D,0x57,0xBD,0xCF,0xB5,0x1B,0xA8,0xBB,0x32,0x33,0xD3,0x34,0x5A,0xC1,0x5D,0xFB,0x28,
463 0x6E,0xE1,0x67,0x51,0xBB,0x31,0x92,0x83,0xAC,0xAA,0x72,0x52,0xFD,0x13,0x4F,0x73,0xD3,0xF0,0x5E,0xFC,0xBA,0xB1,0x3C,0x7B,0x08,0x76,0x03,0x38,0x1E,0xD1,0xCC,0x33,
464 0xA3,0x1E,0xFC,0xE0,0x82,0x30,0x27,0x93,0x71,0x35,0x75,0x77,0xBA,0x78,0x10,0x33,0xCD,0xAB,0xCF,0x8E,0xAD,0xF9,0x32,0xC9,0x15,0x9F,0xD6,0x6D,0xA8,0xAE,0xB1,0x3F,
465 0x90,0xEB,0xD4,0xF9,0x31,0x81,0xA3,0x53,0x99,0x4B,0x3C,0x93,0x3B,0xFE,0x55,0xFF,0x25,0x9F,0xCC,0x07,0xC5,0x2C,0x14,0xA7,0xA4,0x1E,0x6C,0xB6,0x91,0x2A,0xE0,0x3E,
466 0x7F,0x39,0x0A,0xD9,0x24,0x3C,0x01,0xA0,0x30,0x99,0x8E,0xB8,0x1D,0xF9,0xA7,0x78,0x86,0x95,0x35,0x0E,0x21,0xDA,0x7A,0x7B,0xAD,0x9F,0x4E,0xF6,0x63,0x5B,0x96,0xBB,
467 0x87,0x36,0x3F,0xA7,0x1A,0x66,0x91,0xCD,0xB0,0x3B,0xC0,0x4F,0x54,0xD2,0x5F,0xBB,0x38,0x89,0x1C,0x79,0x7E,0xA2,0x02,0xE4,0x80,0x84,0x1E,0x33,0xAB,0x74,0xFA,0xBE,
468 0x31,0x46,0x2E,0xC5,0x15,0xB9,0x12,0xE9,0xD3,0x73,0x43,0xEA,0x74,0x11,0xA7,0xC0,0xD5,0xD8,0x39,0x08,0x9F,0x4F,0xC7,0x71,0x25,0x09,0x51,0x65,0xD6,0xA8,0x02,0x1F
471 return random_noise
[ (random_noise
[p
[1] & 1023] + p
[0]) & 1023 ] & umod
;
475 // ===========================================================================================================
477 static void init_mesh( struct mesh
*m
, float const *tris
, u32 length
)
479 m
->elements
= length
/3;
480 glGenVertexArrays( 1, &m
->vao
);
481 glGenBuffers( 1, &m
->vbo
);
483 glBindVertexArray( m
->vao
);
484 glBindBuffer( GL_ARRAY_BUFFER
, m
->vbo
);
485 glBufferData( GL_ARRAY_BUFFER
, length
*sizeof(float), tris
, GL_STATIC_DRAW
);
487 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2 * sizeof(float), (void*)0 );
488 glEnableVertexAttribArray( 0 );
493 static void free_mesh( struct mesh
*m
)
495 glDeleteVertexArrays( 1, &m
->vao
);
496 glDeleteBuffers( 1, &m
->vbo
);
499 static void draw_mesh( int const start
, int const count
)
501 glDrawArrays( GL_TRIANGLES
, start
*3, count
*3 );
504 static void use_mesh( struct mesh
*m
)
506 glBindVertexArray( m
->vao
);
509 void leaderboard_set_score( struct cmp_level
*cmp_level
, u32 score
);
512 // ===========================================================================================================
514 static void map_free(void)
516 arrfree( world
.data
);
519 free( world
.cmd_buf_tiles
);
520 world
.cmd_buf_tiles
= NULL
;
530 world
.initialzed
= 0;
533 static void io_reset(void)
535 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
537 struct cell_terminal
*term
= &world
.io
[i
];
539 for( int j
= 0; j
< term
->run_count
; j
++ )
540 term
->runs
[j
].recv_count
= 0;
544 static struct cell
*pcell( v2i pos
)
546 return &world
.data
[ pos
[1]*world
.w
+ pos
[0] ];
549 static void lcell( int id
, v2i pos
)
551 pos
[0] = id
% world
.w
;
552 pos
[1] = (id
- pos
[0]) / world
.w
;
555 static void map_reclassify( v2i start
, v2i end
, int update_texbuffer
)
557 v2i full_start
= { 1,1 };
558 v2i full_end
= { world
.w
-1, world
.h
-1 };
567 u8 info_buffer
[64*64*4];
570 int px0
= vg_max( start
[0], full_start
[0] ),
571 px1
= vg_min( end
[0], full_end
[0] ),
572 py0
= vg_max( start
[1], full_start
[1] ),
573 py1
= vg_min( end
[1], full_end
[1] );
575 for( int y
= py0
; y
< py1
; y
++ )
577 for( int x
= px0
; x
< px1
; x
++ )
579 struct cell
*cell
= pcell((v2i
){x
,y
});
581 v2i dirs
[] = {{1,0},{0,1},{-1,0},{0,-1}};
586 if( cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
|FLAG_EMITTER
) )
588 for( int i
= 0; i
< vg_list_size( dirs
); i
++ )
590 struct cell
*neighbour
= pcell((v2i
){x
+dirs
[i
][0], y
+dirs
[i
][1]});
591 if( neighbour
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
|FLAG_EMITTER
) )
599 if( cell
->state
& FLAG_WALL
)
600 height
= 0xFF-0x3F + hash21i( (v2i
){x
,y
}, 0x3F );
602 config
= cell
->state
& FLAG_INPUT_NICE
? 0xB: 0xF;
605 pcell((v2i
){x
,y
})->config
= config
;
607 u8
*info_px
= &info_buffer
[ (pixel_id
++)*4 ];
609 info_px
[1] = cell
->state
& FLAG_WALL
? 0: 255;
615 ((cell
->state
& FLAG_IS_TRIGGER
) && (cell
->config
== 0xF || cell
->config
== k_cell_type_split
)) ||
616 ((cell
->state
& FLAG_TARGETED
) && ((cell
->config
!= k_cell_type_split
) && !(cell
->state
& FLAG_EMITTER
)))
617 ) && update_texbuffer
619 cell
->state
&= ~(FLAG_TARGETED
|FLAG_IS_TRIGGER
);
620 for( u32 i
= 0; i
< 2; i
++ )
624 struct cell
*other_ptr
= &world
.data
[ cell
->links
[i
] ];
625 other_ptr
->links
[ i
] = 0;
626 other_ptr
->state
&= ~FLAG_IS_TRIGGER
;
628 if( other_ptr
->links
[ i
^ 0x1 ] == 0 )
629 other_ptr
->state
&= ~FLAG_TARGETED
;
639 if( update_texbuffer
)
641 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
642 glTexSubImage2D( GL_TEXTURE_2D
, 0, px0
+ 16, py0
+ 16, px1
-px0
, py1
-py0
, GL_RGBA
, GL_UNSIGNED_BYTE
, info_buffer
);
646 static void gen_level_text( struct cmp_level
*pLevel
)
649 ui_px
const unit_scale_px
= 4*UI_GLYPH_SPACING_X
; // 4 char per unit
650 ui_begin( &world
.st
.world_text
, world
.w
*unit_scale_px
, world
.h
*unit_scale_px
);
652 for( int i
= 0; i
< vg_list_size( pLevel
->strings
); i
++ )
654 struct world_string
*wstr
= &pLevel
->strings
[i
];
660 pos
[0] = -UI_GLYPH_SPACING_X
/2;
662 if( wstr
->placement
== k_placement_bottom
)
663 pos
[1] = 2*-unit_scale_px
;
665 pos
[1] = (world
.h
-1)*-unit_scale_px
-6;
667 ui_text( &world
.st
.world_text
, pos
, wstr
->str
, 1, k_text_align_left
);
671 ui_resolve( &world
.st
.world_text
);
674 static int map_load( const char *str
, const char *name
)
683 if( c
[world
.w
] == ';' )
685 else if( !c
[world
.w
] )
687 vg_error( "Unexpected EOF when parsing level\n" );
692 struct cell
*row
= arraddnptr( world
.data
, world
.w
);
694 int reg_start
= 0, reg_end
= 0;
696 u32
*links_to_make
= NULL
;
697 int links_satisfied
= 0;
699 char link_id_buffer
[32];
707 if( *c
== '\r' ) { c
++; continue; } // fuck off windows
713 if( *c
== '\r' ) c
++;
720 if( *c
== '\r' ) { c
++; continue; }
722 if( reg_start
< reg_end
)
724 struct cell_terminal
*terminal
= &world
.io
[ reg_start
];
725 struct terminal_run
*run
= &terminal
->runs
[ terminal
->run_count
-1 ];
727 if( (*c
>= 'a' && *c
<= 'z') || *c
== ' ' )
733 run
->steps
[ run
->step_count
++ ] = code
;
737 if( *c
== ',' || *c
== '\n' )
746 terminal
->runs
[ terminal
->run_count
].step_count
= 0;
747 terminal
->run_count
++;
748 world
.max_runs
= vg_max( world
.max_runs
, terminal
->run_count
);
752 vg_error( "Unkown attribute '%c' (row: %u)\n", *c
, world
.h
);
759 if( links_satisfied
< arrlen( links_to_make
) )
761 struct cell
*target
= &world
.data
[ links_to_make
[ links_satisfied
] ];
763 if( (((u32
)*c
>= (u32
)'0') && ((u32
)*c
<= (u32
)'9')) || *c
== '-' )
765 if( link_id_n
>= vg_list_size( link_id_buffer
)-1 )
767 vg_error( "Number was way too long to be parsed (row: %u)\n", world
.h
);
771 link_id_buffer
[ link_id_n
++ ] = *c
;
773 else if( *c
== ',' || *c
== '\n' )
775 link_id_buffer
[ link_id_n
] = 0x00;
776 int value
= atoi( link_id_buffer
);
778 target
->links
[value
>= 0? 1:0] = abs(value
);
787 vg_error( "Invalid character '%c' (row: %u)\n", *c
, world
.h
);
793 vg_error( "Too many values to assign (row: %u)\n", world
.h
);
802 // Registry length-error checks
803 if( reg_start
!= reg_end
)
805 vg_error( "Not enough spawn values assigned (row: %u, %u of %u)\n", world
.h
, reg_start
, reg_end
);
809 if( links_satisfied
!= arrlen( links_to_make
) )
811 vg_error( "Not enough link values assigned (row: %u, %u of %u)\n", world
.h
, links_satisfied
, arrlen( links_to_make
) );
817 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world
.h
, cx
, world
.w
);
821 row
= arraddnptr( world
.data
, world
.w
);
824 reg_end
= reg_start
= arrlen( world
.io
);
826 arrsetlen( links_to_make
, 0 );
833 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world
.h
, cx
, world
.w
);
837 // Tile initialization
839 struct cell
*cell
= &row
[ cx
];
845 if( *c
== '+' || *c
== '-' || *c
== '*' )
847 struct cell_terminal
*term
= arraddnptr( world
.io
, 1 );
849 term
->pos
[1] = world
.h
;
852 term
->runs
[0].step_count
= 0;
856 case '+': cell
->state
= FLAG_INPUT
; break;
857 case '-': cell
->state
= FLAG_OUTPUT
; break;
858 case '*': cell
->state
= FLAG_EMITTER
; break;
863 else if( *c
== '.' ) cell
->state
= FLAG_INPUT_NICE
;
864 else if( *c
== '#' ) cell
->state
= FLAG_WALL
;
865 else if( ((u32
)*c
>= (u32
)'A') && ((u32
)*c
<= (u32
)'A'+0xf) )
867 // Canal flag bits (4bit/16 value):
873 cell
->state
= ((u32
)*c
- (u32
)'A') & (FLAG_CANAL
|FLAG_IS_TRIGGER
);
875 if( cell
->state
& FLAG_IS_TRIGGER
)
876 arrpush( links_to_make
, cx
+ world
.h
*world
.w
);
880 else cell
->state
= 0x00;
888 // Assign emitter codes
889 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
891 struct cell_terminal
*term
= &world
.io
[i
];
892 struct cell
*cell
= pcell( term
->pos
);
894 if( cell
->state
& FLAG_EMITTER
)
896 if( (term
->run_count
> 0) && (term
->runs
[0].step_count
>= 2) )
898 cell
->emit
[0] = term
->runs
[0].steps
[0];
899 cell
->emit
[1] = term
->runs
[0].steps
[1];
903 vg_error( "Emitter was not assigned emit values\n" );
909 // Update data texture to fill out the background
911 u8 info_buffer
[64*64*4];
912 for( int x
= 0; x
< 64; x
++ )
914 for( int y
= 0; y
< 64; y
++ )
916 u8
*px
= &info_buffer
[((x
*64)+y
)*4];
918 // Fade out edges of world so that there isnt an obvious line
919 int dist_x
= 16 - VG_MIN( VG_MIN( x
, 16 ), 16-VG_MAX( x
-16-world
.w
, 0 ) );
920 int dist_y
= 16 - VG_MIN( VG_MIN( y
, 16 ), 16-VG_MAX( y
-16-world
.h
, 0 ) );
921 int dist
= VG_MAX( dist_x
, dist_y
) * 16;
923 int value
= VG_MAX( 0, 0xFF-0x3F + hash21i( (v2i
){x
,y
}, 0x3F ) - dist
);
932 // Level selection area
934 for( int i
= 0; i
< vg_list_size( career_packs
); i
++ )
936 struct career_level_pack
*grid
= &career_packs
[ i
];
940 for( int y
= 0; y
< grid
->dims
[1]; y
++ )
942 for( int x
= 0; x
< grid
->dims
[0]; x
++ )
944 u8
*px
= &info_buffer
[((y
+16+grid
->origin
[1])*64+16+x
+grid
->origin
[0])*4];
947 if( j
< grid
->count
)
949 struct cmp_level
*lvl
= &grid
->pack
[ j
++ ];
950 v2i_add( grid
->origin
, (v2i
){x
,y
}, lvl
->btn
.position
);
956 info_buffer
[(((16+world
.h
-3)*64)+world
.w
+16-1)*4] = 0x30;
957 info_buffer
[(((16+world
.h
-2)*64)+world
.w
+16-1)*4] = 0x30;
959 // Random walks.. kinda
960 for( int i
= 0; i
< arrlen(world
.io
); i
++ )
962 struct cell_terminal
*term
= &world
.io
[ i
];
968 // Only make breakouts for terminals on the edge
969 if( !(term
->pos
[1] == 1 || term
->pos
[1] == world
.h
-2) )
972 turtle
[0] = 16+term
->pos
[0];
973 turtle
[1] = 16+term
->pos
[1];
976 turtle_dir
[1] = pcell(term
->pos
)->state
& (FLAG_INPUT
|FLAG_EMITTER
)? 1: -1;
977 original_y
= turtle_dir
[1];
979 info_buffer
[((turtle
[1]*64)+turtle
[0])*4] = 0;
980 v2i_add( turtle_dir
, turtle
, turtle
);
982 for( int i
= 0; i
< 100; i
++ )
984 info_buffer
[((turtle
[1]*64)+turtle
[0])*4] = 0;
986 v2i_add( turtle_dir
, turtle
, turtle
);
988 if( turtle
[0] == 0 ) break;
989 if( turtle
[0] == 63 ) break;
990 if( turtle
[1] == 0 ) break;
991 if( turtle
[1] == 63 ) break;
993 int random_value
= hash21i( turtle
, 0xFF );
994 if( random_value
> 255-40 && !turtle_dir
[0] )
999 else if( random_value
> 255-80 && !turtle_dir
[0] )
1004 else if( random_value
> 255-100 )
1007 turtle_dir
[1] = original_y
;
1012 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
1013 glTexSubImage2D( GL_TEXTURE_2D
, 0, 0, 0, 64, 64, GL_RGBA
, GL_UNSIGNED_BYTE
, info_buffer
);
1016 arrfree( links_to_make
);
1018 map_reclassify( NULL
, NULL
, 1 );
1021 for( int i
= 0; i
< world
.h
*world
.w
; i
++ )
1023 struct cell
*src
= &world
.data
[i
];
1024 if( src
->state
& FLAG_IS_TRIGGER
)
1026 int link_id
= src
->links
[0]?0:1;
1027 if( src
->links
[link_id
] <= world
.h
*world
.w
)
1029 struct cell
*target
= &world
.data
[ src
->links
[link_id
] ];
1030 if( ((target
->state
& FLAG_CANAL
) && (target
->config
== k_cell_type_split
))
1031 || (target
->state
& FLAG_EMITTER
) )
1033 if( target
->links
[ link_id
] )
1035 vg_error( "Link target was already targeted\n" );
1041 target
->links
[ link_id
] = i
;
1042 target
->state
|= FLAG_TARGETED
;
1047 vg_error( "Link target was invalid\n" );
1053 vg_error( "Link target out of bounds\n" );
1059 // ==========================================================
1062 vg_success( "Map '%s' loaded! (%u:%u)\n", name
, world
.w
, world
.h
);
1066 strncpy( world
.map_name
, name
, vg_list_size( world
.map_name
)-1 );
1067 world
.initialzed
= 1;
1069 // Setup world button locations
1070 for( int i
= 0; i
< vg_list_size( world
.st
.buttons
); i
++ )
1072 struct world_button
*btn
= &world
.st
.buttons
[i
];
1073 btn
->position
[0] = world
.w
-1;
1074 btn
->position
[1] = world
.h
-i
-2;
1077 // Allocate buffers for render commands
1078 world
.cmd_buf_tiles
= malloc( world
.w
*world
.h
* sizeof( struct render_cmd
) );
1079 world
.max_commands
= world
.w
*world
.h
;
1084 arrfree( links_to_make
);
1089 static void map_serialize( FILE *stream
)
1091 for( int y
= 0; y
< world
.h
; y
++ )
1093 for( int x
= 0; x
< world
.w
; x
++ )
1095 struct cell
*cell
= pcell( (v2i
){ x
, y
} );
1097 if( cell
->state
& FLAG_WALL
) fputc( '#', stream
);
1098 else if( cell
->state
& FLAG_INPUT_NICE
) fputc( '.', stream
);
1099 else if( cell
->state
& FLAG_INPUT
) fputc( '+', stream
);
1100 else if( cell
->state
& FLAG_OUTPUT
) fputc( '-', stream
);
1101 else if( cell
->state
& FLAG_EMITTER
) fputc( '*', stream
);
1102 else if( cell
->state
& (FLAG_CANAL
|FLAG_IS_TRIGGER
|FLAG_RESERVED0
|FLAG_RESERVED1
) )
1104 fputc( (cell
->state
& (FLAG_CANAL
|FLAG_IS_TRIGGER
|FLAG_RESERVED0
|FLAG_RESERVED1
)) + (u32
)'A', stream
);
1106 else fputc( ' ', stream
);
1109 fputc( ';', stream
);
1111 int terminal_write_count
= 0;
1113 for( int x
= 0; x
< world
.w
; x
++ )
1115 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1117 struct cell_terminal
*term
= &world
.io
[i
];
1118 if( v2i_eq( term
->pos
, (v2i
){x
,y
} ) )
1120 if( terminal_write_count
)
1121 fputc( ',', stream
);
1122 terminal_write_count
++;
1124 for( int j
= 0; j
< term
->run_count
; j
++ )
1126 struct terminal_run
*run
= &term
->runs
[j
];
1128 for( int k
= 0; k
< run
->step_count
; k
++ )
1130 i8 step
= run
->steps
[k
];
1131 fputc( step
== -1? ' ': ('a' + run
->steps
[k
]), stream
);
1134 if( j
< term
->run_count
-1 )
1135 fputc( ':', stream
);
1141 for( int x
= 0; x
< world
.w
; x
++ )
1143 struct cell
*cell
= pcell( (v2i
){ x
,y
} );
1144 if( cell
->state
& FLAG_IS_TRIGGER
)
1146 if( terminal_write_count
)
1147 fputc( ',', stream
);
1148 terminal_write_count
++;
1150 fprintf( stream
, "%d", cell
->links
[0]? -cell
->links
[0]: cell
->links
[1] );
1154 fputc( '\n', stream
);
1159 // ===========================================================================================================
1161 #pragma pack(push,1)
1162 struct dcareer_state
1175 levels
[ NUM_CAMPAIGN_LEVELS
];
1179 static int career_load_success
= 0;
1181 static void career_serialize(void)
1183 if( !career_load_success
)
1186 struct dcareer_state encoded
;
1187 encoded
.version
= 2;
1188 encoded
.in_map
= world
.pCmpLevel
? world
.pCmpLevel
->serial_id
: -1;
1190 memset( encoded
.reserved
, 0, sizeof( encoded
.reserved
) );
1192 for( int i
= 0; i
< vg_list_size( career_packs
); i
++ )
1194 struct career_level_pack
*set
= &career_packs
[i
];
1196 for( int j
= 0; j
< set
->count
; j
++ )
1198 struct cmp_level
*lvl
= &set
->pack
[j
];
1199 struct dlevel_state
*dest
= &encoded
.levels
[lvl
->serial_id
];
1201 dest
->score
= lvl
->completed_score
;
1202 dest
->unlocked
= lvl
->unlocked
;
1203 dest
->reserved
[0] = 0;
1204 dest
->reserved
[1] = 0;
1208 vg_asset_write( "sav/game.sv2", &encoded
, sizeof( struct dcareer_state
) );
1211 static void career_unlock_level( struct cmp_level
*lvl
);
1212 static void career_unlock_level( struct cmp_level
*lvl
)
1217 career_unlock_level( lvl
->linked
);
1220 static void career_pass_level( struct cmp_level
*lvl
, int score
, int upload
)
1224 if( score
< lvl
->completed_score
|| lvl
->completed_score
== 0 )
1227 if( !lvl
->is_tutorial
&& upload
)
1228 leaderboard_set_score( lvl
, score
);
1231 lvl
->completed_score
= score
;
1234 if( lvl
->unlock
) career_unlock_level( lvl
->unlock
);
1237 if( lvl
->achievement
)
1239 sw_set_achievement( lvl
->achievement
);
1242 // Check ALL maps to trigger master engineer
1243 for( int i
= 0; i
< vg_list_size( career_packs
); i
++ )
1245 struct career_level_pack
*set
= &career_packs
[i
];
1247 for( int j
= 0; j
< set
->count
; j
++ )
1249 if( set
->pack
[j
].completed_score
== 0 )
1254 sw_set_achievement( "MASTER_ENGINEER" );
1259 static void career_reset_level( struct cmp_level
*lvl
)
1262 lvl
->completed_score
= 0;
1265 static void career_load(void)
1268 struct dcareer_state encoded
;
1271 memset( (void*)&encoded
, 0, sizeof( struct dcareer_state
) );
1273 encoded
.levels
[0].unlocked
= 1;
1275 // Load and copy data into encoded
1276 void *cr
= vg_asset_read_s( "sav/game.sv2", &sz
);
1280 if( sz
> sizeof( struct dcareer_state
) )
1281 vg_warn( "This save file is too big! Some levels will be lost\n" );
1283 if( sz
<= offsetof( struct dcareer_state
, levels
) )
1285 vg_warn( "This save file is too small to have a header. Creating a blank one\n" );
1289 memcpy( (void*)&encoded
, cr
, VG_MIN( sizeof( struct dcareer_state
), sz
) );
1293 vg_info( "No save file... Using blank one\n" );
1296 for( int i
= 0; i
< vg_list_size( career_packs
); i
++ )
1298 struct career_level_pack
*set
= &career_packs
[i
];
1300 for( int j
= 0; j
< set
->count
; j
++ )
1301 career_reset_level( &set
->pack
[j
] );
1304 // Header information
1305 // =================================
1307 struct cmp_level
*lvl_to_load
= &career_packs
[0].pack
[0];
1309 // Decode everything from dstate
1310 for( int i
= 0; i
< vg_list_size( career_packs
); i
++ )
1312 struct career_level_pack
*set
= &career_packs
[i
];
1314 for( int j
= 0; j
< set
->count
; j
++ )
1316 struct cmp_level
*lvl
= &set
->pack
[j
];
1317 struct dlevel_state
*src
= &encoded
.levels
[lvl
->serial_id
];
1319 if( src
->unlocked
) career_unlock_level( lvl
);
1320 if( src
->score
) lvl
->completed_score
= src
->score
;
1322 if( lvl
->serial_id
== encoded
.in_map
)
1327 if( console_changelevel( 1, &lvl_to_load
->map_name
) )
1329 world
.pCmpLevel
= lvl_to_load
;
1330 gen_level_text( world
.pCmpLevel
);
1333 career_load_success
= 1;
1337 // ===========================================================================================================
1338 static int is_simulation_running(void)
1340 return world
.st
.buttons
[ k_world_button_sim
].state
;
1343 static void clear_animation_flags(void)
1345 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
1346 world
.data
[ i
].state
&= ~(FLAG_FLIP_FLOP
|FLAG_FLIP_ROTATING
);
1349 static void simulation_stop(void)
1351 world
.st
.buttons
[ k_world_button_sim
].state
= 0;
1352 world
.st
.buttons
[ k_world_button_pause
].state
= 0;
1354 world
.num_fishes
= 0;
1355 world
.sim_frame
= 0;
1357 world
.frame_lerp
= 0.0f
;
1361 sfx_system_fadeout( &audio_system_balls_rolling
, 44100 );
1363 clear_animation_flags();
1365 vg_info( "Stopping simulation!\n" );
1368 static void simulation_start(void)
1370 vg_success( "Starting simulation!\n" );
1372 sfx_set_playrnd( &audio_rolls
, &audio_system_balls_rolling
, 0, 1 );
1374 world
.num_fishes
= 0;
1375 world
.sim_frame
= 0;
1378 world
.sim_delta_speed
= world
.st
.buttons
[ k_world_button_speedy
].state
? 10.0f
: 2.5f
;
1379 world
.sim_delta_ref
= vg_time
;
1380 world
.sim_internal_ref
= 0.0f
;
1381 world
.sim_internal_time
= 0.0f
;
1382 world
.pause_offset_target
= 0.0f
;
1384 world
.sim_target
= 0;
1386 clear_animation_flags();
1390 if( world
.pCmpLevel
)
1392 world
.pCmpLevel
->completed_score
= 0;
1396 static int world_check_pos_ok( v2i co
, int dist
)
1398 return (co
[0] < dist
|| co
[0] >= world
.w
-dist
|| co
[1] < dist
|| co
[1] >= world
.h
-dist
)? 0: 1;
1401 static int cell_interactive( v2i co
)
1406 if( world_check_pos_ok( co
, 1 ) )
1409 if( cell
->state
& FLAG_EMITTER
)
1413 if( !world_check_pos_ok( co
, 2 ) )
1417 if( cell
->state
& (FLAG_WALL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1420 // List of 3x3 configurations that we do not allow
1421 static u32 invalid_src
[][9] =
1453 // Statically compile invalid configurations into bitmasks
1454 static u32 invalid
[ vg_list_size(invalid_src
) ];
1456 for( int i
= 0; i
< vg_list_size(invalid_src
); i
++ )
1460 for( int j
= 0; j
< 3; j
++ )
1461 for( int k
= 0; k
< 3; k
++ )
1462 comped
|= invalid_src
[i
][ j
*3+k
] << ((j
*5)+k
);
1464 invalid
[i
] = comped
;
1467 // Extract 5x5 grid surrounding tile
1469 for( int y
= co
[1]-2; y
< co
[1]+3; y
++ )
1470 for( int x
= co
[0]-2; x
< co
[0]+3; x
++ )
1472 struct cell
*cell
= pcell((v2i
){x
,y
});
1474 if( cell
&& (cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
|FLAG_EMITTER
)) )
1475 blob
|= 0x1 << ((y
-(co
[1]-2))*5 + x
-(co
[0]-2));
1478 // Run filter over center 3x3 grid to check for invalid configurations
1479 int kernel
[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
1480 for( int i
= 0; i
< vg_list_size(kernel
); i
++ )
1482 if( blob
& (0x1 << (6+kernel
[i
])) )
1484 u32 window
= blob
>> kernel
[i
];
1486 for( int j
= 0; j
< vg_list_size(invalid
); j
++ )
1487 if((window
& invalid
[j
]) == invalid
[j
])
1495 static void vg_update(void)
1498 if( world
.st
.lvl_to_load
)
1500 world
.st
.world_transition
= (world
.st
.lvl_load_time
-vg_time
) * 4.0f
;
1502 if( vg_time
> world
.st
.lvl_load_time
)
1504 if( console_changelevel( 1, &world
.st
.lvl_to_load
->map_name
) )
1506 world
.pCmpLevel
= world
.st
.lvl_to_load
;
1507 gen_level_text( world
.pCmpLevel
);
1510 world
.st
.lvl_to_load
= NULL
;
1515 world
.st
.world_transition
= vg_minf( 1.0f
, (vg_time
-world
.st
.lvl_load_time
) * 4.0f
);
1519 // ========================================================================================================
1521 float r1
= (float)vg_window_y
/ (float)vg_window_x
,
1522 r2
= (float)world
.h
/ (float)world
.w
,
1525 static float size_current
= 2.0f
;
1526 static v3f origin_current
= { 0.0f
, 0.0f
, 0.0f
};
1527 static v2f drag_offset
= { 0.0f
, 0.0f
};
1528 static v2f view_point
= { 0.0f
, 0.0f
};
1531 size
= ( r2
< r1
? (float)(world
.w
+5) * 0.5f
: ((float)(world
.h
+5) * 0.5f
) / r1
) + 0.5f
;
1536 origin
[0] = floorf( -0.5f
* ((float)world
.w
-4.5f
) );
1537 origin
[1] = floorf( -0.5f
* world
.h
);
1539 // Create and clamp result view
1540 v2_add( view_point
, drag_offset
, result_view
);
1541 result_view
[0] = vg_clampf( result_view
[0], -world
.st
.zoom
, world
.st
.zoom
);
1542 result_view
[1] = vg_clampf( result_view
[1], -world
.st
.zoom
*r1
, world
.st
.zoom
*r1
);
1544 v2_add( origin
, result_view
, vt_target
);
1546 // Lerp towards target
1547 size_current
= vg_lerpf( size_current
, size
- world
.st
.zoom
, vg_time_delta
* 6.0f
);
1548 v2_lerp( origin_current
, vt_target
, vg_time_delta
* 6.0f
, origin_current
);
1550 m3x3_projection( m_projection
, -size_current
, size_current
, -size_current
*r1
, size_current
*r1
);
1551 m3x3_identity( m_view
);
1552 m3x3_translate( m_view
, origin_current
);
1553 m3x3_mul( m_projection
, m_view
, vg_pv
);
1554 vg_projection_update();
1557 // ========================================================================================================
1558 v2_copy( vg_mouse_ws
, world
.tile_pos
);
1560 world
.tile_x
= floorf( world
.tile_pos
[0] );
1561 world
.tile_y
= floorf( world
.tile_pos
[1] );
1565 static v2f drag_origin
; // x/y pixel
1567 if( vg_get_button_down( "tertiary" ) )
1568 v2_copy( vg_mouse
, drag_origin
);
1569 else if( vg_get_button( "tertiary" ) )
1572 v2_sub( vg_mouse
, drag_origin
, drag_offset
);
1573 v2_div( drag_offset
, (v2f
){ vg_window_x
, vg_window_y
}, drag_offset
);
1574 v2_mul( drag_offset
, (v2f
){ size_current
*2.0f
, -size_current
*r1
*2.0f
}, drag_offset
);
1578 v2_copy( result_view
, view_point
);
1579 v2_copy( (v2f
){0.0f
,0.0f
}, drag_offset
);
1591 rsize
= size
-world
.st
.zoom
;
1593 v2_div( vg_mouse
, (v2f
){ vg_window_x
*0.5f
, vg_window_y
*0.5f
}, mview_local
);
1594 v2_add( (v2f
){ -rsize
, -rsize
*r1
}, (v2f
){ mview_local
[0]*rsize
, (2.0f
-mview_local
[1])*rsize
*r1
}, mview_cur
);
1596 world
.st
.zoom
= vg_clampf( world
.st
.zoom
+ vg_mouse_wheel
[1], 0.0f
, size
- 4.0f
);
1598 // Recalculate new position
1599 rsize
= size
-world
.st
.zoom
;
1600 v2_add( (v2f
){ -rsize
, -rsize
*r1
}, (v2f
){ mview_local
[0]*rsize
, (2.0f
-mview_local
[1])*rsize
*r1
}, mview_new
);
1603 v2_sub( mview_new
, mview_cur
, mview_delta
);
1604 v2_add( mview_delta
, view_point
, view_point
);
1608 // ========================================================================================================
1609 if( !is_simulation_running() && !gui_want_mouse() )
1611 v2_copy( vg_mouse_ws
, world
.drag_to_co
);
1613 if( cell_interactive( (v2i
){ world
.tile_x
, world
.tile_y
} ))
1615 world
.selected
= world
.tile_y
* world
.w
+ world
.tile_x
;
1617 static u32 modify_state
= 0;
1618 struct cell
*cell_ptr
= &world
.data
[world
.selected
];
1620 if( !(cell_ptr
->state
& FLAG_EMITTER
) )
1622 if( vg_get_button_down("primary") )
1623 modify_state
= (cell_ptr
->state
& FLAG_CANAL
) ^ FLAG_CANAL
;
1625 if( vg_get_button("primary") && ((cell_ptr
->state
& FLAG_CANAL
) != modify_state
) )
1627 cell_ptr
->state
&= ~FLAG_CANAL
;
1628 cell_ptr
->state
|= modify_state
;
1630 if( cell_ptr
->state
& FLAG_CANAL
)
1632 cell_ptr
->links
[0] = 0;
1633 cell_ptr
->links
[1] = 0;
1635 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 3, 6 );
1640 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 0, 3 );
1644 map_reclassify((v2i
){ world
.tile_x
-2, world
.tile_y
-2 },
1645 (v2i
){ world
.tile_x
+2, world
.tile_y
+2 }, 1 );
1648 if( vg_get_button_down("secondary") && (cell_ptr
->state
& FLAG_CANAL
) && !(cell_ptr
->config
== k_cell_type_split
) )
1650 world
.id_drag_from
= world
.selected
;
1652 struct cell_description
*desc
= &cell_descriptions
[ world
.data
[world
.id_drag_from
].config
];
1653 v2_add( desc
->trigger_pos
, (v2f
){ world
.tile_x
, world
.tile_y
}, world
.drag_from_co
);
1657 float local_x
= vg_mouse_ws
[0] - (float)world
.tile_x
;
1659 if( vg_get_button_up("secondary") && world
.id_drag_from
== world
.selected
)
1661 u32 link_id
= cell_ptr
->links
[ 0 ]? 0: 1;
1663 // break existing connection off
1664 if( cell_ptr
->links
[ link_id
] )
1666 struct cell
*current_connection
= &world
.data
[ cell_ptr
->links
[ link_id
]];
1668 if( !current_connection
->links
[ link_id
^ 0x1 ] )
1669 current_connection
->state
&= ~FLAG_TARGETED
;
1671 current_connection
->links
[ link_id
] = 0;
1672 cell_ptr
->links
[ link_id
] = 0;
1675 cell_ptr
->state
&= ~FLAG_IS_TRIGGER
;
1676 world
.id_drag_from
= 0;
1679 else if( world
.id_drag_from
&& (cell_ptr
->state
& (FLAG_CANAL
|FLAG_EMITTER
)) &&
1680 ((cell_ptr
->config
== k_cell_type_split
) || (cell_ptr
->state
& FLAG_EMITTER
)) )
1682 world
.drag_to_co
[0] = (float)world
.tile_x
+ (local_x
> 0.5f
? 0.75f
: 0.25f
);
1683 world
.drag_to_co
[1] = (float)world
.tile_y
+ 0.25f
;
1685 if( vg_get_button_up("secondary") )
1687 struct cell
*drag_ptr
= &world
.data
[world
.id_drag_from
];
1688 u32 link_id
= local_x
> 0.5f
? 1: 0;
1690 // Cleanup existing connections
1691 if( cell_ptr
->links
[ link_id
] )
1693 vg_warn( "Destroying existing connection on link %u (%hu)\n", link_id
, cell_ptr
->links
[ link_id
] );
1695 struct cell
*current_connection
= &world
.data
[ cell_ptr
->links
[ link_id
]];
1696 current_connection
->state
&= ~FLAG_IS_TRIGGER
;
1697 current_connection
->links
[ link_id
] = 0;
1700 for( u32 i
= 0; i
< 2; i
++ )
1702 if( drag_ptr
->links
[ i
] )
1704 vg_warn( "Destroying link %u (%hu)\n", i
, drag_ptr
->links
[ i
] );
1706 struct cell
*current_connection
= &world
.data
[ drag_ptr
->links
[ i
]];
1707 if( current_connection
->links
[ i
^ 0x1 ] == 0 )
1708 current_connection
->state
&= ~FLAG_TARGETED
;
1710 current_connection
->links
[ i
] = 0;
1711 drag_ptr
->links
[ i
] = 0;
1715 // Create the new connection
1716 vg_success( "Creating connection on link %u (%hu)\n", link_id
, world
.id_drag_from
);
1718 cell_ptr
->links
[ link_id
] = world
.id_drag_from
;
1719 drag_ptr
->links
[ link_id
] = world
.selected
;
1721 cell_ptr
->state
|= FLAG_TARGETED
;
1722 drag_ptr
->state
|= FLAG_IS_TRIGGER
;
1723 world
.id_drag_from
= 0;
1729 world
.selected
= -1;
1732 if( !(vg_get_button("secondary") && world
.id_drag_from
) )
1733 world
.id_drag_from
= 0;
1737 world
.selected
= -1;
1738 world
.id_drag_from
= 0;
1741 // Marble state updates
1742 // ========================================================================================================
1743 if( is_simulation_running() )
1745 float old_time
= world
.sim_internal_time
;
1747 if( !world
.st
.buttons
[ k_world_button_pause
].state
)
1748 world
.sim_internal_time
= world
.sim_internal_ref
+ (vg_time
-world
.sim_delta_ref
) * world
.sim_delta_speed
;
1750 world
.sim_internal_time
= vg_lerpf( world
.sim_internal_time
, world
.sim_internal_ref
+ world
.pause_offset_target
, vg_time_delta
*15.0f
);
1751 world
.sim_internal_delta
= world
.sim_internal_time
-old_time
;
1753 world
.sim_target
= (int)floorf(world
.sim_internal_time
);
1755 int success_this_frame
= 0;
1756 int failure_this_frame
= 0;
1758 while( world
.sim_frame
< world
.sim_target
)
1760 sfx_set_playrnd( &audio_random
, &audio_system_balls_switching
, 0, 8 );
1762 // Update splitter deltas
1763 for( int i
= 0; i
< world
.h
*world
.w
; i
++ )
1765 struct cell
*cell
= &world
.data
[i
];
1766 if( cell
->config
== k_cell_type_split
)
1768 cell
->state
&= ~FLAG_FLIP_ROTATING
;
1770 if( cell
->state
& (FLAG_IS_TRIGGER
|FLAG_EMITTER
) )
1771 cell
->state
&= ~FLAG_TRIGGERED
;
1774 int alive_count
= 0;
1776 // Update fish positions
1777 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1779 struct fish
*fish
= &world
.fishes
[i
];
1781 if( fish
->state
== k_fish_state_soon_dead
)
1782 fish
->state
= k_fish_state_dead
;
1784 if( fish
->state
== k_fish_state_soon_alive
)
1785 fish
->state
= k_fish_state_alive
;
1787 if( fish
->state
< k_fish_state_alive
)
1790 struct cell
*cell_current
= pcell( fish
->pos
);
1792 if( fish
->state
== k_fish_state_alive
)
1795 if( cell_current
->state
& FLAG_OUTPUT
)
1797 for( int j
= 0; j
< arrlen( world
.io
); j
++ )
1799 struct cell_terminal
*term
= &world
.io
[j
];
1801 if( v2i_eq( term
->pos
, fish
->pos
) )
1803 struct terminal_run
*run
= &term
->runs
[ world
.sim_run
];
1804 if( run
->recv_count
< vg_list_size( run
->recieved
) )
1806 if( fish
->colour
== run
->steps
[ run
->recv_count
] )
1807 success_this_frame
= 1;
1809 failure_this_frame
= 1;
1811 run
->recieved
[ run
->recv_count
++ ] = fish
->colour
;
1814 failure_this_frame
= 1;
1820 fish
->state
= k_fish_state_dead
;
1821 fish
->death_time
= -1000.0f
;
1826 if( cell_current
->config
== k_cell_type_merge
)
1831 fish
->flow_reversed
= 0;
1835 if( cell_current
->config
== k_cell_type_split
)
1838 fish
->dir
[0] = cell_current
->state
&FLAG_FLIP_FLOP
?1:-1;
1841 if( !(cell_current
->state
& FLAG_TARGETED
) )
1842 cell_current
->state
^= FLAG_FLIP_FLOP
;
1846 // Apply cell out-flow
1847 struct cell_description
*desc
= &cell_descriptions
[ cell_current
->config
];
1849 v2i_copy( fish
->flow_reversed
? desc
->start
: desc
->end
, fish
->dir
);
1853 v2i_add( fish
->pos
, fish
->dir
, pos_next
);
1855 struct cell
*cell_next
= pcell( pos_next
);
1857 if( cell_next
->state
& (FLAG_CANAL
|FLAG_OUTPUT
) )
1859 struct cell_description
*desc
= &cell_descriptions
[ cell_next
->config
];
1861 if( cell_next
->config
== k_cell_type_merge
)
1863 if( fish
->dir
[0] == 0 )
1865 fish
->state
= k_fish_state_dead
;
1866 fish
->death_time
= world
.sim_internal_time
;
1869 fish
->flow_reversed
= 0;
1873 if( cell_next
->config
== k_cell_type_split
)
1875 if( fish
->dir
[0] == 0 )
1877 sfx_set_playrnd( &audio_splitter
, &audio_system_balls_important
, 0, 1 );
1878 cell_next
->state
|= FLAG_FLIP_ROTATING
;
1880 fish
->flow_reversed
= 0;
1884 fish
->state
= k_fish_state_dead
;
1885 fish
->death_time
= world
.sim_internal_time
;
1889 fish
->flow_reversed
= ( fish
->dir
[0] != -desc
->start
[0] ||
1890 fish
->dir
[1] != -desc
->start
[1] )? 1: 0;
1895 if( world_check_pos_ok( fish
->pos
, 2 ) )
1896 fish
->state
= k_fish_state_bg
;
1899 fish
->state
= k_fish_state_dead
;
1900 fish
->death_time
= world
.sim_internal_time
;
1905 //v2i_add( fish->pos, fish->dir, fish->pos );
1907 else if( fish
->state
== k_fish_state_bg
)
1909 v2i_add( fish
->pos
, fish
->dir
, fish
->pos
);
1911 if( !world_check_pos_ok( fish
->pos
, 2 ) )
1913 fish
->state
= k_fish_state_dead
;
1914 fish
->death_time
= -1000.0f
;
1918 struct cell
*cell_entry
= pcell( fish
->pos
);
1920 if( cell_entry
->state
& FLAG_CANAL
)
1922 if( cell_entry
->config
== k_cell_type_con_r
|| cell_entry
->config
== k_cell_type_con_u
1923 || cell_entry
->config
== k_cell_type_con_l
|| cell_entry
->config
== k_cell_type_con_d
)
1926 sw_set_achievement( "CAN_DO_THAT" );
1929 fish
->state
= k_fish_state_soon_alive
;
1933 fish
->flow_reversed
= 1;
1935 switch( cell_entry
->config
)
1937 case k_cell_type_con_r
: fish
->dir
[0] = 1; break;
1938 case k_cell_type_con_l
: fish
->dir
[0] = -1; break;
1939 case k_cell_type_con_u
: fish
->dir
[1] = 1; break;
1940 case k_cell_type_con_d
: fish
->dir
[1] = -1; break;
1946 else { vg_error( "fish behaviour unimplemented for behaviour type (%d)\n" ); }
1948 if( fish
->state
>= k_fish_state_alive
)
1952 // Second pass (triggers)
1953 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1955 struct fish
*fish
= &world
.fishes
[i
];
1958 if( fish
->state
== k_fish_state_alive
)
1959 v2i_add( fish
->pos
, fish
->dir
, fish
->pos
);
1961 if( fish
->state
== k_fish_state_alive
|| fish
->state
== k_fish_state_soon_alive
)
1963 struct cell
*cell_current
= pcell( fish
->pos
);
1965 if( cell_current
->state
& FLAG_IS_TRIGGER
)
1967 int trigger_id
= cell_current
->links
[0]?0:1;
1969 struct cell
*target_peice
= &world
.data
[ cell_current
->links
[trigger_id
] ];
1972 if( (target_peice
->state
& FLAG_EMITTER
) && !(target_peice
->state
& FLAG_TRIGGERED
))
1974 struct fish
*fish
= &world
.fishes
[ world
.num_fishes
];
1975 lcell( cell_current
->links
[trigger_id
], fish
->pos
);
1977 fish
->state
= k_fish_state_soon_alive
;
1978 fish
->colour
= target_peice
->emit
[ trigger_id
];
1980 if( target_peice
->config
!= k_cell_type_stub
)
1982 struct cell_description
*desc
= &cell_descriptions
[ target_peice
->config
];
1983 v2i_copy( desc
->start
, fish
->dir
);
1984 fish
->flow_reversed
= 1;
1986 world
.num_fishes
++;
1993 target_peice
->state
|= FLAG_FLIP_FLOP
;
1995 target_peice
->state
&= ~FLAG_FLIP_FLOP
;
1998 cell_current
->state
|= FLAG_TRIGGERED
;
2003 // Third pass (collisions)
2004 struct fish
*fi
, *fj
;
2006 for( int i
= 0; i
< world
.num_fishes
; i
++ )
2008 fi
= &world
.fishes
[i
];
2010 if( fi
->state
== k_fish_state_alive
)
2012 int continue_again
= 0;
2014 for( int j
= i
+1; j
< world
.num_fishes
; j
++ )
2016 fj
= &world
.fishes
[j
];
2018 if( fj
->state
== k_fish_state_alive
)
2023 v2i_sub( fi
->pos
, fi
->dir
, fi_prev
);
2024 v2i_sub( fj
->pos
, fj
->dir
, fj_prev
);
2027 collide_next_frame
= (
2028 (fi
->pos
[0] == fj
->pos
[0]) &&
2029 (fi
->pos
[1] == fj
->pos
[1]))? 1: 0,
2030 collide_this_frame
= (
2031 (fi_prev
[0] == fj
->pos
[0]) &&
2032 (fi_prev
[1] == fj
->pos
[1]) &&
2033 (fj_prev
[0] == fi
->pos
[0]) &&
2034 (fj_prev
[1] == fi
->pos
[1])
2037 if( collide_next_frame
|| collide_this_frame
)
2040 sw_set_achievement( "BANG" );
2043 // Shatter death (+0.5s)
2044 float death_time
= world
.sim_internal_time
+ ( collide_this_frame
? 0.0f
: 0.5f
);
2046 fi
->state
= k_fish_state_soon_dead
;
2047 fj
->state
= k_fish_state_soon_dead
;
2048 fi
->death_time
= death_time
;
2049 fj
->death_time
= death_time
;
2056 if( continue_again
)
2062 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
2064 struct cell_terminal
*term
= &world
.io
[ i
];
2065 int is_input
= pcell(term
->pos
)->state
& FLAG_INPUT
;
2069 if( world
.sim_frame
< term
->runs
[ world
.sim_run
].step_count
)
2071 i8 emit
= term
->runs
[ world
.sim_run
].steps
[ world
.sim_frame
];
2075 struct fish
*fish
= &world
.fishes
[ world
.num_fishes
];
2076 v2i_copy( term
->pos
, fish
->pos
);
2078 fish
->state
= k_fish_state_alive
;
2079 fish
->colour
= emit
;
2081 struct cell
*cell_ptr
= pcell( fish
->pos
);
2083 if( cell_ptr
->config
!= k_cell_type_stub
)
2085 struct cell_description
*desc
= &cell_descriptions
[ cell_ptr
->config
];
2087 v2i_copy( desc
->start
, fish
->dir
);
2088 fish
->flow_reversed
= 1;
2090 world
.num_fishes
++;
2097 if( alive_count
== 0 )
2099 world
.completed
= 1;
2101 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
2103 struct cell_terminal
*term
= &world
.io
[ i
];
2105 if( pcell(term
->pos
)->state
& FLAG_OUTPUT
)
2107 struct terminal_run
*run
= &term
->runs
[ world
.sim_run
];
2109 if( run
->recv_count
== run
->step_count
)
2111 for( int j
= 0; j
< run
->step_count
; j
++ )
2113 if( run
->recieved
[j
] != run
->steps
[j
] )
2115 world
.completed
= 0;
2122 world
.completed
= 0;
2128 if( world
.completed
)
2130 if( world
.sim_run
< world
.max_runs
-1 )
2132 vg_success( "Run passed, starting next\n" );
2134 world
.sim_frame
= 0;
2135 world
.sim_target
= 0;
2136 world
.num_fishes
= 0;
2138 // Reset timing reference points
2139 world
.sim_delta_ref
= vg_time
;
2140 world
.sim_internal_ref
= 0.0f
;
2142 if( world
.st
.buttons
[ k_world_button_pause
].state
)
2143 world
.pause_offset_target
= 0.5f
;
2145 world
.pause_offset_target
= 0.0f
;
2147 world
.sim_internal_time
= 0.0f
;
2149 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
2150 world
.data
[ i
].state
&= ~FLAG_FLIP_FLOP
;
2156 vg_success( "Level passed!\n" );
2159 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
2160 if( world
.data
[ i
].state
& FLAG_CANAL
)
2163 world
.score
= score
;
2164 world
.time
= world
.sim_frame
;
2166 // Copy into career data
2167 if( world
.pCmpLevel
)
2169 career_pass_level( world
.pCmpLevel
, world
.score
, 1 );
2172 sfx_set_play( &audio_tones
, &audio_system_balls_extra
, 9 );
2173 failure_this_frame
= 0;
2174 success_this_frame
= 0;
2180 if( world
.sim_run
> 0 )
2181 sw_set_achievement( "GOOD_ENOUGH" );
2184 vg_error( "Level failed :(\n" );
2195 if( failure_this_frame
)
2197 sfx_set_play( &audio_tones
, &audio_system_balls_extra
, 0 );
2199 else if( success_this_frame
)
2201 static int succes_counter
= 0;
2203 sfx_set_play( &audio_tones
, &audio_system_balls_extra
, 1+(succes_counter
++) );
2205 if( succes_counter
== 7 )
2210 // =====================================================================================================
2212 world
.frame_lerp
= world
.sim_internal_time
- floorf( world
.sim_internal_time
);
2214 for( int i
= 0; i
< world
.num_fishes
; i
++ )
2216 struct fish
*fish
= &world
.fishes
[i
];
2218 if( fish
->state
== k_fish_state_dead
)
2221 if( fish
->state
== k_fish_state_soon_dead
&& (world
.sim_internal_time
> fish
->death_time
) )
2222 continue; // Todo: particle thing?
2224 struct cell
*cell
= pcell(fish
->pos
);
2225 struct cell_description
*desc
= &cell_descriptions
[ cell
->config
];
2229 float t
= world
.frame_lerp
;
2230 if( fish
->flow_reversed
&& !desc
->is_linear
)
2233 v2_copy( fish
->physics_co
, fish
->physics_v
);
2235 switch( cell
->config
)
2237 case k_cell_type_merge
:
2238 if( fish
->dir
[0] == 1 )
2243 case k_cell_type_con_r
: curve
= curve_1
; break;
2244 case k_cell_type_con_l
: curve
= curve_4
; break;
2245 case k_cell_type_con_u
: curve
= curve_2
; break;
2246 case k_cell_type_con_d
: curve
= curve_8
; break;
2247 case 3: curve
= curve_3
; break;
2248 case 6: curve
= curve_6
; break;
2249 case 9: curve
= curve_9
; break;
2250 case 12: curve
= curve_12
; break;
2252 if( t
> curve_7_linear_section
)
2254 t
-= curve_7_linear_section
;
2255 t
*= (1.0f
/(1.0f
-curve_7_linear_section
));
2257 curve
= cell
->state
& FLAG_FLIP_FLOP
? curve_7
: curve_7_1
;
2261 default: curve
= NULL
; break;
2267 float t3
= t
* t
* t
;
2269 float cA
= 3.0f
*t2
- 3.0f
*t3
;
2270 float cB
= 3.0f
*t3
- 6.0f
*t2
+ 3.0f
*t
;
2271 float cC
= 3.0f
*t2
- t3
- 3.0f
*t
+ 1.0f
;
2273 fish
->physics_co
[0] = t3
*curve
[3][0] + cA
*curve
[2][0] + cB
*curve
[1][0] + cC
*curve
[0][0];
2274 fish
->physics_co
[1] = t3
*curve
[3][1] + cA
*curve
[2][1] + cB
*curve
[1][1] + cC
*curve
[0][1];
2275 fish
->physics_co
[0] += (float)fish
->pos
[0];
2276 fish
->physics_co
[1] += (float)fish
->pos
[1];
2281 origin
[0] = (float)fish
->pos
[0] + (float)fish
->dir
[0]*-0.5f
+ 0.5f
;
2282 origin
[1] = (float)fish
->pos
[1] + (float)fish
->dir
[1]*-0.5f
+ 0.5f
;
2284 fish
->physics_co
[0] = origin
[0] + (float)fish
->dir
[0]*t
;
2285 fish
->physics_co
[1] = origin
[1] + (float)fish
->dir
[1]*t
;
2288 v2_sub( fish
->physics_co
, fish
->physics_v
, fish
->physics_v
);
2289 v2_divs( fish
->physics_v
, world
.sim_internal_delta
, fish
->physics_v
);
2294 static void render_tile( v2i pos
, struct cell
*ptr
, v4f
const regular_colour
, v4f
const selected_colour
)
2296 int selected
= world
.selected
== pos
[1]*world
.w
+ pos
[0];
2299 uv
[0] = ptr
->config
& 0x3;
2300 uv
[1] = ptr
->config
>> 2;
2302 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ),
2311 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, selected_colour
);
2313 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
2319 // Renders specific chunk of tiles
2320 static void render_tile_block( v2i start
, v2i end
, v4f
const regular_colour
, v4f
const selected_colour
)
2322 v2i full_start
= { 0,0 };
2323 v2i full_end
= { world
.w
, world
.h
};
2325 if( !start
|| !end
)
2331 for( int y
= start
[1]; y
< end
[1]; y
++ )
2333 for( int x
= start
[0]; x
< end
[0]; x
++ )
2336 struct cell
*cell
= pcell( pos
);
2338 if( cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
|FLAG_EMITTER
) )
2339 render_tile( pos
, cell
, regular_colour
, selected_colour
);
2344 // Renders all tiles in the command list
2345 static void render_tiles( v4f
const regular_colour
, v4f
const selected_colour
)
2347 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
2351 struct render_cmd
*arr
;
2355 { world
.cmd_buf_tiles
, world
.tile_count
},
2356 { world
.cmd_buf_specials
, world
.tile_special_count
}
2359 for( int i
= 0; i
< vg_list_size( render_lists
); i
++ )
2361 struct render_list
*list
= &render_lists
[i
];
2362 for( int j
= 0; j
< list
->count
; j
++ )
2364 struct render_cmd
*cmd
= &list
->arr
[j
];
2365 struct cell
*cell
= cmd
->ptr
;
2367 render_tile( cmd
->pos
, cell
, regular_colour
, selected_colour
);
2372 static int world_button_exec( struct world_button
*btn
, v2f texture
, v3f colour
, enum world_button_status
*status
)
2374 static v2i click_grab
= { -9999, -9999 };
2379 click_grab
[0] = -9999;
2380 click_grab
[1] = -9999;
2384 v2i click_tile
= { world
.tile_x
, world
.tile_y
};
2387 int is_hovering
= v2i_eq( click_tile
, btn
->position
);
2389 // Set up light targets before logic runs
2391 btn
->light_target
= is_hovering
? 0.9f
: 0.8f
;
2393 btn
->light_target
= is_hovering
? 0.2f
: 0.0f
;
2395 if( vg_get_button( "primary" ) && is_hovering
)
2396 btn
->light_target
= 1.0f
;
2398 // Process click action
2401 if( vg_get_button_down( "primary" ) && is_hovering
)
2402 v2i_copy( click_tile
, click_grab
);
2403 else if( v2i_eq( click_grab
, click_tile
) && vg_get_button_up( "primary" ) )
2406 *status
= btn
->state
? k_world_button_on_disable
: k_world_button_on_enable
;
2408 if( btn
->mode
== k_world_button_mode_toggle
)
2411 sfx_set_play( &audio_clicks
, &audio_system_ui
, btn
->state
? 1:0 );
2419 btn
->light
= vg_lerpf( btn
->light
, btn
->light_target
+ btn
->extra_light
, vg_time_delta
*26.0f
);
2421 v3_copy( colour
, final_colour
);
2422 final_colour
[3] = btn
->light
;
2424 glUniform4f( SHADER_UNIFORM( shader_buttons
, "uOffset" ),
2430 glUniform4fv( SHADER_UNIFORM( shader_buttons
, "uColour" ), 1, final_colour
);
2436 static void level_selection_buttons(void)
2438 v3f tutorial_colour
= { 0.204f
, 0.345f
, 0.553f
};
2439 v3f locked_colour
= { 0.2f
, 0.2f
, 0.2f
};
2441 struct cmp_level
*switch_level_to
= NULL
;
2443 for( int i
= 0; i
< vg_list_size( career_packs
); i
++ )
2445 struct career_level_pack
*grid
= &career_packs
[i
];
2447 for( int j
= 0; j
< grid
->count
; j
++ )
2449 struct cmp_level
*lvl
= &grid
->pack
[ j
];
2451 if( world
.pCmpLevel
== lvl
)
2452 lvl
->btn
.extra_light
= 0.35f
+ fabsf(sinf( vg_time
* 2.0f
)) * 0.05f
;
2453 else lvl
->btn
.extra_light
= 0.2f
;
2455 if( lvl
->completed_score
)
2456 lvl
->btn
.extra_light
+= 0.8f
;
2458 enum world_button_status status
;
2459 if( world_button_exec(
2462 lvl
->unlocked
? (lvl
->is_tutorial
? tutorial_colour
: grid
->primary_colour
): locked_colour
,
2466 if( status
== k_world_button_on_enable
&& lvl
->unlocked
)
2467 switch_level_to
= lvl
;
2472 if( switch_level_to
)
2474 world
.st
.lvl_to_load
= switch_level_to
;
2475 world
.st
.lvl_load_time
= vg_time
+ 0.25f
;
2476 world
.st
.world_transition
= 1.0f
;
2479 if( console_changelevel( 1, &switch_level_to->map_name ) )
2481 world.pCmpLevel = switch_level_to;
2482 gen_level_text( world.pCmpLevel );
2488 static void render_sprite( enum sprites_auto_combine_index id
, v3f pos
)
2490 struct vg_sprite
*sp
= &sprites_auto_combine
[ id
];
2492 glUniform4fv( SHADER_UNIFORM( shader_sprite
, "uUv" ), 1, sp
->uv_xywh
);
2493 glUniform3f( SHADER_UNIFORM( shader_sprite
, "uPos" ), pos
[0], pos
[1], pos
[2] * world
.st
.world_transition
);
2498 void vg_render(void)
2500 glViewport( 0,0, vg_window_x
, vg_window_y
);
2502 glDisable( GL_DEPTH_TEST
);
2503 glClearColor( 0.369768f
, 0.3654f
, 0.42f
, 1.0f
);
2504 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
2506 v4f
const colour_default
= {1.0f
, 1.0f
, 1.0f
, 1.0f
};
2507 v4f
const colour_selected
= {0.90f
, 0.92f
, 1.0f
, 1.0f
};
2509 int const circle_base
= 6;
2510 int const filled_start
= circle_base
+0;
2511 int const filled_count
= circle_base
+32;
2512 int const empty_start
= circle_base
+32;
2513 int const empty_count
= circle_base
+32*2;
2515 struct world_theme
*theme
= &world_themes
[ world_theme_id
];
2517 if( !world
.initialzed
)
2520 // Extract render commands
2521 world
.tile_count
= 0;
2522 world
.tile_special_count
= 0;
2524 for( int y
= 1; y
< world
.h
-1; y
++ )
2526 for( int x
= 1; x
< world
.w
-1; x
++ )
2528 struct cell
*cell
= pcell((v2i
){x
,y
});
2530 if( cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
|FLAG_EMITTER
|FLAG_INPUT_NICE
) )
2532 struct render_cmd
*cmd
;
2535 (cell
->config
== k_cell_type_split
&& (cell
->state
& FLAG_CANAL
))
2536 || (cell
->state
& (FLAG_EMITTER
|FLAG_IS_TRIGGER
))
2538 cmd
= &world
.cmd_buf_tiles
[ world
.max_commands
- (++ world
.tile_special_count
) ];
2540 cmd
= &world
.cmd_buf_tiles
[ world
.tile_count
++ ];
2549 world
.cmd_buf_specials
= &world
.cmd_buf_tiles
[ world
.max_commands
- world
.tile_special_count
];
2552 // ========================================================================================================
2553 use_mesh( &world
.shapes
);
2555 SHADER_USE( shader_background
);
2556 glUniformMatrix3fv( SHADER_UNIFORM( shader_background
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
2558 glActiveTexture( GL_TEXTURE0
);
2559 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
2560 glUniform1i( SHADER_UNIFORM( shader_background
, "uTexMain" ), 0 );
2562 glUniform3f( SHADER_UNIFORM( shader_background
, "uOffset" ), -16, -16, 64 );
2563 glUniform1f( SHADER_UNIFORM( shader_background
, "uVariance" ), 0.02f
);
2565 glActiveTexture( GL_TEXTURE1
);
2566 glBindTexture( GL_TEXTURE_2D
, world
.random_samples
);
2567 glUniform1i( SHADER_UNIFORM( shader_background
, "uSamplerNoise" ), 1 );
2568 glUniform1f( SHADER_UNIFORM( shader_background
, "uVisibility" ), 1.0f
); //world.st.world_transition );
2572 // TILESET BACKGROUND LAYER
2573 // ========================================================================================================
2574 use_mesh( &world
.shapes
);
2575 SHADER_USE( shader_tile_main
);
2578 m2x2_identity( subtransform
);
2579 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
2580 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_main
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
2581 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 0.0f
);
2582 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uForeground" ), 0.0f
);
2583 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uVisibility" ), world
.st
.world_transition
* 2.0f
);
2586 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2587 glBlendEquation(GL_FUNC_ADD
);
2590 vg_tex2d_bind( &tex_tile_data
, 0 );
2591 vg_tex2d_bind( theme
->tex_tiles
, 1 );
2593 glUniform3fv( SHADER_UNIFORM( shader_tile_main
, "uShadowing" ), 1, theme
->col_shadow
);
2595 render_tiles( colour_default
, colour_default
);
2598 // ========================================================================================================
2599 SHADER_USE( shader_ball
);
2600 glUniformMatrix3fv( SHADER_UNIFORM( shader_ball
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
2602 vg_tex2d_bind( &tex_ball_noise
, 0 );
2603 glUniform1i( SHADER_UNIFORM( shader_ball
, "uTexMain" ), 0 );
2605 if( world
.st
.buttons
[ k_world_button_sim
].state
)
2607 for( int i
= 0; i
< world
.num_fishes
; i
++ )
2609 struct fish
*fish
= &world
.fishes
[i
];
2611 render_pos
[2] = 1.0f
;
2613 if( fish
->state
== k_fish_state_dead
|| fish
->state
== k_fish_state_soon_dead
)
2615 float death_anim_time
= world
.sim_internal_time
- fish
->death_time
;
2618 if( death_anim_time
> 0.0f
&& death_anim_time
< 1.0f
)
2620 float amt
= 1.0f
-death_anim_time
*death_anim_time
;
2622 v2_muladds( fish
->physics_co
, fish
->physics_v
, -1.0f
* world
.sim_internal_delta
* amt
, fish
->physics_co
);
2623 render_pos
[2] = amt
;
2625 else if( world
.sim_internal_time
> fish
->death_time
)
2628 else if( fish
->state
== k_fish_state_bg
)
2631 v2_copy( fish
->physics_co
, render_pos
);
2633 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
2634 colour_code_v3( fish
->colour
, dot_colour
);
2636 glUniform3fv( SHADER_UNIFORM( shader_ball
, "uColour" ), 1, dot_colour
);
2637 glUniform3fv( SHADER_UNIFORM( shader_ball
, "uOffset" ), 1, render_pos
);
2638 glUniform2f( SHADER_UNIFORM( shader_ball
, "uTexOffset" ), (float)i
* 1.2334, (float)i
* -0.3579f
);
2643 // TILESET FOREGROUND LAYER
2644 // ========================================================================================================
2645 SHADER_USE( shader_tile_main
);
2648 vg_tex2d_bind( &tex_tile_data
, 0 );
2649 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
2651 // TODO: is this needed to be rebinded?
2652 vg_tex2d_bind( theme
->tex_tiles
, 1 );
2653 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
2655 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uForeground" ), 1.0f
);
2656 render_tiles( colour_default
, colour_selected
);
2659 for( int i
= 0; i
< world
.tile_special_count
; i
++ )
2661 struct render_cmd
*cmd
= &world
.cmd_buf_specials
[i
];
2662 struct cell
*cell
= cmd
->ptr
;
2664 if( cell
->config
== k_cell_type_split
)
2666 float rotation
= cell
->state
& FLAG_FLIP_FLOP
? vg_rad( -45.0f
): vg_rad( 45.0f
);
2668 if( cell
->state
& FLAG_FLIP_ROTATING
)
2670 if( (world
.frame_lerp
> curve_7_linear_section
) )
2672 float const rotation_speed
= 0.4f
;
2673 if( (world
.frame_lerp
< 1.0f
-rotation_speed
) )
2675 float t
= world
.frame_lerp
- curve_7_linear_section
;
2676 t
*= -2.0f
* (1.0f
/(1.0f
-(curve_7_linear_section
+rotation_speed
)));
2686 m2x2_create_rotation( subtransform
, rotation
);
2688 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
2689 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ),
2691 (float)cmd
->pos
[1] + 0.125f
,
2692 cell
->state
& FLAG_TARGETED
? 3.0f
: 2.0f
,
2700 // ========================================================================================================
2701 if( world
.selected
!= -1 && !(world
.data
[ world
.selected
].state
& FLAG_CANAL
) && !world
.id_drag_from
)
2703 v2i new_begin
= { world
.tile_x
- 2, world
.tile_y
- 2 };
2704 v2i new_end
= { world
.tile_x
+ 2, world
.tile_y
+ 2 };
2706 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
2707 map_reclassify( new_begin
, new_end
, 0 );
2709 m2x2_identity( subtransform
);
2710 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 1.0f
);
2711 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
2712 glUniform2fv( SHADER_UNIFORM( shader_tile_main
, "uMousePos" ), 1, world
.tile_pos
);
2714 render_tile_block( new_begin
, new_end
, colour_default
, colour_default
);
2716 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
2717 map_reclassify( new_begin
, new_end
, 0 );
2721 // ========================================================================================================
2722 SHADER_USE( shader_buttons
);
2723 glUniformMatrix3fv( SHADER_UNIFORM( shader_buttons
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
2725 vg_tex2d_bind( &tex_buttons
, 0 );
2726 glUniform1i( SHADER_UNIFORM( shader_buttons
, "uTexMain" ), 0 );
2728 enum world_button_status stat
;
2729 int world_paused
= world
.st
.buttons
[k_world_button_pause
].state
;
2730 int world_running
= world
.st
.buttons
[k_world_button_sim
].state
;
2732 float sim_icon_x
= world_paused
? 3.0f
: (world_running
? 2.0f
: 0.0f
);
2734 v3f btn_dark_blue
= { 0.204f
, 0.345f
, 0.553f
};
2735 v3f btn_orange
= { 0.553f
, 0.345f
, 0.204f
};
2737 if( world_button_exec( &world
.st
.buttons
[k_world_button_sim
], (v2f
){ sim_icon_x
, 3.0f
}, btn_dark_blue
, &stat
))
2739 if( stat
== k_world_button_on_enable
)
2744 world
.pause_offset_target
= 0.5f
;
2750 // Trigger single step
2751 world
.pause_offset_target
+= 1.0f
;
2752 world
.st
.buttons
[k_world_button_sim
].state
= 1;
2761 if( world_button_exec( &world
.st
.buttons
[k_world_button_pause
], (v2f
){ 1.0f
, 3.0f
}, btn_dark_blue
, &stat
))
2763 world
.sim_internal_ref
= world
.sim_internal_time
;
2764 world
.sim_delta_ref
= vg_time
;
2766 if( stat
== k_world_button_on_enable
)
2768 float time_frac
= world
.sim_internal_time
-floorf(world
.sim_internal_time
);
2769 world
.pause_offset_target
= 0.5f
- time_frac
;
2772 world
.pause_offset_target
= 0.0f
;
2775 if( world_button_exec( &world
.st
.buttons
[k_world_button_speedy
], (v2f
){ 0.0f
, 2.0f
}, btn_orange
, &stat
))
2777 world
.sim_delta_speed
= stat
== k_world_button_on_enable
? 10.0f
: 2.5f
;
2781 world
.sim_delta_ref
= vg_time
;
2782 world
.sim_internal_ref
= world
.sim_internal_time
;
2786 if( world_button_exec( &world
.st
.buttons
[k_world_button_settings
], (v2f
){ 1.0f
, 2.0f
}, btn_orange
, &stat
))
2788 world
.st
.state
= stat
== k_world_button_on_enable
? k_game_state_settings
: k_game_state_main
;
2791 level_selection_buttons();
2793 if( vg_get_button_up( "primary" ) )
2794 world_button_exec( NULL
, NULL
, NULL
, NULL
);
2797 // ========================================================================================================
2799 //glEnable(GL_BLEND);
2800 SHADER_USE( shader_tile_colour
);
2801 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
2803 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
2805 struct cell_terminal
*term
= &world
.io
[ i
];
2806 struct cell
*cell
= pcell(term
->pos
);
2808 int is_input
= cell
->state
& FLAG_INPUT
;
2809 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
2811 if( cell
->state
& FLAG_EMITTER
)
2813 for( int j
= 0; j
< 2; j
++ )
2815 if( cell
->emit
[j
] != -1 )
2817 colour_code_v3( cell
->emit
[j
], dot_colour
);
2819 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ),
2820 term
->pos
[0] + 0.25f
+ (float)j
* 0.5f
,
2821 term
->pos
[1] + 0.25f
,
2825 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2826 draw_mesh( filled_start
, filled_count
);
2832 for( int k
= 0; k
< term
->run_count
; k
++ )
2834 float arr_base
= is_input
? 1.2f
: -0.2f
,
2835 run_offset
= (is_input
? 0.2f
: -0.2f
) * (float)k
,
2836 y_position
= is_input
?
2837 (arr_base
+ (float)term
->pos
[1] + (float)(term
->run_count
-1)*0.2f
) - run_offset
:
2838 (float)term
->pos
[1] + arr_base
+ run_offset
;
2843 if( is_simulation_running() )
2845 if( k
== world
.sim_run
)
2847 float a
= fabsf(sinf( vg_time
* 2.0f
)) * 0.075f
+ 0.075f
;
2849 v4_copy( (v4f
){ 1.0f
, 1.0f
, 1.0f
, a
}, bar_colour
);
2852 v4_copy( (v4f
){ 0.0f
, 0.0f
, 0.0f
, 0.13f
}, bar_colour
);
2856 else if( 1 || k
& 0x1 )
2859 v4_copy( (v4f
){ 1.0f
, 1.0f
, 1.0f
, 0.07f
}, bar_colour
);
2861 v4_copy( (v4f
){ 0.0f
, 0.0f
, 0.0f
, 0.13f
}, bar_colour
);
2868 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, bar_colour
);
2869 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ),
2870 (float)term
->pos
[0], y_position
- 0.1f
, 1.0f
);
2875 for( int j
= 0; j
< term
->runs
[k
].step_count
; j
++ )
2877 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ),
2878 (float)term
->pos
[0] + 0.2f
+ 0.2f
* (float)j
,
2885 i8 colour
= term
->runs
[k
].steps
[j
];
2888 colour_code_v3( colour
, dot_colour
);
2889 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2891 // Draw filled if tick not passed, draw empty if empty
2892 if( (world
.sim_frame
> j
&& world
.sim_run
>= k
) || world
.sim_run
> k
)
2893 draw_mesh( empty_start
, empty_count
);
2895 draw_mesh( filled_start
, filled_count
);
2901 if( term
->runs
[k
].recv_count
> j
)
2903 colour_code_v3( term
->runs
[k
].recieved
[j
], dot_colour
);
2904 v3_muls( dot_colour
, 0.8f
, dot_colour
);
2905 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2907 draw_mesh( filled_start
, filled_count
);
2910 colour_code_v3( term
->runs
[k
].steps
[j
], dot_colour
);
2911 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2913 draw_mesh( empty_start
, empty_count
);
2920 // ========================================================================================================
2921 SHADER_USE( shader_sprite
);
2922 glUniformMatrix3fv( SHADER_UNIFORM( shader_sprite
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
2924 vg_tex2d_bind( &tex_sprites
, 0 );
2925 glUniform1i( SHADER_UNIFORM( shader_sprite
, "uTexMain" ), 0 );
2927 for( int i
= 0; i
< world
.tile_special_count
; i
++ )
2929 struct render_cmd
*cmd
= &world
.cmd_buf_specials
[i
];
2930 struct cell
*cell
= cmd
->ptr
;
2932 if( (cell
->config
== k_cell_type_split
) || (cell
->state
& FLAG_EMITTER
) )
2934 v2f center
= { cmd
->pos
[0] + 0.5f
, cmd
->pos
[1] + 0.5f
};
2936 v3f p0
= { 0.0f
, 0.0f
, 4.0f
};
2937 v3f p1
= { 0.0f
, 0.0f
, 4.0f
};
2939 v2_add( center
, (v2f
){ -0.25f
, -0.25f
}, p0
);
2940 v2_add( center
, (v2f
){ 0.25f
, -0.25f
}, p1
);
2942 render_sprite( k_sprite_jack_1
, p0
);
2943 render_sprite( k_sprite_jack_2
, p1
);
2945 else if( cell
->state
& FLAG_IS_TRIGGER
)
2947 v3f p0
= { 0.0f
, 0.0f
, 4.0f
};
2949 struct cell_description
*desc
= &cell_descriptions
[ cell
->config
];
2951 v2_add( (v2f
){ cmd
->pos
[0], cmd
->pos
[1] }, desc
->trigger_pos
, p0
);
2952 render_sprite( desc
->trigger_sprite
, p0
);
2957 // ========================================================================================================
2960 m3x3_identity( mvp_text
);
2961 m3x3_scale( mvp_text
, (v3f
){
2962 1.0f
/ ((float)UI_GLYPH_SPACING_X
*4.0f
),
2963 1.0f
/ -((float)UI_GLYPH_SPACING_X
*4.0f
),
2967 m3x3_mul( vg_pv
, mvp_text
, mvp_text
);
2968 ui_draw( &world
.st
.world_text
, mvp_text
);
2971 // ========================================================================================================
2974 SHADER_USE( shader_wire
);
2975 glBindVertexArray( world
.wire
.vao
);
2977 glUniformMatrix3fv( SHADER_UNIFORM( shader_wire
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
2979 v4f
const wire_left_colour
= { 0.5f
, 0.5f
, 0.5f
, 1.0f
};
2980 v4f
const wire_right_colour
= { 0.2f
, 0.2f
, 0.2f
, 1.0f
};
2981 v4f
const wire_drag_colour
= { 0.2f
, 0.2f
, 0.2f
, 0.6f
};
2983 glUniform1f( SHADER_UNIFORM( shader_wire
, "uTime" ), world
.frame_lerp
);
2984 glUniform1f( SHADER_UNIFORM( shader_wire
, "uGlow" ), 0.0f
);
2986 if( world
.id_drag_from
)
2988 glUniform4fv( SHADER_UNIFORM( shader_wire
, "uColour" ), 1, wire_drag_colour
);
2989 glUniform1f( SHADER_UNIFORM( shader_wire
, "uCurve" ), 0.4f
);
2990 glUniform3f( SHADER_UNIFORM( shader_wire
, "uStart" ), world
.drag_from_co
[0], world
.drag_from_co
[1], 0.20f
*world
.st
.world_transition
);
2991 glUniform3f( SHADER_UNIFORM( shader_wire
, "uEnd" ), world
.drag_to_co
[0], world
.drag_to_co
[1], 0.20f
*world
.st
.world_transition
);
2992 glDrawElements( GL_TRIANGLES
, world
.wire
.em
, GL_UNSIGNED_SHORT
, (void*)(0) );
2995 // Pulling animation
2996 float rp_x1
= world
.frame_lerp
*9.0f
;
2997 float rp_xa
= rp_x1
*expf(1.0f
-rp_x1
)* 0.36f
;
2998 float rp_x2
= 1.0f
-rp_xa
;
3000 for( int i
= 0; i
< world
.tile_special_count
; i
++ )
3002 struct render_cmd
*cmd
= &world
.cmd_buf_specials
[i
];
3003 struct cell
*cell
= cmd
->ptr
;
3005 if( cell
->state
& FLAG_TARGETED
)
3007 for( int j
= 0; j
< 2; j
++ )
3009 if( !cell
->links
[j
] )
3012 struct cell
*other_cell
= &world
.data
[ cell
->links
[ j
]];
3013 struct cell_description
*desc
= &cell_descriptions
[ other_cell
->config
];
3015 int x2
= cell
->links
[j
] % world
.w
;
3016 int y2
= (cell
->links
[j
] - x2
) / world
.w
;
3021 endpoint
[0] = (float)cmd
->pos
[0] + (j
? 0.75f
: 0.25f
);
3022 endpoint
[1] = (float)cmd
->pos
[1] + 0.25f
;
3027 v2_add( desc
->trigger_pos
, startpoint
, startpoint
);
3029 if( cmd
->ptr
->state
& FLAG_EMITTER
)
3032 colour_code_v3( cmd
->ptr
->emit
[j
], wire_colour
);
3033 wire_colour
[3] = 1.0f
;
3035 glUniform4fv( SHADER_UNIFORM( shader_wire
, "uColour" ), 1, wire_colour
);
3038 glUniform4fv( SHADER_UNIFORM( shader_wire
, "uColour" ), 1, j
? wire_right_colour
: wire_left_colour
);
3040 glUniform1f( SHADER_UNIFORM( shader_wire
, "uCurve" ), other_cell
->state
& FLAG_TRIGGERED
? rp_x2
* 0.4f
: 0.4f
);
3041 glUniform1f( SHADER_UNIFORM( shader_wire
, "uGlow" ), other_cell
->state
& FLAG_TRIGGERED
? rp_xa
: 0.0f
);
3042 glUniform3f( SHADER_UNIFORM( shader_wire
, "uEnd" ), startpoint
[0], startpoint
[1], 0.18f
*world
.st
.world_transition
);
3043 glUniform3f( SHADER_UNIFORM( shader_wire
, "uStart" ), endpoint
[0], endpoint
[1], 0.18f
*world
.st
.world_transition
);
3044 glDrawElements( GL_TRIANGLES
, world
.wire
.em
, GL_UNSIGNED_SHORT
, (void*)(0) );
3050 // ========================================================================================================
3052 SHADER_USE( shader_tile_colour
);
3053 use_mesh( &world
.shapes
);
3055 for( int i
= 0; i
< world
.tile_special_count
; i
++ )
3057 struct render_cmd
*cmd
= &world
.cmd_buf_specials
[i
];
3058 struct cell
*cell
= cmd
->ptr
;
3060 if( cell
->state
& FLAG_TARGETED
)
3062 for( int j
= 0; j
< 2; j
++ )
3064 if( !cell
->links
[j
] )
3067 struct cell
*other_cell
= &world
.data
[ cell
->links
[ j
]];
3068 struct cell_description
*desc
= &cell_descriptions
[ other_cell
->config
];
3070 int x2
= cell
->links
[j
] % world
.w
;
3071 int y2
= (cell
->links
[j
] - x2
) / world
.w
;
3075 pts
[0][0] = (float)cmd
->pos
[0] + (j
? 0.75f
: 0.25f
);
3076 pts
[0][1] = (float)cmd
->pos
[1] + 0.25f
;
3081 v2_add( desc
->trigger_pos
, pts
[1], pts
[1] );
3083 if( cell
->state
& FLAG_EMITTER
)
3086 colour_code_v3( cell
->emit
[j
], wire_colour
);
3088 v3_muls( wire_colour
, 0.8f
, wire_colour
);
3089 wire_colour
[3] = 1.0f
;
3091 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, wire_colour
);
3094 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1,j
?wire_right_colour
: wire_left_colour
);
3096 for( int i
= 0; i
< 2; i
++ )
3098 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ),
3101 0.08f
* world
.st
.world_transition
3103 draw_mesh( filled_start
, filled_count
);
3109 // SUB SPLITTER DIRECTION
3110 // ========================================================================================================
3113 glUniform4f( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 0.9f, 0.35f, 0.1f, 0.75f );
3115 for( int i = 0; i < world.tile_special_count; i ++ )
3117 struct render_cmd *cmd = &world.cmd_buf_specials[i];
3118 struct cell *cell = cmd->ptr;
3120 if( cell->state & FLAG_TARGETED && cell->config == k_cell_type_split )
3122 glUniform3f( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), cmd->pos[0], cmd->pos[1], 1.0f );
3123 draw_mesh( cell->state & FLAG_FLIP_FLOP? 5: 4, 1 );
3129 // ========================================================================================================
3130 glBlendFunc(GL_ONE
, GL_ONE
);
3131 glBlendEquation(GL_FUNC_ADD
);
3133 SHADER_USE( shader_sprite
);
3135 vg_tex2d_bind( &tex_sprites
, 0 );
3136 glUniform1i( SHADER_UNIFORM( shader_sprite
, "uTexMain" ), 0 );
3138 for( int i
= 0; i
< world
.tile_special_count
; i
++ )
3140 struct render_cmd
*cmd
= &world
.cmd_buf_specials
[i
];
3141 struct cell
*cell
= cmd
->ptr
;
3143 if( cell
->config
== k_cell_type_split
)
3145 v2f center
= { cmd
->pos
[0] + 0.5f
, cmd
->pos
[1] + 0.5f
};
3147 v3f p0
= { 0.0f
, 0.0f
, 12.0f
};
3148 v3f p1
= { 0.0f
, 0.0f
, 12.0f
};
3150 v2_add( center
, (v2f
){ -0.25f
, -0.25f
}, p0
);
3151 v2_add( center
, (v2f
){ 0.25f
, -0.25f
}, p1
);
3153 if( cell
->state
& FLAG_FLIP_FLOP
)
3154 render_sprite( k_sprite_flare_y
, p1
);
3156 render_sprite( k_sprite_flare_b
, p0
);
3160 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
3161 glBlendEquation(GL_FUNC_ADD
);
3164 glDisable(GL_BLEND
);
3168 float const score_bright = 1.25f;
3169 glUniform4f( SHADER_UNIFORM( shader_tile_colour, "uColour" ),
3170 0.4f*score_bright, 0.39f*score_bright, 0.45f*score_bright, 1.0f );
3172 use_mesh( &world.numbers );
3173 draw_numbers( (v3f){ 2.0f, (float)world.h-1.875f, 0.3333f }, world.score );
3179 // Drawing world name
3180 if( world
.pCmpLevel
)
3182 gui_text( (ui_px
[2]){ vg_window_x
/ 2, 4 }, world
.pCmpLevel
->title
, 2, k_text_align_center
);
3183 gui_text( (ui_px
[2]){ vg_window_x
/ 2, 28 }, world
.pCmpLevel
->description
, 1, k_text_align_center
);
3186 if( world
.st
.state
== k_game_state_settings
)
3190 ui_global_ctx
.cursor
[2] = 225;
3196 gui_capture_mouse( 200 );
3198 gui_fill_rect( ui_global_ctx
.cursor
, 0xC0202020 );
3199 ui_rect_pad( ui_global_ctx
.cursor
, 8 );
3201 ui_global_ctx
.cursor
[3] = 25;
3205 gui_text( ui_global_ctx
.cursor
, "SETTINGS", 2, 0 );
3209 // Colour scheme selection
3210 ui_global_ctx
.cursor
[1] += 30;
3212 gui_text( ui_global_ctx
.cursor
, "Colour Scheme", 1, 0 );
3213 ui_global_ctx
.cursor
[1] += 25;
3217 ui_global_ctx
.cursor
[2] = 50;
3219 for( int i
= 0; i
< 4; i
++ )
3224 u32 rgb
= 0xff000000;
3226 for( int j
= 0; j
< 3; j
++ )
3227 rgb
|= (u32
)(colour_sets
[ colour_set_id
][i
][j
]*255.0f
) << j
* 8;
3229 gui_fill_rect( ui_global_ctx
.cursor
, rgb
);
3238 ui_global_ctx
.cursor
[2] = 25;
3239 if( gui_button( 0 ) == k_button_click
)
3241 if( colour_set_id
> 0 )
3244 gui_text( ui_global_ctx
.cursor
, "<", 1, 0 );
3247 ui_global_ctx
.cursor
[2] = 150;
3250 gui_fill_rect( ui_global_ctx
.cursor
, 0x33ffffff );
3252 (ui_px
[2]){ ui_global_ctx
.cursor
[0] + 75, ui_global_ctx
.cursor
[1] + 6 },
3253 (const char *[]){ "Normal", "Extra1", "Extra2" }[ colour_set_id
],
3254 1, k_text_align_center
3259 ui_global_ctx
.cursor
[2] = 25;
3260 if( gui_button( 1 ) == k_button_click
)
3262 if( colour_set_id
< vg_list_size( colour_sets
)-1 )
3265 gui_text( ui_global_ctx
.cursor
, ">", 1, 0 );
3271 // TODO: remove code dupe
3272 ui_global_ctx
.cursor
[1] += 16;
3274 gui_text( ui_global_ctx
.cursor
, "Tile Theme", 1, 0 );
3275 ui_global_ctx
.cursor
[1] += 20;
3279 ui_global_ctx
.cursor
[2] = 25;
3280 if( gui_button( 0 ) == k_button_click
)
3282 if( world_theme_id
> 0 )
3285 gui_text( ui_global_ctx
.cursor
, "<", 1, 0 );
3288 ui_global_ctx
.cursor
[2] = 150;
3291 gui_fill_rect( ui_global_ctx
.cursor
, 0x33ffffff );
3293 (ui_px
[2]){ ui_global_ctx
.cursor
[0] + 75, ui_global_ctx
.cursor
[1] + 6 },
3294 world_themes
[ world_theme_id
].name
, 1, k_text_align_center
3299 ui_global_ctx
.cursor
[2] = 25;
3300 if( gui_button( 1 ) == k_button_click
)
3302 if( world_theme_id
< vg_list_size( world_themes
)-1 )
3305 gui_text( ui_global_ctx
.cursor
, ">", 1, 0 );
3314 #if STEAM_LEADERBOARDS
3315 void leaderboard_dispatch_score(void)
3318 sw_upload_leaderboard_score(
3319 ui_data
.upload_request
.level
->steam_leaderboard
,
3320 k_ELeaderboardUploadScoreMethodKeepBest
,
3321 ui_data
.upload_request
.score
,
3326 ui_data
.upload_request
.is_waiting
= 0;
3328 vg_success( "Dispatched leaderboard score\n" );
3331 void leaderboard_found( LeaderboardFindResult_t
*pCallback
)
3333 if( !pCallback
->m_bLeaderboardFound
)
3335 vg_error( "Leaderboard could not be found\n" );
3336 ui_data
.steam_leaderboard
= 0;
3340 const char *recieved_name
= sw_get_leaderboard_name( pCallback
->m_hSteamLeaderboard
);
3342 // Update UI state and request entries if this callback found the current UI level
3343 if( ui_data
.level_selected
)
3345 if( !strcmp( recieved_name
, ui_data
.level_selected
->map_name
) )
3347 sw_download_leaderboard_entries( pCallback
->m_hSteamLeaderboard
, k_ELeaderboardDataRequestFriends
, 0, 8 );
3348 ui_data
.level_selected
->steam_leaderboard
= pCallback
->m_hSteamLeaderboard
;
3352 // Dispatch the waiting request if there was one
3353 if( ui_data
.upload_request
.is_waiting
)
3355 if( !strcmp( recieved_name
, ui_data
.upload_request
.level
->map_name
) )
3357 ui_data
.upload_request
.level
->steam_leaderboard
= pCallback
->m_hSteamLeaderboard
;
3358 leaderboard_dispatch_score();
3364 void leaderboard_downloaded( LeaderboardScoresDownloaded_t
*pCallback
)
3366 // Update UI if this leaderboard matches what we currently have in view
3367 if( ui_data
.level_selected
->steam_leaderboard
== pCallback
->m_hSteamLeaderboard
)
3369 vg_info( "Recieved %d entries\n", pCallback
->m_cEntryCount
);
3370 ui_data
.leaderboard_count
= VG_MIN( pCallback
->m_cEntryCount
, 8 );
3372 u64_steamid local_player
= sw_get_steamid();
3374 for( int i
= 0; i
< ui_data
.leaderboard_count
; i
++ )
3376 LeaderboardEntry_t entry
;
3377 sw_get_downloaded_entry( pCallback
->m_hSteamLeaderboardEntries
, i
, &entry
, NULL
, 0 );
3379 struct leaderboard_player
*player
= &ui_data
.leaderboard_players
[i
];
3381 player
->id
= entry
.m_steamIDUser
.m_unAll64Bits
;
3382 strncpy( player
->player_name
, sw_get_friend_persona_name( player
->id
), vg_list_size( player
->player_name
)-1 );
3383 player
->score
= entry
.m_nScore
;
3385 snprintf( player
->score_text
, vg_list_size(player
->score_text
), "%d", player
->score
);
3386 player
->texture
= sw_get_player_image( player
->id
);
3388 if( player
->texture
== 0 )
3389 player
->texture
= tex_unkown
.name
;
3391 player
->is_local_player
= local_player
== player
->id
? 1: 0;
3394 if( ui_data
.leaderboard_count
)
3395 ui_data
.leaderboard_show
= 1;
3397 ui_data
.leaderboard_show
= 0;
3399 else vg_warn( "Downloaded leaderboard does not match requested!\n" );
3402 void leaderboard_set_score( struct cmp_level
*cmp_level
, u32 score
)
3404 if( ui_data
.upload_request
.is_waiting
)
3405 vg_warn( "You are uploading leaderboard entries too quickly!\n" );
3407 ui_data
.upload_request
.level
= cmp_level
;
3408 ui_data
.upload_request
.score
= score
;
3409 ui_data
.upload_request
.is_waiting
= 1;
3411 // If leaderboard ID has been downloaded already then just immediately dispatch this
3412 if( cmp_level
->steam_leaderboard
)
3413 leaderboard_dispatch_score();
3415 sw_find_leaderboard( cmp_level
->map_name
);
3420 // ===========================================================================================================
3422 static int console_credits( int argc
, char const *argv
[] )
3424 vg_info( "Aknowledgements:\n" );
3425 vg_info( " GLFW zlib/libpng glfw.org\n" );
3426 vg_info( " miniaudio MIT0 miniaud.io\n" );
3427 vg_info( " QOI MIT phoboslab.org\n" );
3428 vg_info( " STB library MIT nothings.org\n" );
3429 vg_info( " Ubuntu Regular ubuntu.com\n" );
3433 static int console_save_map( int argc
, char const *argv
[] )
3435 if( !world
.initialzed
)
3437 vg_error( "Tried to save uninitialized map!\n" );
3441 char map_path
[ 256 ];
3443 strcpy( map_path
, "sav/" );
3444 strcat( map_path
, world
.map_name
);
3445 strcat( map_path
, ".map" );
3447 FILE *test_writer
= fopen( map_path
, "wb" );
3450 vg_info( "Saving map to '%s'\n", map_path
);
3451 map_serialize( test_writer
);
3453 fclose( test_writer
);
3458 vg_error( "Unable to open stream for writing\n" );
3463 static int console_load_map( int argc
, char const *argv
[] )
3465 char map_path
[ 256 ];
3470 strcpy( map_path
, "sav/" );
3471 strcat( map_path
, argv
[0] );
3472 strcat( map_path
, ".map" );
3474 char *text_source
= vg_textasset_read( map_path
);
3478 strcpy( map_path
, "maps/" );
3479 strcat( map_path
, argv
[0] );
3480 strcat( map_path
, ".map" );
3482 text_source
= vg_textasset_read( map_path
);
3487 vg_info( "Loading map: '%s'\n", map_path
);
3488 world
.pCmpLevel
= NULL
;
3490 if( !map_load( text_source
, argv
[0] ) )
3492 free( text_source
);
3496 free( text_source
);
3501 vg_error( "Missing maps '%s'\n", argv
[0] );
3507 vg_error( "Missing argument <map_path>\n" );
3512 static int console_changelevel( int argc
, char const *argv
[] )
3516 // Save current level
3517 console_save_map( 0, NULL
);
3519 if( console_load_map( argc
, argv
) )
3521 world
.st
.zoom
= 0.0f
;
3528 vg_error( "Missing argument <map_path>\n" );
3534 // START UP / SHUTDOWN
3535 // ===========================================================================================================
3537 #define TRANSFORM_TRI_2D( S, OX, OY, X1, Y1, X2, Y2, X3, Y3 ) \
3538 X1*S+OX, Y1*S+OY, X2*S+OX, Y2*S+OY, X3*S+OX, Y3*S+OY
3542 // Steamworks callbacks
3543 #ifdef STEAM_LEADERBOARDS
3544 sw_leaderboard_found
= &leaderboard_found
;
3545 sw_leaderboard_downloaded
= &leaderboard_downloaded
;
3548 vg_function_push( (struct vg_cmd
){
3549 .name
= "_map_write",
3550 .function
= console_save_map
3553 vg_function_push( (struct vg_cmd
){
3554 .name
= "_map_load",
3555 .function
= console_load_map
3558 vg_function_push( (struct vg_cmd
){
3560 .function
= console_changelevel
3563 vg_function_push( (struct vg_cmd
){
3565 .function
= console_credits
3568 vg_convar_push( (struct vg_convar
){
3570 .data
= &colour_set_id
,
3571 .data_type
= k_convar_dtype_i32
,
3572 .opt_i32
= { .min
= 0, .max
= 2, .clamp
= 1 },
3576 vg_convar_push( (struct vg_convar
){
3578 .data
= &world_theme_id
,
3579 .data_type
= k_convar_dtype_i32
,
3580 .opt_i32
= { .min
= 0, .max
= vg_list_size( world_themes
)-1, .clamp
= 1 },
3584 // Combined quad, long quad / empty circle / filled circle mesh
3586 float combined_mesh
[6*6 + 32*6*3] = {
3587 0.0f
, 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
,
3588 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
, 0.0f
,
3590 0.0f
, 0.0f
, 0.0f
, 0.2f
, 1.0f
, 0.2f
,
3591 0.0f
, 0.0f
, 1.0f
, 0.2f
, 1.0f
, 0.0f
,
3593 TRANSFORM_TRI_2D( 0.15f
,0.05f
,0.4f
, 0.0f
, 1.0f
, 1.0f
, 2.0f
, 1.0f
, 0.0f
),
3594 TRANSFORM_TRI_2D( 0.15f
,0.80f
,0.4f
, 0.0f
, 0.0f
, 0.0f
, 2.0f
, 1.0f
, 1.0f
)
3597 float *circle_mesh
= combined_mesh
+ 6*6;
3600 for( int i
= 0; i
< res
; i
++ )
3602 v2f v0
= { sinf( ((float)i
/(float)res
)*VG_TAUf
), cosf( ((float)i
/(float)res
)*VG_TAUf
) };
3603 v2f v1
= { sinf( ((float)(i
+1)/(float)res
)*VG_TAUf
), cosf( ((float)(i
+1)/(float)res
)*VG_TAUf
) };
3605 circle_mesh
[ i
*6+0 ] = 0.0f
;
3606 circle_mesh
[ i
*6+1 ] = 0.0f
;
3608 v2_copy( v0
, circle_mesh
+ 32*6 + i
*12 );
3609 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+2 );
3610 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+4 );
3612 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+6 );
3613 v2_muls( v1
, 0.8f
, circle_mesh
+ 32*6 + i
*12+8 );
3614 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+10 );
3616 v2_copy( v0
, circle_mesh
+ i
*6+4 );
3617 v2_copy( v1
, circle_mesh
+ i
*6+2 );
3618 v2_copy( v0
, circle_mesh
+i
*6+4 );
3619 v2_copy( v1
, circle_mesh
+i
*6+2 );
3622 init_mesh( &world
.shapes
, combined_mesh
, vg_list_size( combined_mesh
) );
3627 int const num_segments
= 64;
3629 struct mesh_wire
*mw
= &world
.wire
;
3631 v2f wire_points
[ num_segments
* 2 ];
3632 u16 wire_indices
[ 6*(num_segments
-1) ];
3634 for( int i
= 0; i
< num_segments
; i
++ )
3636 float l
= (float)i
/ (float)(num_segments
-1);
3638 v2_copy( (v2f
){ l
, -0.5f
}, wire_points
[i
*2+0] );
3639 v2_copy( (v2f
){ l
, 0.5f
}, wire_points
[i
*2+1] );
3641 if( i
< num_segments
-1 )
3643 wire_indices
[ i
*6+0 ] = i
*2 + 0;
3644 wire_indices
[ i
*6+1 ] = i
*2 + 1;
3645 wire_indices
[ i
*6+2 ] = i
*2 + 3;
3646 wire_indices
[ i
*6+3 ] = i
*2 + 0;
3647 wire_indices
[ i
*6+4 ] = i
*2 + 3;
3648 wire_indices
[ i
*6+5 ] = i
*2 + 2;
3652 glGenVertexArrays( 1, &mw
->vao
);
3653 glGenBuffers( 1, &mw
->vbo
);
3654 glGenBuffers( 1, &mw
->ebo
);
3655 glBindVertexArray( mw
->vao
);
3657 glBindBuffer( GL_ARRAY_BUFFER
, mw
->vbo
);
3659 glBufferData( GL_ARRAY_BUFFER
, sizeof( wire_points
), wire_points
, GL_STATIC_DRAW
);
3660 glBindVertexArray( mw
->vao
);
3662 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, mw
->ebo
);
3663 glBufferData( GL_ELEMENT_ARRAY_BUFFER
, sizeof( wire_indices
), wire_indices
, GL_STATIC_DRAW
);
3666 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2*sizeof(float), (void*)0 );
3667 glEnableVertexAttribArray( 0 );
3671 mw
->em
= vg_list_size( wire_indices
);
3674 // Create info data texture
3676 glGenTextures( 1, &world
.background_data
);
3677 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
3678 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RGBA
, 64, 64, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
3682 // Create random smaples texture
3684 u8
*data
= malloc(512*512*2);
3685 for( int i
= 0; i
< 512*512*2; i
++ )
3686 data
[ i
] = rand()/(RAND_MAX
/255);
3688 glGenTextures( 1, &world
.random_samples
);
3689 glBindTexture( GL_TEXTURE_2D
, world
.random_samples
);
3690 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RG
, 512, 512, 0, GL_RG
, GL_UNSIGNED_BYTE
, data
);
3697 resource_load_main();
3701 ui_init_context( &world
.st
.world_text
, 15000 );
3704 // Restore gamestate
3705 career_local_data_init();
3715 console_save_map( 0, NULL
);
3718 resource_free_main();
3720 glDeleteTextures( 1, &world
.background_data
);
3721 glDeleteTextures( 1, &world
.random_samples
);
3723 glDeleteVertexArrays( 1, &world
.wire
.vao
);
3724 glDeleteBuffers( 1, &world
.wire
.vbo
);
3725 glDeleteBuffers( 1, &world
.wire
.ebo
);
3727 free_mesh( &world
.shapes
);
3729 ui_context_free( &world
.st
.world_text
);
3734 int main( int argc
, char *argv
[] )
3736 vg_init( argc
, argv
, "Marble Computing" );