erebus

diff src/main.cc @ 29:fb20e3855740

write the rendered image when pressing '`' and on exit.
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 06 Jun 2014 15:08:09 +0300
parents c8a6fb04fefa
children b1fc96c71bcc
line diff
     1.1 --- a/src/main.cc	Fri Jun 06 14:39:40 2014 +0300
     1.2 +++ b/src/main.cc	Fri Jun 06 15:08:09 2014 +0300
     1.3 @@ -1,8 +1,10 @@
     1.4  #include <stdio.h>
     1.5  #include <stdlib.h>
     1.6  #include <assert.h>
     1.7 +#include <signal.h>
     1.8  #include <vector>
     1.9  #include <chrono>
    1.10 +#include <imago2.h>
    1.11  #include "opengl.h"
    1.12  #include "erebus.h"
    1.13  
    1.14 @@ -16,6 +18,7 @@
    1.15  static void update_rect(int x, int y, int xsz, int ysz, float *pixels);
    1.16  static void idle();
    1.17  static void display();
    1.18 +static void save_image(const char *fname = 0);
    1.19  static void reshape(int x, int y);
    1.20  static void keyb(unsigned char key, int x, int y);
    1.21  static void keyb_up(unsigned char key, int x, int y);
    1.22 @@ -24,6 +27,7 @@
    1.23  static void sball_button(int bn, int st);
    1.24  static void sball_motion(int x, int y, int z);
    1.25  static int next_pow2(int x);
    1.26 +static void sighandler(int s);
    1.27  
    1.28  static int width, height, rtex_width, rtex_height;
    1.29  static unsigned int rtex;
    1.30 @@ -58,6 +62,11 @@
    1.31  		return 1;
    1.32  	}
    1.33  	atexit(cleanup);
    1.34 +	signal(SIGINT, sighandler);
    1.35 +	signal(SIGSEGV, sighandler);
    1.36 +	signal(SIGILL, sighandler);
    1.37 +	signal(SIGTERM, sighandler);
    1.38 +	signal(SIGFPE, sighandler);
    1.39  
    1.40  	glutMainLoop();
    1.41  }
    1.42 @@ -93,6 +102,7 @@
    1.43  
    1.44  static void cleanup()
    1.45  {
    1.46 +	save_image("final.png");
    1.47  	erb_destroy(erb);
    1.48  }
    1.49  
    1.50 @@ -172,6 +182,15 @@
    1.51  	assert(glGetError() == GL_NO_ERROR);
    1.52  }
    1.53  
    1.54 +static void save_image(const char *fname)
    1.55 +{
    1.56 +	float *fb = erb_get_framebuffer(erb);
    1.57 +
    1.58 +	if(img_save_pixels(fname ? fname : "output.png", fb, width, height, IMG_FMT_RGBAF) == -1) {
    1.59 +		fprintf(stderr, "failed to save image\n");
    1.60 +	}
    1.61 +}
    1.62 +
    1.63  static void reshape(int x, int y)
    1.64  {
    1.65  	glViewport(0, 0, x, y);
    1.66 @@ -193,6 +212,11 @@
    1.67  		glutIdleFunc(idle);
    1.68  		erb_begin_frame(erb, 0);
    1.69  		break;
    1.70 +
    1.71 +	case '`':
    1.72 +		printf("saving image.\n");
    1.73 +		save_image();
    1.74 +		break;
    1.75  	}
    1.76  
    1.77  	if(erb_input_keyboard(erb, key, true)) {
    1.78 @@ -243,3 +267,8 @@
    1.79  	}
    1.80  	return res;
    1.81  }
    1.82 +
    1.83 +static void sighandler(int s)
    1.84 +{
    1.85 +	exit(0);
    1.86 +}