-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtile.cpp
76 lines (63 loc) · 1.13 KB
/
tile.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
#include <string.h>
#include "types.h"
class tile {
protected:
int tilewidth;
int tileheight;
int tilesize;
char tilebpp;
unsigned char *tiledata;
};
tile::tile()
{
tilewidth=0;
tileheight=0;
tilesize=0;
tilebpp=0;
tiledata=NULL;
}
tile::~tile()
{
if(tiledata) delete[] tiledata;
}
tile::tile(int width, int height, char bpp, unsigned char *data)
{
tilesize=width*height*bpp/8;
tilewidth=width;
tileheight=height;
tilebpp=bpp;
tiledata=new unsigned char[tilesize];
if (tiledata == NULL) return;
memcpy(tiledata,data,tilesize);
}
uint32_t tile::getpixel(int x, int y)
{
uint32_t ret;
switch(tilebpp) {
case 1:
ret=(tiledata[y*(tilewidth/8) + (x/8)];
ret=(ret >> (x&7)) & 0x01;
break;
case 2:
ret=tiledata[y*(tilewidth/4) + (x/4)];
ret=(ret >> (x&2*6)) & 0x03;
break;
case 4:
ret=tiledata[y*(tilewidth/2) + (x/2)];
ret=(ret >> (x&1*4)) & 0x0f;
break;
case 8:
ret=tiledata[y*tilewidth + x];
break;
case 16:
ret=tiledata[y*tilewidth*2 + x*2];
break;
case 24:
ret=tiledata[y*tilewidth*3 + x*3];
break;
case 32:
ret=tiledata[y*tilewidth*4 + x*4];
break;
}
return ret;
}