Repository navigation
Expand file tree
/
Copy pathtileset.cpp
More file actions
125 lines (99 loc) · 2.35 KB
/
Copy pathtileset.cpp
File metadata and controls
125 lines (99 loc) · 2.35 KB
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
#include <tileset.h>
#include <easydraw.h>
#include <comdef.h>
#include <fstream>
#include <globals.h>
#include "utilities.h"
static void LogError(const char *file, const char *errormessage)
{
using namespace std;
fstream os(file, ios::out | ios::app);
os << errormessage << endl;
}
CTileSet::CTileSet()
{
m_TileCount = 0;
TileList = 0;
TileSet = 0;
reload = 0;
}
CTileSet::~CTileSet()
{
Unload();
}
void CTileSet::DrawTile(LPDIRECTDRAWSURFACE7 where, int x, int y, DWORD which)
{
OffsetRect(&framerect, x, y);
where->Blt(&framerect, TileSet->surface, &TileList[which].src, DDBLT_WAIT | DDBLT_KEYSRC, 0);
OffsetRect(&framerect, -x, -y);
}
void CTileSet::Reload()
{
LPDIRECTDRAW7 pdd;
TileSet->surface->GetDDInterface((void **)&pdd);
pdd->RestoreAllSurfaces();
Load(pdd, reload);
}
void CTileSet::Unload()
{
if( TileSet )
{
delete TileSet;
TileSet = 0;
}
if( reload )
{
free(reload);
reload = 0;
}
if( TileList )
{
delete[] TileList;
TileList = 0;
m_TileCount = 0;
}
}
bool CTileSet::Load(LPDIRECTDRAW7 dd, char *file)
{
DDSURFACEDESC2 ddsd = {0};
U16ptr ptr;
int w, h, rowcount, colcount;
Unload(); // if one was previously loaded, unload it
TileSet = new Image;
reload = strdup(file);
if( !easyx::LoadBitmap(dd, &TileSet->surface, file) )
return false;
ddsd.dwSize = sizeof ddsd;
if( FAILED(TileSet->surface->Lock(0, &ddsd, DDLOCK_SURFACEMEMORYPTR, 0)) )
return false;
ptr = (U16ptr)ddsd.lpSurface;
w = ddsd.dwWidth; //ddsd.lPitch >> 1;
h = ddsd.dwHeight;
U16 transparent = ptr[w-1];
colcount = 0; // count the columns (the horizontal part) by counting the transparency pixels
for( int x = 1; x < w; x++ ) {
if( ptr[x] == transparent )
colcount++;
}
rowcount = 0;
for( int y = 1; y < h; y++ ) {
if( ptr[y*(ddsd.lPitch>>1)] == transparent )
rowcount++;
}
m_TileCount = rowcount * colcount;
TileList = new TILEINFO[m_TileCount];
for( y = 0; y < rowcount; y++ ) {
for( x = 0; x < colcount; x++ )
{
SetRect(&TileList[(y*colcount)+x].src, x*(w/colcount)+1, y*(h/rowcount)+1,
x*(w/colcount)+(w/colcount), y*(h/rowcount)+(h/rowcount));
}
}
SetRect(&framerect, 0, 0, w/colcount, h/rowcount);
TileSet->surface->Unlock(0);
DDCOLORKEY ddck;
ddck.dwColorSpaceHighValue = transparent;
ddck.dwColorSpaceLowValue = transparent;
TileSet->surface->SetColorKey(DDCKEY_SRCBLT, &ddck);
return true;
}