aboutsummaryrefslogtreecommitdiffstats
path: root/src/bitmap.h
blob: 48c8442a23deee6b8d045bd4e2d757cf7b22d7ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
Copyright (c) 2021 Bad Diode

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE.
*/

#ifndef GBAEXP_BITMAP_H
#define GBAEXP_BITMAP_H

#include "common.h"

// 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 void
draw_line(int x0, int y0, int x1, int y1, Color clr) {
    // Pointer to the initial position of the screen buffer where we will start
    // writing our data.
    vu16 *destination = (u16*)(SCREEN_BUFFER + y0 * SCREEN_WIDTH + x0);

    // Adjust the step direction and calculate deltas.
    int x_step;
    int y_step;
    int dx;
    int dy;
    if (x0 > x1) {
        x_step = -1;
        dx = x0 - x1;
    } else {
        x_step = 1;
        dx = x1 - x0;
    }
    if (y0 > y1) {
        y_step = -SCREEN_WIDTH;
        dy = y0 - y1;
    } else {
        y_step = +SCREEN_WIDTH;
        dy = y1 - y0;
    }

    if(dy == 0) {
        // Horizontal line.
        for(int i = 0; i <= dx; i++) {
            destination[i * x_step] = clr;
        }
    } else if(dx == 0) {
        // Vertical line.
        for(int i = 0; i <= dy; i++) {
            destination[i * y_step] = clr;
        }
    } else if (dx >= dy){
        // Positive slope.
        int diff = 2 * dy - dx;
        for (int i = 0; i <= dx; ++i) {
            *destination = clr;
            if (diff >= 0) {
                destination += y_step;
                diff -= 2 * dx;
            }
            destination += x_step;
            diff += 2 * dy;
        }
    } else {
        // Negative slope.
        int diff = 2 * dx - dy;
        for (int i = 0; i <= dy; ++i) {
            *destination = clr;
            if (diff >= 0) {
                destination += x_step;
                diff -= 2 * dy;
            }
            destination += y_step;
            diff += 2 * dx;
        }
    }
}

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 (int i = 0; i <= dx; ++i) {
        int x = x0 + i;
        FRAMEBUFFER[y0][x] = clr;
        FRAMEBUFFER[y1][x] = clr;
    }
    for (int 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 (int i = 0; i <= dx; ++i) {
        for (int j = 0; j <= dy; ++j) {
            int x = x0 + i;
            int y = y0 + j;
            FRAMEBUFFER[y][x] = clr;
        }
    }
}

// In Mode4 the buffer is of 8 bytes per pixel instead of 16. We can't write the
// color directly, instead the color is stored in the palette memory at
// `MEM_PAL`. Note that in this mode MEM_PAL[0] is the background color. This
// plotter takes an index to a color stored in MEM_PAL[col_index]. Because the
// GBA needs to meet memory alignment requirements, we can't write a u8 into
// memory, instead we need to read a u16 word, mask and or the corresponding
// bits and wave the updated u16.
static inline void
put_pixel_m4(int x, int y, u8 clr_idx, vu16 *buffer) {
    int buffer_index = (y * SCREEN_WIDTH + x) / 2;
    vu16 *destination = &buffer[buffer_index];
    // Odd pixels will go to the top 8 bits of the destination. Even pixels to
    // the lower 8 bits.
    int odd = x & 0x1;
    if(odd) {
        *destination= (*destination & 0xFF) | (clr_idx << 8);
    } else {
        *destination= (*destination & ~0xFF) |  clr_idx;
    }
}

static inline void
put_pixel_m3(int x, int y, u16 color, Scanline *buffer) {
    buffer[y][x] = color;
}

static inline void
clear_screen_m4() {
    size_t size = SCREEN_WIDTH * SCREEN_HEIGHT / 4;
    u32 *buf = backbuffer;
    for (size_t i = 0; i < size; ++i) {
        buf[i] = 0;
    }
}

static inline void
clear_screen_m3() {
    size_t size = SCREEN_WIDTH * SCREEN_HEIGHT / 4;
    u32 *buf = FRAMEBUFFER;
    for (size_t i = 0; i < size; ++i) {
        buf[i] = 0;
    }
}

static inline void
draw_fill_rect_m4(int x0, int y0, int x1, int y1, u8 col_index, vu16 *buffer) {
    int ix, iy;
    for(iy = y0; iy < y1; iy++) {
        for(ix = x0; ix < x1; ix++) {
            put_pixel_m4(ix, iy, col_index, buffer);
        }
    }
}

void
draw_logo(void) {
    int side = 60;
    int line = 35;
    int height = side * 0.5;
    int x = SCREEN_WIDTH / 2 - height / 2;
    int y = SCREEN_HEIGHT / 2;

    // Draw red triangle.
    draw_line(x + height - 1, y - side / 2, x, y - 1, COLOR_RED);
    draw_line(x + height - 1, y + side / 2, x, y + 1, COLOR_RED);
    draw_line(x + height - 1, y - side / 2 + 1, x, y, COLOR_RED);
    draw_line(x + height - 1, y + side / 2 - 1, x, y, COLOR_RED);

    // Draw white triangle.
    draw_line(x, y - side / 2, x, y + side / 2, COLOR_WHITE);
    draw_line(x + 1, y - side / 2, x + height, y - 1, COLOR_WHITE);
    draw_line(x + 1, y + side / 2, x + height, y + 1, COLOR_WHITE);

    // Draw white line at triangle tip.
    draw_line(x + height, y - side / 2, x + height, y + side / 2, COLOR_WHITE);
    draw_line(x + height + 1, y - side / 2, x + height + 1, y + side / 2, COLOR_WHITE);

    // Double triangle line.
    draw_line(x - 1, y - side / 2, x - 1, y + side / 2, COLOR_WHITE);
    draw_line(x + 1, y - side / 2 + 1, x + height, y, COLOR_WHITE);
    draw_line(x + 1, y + side / 2 - 1, x + height, y, COLOR_WHITE);

    // Draw white lines.
    draw_line(x - line, y, x, y, COLOR_WHITE);
    draw_line(x + height, y, x + height + line, y, COLOR_WHITE);
    draw_line(x - line, y + 1, x, y + 1, COLOR_WHITE);
    draw_line(x + height, y + 1, x + height + line, y + 1, COLOR_WHITE);
}

#endif // GBAEXP_BITMAP_H