mightymandel v16

GPU-based Mandelbrot set explorer

record.c
Go to the documentation of this file.
1 // mightymandel -- GPU-based Mandelbrot Set explorer
2 // Copyright (C) 2012,2013,2014,2015 Claude Heiland-Allen
3 // License GPL3+ http://www.gnu.org/licenses/gpl.html
4 
5 #include <stdbool.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <GL/glew.h>
10 
11 #include "record.h"
12 #include "logging.h"
13 #include "texture.h"
14 
15 void record_begin(struct record *s) {
16  s->buffer = 0;
17  s->bytes = 0;
18 }
19 
20 void record_end(struct record *s) {
21  if (s->buffer) {
22  free(s->buffer);
23  s->buffer = 0;
24  s->bytes = 0;
25  }
26 }
27 
28 void record_do(struct record *s, const char *name, int width, int height, const char *comment) {
29  if (! s->buffer) {
30  s->bytes = 3 * width * height;
31  s->buffer = (unsigned char *) malloc(s->bytes);
32  }
33  glActiveTexture(GL_TEXTURE0 + TEX_RGB);
34  glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, s->buffer);
35  bool ok = true;
36  FILE *out = fopen(name, "wb");
37  if (! out) {
38  log_message(LOG_ERROR, "couldn't open output file: %s\n", name);
39  ok = false;
40  } else {
41  fprintf(out, "P6\n");
42  fprintf(out, "%s", comment);
43  fprintf(out, "%d %d\n255\n", width, height);
44  fflush(out);
45  // PPM is rows top to bottom, OpenGL is rows bottom to top
46  for (int y = height - 1; y >= 0; --y) {
47  if (1 != fwrite(s->buffer + y * width * 3, width * 3, 1, out)) {
48  log_message(LOG_ERROR, "error writing to file: %s\n", name);
49  ok = false;
50  break;
51  }
52  }
53  fflush(out);
54  fclose(out);
55  }
56  if (ok) {
57  log_message(LOG_NOTICE, "saved: %s\n", name);
58  }
59 }