/* * square1.c * this program attempts to draw a square in a loop */ /* the width and height of the screen */ #define WIDTH 240 #define HEIGHT 160 /* these identifiers define different bit positions of the display control */ #define MODE3 0x0003 #define BG2 0x0400 /* the screen is simply a pointer into memory at a specific address this * * pointer points to 16-bit colors of which there are 240x160 */ volatile unsigned short* screen = (volatile unsigned short*) 0x6000000; /* the display control pointer points to the gba graphics register */ volatile unsigned long* display_control = (volatile unsigned long*) 0x4000000; /* compute a 16-bit integer color based on the three components */ unsigned short make_color(unsigned char r, unsigned char g, unsigned char b) { unsigned short color = (b & 0x1f) << 10; color |= (g & 0x1f) << 5; color |= (r & 0x1f); return color; } /* a colored square */ struct square { unsigned short x, y, size, color; }; /* place a pixel of a given color on the screen */ void put_pixel(int row, int col, unsigned short color) { /* set the screen location to this color */ screen[row * WIDTH + col] = color; } /* draw a square onto the screen */ void draw_square(struct square* s) { /* for each row */ for (unsigned short row = s->y; row < (s->y + s->size); row++) { /* for each column */ for (unsigned short col = s->x; col < (s->x + s->size); col++) { put_pixel(row, col, s->color); } } } /* clear the screen to black */ void clear_screen() { /* set each pixel black */ for (unsigned short row = 0; row < HEIGHT; row++) { for (unsigned short col = 0; col < WIDTH; col++) { put_pixel(row, col, 0); } } } /* the main function */ int main() { /* we set the mode to mode 3 with background 2 on */ *display_control = MODE3 | BG2; /* make a green square */ struct square s = {10, 10, 15, make_color(0, 20, 2)}; /* loop forever */ while (1) { /* clear the screen */ clear_screen(); /* draw the square */ draw_square(&s); } }