fractorb
changeset 2:03e8b9a5031d
screenshot function
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 20 Nov 2017 03:53:02 +0200 |
parents | 436f82447c44 |
children | f440ecffc45a |
files | src/main.c |
diffstat | 1 files changed, 53 insertions(+), 0 deletions(-) [+] |
line diff
1.1 --- a/src/main.c Sun Nov 19 00:57:39 2017 +0200 1.2 +++ b/src/main.c Mon Nov 20 03:53:02 2017 +0200 1.3 @@ -1,6 +1,9 @@ 1.4 #include <stdio.h> 1.5 #include <stdlib.h> 1.6 +#include <string.h> 1.7 #include <math.h> 1.8 +#include <errno.h> 1.9 +#include <dirent.h> 1.10 #include <GL/glew.h> 1.11 #include <GL/glut.h> 1.12 #include "sdr.h" 1.13 @@ -13,6 +16,7 @@ 1.14 void keyup(unsigned char key, int x, int y); 1.15 void mouse(int bn, int st, int x, int y); 1.16 void motion(int x, int y); 1.17 +void screenshot(void); 1.18 1.19 static int win_width = 1280, win_height = 800; 1.20 static float win_aspect; 1.21 @@ -127,6 +131,10 @@ 1.22 } 1.23 break; 1.24 1.25 + case '`': 1.26 + screenshot(); 1.27 + break; 1.28 + 1.29 default: 1.30 break; 1.31 } 1.32 @@ -173,3 +181,48 @@ 1.33 glutPostRedisplay(); 1.34 } 1.35 } 1.36 + 1.37 +void screenshot(void) 1.38 +{ 1.39 + FILE *fp; 1.40 + DIR *dir; 1.41 + struct dirent *dent; 1.42 + unsigned char *img; 1.43 + static int shotnum = -1; 1.44 + char fname[128]; 1.45 + 1.46 + if(shotnum == -1) { 1.47 + shotnum = 1; 1.48 + if((dir = opendir("."))) { 1.49 + while((dent = readdir(dir))) { 1.50 + int num; 1.51 + if(sscanf(dent->d_name, "img%d.ppm", &num) == 1) { 1.52 + if(num >= shotnum) shotnum = num + 1; 1.53 + } 1.54 + } 1.55 + closedir(dir); 1.56 + } 1.57 + } else { 1.58 + ++shotnum; 1.59 + } 1.60 + 1.61 + sprintf(fname, "img%04d.ppm", shotnum > 0 ? shotnum : 0); 1.62 + if(!(fp = fopen(fname, "wb"))) { 1.63 + fprintf(stderr, "failed to open %s for writing: %s\n", fname, strerror(errno)); 1.64 + return; 1.65 + } 1.66 + 1.67 + if(!(img = malloc(win_width * win_height * 3))) { 1.68 + fprintf(stderr, "failed to allocate screenshot buffer\n"); 1.69 + fclose(fp); 1.70 + return; 1.71 + } 1.72 + 1.73 + glReadPixels(0, 0, win_width, win_height, GL_RGB, GL_UNSIGNED_BYTE, img); 1.74 + 1.75 + fprintf(fp, "P6\n%d %d\n255\n", win_width, win_height); 1.76 + fwrite(img, 1, win_width * win_height * 3, fp); 1.77 + fclose(fp); 1.78 + free(img); 1.79 + return; 1.80 +}