the rewrite branch builds, not tested yet
[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         if(vbe_info(&vbe) == -1) {
29                 return -1;
30         }
31
32         vbe_print_info(stdout, &vbe);
33         fflush(stdout);
34
35         vbe_init_ver = VBE_VER_MAJOR(vbe.ver);
36         return 0;
37 }
38
39 void *set_video_mode(int xsz, int ysz, int bpp)
40 {
41         int i, nmodes;
42         int best_match_mode = -1;
43         struct vbe_mode_info minf;
44
45         if(!vbe_init_ver) {
46                 if(init_vbe() == -1) {
47                         fprintf(stderr, "failed to initialize VBE\n");
48                         return 0;
49                 }
50                 if(vbe_init_ver < 2) {
51                         fprintf(stderr, "VBE >= 2.0 required\n");
52                         return 0;
53                 }
54         }
55
56         mode = -1;
57
58         for(i=0; i<nmodes; i++) {
59                 vbe_mode_info(vbe.modes[i], &minf);
60                 if(minf.xres != xsz || minf.yres != ysz) continue;
61                 if(minf.bpp == bpp) {
62                         mode = vbe.modes[i];
63                         break;
64                 }
65                 if(SAME_BPP(minf.bpp, bpp)) {
66                         best_match_mode = mode;
67                 }
68         }
69
70         if(mode == -1) {
71                 if(best_match_mode == -1) {
72                         fprintf(stderr, "failed to find requested video mode (%dx%d %d bpp)\n", xsz, ysz, bpp);
73                         return 0;
74                 }
75                 mode = best_match_mode;
76         }
77
78         vbe_mode_info(mode, &mode_info);
79         printf("setting video mode %x: (%dx%d %d)\n", (unsigned int)mode, mode_info.xres,
80                         mode_info.yres, mode_info.bpp);
81
82         if(vbe_setmode(mode) == -1) {
83                 fprintf(stderr, "failed to set video mode\n");
84                 return 0;
85         }
86
87         pgsize = mode_info.xres * mode_info.yres * (bpp / 8);
88         fbsize = mode_info.num_img_pages * pgsize;
89
90         vpgaddr[0] = (void*)dpmi_mmap(mode_info.fb_addr, fbsize);
91
92         if(mode_info.num_img_pages > 1) {
93                 vpgaddr[1] = (char*)vpgaddr[0] + pgsize;
94                 fbidx = 1;
95                 page_flip(FLIP_NOW);    /* start with the second page visible */
96         } else {
97                 fbidx = 0;
98                 vpgaddr[1] = 0;
99         }
100
101         return vpgaddr[0];
102 }
103
104 int set_text_mode(void)
105 {
106         vga_setmode(3);
107         return 0;
108 }
109
110 void *page_flip(int vsync)
111 {
112         if(!vpgaddr[1]) {
113                 /* page flipping not supported */
114                 return vpgaddr[0];
115         }
116
117         vbe_swap(fbidx ? pgsize : 0, vsync ? VBE_SWAP_VBLANK : VBE_SWAP_NOW);
118         fbidx = (fbidx + 1) & 1;
119
120         return vpgaddr[fbidx];
121 }