-
Notifications
You must be signed in to change notification settings - Fork 0
/
FGTileFiller.java
117 lines (99 loc) · 3.13 KB
/
FGTileFiller.java
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
package femto.mode;
public class FGTileFiller implements LineFiller {
// Get the map, get the tiles, render
pointer tileMap;
pointer tileWindow;
pointer tileSet;
ushort[] palette;
int mapWidth;
int mapHeight;
// int color;
int cameraX = 0;
int cameraY = 0;
int adjustedY;
int tileW = 10;
int tileH = 8;
int tileSize = 0;
FGTileFiller(ushort[] palette) {
this.palette = palette;
}
void setTile(int tileId, int x, int y) {
__inline_cpp__("
((uint8_t * ) tileMap)[x + y * mapWidth] = tileId;
");
}
int getTile(int x, int y) {
int t = -1;
__inline_cpp__("
t = ((uint8_t * ) tileMap)[x + y * mapWidth];
");
return t;
}
void setMap(pointer map, pointer tileSet) {
this.tileSet = tileSet;
tileW = TileMaps.tileW;
tileH = TileMaps.tileH;
tileSize = tileW * tileH;
__inline_cpp__("
mapWidth = ((char * ) map)[0];
mapHeight = ((char * ) map)[1];
tileMap = (uint8_t * ) map + 2;
");
}
void draw(int x, int y) {
cameraX = x;
cameraY = y;
}
void fillLine(ushort[] line, int y) {
adjustedY = y - cameraY;
// Clip top and bottom of map.
if (adjustedY < 0 || adjustedY >= mapHeight * tileH) return;
// Set the Y for the map and tileset lookup
var mapY = ((adjustedY) / tileH) * mapWidth;
var tileY = ((adjustedY) % tileH) * tileW;
// Set the X for the map and tileset lookup
var mapX = cameraX / tileW;
var tileX = cameraX % tileW;
// Clip to the left of the map
int start = -cameraX;
if (cameraX < 0) {
mapX = 0;
tileX = 0;
}
if (start < 0) start = 0;
__inline_cpp__("
auto tileStart = ((uint8_t * ) tileMap)+mapY;//[mapX + mapY];
");
// Loop the map width to collect the tiles
for (int i = start; i < 220 && mapX < mapWidth;) {
// Clip the right hand side of the map. Whee~
// if (mapX >= mapWidth) return;
int iter = min(tileW - tileX, 220 - i);
__inline_cpp__("
// Get tile ID from the map. Then use that to find the tile itself from the tileset
auto tileId = ((uint8_t * ) tileStart)[mapX] * (tileSize);
if(tileId==0){
i += iter;
tileX = 0L;
mapX++;
continue;
}
auto tile = ((uint8_t * ) tileSet) + tileId + tileY + tileX;
");
// Loop over the Tile color IDs and put them in the line array.
for (int t = 0; t < iter; t++) {
__inline_cpp__("
// color = tile[tileX + t];
if(tile[t] == 0) continue;
line->elements[i + t] = palette->elements[tile[t]];
");
}
i += iter;
tileX = 0;
mapX++;
}
}
int min(int a, int b) {
return (a < b) ? a : b;
}
}