/* * square.c * this program lets you move the square, with vsync */ #include "gba.h" /* a colored square */ struct square { unsigned short x, y, size; unsigned char color; }; /* draw a square onto the screen */ void draw_square(volatile unsigned short* buffer, struct square* s) { short row, col; /* for each row of the square */ for (row = s->y; row < (s->y + s->size); row++) { /* loop through each column of the square */ for (col = s->x; col < (s->x + s->size); col++) { /* set the screen location to this color */ put_pixel(buffer, row, col, s->color); } } } /* clear the screen right around the square */ void update_screen(volatile unsigned short* buffer, unsigned short color, struct square* s) { short row, col; for (row = s->y - 3; row < (s->y + s->size + 3); row++) { for (col = s->x - 3; col < (s->x + s->size + 3); col++) { put_pixel(buffer, row, col, color); } } } /* handle the buttons which are pressed down */ void handle_buttons(struct square* s) { /* move the square with the arrow keys */ if (button_pressed(BUTTON_DOWN)) { s->y += 1; } if (button_pressed(BUTTON_UP)) { s->y -= 1; } if (button_pressed(BUTTON_RIGHT)) { s->x += 1; } if (button_pressed(BUTTON_LEFT)) { s->x -= 1; } } /* the main function */ int main() { /* we set the mode to mode 4 with bg2 on */ *display_control = MODE4 | BG2; /* make a green square */ struct square s = {10, 10, 15, add_color(0, 20, 2)}; /* add black to the palette */ unsigned char black = add_color(0, 0, 0); /* the buffer we start with */ volatile unsigned short* buffer = front_buffer; /* clear whole screen first */ clear_screen(front_buffer, black); clear_screen(back_buffer, black); /* loop forever */ while (1) { /* clear the screen - only the areas around the square! */ update_screen(buffer, black, &s); /* draw the square */ draw_square(buffer, &s); /* handle button input */ handle_buttons(&s); /* wiat for vblank before switching buffers */ wait_vblank(); /* swap the buffers */ buffer = flip_buffers(buffer); } }