-
Notifications
You must be signed in to change notification settings - Fork 14
/
terrain.cpp
328 lines (285 loc) · 14.4 KB
/
terrain.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
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
#include "terrain.h"
#include <libndls.h>
#include "textures/terrain.h"
#include "textures/inv_selection.h"
const char *block_names[BLOCK_NORMAL_LAST + 1] =
{
"Air",
"Stone",
"Dirt",
"Sand",
"Wood",
"Leaves",
"Normal Planks",
"Wall",
"Coal Ore",
"Gold Ore",
"Iron Ore",
"Diamond Ore",
"Redstone Ore",
"TNT",
"Sponge",
"Dark Planks",
"Bright Planks",
"Furnace",
"Crafting Table",
"Bookshelf",
"Grass",
"Pumpkin",
"Bedrock",
"Glass",
"Cobblestone",
"Glowstone",
"Iron block",
"Gold block",
"Diamond block",
"Netherrack"
};
struct BLOCK_TEXTURE {
BLOCK block;
BLOCK_SIDE_BITFIELD sides;
};
#define NON {BLOCK_AIR, 0}
#define ALL(block) {block, BLOCK_TOP_BIT | BLOCK_BOTTOM_BIT | BLOCK_LEFT_BIT | BLOCK_RIGHT_BIT | BLOCK_FRONT_BIT | BLOCK_BACK_BIT}
#define TOP(block) {block, BLOCK_TOP_BIT}
#define BOT(block) {block, BLOCK_BOTTOM_BIT}
#define LEF(block) {block, BLOCK_LEFT_BIT}
#define RIG(block) {block, BLOCK_RIGHT_BIT}
#define FRO(block) {block, BLOCK_FRONT_BIT}
#define BAC(block) {block, BLOCK_BACK_BIT}
#define TAB(block) {block, BLOCK_TOP_BIT | BLOCK_BOTTOM_BIT}
#define SID(block) {block, BLOCK_FRONT_BIT | BLOCK_BACK_BIT | BLOCK_LEFT_BIT | BLOCK_RIGHT_BIT}
#define SWF(block) {block, BLOCK_BACK_BIT | BLOCK_LEFT_BIT | BLOCK_RIGHT_BIT}
#define AWF(block) {block, BLOCK_TOP_BIT | BLOCK_BOTTOM_BIT | BLOCK_LEFT_BIT | BLOCK_RIGHT_BIT | BLOCK_BACK_BIT}
//Maps location in texture atlas to block ID
static const BLOCK_TEXTURE texture_atlas[][16] =
{
{ TOP(BLOCK_GRASS), ALL(BLOCK_STONE), ALL(BLOCK_DIRT), SID(BLOCK_GRASS), ALL(BLOCK_PLANKS_NORMAL), NON, NON, ALL(BLOCK_WALL), ALL(BLOCK_TNT), TOP(BLOCK_TNT), BOT(BLOCK_TNT), NON, NON, NON, NON, NON },
{ NON, ALL(BLOCK_BEDROCK), ALL(BLOCK_SAND), ALL(BLOCK_COBBLESTONE), SID(BLOCK_WOOD), TAB(BLOCK_WOOD), ALL(BLOCK_IRON), ALL(BLOCK_GOLD), ALL(BLOCK_DIAMOND), NON, NON, NON, NON, NON, NON, NON },
{ ALL(BLOCK_GOLD_ORE), ALL(BLOCK_IRON_ORE), ALL(BLOCK_COAL_ORE), FRO(BLOCK_BOOKSHELF), NON, NON, NON, NON, NON, NON, NON, TAB(BLOCK_CRAFTING_TABLE), FRO(BLOCK_FURNACE), SWF(BLOCK_FURNACE), NON, NON },
{ ALL(BLOCK_SPONGE), ALL(BLOCK_GLASS), ALL(BLOCK_DIAMOND_ORE), ALL(BLOCK_REDSTONE_ORE), NON, ALL(BLOCK_LEAVES), NON, NON, NON, NON, NON, SID(BLOCK_CRAFTING_TABLE), FRO(BLOCK_CRAFTING_TABLE), NON, TOP(BLOCK_FURNACE), NON },
{ NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON },
{ NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON },
{ NON, NON, NON, NON, NON, NON, TAB(BLOCK_PUMPKIN), ALL(BLOCK_NETHERRACK), NON, ALL(BLOCK_GLOWSTONE), NON, NON, NON, NON, NON, NON},
{ NON, NON, NON, NON, NON, NON, SWF(BLOCK_PUMPKIN), FRO(BLOCK_PUMPKIN), NON, NON, NON, NON, NON, NON, NON, NON },
{ NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON },
{ NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON },
{ NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON },
{ NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON },
{ NON, NON, NON, NON, NON, NON, ALL(BLOCK_PLANKS_DARK), AWF(BLOCK_BOOKSHELF), NON, NON, NON, NON, NON, NON, NON, NON },
{ NON, NON, NON, NON, NON, NON, ALL(BLOCK_PLANKS_BRIGHT), NON, NON, NON, NON, NON, NON, NON, NON, NON },
{ NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON },
{ NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON }
};
static const struct { int x, y; } special_block_texture_idx[BLOCK_SPECIAL_LAST - BLOCK_SPECIAL_START + 1] =
{
{4, 0}, // Torch -> Planks
{4, 3}, // Flower -> Leaves
{11, 0}, // Spiderweb -> Spiderweb
{9, 7}, // Cake -> Cake
{14, 8}, // Mushroom -> Mushroom block
{1, 6}, // Door -> Door bottom
{13, 12}, // Water -> Water
{13, 14}, // Lava -> Lava
{15, 5}, // Wheat -> Wheat
{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, // Gap...
{3, 13}, // Redstone Lamp -> Lamp (off)
{1, 0}, // Redstone Switch -> Stone
{4, 10}, // Redstone Wire -> Redstone Wire
{4, 0}, // Redstone Torch -> Planks
{1, 0}, // Pressure Plate -> Stone
};
TerrainAtlasEntry block_textures[BLOCK_NORMAL_LAST + 1][BLOCK_SIDE_LAST + 1];
TerrainQuadEntry quad_block_textures[BLOCK_NORMAL_LAST + 1][BLOCK_SIDE_LAST + 1], dual_block_textures[2][BLOCK_NORMAL_LAST + 1][BLOCK_SIDE_LAST + 1];
TerrainAtlasEntry terrain_atlas[16][16];
TerrainAtlasEntry special_block_textures[BLOCK_SPECIAL_LAST - BLOCK_SPECIAL_START + 1];
TEXTURE *terrain_current, *terrain_resized, *terrain_quad, *inv_selection_p, *door_preview;
//Some textures have a different color in different biomes. We have to make them green. Grey grass just looks so unhealty
static void makeColor(const RGB &color, TEXTURE &texture, const int x, const int y, const int w, const int h)
{
for(int x1 = x; x1 < w + x; x1++)
for(int y1 = y; y1 < h + y; y1++)
{
RGB grey = rgbColor(texture.bitmap[x1 + y1*texture.width]);
grey.r *= color.r;
grey.g *= color.g;
grey.b *= color.b;
texture.bitmap[x1 + y1*texture.width] = colorRGB(grey);
}
}
static void makeWaterOpaque(TEXTURE &texture, const int x, const int y, const int w, const int h)
{
for(int x1 = x; x1 < w + x; x1++)
for(int y1 = y; y1 < h + y; y1++)
{
RGB color = rgbColor(texture.bitmap[x1 + y1*texture.width]);
// Taking the (non-remultiplied) color directly looks too bright
color.r = (color.r * 3) / 4;
color.g = (color.g * 3) / 4;
color.b = (color.b * 3) / 4;
texture.bitmap[x1 + y1*texture.width] = colorRGB(color);
}
}
void terrainInit(const char *texture_path)
{
terrain_current = loadTextureFromFile(texture_path);
if(!terrain_current)
terrain_current = &terrain; //Use default, included texture
else
puts("External texture loaded!");
int fields_x = 16;
int fields_y = 16;
int field_width = terrain_current->width / fields_x;
int field_height = terrain_current->height / fields_y;
//Give grass and leaves color
const RGB green = { 0.5f, 0.8f, 0.3f };
makeColor(green, *terrain_current, 0, 0, field_width, field_height);
makeColor(green, *terrain_current, 5 * field_width, 3 * field_height, field_width, field_height);
makeColor(green, *terrain_current, 4 * field_width, 3 * field_height, field_width, field_height);
//Also redstone
drawTexture(*terrain_current, *terrain_current, 4 * field_width, 10 * field_height, field_width, field_height, 4 * field_width, 11 * field_height, field_width, field_height);
drawTexture(*terrain_current, *terrain_current, 5 * field_width, 10 * field_height, field_width, field_height, 5 * field_width, 11 * field_height, field_width, field_height);
const RGB black = { 0.3f, 0.2f, 0.2f };
makeColor(black, *terrain_current, 4 * field_width, 10 * field_height, field_width, field_height);
makeColor(black, *terrain_current, 5 * field_width, 10 * field_height, field_width, field_height);
const RGB red = { 1.0f, 0.2f, 0.2f };
makeColor(red, *terrain_current, 4 * field_width, 11 * field_height, field_width, field_height);
makeColor(red, *terrain_current, 5 * field_width, 11 * field_height, field_width, field_height);
//And redstone switches
drawTexture(*terrain_current, *terrain_current, 0 * field_width, 6 * field_height, field_width, field_height, 10 * field_width, 15 * field_height, field_width, field_height);
const RGB red_tint = { 1.0f, 0.8f, 0.8f };
makeColor(red_tint, *terrain_current, 10 * field_width, 15 * field_height, field_width, field_height);
// Water has opacity of 0.5 but it's not rendered with alpha here.
makeWaterOpaque(*terrain_current, 13 * field_width, 12 * field_height, field_width, field_height);
if(terrain_current->width == 384 && terrain_current->height == 384)
terrain_resized = terrain_current;
else
terrain_resized = resizeTexture(*terrain_current, 384, 384);
for(int y = 0; y < fields_y; y++)
for(int x = 0; x < fields_x; x++)
{
//+1 and -2 to work around GLFix inaccuracies resulting in rounding errors
TerrainAtlasEntry tea = terrain_atlas[x][y] = {textureArea(x * field_width + 1, y * field_height + 1, field_width - 2, field_height - 2),
textureArea(x * 24, y * 24, 24, 24) };
BLOCK_TEXTURE bt = texture_atlas[y][x];
if(bt.sides == 0)
continue;
if(bt.sides & BLOCK_BOTTOM_BIT)
block_textures[bt.block][BLOCK_BOTTOM] = tea;
if(bt.sides & BLOCK_TOP_BIT)
block_textures[bt.block][BLOCK_TOP] = tea;
if(bt.sides & BLOCK_LEFT_BIT)
block_textures[bt.block][BLOCK_LEFT] = tea;
if(bt.sides & BLOCK_RIGHT_BIT)
block_textures[bt.block][BLOCK_RIGHT] = tea;
if(bt.sides & BLOCK_FRONT_BIT)
block_textures[bt.block][BLOCK_FRONT] = tea;
if(bt.sides & BLOCK_BACK_BIT)
block_textures[bt.block][BLOCK_BACK] = tea;
}
//Slight hack, you can't assign a texture to multiple blocks
block_textures[BLOCK_GRASS][BLOCK_BOTTOM] = block_textures[BLOCK_DIRT][BLOCK_BOTTOM];
// Assign special_block_textures based on special_block_texture_idx
for(unsigned int i = 0; i < sizeof(special_block_texture_idx)/sizeof(*special_block_texture_idx); i++)
{
auto &idx = special_block_texture_idx[i];
special_block_textures[i] = terrain_atlas[idx.x][idx.y];
}
//Prerender four times the same texture to speed up drawing, see terrain.h
const BLOCK_TEXTURE quad_textures[] = { ALL(BLOCK_DIRT), SID(BLOCK_GRASS), TOP(BLOCK_GRASS), ALL(BLOCK_STONE), ALL(BLOCK_SAND), SID(BLOCK_WOOD), ALL(BLOCK_PLANKS_NORMAL), ALL(BLOCK_LEAVES) };
terrain_quad = newTexture(field_width * 2 * (sizeof(quad_textures)/sizeof(*quad_textures)), field_height * 2);
for(BLOCK b = 0; b <= BLOCK_NORMAL_LAST; b++)
for(uint8_t s = 0; s <= BLOCK_SIDE_LAST; s++)
quad_block_textures[b][s].has_quad = false;
unsigned int x = 0;
for(BLOCK_TEXTURE bt : quad_textures)
{
TextureAtlasEntry *tae = nullptr;
if(bt.sides & BLOCK_BOTTOM_BIT)
tae = &block_textures[bt.block][BLOCK_BOTTOM].current;
if(bt.sides & BLOCK_TOP_BIT)
tae = &block_textures[bt.block][BLOCK_TOP].current;
if(bt.sides & BLOCK_LEFT_BIT)
tae = &block_textures[bt.block][BLOCK_LEFT].current;
if(bt.sides & BLOCK_RIGHT_BIT)
tae = &block_textures[bt.block][BLOCK_RIGHT].current;
if(bt.sides & BLOCK_FRONT_BIT)
tae = &block_textures[bt.block][BLOCK_FRONT].current;
if(bt.sides & BLOCK_BACK_BIT)
tae = &block_textures[bt.block][BLOCK_BACK].current;
if(!tae)
{
printf("Block %d has no texture!\n", bt.block);
continue;
}
//- 1 to reverse the workaround above. Yes, I hate myself for this.
drawTexture(*terrain_current, *terrain_quad, tae->left - 1, tae->top - 1, field_width, field_height, x, 0, field_width, field_height);
drawTexture(*terrain_current, *terrain_quad, tae->left - 1, tae->top - 1, field_width, field_height, x + field_width, 0, field_width, field_height);
drawTexture(*terrain_current, *terrain_quad, tae->left - 1, tae->top - 1, field_width, field_height, x+ field_width, field_height, field_width, field_height);
drawTexture(*terrain_current, *terrain_quad, tae->left - 1, tae->top - 1, field_width, field_height, x, field_height, field_width, field_height);
//Get an average color of the block
RGB sum;
for(unsigned int tex_x = tae->left - 1; tex_x <= tae->right; ++tex_x)
for(unsigned int tex_y = tae->top - 1; tex_y <= tae->bottom; ++tex_y)
{
RGB rgb = rgbColor(terrain_current->bitmap[tex_x + tex_y*terrain_current->width]);
sum.r += rgb.r;
sum.g += rgb.g;
sum.b += rgb.b;
}
int pixels = field_width * field_height;
sum.r /= pixels;
sum.g /= pixels;
sum.b /= pixels;
auto colorWithBrightness = [](const RGB &c, const GLFix brightness) {
return colorRGB(c.r * brightness, c.g * brightness, c.b * brightness);
};
//And add the workaround here again..
TerrainQuadEntry tqe = {
true,
textureArea(x + 1, 1, field_width * 2 - 2, field_height * 2 - 2),
{ colorRGB(sum),
colorWithBrightness(sum, 0.90f),
colorWithBrightness(sum, 0.80f),
colorWithBrightness(sum, 0.85f),
colorWithBrightness(sum, 0.95f), }
};
if(bt.sides & BLOCK_BOTTOM_BIT)
quad_block_textures[bt.block][BLOCK_BOTTOM] = tqe;
if(bt.sides & BLOCK_TOP_BIT)
quad_block_textures[bt.block][BLOCK_TOP] = tqe;
if(bt.sides & BLOCK_LEFT_BIT)
quad_block_textures[bt.block][BLOCK_LEFT] = tqe;
if(bt.sides & BLOCK_RIGHT_BIT)
quad_block_textures[bt.block][BLOCK_RIGHT] = tqe;
if(bt.sides & BLOCK_FRONT_BIT)
quad_block_textures[bt.block][BLOCK_FRONT] = tqe;
if(bt.sides & BLOCK_BACK_BIT)
quad_block_textures[bt.block][BLOCK_BACK] = tqe;
x += field_width * 2;
}
//Part 2 of the hack above
quad_block_textures[BLOCK_GRASS][BLOCK_BOTTOM] = quad_block_textures[BLOCK_DIRT][BLOCK_BOTTOM];
if(lcd_type() == SCR_320x240_4)
{
greyscaleTexture(*terrain_current);
if(terrain_resized != terrain_current)
greyscaleTexture(*terrain_resized);
greyscaleTexture(*terrain_quad);
}
//Make the texture available to others for sharing
inv_selection_p = &inv_selection;
door_preview = newTexture(16, 32);
TextureAtlasEntry door = terrain_atlas[1][5].current;
door.bottom += door.bottom - door.top; //Double height
drawTexture(*terrain_current, *door_preview, door.left, door.top, door.right - door.left, door.bottom - door.top, 0, 0, 16, 32);
}
void terrainUninit()
{
if(terrain_resized != terrain_current)
deleteTexture(terrain_resized);
if(terrain_current != &terrain)
deleteTexture(terrain_current);
deleteTexture(terrain_quad);
deleteTexture(door_preview);
}