rayzor

annotate src/main.cc @ 0:2a5340a6eee4

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