-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextiles.c
More file actions
135 lines (106 loc) · 3.6 KB
/
extiles.c
File metadata and controls
135 lines (106 loc) · 3.6 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
126
127
128
129
130
131
132
133
134
135
// SPDX-License-Identifier: MIT
#include "../../amp.h"
#include "utils.h"
static void draw_dungeon(struct amp_type *canvas, const struct amp_type *tiles);
int main(int argc, char **argv) {
auto gfx_blob = load_file("../tiles.amp");
if (!gfx_blob.data) {
return EXIT_FAILURE;
}
constexpr size_t width = 80;
constexpr size_t height = 24;
size_t canvas_size = amp_calc_size(width, height);
uint8_t *canvas_data = malloc(canvas_size);
if (!canvas_data) {
static const char message[] = "malloc: allocation failed\n";
write(2, message, strlen(message));
return EXIT_FAILURE;
}
struct amp_type canvas;
amp_init(&canvas, width, height, canvas_data, canvas_size);
const char *error_message = nullptr;
uint8_t *gfx_data = nullptr;
do {
uint32_t gfx_w, gfx_h;
size_t gfx_size = amp_doc_parse_size(
gfx_blob.data, gfx_blob.size, &gfx_w, &gfx_h
);
if (!gfx_size) {
error_message = "amp_parse_size: parse error\n";
break;
}
if ((gfx_data = malloc(gfx_size)) == nullptr) {
error_message = "malloc: allocation failed\n";
break;
}
struct amp_type gfx;
if (amp_init(&gfx, gfx_w, gfx_h, gfx_data, gfx_size) > gfx_size) {
error_message = "amp_init: not enough memory provided\n";
break;
}
if (!amp_decode(&gfx, gfx_blob.data, gfx_blob.size)) {
error_message = "amp_decode: parse error\n";
break;
}
draw_dungeon(&canvas, &gfx);
} while (false);
if (!error_message) {
amp_set_palette(&canvas, AMP_PAL_24BIT);
amp_to_ans(&canvas, nullptr, 0); // Write to stdout.
amp_stdout("\n", 1);
}
free(gfx_data);
free(canvas_data);
free(gfx_blob.data);
if (error_message) {
write(2, error_message, strlen(error_message));
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
static void draw_dungeon(
struct amp_type *canvas, const struct amp_type *tiles
) {
constexpr uint8_t dungeon_w = 10;
constexpr uint8_t dungeon_h = 6;
constexpr uint8_t dungeon_d = 2;
static const uint8_t dungeon[dungeon_d][dungeon_h][dungeon_w] = {
{
{ 4, 4, 4, 4, 5, 5, 4, 4, 4, 4 },
{ 4, 4, 5, 5, 5, 5, 5, 4, 4, 4 },
{ 5, 5, 5, 5, 5, 4, 5, 5, 5, 4 },
{ 4, 4, 5, 5, 5, 4, 5, 5, 4, 4 },
{ 4, 4, 4, 4, 5, 5, 5, 4, 4, 4 },
{ 4, 4, 4, 4, 4, 5, 5, 4, 4, 4 }
},
{
{ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 1, 0, 3, 0 },
{ 0, 0, 3, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 2, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
}
};
for (long d=0; d<dungeon_d; ++d) {
for (long y=0; y<dungeon_h; ++y) {
for (long x=0; x<dungeon_w; ++x) {
uint8_t tile_index = dungeon[d][y][x];
if (!tile_index) {
continue;
}
tile_index--;
uint8_t tile_w = 8;
uint8_t tile_h = 4;
long tile_from_x = tile_w * tile_index + tile_index;
long tile_from_y = 0;
long tile_to_x = tile_w * x;
long tile_to_y = tile_h * y;
amp_draw_ansmap_region(
canvas, tile_to_x, tile_to_y,
tiles, tile_from_x, tile_from_y, tile_w, tile_h
);
}
}
}
}