initial commit
[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 int main(int argc, char **argv)
12 {
13         int mode;
14         int xres = 320;
15         int yres = 200;
16         int bpp = 8;
17
18         if(vid_init() == -1) {
19                 fprintf(stderr, "failed to initialize video\n");
20                 return 1;
21         }
22         printf("press any key...\n");
23         getch();
24
25         if((mode = vid_findmode(xres, yres, bpp)) == -1) {
26                 fprintf(stderr, "failed to find video mode: %dx%d %dbpp\n", xres, yres, bpp);
27                 return 1;
28         }
29         if(!(vmem = vid_setmode(mode))) {
30                 fprintf(stderr, "failed to set video mode %04x\n", mode);
31                 return 1;
32         }
33         vm = vid_modeinfo(mode);
34
35         switch(vm->bpp) {
36         case 8:
37                 test8bpp();
38                 break;
39
40         default:
41                 break;
42         }
43
44         for(;;) {
45                 while(kbhit()) {
46                         if(getch() == 27) {
47                                 goto end;
48                         }
49                 }
50         }
51
52 end:
53         vid_setmode(3);
54         vid_cleanup();
55         return 0;
56 }
57
58 static float fmin(float a, float b)
59 {
60         return a < b ? a : b;
61 }
62
63 #define PI              3.14159265
64 #define TWO_PI  (PI * 2.0f)
65
66 void test8bpp(void)
67 {
68         int i, j;
69         unsigned char *fb = vmem;
70         struct vid_color cmap[256];
71
72         for(i=0; i<256; i++) {
73                 float x = (float)i / (256.0f / TWO_PI);
74                 cmap[i].r = (int)fmin(0.0f, sin(x) * 255.0f);
75                 cmap[i].g = (int)fmin(0.0f, sin(x - PI) * 255.0f);
76                 cmap[i].b = (int)fmin(0.0f, cos(x - TWO_PI) * 255.0f);
77         }
78         vid_setpal(0, 256, cmap);
79
80         vid_vsync();
81
82         for(i=0; i<vm->height; i++) {
83                 for(j=0; j<vm->width; j++) {
84                         int xor = i^j;
85                         *fb++ = xor;
86                 }
87         }
88 }