rayzor

annotate src/main.cc @ 2:c273c6f799a4

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