rayzor
view src/main.cc @ 5:5fcf72837b69
fixed the dosemu bit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 06 Apr 2014 02:43:24 +0300 |
parents | a826bf0fb169 |
children | a68dbf80d547 |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include "inttypes.h"
6 #include "gfx.h"
7 #include "keyb.h"
8 #include "mouse.h"
9 #include "logger.h"
11 static void display();
12 static void swap_buffers();
13 static void handle_keyboard();
14 static void handle_mouse();
15 static bool parse_args(int argc, char **argv);
17 static int xsz = 800;
18 static int ysz = 600;
19 static int bpp = 16;
20 static int bytespp;
21 static unsigned char *fb;
22 static unsigned char *backbuf;
23 static int rbits, gbits, bbits;
24 static int rshift, gshift, bshift;
25 static unsigned int rmask, gmask, bmask;
27 static bool quit;
29 int main(int argc, char **argv)
30 {
31 if(!parse_args(argc, argv)) {
32 return 1;
33 }
34 if(kb_init(32) == -1) {
35 fprintf(stderr, "failed to initialize keyboard driver\n");
36 return 1;
37 }
38 if(!(fb = (unsigned char*)set_video_mode(xsz, ysz, bpp))) {
39 set_text_mode();
40 fprintf(stderr, "failed to set video mode: %dx%d %dbpp\n", xsz, ysz, bpp);
41 return 1;
42 }
43 bpp = get_color_depth();
44 get_color_bits(&rbits, &gbits, &bbits);
45 get_color_shift(&rshift, &gshift, &bshift);
46 get_color_mask(&rmask, &gmask, &bmask);
47 bytespp = (int)ceil(bpp / 8.0);
49 printlog("bpp: %d (%d %d %d)\n", bpp, rbits, gbits, bbits);
50 printlog("shift: %d %d %d\n", rshift, gshift, bshift);
51 printlog("mask: %x %x %x\n", rmask, gmask, bmask);
53 backbuf = new unsigned char[xsz * ysz * 3];
55 // main loop
56 for(;;) {
57 handle_keyboard();
58 handle_mouse();
59 if(quit) break;
61 display();
62 }
64 delete [] backbuf;
66 set_text_mode();
67 kb_shutdown();
69 printf("Thank you for using Rayzor!\n");
70 return 0;
71 }
73 static void display()
74 {
75 unsigned char *fbptr = backbuf;
77 for(int i=0; i<ysz; i++) {
78 for(int j=0; j<xsz; j++) {
79 bool chess = ((i / 16) & 1) == ((j / 16) & 1);
80 fbptr[chess ? 0 : 2] = 255;
81 fbptr[1] = 128;
82 fbptr[chess ? 2 : 0] = 32;
83 fbptr += 3;
84 }
85 }
87 swap_buffers();
88 wait_vsync();
89 }
91 #define PACK_RGB(r, g, b) \
92 ((((r) << rshift) & rmask) | \
93 (((g) << gshift) & gmask) | \
94 (((b) << bshift) & bmask))
96 static void swap_buffers()
97 {
98 unsigned char *src = backbuf;
99 int num_pixels = xsz * ysz;
101 switch(bpp) {
102 case 32:
103 {
104 uint32_t *dest = (uint32_t*)fb;
105 for(int i=0; i<num_pixels; i++) {
106 *dest++ = PACK_RGB(src[0], src[1], src[2]);
107 src += 3;
108 }
109 }
110 break;
112 case 24:
113 memcpy(fb, backbuf, num_pixels * 3);
114 break;
116 case 16:
117 case 15:
118 {
119 int srs = 8 - rbits;
120 int sgs = 8 - gbits;
121 int sbs = 8 - bbits;
122 uint16_t *dest = (uint16_t*)fb;
123 for(int i=0; i<num_pixels; i++) {
124 *dest++ = PACK_RGB(src[0] >> srs, src[1] >> sgs, src[2] >> sbs);
125 src += 3;
126 }
127 }
128 break;
130 default:
131 break;
132 }
133 }
135 static void handle_keyboard()
136 {
137 int key;
139 while((key = kb_getkey()) != -1) {
140 switch(key) {
141 case 27:
142 quit = true;
143 return;
144 }
145 }
146 }
148 static void handle_mouse()
149 {
150 }
152 static struct {
153 int opt;
154 const char *lopt;
155 const char *desc;
156 } options[] = {
157 {'s', "size", "resolution <xres>x<yres>[:bpp]"},
158 {'h', "help", "print usage information and exit"},
159 {-1, 0, 0}
160 };
162 static void print_usage(const char *argv0)
163 {
164 printf("%s usage\n", argv0);
165 for(int i=0; options[i].opt != -1; i++) {
166 printf(" -%c, -%s: %s\n", options[i].opt, options[i].lopt, options[i].desc);
167 }
168 exit(0);
169 }
171 static bool parse_args(int argc, char **argv)
172 {
173 for(int i=1; i<argc; i++) {
174 if(argv[i][0] == '-') {
175 int opt = -1;
177 for(int j=0; options[j].opt != -1; j++) {
178 if(argv[i][2] == 0) {
179 if(argv[i][1] == options[j].opt) {
180 opt = options[j].opt;
181 break;
182 }
183 } else {
184 if(strcmp(argv[i] + 1, options[j].lopt) == 0) {
185 opt = options[j].opt;
186 break;
187 }
188 }
189 }
191 switch(opt) {
192 case 's':
193 if(sscanf(argv[++i], "%dx%d:%d", &xsz, &ysz, &bpp) < 2) {
194 fprintf(stderr, "%s must be followed by a resolution: WxH\n", argv[i - 1]);
195 return false;
196 }
197 break;
199 case 'h':
200 print_usage(argv[0]); // doesn't return
201 break;
203 default:
204 fprintf(stderr, "unknown option: %s\n", argv[i]);
205 return false;
206 }
207 } else {
208 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
209 return false;
210 }
211 }
212 return true;
213 }