fixed vbe
[vidsys] / main.c
1 #include <stdio.h>
2 #include <math.h>
3 #include <limits.h>
4 #include <conio.h>
5 #include "vidsys.h"
6
7 void test8bpp(void);
8
9 struct vid_modeinfo *vm;
10 void *vmem;
11
12 static const char *usage_fmt = "Usage %s: [options]\n"
13         "Options:      \n"
14         " -s <XxY>: video resolution\n"
15         " -b <bpp>: color depth\n";
16
17 int main(int argc, char **argv)
18 {
19         int i;
20         int mode;
21         int xres = 640;
22         int yres = 480;
23         int bpp = 8;
24
25         for(i=1; i<argc; i++) {
26                 if(argv[i][0] == '-') {
27                         if(argv[i][2]) {
28 invalopt:               fprintf(stderr, "invalid option: %s\n", argv[i]);
29                                 return 1;
30                         }
31                         switch(argv[i][1]) {
32                         case 's':
33                                 if(!argv[++i] || sscanf(argv[i], "%dx%d", &xres, &yres) != 2) {
34                                         fprintf(stderr, "invalid resolution: %s\n", argv[i]);
35                                         return 1;
36                                 }
37                                 break;
38
39                         case 'b':
40                                 if(!argv[++i] || (bpp = atoi(argv[i])) <= 0) {
41                                         fprintf(stderr, "invalid color depth: %s\n", argv[i]);
42                                         return 1;
43                                 }
44
45                         case 'h':
46                                 printf(usage_fmt, argv[0]);
47                                 return 0;
48
49                         default:
50                                 goto invalopt;
51                         }
52                 } else {
53                         fprintf(stderr, "unexpected argument: %s\n", argv[i]);
54                         return 1;
55                 }
56         }
57
58
59         if(vid_init() == -1) {
60                 fprintf(stderr, "failed to initialize video\n");
61                 return 1;
62         }
63         printf("press any key...\n");
64         getch();
65
66         if((mode = vid_findmode(xres, yres, bpp)) == -1) {
67                 fprintf(stderr, "failed to find video mode: %dx%d %dbpp\n", xres, yres, bpp);
68                 return 1;
69         }
70         if(!(vmem = vid_setmode(mode))) {
71                 fprintf(stderr, "failed to set video mode %04x\n", mode);
72                 return 1;
73         }
74         vm = vid_modeinfo(mode);
75
76         switch(vm->bpp) {
77         case 8:
78                 test8bpp();
79                 break;
80
81         default:
82                 break;
83         }
84
85         for(;;) {
86                 while(kbhit()) {
87                         if(getch() == 27) {
88                                 goto end;
89                         }
90                 }
91         }
92
93 end:
94         vid_setmode(3);
95         vid_cleanup();
96         return 0;
97 }
98
99 static float fmin(float a, float b)
100 {
101         return a < b ? a : b;
102 }
103
104 #define PI              3.14159265
105 #define TWO_PI  (PI * 2.0f)
106
107 void test8bpp(void)
108 {
109         int i, j;
110         unsigned char *fb;
111         struct vid_color cmap[256];
112         int winsz, winleft, winpos;
113
114         for(i=0; i<256; i++) {
115                 float x = (float)i / (256.0f / TWO_PI);
116                 cmap[i].r = (int)fmin(0.0f, sin(x) * 255.0f);
117                 cmap[i].g = (int)fmin(0.0f, sin(x - PI) * 255.0f);
118                 cmap[i].b = (int)fmin(0.0f, cos(x - TWO_PI) * 255.0f);
119         }
120         vid_setpal(0, 256, cmap);
121
122         if(vid_islinear()) {
123                 winsz = INT_MAX;
124         } else {
125                 winsz = 65536;
126                 winpos = 0;
127                 vid_setwin(0, 0);
128         }
129         winleft = winsz;
130         fb = vmem;
131
132         vid_vsync();
133
134         for(i=0; i<vm->height; i++) {
135                 for(j=0; j<vm->width; j++) {
136                         if(winleft-- <= 0) {
137                                 winpos += vm->win_step;
138                                 vid_setwin(0, winpos);
139                                 winleft = winsz;
140                                 fb = vmem;
141                         }
142                         *fb++ = i ^ j;
143                 }
144         }
145 }