72 lines
1.9 KiB
C
72 lines
1.9 KiB
C
//---
|
|
// util: General utilities (what a surprise huh?)
|
|
//---
|
|
|
|
#pragma once
|
|
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
|
|
/* Integer square root (recursive, logarithmic complexity). */
|
|
int64_t sqrtll(int64_t n);
|
|
|
|
#include "fixed.h"
|
|
|
|
// Heaps
|
|
|
|
/* Queue objects are passed by value in this API. */
|
|
typedef struct {
|
|
/* Number of slots allocated */
|
|
uint16_t alloc_size;
|
|
/* Number of slots used */
|
|
uint16_t size;
|
|
/* Size of elements */
|
|
uint16_t elsize;
|
|
/* Array of elements, of size alloc_size * elsize */
|
|
void *array;
|
|
/* Comparison function */
|
|
int (*compare)(void const *, void const *);
|
|
} pqueue_t;
|
|
|
|
/* Allocate a queue for [size] elements of individual size [elsize] with the
|
|
specified comparison function. */
|
|
pqueue_t pqueue_alloc(size_t size, size_t elsize,
|
|
int (*compare)(void const *, void const *));
|
|
|
|
/* Free the buffer of the queue. */
|
|
void pqueue_destroy(pqueue_t *q);
|
|
|
|
/* Check whether the queue is empty. */
|
|
bool pqueue_empty(pqueue_t const *q);
|
|
|
|
/* Add [*element] to the queue. */
|
|
void pqueue_add(pqueue_t *q, void const *element);
|
|
|
|
/* Pop the smallest element of the queue into [*ret]. */
|
|
void pqueue_pop(pqueue_t *q, void *ret);
|
|
|
|
/* Like qsort(), but actually it's a heap sort. */
|
|
void heap_sort(void *base, size_t n, size_t elsize,
|
|
int (*compare)(void const *, void const *));
|
|
|
|
// Other utils
|
|
|
|
/* Round F-key. */
|
|
void fkey_button(int position, char const *text, int color);
|
|
|
|
/* Size of text with digit-only image font. */
|
|
void font_damage_size(int value, int *w, int *h);
|
|
|
|
/* Render text with digit-only image font. */
|
|
void font_damage_print(int x, int y, int color, int align_x, int align_y,
|
|
int value);
|
|
|
|
#define RGB24(hex) \
|
|
(((hex & 0xf80000) >> 8) | \
|
|
((hex & 0x00fc00) >> 5) | \
|
|
((hex & 0x0000f8) >> 3))
|
|
|
|
/* Cubic transition from [start] to [end] in total time [tmax] */
|
|
int cubic(int start, int end, fixed_t t, fixed_t tmax);
|