#include #include "common.h" #include "gba-buttons.c" #include "background-tiles.c" #include "sprites.h" #include "text.h" // // Main functions. // // TODO: Cleanup OBJ/OAM memory copying and access. // // // BIOS calls // int hblank_counter = 0; void irs_hblank_func() { hblank_counter++; if (DISP_VCOUNT >= 160) { PAL_BUFFER_BG[0] = rgb15(0, 0, 0); } else { u16 clr = (DISP_VCOUNT / 8); PAL_BUFFER_BG[0] = rgb15(clr, 0, 31 - clr); } } int main(void) { // Configure the display in mode 0 to show OBJs, where tile memory is // sequential. DISP_CTRL = DISP_ENABLE_SPRITES | DISP_MODE_0 | DISP_BG_0; // Initialize sprite button overlay. init_sprite_pal(0, COLOR_WHITE); init_sprites(0); init_button_sprites(); // Initialize text engine. txt_init(0, COLOR_RED, 0); // Register interrupts. irq_init(); irs_set(IRQ_VBLANK, irs_stub); irs_set(IRQ_HBLANK, irs_hblank_func); // turn sound on SOUND_STATUS = SOUND_ENABLE; SOUND_DMG_MASTER = sound_volume(SOUND_SQUARE1 | SOUND_SQUARE2, 7); SOUND_DSOUND_MASTER = SOUND_DMG100; char *note_names[] = { "C" , "C#" , "D" , "D#" , "E" , "F" , "F#" , "G" , "G#" , "A" , "A#" , "B" }; int frame_counter = 0; Note active_note = NOTE_C; u8 octave = 0; bool playing = false; bool new_note = false; u8 interval = 1; while(true) { bios_vblank_wait(); poll_keys(); txt_position(0, 1); txt_clear_line(); if (key_pressed(KEY_B)) { active_note = NOTE_C; new_note = true; octave = 0; } else if (key_pressed(KEY_A)) { active_note = NOTE_D; new_note = true; octave = 0; } else if (key_pressed(KEY_LEFT)) { active_note = NOTE_E; new_note = true; octave = 0; } else if (key_pressed(KEY_DOWN)) { active_note = NOTE_F; new_note = true; octave = 0; } else if (key_pressed(KEY_RIGHT)) { active_note = NOTE_G; new_note = true; octave = 0; } else if (key_pressed(KEY_UP)) { active_note = NOTE_A; new_note = true; octave = 0; } else if (key_pressed(KEY_L)) { active_note = NOTE_B; new_note = true; octave = 0; } else if (key_pressed(KEY_R)) { active_note = NOTE_C; new_note = true; octave = 1; } if (key_pressed(KEY_START)) { interval++; } if (key_pressed(KEY_SELECT)) { interval--; } if (key_curr > 0) { txt_printf(" Playing: %s + %s\n", note_names[active_note], (note_names[(active_note + interval) % 12])); playing = true; } else { playing = false; } if (playing) { if (new_note) { SOUND_SQUARE1_CTRL = SOUND_SQUARE_ENV_VOL(4) | SOUND_SQUARE_ENV_TIME(0); SOUND_SQUARE2_CTRL = SOUND_SQUARE_ENV_VOL(4) | SOUND_SQUARE_ENV_TIME(0); SOUND_SQUARE1_FREQ = SOUND_SQUARE_RESET | sound_rate(active_note, octave); SOUND_SQUARE2_FREQ = SOUND_SQUARE_RESET | sound_rate((active_note + interval) % 12, octave); } new_note = false; } else { SOUND_SQUARE1_CTRL = SOUND_SQUARE_ENV_VOL(0) | SOUND_SQUARE_ENV_TIME(0); SOUND_SQUARE2_CTRL = SOUND_SQUARE_ENV_VOL(0) | SOUND_SQUARE_ENV_TIME(0); SOUND_SQUARE1_FREQ = 0; SOUND_SQUARE2_FREQ = 0; } frame_counter++; update_button_sprites(); }; return 0; }