// Tetris Game for the Commodore 64
// The tetris game tries to match NES tetris gameplay pretty closely
// Source: https://meatfighter.com/nintendotetrisai/

import "c64"
import "keyboard"
import "sid"
import "tetris-data"
import "tetris-render"
import "tetris-sprites"
import "tetris-play"

void main() {
	sid_rnd_init();
	asm { sei }
	render_init();
	sprites_init();
	sprites_irq_init();
	play_init();
	// Spawn twice to spawn both current & next
	play_spawn_current();
	play_spawn_current();
	render_playfield();
	render_moving();
	render_next();
	while(true) {
		// Wait for a frame to pass
		while(*RASTER!=$ff) {}
		//*BORDERCOL = render_screen_show/$10;
		// Update D018 to show the selected screen
        render_show();
		// Scan keyboard events
		keyboard_event_scan();
		byte key_event = keyboard_event_get();
		byte render = 0;
		if(game_over==0) {
		    render = play_movement(key_event);
		} else {
		    while(true) {
		        (*BORDERCOL)++;
		    }
		}
		if(render!=0) {
			render_playfield();
			render_moving();
			render_next();
			render_score();
			render_screen_swap();
		}
		//*BORDERCOL = 0;
	}
}
