Back to floats.
This commit is contained in:
@@ -6,5 +6,4 @@
|
||||
# Sources
|
||||
target_sources(${DUSK_TARGET_NAME}
|
||||
PRIVATE
|
||||
worldunit.c
|
||||
)
|
||||
@@ -1,62 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "worldunit.h"
|
||||
#include "assert/assert.h"
|
||||
|
||||
void worldChunkPosAdd(worldchunkpos_t *pos, const worldsubtile_t amt) {
|
||||
assertNotNull(pos, "Position pointer cannot be NULL");
|
||||
|
||||
/*
|
||||
int32_t sum = (int32_t)pos->subtile + (int32_t)amt;
|
||||
int32_t shifted = sum + 128; // may be negative or large
|
||||
int32_t carry = div_floor(shifted, 256); // tiles to add (can be neg)
|
||||
int32_t rem = mod_floor(shifted, 256); // 0..255
|
||||
int32_t new_sub = rem - 128; // back to [-128,127]
|
||||
|
||||
pos->subtile = (int8_t)new_sub;
|
||||
pos->tile = (uint8_t)((int32_t)pos->tile + carry); // wrap mod 256
|
||||
*/
|
||||
|
||||
int32_t shiftedTotal = (int32_t)pos->subtile + (int32_t)amt + 128;
|
||||
int32_t tileCarry = shiftedTotal >> 8; // divide by 256
|
||||
int32_t wrappedSubtile = shiftedTotal - (tileCarry << 8);
|
||||
pos->subtile = (int8_t)(wrappedSubtile - 128);
|
||||
pos->tile = (uint8_t)(pos->tile + (uint8_t)tileCarry);
|
||||
}
|
||||
|
||||
void worldPosAddSubtile(worldpos_t *pos, const worldsubtile_t amt) {
|
||||
assertNotNull(pos, "Position pointer cannot be NULL");
|
||||
|
||||
// Same as worldChunkPosAdd but with chunk handling.
|
||||
int32_t shiftedTotal = (int32_t)pos->subtile + (int32_t)amt + 128;
|
||||
int32_t tileCarry = shiftedTotal >> 8; // divide by 256
|
||||
int32_t wrappedSubtile = shiftedTotal - (tileCarry << 8);
|
||||
pos->subtile = (int8_t)(wrappedSubtile - 128);
|
||||
int32_t newTile = (int32_t)pos->tile + (int32_t)tileCarry;
|
||||
int32_t chunkCarry = newTile / WORLD_CHUNK_SIZE;
|
||||
pos->tile = (uint8_t)(newTile % WORLD_CHUNK_SIZE);
|
||||
pos->chunk = (uint8_t)(pos->chunk + (uint8_t)chunkCarry);
|
||||
}
|
||||
|
||||
float_t worldChunkPosToF32(const worldchunkpos_t pos, const uint8_t tileSize) {
|
||||
const float_t scaleFactor = (float_t)tileSize * (1.0f / 256.0f);
|
||||
return (
|
||||
(float_t)pos.tile * (float_t)tileSize + ((float_t)pos.subtile + 128.0f) *
|
||||
scaleFactor
|
||||
);
|
||||
}
|
||||
|
||||
float_t worldPosToF32(const worldpos_t pos, const uint8_t tileSize) {
|
||||
const float_t scaleFactor = (float_t)tileSize * (1.0f / 256.0f);
|
||||
const float_t chunkFactor = WORLD_CHUNK_SIZE * (float_t)tileSize;
|
||||
return (
|
||||
(float_t)pos.chunk * chunkFactor +
|
||||
(float_t)pos.tile * (float_t)tileSize +
|
||||
((float_t)pos.subtile + 128.0f) * scaleFactor
|
||||
);
|
||||
}
|
||||
@@ -1,100 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "dusk.h"
|
||||
|
||||
#define WORLD_DIMENSIONS 3
|
||||
#define WORLD_SUBTITLE_MIN -128
|
||||
#define WORLD_SUBTITLE_MAX 127
|
||||
#define WORLD_CHUNK_SIZE 256 // Tiles per axis per chunk.
|
||||
|
||||
/**
|
||||
* Position in SUBTILE space in a world, each unit represents a single subtile.
|
||||
* This is divided by the size of the tile, e.g. if a tile is 16x16 then there
|
||||
* are 128 / tile size = units per pixel of a tile. This means there are always
|
||||
* between WORLD_SUBTITLE_MIN and WORLD_SUBTITLE_MAX subtiles in a tile.
|
||||
*
|
||||
* The extra benefit of this is that different tile sizes don't require any game
|
||||
* logic updates!
|
||||
*/
|
||||
typedef int8_t worldsubtile_t;
|
||||
|
||||
/**
|
||||
* Position in TILE space in a world, each unit represents a single tile. This
|
||||
* is within CHUNK space. This is not different depending on chunk size, if the
|
||||
* chunks are 32 tiles wide then the max tile value is 31.
|
||||
*/
|
||||
typedef uint8_t worldtile_t;
|
||||
|
||||
/**
|
||||
* Represents a position in a world in SUBTILE and TILE space. This is basically
|
||||
* just a convenience struct so you don't have to pass two variables around.
|
||||
*
|
||||
* For example, an entity may be at tile (2, 3) and subtile (8, 12).
|
||||
* meaning that the entity is at pixel (2 * TILE_SIZE + 8, 3 * TILE_SIZE + 12)
|
||||
* in world space.
|
||||
*
|
||||
* This is still within CHUNK space.
|
||||
*/
|
||||
typedef struct worldchunkpos_s {
|
||||
worldsubtile_t subtile;
|
||||
worldtile_t tile;
|
||||
} worldchunkpos_t;
|
||||
|
||||
/**
|
||||
* Position in CHUNK space in a world, each unit represents a single chunk.
|
||||
*/
|
||||
typedef uint8_t worldchunk_t;
|
||||
|
||||
/**
|
||||
* Represents a position in a world in SUBTILE, TILE and CHUNK space. This is in
|
||||
* WORLD space, so this is the full position of an entity in the world.
|
||||
*/
|
||||
typedef struct worldpos_s {
|
||||
worldsubtile_t subtile;
|
||||
worldtile_t tile;
|
||||
worldchunk_t chunk;
|
||||
} worldpos_t;
|
||||
|
||||
/**
|
||||
* Adds a number of subtiles to a world chunk position, rolling over into tiles
|
||||
* and chunks as necessary.
|
||||
*
|
||||
* @param pos Pointer to the world chunk position to modify.
|
||||
* @param amt The amount of subtiles to add (can be negative).
|
||||
*/
|
||||
void worldChunkPosAdd(worldchunkpos_t *pos, const worldsubtile_t amt);
|
||||
|
||||
/**
|
||||
* Adds a number of subtiles to a world position, rolling over into tiles and
|
||||
* chunks as necessary.
|
||||
*
|
||||
* @param pos Pointer to the world position to modify.
|
||||
* @param amt The amount of subtiles to add (can be negative).
|
||||
*/
|
||||
void worldPosAddSubtile(worldpos_t *pos, const worldsubtile_t amt);
|
||||
|
||||
/**
|
||||
* Converts a world chunk position to a floating point number, given the tile
|
||||
* size in pixels.
|
||||
*
|
||||
* @param pos Pointer to the world chunk position to convert.
|
||||
* @param tileSize The size of a tile in pixels.
|
||||
* @return The position as a floating point number.
|
||||
*/
|
||||
float_t worldChunkPosToF32(const worldchunkpos_t pos, const uint8_t tileSize);
|
||||
|
||||
/**
|
||||
* Converts a world position to a floating point number, given the tile size
|
||||
* in pixels.
|
||||
*
|
||||
* @param pos Pointer to the world position to convert.
|
||||
* @param tileSize The size of a tile in pixels.
|
||||
* @return The position as a floating point number.
|
||||
*/
|
||||
float_t worldPosToF32(const worldpos_t pos, const uint8_t tileSize);
|
||||
Reference in New Issue
Block a user