-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathimage.c
77 lines (67 loc) · 2.34 KB
/
image.c
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
/* -*- c -*-
* Copyright (C) 1999 Gus Hartmann & Peter Keller
* Copyright (C) 2024 Ron Wills <ron@digitalcombine.ca>
*
* 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.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.*
*/
#include "sweep.h"
#include <time.h>
/*******************
* game_save_image *
*******************/
void game_save_image(game_stats_t* game) {
int width, height;
unsigned char value;
FILE* fp;
char filename[40];
time_t now;
time(&now);
strftime(filename, 39, "sweep-%F-%R.ppm", localtime(&now));
if ((fp = fopen(filename, "w")) == NULL) {
log_error("Unable to save game image to %s", filename);
return;
}
log_status("Saving game image to %s", filename);
/* dump the stats out */
fprintf(fp, "P6\n%d\n%d\n255\n", game->width, game->height);
for (height = 0; height < game->height; height++) {
for (width = 0; width < game->width; width++) {
if ((abs(width - game->CursorX) < 2) &&
(abs(game->CursorY - height) < 10)) {
fwrite("\xff\x00\x00", 1, 3, fp);
} else if ((abs(width - game->CursorX) < 10) &&
(abs(game->CursorY - height) < 2)) {
fwrite("\xff\x00\x00", 1, 3, fp);
} else {
value = game_get_mine(game, width, height);
switch ( value ) {
case UNKNOWN: case MINE:
fwrite("\x00\x00\x00", 1, 3, fp);
break;
case MARKED: case BAD_MARK:
fwrite("\x00\x00\xff", 1, 3, fp);
break;
case DETONATED:
fwrite("\xff\x00\x00", 1, 3, fp);
break;
case EMPTY: default:
fwrite("\xff\xff\xff", 1, 3, fp);
break;
}
}
}
}
fclose(fp);
}