rayzor
view src/main.cc @ 4:18bdbcbaee81
forgot to add stl/vector.h
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 06 Apr 2014 01:12:50 +0300 |
parents | 2a5340a6eee4 |
children | 5fcf72837b69 |
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 }
90 #define PACK_RGB(r, g, b) \
91 ((((r) << rshift) & rmask) | \
92 (((g) << gshift) & gmask) | \
93 (((b) << bshift) & bmask))
95 static void swap_buffers()
96 {
97 unsigned char *src = backbuf;
98 int num_pixels = xsz * ysz;
100 switch(bpp) {
101 case 32:
102 {
103 uint32_t *dest = (uint32_t*)fb;
104 for(int i=0; i<num_pixels; i++) {
105 *dest++ = PACK_RGB(src[0], src[1], src[2]);
106 src += 3;
107 }
108 }
109 break;
111 case 24:
112 memcpy(fb, backbuf, num_pixels * 3);
113 break;
115 case 16:
116 case 15:
117 {
118 int srs = 8 - rbits;
119 int sgs = 8 - gbits;
120 int sbs = 8 - bbits;
121 uint16_t *dest = (uint16_t*)fb;
122 for(int i=0; i<num_pixels; i++) {
123 *dest++ = PACK_RGB(src[0] >> srs, src[1] >> sgs, src[2] >> sbs);
124 src += 3;
125 }
126 }
127 break;
129 default:
130 break;
131 }
132 }
134 static void handle_keyboard()
135 {
136 if(!kb_isdown(KB_ANY))
137 return;
139 int c = kb_getkey();
140 switch(c) {
141 case 27:
142 quit = true;
143 return;
144 }
145 }
147 static void handle_mouse()
148 {
149 }
151 static struct {
152 int opt;
153 const char *lopt;
154 const char *desc;
155 } options[] = {
156 {'s', "size", "resolution <xres>x<yres>[:bpp]"},
157 {'h', "help", "print usage information and exit"},
158 {-1, 0, 0}
159 };
161 static void print_usage(const char *argv0)
162 {
163 printf("%s usage\n", argv0);
164 for(int i=0; options[i].opt != -1; i++) {
165 printf(" -%c, -%s: %s\n", options[i].opt, options[i].lopt, options[i].desc);
166 }
167 exit(0);
168 }
170 static bool parse_args(int argc, char **argv)
171 {
172 for(int i=1; i<argc; i++) {
173 if(argv[i][0] == '-') {
174 int opt = -1;
176 for(int j=0; options[j].opt != -1; j++) {
177 if(argv[i][2] == 0) {
178 if(argv[i][1] == options[j].opt) {
179 opt = options[j].opt;
180 break;
181 }
182 } else {
183 if(strcmp(argv[i] + 1, options[j].lopt) == 0) {
184 opt = options[j].opt;
185 break;
186 }
187 }
188 }
190 switch(opt) {
191 case 's':
192 if(sscanf(argv[++i], "%dx%d:%d", &xsz, &ysz, &bpp) < 2) {
193 fprintf(stderr, "%s must be followed by a resolution: WxH\n", argv[i - 1]);
194 return false;
195 }
196 break;
198 case 'h':
199 print_usage(argv[0]); // doesn't return
200 break;
202 default:
203 fprintf(stderr, "unknown option: %s\n", argv[i]);
204 return false;
205 }
206 } else {
207 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
208 return false;
209 }
210 }
211 return true;
212 }