+#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <GL/glut.h>
static void idle(void);
static void reshape(int x, int y);
static void keyb(unsigned char key, int x, int y);
+static int save_image(const char *fname, unsigned char *pix, int xsz, int ysz);
static long start_time;
+static int capture;
+static long cap_frame_interval = 67;
+static unsigned char *pixels;
int main(int argc, char **argv)
{
+ char *env;
unsigned int flags = GLUT_RGB | GLUT_DOUBLE;
+ glutInitWindowSize(1280, 800);
glutInit(&argc, argv);
if(app_parse_args(argc, argv) == -1) {
return 1;
}
#endif
- glutInitWindowSize(1280, 800);
glutInitDisplayMode(flags);
glutCreateWindow("census");
+ win_width = glutGet(GLUT_WINDOW_WIDTH);
+ win_height = glutGet(GLUT_WINDOW_HEIGHT);
+
if(fullscr) {
glutFullScreen();
glutSetCursor(GLUT_CURSOR_NONE);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyb);
+ if((env = getenv("CAPFPS")) && (cap_frame_interval = atoi(env)) > 0) {
+ cap_frame_interval = 1000 / cap_frame_interval;
+ capture = 1;
+ if(!(pixels = malloc(win_width * win_height * 3))) {
+ fprintf(stderr, "failed to allocate frame capture buffer\n");
+ return 1;
+ }
+ nloops = 1;
+ }
+
if(app_init() == -1) {
return 1;
}
static void display(void)
{
- msec = glutGet(GLUT_ELAPSED_TIME) - start_time;
+ if(capture) {
+ msec += cap_frame_interval;
+ } else {
+ msec = glutGet(GLUT_ELAPSED_TIME) - start_time;
+ }
app_display();
+ if(capture) {
+ static int frame;
+ char buf[64];
+
+ glReadPixels(0, 0, win_width, win_height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
+
+ sprintf(buf, "frm%03d.ppm", frame++);
+ save_image(buf, pixels, win_width, win_height);
+ }
+
glutSwapBuffers();
assert(glGetError() == GL_NO_ERROR);
}
glutReshapeWindow(saved_width, saved_height);
glutSetCursor(GLUT_CURSOR_INHERIT);
}
+
+static int save_image(const char *fname, unsigned char *pix, int xsz, int ysz)
+{
+ int i;
+ FILE *fp;
+
+ if(!(fp = fopen(fname, "wb"))) {
+ fprintf(stderr, "failed to open %s for writing\n", fname);
+ return -1;
+ }
+ fprintf(fp, "P6\n%d %d\n255\n", xsz, ysz);
+
+ for(i=0; i<xsz * ysz; i++) {
+ fputc(*pix++, fp);
+ fputc(*pix++, fp);
+ fputc(*pix++, fp);
+ }
+ fclose(fp);
+ return 0;
+}