File: screenshot.c

package info (click to toggle)
gltron 0.61-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,948 kB
  • ctags: 1,175
  • sloc: ansic: 7,580; perl: 168; makefile: 149; sh: 15
file content (119 lines) | stat: -rw-r--r-- 2,631 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "gltron.h"

#include <png.h>

#include <stdio.h>
#include <stdlib.h>

int screenShot(char *filename, gDisplay *d);
int bmpScreenShot(char *filename, gDisplay *d);

void doScreenShot() {
  char buf[100];

#ifndef macintosh  
  sprintf(buf, "gltron-0.61-%d.png", screenshots);
  screenShot(buf, game->screen);
  screenshots++;

#endif
}

void doBmpScreenShot() {
  char buf[100];

  sprintf(buf, "gltron-0.61-%d.bmp", screenshots);
  bmpScreenShot(buf, game->screen);
  screenshots++;
}

FILE *fp;

void user_write_data(png_structp png_ptr,
		     png_bytep data, png_size_t length) {
  fwrite(data, length, 1, fp);
}

void user_flush_data(png_structp png_ptr) {
  fflush(fp);
}

int screenShot(char *filename, gDisplay *d) {
  unsigned char *data;

  png_structp png_ptr;
  png_infop info_ptr;
  png_byte **row_pointers;
  int colortype;
  int width, height;
  int i;

  width = d->w;
  height = d->h;
  data = malloc(width * height * 3);
  
  glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
  
  fp = fopen(filename, "wb");
  if(!fp) {
    fprintf(stderr, "can't open %s for writing\n", filename);
    return 1;
  }
  
  png_ptr = png_create_write_struct
    /*     (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
	   user_error_fn, user_warning_fn); */
    (PNG_LIBPNG_VER_STRING, 0, 0, 0);

  if (!png_ptr)
    return 1;

  info_ptr = png_create_info_struct(png_ptr);
  if (!info_ptr) {
      png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
      return 1;
  }
  /* png_init_io(png_ptr, fp); */
  png_set_write_fn(png_ptr, 0, user_write_data, user_flush_data);

  colortype = PNG_COLOR_TYPE_RGB;

  png_set_IHDR(png_ptr, info_ptr, width, height,
	       8, colortype, PNG_INTERLACE_NONE,
	       PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  png_write_info(png_ptr, info_ptr);

  /* get pointers */
  row_pointers = (png_byte**) malloc(height * sizeof(png_byte*));
  for(i = 0; i < height; i++)
    row_pointers[i] = data + (height - i - 1) 
      * 3 * width;

  png_write_image(png_ptr, row_pointers);
  png_write_end(png_ptr, info_ptr);
  png_destroy_write_struct(&png_ptr, &info_ptr);
  
  free(row_pointers);
  free(data);

  fprintf(stderr, "written screenshot to %s\n", filename);
  return 0;
}


int bmpScreenShot(char *filename, gDisplay *d) {
  unsigned char *data;
  int width, height;
    
  width = d->w;
  height = d->h;
  data = malloc(width * height * 3);
  glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
  SystemWriteBMP(filename, width, height, data);
  free(data);
  fprintf(stderr, "written screenshot to %s\n", filename);
  return 0;
}