progress
[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
60         va_start(ap, fmt);
61         tg_vtext(x, y, fmt, ap);
62         va_end(ap);
63 }
64
65 void tg_vtext(int x, int y, const char *fmt, va_list ap)
66 {
67         char buf[256], *ptr;
68         unsigned short *fbptr = framebuf + y * 80 + x;
69
70         vsprintf(buf, fmt, ap);
71
72         ptr = buf;
73         while(*ptr) {
74                 *fbptr++ = *ptr++ | attr;
75         }
76 }
77
78 void tg_rect(const char *label, int x, int y, int xsz, int ysz, unsigned int flags)
79 {
80         int i, j;
81         unsigned short *fbptr = framebuf + y * 80 + x;
82
83         for(i=0; i<ysz; i++) {
84                 for(j=0; j<xsz; j++) {
85                         fbptr[j] = attr | bgchar;
86                 }
87                 fbptr += 80;
88         }
89
90         if(flags & TGFX_FRAME) {
91                 fbptr = framebuf + y * 80 + x;
92                 for(i=0; i<xsz-2; i++) {
93                         fbptr[i + 1] = attr | 0xcd;
94                         fbptr[(ysz-1) * 80 + i + 1] = attr | 0xcd;
95                 }
96                 for(i=0; i<ysz-2; i++) {
97                         fbptr[(i + 1) * 80] = attr | 0xba;
98                         fbptr[(xsz-1) + (i + 1) * 80] = attr | 0xba;
99                 }
100                 fbptr[0] = attr | 0xc9;
101                 fbptr[xsz-1] = attr | 0xbb;
102                 fbptr += (ysz - 1) * 80;
103                 fbptr[0] = attr | 0xc8;
104                 fbptr[xsz-1] = attr | 0xbc;
105         }
106
107         if(label) {
108                 tg_text(x + 2, y, "%s", label);
109         }
110 }