summaryrefslogtreecommitdiff
path: root/meme/screen.c
blob: b9b570f507ba47805d3a3393ce98f13c5cba05a3 (plain)
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
/*
 * screen.c - Screen dump
 *
 * Written 2015-2016 by Werner Almesberger
 * Copyright 2015-2016 Werner Almesberger
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */


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

#include "SDL.h"

#include "screen.h"


#define	BASE_NAME	"meme-"


const char *screen_dump_name = NULL;


void dump_screen_file(SDL_Surface *s, const char *name)
{
	if (SDL_SaveBMP(s, name)) {
		fprintf(stderr, "SDL_SaveBMP: %s\n", SDL_GetError());
		exit(1);
	}
	fprintf(stderr, "Screen dump to %s\n", name);
}


static void dump_screen_unique(SDL_Surface *s)
{
	char tmp[sizeof(BASE_NAME) + 4 + 4];
	int n;

	for (n = 0; n <= 9999; n++) {
		snprintf(tmp, sizeof(tmp), BASE_NAME "%04d.bmp", n);
		if (access(tmp, R_OK)) {
			dump_screen_file(s, tmp);
			return;
		}
	}
	fprintf(stderr, "dump_screen: no available name\n");
	exit(1);
}


void dump_screen(SDL_Surface *s)
{
	if (screen_dump_name)
		dump_screen_file(s, screen_dump_name);
	else
		dump_screen_unique(s);
}