-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave_operation.cpp
122 lines (94 loc) · 2.9 KB
/
save_operation.cpp
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
120
121
#include <cstdio>
#include <cstring>
#include "libtcod.hpp"
#include "gui/gui.hpp"
#include "app.h"
#include "message_box.h"
#include "save_operation.h"
SaveOperation::SaveOperation() {
}
SaveOperation::~SaveOperation() {
}
void SaveOperation::start() {
doSave(app);
app->changeOperation(app->previousOperation);
}
void SaveOperation::update() {
}
void SaveOperation::end() {
}
bool doSave(App *app) {
FILE *fp;
MessageBox msgBox("Save", "Enter filename to save to", 2, true);
if(!app->filename.empty())
msgBox.setString(app->filename.c_str());
msgBox.show();
// If user presses ESCAPE or cancel
if(msgBox.getButtonPressed() == 0 || msgBox.getButtonPressed() == 2) {
return false;
}
if(msgBox.getButtonPressed() == 1 && msgBox.getString() != NULL) {
app->filename = msgBox.getString();
fp = fopen(app->filename.c_str(), "w");
if(fp == NULL) {
printf("The file %s could not be saved.\n", app->filename.c_str());
MessageBox msgBox2("Error", "The file could not be saved", 1);
msgBox2.show();
return false;
} else {
// If the filename ends with a z (for example test.ascz) then use the compressed format
if(app->filename.at(app->filename.length() - 1) == 'z') {
fclose(fp); // Don't need fp anymore
TCODZip zipFile;
char *fileHeader = new char[100];
sprintf(fileHeader, "ASCII-Paint v%g\n", app->mapVersion);
zipFile.putString(fileHeader);
zipFile.putInt(app->canvasWidth);
zipFile.putInt(app->canvasHeight);
// Get image from canvas
CanvasImage *img = app->getCanvasImage();
// Write the brush data for every brush in the image
for(int x = 0; x < app->canvasWidth; x++) {
for(int y = 0; y < app->canvasHeight; y++) {
Brush b = (*img)[x * app->canvasHeight + y];
zipFile.putChar(b.symbol);
zipFile.putChar(b.fore.r);
zipFile.putChar(b.fore.g);
zipFile.putChar(b.fore.b);
zipFile.putChar(b.back.r);
zipFile.putChar(b.back.g);
zipFile.putChar(b.back.b);
zipFile.putChar(b.solid);
zipFile.putChar(b.walkable);
}
}
zipFile.saveToFile(app->filename.c_str());
} else {
fprintf(fp, "ASCII-Paint v%g\n", app->mapVersion);
fprintf(fp, "%i %i\n", app->canvasWidth, app->canvasHeight);
// Use the # character as a marker for the start of image data
fputc('#', fp);
// Get image from canvas
CanvasImage *img = app->getCanvasImage();
// Write the brush data for every brush in the image
for(int x = 0; x < app->canvasWidth; x++) {
for(int y = 0; y < app->canvasHeight; y++) {
Brush b = (*img)[x * app->canvasHeight + y];
fputc(b.symbol, fp);
fputc(b.fore.r, fp);
fputc(b.fore.g, fp);
fputc(b.fore.b, fp);
fputc(b.back.r, fp);
fputc(b.back.g, fp);
fputc(b.back.b, fp);
fputc(b.solid, fp);
fputc(b.walkable, fp);
}
}
fclose(fp);
}
app->canvasModified = false;
}
}
return true;
}