2048/src/utils.c
2023-10-18 18:42:43 -04:00

55 lines
1.1 KiB
C

#include "utils.h"
#include <math.h>
#include <sys/stat.h>
// int_string
struct string *int_string(uint n) {
uint l = log10(n) + 1; // length
struct string *str = alloc_string(l);
snprintf(str->s, l, "%i", n);
return str;
}
// alloc_string
struct string *alloc_string(uint l) {
struct string *s = (struct string *)malloc(sizeof(struct string) + 1);
s->s = (char *)malloc(l + 1);
s->length = l;
memset(s->s, 0, l);
return s;
}
// free_string
void free_string(struct string *s) {
free(s->s);
free(s);
}
// create_plane
// Programmer beware: this can and will return an erronous plane -- up to you to error handle
struct ncplane *create_plane(struct notcurses *nc, char *name, uint cols, uint rows, uint y, uint x, uint flags) {
struct ncplane *stdp = notcurses_stdplane(nc);
allocm(struct ncplane_options, opt);
opt->name = name;
opt->cols = cols;
opt->rows = rows;
opt->y = y;
opt->x = x;
opt->flags = flags;
opt->margin_b = 0;
opt->margin_r = 0;
struct ncplane *plane = ncplane_create(stdp, opt);
free(opt);
return plane;
}