From 7bbdeddf1ccd100126dd0baffe905408cc50a906 Mon Sep 17 00:00:00 2001 From: cancel Date: Sun, 25 Nov 2018 00:31:03 +0900 Subject: [PATCH] Add start of some basic grid stuff --- Makefile | 2 +- main.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 004894c..2ebad03 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -basic_flags := -std=c99 -Wall -Wpedantic -Wextra -pipe +basic_flags := -std=c99 -pipe -Wall -Wpedantic -Wextra -Werror=implicit-function-declaration debug_flags := -DDEBUG -O0 -ggdb -feliminate-unused-debug-symbols release_flags := -DNDEBUG -O2 -s -D_FORTIFY_SOURCE=2 -fstack-protector-strong -fpie -Wl,-pie library_flags := -lncurses diff --git a/main.c b/main.c index c9cc03f..9bb826c 100644 --- a/main.c +++ b/main.c @@ -3,9 +3,51 @@ #include #include #include +#include #include +#include #include +typedef char Term; +typedef uint32_t U32; +typedef int32_t I32; + +typedef struct { + Term* buffer; + U32 height; + U32 width; +} Field; + +void field_init_zero(Field* f, U32 height, U32 width) { + size_t num_cells = height * width; + f->buffer = calloc(num_cells, sizeof(Term)); + f->height = height; + f->width = width; +} + +void field_init_fill(Field* f, U32 height, U32 width, Term fill_char) { + size_t num_cells = height * width; + f->buffer = malloc(num_cells * sizeof(Term)); + memset(f->buffer, fill_char, num_cells); + f->height = height; + f->width = width; +} + +void field_realloc(Field* f, U32 height, U32 width) { + size_t cells = height * width; + f->buffer = realloc(f->buffer, cells * sizeof(Term)); + f->height = height; + f->width = width; +} + +void field_deinit(Field* f) { + assert(f->buffer != NULL); + free(f->buffer); +#ifndef NDEBUG + f->buffer = NULL; +#endif +} + typedef struct { chtype* buffer; int size_y;