-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopen_operation.cpp
136 lines (103 loc) · 3.14 KB
/
open_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
122
123
124
125
126
127
128
129
130
131
132
#include <cstdio>
#include "libtcod.hpp"
#include "gui/gui.hpp"
#include "app.h"
#include "message_box.h"
#include "open_operation.h"
OpenOperation::OpenOperation() {
}
OpenOperation::~OpenOperation() {
}
void OpenOperation::start() {
FILE *fp;
MessageBox msgBox("Open", "Enter filename to open", 2, true);
msgBox.show();
if(msgBox.getButtonPressed() == 1 && msgBox.getString() != NULL) {
app->filename = msgBox.getString();
fp = fopen(app->filename.c_str(), "r");
if(fp == NULL) {
printf("The file %s could not be opened.\n", app->filename.c_str());
MessageBox msgBox2("Error", "The file could not be opened", 1);
msgBox2.show();
} else {
if(app->filename.at(app->filename.length() - 1) == 'z') {
fclose(fp); // Don't need fp anymore
TCODZip zipFile;
zipFile.loadFromFile(app->filename.c_str());
const char *fileHeader = new char[100];
fileHeader = zipFile.getString();
float version;
sscanf(fileHeader, "ASCII-Paint v%g", &version);
app->canvasWidth = zipFile.getInt();
app->canvasHeight = zipFile.getInt();
app->initCanvas();
CanvasImage *img = new CanvasImage;
for(int x = 0; x < app->canvasWidth; x++) {
for(int y = 0; y < app->canvasHeight; y++) {
Brush b;
b.symbol = zipFile.getChar();
b.fore.r = zipFile.getChar();
b.fore.g = zipFile.getChar();
b.fore.b = zipFile.getChar();
b.back.r = zipFile.getChar();
b.back.g = zipFile.getChar();
b.back.b = zipFile.getChar();
// Solid and walkable added in version 0.3
if(version >= 0.3f) {
b.solid = zipFile.getChar();
b.walkable = zipFile.getChar();
} else {
// For older maps set the default solidness
b.solid = false;
b.walkable = true;
}
img->push_back(b);
}
}
app->setCanvasImage(*img);
} else {
float version;
fscanf(fp, "ASCII-Paint v%g", &version);
// Check if we have the required version of ASCII paint to open the file
if(version > app->mapVersion) {
printf("The file was made with version %g whereas we are running version %g\n",
version, app->mapVersion);
return;
}
fscanf(fp, "%i %i", &app->canvasWidth, &app->canvasHeight);
app->initCanvas();
CanvasImage *img = new CanvasImage;
// Scan until we hit #(the marker for the start of image data)
while(fgetc(fp) != '#');
for(int x = 0; x < app->canvasWidth; x++) {
for(int y = 0; y < app->canvasHeight; y++) {
Brush b;
b.symbol = fgetc(fp);
b.fore.r = fgetc(fp);
b.fore.g = fgetc(fp);
b.fore.b = fgetc(fp);
b.back.r = fgetc(fp);
b.back.g = fgetc(fp);
b.back.b = fgetc(fp);
// Solid and walkable added in version 0.3
if(version >= 0.3f) {
b.solid = fgetc(fp);
b.walkable = fgetc(fp);
} else {
// For older maps set the default solidness
b.solid = false;
b.walkable = true;
}
img->push_back(b);
}
}
app->setCanvasImage(*img);
}
}
}
app->changeOperation(app->previousOperation);
}
void OpenOperation::update() {
}
void OpenOperation::end() {
}