/* keys_demo1 ARM9 Code Chris Double (chris.double@double.co.nz) http://www.double.co.nz/nintendo_ds */ #include static int old_x = 0; static int old_y = 0; static int shape_x = 0; static int shape_y = 0; static int shape_width = 10; static int shape_height = 10; static uint16 shape_color = RGB15(31, 0, 0); void draw_shape(int x, int y, uint16* buffer, uint16 color) { buffer += y * SCREEN_WIDTH + x; for(int i = 0; i < shape_height; ++i) { uint16* line = buffer + (SCREEN_WIDTH * i); for(int j = 0; j < shape_width; ++j) { *line++ = color; } } } void on_irq() { if(REG_IF & IRQ_VBLANK) { draw_shape(old_x, old_y, VRAM_A, RGB15(0, 0, 0)); draw_shape(shape_x, shape_y, VRAM_A, shape_color); // Tell the DS we handled the VBLANK interrupt VBLANK_INTR_WAIT_FLAGS |= IRQ_VBLANK; REG_IF |= IRQ_VBLANK; } else { // Ignore all other interrupts REG_IF = REG_IF; } } void InitInterruptHandler() { REG_IME = 0; IRQ_HANDLER = on_irq; REG_IE = IRQ_VBLANK; REG_IF = ~0; DISP_SR = DISP_VBLANK_IRQ; REG_IME = 1; } int main(void) { powerON(POWER_ALL); videoSetMode(MODE_FB0); vramSetBankA(VRAM_A_LCD); InitInterruptHandler(); while(1) { old_x = shape_x; old_y = shape_y; // Read the register containing the data of which // keys have been pressed. uint16 keysPressed = ~(REG_KEYINPUT); uint16 specialKeysPressed = ~IPC->buttons; // Based on the key pressed, move the shape. if(keysPressed & KEY_UP) --shape_y; if(keysPressed & KEY_DOWN) ++shape_y; if(keysPressed & KEY_LEFT) --shape_x; if(keysPressed & KEY_RIGHT) ++shape_x; // Change the color of the shape if the relevant key was pressed. if(keysPressed & KEY_A) shape_color = RGB15(31, 0, 0); if(keysPressed & KEY_B) shape_color = RGB15(0, 31, 0); if(keysPressed & KEY_SELECT) shape_color = RGB15(0, 0, 31); if(keysPressed & KEY_START) shape_color = RGB15(31, 31, 31); if(keysPressed & KEY_R) shape_color = RGB15(15, 0, 15); if(keysPressed & KEY_L) shape_color = RGB15(7, 15, 7); // Y Key if(specialKeysPressed & (1 << 1)) shape_color = RGB15(7, 7, 7); // X Key if(specialKeysPressed & (1 << 0)) shape_color = RGB15(0, 15, 15); // Pen Down if(specialKeysPressed & (1 << 6)) shape_color = RGB15(0, 31, 31); // Hinge closed if(!(specialKeysPressed & (1 << 7))) shape_color = RGB15(0, 0, 0); // Make sure the shape stays within the correct bounds of the // screen. if(shape_x + shape_width >= SCREEN_WIDTH) shape_x = 0; else if(shape_x < 0) shape_x = SCREEN_WIDTH - shape_width - 1; if(shape_y + shape_height >= SCREEN_HEIGHT) shape_y = 0; else if(shape_y < 0) shape_y = SCREEN_HEIGHT - shape_height - 1; swiWaitForVBlank(); } return 0; }