-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasc_file.cpp
169 lines (130 loc) · 3.1 KB
/
asc_file.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <cstdio>
#include "asc_file.hpp"
AscFile::AscFile() {
}
AscFile::~AscFile() {
}
int AscFile::load(char *filename) {
FILE *fp;
fp = fopen(filename, "r");
if(fp == NULL) {
return -1;
}
float version;
fscanf(fp, "ASCII-Paint v%g", &version);
fscanf(fp, "%i %i", &width, &height);
setSize(width, height);
// Scan until we hit #(the marker for the start of image data)
while(fgetc(fp) != '#');
for(int x = 0; x < width; x++) {
for(int y = 0; y < height; y++) {
char c = fgetc(fp);
int fr = fgetc(fp);
int fg = fgetc(fp);
int fb = fgetc(fp);
int br = fgetc(fp);
int bg = fgetc(fp);
int bb = fgetc(fp);
int solid = fgetc(fp);
int walkable = fgetc(fp);
setChar(x, y, c);
setFore(x, y, fr, fg, fb);
setBack(x, y, br, bg, bb);
setSolid(x, y, solid);
setSolid(x, y, walkable);
}
}
fclose(fp);
return 1;
}
int AscFile::save(char *filename) {
FILE *fp;
fp = fopen(filename, "w");
if(fp == NULL)
return 0;
float version = 0.1;
fprintf(fp, "ASCII-Paint v%g\n", version);
fprintf(fp, "%i %i\n", width, height);
// Use the # character as a marker for the start of image data
fputc('#', fp);
// Write the brush data for every brush in the image
for(int x = 0; x < width; x++) {
for(int y = 0; y < height; y++) {
char c = getChar(x, y);
AscRgb f = getFore(x, y);
AscRgb b = getBack(x, y);
fputc(c, fp);
fputc(f.r, fp);
fputc(f.g, fp);
fputc(f.b, fp);
fputc(b.r, fp);
fputc(b.g, fp);
fputc(b.b, fp);
}
}
fclose(fp);
return 1;
}
int AscFile::getWidth() {
return width;
}
int AscFile::getHeight() {
return height;
}
void AscFile::setSize(int w, int h) {
width = w;
height = h;
fore = new AscRgb[width * height];
back = new AscRgb[width * height];
chars = new unsigned char[width * height];
solid = new bool[width * height];
walkable = new bool[width * height];
}
void AscFile::setChar(int x, int y, unsigned char chr) {
chars[x * height + y] = chr;
}
void AscFile::setFore(int x, int y, int r, int g, int b) {
fore[x * height + y].r = r;
fore[x * height + y].g = g;
fore[x * height + y].b = b;
}
void AscFile::setFore(int x, int y, AscRgb f) {
setFore(x, y, f.r, f.g, f.b);
}
void AscFile::setBack(int x, int y, int r, int g, int b) {
back[x * height + y].r = r;
back[x * height + y].g = g;
back[x * height + y].b = b;
}
void AscFile::setBack(int x, int y, AscRgb b) {
setFore(x, y, b.r, b.g, b.b);
}
void AscFile::setSolid(int x, int y, bool s) {
solid[x * height + y] = s;
}
void AscFile::setWalkable(int x, int y, bool w) {
solid[x * height + y] = w;
}
unsigned char AscFile::getChar(int x, int y) {
return chars[x * height + y];
}
AscRgb AscFile::getFore(int x, int y) {
AscRgb rgb;
rgb.r = fore[x * height + y].r;
rgb.g = fore[x * height + y].g;
rgb.b = fore[x * height + y].b;
return rgb;
}
AscRgb AscFile::getBack(int x, int y) {
AscRgb rgb;
rgb.r = back[x * height + y].r;
rgb.g = back[x * height + y].g;
rgb.b = back[x * height + y].b;
return rgb;
}
bool AscFile::getSolid(int x, int y) {
return solid[x * height + y];
}
bool AscFile::getWalkable(int x, int y) {
return walkable[x * height + y];
}