moved dos tgfx.c to src/dos
[oftp] / src / dos / tgfx.c
1 #include <stdio.h>
2 #include <stdarg.h>
3 #include <dos.h>
4 #include <conio.h>
5 #include "tgfx.h"
6
7 static unsigned short attr = 0x0700;
8 static int bgcol = 0, fgcol = 7;
9 static int bgchar = ' ';
10 static unsigned short *framebuf = (unsigned short*)0xb8000;
11
12 #define UPD_ATTR        attr = (fgcol << 8) | (bgcol << 12)
13
14 void tg_clear(void)
15 {
16         tg_rect(0, 0, 0, 80, 25, 0);
17 }
18
19 void tg_fgcolor(int col)
20 {
21         fgcol = col & 0xf;
22         UPD_ATTR;
23 }
24
25 void tg_bgcolor(int col)
26 {
27         bgcol = col & 0xf;
28         UPD_ATTR;
29 }
30
31 void tg_color(int col)
32 {
33         fgcol = col & 0xf;
34         bgcol = (col >> 4) & 0xf;
35         attr = col << 8;
36 }
37
38 void tg_bgchar(int c)
39 {
40         bgchar = c;
41 }
42
43 #define CRTC_ADDR_PORT  0x3d4
44 #define CRTC_DATA_PORT  0x3d5
45 #define REG_CRTC_CURH   0xe
46 #define REG_CRTC_CURL   0xf
47
48 void tg_setcursor(int x, int y)
49 {
50         unsigned int addr = y * 80 + x;
51
52         outpw(CRTC_ADDR_PORT, (addr & 0xff00) | REG_CRTC_CURH);
53         outpw(CRTC_ADDR_PORT, (addr << 8) | REG_CRTC_CURL);
54 }
55
56 void tg_text(int x, int y, const char *fmt, ...)
57 {
58         va_list ap;
59         char buf[128], *ptr;
60         unsigned short *fbptr = framebuf + y * 80 + x;
61
62         va_start(ap, fmt);
63         vsprintf(buf, fmt, ap);
64         va_end(ap);
65
66         ptr = buf;
67         while(*ptr) {
68                 *fbptr++ = *ptr++ | attr;
69         }
70 }
71
72 void tg_rect(const char *label, int x, int y, int xsz, int ysz, unsigned int flags)
73 {
74         int i, j;
75         unsigned short *fbptr = framebuf + y * 80 + x;
76
77         for(i=0; i<ysz; i++) {
78                 for(j=0; j<xsz; j++) {
79                         fbptr[j] = attr | bgchar;
80                 }
81                 fbptr += 80;
82         }
83
84         if(flags & TGFX_FRAME) {
85                 fbptr = framebuf + y * 80 + x;
86                 for(i=0; i<xsz-2; i++) {
87                         fbptr[i + 1] = attr | 0xcd;
88                         fbptr[(ysz-1) * 80 + i + 1] = attr | 0xcd;
89                 }
90                 for(i=0; i<ysz-2; i++) {
91                         fbptr[(i + 1) * 80] = attr | 0xba;
92                         fbptr[(xsz-1) + (i + 1) * 80] = attr | 0xba;
93                 }
94                 fbptr[0] = attr | 0xc9;
95                 fbptr[xsz-1] = attr | 0xbb;
96                 fbptr += (ysz - 1) * 80;
97                 fbptr[0] = attr | 0xc8;
98                 fbptr[xsz-1] = attr | 0xbc;
99         }
100
101         if(label) {
102                 tg_text(x + 2, y, "%s", label);
103         }
104 }