Fix game mode

This commit is contained in:
hkz 2025-10-13 19:36:20 +02:00
commit 735513e5c8
4 changed files with 41 additions and 34 deletions

View file

@ -39,10 +39,13 @@ static state_page_data* state_page = (state_page_data*)STATE_PAGE;
static shared_page_data *shared_page = (shared_page_data*)SHARED_PAGE;
static uint8_t text_buf[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
__attribute__((interrupt)) void irq_handler(void);
void main(void) {
uint16_t moves_count = 0;
uint16_t score = 0;
int8_t done = 0;
uint8_t read_key;
__disable_interrupts(); // Make sure the interrupts are disabled
@ -59,7 +62,7 @@ void main(void) {
vdp_switch_nt(0); // Make sure VDP shows the gamegrid
// Setup the IRQ handler
POKEW(IRQ_HANDLER_ADDRESS, (uint16_t)vdp_irq_handler);
POKEW(IRQ_HANDLER_ADDRESS, (uint16_t)irq_handler);
// Reset the game, calculate the initial score depending on which tiles we randomly get
score = reset_game();
@ -93,66 +96,53 @@ void main(void) {
vdp_redraw_tiles(get_front_grid());
//__enable_interrupts();
__enable_interrupts();
while(1) { // Game loop
lfsr_update();
__disable_interrupts();
switch(read_kb()) {
case K_UP:
__disable_interrupts();
SND_TAP();
vdp_draw_joystick(JS_POS_UP);
done = step_game(GAME_STEP_UP);
//__enable_interrupts();
break;
case K_DOWN:
__disable_interrupts();
SND_TAP();
vdp_draw_joystick(JS_POS_DOWN);
done = step_game(GAME_STEP_DOWN);
//__enable_interrupts();
break;
case K_LEFT:
__disable_interrupts();
SND_TAP();
vdp_draw_joystick(JS_POS_LEFT);
done = step_game(GAME_STEP_LEFT);
//__enable_interrupts();
break;
case K_RIGHT:
__disable_interrupts();
SND_TAP();
vdp_draw_joystick(JS_POS_RIGHT);
done = step_game(GAME_STEP_RIGHT);
//__enable_interrupts();
break;
case K_CTRL_R:
__disable_interrupts();
snd_mod_button();
//__enable_interrupts();
score = 0; // We'll reset the score
done = -1;
break;
case K_CTRL_S: // The following two will return early
__disable_interrupts();
snd_mod_button();
//__enable_interrupts();
memcpy((void*)(state_page->save_grid), get_front_grid(), GRID_SIDE * GRID_SIDE);
state_page->saved_moves_count = moves_count;
shared_page->master_command = MASTER_COMMAND_SAVE;
case K_CTRL_L:
__disable_interrupts();
snd_mod_button();
//__enable_interrupts();
shared_page->next_module_idx = MODULE_GAME;
gad->mode = GAME_MODE_LOAD;
__disable_interrupts();
return;
default:
__enable_interrupts();
continue; // Do nothing, loop again
}
@ -161,9 +151,7 @@ void main(void) {
num_to_decbuf(moves_count, 5, text_buf); // Moves count
decbuf_to_ascii(5, text_buf);
__disable_interrupts();
vdp_print_string(0, MOVES_TEXT_X, MOVES_TEXT_Y, (char*)text_buf);
//__enable_interrupts();
// If we have won, or we got a reset request, break out of this loop
if(done) {
@ -171,7 +159,6 @@ void main(void) {
num_to_decbuf(score, 5, text_buf); // Score
decbuf_to_ascii(5, text_buf);
__disable_interrupts();
vdp_print_string(0, SCORE_TEXT_X, SCORE_TEXT_Y, (char*)text_buf);
break;
@ -189,11 +176,11 @@ void main(void) {
// Draw the score
num_to_decbuf(score, 5, text_buf); // Score
decbuf_to_ascii(5, text_buf);
__disable_interrupts();
vdp_print_string(0, SCORE_TEXT_X, SCORE_TEXT_Y, (char*)text_buf);
vdp_redraw_tiles(get_front_grid());
vdp_draw_joystick(JS_POS_CENTER);
//__enable_interrupts();
__enable_interrupts();
}
dld->mode = (done > 0) ? DLOG_MODE_WIN : DLOG_MODE_LOSE;
@ -207,3 +194,8 @@ void main(void) {
return;
}
__attribute__((interrupt)) void irq_handler(void) {
vdp_write_interleaved_sat();
}