summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-06-06 11:33:08 +0200
committerBad Diode <bd@badd10de.dev>2021-06-06 11:33:08 +0200
commit3a141f98c21809baa84dd698b00174d8074f9a73 (patch)
tree959187cf4e071f221d2a94e8a7f56b64052ea7a1
parent781ab3f0f32ea3db697f2c77e17099b243c3ed29 (diff)
downloadgba-sequencer-3a141f98c21809baa84dd698b00174d8074f9a73.tar.gz
gba-sequencer-3a141f98c21809baa84dd698b00174d8074f9a73.zip
Add draw_filled_rect function
-rw-r--r--src/main.c15
-rw-r--r--src/renderer.c23
-rw-r--r--src/renderer.h3
3 files changed, 30 insertions, 11 deletions
diff --git a/src/main.c b/src/main.c
index 9e0be39..3a15655 100644
--- a/src/main.c
+++ b/src/main.c
@@ -53,12 +53,15 @@ WITH REGARD TO THIS SOFTWARE.
53void 53void
54test_rects() { 54test_rects() {
55 for (size_t i = 0; i < 100; i++) { 55 for (size_t i = 0; i < 100; i++) {
56 draw_line(0, 10, 10, 10, 1); 56 // draw_line(0, 10, 10, 10, 1);
57 draw_line(10, 10, 10, 20, 1); 57 // draw_line(10, 10, 10, 20, 1);
58 draw_line(0, 0, 10, 10, 2); 58 // draw_line(0, 0, 10, 10, 2);
59 draw_line(0, 20, 20, 14, 1); 59 // draw_line(0, 20, 20, 14, 1);
60 draw_line(0, 0, 239, 159, 2); 60 // draw_line(0, 0, 239, 159, 2);
61 draw_line(0, 159, 239, 0, 3); 61 // draw_line(0, 159, 239, 0, 3);
62 // draw_filled_rect(10, 10, SCREEN_WIDTH - 11, SCREEN_HEIGHT - 11, 3);
63 // draw_filled_rect(10, 10, 13, 21, 3);
64 draw_filled_rect(10, 10, 20, 20, 3);
62 } 65 }
63} 66}
64 67
diff --git a/src/renderer.c b/src/renderer.c
index 2b1f0d2..773d50b 100644
--- a/src/renderer.c
+++ b/src/renderer.c
@@ -26,7 +26,7 @@ static u32 dirty_tiles[21] = {0};
26 26
27IWRAM_CODE 27IWRAM_CODE
28void 28void
29draw_pixel(size_t x, size_t y, u8 color) { 29draw_pixel(size_t x, size_t y, u8 clr) {
30 BOUNDCHECK_SCREEN(x, y); 30 BOUNDCHECK_SCREEN(x, y);
31 31
32 // Find row position for the given x/y coordinates. 32 // Find row position for the given x/y coordinates.
@@ -38,7 +38,7 @@ draw_pixel(size_t x, size_t y, u8 color) {
38 38
39 // Update backbuffer. 39 // Update backbuffer.
40 size_t shift = start_col * sizeof(u32); 40 size_t shift = start_col * sizeof(u32);
41 BACKBUF[pos] = (BACKBUF[pos] & ~(0xF << shift)) | color << shift; 41 BACKBUF[pos] = (BACKBUF[pos] & ~(0xF << shift)) | clr << shift;
42 42
43 // Mark tile as dirty. 43 // Mark tile as dirty.
44 dirty_tiles[tile_y] |= 1 << tile_x; 44 dirty_tiles[tile_y] |= 1 << tile_x;
@@ -129,7 +129,7 @@ draw_line(size_t x0, size_t y0, size_t x1, size_t y1, u8 clr) {
129 if (dx >= dy) { 129 if (dx >= dy) {
130 // Positive slope. 130 // Positive slope.
131 int diff = 2 * dy - dx; 131 int diff = 2 * dy - dx;
132 for (int i = 0; i <= dx; ++i) { 132 for (size_t i = 0; i <= dx; ++i) {
133 draw_pixel(x, y, clr); 133 draw_pixel(x, y, clr);
134 if (diff >= 0) { 134 if (diff >= 0) {
135 y += y_step; 135 y += y_step;
@@ -141,7 +141,7 @@ draw_line(size_t x0, size_t y0, size_t x1, size_t y1, u8 clr) {
141 } else { 141 } else {
142 // Negative slope. 142 // Negative slope.
143 int diff = 2 * dx - dy; 143 int diff = 2 * dx - dy;
144 for (int i = 0; i <= dy; ++i) { 144 for (size_t i = 0; i <= dy; ++i) {
145 draw_pixel(x, y, clr); 145 draw_pixel(x, y, clr);
146 if (diff >= 0) { 146 if (diff >= 0) {
147 x += x_step; 147 x += x_step;
@@ -253,6 +253,21 @@ draw_rect(size_t x0, size_t y0, size_t x1, size_t y1, u8 clr) {
253 253
254IWRAM_CODE 254IWRAM_CODE
255void 255void
256draw_filled_rect(size_t x0, size_t y0, size_t x1, size_t y1, u8 clr) {
257 BOUNDCHECK_SCREEN(x0, y0);
258 BOUNDCHECK_SCREEN(x1, y1);
259
260 size_t dx = x1 - x0;
261 size_t dy = y1 - y0;
262 size_t n_rect = MIN(dx, dy);
263 n_rect = n_rect / 2 + 1;
264 for (size_t i = 0; i < n_rect; i++) {
265 draw_rect(x0 + i, y0 + i, x1 - i, y1 - i, clr);
266 }
267}
268
269IWRAM_CODE
270void
256draw_tile(size_t x, size_t y, Tile *tile, bool merge) { 271draw_tile(size_t x, size_t y, Tile *tile, bool merge) {
257 BOUNDCHECK_SCREEN(x, y); 272 BOUNDCHECK_SCREEN(x, y);
258 273
diff --git a/src/renderer.h b/src/renderer.h
index 6e5cc38..e0aae31 100644
--- a/src/renderer.h
+++ b/src/renderer.h
@@ -28,9 +28,10 @@
28#define FONT_SB 15 28#define FONT_SB 15
29#define FONT_OFFSET 192 29#define FONT_OFFSET 192
30 30
31void draw_pixel(size_t x, size_t y, u8 color); 31void draw_pixel(size_t x, size_t y, u8 clr);
32void draw_line(size_t x0, size_t y0, size_t x1, size_t y1, u8 clr); 32void draw_line(size_t x0, size_t y0, size_t x1, size_t y1, u8 clr);
33void draw_rect(size_t x0, size_t y0, size_t x1, size_t y1, u8 clr); 33void draw_rect(size_t x0, size_t y0, size_t x1, size_t y1, u8 clr);
34void draw_filled_rect(size_t x0, size_t y0, size_t x1, size_t y1, u8 clr);
34void draw_tile(size_t x, size_t y, Tile *tile, bool merge); 35void draw_tile(size_t x, size_t y, Tile *tile, bool merge);
35void flip_buffer(void); 36void flip_buffer(void);
36void renderer_init(void); 37void renderer_init(void);