rayzor
diff src/main.cc @ 6:a68dbf80d547
finally showing something ... :)
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 07 Apr 2014 06:04:11 +0300 |
parents | 5fcf72837b69 |
children | 75bc89c2abc4 |
line diff
1.1 --- a/src/main.cc Sun Apr 06 02:43:24 2014 +0300 1.2 +++ b/src/main.cc Mon Apr 07 06:04:11 2014 +0300 1.3 @@ -7,17 +7,22 @@ 1.4 #include "keyb.h" 1.5 #include "mouse.h" 1.6 #include "logger.h" 1.7 +#include "min3d.h" 1.8 +#include "scene.h" 1.9 1.10 +static bool init(); 1.11 +static void cleanup(); 1.12 static void display(); 1.13 static void swap_buffers(); 1.14 static void handle_keyboard(); 1.15 static void handle_mouse(); 1.16 static bool parse_args(int argc, char **argv); 1.17 1.18 -static int xsz = 800; 1.19 -static int ysz = 600; 1.20 +static int xsz = 640; 1.21 +static int ysz = 480; 1.22 static int bpp = 16; 1.23 static int bytespp; 1.24 +static bool novideo; 1.25 static unsigned char *fb; 1.26 static unsigned char *backbuf; 1.27 static int rbits, gbits, bbits; 1.28 @@ -26,31 +31,17 @@ 1.29 1.30 static bool quit; 1.31 1.32 +struct m3d_image rbuf; 1.33 +static Scene *scn; 1.34 + 1.35 int main(int argc, char **argv) 1.36 { 1.37 if(!parse_args(argc, argv)) { 1.38 return 1; 1.39 } 1.40 - if(kb_init(32) == -1) { 1.41 - fprintf(stderr, "failed to initialize keyboard driver\n"); 1.42 + if(!init()) { 1.43 return 1; 1.44 } 1.45 - if(!(fb = (unsigned char*)set_video_mode(xsz, ysz, bpp))) { 1.46 - set_text_mode(); 1.47 - fprintf(stderr, "failed to set video mode: %dx%d %dbpp\n", xsz, ysz, bpp); 1.48 - return 1; 1.49 - } 1.50 - bpp = get_color_depth(); 1.51 - get_color_bits(&rbits, &gbits, &bbits); 1.52 - get_color_shift(&rshift, &gshift, &bshift); 1.53 - get_color_mask(&rmask, &gmask, &bmask); 1.54 - bytespp = (int)ceil(bpp / 8.0); 1.55 - 1.56 - printlog("bpp: %d (%d %d %d)\n", bpp, rbits, gbits, bbits); 1.57 - printlog("shift: %d %d %d\n", rshift, gshift, bshift); 1.58 - printlog("mask: %x %x %x\n", rmask, gmask, bmask); 1.59 - 1.60 - backbuf = new unsigned char[xsz * ysz * 3]; 1.61 1.62 // main loop 1.63 for(;;) { 1.64 @@ -59,33 +50,96 @@ 1.65 if(quit) break; 1.66 1.67 display(); 1.68 + 1.69 + if(novideo) break; 1.70 } 1.71 1.72 - delete [] backbuf; 1.73 - 1.74 - set_text_mode(); 1.75 - kb_shutdown(); 1.76 - 1.77 + cleanup(); 1.78 printf("Thank you for using Rayzor!\n"); 1.79 return 0; 1.80 } 1.81 1.82 +static bool init() 1.83 +{ 1.84 + if(!novideo) { 1.85 + if(kb_init(32) == -1) { 1.86 + fprintf(stderr, "failed to initialize keyboard driver\n"); 1.87 + return false; 1.88 + } 1.89 + 1.90 + if(!(fb = (unsigned char*)set_video_mode(xsz, ysz, bpp))) { 1.91 + set_text_mode(); 1.92 + fprintf(stderr, "failed to set video mode: %dx%d %dbpp\n", xsz, ysz, bpp); 1.93 + return false; 1.94 + } 1.95 + bpp = get_color_depth(); 1.96 + get_color_bits(&rbits, &gbits, &bbits); 1.97 + get_color_shift(&rshift, &gshift, &bshift); 1.98 + get_color_mask(&rmask, &gmask, &bmask); 1.99 + bytespp = (int)ceil(bpp / 8.0); 1.100 + 1.101 + printlog("bpp: %d (%d %d %d)\n", bpp, rbits, gbits, bbits); 1.102 + printlog("shift: %d %d %d\n", rshift, gshift, bshift); 1.103 + printlog("mask: %x %x %x\n", rmask, gmask, bmask); 1.104 + } else { 1.105 + logger_output(stdout); 1.106 + printlog("novideo (debug) mode\n"); 1.107 + bpp = 24; 1.108 + rbits = gbits = bbits = 8; 1.109 + bytespp = 3; 1.110 + } 1.111 + 1.112 + backbuf = new unsigned char[xsz * ysz * 3]; 1.113 + if(!backbuf) { 1.114 + return false; 1.115 + } 1.116 + 1.117 + m3d_init(); 1.118 + rbuf.pixels = backbuf; 1.119 + rbuf.xsz = xsz; 1.120 + rbuf.ysz = ysz; 1.121 + m3d_set_buffers(&rbuf, 0); 1.122 + 1.123 + m3d_matrix_mode(M3D_PROJECTION); 1.124 + m3d_load_identity(); 1.125 + m3d_perspective(50.0, (float)xsz / (float)ysz, 0.5, 500.0); 1.126 + 1.127 + scn = new Scene; 1.128 + 1.129 + Sphere *sph = new Sphere; 1.130 + scn->add_object(sph); 1.131 + 1.132 + return true; 1.133 +} 1.134 + 1.135 +static void cleanup() 1.136 +{ 1.137 + delete scn; 1.138 + 1.139 + m3d_shutdown(); 1.140 + delete [] backbuf; 1.141 + 1.142 + if(!novideo) { 1.143 + set_text_mode(); 1.144 + kb_shutdown(); 1.145 + } 1.146 +} 1.147 + 1.148 + 1.149 static void display() 1.150 { 1.151 - unsigned char *fbptr = backbuf; 1.152 + m3d_clear(M3D_COLOR_BUFFER_BIT); 1.153 1.154 - for(int i=0; i<ysz; i++) { 1.155 - for(int j=0; j<xsz; j++) { 1.156 - bool chess = ((i / 16) & 1) == ((j / 16) & 1); 1.157 - fbptr[chess ? 0 : 2] = 255; 1.158 - fbptr[1] = 128; 1.159 - fbptr[chess ? 2 : 0] = 32; 1.160 - fbptr += 3; 1.161 - } 1.162 + m3d_matrix_mode(M3D_MODELVIEW); 1.163 + m3d_load_identity(); 1.164 + m3d_translate(0, 0, -10); 1.165 + 1.166 + scn->draw(); 1.167 + 1.168 + if(!novideo) { 1.169 + swap_buffers(); 1.170 + wait_vsync(); 1.171 } 1.172 - 1.173 - swap_buffers(); 1.174 - wait_vsync(); 1.175 } 1.176 1.177 #define PACK_RGB(r, g, b) \ 1.178 @@ -136,6 +190,8 @@ 1.179 { 1.180 int key; 1.181 1.182 + if(novideo) return; 1.183 + 1.184 while((key = kb_getkey()) != -1) { 1.185 switch(key) { 1.186 case 27: 1.187 @@ -155,6 +211,7 @@ 1.188 const char *desc; 1.189 } options[] = { 1.190 {'s', "size", "resolution <xres>x<yres>[:bpp]"}, 1.191 + {'n', "novid", "don't switch video mode (for debugging)"}, 1.192 {'h', "help", "print usage information and exit"}, 1.193 {-1, 0, 0} 1.194 }; 1.195 @@ -196,6 +253,10 @@ 1.196 } 1.197 break; 1.198 1.199 + case 'n': 1.200 + novideo = true; 1.201 + break; 1.202 + 1.203 case 'h': 1.204 print_usage(argv[0]); // doesn't return 1.205 break;