Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
ChunkBlob.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
18#pragma once
19
20#include "../core/Types.hpp"
22#include "TilemapSystem.hpp"
23
24#include <cstring>
25#include <vector>
26
27namespace esengine {
28namespace tilemap {
29
31ES_CONST(hex, ts=TILEMAP_BLOB_MAGIC_V1)
32static constexpr u32 BLOB_MAGIC_V1 = 0x4D545345;
33
35ES_CONST(hex, ts=TILEMAP_BLOB_MAGIC_V2)
36static constexpr u32 BLOB_MAGIC_V2 = 0x50414D54;
37
38// What 'ESTM' meant. History, not configuration: pointing these at the live
39// constants would make every old map read as whatever the engine is now, which
40// is the failure this file exists to refuse.
41ES_CONST(ts=TILEMAP_V1_CHUNK_SIZE)
42static constexpr u32 V1_CHUNK_SIZE = 16;
43ES_CONST(hex, ts=TILEMAP_V1_ID_MASK)
44static constexpr u16 V1_TILE_ID_MASK = 0x1FFF;
45ES_CONST(hex, ts=TILEMAP_V1_FLIP_H)
46static constexpr u16 V1_TILE_FLIP_H = 0x2000;
47ES_CONST(hex, ts=TILEMAP_V1_FLIP_V)
48static constexpr u16 V1_TILE_FLIP_V = 0x4000;
49ES_CONST(hex, ts=TILEMAP_V1_FLIP_D)
50static constexpr u16 V1_TILE_FLIP_D = 0x8000;
51
59
60 bool operator==(const CellEncoding& o) const {
61 return chunkSize == o.chunkSize && idMask == o.idMask
62 && flipH == o.flipH && flipV == o.flipV && flipD == o.flipD;
63 }
64};
65
69
75
76inline constexpr usize BLOB_HEADER_V1_BYTES = sizeof(u32) * 2;
77inline constexpr usize BLOB_HEADER_V2_BYTES = sizeof(u32) + sizeof(u16) * 6 + sizeof(u32);
78
85
86inline void appendBlobHeader(std::vector<u8>& out, u32 chunkCount) {
87 const CellEncoding enc = runningEncoding();
88 const u16 fields[6] = { enc.chunkSize, enc.idMask, enc.flipH, enc.flipV, enc.flipD, 0 };
89 auto put = [&out](const void* p, usize n) {
90 const u8* b = static_cast<const u8*>(p);
91 out.insert(out.end(), b, b + n);
92 };
93 put(&BLOB_MAGIC_V2, sizeof(BLOB_MAGIC_V2));
94 put(fields, sizeof(fields));
95 put(&chunkCount, sizeof(chunkCount));
96}
97
103inline bool parseBlobHeader(const u8* raw, usize size, BlobHeader& out) {
104 if (size < BLOB_HEADER_V1_BYTES) return false;
105 u32 magic = 0;
106 std::memcpy(&magic, raw, sizeof(magic));
107
108 if (magic == BLOB_MAGIC_V2) {
109 if (size < BLOB_HEADER_V2_BYTES) return false;
110 u16 fields[5] = {};
111 std::memcpy(fields, raw + sizeof(u32), sizeof(fields));
112 out.wrote = { fields[0], fields[1], fields[2], fields[3], fields[4] };
114 } else if (magic == BLOB_MAGIC_V1) {
115 out.wrote = v1Encoding();
117 } else {
118 return false;
119 }
120 if (out.wrote.chunkSize == 0) return false;
121 std::memcpy(&out.chunkCount, raw + out.payloadAt - sizeof(u32), sizeof(out.chunkCount));
122 return true;
123}
124
131inline bool recodeCell(u16 word, const CellEncoding& from, const CellEncoding& to, u16& out) {
132 const u16 id = static_cast<u16>(word & from.idMask);
133 if (id > to.idMask) return false;
134 u16 next = id;
135 if (word & from.flipH) next = static_cast<u16>(next | to.flipH);
136 if (word & from.flipV) next = static_cast<u16>(next | to.flipV);
137 if (word & from.flipD) next = static_cast<u16>(next | to.flipD);
138 out = next;
139 return true;
140}
141
147template <class Fn>
148inline bool walkBlobCells(const u8* raw, usize size, const BlobHeader& header, Fn&& emit) {
149 const u32 side = header.wrote.chunkSize;
150 const usize perChunk = sizeof(i32) * 2 + static_cast<usize>(side) * side * sizeof(u16);
151 if (size < header.payloadAt + static_cast<usize>(header.chunkCount) * perChunk) return false;
152
153 usize at = header.payloadAt;
154 for (u32 c = 0; c < header.chunkCount; ++c) {
155 i32 cx = 0, cy = 0;
156 std::memcpy(&cx, raw + at, sizeof(cx));
157 std::memcpy(&cy, raw + at + sizeof(i32), sizeof(cy));
158 const u8* cells = raw + at + sizeof(i32) * 2;
159 for (u32 i = 0; i < side * side; ++i) {
160 u16 word = 0;
161 std::memcpy(&word, cells + static_cast<usize>(i) * sizeof(u16), sizeof(word));
162 if (word == EMPTY_TILE) continue;
163 emit(static_cast<i32>(cx * static_cast<i32>(side) + static_cast<i32>(i % side)),
164 static_cast<i32>(cy * static_cast<i32>(side) + static_cast<i32>(i / side)), word);
165 }
166 at += perChunk;
167 }
168 return true;
169}
170
173inline i32 floorDiv(i32 a, i32 b) { return a / b - static_cast<i32>(a % b < 0); }
174inline i32 floorMod(i32 a, i32 b) { const i32 m = a % b; return m < 0 ? m + b : m; }
175
176} // namespace tilemap
177} // namespace esengine
Reflection and metadata system for automatic binding generation.
#define ES_CONST(...)
Mark a constant that CROSSES the boundary, so EHT emits its TS twin.
Core type definitions and utilities for ESEngine.
bool recodeCell(u16 word, const CellEncoding &from, const CellEncoding &to, u16 &out)
static constexpr u32 V1_CHUNK_SIZE
Definition ChunkBlob.hpp:42
static constexpr u32 BLOB_MAGIC_V1
Definition ChunkBlob.hpp:32
i32 floorMod(i32 a, i32 b)
bool parseBlobHeader(const u8 *raw, usize size, BlobHeader &out)
static constexpr u16 V1_TILE_FLIP_V
Definition ChunkBlob.hpp:48
static constexpr u32 CHUNK_SIZE
void appendBlobHeader(std::vector< u8 > &out, u32 chunkCount)
Definition ChunkBlob.hpp:86
i32 floorDiv(i32 a, i32 b)
static constexpr u16 V1_TILE_ID_MASK
Definition ChunkBlob.hpp:44
static constexpr u16 TILE_FLIP_D
static constexpr u16 TILE_FLIP_H
constexpr usize BLOB_HEADER_V2_BYTES
Definition ChunkBlob.hpp:77
constexpr usize BLOB_HEADER_V1_BYTES
Definition ChunkBlob.hpp:76
static constexpr u16 EMPTY_TILE
static constexpr u32 BLOB_MAGIC_V2
Definition ChunkBlob.hpp:36
CellEncoding runningEncoding()
Definition ChunkBlob.hpp:66
static constexpr u16 TILE_FLIP_V
static constexpr u16 V1_TILE_FLIP_H
Definition ChunkBlob.hpp:46
static constexpr u16 TILE_ID_MASK
CellEncoding v1Encoding()
Definition ChunkBlob.hpp:71
static constexpr u16 V1_TILE_FLIP_D
Definition ChunkBlob.hpp:50
bool walkBlobCells(const u8 *raw, usize size, const BlobHeader &header, Fn &&emit)
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
std::size_t usize
Size type for indexing and sizes.
Definition Types.hpp:64
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
std::uint8_t u8
Unsigned 8-bit integer.
Definition Types.hpp:41
std::uint16_t u16
Unsigned 16-bit integer.
Definition Types.hpp:43
bool operator==(const CellEncoding &o) const
Definition ChunkBlob.hpp:60