rayzor
view src/gfx.c @ 5:5fcf72837b69
fixed the dosemu bit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 06 Apr 2014 02:43:24 +0300 |
parents | 2a5340a6eee4 |
children | a68dbf80d547 |
line source
1 #ifndef GFX_H_
2 #define GFX_H_
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include "vbe.h"
9 #define REALPTR(s, o) (void*)(((uint32_t)(s) << 4) + (uint32_t)(o))
10 #define VBEPTR(x) REALPTR(((x) & 0xffff0000) >> 16, (x) & 0xffff)
11 #define VBEPTR_SEG(x) (((x) & 0xffff0000) >> 16)
12 #define VBEPTR_OFF(x) ((x) & 0xffff)
14 #define SAME_BPP(a, b) \
15 ((a) == (b) || (a) == 16 && (b) == 15 || (a) == 15 && (b) == 16 || (a) == 32 && (b) == 24 || (a) == 24 && (b) == 32)
17 static unsigned int make_mask(int sz, int pos);
19 static struct vbe_info *vbe_info;
20 static struct vbe_mode_info *mode_info;
21 static int pal_bits = 6;
23 void *set_video_mode(int xsz, int ysz, int bpp)
24 {
25 int i;
26 uint16_t *modes, best = 0;
27 unsigned int fbsize;
29 /* check for VBE2 support and output some info */
30 if(!vbe_info) {
31 if(!(vbe_info = vbe_get_info())) {
32 fprintf(stderr, "VESA BIOS Extensions not available\n");
33 return 0;
34 }
36 printf("VBE Version: %x.%x\n", vbe_info->version >> 8, vbe_info->version & 0xff);
37 if(vbe_info->version < 0x200) {
38 fprintf(stderr, "This program requires VBE 2.0 or greater. Try running UniVBE\n");
39 return 0;
40 }
42 printf("Graphics adapter: %s, %s (%s)\n", VBEPTR(vbe_info->oem_vendor_name_ptr),
43 VBEPTR(vbe_info->oem_product_name_ptr), VBEPTR(vbe_info->oem_product_rev_ptr));
44 printf("Video memory: %dmb\n", vbe_info->total_mem << 6);
46 modes = VBEPTR(vbe_info->vid_mode_ptr);
47 }
49 for(i=0; i<1024; i++) { /* impose an upper limit to avoid inf-loops */
50 if(modes[i] == 0xffff) {
51 break; /* reached the end */
52 }
54 mode_info = vbe_get_mode_info(modes[i] | VBE_MODE_LFB);
55 if(!mode_info || mode_info->xres != xsz || mode_info->yres != ysz) {
56 continue;
57 }
58 if(SAME_BPP(mode_info->bpp, bpp)) {
59 best = modes[i];
60 }
61 }
63 if(best) {
64 mode_info = vbe_get_mode_info(best);
65 } else {
66 fprintf(stderr, "Requested video mode (%dx%d %dbpp) is unavailable\n", xsz, ysz, bpp);
67 return 0;
68 }
70 if(vbe_set_mode(best | VBE_MODE_LFB) == -1) {
71 fprintf(stderr, "Failed to set video mode %dx%d %dbpp\n", mode_info->xres, mode_info->yres, mode_info->bpp);
72 return 0;
73 }
75 /* attempt to set 8 bits of color per component in palettized modes */
76 if(bpp <= 8) {
77 pal_bits = vbe_set_palette_bits(8);
78 printf("palette bits per color primary: %d\n", pal_bits);
79 }
81 fbsize = xsz * ysz * mode_info->num_img_pages * (bpp / CHAR_BIT);
82 return (void*)dpmi_mmap(mode_info->fb_addr, fbsize);
83 }
85 int set_text_mode(void)
86 {
87 vbe_set_mode(0x3);
88 return 0;
89 }
91 int get_color_depth(void)
92 {
93 if(!mode_info) {
94 return -1;
95 }
96 return mode_info->bpp;
97 }
99 int get_color_bits(int *rbits, int *gbits, int *bbits)
100 {
101 if(!mode_info) {
102 return -1;
103 }
104 *rbits = mode_info->rmask_size;
105 *gbits = mode_info->gmask_size;
106 *bbits = mode_info->bmask_size;
107 return 0;
108 }
110 int get_color_mask(unsigned int *rmask, unsigned int *gmask, unsigned int *bmask)
111 {
112 if(!mode_info) {
113 return -1;
114 }
115 *rmask = make_mask(mode_info->rmask_size, mode_info->rpos);
116 *gmask = make_mask(mode_info->gmask_size, mode_info->gpos);
117 *bmask = make_mask(mode_info->bmask_size, mode_info->bpos);
118 return 0;
119 }
121 int get_color_shift(int *rshift, int *gshift, int *bshift)
122 {
123 if(!mode_info) {
124 return -1;
125 }
126 *rshift = mode_info->rpos;
127 *gshift = mode_info->gpos;
128 *bshift = mode_info->bpos;
129 return 0;
130 }
132 void set_palette(int idx, int r, int g, int b)
133 {
134 int col[3];
135 col[0] = r;
136 col[1] = g;
137 col[2] = b;
138 vbe_set_palette(idx, col, 1, pal_bits);
139 }
141 void wait_vsync(void)
142 {
143 /* TODO */
144 }
146 static unsigned int make_mask(int sz, int pos)
147 {
148 unsigned int i, mask = 0;
150 for(i=0; i<sz; i++) {
151 mask |= 1 << i;
152 }
153 return mask << pos;
154 }
157 #endif /* GFX_H_ */