|
TGuy 0.18.1
TrashGuy C library created to be easily usable from other languages with CFFI support.
|
#include <libtguy.h>#include <math.h>#include <assert.h>#include <string.h>#include <stdlib.h>#include <limits.h>Classes | |
| struct | TGStrViewArr |
| struct | TrashGuyState |
Macros | |
| #define | TGUY_NO_GRAPHEME |
| #define | ignored_ (void) |
| #define | tg_max(a, b) ((a) > (b) ? (a) : (b)) |
Functions | |
| static TGStrView * | cstr2tgstrv (TGStrView *res, const char *str, size_t len) |
| static size_t | strvarr_strlen (const TGStrView arr[], size_t len) |
| static size_t | strvarr_write (char str_out[], const TGStrView arr[], size_t len) |
| static size_t | strvarr_copy_src (TGStrView *dst, const TGStrView *src, size_t len, const char str_mem[]) |
| static void | strvarr_copy (TGStrView *dst, const TGStrView *src, size_t len) |
| static unsigned | get_first_frame_for_element (unsigned first_element_frames_count, unsigned element_index) |
| static void | tguy_clear_field (const TrashGuyState *st, unsigned n_clear_elements) |
| TrashGuyState * | tguy_from_arr_ex_2 (const TGStrView arr[], size_t len, unsigned spacing, const TGStrView *sprite_space, const TGStrView *sprite_can, const TGStrView *sprite_right, const TGStrView *sprite_left, int preserve_strings) |
| TrashGuyState * | tguy_from_arr_ex (const TGStrView arr[], size_t len, unsigned spacing, const TGStrView *sprite_space, const TGStrView *sprite_can, const TGStrView *sprite_right, const TGStrView *sprite_left) |
| TrashGuyState * | tguy_from_arr (const TGStrView arr[], size_t len, unsigned spacing) |
| static char * | tguy_utf8_next (const char *begin, const char *end) |
| static size_t | tguy_codepoints_len (const char *string, size_t len) |
| TrashGuyState * | tguy_from_utf8_ex (const char string[], size_t len, unsigned spacing, const char *sprite_space, size_t sprite_space_len, const char *sprite_can, size_t sprite_can_len, const char *sprite_right, size_t sprite_right_len, const char *sprite_left, size_t sprite_left_len) |
| TrashGuyState * | tguy_from_utf8 (const char string[], size_t len, unsigned spacing) |
| TrashGuyState * | tguy_from_cstr_arr_ex (const char *const arr[], size_t len, unsigned spacing, const char *sprite_space, size_t sprite_space_len, const char *sprite_can, size_t sprite_can_len, const char *sprite_right, size_t sprite_right_len, const char *sprite_left, size_t sprite_left_len) |
| TrashGuyState * | tguy_from_cstr_arr (const char *const arr[], size_t len, unsigned spacing) |
| void | tguy_free (TrashGuyState *st) |
| unsigned | tguy_set_frame (TrashGuyState *restrict st, unsigned frame) |
| unsigned | tguy_set_pos (TrashGuyState *st, unsigned sprite_pos, unsigned facing_right, unsigned element_index) |
| void | tguy_get_frame_state (const TrashGuyState *st, unsigned *frame, unsigned *sprite_pos, unsigned *facing_right, unsigned *element_index) |
| size_t | tguy_fprint (const TrashGuyState *st, FILE *fp) |
| size_t | tguy_print (const TrashGuyState *st) |
| size_t | tguy_sprint (const TrashGuyState *st, char *buf) |
| const TGStrView * | tguy_get_arr (const TrashGuyState *st, size_t *len) |
| unsigned | tguy_get_frames_count (const TrashGuyState *st) |
| size_t | tguy_get_bsize (TrashGuyState *st) |
| const char * | tguy_get_string (TrashGuyState *st, size_t *len) |
| unsigned | tguy_get_first_frame_for_element (const TrashGuyState *st, unsigned element_index) |
| unsigned | tguy_get_version (void) |
| [out] | res | where to keep resulting TGStrView for str |
| str | string or NULL | |
| len | str length or -1 if string is nul terminated |
|
inlinestatic |
Returns first frame of sequence of frames involving the work with element_index of TrashGuyState::text.
You can perceive this function as a parabola, where each x is element_index and y is returned frame (starts from 0):
frame = element_index2 + (TrashGuyState::first_element_frames_count - 1)element_index
See this visualization.
For example:
(get_first_frame_for_element(4, i + 1) - get_first_frame_for_element(4, i))
will yield number of frames needed to process i-th element if TrashGuyState::first_element_frames_count was 4.
| first_element_frames_count | TrashGuyState::first_element_frames_count |
| element_index | index of text element in TrashGuyState::text[element_index] |
|
inlinestatic |
Replaces TrashGuyState::arena[1:n+1] with with TrashGuyState::sprite_space
| st | Valid TrashGuyState |
| n_clear_elements | number of TrashGuyState::text elements to clear with TrashGuyState::sprite_space |
| size_t tguy_fprint | ( | const TrashGuyState * | st, |
| FILE * | fp | ||
| ) |
Writes currently set TrashGuy frame to fp without newline
| st | Valid TrashGuyState with frame set |
| fp | Valid FILE |
| void tguy_free | ( | TrashGuyState * | st | ) |
Deallocates memory used by a TrashGuyState, does nothing if pointer is NULL
| st | Valid TrashGuyState * or NULL |
| TrashGuyState * tguy_from_cstr_arr | ( | const char *const | arr[], |
| size_t | len, | ||
| unsigned | spacing | ||
| ) |
| arr | Array of nul-terminated C strings, in case of NULL, acts like empty array and len is set as 0 |
| len | Number of elements in array |
| spacing | Number of space sprites to be placed between the TrashGuy sprite and fist element initially |
| TrashGuyState * tguy_from_cstr_arr_ex | ( | const char *const | arr[], |
| size_t | len, | ||
| unsigned | spacing, | ||
| const char * | sprite_space, | ||
| size_t | sprite_space_len, | ||
| const char * | sprite_can, | ||
| size_t | sprite_can_len, | ||
| const char * | sprite_right, | ||
| size_t | sprite_right_len, | ||
| const char * | sprite_left, | ||
| size_t | sprite_left_len | ||
| ) |
| arr | Array of nul-terminated C strings, in case of NULL, acts like empty array and len is set as 0 |
| len | Number of elements in array |
| spacing | Number of space sprites to be placed between the TrashGuy sprite and fist element initially |
| sprite_space | Sprite to be used as empty space |
| sprite_space_len | Number of bytes for sprite_space |
| sprite_can | Sprite to be used as trash can |
| sprite_can_len | Number of bytes for sprite_can |
| sprite_right | Sprite to be used when TrashGuy moves right |
| sprite_right_len | Number of bytes for sprite_right |
| sprite_left | Sprite to be used when TrashGuy moves left |
| sprite_left_len | Number of bytes for sprite_left |
| TrashGuyState * tguy_from_utf8 | ( | const char | string[], |
| size_t | len, | ||
| unsigned | spacing | ||
| ) |
Creates new TrashGuysState from valid utf-8 string, where each grapheme cluster is made into an element to dump
| string | Valid utf-8 string, in case of NULL, acts like empty string and len is set as 0 |
| len | Number of bytes string has, if -1, then strlen will be used |
| spacing | Number of space sprites to be placed between the TrashGuy sprite and fist element initially |
| const TGStrView * tguy_get_arr | ( | const TrashGuyState * | st, |
| size_t * | len | ||
| ) |
Returns read-only array view of current TrashGuy frame: TGStrView[]{ {"t",1}, {"e",1}, {"ї",2}, {"s",1}, {"t",1}, {NULL,0} }
| st | Valid TrashGuyState with frame set |
| [out,optional] | len Length of the returned array, excluding NULL terminator |
| size_t tguy_get_bsize | ( | TrashGuyState * | st | ) |
If bsize is not set, then iterate over possible arena layout and compute buf size big enough to keep any frame plus nul terminator
| unsigned tguy_get_first_frame_for_element | ( | const TrashGuyState * | st, |
| unsigned | element_index | ||
| ) |
Returns first frame for when certain element is being processed. You can get a range of frames [first,last] for when certain element is processed by calling first = tguy_get_first_frame_for_element(element_index) last = tguy_get_first_frame_for_element(element_index + 1) - 1
| st | Valid TrashGuyState |
| element_index | Element index, in tguy_from_utf8("teїst",-1,4), 't' would be index 0, 'ї' would be index 2, etc. |
| void tguy_get_frame_state | ( | const TrashGuyState * | st, |
| unsigned * | frame, | ||
| unsigned * | sprite_pos, | ||
| unsigned * | facing_right, | ||
| unsigned * | element_index | ||
| ) |
| st | Valid TrashGuyState * |
| [out,optional] | frame Current frame, 0 <= frame < tguy_get_frames_count() |
| [out,optional] | sprite_pos Position within the arena, 0 < sprite_pos < tguy_get_arr().len |
| [out,optional] | facing_right Whether sprite is facing right |
| [out,optional] | element_index Index of the element that's currently being processed, see tguy_get_first_frame_for_element() |
| unsigned tguy_get_frames_count | ( | const TrashGuyState * | st | ) |
Returns number of frames particular TrashGuyState has
| st | Valid TrashGuyState |
| unsigned tguy_get_version | ( | void | ) |
Get version as integer in format MMMmmmppp. 020107002 -> 20.107.2
| size_t tguy_print | ( | const TrashGuyState * | st | ) |
Writes currently set TrashGuy frame to stdout without newline
| st | Valid TrashGuyState with frame set |
| unsigned tguy_set_frame | ( | TrashGuyState *restrict | st, |
| unsigned | frame | ||
| ) |
In order to properly set frame we need to know few things beforehand:
All we know is desired frame and number of frames per first element.
frame = element_index * (first_element_frames_count + element_index - 1) 1*element_index2 + (first_element_frames_count - 1)*element_index + (-frame) = 0 ax2 + bx + c = 0 x1 = (-b + sqrt(b2 - 4ac)) / 2a x1 = (sqrt(b2 + 4c) - b) / 2 , which is the final formula boundary <= (boundary + sub_frame) < boundary + frames_per_element , sub_frame = frame - boundary sub_frame < (total / 2) -> right, else -> left sub_frame % (total / 2) | unsigned tguy_set_pos | ( | TrashGuyState * | st, |
| unsigned | sprite_pos, | ||
| unsigned | facing_right, | ||
| unsigned | element_index | ||
| ) |
Sets the current frame for TrashGuyState from state components Position is index into arena received via tguy_get_arr().
Example:
| st | Valid TrashGuyState * |
| sprite_pos | Position within the arena, 0 < sprite_pos < tguy_get_arr().len |
| facing_right | Whether sprite should be facing right, otherwise left carrying the element |
| element_index | Index of the element that's currently being processed, see tguy_get_first_frame_for_element() |