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