-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathwxIMPLEMENT_PLUGGABLE_CLASS(name, basename)
426 lines (366 loc) · 10.7 KB
/
wxIMPLEMENT_PLUGGABLE_CLASS(name, basename)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#include "wx/filename.h"
#include "wx/stdpaths.h"
#include "wx/msgdlg.h"
#include "wx/log.h"
#include "Map.h"
#include <fstream>
using namespace std;
Map::Map()
{
decode = NULL;
tiles = NULL;
mCol = mRow = mPixelWidth = mPixelHeight = 0;
LoadResource();
}
Map::~Map()
{
DestoryData();
DestoryResource();
//dtor
}
bool Map::ReadFile(const wxString FilePath)
{
DestoryData();
ifstream mapfile(FilePath.char_str(), ios_base::binary | ios_base::in);
if(!mapfile.is_open()) return false;
unsigned char temp4b[4];
char tempc;
wxString wx_mpcpath;
//check file format correctly
char vinfo[16];
mapfile.read(vinfo, 16);
if(strncmp(vinfo, "MAP File Ver2.0", 12) != 0)
{
mapfile.close();
return false;
}
//initialize mpc file path
mapfile.seekg(16, ios_base::cur);
char mpcpath[32] = {0};
for(int pidx = 0; pidx < 32 ; pidx++)
{
mapfile.read(&tempc, 1);
if(tempc != 0)
mpcpath[pidx] = tempc;
}
if(mpcpath[0] == 0)
{
wx_mpcpath = wxT("\\mpc\\map\\") +
wxFileName::FileName(FilePath).GetName();
}
else
{
wx_mpcpath = wxString(mpcpath);
}
wxString exepath;
exepath = wxStandardPaths::Get().GetExecutablePath();
exepath = wxFileName::FileName(exepath).GetPath(wxPATH_GET_VOLUME);
wx_mpcpath = exepath + wx_mpcpath;
mapfile.seekg(4, ios_base::cur);
mapfile.read((char*)temp4b, 4);
mCol = Char2Long(temp4b);
mPixelWidth = (mCol + 1) * 64 ;
mapfile.read((char*)temp4b, 4);
mRow = Char2Long(temp4b);
mPixelHeight = (mRow + 1) * 16;
mapfile.seekg(192, ios_base::beg);
decode = new MpcDecode[255];
char mpcname[32] = {0};
for(int namei = 0; namei < 255; namei++)
{
for(int nc = 0; nc < 64; nc++)
{
mapfile.read(&tempc, 1);
if(nc < 31)
{
mpcname[nc] = tempc;
}
}
if(decode[namei].ReadMpcFile(wx_mpcpath + wxT("\\") + wxString(mpcname)))
{
decode[namei].BufferData();
}
}
long totaltiles = mCol * mRow;
tiles = new Tile[totaltiles];
mapfile.seekg(16512, ios_base::beg);
for(long tilei = 0; tilei < totaltiles; tilei++)
{
mapfile.read((char*)(&tiles[tilei]), 8);
mapfile.seekg(2, ios_base::cur);
}
mapfile.close();
return true;
}
void Map::DrawLayer(int index, Tile *tiles, MpcDecode *decode, wxImage *img)
{
if(index < 1 || index > 3) return;
if(tiles == NULL || decode == NULL || img == NULL) return;
long totaltiles = mCol * mRow;
long width, height;
unsigned char mpcno, frmno, *frmdata;
for(long tilei = 0; tilei < totaltiles; tilei++)
{
switch(index)
{
case 1:
mpcno = tiles[tilei].fir_mpc_no;
frmno = tiles[tilei].fir_frm_no;
break;
case 2:
mpcno = tiles[tilei].sec_mpc_no;
frmno = tiles[tilei].sec_frm_no;
break;
case 3:
mpcno = tiles[tilei].thr_mpc_no;
frmno = tiles[tilei].thr_frm_no;
break;
default:
break;
}
if(mpcno == 0) continue;
frmdata = decode[mpcno - 1].GetBuffedFrameData(frmno, &width, &height);
DrawTile(tilei%mCol, tilei/mCol, width, height, frmdata, img);
}
}
/**
index: 1 - barrer, 2 - trap
**/
void Map::DrawLayer(int index, wxImage* img)
{
if(index < 1|| index > 2) return;
long totaltiles = mCol * mRow;
long width, height;
unsigned char barrertype, trapindex, *frmdata;
for(long tilei = 0; tilei < totaltiles; tilei++)
{
frmdata = NULL;
barrertype = 0;
trapindex = 0;
width = 0;
height = 0;
switch(index)
{
case 1: //barrer
barrertype = tiles[tilei].barrer_type;
break;
case 2: //trap
trapindex = tiles[tilei].trap_index;
break;
default:
break;
}
if(barrertype != 0)
{
switch(barrertype)
{
case 0x40:
width = mBarrer[0].width;
height = mBarrer[0].height;
frmdata = mBarrer[0].data;
break;
case 0x60:
width = mBarrer[1].width;
height = mBarrer[1].height;
frmdata = mBarrer[1].data;
break;
case 0x80:
width = mBarrer[2].width;
height = mBarrer[2].height;
frmdata = mBarrer[2].data;
break;
case 0xA0:
width = mBarrer[3].width;
height = mBarrer[3].height;
frmdata = mBarrer[3].data;
break;
default:
break;
}
}
if(trapindex != 0 && trapindex <= 19)
{
width = mTrap[trapindex - 1].width;
height = mTrap[trapindex - 1].height;
frmdata = mTrap[trapindex - 1].data;
}
if(width == 0 || height == 0 || frmdata == NULL) continue;
DrawTile(tilei%mCol, tilei/mCol, width, height, frmdata, img);
}
}
bool Map::GetTilePosition(int pixelX, int pixelY, int *tileX, int *tileY)
{
if(tileX == NULL ||
tileY == NULL ||
pixelX < 0 ||
pixelX > mPixelWidth ||
pixelY < 0 ||
pixelY > mPixelHeight) return false;
//first caculating even row position, considering each tile is 64 * 32 rectangle
int nx, ny;
nx = pixelX / 64;
ny = (pixelY/ 32) * 2;
//now caclue real position, please see 鳳華芞釴梓.jpg
int dx, dy;
dx = pixelX - nx * 64;
dy = pixelY - (ny / 2) * 32;
if(dx < 32)
{
if(dy < (32 - dx) / 2) // 1
{
nx = nx - 1;
ny = ny - 1;
}
else if(dy > (dx / 2 + 16)) // 2
{
nx = nx - 1;
ny = ny + 1;
}
}
if(dx > 32)
{
if(dy < (dx - 32) / 2) //3
{
ny = ny - 1;
}
else if(dy > ((64 - dx) / 2 + 16)) // 4
{
ny = ny + 1;
}
}
*tileX = nx;
*tileY = ny;
return true;
}
bool Map::GetPixelPosition(int Column, int Row, int *pixelX, int *pixelY)
{
if(pixelX == NULL ||
pixelY == NULL ||
Column < 0 ||
Column > mCol ||
Row < 0 ||
Row > mRow) return false;
long basex, basey;
basex = (Row%2) * 32 + 64 * Column;
basey = 16 * Row;
*pixelX = basex;
*pixelY = basey;
return true;
}
void Map::DrawTile(long Column, long Row,
long TileWidth, long TileHeight,
unsigned char* TileData, wxImage *img)
{
if(TileData == NULL || Column > mCol || Row > mRow || img == NULL) return;
//tile top-left position
long basex, basey;
bool hasAlpha = img->HasAlpha();
basex = (Row%2) * 32 + 64 * Column;
basey = 16 * Row;
//graph top-left position
long graphx, graphy;
graphx = basex + (32 - TileWidth/2);
graphy = basey + (32 - TileHeight);
unsigned long datai = 0;
for(long hi = 0; hi < TileHeight; hi++)
{
for(long wi = 0; wi < TileWidth; wi++)
{
if(TileData[datai + 3] != 0)
{
img->SetRGB(wxRect(graphx + wi, graphy + hi, 1, 1),
TileData[datai],
TileData[datai + 1],
TileData[datai + 2]);
if(hasAlpha)img->SetAlpha(graphx + wi, graphy + hi, 0xFF);
}
datai += 4;
}
}
}
wxImage* Map::getImage(unsigned char layer, bool hasAlpha)
{
wxImage *img = new wxImage;
img->Create(mPixelWidth, mPixelHeight, true);
if(hasAlpha)img->SetAlpha();
if(layer & LAYER1) DrawLayer(1, tiles, decode, img);
if(layer & LAYER2) DrawLayer(2, tiles, decode, img);
if(layer & LAYER3) DrawLayer(3, tiles, decode, img);
if(layer & TRAP) DrawLayer(2, img);
if(layer & BARRER) DrawLayer(1, img);
return img;
}
//wxImage* Map::getImage(int beginPosX, int beginPosY, int width, int height, unsigned char layer)
//{
// wxImage *img = new wxImage;
// img->Create(width, height, true);
// img->SetAlpha();
//
//}
void Map::LoadResource()
{
wxLogNull lognull;
wxImage img;
wxString exepath;
exepath = wxStandardPaths::Get().GetExecutablePath();
exepath = wxFileName::FileName(exepath).GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR);
exepath += wxT("resource\\");
for(int i = 0; i < 19; i++)
{
img.LoadFile(exepath + wxString::Format(wxT("%d.png"), i + 1));
AssignImgData(&mTrap[i], img);
}
img.LoadFile(exepath + wxT("芵.png"));
AssignImgData(&mBarrer[0], img);
img.LoadFile(exepath + wxT("泐芵.png"));
AssignImgData(&mBarrer[1], img);
img.LoadFile(exepath + wxT("梤.png"));
AssignImgData(&mBarrer[2], img);
img.LoadFile(exepath + wxT("泐梤.png"));
AssignImgData(&mBarrer[3], img);
}
void Map::DestoryResource()
{
for(int i = 0; i < 19; i++)
{
if(mTrap[i].data != NULL) free(mTrap[i].data);
}
for(int j = 0; j < 4; j++)
{
if(mBarrer[j].data != NULL) free(mBarrer[j].data);
}
}
void Map::AssignImgData(ImgData *imgdata, wxImage &img)
{
if(imgdata == NULL) return;
if(img.IsOk())
{
unsigned char *pixdata, *alphadata;
int width, height;
long datasize;
if(!img.HasAlpha()) img.SetAlpha();
width = img.GetWidth();
height = img.GetHeight();
pixdata = img.GetData();
alphadata = img.GetAlpha();
datasize = (long) 4 * width * height;
(*imgdata).width = (long)width;
(*imgdata).height = (long)height;
(*imgdata).data = (unsigned char*)malloc(datasize);
if((*imgdata).data == NULL) return;
int pi = 0, ai = 0;
for(int di = 0; di < datasize;)
{
(*imgdata).data[di++] = pixdata[pi++];
(*imgdata).data[di++] = pixdata[pi++];
(*imgdata).data[di++] = pixdata[pi++];
(*imgdata).data[di++] = alphadata[ai++];
}
}
else
{
(*imgdata).width = 0;
(*imgdata).height = 0;
(*imgdata).data = NULL;
}
}