570eabac2d90787b5a9b38a38d0d5cdacc689fbe
[dosdemo] / src / dos / gfx.c
1 #include <stdio.h>
2 #include "gfx.h"
3 #include "vbe.h"
4 #include "vga.h"
5 #include "cdpmi.h"
6
7 #ifdef __DJGPP__
8 #include <sys/nearptr.h>
9 #define REALPTR(s, o)   (void*)(((uint32_t)(s) << 4) - __djgpp_base_address + ((uint32_t)(o)))
10 #else
11 #define REALPTR(s, o)   (void*)(((uint32_t)(s) << 4) + ((uint32_t)(o)))
12 #endif
13
14 #define SAME_BPP(a, b)  \
15     ((a) == (b) || ((a) == 16 && (b) == 15) || ((a) == 15 && (b) == 16) || \
16      ((a) == 32 && (b) == 24) || ((a) == 24 && (b) == 32))
17
18 static int vbe_init_ver;
19 static struct vbe_info vbe;
20 static int mode, pgsize, fbsize;
21 static struct vbe_mode_info mode_info;
22
23 static void *vpgaddr[2];
24 static int fbidx;
25
26 static int init_vbe(void)
27 {
28         int i, num;
29
30         if(vbe_info(&vbe) == -1) {
31                 fprintf(stderr, "failed to retrieve VBE information\n");
32                 return -1;
33         }
34
35         vbe_print_info(stdout, &vbe);
36
37         num = vbe_num_modes(&vbe);
38         for(i=0; i<num; i++) {
39                 struct vbe_mode_info minf;
40
41                 if(vbe_mode_info(vbe.modes[i], &minf) == -1) {
42                         continue;
43                 }
44                 printf("%04x: ", vbe.modes[i]);
45                 vbe_print_mode_info(stdout, &minf);
46         }
47         fflush(stdout);
48
49         vbe_init_ver = VBE_VER_MAJOR(vbe.ver);
50         return 0;
51 }
52
53 void *set_video_mode(int xsz, int ysz, int bpp)
54 {
55         int i, nmodes;
56         int best_match_mode = -1;
57         struct vbe_mode_info minf;
58
59         if(!vbe_init_ver) {
60                 if(init_vbe() == -1) {
61                         fprintf(stderr, "failed to initialize VBE\n");
62                         return 0;
63                 }
64                 if(vbe_init_ver < 2) {
65                         fprintf(stderr, "VBE >= 2.0 required\n");
66                         return 0;
67                 }
68         }
69
70         mode = -1;
71         nmodes = vbe_num_modes(&vbe);
72         for(i=0; i<nmodes; i++) {
73                 if(vbe_mode_info(vbe.modes[i], &minf) == -1) {
74                         continue;
75                 }
76                 if(minf.xres != xsz || minf.yres != ysz) continue;
77                 if(minf.bpp == bpp) {
78                         mode = vbe.modes[i];
79                         break;
80                 }
81                 if(SAME_BPP(minf.bpp, bpp)) {
82                         best_match_mode = mode;
83                 }
84         }
85
86         if(mode == -1) {
87                 if(best_match_mode == -1) {
88                         fprintf(stderr, "failed to find requested video mode (%dx%d %d bpp)\n", xsz, ysz, bpp);
89                         return 0;
90                 }
91                 mode = best_match_mode;
92         }
93
94         vbe_mode_info(mode, &mode_info);
95         printf("setting video mode %x: (%dx%d %d)\n", (unsigned int)mode, mode_info.xres,
96                         mode_info.yres, mode_info.bpp);
97
98         if(vbe_setmode(mode) == -1) {
99                 fprintf(stderr, "failed to set video mode\n");
100                 return 0;
101         }
102
103         pgsize = mode_info.xres * mode_info.yres * (bpp / 8);
104         fbsize = mode_info.num_img_pages * pgsize;
105
106         vpgaddr[0] = (void*)dpmi_mmap(mode_info.fb_addr, fbsize);
107
108         if(mode_info.num_img_pages > 1) {
109                 vpgaddr[1] = (char*)vpgaddr[0] + pgsize;
110                 fbidx = 1;
111                 page_flip(FLIP_NOW);    /* start with the second page visible */
112         } else {
113                 fbidx = 0;
114                 vpgaddr[1] = 0;
115         }
116
117         return vpgaddr[0];
118 }
119
120 int set_text_mode(void)
121 {
122         vga_setmode(3);
123         return 0;
124 }
125
126 void *page_flip(int vsync)
127 {
128         if(!vpgaddr[1]) {
129                 /* page flipping not supported */
130                 return vpgaddr[0];
131         }
132
133         vbe_swap(fbidx ? pgsize : 0, vsync ? VBE_SWAP_VBLANK : VBE_SWAP_NOW);
134         fbidx = (fbidx + 1) & 1;
135
136         return vpgaddr[fbidx];
137 }