Atlas - VGA.C
Home / systems / IBM-PC / PhotonX / src Lines: 2 | Size: 5165 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1#include <dos.h> 2#include <conio.h> 3#include "VGA.H" 4 5unsigned char far *vga = (unsigned char far *)MK_FP(VGA_SEG, 0); 6static uint8_t g_cls_color = 0; 7extern char font8x8_basic[128][8]; 8 9static int in_gfx_bounds(int x, int y) 10{ 11 if (x < 0 || x >= 320) 12 return 0; 13 if (y < 0 || y >= 200) 14 return 0; 15 return 1; 16} 17 18static void swap_u16(uint16_t *a, uint16_t *b) 19{ 20 uint16_t temp = *a; 21 *a = *b; 22 *b = temp; 23} 24 25void vga_set_mode(uint8_t mode) 26{ 27 union REGS regs; 28 regs.h.ah = 0x00; 29 regs.h.al = mode; 30 int86(0x10, ®s, ®s); 31} 32 33 34void vga_text_setcolor(uint8_t fg, uint8_t bg) 35{ 36 textcolor(fg & 0x0F); 37 textbackground(bg & 0x0F); 38} 39 40void vga_text_gotoxy(uint8_t x, uint8_t y) 41{ 42 gotoxy((int)x + 1, (int)y + 1); 43} 44 45void vga_text_putch(char c) 46{ 47 putch(c); 48} 49 50void vga_text_puts(const char *str) 51{ 52 cputs(str); 53} 54 55void vga_text_cls(void) 56{ 57 clrscr(); 58} 59 60void vga_set_palette(uint8_t index, uint8_t r, uint8_t g, uint8_t b) 61{ 62 outp(0x3C8, index); 63 outp(0x3C9, r); 64 outp(0x3C9, g); 65 outp(0x3C9, b); 66} 67 68void vga_gfx_setclscolor(uint8_t color) 69{ 70 g_cls_color = color; 71} 72 73void vga_gfx_cls(void) 74{ 75 int i; 76 for (i = 0; i < 320 * 200; i++) 77 vga[i] = g_cls_color; 78} 79 80void vga_gfx_putpixel(uint16_t x, uint16_t y, uint8_t color) 81{ 82 if (!in_gfx_bounds((int)x, (int)y)) 83 return; 84 85 vga[(unsigned int)y * 320u + (unsigned int)x] = color; 86} 87 88void vga_gfx_line(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint8_t color) 89{ 90 int dx, dy, sx, sy, err, e2; 91 int cx = (int)x1; 92 int cy = (int)y1; 93 int tx = (int)x2; 94 int ty = (int)y2; 95 96 dx = (tx > cx) ? (tx - cx) : (cx - tx); 97 dy = (ty > cy) ? (ty - cy) : (cy - ty); 98 sx = (cx < tx) ? 1 : -1; 99 sy = (cy < ty) ? 1 : -1; 100 err = dx - dy; 101 102 for (;;) 103 { 104 if (in_gfx_bounds(cx, cy)) 105 vga[cy * 320 + cx] = color; 106 107 if (cx == tx && cy == ty) 108 break; 109 110 e2 = err << 1; 111 if (e2 > -dy) 112 { 113 err -= dy; 114 cx += sx; 115 } 116 if (e2 < dx) 117 { 118 err += dx; 119 cy += sy; 120 } 121 } 122} 123 124void vga_gfx_rect(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint8_t color) 125{ 126 if (x2 < x1) 127 swap_u16(&x1, &x2); 128 if (y2 < y1) 129 swap_u16(&y1, &y2); 130 131 vga_gfx_line(x1, y1, x2, y1, color); 132 vga_gfx_line(x2, y1, x2, y2, color); 133 vga_gfx_line(x2, y2, x1, y2, color); 134 vga_gfx_line(x1, y2, x1, y1, color); 135} 136 137void vga_gfx_circle(uint16_t x, uint16_t y, uint16_t radius, uint8_t color) 138{ 139 int cx = (int)x; 140 int cy = (int)y; 141 int r = (int)radius; 142 int px = r; 143 int py = 0; 144 int err = 0; 145 146 while (px >= py) 147 { 148 vga_gfx_putpixel((uint16_t)(cx + px), (uint16_t)(cy + py), color); 149 vga_gfx_putpixel((uint16_t)(cx + py), (uint16_t)(cy + px), color); 150 vga_gfx_putpixel((uint16_t)(cx - py), (uint16_t)(cy + px), color); 151 vga_gfx_putpixel((uint16_t)(cx - px), (uint16_t)(cy + py), color); 152 vga_gfx_putpixel((uint16_t)(cx - px), (uint16_t)(cy - py), color); 153 vga_gfx_putpixel((uint16_t)(cx - py), (uint16_t)(cy - px), color); 154 vga_gfx_putpixel((uint16_t)(cx + py), (uint16_t)(cy - px), color); 155 vga_gfx_putpixel((uint16_t)(cx + px), (uint16_t)(cy - py), color); 156 157 if (err <= 0) 158 { 159 py++; 160 err += (py << 1) + 1; 161 } 162 if (err > 0) 163 { 164 px--; 165 err -= (px << 1) + 1; 166 } 167 } 168} 169 170void vga_gfx_putch(char c, int x, int y, uint8_t color) 171{ 172 int row; 173 int col; 174 uint8_t bits; 175 uint8_t glyph = (uint8_t)c; 176 177 if (glyph >= 128) 178 glyph = (uint8_t)'?'; 179 180 for (row = 0; row < 8; row++) 181 { 182 bits = (uint8_t)font8x8_basic[glyph][row]; 183 for (col = 0; col < 8; col++) 184 { 185 if (bits & (1u << col)) 186 vga_gfx_putpixel((uint16_t)(x + col), (uint16_t)(y + row), color); 187 } 188 } 189} 190 191void vga_gfx_puts(const char *str, int x, int y, uint8_t color) 192{ 193 int cx = x; 194 195 while (*str) 196 { 197 if (*str == '\n') 198 { 199 cx = x; 200 y += 8; 201 } 202 else 203 { 204 vga_gfx_putch(*str, cx, y, color); 205 cx += 8; 206 } 207 str++; 208 } 209} 210 211void set_mode(unsigned char mode) 212{ 213 vga_set_mode((uint8_t)mode); 214} 215 216void set_palette(unsigned char index, unsigned char r, unsigned char g, unsigned char b) 217{ 218 vga_set_palette((uint8_t)index, (uint8_t)r, (uint8_t)g, (uint8_t)b); 219} 220 221void clear_screen(unsigned char color) 222{ 223 vga_gfx_setclscolor((uint8_t)color); 224 vga_gfx_cls(); 225} 226 227void put_pixel(int x, int y, unsigned char color) 228{ 229 if (x < 0 || y < 0) 230 return; 231 232 vga_gfx_putpixel((uint16_t)x, (uint16_t)y, (uint8_t)color); 233} 234 235void vga_reset(void) 236{ 237 vga_set_mode(VGA_MODE_TEXT_80x25); 238 clrscr(); 239 textcolor(15); 240 textbackground(0); 241}[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.