2048/src/utils.c

55 lines
1.1 KiB
C
Raw Permalink Normal View History

2022-07-27 01:16:32 +05:30
#include "utils.h"
2023-10-19 04:12:43 +05:30
#include <math.h>
2022-07-27 01:16:32 +05:30
#include <sys/stat.h>
2023-10-19 04:12:43 +05:30
// int_string
struct string *int_string(uint n) {
uint l = log10(n) + 1; // length
struct string *str = alloc_string(l);
2022-07-27 01:16:32 +05:30
2023-10-19 04:12:43 +05:30
snprintf(str->s, l, "%i", n);
2022-07-27 01:16:32 +05:30
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);
}
2023-10-19 04:12:43 +05:30
// 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;
2022-07-27 01:16:32 +05:30
}