Compare commits

..

No commits in common. "da98c0941bdc9cff90fccbae901d513899c66c6e" and "1a7322dccf13109e173b21c595934d698f45506c" have entirely different histories.

22 changed files with 92 additions and 163 deletions

View File

@ -24,7 +24,6 @@ set(TWN_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR} CACHE INTERNAL "")
# feature configuration, set them with -DFEATURE=ON/OFF in cli
option(TWN_FEATURE_DYNLIB_GAME "Enable dynamic library loading support" ON)
option(TWN_USE_AMALGAM "Enable use of twn_amalgam.c as a single compilation unit" ON)
# todo: figure out how to compile for dynamic linking instead
if(EMSCRIPTEN)
@ -42,21 +41,19 @@ if(HAIKU)
endif()
# add -fPIC globally so that it's linked well
add_compile_options($<$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>:-fPIC>
-fvisibility=hidden)
add_compile_options($<$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>:-fPIC>)
set(PHYSFS_BUILD_SHARED FALSE CACHE INTERNAL "")
set(PHYSFS_DISABLE_INSTALL TRUE CACHE INTERNAL "")
set(PHYSFS_TARGETNAME_UNINSTALL "physfs_uninstall" CACHE INTERNAL "")
set(PHYSFS_ARCHIVE_GRP OFF CACHE BOOL "")
set(PHYSFS_ARCHIVE_WAD OFF CACHE INTERNAL "")
set(PHYSFS_ARCHIVE_HOG OFF CACHE INTERNAL "")
set(PHYSFS_ARCHIVE_MVL OFF CACHE INTERNAL "")
set(PHYSFS_ARCHIVE_QPAK OFF CACHE INTERNAL "")
set(PHYSFS_ARCHIVE_SLB OFF CACHE INTERNAL "")
set(PHYSFS_ARCHIVE_ISO9660 OFF CACHE INTERNAL "")
set(PHYSFS_ARCHIVE_VDF OFF CACHE INTERNAL "")
set(PHYSFS_ARCHIVE_7Z OFF CACHE INTERNAL "")
set(PHYSFS_BUILD_SHARED FALSE)
set(PHYSFS_DISABLE_INSTALL TRUE)
set(PHYSFS_TARGETNAME_UNINSTALL "physfs_uninstall")
set(PHYSFS_ARCHIVE_GRP OFF)
set(PHYSFS_ARCHIVE_WAD OFF)
set(PHYSFS_ARCHIVE_HOG OFF)
set(PHYSFS_ARCHIVE_MVL OFF)
set(PHYSFS_ARCHIVE_QPAK OFF)
set(PHYSFS_ARCHIVE_SLB OFF)
set(PHYSFS_ARCHIVE_ISO9660 OFF)
set(PHYSFS_ARCHIVE_VDF OFF)
add_subdirectory(third-party/physfs ${CMAKE_CURRENT_BINARY_DIR}/third-party/physfs SYSTEM)
add_subdirectory(third-party/libxm ${CMAKE_CURRENT_BINARY_DIR}/third-party/libxm SYSTEM)
@ -91,8 +88,7 @@ set(TWN_THIRD_PARTY_SOURCE_FILES
third-party/tomlc99/toml.c
$<$<NOT:$<BOOL:${EMSCRIPTEN}>>:third-party/glad/src/glad.c>)
set(TWN_NONOPT_SOURCE_FILES
src/twn_stb.c
set(TWN_SOURCE_FILES
src/twn_loop.c
src/twn_main.c
src/twn_context.c include/twn_context.h
@ -108,10 +104,7 @@ set(TWN_NONOPT_SOURCE_FILES
src/rendering/twn_triangles.c
src/rendering/twn_circles.c
src/rendering/twn_skybox.c
src/rendering/twn_fog.c)
set(TWN_SOURCE_FILES
$<IF:$<BOOL:${TWN_USE_AMALGAM}>,src/twn_amalgam.c src/twn_stb.c,${TWN_NONOPT_SOURCE_FILES}>
src/rendering/twn_fog.c
# for dynamic load based solution main is compiled in a separate target
$<$<NOT:$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>>:src/twn_main.c
@ -120,7 +113,6 @@ set(TWN_SOURCE_FILES
${SYSTEM_SOURCE_FILES})
list(TRANSFORM TWN_SOURCE_FILES PREPEND ${TWN_ROOT_DIR}/)
source_group(TREE ${TWN_ROOT_DIR} FILES ${TWN_NONOPT_SOURCE_FILES})
add_library(twn_third_parties STATIC ${TWN_THIRD_PARTY_SOURCE_FILES})
@ -131,6 +123,8 @@ else()
add_library(${TWN_TARGET} STATIC ${TWN_SOURCE_FILES} ${twn_third_parties})
endif()
source_group(TREE ${TWN_ROOT_DIR} FILES ${TWN_SOURCE_FILES})
set_target_properties(${TWN_TARGET} PROPERTIES
C_STANDARD 11
C_STANDARD_REQUIRED ON
@ -139,8 +133,7 @@ set_target_properties(${TWN_TARGET} PROPERTIES
# precompile commonly used not-so-small headers
target_precompile_headers(${TWN_TARGET} PRIVATE
$<$<NOT:$<BOOL:${EMSCRIPTEN}>>:third-party/glad/include/glad/glad.h>
${SDL2_INCLUDE_DIR}/SDL.h
third-party/physfs/src/physfs.h)
third-party/stb/stb_ds.h)
function(give_options_without_warnings target)
@ -175,19 +168,20 @@ function(give_options_without_warnings target)
target_compile_options(${target} PUBLIC
${BUILD_FLAGS}
$<$<CONFIG:Release>:${BUILD_FLAGS_RELEASE}>
$<$<CONFIG:Debug>:${BUILD_FLAGS_DEBUG}>)
$<$<CONFIG:Debug>:${BUILD_FLAGS_DEBUG}>
$<$<BOOL:${LINUX}>:-Wl,-rpath,$ORIGIN/>)
target_link_options(${target} PUBLIC
${BUILD_FLAGS}
# -Wl,--no-undefined # TODO: use later for implementing no-libc
$<$<CONFIG:Release>:${BUILD_FLAGS_RELEASE}>
$<$<CONFIG:Debug>:${BUILD_FLAGS_DEBUG}>
$<$<BOOL:${LINUX}>:-Wl,-rpath,$ORIGIN/>
-Bsymbolic-functions
-Wl,--hash-style=gnu)
target_compile_definitions(${target} PRIVATE
$<$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>:TWN_FEATURE_DYNLIB_GAME>
_GNU_SOURCE)
$<$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>:TWN_FEATURE_DYNLIB_GAME>)
endfunction()
@ -199,8 +193,7 @@ function(give_options target)
-Wno-padded
-Wno-declaration-after-statement
-Wno-unsafe-buffer-usage
-Wno-unused-command-line-argument
-Wno-covered-switch-default)
-Wno-unused-command-line-argument)
set(WARNING_FLAGS
-Wall
@ -264,8 +257,6 @@ function(use_townengine target sources output_directory)
# launcher binary, loads game and engine shared library
add_executable(${target} ${TWN_ROOT_DIR}/src/twn_main.c)
target_compile_options(${target} PRIVATE $<$<BOOL:${LINUX}>:-Wl,-rpath,$ORIGIN/>)
# todo: copy instead?
# put libtownengine.so alongside the binary
set_target_properties(${TWN_TARGET} PROPERTIES

View File

@ -13,7 +13,7 @@
#define RIGHT_CLICK_ADD 500
static void handle_input(void)
void handle_input(void)
{
State *state = ctx.udata;

View File

@ -1,12 +1,7 @@
#!/bin/env sh
# check whether ninja is around (you better start running)
if [ -x "$(command -v ninja)" ]; then
generator="-G Ninja"
fi
if [ "$1" = "web" ]; then
emcmake cmake $generator -B .build-web "${@:2}" && cmake --build .build-web --parallel
emcmake cmake -B .build-web "${@:2}" && cmake --build .build-web --parallel
else
cmake $generator -B .build "$@" && cmake --build .build --parallel
cmake -B .build "$@" && cmake --build .build --parallel
fi

View File

@ -4,7 +4,7 @@
#if defined(__WIN32)
#define TWN_API __declspec(dllexport)
#else
#define TWN_API __attribute__((visibility("default")))
#define TWN_API
#endif
#endif

View File

@ -83,7 +83,6 @@ static void watcher_callback(XWATCHER_FILE_EVENT event,
(void)data;
switch(event) {
case XWATCHER_FILE_CREATED:
case XWATCHER_FILE_MODIFIED:
SDL_LockMutex(lock);
last_tick_modified = ctx.game.frame_number;
@ -91,12 +90,6 @@ static void watcher_callback(XWATCHER_FILE_EVENT event,
SDL_UnlockMutex(lock);
break;
case XWATCHER_FILE_UNSPECIFIED:
case XWATCHER_FILE_REMOVED:
case XWATCHER_FILE_OPENED:
case XWATCHER_FILE_ATTRIBUTES_CHANGED:
case XWATCHER_FILE_NONE:
case XWATCHER_FILE_RENAMED:
default:
break;
}

View File

@ -223,8 +223,6 @@ static void render_2d(void) {
case PRIMITIVE_2D_TEXT:
render_text(&current->text);
break;
default:
SDL_assert(false);
}
}
}

View File

@ -194,8 +194,6 @@ void use_2d_pipeline(void);
void use_texture_mode(TextureMode mode);
void upload_quad_vertices(Rect rect);
void finally_render_sprites(Primitive2D const primitives[],
struct SpriteBatch batch,
VertexBuffer buffer);

View File

@ -5,7 +5,6 @@
#include "twn_text_c.h"
#include <glad/glad.h>
#include <stb_ds.h>
/* interleaved vertex array data */

View File

@ -3,7 +3,7 @@
#include <SDL2/SDL.h>
static char *paths_in_use;
char *paths_in_use;
void draw_skybox(const char *paths) {
if (paths_in_use && SDL_strcmp(paths, paths_in_use) == 0)

View File

@ -199,14 +199,9 @@ void render_sprites(const Primitive2D primitives[],
v2 = (Vec2){ sprite.rect.x + sprite.rect.w, sprite.rect.y + sprite.rect.h };
v3 = (Vec2){ sprite.rect.x + sprite.rect.w, sprite.rect.y };
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
} else if (sprite.rect.w == sprite.rect.h) {
/* rotated square case */
#pragma GCC diagnostic pop
const Vec2 c = frect_center(sprite.rect);
const Vec2 t = fast_cossine(sprite.rotation + (float)M_PI_4);
const Vec2 d = {

View File

@ -5,7 +5,7 @@
#include "twn_engine_context_c.h"
#include <stb_truetype.h>
#include <stb_ds.h>
#define ASCII_START 32
#define ASCII_END 128

View File

@ -35,7 +35,7 @@ void draw_triangle(const char *path,
.uv2 = uv2,
}};
union UncoloredSpaceTriangle *triangles = (union UncoloredSpaceTriangle *)(void *)batch_p->value.primitives;
union UncoloredSpaceTriangle *triangles = (union UncoloredSpaceTriangle *)batch_p->value.primitives;
arrpush(triangles, triangle);
batch_p->value.primitives = (uint8_t *)triangles;
@ -66,7 +66,7 @@ void draw_uncolored_space_traingle_batch(struct MeshBatch *batch,
/* update pixel-based uvs to correspond with texture atlases */
for (size_t i = 0; i < primitives_len; ++i) {
struct UncoloredSpaceTrianglePayload *payload =
&((union UncoloredSpaceTriangle *)(void *)batch->primitives)[i].payload;
&((union UncoloredSpaceTriangle *)batch->primitives)[i].payload;
payload->uv0.x = xr + ((float)payload->uv0.x / srcrect.w) * wr;
payload->uv0.y = yr + ((float)payload->uv0.y / srcrect.h) * hr;

View File

@ -7,6 +7,7 @@
#include <sys/auxv.h>
#include <elf.h>
#include <linux/limits.h>
#define __USE_GNU
#include <dlfcn.h>
#include <SDL2/SDL.h>

View File

@ -1,19 +0,0 @@
/* a technique for faster compilation */
/* it includes all non-optional .c files directly in a single compilation unit */
#include "twn_audio.c"
#include "twn_camera.c"
#include "twn_context.c"
#include "twn_input.c"
#include "twn_loop.c"
#include "twn_main.c"
#include "twn_textures.c"
#include "twn_util.c"
#include "rendering/twn_circles.c"
#include "rendering/twn_draw.c"
#include "rendering/twn_fog.c"
#include "rendering/twn_skybox.c"
#include "rendering/twn_sprites.c"
#include "rendering/twn_text.c"
#include "rendering/twn_triangles.c"

View File

@ -126,8 +126,6 @@ static union AudioContext init_audio_context(const char *path, AudioFileType typ
};
}
case AUDIO_FILE_TYPE_UNKNOWN:
case AUDIO_FILE_TYPE_COUNT:
default:
CRY("Audio error", "Unhandled audio format (in init)");
return (union AudioContext){0};
@ -149,8 +147,6 @@ static void repeat_audio(AudioChannel *channel) {
break;
}
case AUDIO_FILE_TYPE_UNKNOWN:
case AUDIO_FILE_TYPE_COUNT:
default:
CRY("Audio error", "Unhandled audio format (in repeat)");
break;
@ -223,8 +219,6 @@ TWN_API void audio_set(const char *channel, AudioParam param, float value) {
}
pair->value.panning = value;
break;
default:
CRY("Audio channel parameter setting failed", "Invalid parameter enum given");
}
}
@ -235,8 +229,8 @@ static void audio_mixin_streams(const AudioChannel *channel,
uint8_t *restrict b,
size_t frames)
{
float *const sa = (float *)(void *)a;
float *const sb = (float *)(void *)b;
float *const sa = (float *)a;
float *const sb = (float *)b;
const float left_panning = fminf(fabsf(channel->panning - 1.0f), 1.0f);
const float right_panning = fminf(fabsf(channel->panning + 1.0f), 1.0f);
@ -327,8 +321,6 @@ static void audio_sample_and_mixin_channel(const AudioChannel *channel,
break;
}
case AUDIO_FILE_TYPE_UNKNOWN:
case AUDIO_FILE_TYPE_COUNT:
default:
CRY("Audio error", "Unhandled audio format (in sampling)");
break;

View File

@ -15,12 +15,6 @@ static void update_action_pressed_state(InputState *input, Action *action) {
switch (action->bindings[i].source) {
case BUTTON_SOURCE_NOT_SET:
break;
case BUTTON_SOURCE_KEYBOARD_CHARACTER:
CRY("Action pressed state updated failed", "BUTTON_SOURCE_KEYBOARD_CHARACTER isn't handled");
break;
case BUTTON_SOURCE_GAMEPAD:
CRY("Action pressed state updated failed", "BUTTON_SOURCE_GAMEPAD isn't handled");
break;
case BUTTON_SOURCE_KEYBOARD_PHYSICAL:
/* not pressed */
if (input->keyboard_state[action->bindings[i].code.scancode] == 0) {
@ -104,8 +98,6 @@ static void input_bind_code_to_action(InputState *input,
case BUTTON_SOURCE_MOUSE:
is_already_bound = binding->code.mouse_button == code.mouse_button;
break;
default:
SDL_assert(false);
}
if (is_already_bound) {
@ -164,8 +156,6 @@ static void input_unbind_code_from_action(InputState *input,
case BUTTON_SOURCE_MOUSE:
is_bound = binding->code.mouse_button == code.mouse_button;
break;
default:
SDL_assert(false);
}
/* stop early, this won't change */
@ -230,7 +220,7 @@ void input_bind_action_control(char const *action_name,
input_bind_code_to_action(&ctx.input,
action_name,
BUTTON_SOURCE_MOUSE,
(union ButtonCode) { .mouse_button = (uint8_t)SDL_BUTTON(mouse_button)});
(union ButtonCode) { .mouse_button = SDL_BUTTON(mouse_button)});
} else
log_warn("(%s) Invalid control value given: %i.", __func__, control);
}
@ -252,7 +242,7 @@ void input_unbind_action_control(char const *action_name,
input_unbind_code_from_action(&ctx.input,
action_name,
BUTTON_SOURCE_MOUSE,
(union ButtonCode) { .mouse_button = (uint8_t)SDL_BUTTON(mouse_button)});
(union ButtonCode) { .mouse_button = SDL_BUTTON(mouse_button)});
} else
log_warn("(%s) Invalid control value given: %i.", __func__, control);
}

View File

@ -112,7 +112,7 @@ static void APIENTRY opengl_log(GLenum source,
#endif
static void preserve_persistent_ctx_fields(void) {
void preserve_persistent_ctx_fields(void) {
ctx.game.udata = ctx.game_copy.udata;
}

View File

@ -1,24 +0,0 @@
/* single compilation unit for every stb implementation */
#define STB_DS_IMPLEMENTATION
#define STBDS_ASSERT SDL_assert
#define STBDS_REALLOC(context,ptr,size) ((void)(context), SDL_realloc(ptr, size))
#define STBDS_FREE(context,ptr) ((void)(context), SDL_free(ptr))
#include <stb_ds.h>
#define STB_RECT_PACK_IMPLEMENTATION
#define STBRP_SORT SDL_qsort
#define STBRP_ASSERT SDL_assert
#include <stb_rect_pack.h>
#define STB_TRUETYPE_IMPLEMENTATION
#define STBTT_malloc(x,u) ((void)(u), SDL_malloc(x))
#define STBTT_free(x,u) ((void)(u), SDL_free(x))
#define STBTT_assert(x) SDL_assert(x)
#define STBTT_strlen(x) SDL_strlen(x)
#define STBTT_memcpy SDL_memcpy
#define STBTT_memset SDL_memset
#include <stb_truetype.h>
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>

View File

@ -8,6 +8,8 @@
#include <physfsrwops.h>
#include <stb_ds.h>
#include <stb_rect_pack.h>
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#include <stdbool.h>
@ -394,7 +396,7 @@ void textures_update_atlas(TextureCache *cache) {
add_new_atlas(cache);
++cache->atlas_index;
}
}
};
update_texture_rects_in_atlas(cache, rects);
recreate_current_atlas_texture(cache);

View File

@ -4,7 +4,23 @@
#include <SDL2/SDL.h>
#include <physfsrwops.h>
#define STB_DS_IMPLEMENTATION
#define STBDS_ASSERT SDL_assert
#define STBDS_REALLOC(context,ptr,size) ((void)(context), SDL_realloc(ptr, size))
#define STBDS_FREE(context,ptr) ((void)(context), SDL_free(ptr))
#include <stb_ds.h>
#define STB_RECT_PACK_IMPLEMENTATION
#define STBRP_SORT SDL_qsort
#define STBRP_ASSERT SDL_assert
#include <stb_rect_pack.h>
#define STB_TRUETYPE_IMPLEMENTATION
#define STBTT_malloc(x,u) ((void)(u), SDL_malloc(x))
#define STBTT_free(x,u) ((void)(u), SDL_free(x))
#define STBTT_assert(x) SDL_assert(x)
#define STBTT_strlen(x) SDL_strlen(x)
#define STBTT_memcpy SDL_memcpy
#define STBTT_memset SDL_memset
#include <stb_truetype.h>
#include <stdarg.h>
@ -17,16 +33,10 @@ void cry_impl(const char *file, const int line, const char *title, const char *t
static void log_impl(const char *restrict format, va_list args, SDL_LogPriority priority) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION,
priority,
format,
args);
#pragma GCC diagnostic pop
}
@ -115,6 +125,7 @@ int clampi(int i, int min, int max) {
}
int64_t file_to_bytes(const char *path, unsigned char **buf_out) {
SDL_RWops *handle = PHYSFSRWOPS_openRead(path);
@ -261,12 +272,10 @@ void tick_timer(int *value) {
*value = MAX(*value - 1, 0);
}
void tick_ftimer(float *value) {
*value = MAX(*value - ((float)ctx.delta_time / (float)ctx.clocks_per_second), 0.0f);
}
bool repeat_ftimer(float *value, float at) {
*value -= (float)ctx.delta_time / (float)ctx.clocks_per_second;
if (*value < 0.0f) {

View File

@ -85,7 +85,16 @@ set(PHYSFS_SRCS
src/physfs_platform_android.c
src/physfs_archiver_dir.c
src/physfs_archiver_unpacked.c
src/physfs_archiver_grp.c
src/physfs_archiver_hog.c
src/physfs_archiver_7z.c
src/physfs_archiver_mvl.c
src/physfs_archiver_qpak.c
src/physfs_archiver_wad.c
src/physfs_archiver_zip.c
src/physfs_archiver_slb.c
src/physfs_archiver_iso9660.c
src/physfs_archiver_vdf.c
${PHYSFS_CPP_SRCS}
${PHYSFS_M_SRCS}
)

View File

@ -476,7 +476,7 @@ extern void stbds_rand_seed(size_t seed);
// these are the hash functions used internally if you want to test them or use them for other purposes
extern size_t stbds_hash_bytes(void *p, size_t len, size_t seed);
extern size_t stbds_hash_string(char const*str, size_t seed);
extern size_t stbds_hash_string(char *str, size_t seed);
// this is a simple string arena allocator, initialize with e.g. 'stbds_string_arena my_arena={0}'.
typedef struct stbds_string_arena stbds_string_arena;
@ -494,11 +494,11 @@ extern void stbds_unit_tests(void);
extern void * stbds_arrgrowf(void *a, size_t elemsize, size_t addlen, size_t min_cap);
extern void stbds_arrfreef(void *a);
extern void stbds_hmfree_func(void *p, size_t elemsize);
extern void * stbds_hmget_key(void *a, size_t elemsize, void const*key, size_t keysize, int mode);
extern void * stbds_hmget_key_ts(void *a, size_t elemsize, void const*key, size_t keysize, ptrdiff_t *temp, int mode);
extern void * stbds_hmget_key(void *a, size_t elemsize, void *key, size_t keysize, int mode);
extern void * stbds_hmget_key_ts(void *a, size_t elemsize, void *key, size_t keysize, ptrdiff_t *temp, int mode);
extern void * stbds_hmput_default(void *a, size_t elemsize);
extern void * stbds_hmput_key(void *a, size_t elemsize, void const*key, size_t keysize, int mode);
extern void * stbds_hmdel_key(void *a, size_t elemsize, void const*key, size_t keysize, size_t keyoffset, int mode);
extern void * stbds_hmput_key(void *a, size_t elemsize, void *key, size_t keysize, int mode);
extern void * stbds_hmdel_key(void *a, size_t elemsize, void *key, size_t keysize, size_t keyoffset, int mode);
extern void * stbds_shmode_func(size_t elemsize, int mode);
#ifdef __cplusplus
@ -531,7 +531,7 @@ extern void * stbds_shmode_func(size_t elemsize, int mode);
#define STBDS_OFFSETOF(var,field) ((char *) &(var)->field - (char *) (var))
#define stbds_header(t) (((stbds_array_header *) (void *) (t)) - 1)
#define stbds_header(t) ((stbds_array_header *) (t) - 1)
#define stbds_temp(t) stbds_header(t)->temp
#define stbds_temp_key(t) (*(char **) stbds_header(t)->hash_table)
@ -561,7 +561,7 @@ extern void * stbds_shmode_func(size_t elemsize, int mode);
#define stbds_arrgrow(a,b,c) ((a) = stbds_arrgrowf_wrapper((a), sizeof *(a), (b), (c)))
#define stbds_hmput(t, k, v) \
((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (void const*) STBDS_ADDRESSOF((t)->key, (k)), sizeof (t)->key, 0), \
((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (void*) STBDS_ADDRESSOF((t)->key, (k)), sizeof (t)->key, 0), \
(t)[stbds_temp((t)-1)].key = (k), \
(t)[stbds_temp((t)-1)].value = (v))
@ -570,11 +570,11 @@ extern void * stbds_shmode_func(size_t elemsize, int mode);
(t)[stbds_temp((t)-1)] = (s))
#define stbds_hmgeti(t,k) \
((t) = stbds_hmget_key_wrapper((t), sizeof *(t), (void const*) STBDS_ADDRESSOF((t)->key, (k)), sizeof (t)->key, STBDS_HM_BINARY), \
((t) = stbds_hmget_key_wrapper((t), sizeof *(t), (void*) STBDS_ADDRESSOF((t)->key, (k)), sizeof (t)->key, STBDS_HM_BINARY), \
stbds_temp((t)-1))
#define stbds_hmgeti_ts(t,k,temp) \
((t) = stbds_hmget_key_ts_wrapper((t), sizeof *(t), (void const*) STBDS_ADDRESSOF((t)->key, (k)), sizeof (t)->key, &(temp), STBDS_HM_BINARY), \
((t) = stbds_hmget_key_ts_wrapper((t), sizeof *(t), (void*) STBDS_ADDRESSOF((t)->key, (k)), sizeof (t)->key, &(temp), STBDS_HM_BINARY), \
(temp))
#define stbds_hmgetp(t, k) \
@ -584,7 +584,7 @@ extern void * stbds_shmode_func(size_t elemsize, int mode);
((void) stbds_hmgeti_ts(t,k,temp), &(t)[temp])
#define stbds_hmdel(t,k) \
(((t) = stbds_hmdel_key_wrapper((t),sizeof *(t), (void const*) STBDS_ADDRESSOF((t)->key, (k)), sizeof (t)->key, STBDS_OFFSETOF((t),key), STBDS_HM_BINARY)),(t)?stbds_temp((t)-1):0)
(((t) = stbds_hmdel_key_wrapper((t),sizeof *(t), (void*) STBDS_ADDRESSOF((t)->key, (k)), sizeof (t)->key, STBDS_OFFSETOF((t),key), STBDS_HM_BINARY)),(t)?stbds_temp((t)-1):0)
#define stbds_hmdefault(t, v) \
((t) = stbds_hmput_default_wrapper((t), sizeof *(t)), (t)[-1].value = (v))
@ -603,28 +603,28 @@ extern void * stbds_shmode_func(size_t elemsize, int mode);
#define stbds_hmgetp_null(t,k) (stbds_hmgeti(t,k) == -1 ? NULL : &(t)[stbds_temp((t)-1)])
#define stbds_shput(t, k, v) \
((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (void const*) (k), sizeof (t)->key, STBDS_HM_STRING), \
((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (void*) (k), sizeof (t)->key, STBDS_HM_STRING), \
(t)[stbds_temp((t)-1)].value = (v))
#define stbds_shputi(t, k, v) \
((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (void const*) (k), sizeof (t)->key, STBDS_HM_STRING), \
((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (void*) (k), sizeof (t)->key, STBDS_HM_STRING), \
(t)[stbds_temp((t)-1)].value = (v), stbds_temp((t)-1))
#define stbds_shputs(t, s) \
((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (void const*) (s).key, sizeof (s).key, STBDS_HM_STRING), \
((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (void*) (s).key, sizeof (s).key, STBDS_HM_STRING), \
(t)[stbds_temp((t)-1)] = (s), \
(t)[stbds_temp((t)-1)].key = stbds_temp_key((t)-1)) // above line overwrites whole structure, so must rewrite key here if it was allocated internally
#define stbds_pshput(t, p) \
((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (void const*) (p)->key, sizeof (p)->key, STBDS_HM_PTR_TO_STRING), \
((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (void*) (p)->key, sizeof (p)->key, STBDS_HM_PTR_TO_STRING), \
(t)[stbds_temp((t)-1)] = (p))
#define stbds_shgeti(t,k) \
((t) = stbds_hmget_key_wrapper((t), sizeof *(t), (void const*) (k), sizeof (t)->key, STBDS_HM_STRING), \
((t) = stbds_hmget_key_wrapper((t), sizeof *(t), (void*) (k), sizeof (t)->key, STBDS_HM_STRING), \
stbds_temp((t)-1))
#define stbds_pshgeti(t,k) \
((t) = stbds_hmget_key_wrapper((t), sizeof *(t), (void const*) (k), sizeof (*(t))->key, STBDS_HM_PTR_TO_STRING), \
((t) = stbds_hmget_key_wrapper((t), sizeof *(t), (void*) (k), sizeof (*(t))->key, STBDS_HM_PTR_TO_STRING), \
stbds_temp((t)-1))
#define stbds_shgetp(t, k) \
@ -634,9 +634,9 @@ extern void * stbds_shmode_func(size_t elemsize, int mode);
((void) stbds_pshgeti(t,k), (t)[stbds_temp((t)-1)])
#define stbds_shdel(t,k) \
(((t) = stbds_hmdel_key_wrapper((t),sizeof *(t), (void const*) (k), sizeof (t)->key, STBDS_OFFSETOF((t),key), STBDS_HM_STRING)),(t)?stbds_temp((t)-1):0)
(((t) = stbds_hmdel_key_wrapper((t),sizeof *(t), (void*) (k), sizeof (t)->key, STBDS_OFFSETOF((t),key), STBDS_HM_STRING)),(t)?stbds_temp((t)-1):0)
#define stbds_pshdel(t,k) \
(((t) = stbds_hmdel_key_wrapper((t),sizeof *(t), (void const*) (k), sizeof (*(t))->key, STBDS_OFFSETOF(*(t),key), STBDS_HM_PTR_TO_STRING)),(t)?stbds_temp((t)-1):0)
(((t) = stbds_hmdel_key_wrapper((t),sizeof *(t), (void*) (k), sizeof (*(t))->key, STBDS_OFFSETOF(*(t),key), STBDS_HM_PTR_TO_STRING)),(t)?stbds_temp((t)-1):0)
#define stbds_sh_new_arena(t) \
((t) = stbds_shmode_func_wrapper(t, sizeof *(t), STBDS_SH_ARENA))
@ -702,10 +702,10 @@ template<class T> static T * stbds_hmget_key_ts_wrapper(T *a, size_t elemsize, v
template<class T> static T * stbds_hmput_default_wrapper(T *a, size_t elemsize) {
return (T*)stbds_hmput_default((void *)a, elemsize);
}
template<class T> static T * stbds_hmput_key_wrapper(T *a, size_t elemsize, void const*key, size_t keysize, int mode) {
template<class T> static T * stbds_hmput_key_wrapper(T *a, size_t elemsize, void *key, size_t keysize, int mode) {
return (T*)stbds_hmput_key((void*)a, elemsize, key, keysize, mode);
}
template<class T> static T * stbds_hmdel_key_wrapper(T *a, size_t elemsize, void const*key, size_t keysize, size_t keyoffset, int mode){
template<class T> static T * stbds_hmdel_key_wrapper(T *a, size_t elemsize, void *key, size_t keysize, size_t keyoffset, int mode){
return (T*)stbds_hmdel_key((void*)a, elemsize, key, keysize, keyoffset, mode);
}
template<class T> static T * stbds_shmode_func_wrapper(T *, size_t elemsize, int mode) {
@ -1013,7 +1013,7 @@ static stbds_hash_index *stbds_make_hash_index(size_t slot_count, stbds_hash_ind
#define STBDS_ROTATE_LEFT(val, n) (((val) << (n)) | ((val) >> (STBDS_SIZE_T_BITS - (n))))
#define STBDS_ROTATE_RIGHT(val, n) (((val) >> (n)) | ((val) << (STBDS_SIZE_T_BITS - (n))))
size_t stbds_hash_string(char const*str, size_t seed)
size_t stbds_hash_string(char *str, size_t seed)
{
size_t hash = seed;
while (*str)
@ -1278,7 +1278,7 @@ static ptrdiff_t stbds_hm_find_slot(void *a, size_t elemsize, void *key, size_t
/* NOTREACHED */
}
void * stbds_hmget_key_ts(void *a, size_t elemsize, void const*key, size_t keysize, ptrdiff_t *temp, int mode)
void * stbds_hmget_key_ts(void *a, size_t elemsize, void *key, size_t keysize, ptrdiff_t *temp, int mode)
{
size_t keyoffset = 0;
if (a == NULL) {
@ -1309,7 +1309,7 @@ void * stbds_hmget_key_ts(void *a, size_t elemsize, void const*key, size_t keysi
}
}
void * stbds_hmget_key(void *a, size_t elemsize, void const*key, size_t keysize, int mode)
void * stbds_hmget_key(void *a, size_t elemsize, void *key, size_t keysize, int mode)
{
ptrdiff_t temp;
void *p = stbds_hmget_key_ts(a, elemsize, key, keysize, &temp, mode);
@ -1334,7 +1334,7 @@ void * stbds_hmput_default(void *a, size_t elemsize)
static char *stbds_strdup(char *str);
void *stbds_hmput_key(void *a, size_t elemsize, void const *key, size_t keysize, int mode)
void *stbds_hmput_key(void *a, size_t elemsize, void *key, size_t keysize, int mode)
{
size_t keyoffset=0;
void *raw_a;
@ -1370,7 +1370,7 @@ void *stbds_hmput_key(void *a, size_t elemsize, void const *key, size_t keysize,
// we iterate hash table explicitly because we want to track if we saw a tombstone
{
size_t hash = mode >= STBDS_HM_STRING ? stbds_hash_string((char const*)key,table->seed) : stbds_hash_bytes(key, keysize,table->seed);
size_t hash = mode >= STBDS_HM_STRING ? stbds_hash_string((char*)key,table->seed) : stbds_hash_bytes(key, keysize,table->seed);
size_t step = STBDS_BUCKET_LENGTH;
size_t pos;
ptrdiff_t tombstone = -1;
@ -1469,7 +1469,7 @@ void * stbds_shmode_func(size_t elemsize, int mode)
return STBDS_ARR_TO_HASH(a,elemsize);
}
void * stbds_hmdel_key(void *a, size_t elemsize, void const*key, size_t keysize, size_t keyoffset, int mode)
void * stbds_hmdel_key(void *a, size_t elemsize, void *key, size_t keysize, size_t keyoffset, int mode)
{
if (a == NULL) {
return 0;