#include "shorthand.h" #include "bd-font.c" // // Memory sections. // // Defines for the different memory sections in the GBA. #define MEM_SROM 0x00000000 #define MEM_EW 0x02000000 #define MEM_IW 0x03000000 #define MEM_IO 0x04000000 #define MEM_PAL 0x05000000 #define MEM_VRAM 0x06000000 #define MEM_OAM 0x07000000 #define MEM_PAK 0x08000000 #define MEM_CART 0x0E000000 // // Display modes. // // Display registers. #define DISP_CONTROL *((vu32*)(MEM_IO + 0x0000)) #define DISP_STATUS *((vu32*)(MEM_IO + 0x0004)) #define DISP_VCOUNT *((vu32*)(MEM_IO + 0x0006)) // Display modes. #define DISP_MODE_0 0x0000 #define DISP_MODE_1 0x0001 #define DISP_MODE_2 0x0002 #define DISP_MODE_3 0x0003 #define DISP_MODE_4 0x0004 #define DISP_MODE_5 0x0005 // Layers. #define DISP_BG0 0x0100 #define DISP_BG1 0x0200 #define DISP_BG2 0x0400 #define DISP_BG3 0x0800 #define DISP_OBJ 0x1000 static inline void set_display_mode(u16 value) { *((vu32*)(MEM_IO + 0x0000)) = value; } // Screen settings. #define SCREEN_WIDTH 240 #define SCREEN_HEIGHT 160 // The GBA in mode 3 expects rbg15 colors in the VRAM, where each component // (RGB) have a 0--31 range. For example, pure red would be rgb15(31, 0, 0). typedef u16 Color; // We can treat the screen as a HxW matrix. With the following macro we can // write a pixel to the screen at the (x, y) position using: // // FRAMEBUFFER[y][x] = color; // typedef Color Scanline[SCREEN_WIDTH]; #define FRAMEBUFFER ((Scanline*)MEM_VRAM) #define SCREEN_BUFFER ((vu16*) MEM_VRAM) // // Colors. // static inline Color rgb15(u32 red, u32 green, u32 blue ) { return (blue << 10) | (green << 5) | red; } #define COLOR_RED rgb15(31, 0, 12) #define COLOR_BLUE rgb15(2, 15, 30) #define COLOR_CYAN rgb15(0, 30, 30) #define COLOR_GREY rgb15(10, 10, 10) #define COLOR_BLACK rgb15(0, 0, 0) #define COLOR_WHITE rgb15(28, 28, 28) // Using bd-font, an 8x8 bitmap font. static inline void put_char(int x, int y, Color clr, u8 chr) { for (size_t i = 0; i < 8; ++i) { for (size_t j = 0; j < 8; ++j) { if ((font[chr][i] >> (7 - j)) & 0x1) { FRAMEBUFFER[y + i][x + j] = clr; } } } } static inline void put_text(int x, int y, Color clr, char *msg) { int count = 0; while (*msg) { put_char(x + count, y, clr, *msg++); count += 8; } } // Draws a line with the given color between (x0,y0) and (x1,y1) using the // Bresenham's line drawing algorithm using exclusively integer arithmetic. static inline void draw_line(int x0, int y0, int x1, int y1, Color clr) { // Keep track of the coordinate for writing to the memory buffer. int x = x0; int y = y0; // Adjust the step direction and calculate deltas. int x_step = 1; int y_step = 1; int dx = x1 - x0; int dy = y1 - y0; if (x0 > x1) { x_step = -1; dx = x0 - x1; } if (y0 > y1) { y_step = -1; dy = y0 - y1; } // Precalculate 2 * deltas for x and y. int ddx = dx + dx; int ddy = dy + dy; // These variables are dependant on the slope. We can avoid considering // separate cases for positive and negative slopes by using pointers to // update the step in x or y. int diff; int diff_inc_a; int diff_inc_b; int n_steps; int *a; int *b; int a_step; int b_step; if (dx >= dy) { diff = ddy - dx; diff_inc_a = ddy; diff_inc_b = ddx; n_steps = dx; a = &x; b = &y; a_step = x_step; b_step = y_step; } else { diff = ddx - dy; diff_inc_a = ddx; diff_inc_b = ddy; n_steps = dy; a = &y; b = &x; a_step = y_step; b_step = x_step; } // Draw the line with Bresenham's algorithm. for (size_t i = 0; i <= n_steps; ++i) { FRAMEBUFFER[y][x] = clr; *a += a_step; diff += diff_inc_a; if (diff > 0) { *b += b_step; diff -= diff_inc_b; } } } static inline void draw_rect(int x0, int y0, int x1, int y1, Color clr) { if (x0 > x1) { int tmp = x0; x0 = x1; x1 = tmp; } if (y0 > y1) { int tmp = y0; y0 = y1; y1 = tmp; } int dx = x1 - x0; int dy = y1 - y0; for (size_t i = 0; i <= dx; ++i) { int x = x0 + i; FRAMEBUFFER[y0][x] = clr; FRAMEBUFFER[y1][x] = clr; } for (size_t j = 0; j <= dy; ++j) { int y = y0 + j; FRAMEBUFFER[y][x0] = clr; FRAMEBUFFER[y][x1] = clr; } } static inline void draw_fill_rect(int x0, int y0, int x1, int y1, Color clr) { if (x0 > x1) { int tmp = x0; x0 = x1; x1 = tmp; } if (y0 > y1) { int tmp = y0; y0 = y1; y1 = tmp; } int dx = x1 - x0; int dy = y1 - y0; for (size_t i = 0; i <= dx; ++i) { for (size_t j = 0; j <= dy; ++j) { int x = x0 + i; int y = y0 + j; FRAMEBUFFER[y][x] = clr; } } } static inline void wait_vsync() { while(DISP_VCOUNT >= 160); while(DISP_VCOUNT < 160); } // // Main functions. // int main(void) { set_display_mode(DISP_MODE_3 | DISP_BG2); // Rectangle drawing testing. Color clr = COLOR_WHITE; for (size_t j = 0; j < SCREEN_HEIGHT; j += 8) { clr = clr == COLOR_WHITE ? COLOR_BLACK : COLOR_WHITE; for (size_t i = 0; i < SCREEN_WIDTH; i += 8) { clr = clr == COLOR_WHITE ? COLOR_BLACK : COLOR_WHITE; draw_fill_rect(i, j, 0 + i + 7, j + 7, clr); } } clr = COLOR_GREY; for (size_t j = 8; j < SCREEN_HEIGHT - 8; j += 8) { for (size_t i = 8; i < SCREEN_WIDTH - 8; i += 8) { draw_rect(i, j, 0 + i + 8, j + 8, clr); } } // Bresenham's testing for (size_t i = 0; i < SCREEN_WIDTH; i += 8) { draw_fill_rect(i, 7, 0 + i + 7, 16, COLOR_RED); } put_text(8, 8, COLOR_BLACK, "Lines and rectangles"); draw_fill_rect(69, 29, SCREEN_WIDTH - 69, SCREEN_HEIGHT - 29, COLOR_BLACK); for (size_t i = 70; i <= SCREEN_WIDTH - 70; i += 5) { int x0 = i; int y0 = 30; int x1 = SCREEN_WIDTH - i; int y1 = SCREEN_HEIGHT - 30; draw_line(x0, y0, x1, y1, COLOR_BLUE); } for (size_t i = 30; i <= SCREEN_HEIGHT - 30; i += 5) { int x0 = 70; int y0 = i; int x1 = SCREEN_WIDTH - 70; int y1 = SCREEN_HEIGHT - i; draw_line(x0, y0, x1, y1, COLOR_BLUE); } for (size_t i = 70; i <= SCREEN_WIDTH - 70; i += 10) { int x0 = i; int y0 = 30; int x1 = SCREEN_WIDTH - i; int y1 = SCREEN_HEIGHT - 30; draw_line(x0, y0, x1, y1, COLOR_CYAN); } for (size_t i = 30; i <= SCREEN_HEIGHT - 30; i += 10) { int x0 = 70; int y0 = i; int x1 = SCREEN_WIDTH - 70; int y1 = SCREEN_HEIGHT - i; draw_line(x0, y0, x1, y1, COLOR_CYAN); } while(true) { wait_vsync(); }; return 0; }