Skip to content

Commit b69f0ca

Browse files
committed
updated mapedit with changes to map, mapach and states
1 parent 2485c36 commit b69f0ca

File tree

7 files changed

+617
-100
lines changed

7 files changed

+617
-100
lines changed

src/map.cpp

Lines changed: 202 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
1-
#include "map.h"
1+
/*
2+
cs3-runtime-sdl
3+
Copyright (C) 2024 Francois Blanchette
4+
5+
This program is free software: you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation, either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
218
#include <cstring>
319
#include <cstdio>
420
#include <algorithm>
21+
#include "map.h"
22+
#include "shared/IFile.h"
23+
#include "states.h"
524

6-
static const char SIG[] = "MAPZ";
7-
static const char XTR_SIG[] = "XTR";
25+
static const char SIG[]{'M', 'A', 'P', 'Z'};
26+
static const char XTR_SIG[]{"XTR"};
827
static const char XTR_VER = 0;
928
static const uint16_t VERSION = 0;
1029
static const uint16_t MAX_SIZE = 256;
1130
static const uint16_t MAX_TITLE = 255;
1231

13-
typedef struct {
32+
typedef struct
33+
{
1434
char sig[3];
1535
char ver;
1636
} extrahdr_t;
@@ -28,11 +48,13 @@ CMap::CMap(uint16_t len, uint16_t hei, uint8_t t)
2848
m_size = m_len * m_hei;
2949
memset(m_map, t, m_size * sizeof(m_map[0]));
3050
}
51+
m_states = new CStates;
3152
};
3253

3354
CMap::~CMap()
3455
{
3556
forget();
57+
delete m_states;
3658
};
3759

3860
uint8_t &CMap::at(int x, int y)
@@ -60,6 +82,7 @@ void CMap::set(int x, int y, uint8_t t)
6082

6183
void CMap::forget()
6284
{
85+
m_states->clear();
6386
if (m_map != nullptr)
6487
{
6588
delete[] m_map;
@@ -96,77 +119,174 @@ bool CMap::write(const char *fname)
96119
return tfile != nullptr && result;
97120
}
98121

99-
bool CMap::read(FILE *sfile)
122+
bool CMap::read(IFile &file)
100123
{
101-
if (sfile)
124+
auto readfile = [&file](auto ptr, auto size)
125+
{
126+
return file.read(ptr, size);
127+
};
128+
129+
char sig[4];
130+
131+
readfile(sig, sizeof(SIG));
132+
if (memcmp(sig, SIG, sizeof(SIG)) != 0)
133+
{
134+
m_lastError = "signature mismatch";
135+
printf("%s\n", m_lastError.c_str());
136+
return false;
137+
}
138+
uint16_t ver = 0;
139+
readfile(&ver, sizeof(VERSION));
140+
if (ver > VERSION)
141+
{
142+
m_lastError = "bad version";
143+
printf("%s\n", m_lastError.c_str());
144+
return false;
145+
}
146+
uint16_t len = 0;
147+
uint16_t hei = 0;
148+
readfile(&len, sizeof(uint8_t));
149+
readfile(&hei, sizeof(uint8_t));
150+
len = len ? len : MAX_SIZE;
151+
hei = hei ? hei : MAX_SIZE;
152+
resize(len, hei, true);
153+
readfile(m_map, len * hei);
154+
m_attrs.clear();
155+
uint16_t attrCount = 0;
156+
readfile(&attrCount, sizeof(attrCount));
157+
for (int i = 0; i < attrCount; ++i)
102158
{
103-
char sig[4];
104-
fread(sig, strlen(SIG), 1, sfile);
105-
if (memcmp(sig, SIG, strlen(SIG)) != 0)
159+
uint8_t x;
160+
uint8_t y;
161+
uint8_t a;
162+
readfile(&x, sizeof(x));
163+
readfile(&y, sizeof(y));
164+
readfile(&a, sizeof(a));
165+
setAttr(x, y, a);
166+
}
167+
168+
// Check for XTR Header
169+
extrahdr_t hdr;
170+
memset(&hdr, 0, sizeof(hdr));
171+
m_title = "";
172+
m_states->clear();
173+
size_t ptr = file.tell();
174+
if (readfile(&hdr, sizeof(hdr)))
175+
{
176+
if ((memcmp(&hdr, XTR_SIG, sizeof(hdr.sig)) == 0))
106177
{
107-
m_lastError = "signature mismatch";
108-
printf("%s\n", m_lastError.c_str());
109-
return false;
178+
// printf("reading4: %s %d\n", hdr.sig, hdr.ver);
179+
// read title
180+
uint16_t size = 0;
181+
if (readfile(&size, 1))
182+
{
183+
char tmp[MAX_TITLE + 1];
184+
tmp[size] = 0;
185+
if (readfile(tmp, size) != 0)
186+
{
187+
m_title = tmp;
188+
}
189+
}
190+
// read states
191+
if (hdr.ver >= XTR_VER1)
192+
{
193+
m_states->read(file);
194+
}
110195
}
111-
uint16_t ver = 0;
112-
fread(&ver, sizeof(VERSION), 1, sfile);
113-
if (ver > VERSION)
196+
else
114197
{
115-
m_lastError = "bad version";
116-
printf("%s\n", m_lastError.c_str());
117-
return false;
198+
// revert back to previous position
199+
file.seek(ptr);
118200
}
119-
uint16_t len = 0;
120-
uint16_t hei = 0;
121-
fread(&len, sizeof(uint8_t), 1, sfile);
122-
fread(&hei, sizeof(uint8_t), 1, sfile);
123-
len = len ? len : MAX_SIZE;
124-
hei = hei ? hei : MAX_SIZE;
125-
resize(len, hei, true);
126-
fread(m_map, len * hei, 1, sfile);
127-
m_attrs.clear();
128-
uint16_t attrCount = 0;
129-
fread(&attrCount, sizeof(attrCount), 1, sfile);
130-
for (int i = 0; i < attrCount; ++i)
201+
}
202+
203+
return true;
204+
}
205+
206+
bool CMap::read(FILE *sfile)
207+
{
208+
auto readfile = [sfile](auto ptr, auto size)
209+
{
210+
return fread(ptr, size, 1, sfile) == 1;
211+
};
212+
char sig[4];
213+
readfile(sig, sizeof(SIG));
214+
if (memcmp(sig, SIG, sizeof(SIG)) != 0)
215+
{
216+
m_lastError = "signature mismatch";
217+
printf("%s\n", m_lastError.c_str());
218+
return false;
219+
}
220+
uint16_t ver = 0;
221+
readfile(&ver, sizeof(VERSION));
222+
if (ver > VERSION)
223+
{
224+
m_lastError = "bad version";
225+
printf("%s\n", m_lastError.c_str());
226+
return false;
227+
}
228+
uint16_t len = 0;
229+
uint16_t hei = 0;
230+
readfile(&len, sizeof(uint8_t));
231+
readfile(&hei, sizeof(uint8_t));
232+
len = len ? len : MAX_SIZE;
233+
hei = hei ? hei : MAX_SIZE;
234+
resize(len, hei, true);
235+
readfile(m_map, len * hei);
236+
m_attrs.clear();
237+
uint16_t attrCount = 0;
238+
readfile(&attrCount, sizeof(attrCount));
239+
for (int i = 0; i < attrCount; ++i)
240+
{
241+
uint8_t x;
242+
uint8_t y;
243+
uint8_t a;
244+
readfile(&x, sizeof(x));
245+
readfile(&y, sizeof(y));
246+
readfile(&a, sizeof(a));
247+
setAttr(x, y, a);
248+
}
249+
250+
// Check for XTR Header
251+
extrahdr_t hdr;
252+
memset(&hdr, 0, sizeof(hdr));
253+
m_title = "";
254+
m_states->clear();
255+
size_t ptr = ftell(sfile);
256+
if (readfile(&hdr, sizeof(hdr)))
257+
{
258+
if ((memcmp(&hdr, XTR_SIG, sizeof(hdr.sig)) == 0))
131259
{
132-
uint8_t x;
133-
uint8_t y;
134-
uint8_t a;
135-
fread(&x, sizeof(x), 1, sfile);
136-
fread(&y, sizeof(y), 1, sfile);
137-
fread(&a, sizeof(a), 1, sfile);
138-
setAttr(x, y, a);
139-
}
140-
extrahdr_t hdr;
141-
memset(&hdr, 0, sizeof(hdr));
142-
m_title = "";
143-
// read title
144-
size_t ptr = ftell(sfile);
145-
if (fread(&hdr, sizeof(hdr), 1, sfile) != 0) {
146-
if ((memcmp(&hdr, XTR_SIG, sizeof(hdr.sig)) == 0) && (hdr.ver == XTR_VER)) {
147-
//printf("reading4: %s %d\n", hdr.sig, hdr.ver);
148-
uint16_t size = 0;
149-
if (fread(&size,1, 1, sfile) != 0) {
150-
char tmp[MAX_TITLE + 1];
151-
tmp[size] = 0;
152-
if (fread(tmp, size, 1, sfile) != 0) {
153-
m_title = tmp;
154-
}
260+
// printf("reading4: %s %d\n", hdr.sig, hdr.ver);
261+
// read title
262+
uint16_t size = 0;
263+
if (readfile(&size, 1))
264+
{
265+
char tmp[MAX_TITLE + 1];
266+
tmp[size] = 0;
267+
if (readfile(tmp, size) != 0)
268+
{
269+
m_title = tmp;
155270
}
156271
}
157-
else
272+
// read states
273+
if (hdr.ver >= XTR_VER1)
158274
{
159-
// revert back to previous position
160-
fseek(sfile, ptr, SEEK_SET);
275+
m_states->read(sfile);
161276
}
162277
}
278+
else
279+
{
280+
// revert back to previous position
281+
fseek(sfile, ptr, SEEK_SET);
282+
}
163283
}
164-
return sfile != nullptr;
284+
return true;
165285
}
166286

167287
bool CMap::fromMemory(uint8_t *mem)
168288
{
169-
if (memcmp(mem, SIG, strlen(SIG)) != 0)
289+
if (memcmp(mem, SIG, sizeof(SIG)) != 0)
170290
{
171291
m_lastError = "signature mismatch";
172292
printf("%s\n", m_lastError.c_str());
@@ -205,7 +325,8 @@ bool CMap::fromMemory(uint8_t *mem)
205325
// read title
206326
extrahdr_t hdr;
207327
memcpy(&hdr, ptr, sizeof(hdr));
208-
if ((memcmp(&hdr, XTR_SIG, sizeof(hdr.sig)) == 0) && (hdr.ver == XTR_VER)) {
328+
if ((memcmp(&hdr, XTR_SIG, sizeof(hdr.sig)) == 0) && (hdr.ver == XTR_VER))
329+
{
209330
ptr += sizeof(hdr);
210331
uint8_t size = *ptr;
211332
++ptr;
@@ -221,7 +342,7 @@ bool CMap::write(FILE *tfile)
221342
{
222343
if (tfile)
223344
{
224-
fwrite(SIG, strlen(SIG), 1, tfile);
345+
fwrite(SIG, sizeof(SIG), 1, tfile);
225346
fwrite(&VERSION, sizeof(VERSION), 1, tfile);
226347
fwrite(&m_len, sizeof(uint8_t), 1, tfile);
227348
fwrite(&m_hei, sizeof(uint8_t), 1, tfile);
@@ -239,16 +360,20 @@ bool CMap::write(FILE *tfile)
239360
fwrite(&a, sizeof(a), 1, tfile);
240361
}
241362

363+
//// if (!m_title.empty() && m_title.size() < MAX_TITLE)
364+
// {
242365
// write title
243-
if (!m_title.empty() && m_title.size() < MAX_TITLE) {
244-
extrahdr_t hdr;
245-
memcpy(&hdr.sig, XTR_SIG, sizeof(hdr.sig));
246-
hdr.ver = XTR_VER;
247-
fwrite(&hdr, sizeof(hdr),1, tfile);
248-
int size = m_title.size();
249-
fwrite(&size,1, 1, tfile);
250-
fwrite(m_title.c_str(), m_title.size(), 1, tfile);
251-
}
366+
extrahdr_t hdr;
367+
memcpy(&hdr.sig, XTR_SIG, sizeof(hdr.sig));
368+
hdr.ver = XTR_VER1;
369+
fwrite(&hdr, sizeof(hdr), 1, tfile);
370+
int size = m_title.size();
371+
fwrite(&size, 1, 1, tfile);
372+
fwrite(m_title.c_str(), m_title.size(), 1, tfile);
373+
374+
// write states
375+
m_states->write(tfile);
376+
// }
252377
}
253378
return tfile != nullptr;
254379
}
@@ -388,6 +513,7 @@ CMap &CMap::operator=(const CMap &map)
388513
m_map = new uint8_t[m_size];
389514
memcpy(m_map, map.m_map, m_size);
390515
m_attrs = map.m_attrs;
516+
m_title = map.m_title;
391517
return *this;
392518
}
393519

@@ -481,12 +607,17 @@ void CMap::debug()
481607
}
482608
}
483609

484-
const char * CMap::title()
610+
const char *CMap::title()
485611
{
486612
return m_title.c_str();
487613
}
488614

489-
void CMap::setTitle(const char *title)
615+
void CMap::setTitle(const char *title)
490616
{
491617
m_title = title;
492618
}
619+
620+
CStates &CMap::states()
621+
{
622+
return *m_states;
623+
}

0 commit comments

Comments
 (0)