From e678c68b66df8ac7c93184b73a14313edc710530 Mon Sep 17 00:00:00 2001 From: Miloslav Ciz Date: Tue, 8 Aug 2023 16:17:51 +0200 Subject: [PATCH] Add getblockat function --- TODO.txt | 4 ++++ main.c | 8 ++++++++ map.h | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/TODO.txt b/TODO.txt index f68ec33..a6aec3d 100644 --- a/TODO.txt +++ b/TODO.txt @@ -23,6 +23,10 @@ - possibility to turn off textures completely - how to map textures to blocks? - figured out nice procedural mapping in Blender :) + - PROBABLY LIKE THIS: + - background textures: 128x128 with image-specific 256 color palette, + pixel access shouldn't be as time critical here + - other textures 64x64 with direct 565 values stored - Rendering: two pass: 1st pass renders the scaled down background (possibly not rendering floor, can be just pure green for "grass") to prevent overflows, second pass renders the tracks (or the nearest part of it). diff --git a/main.c b/main.c index 77012a4..008dc05 100644 --- a/main.c +++ b/main.c @@ -9,5 +9,13 @@ int main(void) LCR_debugPrintCurrentMap(); + + uint8_t *b = LCR_getMapBlockAtCoordNumber(0); + + if (!b) + printf("NO!\n"); + else + printf("%d\n",(b - LCR_currentMap.blocks) / 4); + return 0; } diff --git a/map.h b/map.h index e4fc7e4..581f249 100644 --- a/map.h +++ b/map.h @@ -16,6 +16,7 @@ The STORAGE map format is binary and consists of the following values: - 76, 77 (for "LM", magic number) + - one byte recording the map environment - ASCII map name - 10 (separator) - ASCII comment @@ -95,6 +96,8 @@ struct uint8_t blocks[LCR_SETTING_MAP_MAX_SIZE * 4]; uint32_t startPos; + uint8_t environment; + // TODO: name, desc? possibly as a single '\n' separated string? } LCR_currentMap; @@ -132,6 +135,32 @@ uint32_t LCR_mapBlockGetCoordNumber(const uint8_t block[4]) ((((uint32_t) block[3]) & 0x3) << 16); } +uint8_t *LCR_getMapBlockAtCoordNumber(uint32_t coord) +{ + // binary search the block: + + uint16_t a = 0, b = LCR_currentMap.blockCount - 1; + + while (b >= a) + { + uint16_t mid = (a + b) / 2; + + uint8_t *block = LCR_currentMap.blocks + mid * 4; + + uint32_t coord2 = + LCR_mapBlockGetCoordNumber(block); + + if (coord2 == coord) + return block; + else if (coord2 > coord) + b = mid - 1; + else + a = mid + 1; + } + + return 0; +} + /** Adds given block to current map, including possibly deleting a block by adding LCR_BLOCK_NONE. The function handles sorting the block to the right @@ -213,6 +242,10 @@ uint8_t LCR_mapLoad(const uint8_t *map) map++; + LCR_currentMap.environment = *map; + + map++; + while (*map != LCR_MAP_TERMINATOR) { if (!_LCR_mapAddBlock(map))