@@ -104,6 +104,10 @@ static void set_curmode(enum VGA_Mode m) {
104
104
hbuf = getbuf ();
105
105
}
106
106
107
+ inline static size_t compute_scraddr (uint16_t x , uint16_t y ) {
108
+ return y * curmode .pitch + x * curmode .depth ;
109
+ }
110
+
107
111
int vga_init (void ) {
108
112
set_curmode (VGA_80x25_TM );
109
113
hbuf = (uint8_t * )0xb8000 ;
@@ -177,7 +181,7 @@ void ascii_blit_buf(uint8_t* buf, uint16_t x, uint16_t y, unsigned char ch, cons
177
181
const size_t addr = (yc * h + y0 ) * (w * 16 ) + (xc * w + x0 );
178
182
const uint8_t byte = font -> bitmap [addr / 8 + 1 ];
179
183
const bool val = byte & (0x80 >> (addr & 7 ));
180
- const size_t scraddr = ( y0 + y ) * curmode . pitch + (( x0 + x ) * curmode . depth );
184
+ const size_t scraddr = compute_scraddr ( x0 + x , y0 + y );
181
185
buf [scraddr ] = val ? fgc : bgc ;
182
186
}
183
187
}
@@ -198,8 +202,38 @@ void vga_fill_rect_direct(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_
198
202
void vga_fill_rect_buf (uint8_t * buf , uint16_t x , uint16_t y , uint16_t w , uint16_t h , uint8_t cc ) {
199
203
for (uint16_t y0 = 0 ; y0 < h ; ++ y0 ) {
200
204
for (uint16_t x0 = 0 ; x0 < w ; ++ x0 ) {
201
- const size_t pos = (y + y0 ) * curmode .pitch + ((x + x0 ) * curmode .depth );
202
- buf [pos ] = cc ;
205
+ buf [compute_scraddr (x0 + x , y0 + y )] = cc ;
206
+ }
207
+ }
208
+ }
209
+ static uint8_t read_px (uint16_t x , uint16_t y ) {
210
+ return buffer [y * curmode .pitch + x * curmode .depth ];
211
+ }
212
+ void vga_blit_transparent (uint16_t x , uint16_t y , uint16_t w , uint16_t h , const void * pixels ) {
213
+ const uint16_t p = w ;
214
+ if (x + w >= curmode .width ) w = curmode .width - x ;
215
+ if (y + h >= curmode .height ) h = curmode .height - y ;
216
+ uint8_t * buf = buffering ? buffer : hbuf ;
217
+ const uint8_t * px = (const uint8_t * )pixels ;
218
+ for (uint16_t y0 = 0 ; y0 < h ; ++ y0 ) {
219
+ for (uint16_t x0 = 0 ; x0 < w ; ++ x0 ) {
220
+ const uint8_t val = px [y0 * p + x0 ];
221
+ if (val != 0xff ) buf [compute_scraddr (x0 + x , y0 + y )] = val ;
203
222
}
204
223
}
224
+ }
225
+ void vga_blit_sprite (uint16_t x , uint16_t y , uint16_t w , uint16_t h , uint8_t fgc , const void * pixels ) {
226
+ const uint16_t p = w ;
227
+ if (x + w >= curmode .width ) w = curmode .width - x ;
228
+ if (y + h >= curmode .height ) h = curmode .height - y ;
229
+ const uint8_t * px = (const uint8_t * )pixels ;
230
+ uint8_t * buf = buffering ? buffer : hbuf ;
231
+ for (uint16_t y0 = 0 ; y0 < h ; ++ y0 ) {
232
+ for (uint16_t x0 = 0 ; x0 < w ; ++ x0 ) {
233
+ const size_t saddr = y0 * p + x0 ;
234
+ const bool val = px [saddr / 8 ] & (0x80 >> (saddr & 7 ));
235
+ if (val ) buf [compute_scraddr (x0 + x , y0 + y )] = fgc ;
236
+ }
237
+ }
238
+
205
239
}
0 commit comments