Skip to content

Commit

Permalink
tweaks from cute_jam_2018
Browse files Browse the repository at this point in the history
  • Loading branch information
RandyGaul committed Jun 4, 2018
1 parent 5756d21 commit 18c196e
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 31 deletions.
25 changes: 19 additions & 6 deletions cute_alloc.h
Expand Up @@ -49,6 +49,11 @@ ca_frame_t* ca_frame_create(void* memory_chunk, size_t size);
void* ca_frame_alloc(ca_frame_t* frame, size_t size);
void ca_frame_free(ca_frame_t* frame);

typedef struct ca_heap_t ca_heap_t;
ca_heap_t* ca_heap_create(void* memory_chunk, size_t size);
void* ca_heap_alloc(ca_heap_t* frame, size_t size);
void ca_heap_free(ca_heap_t* frame);

// define these to your own user definition as necessary
#if !defined(CUTE_ALLOC_ALLOC)
#define CUTE_ALLOC_ALLOC(size, ctx) ca_leak_check_alloc((size), (char*)__FILE__, __LINE__)
Expand All @@ -65,22 +70,25 @@ void ca_frame_free(ca_frame_t* frame);
// 1 - use the leak checker
// 0 - use plain malloc/free
#define CUTE_ALLOC_LEAK_CHECK 1
void* ca_leak_check_alloc(size_t size, char* file, int line);
void* ca_leak_check_calloc(size_t count, size_t element_size, char* file, int line);
void* ca_leak_check_alloc(size_t size, const char* file, int line);
void* ca_leak_check_calloc(size_t count, size_t element_size, const char* file, int line);
void ca_leak_check_free(void* mem);
int CUTE_ALLOC_CHECK_FOR_LEAKS();
int CUTE_ALLOC_BYTES_IN_USE();

// define these to your own user definition as necessary
#if !defined(CUTE_ALLOC_MALLOC_FUNC)
#include <stdlib.h>
#define CUTE_ALLOC_MALLOC_FUNC malloc
#endif

#if !defined(CUTE_ALLOC_FREE_FUNC)
#include <stdlib.h>
#define CUTE_ALLOC_FREE_FUNC free
#endif

#if !defined(CUTE_ALLOC_CALLOC_FUNC)
#include <stdlib.h>
#define CUTE_ALLOC_CALLOC_FUNC calloc
#endif

Expand Down Expand Up @@ -170,15 +178,20 @@ void ca_frame_free(ca_frame_t* frame)
frame->bytes_left = frame->capacity;
}

#include <stdlib.h> // malloc, free
typedef struct ca_heap_header_t
{
struct ca_heap_header_t* next;
struct ca_heap_header_t* prev;
size_t size;
} ca_heap_header_t;

#if CUTE_ALLOC_LEAK_CHECK
#include <stdio.h>

typedef struct ca_alloc_info_t ca_alloc_info_t;
struct ca_alloc_info_t
{
char* file;
const char* file;
size_t size;
int line;

Expand All @@ -201,7 +214,7 @@ static ca_alloc_info_t* ca_alloc_head()
return &info;
}

void* ca_leak_check_alloc(size_t size, char* file, int line)
void* ca_leak_check_alloc(size_t size, const char* file, int line)
{
ca_alloc_info_t* mem = (ca_alloc_info_t*)CUTE_ALLOC_MALLOC_FUNC(sizeof(ca_alloc_info_t) + size);

Expand All @@ -224,7 +237,7 @@ void* ca_leak_check_alloc(size_t size, char* file, int line)
#define CUTE_ALLOC_MEMSET memset
#endif

void* ca_leak_check_calloc(size_t count, size_t element_size, char* file, int line)
void* ca_leak_check_calloc(size_t count, size_t element_size, const char* file, int line)
{
size_t size = count * element_size;
void* mem = ca_leak_check_alloc(size, file, line);
Expand Down
101 changes: 92 additions & 9 deletions cute_ani.h
Expand Up @@ -71,15 +71,42 @@ typedef struct cute_ani_t
int current_frame;
float seconds;
int paused;

// 0 - no looping (plays animation forwards one frame at a time, then stops).
// positive - loop forwards by incrementing N frames.
// negative - loop backwards by decrementing N frames, starting on the last frame.
// For backwards looping make sure to call `cute_ani_reset` upon initialization to set animation to last frame.
int looping;

int frame_count;
int play_count;
cute_ani_frame_t frames[CUTE_ANI_MAX_FRAMES];
} cute_ani_t;

/**
* Resets an animation's timer and current frame. This needs to be called after loading an
* animation if `ani->looping` has been set to a negative value, in order for the animation
* to begin on the final frame.
*/
void cute_ani_reset(cute_ani_t* ani);

/**
* Increments the timer of `ani`. Will increment the current frame based on `ani->looping`.
* 1 means normal forwards looping, -1 is backwards looping, 0 means play forwards but then stop.
* Other numbers will skip a number of frames. For example, `ani->looping` as 3 will play forwards
* and skip two frames after a frame plays.
*/
void cute_ani_update(cute_ani_t* ani, float dt);

/**
* Sets the current frame of the animation to `frame_index`. Does nothing for out of bounds indices.
*/
void cute_ani_set_frame(cute_ani_t* ani, int frame_index);

/**
* Returns the id of the current frame's image. This id can be passed to `cute_ani_map_cstr` to
* retrieve the string associated with the id.
*/
CUTE_ANI_U64 cute_ani_current_image(cute_ani_t* ani);

typedef enum cute_ani_error_t
Expand All @@ -92,12 +119,28 @@ typedef enum cute_ani_error_t

typedef struct cute_ani_map_t cute_ani_map_t;

/**
* Loads an animation from memory. The file format is described at the top of this file.
*/
cute_ani_error_t cute_ani_load_from_mem(cute_ani_map_t* map, cute_ani_t* ani, const void* mem, int size, int* bytes_read);

/**
* Creates a map object to map integer ids to strings. The strings represent the names of
* different animation frame images.
*/
cute_ani_map_t* cute_ani_map_create(void* mem_ctx);
void cute_ani_map_destroy(cute_ani_map_t* map);

/**
* Manually insert a string into the map. This is not necessary if `cute_ani_load_from_mem` is used, since
* `cute_ani_load_from_mem` will call this function internally.
*/
CUTE_ANI_U64 cute_ani_map_add(cute_ani_map_t* map, const char* unique_image_path);

/**
* Converts an animation's frame id to a c-style nul-terminated string.
*/
const char* cute_ani_map_cstr(cute_ani_map_t* map, CUTE_ANI_U64 unique_image_id);
void cute_ani_map_destroy(cute_ani_map_t* map);

#define CUTE_ANI_H
#endif
Expand Down Expand Up @@ -837,7 +880,14 @@ void cute_ani_reset(cute_ani_t* ani)
{
ani->play_count = 0;
ani->seconds = 0;
ani->current_frame = 0;
if (ani->looping >= 0) ani->current_frame = 0;
else ani->current_frame = ani->frame_count - 1;
}

int cute_ani_mod(int a, int b)
{
int r = a % b;
return r * b < 0 ? r + b : r;
}

void cute_ani_update(cute_ani_t* ani, float dt)
Expand All @@ -849,15 +899,46 @@ void cute_ani_update(cute_ani_t* ani, float dt)

if (frame->seconds <= ani->seconds)
{
++current_frame;
int next_frame;

if (ani->looping)
{
// Calculate next frame.
next_frame = cute_ani_mod(current_frame + ani->looping, ani->frame_count);

if (current_frame == ani->frame_count)
// Increment play count.
int sign = ani->looping >= 0 ? 1 : -1;
if (ani->looping * sign >= ani->frame_count)
{
ani->play_count += ani->looping / ani->frame_count;
}

else if (ani->looping > 0 && next_frame < current_frame)
{
ani->play_count += 1;
}

else if (ani->looping < 0 && next_frame > current_frame)
{
ani->play_count += 1;
}
}

else
{
if (ani->looping) current_frame = 0;
ani->play_count += 1;
if (current_frame + 1 == ani->frame_count)
{
next_frame = current_frame;
ani->play_count += 1;
}

else
{
next_frame = current_frame + 1;
}
}

ani->current_frame = current_frame;
ani->current_frame = next_frame;
ani->seconds = 0;
}

Expand Down Expand Up @@ -1016,7 +1097,7 @@ cute_ani_error_t cute_ani_load_from_mem(cute_ani_map_t* map, cute_ani_t* ani, co
cute_ani_error_t error_code = CUTE_ANI_SUCCESS;
cute_ani_parse_t parse;
cute_ani_parse_t* p = &parse;
p->in = mem;
p->in = (const char*)mem;
p->end = p->in + size;

ani->current_frame = 0;
Expand Down Expand Up @@ -1056,7 +1137,9 @@ cute_ani_map_t* cute_ani_map_create(void* mem_ctx)
{
cute_ani_map_t* map = (cute_ani_map_t*)CUTE_ANI_ALLOC(sizeof(cute_ani_map_t), mem_ctx);
map->mem_ctx = mem_ctx;
strpool_embedded_init(&map->pool, mem_ctx);
strpool_embedded_config_t config = strpool_embedded_default_config;
config.memctx = mem_ctx;
strpool_embedded_init(&map->pool, &config);
map->end_id = cute_ani_map_add(map, "end");
return map;
}
Expand Down
1 change: 1 addition & 0 deletions cute_c2.h
Expand Up @@ -282,6 +282,7 @@ void c2PolytoPolyManifold(const c2Poly* A, const c2x* ax, const c2Poly* B, const

typedef enum
{
C2_NONE,
C2_CIRCLE,
C2_AABB,
C2_CAPSULE,
Expand Down
23 changes: 18 additions & 5 deletions cute_font.h
Expand Up @@ -143,12 +143,25 @@ int cute_font_fill_vertex_buffer(cute_font_t* font, const char* text, float x, f
#define CUTE_FONT_STRTOD strtod
#endif

#ifndef HASHTABLE_MEMSET
#define HASHTABLE_MEMSET(ptr, val, n) CUTE_FONT_MEMSET(ptr, val, n)
#endif

#ifndef HASHTABLE_MEMCPY
#define HASHTABLE_MEMCPY(dst, src, n) CUTE_FONT_MEMCPY(dst, src, n)
#endif

#define HASHTABLE_MEMSET(ptr, val, n) CUTE_FONT_MEMSET(ptr, val, n)
#define HASHTABLE_MEMCPY(dst, src, n) CUTE_FONT_MEMCPY(dst, src, n)
#define HASHTABLE_MALLOC(ctx, size) CUTE_FONT_ALLOC(size, ctx)
#define HASHTABLE_FREE(ctx, ptr) CUTE_FONT_FREE(ptr, ctx)
#define HASHTABLE_U64 CUTE_FONT_U64
#ifndef HASHTABLE_MALLOC
#define HASHTABLE_MALLOC(ctx, size) CUTE_FONT_ALLOC(size, ctx)
#endif HASHTABLE_MALLOC

#ifndef HASHTABLE_FREE
#define HASHTABLE_FREE(ctx, ptr) CUTE_FONT_FREE(ptr, ctx)
#endif

#ifndef HASHTABLE_U64
#define HASHTABLE_U64 CUTE_FONT_U64
#endif


// hashtable.h implementation by Mattias Gustavsson
Expand Down
19 changes: 14 additions & 5 deletions cute_sound.h
Expand Up @@ -553,7 +553,7 @@ static void* cs_malloc16(size_t size)
static void cs_free16(void* p)
{
if (!p) return;
CUTE_SOUND_FREE((char*)p - (size_t)*((char*)p - 1));
CUTE_SOUND_FREE((char*)p - (((size_t)*((char*)p - 1)) & 0xFF));
}

static void cs_last_element(__m128* a, int i, int j, int16_t* samples, int offset)
Expand Down Expand Up @@ -596,6 +596,8 @@ void cs_read_mem_wav(const void* memory, int size, cs_loaded_sound_t* sound)
} Fmt;
#pragma pack(pop)

sound->playing_count = 0;

char* data = (char*)memory;
char* end = data + size;
CUTE_SOUND_CHECK(data, "Unable to read input file (file doesn't exist, or could not allocate heap memory.");
Expand Down Expand Up @@ -816,6 +818,7 @@ cs_loaded_sound_t cs_load_wav(const char* path)
sound->channel_count = channel_count;
sound->channels[0] = a;
sound->channels[1] = b;
sound->playing_count = 0;
free(samples);
return;

Expand Down Expand Up @@ -854,8 +857,6 @@ cs_loaded_sound_t cs_load_wav(const char* path)

void cs_free_sound(cs_loaded_sound_t* sound)
{
// Attempted to free a sound while it is still being used to play audio in the context.
CUTE_SOUND_ASSERT(!sound->playing_count);
cs_free16(sound->channels[0]);
memset(sound, 0, sizeof(cs_loaded_sound_t));
}
Expand Down Expand Up @@ -891,6 +892,7 @@ int cs_is_active(cs_playing_sound_t* sound)

void cs_stop_sound(cs_playing_sound_t* sound)
{
sound->loaded_sound = 0;
sound->active = 0;
}

Expand Down Expand Up @@ -1868,6 +1870,9 @@ void cs_mix(cs_context_t* ctx)
goto remove;

cs_loaded_sound_t* loaded = playing->loaded_sound;
if (!loaded)
goto remove;

__m128* cA = (__m128*)loaded->channels[0];
__m128* cB = (__m128*)loaded->channels[1];

Expand Down Expand Up @@ -1991,8 +1996,12 @@ void cs_mix(cs_context_t* ctx)
*ptr = (*ptr)->next;
playing->next = 0;
playing->active = 0;
playing->loaded_sound->playing_count -= 1;
CUTE_SOUND_ASSERT(playing->loaded_sound->playing_count >= 0);

if (playing->loaded_sound)
{
playing->loaded_sound->playing_count -= 1;
CUTE_SOUND_ASSERT(playing->loaded_sound->playing_count >= 0);
}

cs_remove_filter(playing);

Expand Down

0 comments on commit 18c196e

Please sign in to comment.