rayzor

diff src/main.cc @ 14:a9a948809c6f

starting the renderer screen, plus misc stuff
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 13 Apr 2014 08:06:21 +0300
parents d94a69933a71
children 79609d482762
line diff
     1.1 --- a/src/main.cc	Sat Apr 12 23:37:55 2014 +0300
     1.2 +++ b/src/main.cc	Sun Apr 13 08:06:21 2014 +0300
     1.3 @@ -2,6 +2,8 @@
     1.4  #include <stdlib.h>
     1.5  #include <string.h>
     1.6  #include <signal.h>
     1.7 +#include <errno.h>
     1.8 +#include <direct.h>
     1.9  #include "inttypes.h"
    1.10  #include "gfx.h"
    1.11  #include "keyb.h"
    1.12 @@ -29,6 +31,7 @@
    1.13  static void display();
    1.14  static void swap_buffers();
    1.15  static void draw_cursor(uint32_t *buf, int mx, int my);
    1.16 +static void screenshot();
    1.17  static void handle_keyboard();
    1.18  static void handle_mouse();
    1.19  static bool parse_args(int argc, char **argv);
    1.20 @@ -50,6 +53,7 @@
    1.21  static bool use_mouse;
    1.22  static int mouse_x, mouse_y;
    1.23  static bool quit;
    1.24 +static bool cap_shot;
    1.25  
    1.26  int main(int argc, char **argv)
    1.27  {
    1.28 @@ -188,6 +192,11 @@
    1.29  		draw_cursor(fb_pixels, mouse_x, mouse_y);
    1.30  	}
    1.31  
    1.32 +	if(cap_shot) {
    1.33 +		screenshot();
    1.34 +		cap_shot = false;
    1.35 +	}
    1.36 +
    1.37  	if(!novideo) {
    1.38  		wait_vsync();
    1.39  #ifdef USE_ASM_SWAPBUF
    1.40 @@ -279,6 +288,51 @@
    1.41  	}
    1.42  }
    1.43  
    1.44 +#define PPM_COMMENT	"screenshot saved by the rayzor modeller/renderer"
    1.45 +static void screenshot()
    1.46 +{
    1.47 +	static int shotidx = -1;
    1.48 +	FILE *fp;
    1.49 +	char fname[PATH_MAX];
    1.50 +
    1.51 +	if(shotidx == -1) {
    1.52 +		DIR *dir;
    1.53 +		struct dirent *dent;
    1.54 +
    1.55 +		shotidx = 0;
    1.56 +		if((dir = opendir("."))) {
    1.57 +			while((dent = readdir(dir))) {
    1.58 +				int i, num;
    1.59 +				for(i=0; dent->d_name[i]; i++) {
    1.60 +					fname[i] = tolower(dent->d_name[i]);
    1.61 +				}
    1.62 +				fname[i] = 0;
    1.63 +
    1.64 +				if(sscanf(fname, "shot%d.ppm", &num) == 1 && num > shotidx) {
    1.65 +					shotidx = num;
    1.66 +				}
    1.67 +			}
    1.68 +			closedir(dir);
    1.69 +		}
    1.70 +	}
    1.71 +
    1.72 +	sprintf(fname, "shot%04d.ppm", ++shotidx);
    1.73 +	if(!(fp = fopen(fname, "wb"))) {
    1.74 +		printlog("failed to save screenshot %s: %s\n", fname, strerror(errno));
    1.75 +		return;
    1.76 +	}
    1.77 +
    1.78 +	fprintf(fp, "P6\n" PPM_COMMENT "\n%d %d\n255\n", fb_width, fb_height);
    1.79 +	for(int i=0; i<fb_width * fb_height; i++) {
    1.80 +		uint32_t c = fb_pixels[i];
    1.81 +		fputc(UNPACK_RED(c), fp);
    1.82 +		fputc(UNPACK_GREEN(c), fp);
    1.83 +		fputc(UNPACK_BLUE(c), fp);
    1.84 +	}
    1.85 +
    1.86 +	fclose(fp);
    1.87 +}
    1.88 +
    1.89  static void handle_keyboard()
    1.90  {
    1.91  	int key;
    1.92 @@ -299,6 +353,11 @@
    1.93  			}
    1.94  			break;
    1.95  
    1.96 +		case KB_SYSRQ:
    1.97 +		case KB_F12:
    1.98 +			cap_shot = true;
    1.99 +			break;
   1.100 +
   1.101  		default:
   1.102  			break;
   1.103  		}