/* Our first GBA game. No downloaded game art or existing ROM is used. */ #include "game.h" #include "audio.h" #ifndef GAME_TEST #define DISPLAY (*(volatile u16 *)0x04000000) #define VCOUNT (*(volatile u16 *)0x04000006) #define KEYS (*(volatile u16 *)0x04000130) static void wait_for_frame(void) { while (VCOUNT >= 160) { } while (VCOUNT < 160) { } } /* Timers 2+3 form a cascaded 32-bit clock at 16,777,216 / 1024 Hz. */ static unsigned movement_time(void) { volatile u16 *low = (volatile u16 *)0x04000108; volatile u16 *high = (volatile u16 *)0x0400010c; u16 upper, lower; do { upper = *high; lower = *low; } while (upper != *high); return ((unsigned)upper << 16) | lower; } static void start_movement_timer(void) { volatile u16 *timer2 = (volatile u16 *)0x04000108; volatile u16 *timer3 = (volatile u16 *)0x0400010c; timer2[1] = timer3[1] = 0; timer2[0] = timer3[0] = 0; timer3[1] = 0x0084; /* Enable cascade from timer 2. */ timer2[1] = 0x0083; /* Enable, prescaler 1024; no IRQ required. */ pause_movement_at(movement_time()); } struct DmaChannel { const void *source; volatile void *destination; unsigned control; }; static void present_frame(void) { /* Small DMA transfers fit in VBlank; full map changes use forced blank. */ if (!(DISPLAY & 0x0080)) wait_for_frame(); volatile struct DmaChannel *dma = (volatile struct DmaChannel *)0x040000d4; volatile u16 *video = (volatile u16 *)0x06000000; __asm__ volatile ("" ::: "memory"); for (int y = 0; y < HEIGHT; ++y) { int first = dirty_first[y], end = dirty_end[y]; if (first >= end) continue; dma->source = &SCREEN[y * WIDTH + first]; dma->destination = &video[y * WIDTH + first]; dma->control = 0x80000000u | (unsigned)(end - first); } reset_dirty_rows(); } int main(void) { /* Mode 3 gives us a 240x160 canvas of 15-bit colors. */ DISPLAY = 0x0080; /* Hide the screen until the scene is drawn. */ int x = 108, y = 103, frame = 0, greeting = 0; int dialogue_page = -1; /* -1 means we are not talking. */ int team_open = 0; int team_member = 0; unsigned previous = 0; start_movement_timer(); music_init(movement_time()); load_progress(); reset_follower(x, y); reset_dirty_rows(); draw_scene(x, y); present_frame(); DISPLAY = 0x0403; pause_movement_at(movement_time()); for (;;) { unsigned keys = (~KEYS) & 0x03ff; unsigned pressed = keys & ~previous; previous = keys; unsigned now = movement_time(); music_update(now); if (pressed & R_BUTTON) music_toggle_mute(); int menu_was_open = save_menu.phase != SAVE_MENU_CLOSED; int menu_changed = save_menu_input(keys, !team_open && battle.phase == BATTLE_OFF && dialogue_page < 0); if (menu_was_open || save_menu.phase != SAVE_MENU_CLOSED) { pause_movement_at(now); if (menu_changed) { if (save_menu.phase == SAVE_MENU_CLOSED) { DISPLAY = 0x0080; draw_scene(x, y); present_frame(); DISPLAY = 0x0403; } else { draw_save_menu(); present_frame(); } pause_movement_at(movement_time()); } else present_frame(); continue; } int movement_ticks = 0; if (team_open || battle.phase != BATTLE_OFF || dialogue_page >= 0) pause_movement_at(now); else movement_ticks = movement_ticks_at(now); random_state += 0x9e3779b9u; /* Mix in how long the player waits. */ if (team_open) { if (partner_owned && (pressed & (LEFT | RIGHT))) { int members = caught_count + 1; team_member = (team_member + ((pressed & RIGHT) ? 1 : members - 1)) % members; DISPLAY = 0x0080; draw_team_member(team_member); present_frame(); DISPLAY = 0x0403; pause_movement_at(movement_time()); } else if (pressed & (SELECT | A | B)) { if ((pressed & A) && choose_partner(team_member)) reset_follower(x, y); team_open = 0; DISPLAY = 0x0080; draw_scene(x, y); present_frame(); DISPLAY = 0x0403; pause_movement_at(movement_time()); } else { present_frame(); } continue; } if (battle.phase != BATTLE_OFF) { if (battle_input(pressed)) { if (battle.phase == BATTLE_OFF) { greeting = 180; DISPLAY = 0x0080; draw_scene(x, y); status("YOUR PARTNER RESTED AND IS READY AGAIN"); present_frame(); DISPLAY = 0x0403; pause_movement_at(movement_time()); } else { draw_battle_hud(); present_frame(); } } else { present_frame(); } continue; } /* Talking pauses walking. One fresh button press turns one page. */ if (dialogue_page >= 0) { if (pressed & A) { int already_owned = partner_owned; int already_given = balls_given; dialogue_page = advance_conversation(dialogue_page); if (!already_owned && partner_owned) { reset_follower(x, y); status("SPROUT JOINED YOUR TEAM"); } if (!already_given && balls_given) status("FIVE POKE BALLS ADDED TO YOUR BAG"); if (dialogue_page >= 0) { dialogue_box(dialogue_page); } else { restore(DIALOGUE_X, DIALOGUE_Y, DIALOGUE_W, DIALOGUE_H); actors(x, y, 0); location_status(x, y); } } present_frame(); continue; } if (pressed & SELECT) { team_open = 1; team_member = 0; DISPLAY = 0x0080; draw_team(); present_frame(); DISPLAY = 0x0403; pause_movement_at(movement_time()); continue; } int old_x = x, old_y = y; int old_follower_x = follower_x, old_follower_y = follower_y; enum Facing old_facing = player_facing; enum WalkResult walk = WALK_NORMAL; walk = walk_movement(keys, &x, &y, movement_ticks); if (walk == WALK_MAP_CHANGED) { greeting = 0; dialogue_page = -1; DISPLAY = 0x0080; draw_scene(x, y); present_frame(); DISPLAY = 0x0403; pause_movement_at(movement_time()); continue; } if (walk == WALK_ENCOUNTER) { greeting = 0; DISPLAY = 0x0080; draw_battle(); present_frame(); DISPLAY = 0x0403; pause_movement_at(movement_time()); continue; } if (pressed & A) { if (near_door(x, y)) { if (!indoors()) { door_open = 1; restore(old_x - 8, old_y - 20, 17, 24); if (partner_owned && follower_ready) restore(old_follower_x - 8, old_follower_y - 20, 17, 24); restore(170, 54, 15, 24); actors(x, y, 0); present_frame(); for (int tick = 0; tick < 12; ++tick) { wait_for_frame(); music_update(movement_time()); } } use_door(&x, &y); greeting = 0; DISPLAY = 0x0080; draw_scene(x, y); present_frame(); DISPLAY = 0x0403; pause_movement_at(movement_time()); continue; } else if (near_npc(x, y)) { begin_conversation(x, y); dialogue_page = 0; greeting = 0; status(current_room == ROUTE ? "TALKING TO RANGER FINN" : current_room == BEACH ? "TALKING TO FISHER CORAL" : current_room == MELLON_HOUSE ? "TALKING TO PROFESSOR MELLON" : "TALKING TO MILA"); } else if (current_room == ROUTE) { status(near_route_sign(x, y) ? "GREENLEAF VILLAGE IS TO THE NORTH" : "EXPLORE THE TRAIL OR HEAD NORTH TO RETURN"); greeting = 180; } else if (indoors()) { status("THE EXIT IS AT THE BOTTOM OF THE ROOM"); greeting = 180; } else if (current_room == MELLON_GARDEN) { status("PROFESSOR MELLON IS INSIDE THE HOUSE"); greeting = 180; } else if (current_room == BEACH) { if (cast_rod(x, y)) { greeting = 0; DISPLAY = 0x0080; draw_battle(); present_frame(); DISPLAY = 0x0403; pause_movement_at(movement_time()); continue; } status(!rod_owned ? "TALK TO FISHER CORAL FOR A POKE ROD" : !partner_owned ? "GET SPROUT FROM PROFESSOR MELLON FIRST" : "FACE DOWN AT THE WATER AND PRESS A"); greeting = 180; } else if (begin_battle(x, y)) { greeting = 0; DISPLAY = 0x0080; draw_battle(); present_frame(); DISPLAY = 0x0403; pause_movement_at(movement_time()); continue; } else if (near_golden(x, y)) { status("HELLO NEW FRIEND"); greeting = 180; } else { status("WALK CLOSE TO MILA TO TALK"); greeting = 180; } } if (greeting) { greeting = greeting > movement_ticks ? greeting - movement_ticks : 0; if (!greeting) location_status(x, y); } if (x != old_x || y != old_y || player_facing != old_facing || (partner_owned && (old_follower_x != follower_x || old_follower_y != follower_y))) { restore(old_x - 8, old_y - 20, 17, 24); if (partner_owned && follower_ready) restore(old_follower_x - 8, old_follower_y - 20, 17, 24); actors(x, y, (frame >> 3) & 1); if (!greeting && (near_door(old_x, old_y) != near_door(x, y) || near_map_exit(old_x, old_y) != near_map_exit(x, y) || near_route_sign(old_x, old_y) != near_route_sign(x, y) || near_npc(old_x, old_y) != near_npc(x, y) || near_water(old_x, old_y) != near_water(x, y) || near_golden(old_x, old_y) != near_golden(x, y))) location_status(x, y); } if (dialogue_page >= 0) dialogue_box(dialogue_page); present_frame(); frame = (frame + movement_ticks) & 1023; } } #endif