diff options
author | kartofen <mladenovnasko0@gmail.com> | 2023-04-09 02:02:51 +0300 |
---|---|---|
committer | kartofen <mladenovnasko0@gmail.com> | 2023-04-09 02:02:51 +0300 |
commit | fc027f7d214028086178a7328926c9b14ead464d (patch) | |
tree | d2972055a06565f26a19626ac3fb26ead84e0766 /src/sector.c |
map loading done
Diffstat (limited to 'src/sector.c')
-rw-r--r-- | src/sector.c | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/src/sector.c b/src/sector.c new file mode 100644 index 0000000..3374995 --- /dev/null +++ b/src/sector.c @@ -0,0 +1,150 @@ +#include <stdio.h> +#include <string.h> +#include <errno.h> +#include "sector.h" + +#define VERTS_CAP 512 +#define SECTS_CAP 512 +#define NUMS_CAP 512 + +int map_load(map_t *map, char *filename) +{ + int ret = 1; + + FILE *fp = fopen(filename, "rb"); + if(!fp) { + log_critical(LOG_SYSTEM, "fopen (%s): %s\n", filename, strerror(errno)); + return ret; + } + + // temp structures + struct xy vertices[VERTS_CAP]; + size_t verts = 0; + struct sector sectors[SECTS_CAP]; + size_t sects = 0; + player_t player = {0}; + + char line[512], *ptr; + int offset = 0; + while(fgets(line, sizeof(line), fp) != NULL) + { + char word[256] = {0}; + if(sscanf(ptr = line, "%s %n", word, &offset) != 1) word[0] = '\0'; + + switch(word[0]) + { + case 'v':; // vertex + struct xy vert; + sscanf(ptr += offset, "%f %n", &vert.x, &offset); + + while(sscanf(ptr += offset, "%f %n", &vert.y, &offset) != EOF) { + if(verts >= VERTS_CAP) { + log_critical(LOG_APPLICATION, "VERTS_CAP of %d reached", VERTS_CAP); + goto exit; + } + vertices[verts++] = vert; + } + break; + case 's': // sector + if(sects >= SECTS_CAP) { + log_critical(LOG_APPLICATION, "SECTS_CAP of %d reached", SECTS_CAP); + goto exit; + } + + sscanf(ptr += offset, "%f %f %n", §ors[sects].floor, §ors[sects].ceil, &offset); + + int numbers[NUMS_CAP]; + size_t nums = 0; + while(sscanf(ptr += offset, "%s %n", word, &offset) != EOF) { + if(nums >= NUMS_CAP) { + log_critical(LOG_APPLICATION, "NUMS_CAP of %d reached", NUMS_CAP); + goto exit; + } + + numbers[nums++] = (word[0] == 'x') ? -1 : atoi(word); + } + + size_t nverts = nums/2; + // populate vertices + sectors[sects].vertices = malloc((nverts + 1) * sizeof(*sectors[sects].vertices)); + for(size_t i = 0; i < nverts; i++) + sectors[sects].vertices[i+1] = numbers[i]; + sectors[sects].vertices[0] = sectors[sects].vertices[nverts]; // need to form a loop + + // populate neighbors + sectors[sects].neighbors = malloc(nverts * sizeof(*sectors[sects].neighbors)); + for(size_t i = 0; i < nverts; i++) + sectors[sects].neighbors[i] = numbers[nverts + i]; + + sectors[sects++].nverts = nverts; + break; + case 'p': // player + sscanf(ptr += offset, "%f %f %f %d %n", &player.pos.x, &player.pos.y, &player.angle, (int *)&player.sector, &offset); + break; + } + } + + map->sectors = malloc(sects * sizeof(*sectors)); + map->nsects = sects; + memcpy(map->sectors, sectors, sects * sizeof(*sectors)); + + map->vertices = malloc(verts * sizeof(*vertices)); + map->nverts = verts; + memcpy(map->vertices, vertices, verts * sizeof(*vertices)); + + map->player = player; + + ret = 0; +exit: + fclose(fp); + return ret; +} + +void map_unload(map_t *map) +{ + for(size_t i = 0; i < map->nsects; i++) { + free(map->sectors[i].vertices); + free(map->sectors[i].neighbors); + } + + free(map->sectors); + free(map->vertices); +} + +int map_save(map_t *map, char *filename) +{ + FILE *fp = fopen(filename, "wb"); + if(!fp) { + log_error(LOG_SYSTEM, "fopen (%s): %s\n", filename, strerror(errno)); + return 1; + } + + (void)map; + + // something something + + return 0; +} + +void map_print(map_t *map) +{ + log("%ld %ld", map->nverts, map->nsects); + for(size_t i = 0; i < map->nverts; i++) { + printf("v %ld {%.1f %.1f}\n", i, map->vertices[i].x, map->vertices[i].y); + } + + for(size_t i = 0; i < map->nsects; i++) + { + printf("s %ld %.1f %.1f ", i, map->sectors[i].floor, map->sectors[i].ceil); + + for(size_t j = 0; j < map->sectors[i].nverts + 1; j++) + printf("%d ", map->sectors[i].vertices[j]); + printf(" "); + + for(size_t j = 0; j < map->sectors[i].nverts; j++) + printf("%d ", map->sectors[i].neighbors[j]); + printf("\n"); + } + + printf("p %.1f %.1f %.1f %d", map->player.pos.x, map->player.pos.y, map->player.angle, map->player.sector); +} |