diff --git a/.gitignore b/.gitignore index 41512ca..4e335bf 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ raylib/ *.exe *.swp *.tiled-session +map/maps_out.c diff --git a/Makefile b/Makefile index 807e707..c2f6456 100644 --- a/Makefile +++ b/Makefile @@ -21,13 +21,21 @@ SRC_DIR = src OBJS = $(patsubst $(SRC_DIR)/%.c,build-tmp/%.o,$(wildcard $(SRC_DIR)/*.c)) -all: | builddir build builddir2 +all: | builddir map build builddir2 windef: $(eval OUTPUT = "$(OUTNAME).exe") $(eval BUILD_DIR = build-win) $(eval LDFLAGS = $(LDFLAGS_WIN)) +converter: map/converter.c + $(CC) map/converter.c -o map/converter $(CFLAGS) + +map: converter + ./map/converter + $(CC) -c map/maps_out.c -o build-tmp/maps_out.o $(CFLAGS) + $(eval OBJS += build-tmp/maps_out.o) + builddir: - mkdir $(BUILD_DIR) mkdir build-tmp @@ -56,6 +64,7 @@ clean: - rm -rf build-win - rm -rf build-tmp - rm "$(OUTNAME).amd64" "$(OUTNAME).exe" + - rm map/maps_out.c map/converter -.NOTPARALLEL: builddir builddir2 -.PHONY: all test clean win windef builddir builddir2 testwin +.NOTPARALLEL: builddir builddir2 converter map +.PHONY: all test clean win windef builddir builddir2 testwin converter map diff --git a/map/converter.c b/map/converter.c index c35a2a2..5388031 100644 --- a/map/converter.c +++ b/map/converter.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "../src/types.h" @@ -11,26 +12,43 @@ char *filebuf; -char *filesig = +char filesig[] = "\n" -""; +"\n" +" \n" +" \n" +" "; -int import_map(MapCollec *mapcollec, FILE *mapfile){ +char tmpname[256]; + +int import_map(FILE *mapfile, FILE *outfile){ int32_t fbuf_pos = 0; - int32_t fbuf_max = fread(filebuf, 1, FBUF_SIZE, mapfile); - if(strncmp(filebuf, filesig, sizeof(filesig))){ + fread(filebuf, 1, FBUF_SIZE, mapfile); + if(strncmp(filebuf, filesig, sizeof(filesig)-1)){ printf("Error: Invalid file header.\n"); return 1; } + fbuf_pos += sizeof(filesig)-1; + fprintf(outfile, "uint8_t %s_lyr0[] = {", tmpname); + int32_t datlen = strcspn(&filebuf[fbuf_pos], "<"); + fwrite(&filebuf[fbuf_pos], 1, datlen, outfile); + fprintf(outfile, "};\nMap %s = {{%s_lyr0, 0}, 256, 256};\n", tmpname, tmpname); return 0; } FILE *maps[100] = {NULL}; -int import_maps(MapCollec *mapcollec){ - char tmpname[256]; +int import_maps(){ int i; + FILE *outfile = fopen(MAPDIR "maps_out.c", "w+"); + int nmaps = 0; + if(!outfile){ + printf("Failed to open file \"" MAPDIR "maps_out.c\"\n"); + printf("Error : %d (%d)\n", errno, EINVAL); + return 1; + } + fprintf(outfile, "#include \n#include \"../src/types.h\"\n"); for(i = 0; i < 100; i++){ snprintf(tmpname, 256, MAPDIR MAPNAME "%d.tmx", i); printf("Loading map \"%s\"...\n", tmpname); @@ -39,9 +57,17 @@ int import_maps(MapCollec *mapcollec){ printf("Failed to load map, stopping here\n"); break; } - if(import_map(mapcollec, maps[i])) + snprintf(tmpname, 256, MAPNAME "%d", i); + if(import_map(maps[i], outfile)) return 0; + nmaps++; } + fprintf(outfile, "MapCollec mapcollec = {%d,{", nmaps); + for(int i = 0; i < nmaps; i++){ + fprintf(outfile, "&" MAPNAME "%d,", i); + } + fprintf(outfile, "}};\n"); + fclose(outfile); return i; } @@ -52,20 +78,18 @@ void clean_maps(){ } } -int export_maps(MapCollec *mapcollec){ - -} - int main(){ - MapCollec mapcollec; + printf("===== Converter output =====\n"); filebuf = malloc(FBUF_SIZE); if(!filebuf) return 1; - if(!import_maps(&mapcollec)) + if(!import_maps()) return 1; clean_maps(); free(filebuf); + printf("===== Converter end =====\n"); + return 0; } diff --git a/src/draw.c b/src/draw.c index dae9701..271cb8c 100644 --- a/src/draw.c +++ b/src/draw.c @@ -13,6 +13,7 @@ #include "ui.h" #define TEX_N 9 +#define TILE_N 3 char *tex_files[TEX_N] = { "assets/blobbleu1.png", @@ -26,7 +27,14 @@ char *tex_files[TEX_N] = { "assets/blobvert3.png" }; +char *tile_files[TILE_N] = { + "assets/tile_grass.png", + "assets/tile_cobble.png", + "assets/tile_grass.png" +}; + Texture2D tex_index[TEX_N]; +Image tile_index[TILE_N]; extern GUIInfo gui_info; @@ -36,12 +44,32 @@ int init_draw(){ SetTargetFPS(60); - for(int i = 0; i < TEX_N; i++){ + for(int i = 0; i < TEX_N; i++) tex_index[i] = LoadTexture(tex_files[i]); - } + for(int i = 0; i < TILE_N; i++) + tile_index[i] = LoadImage(tile_files[i]); + return 0; } +Texture2D mkmap_img(Map *map){ + uint8_t *tiles = map->layers[0]; + Image mapimg = GenImageColor(map->width*32, map->height*32, BLACK); + for(int x = 0; x < map->width; x++){ + for(int y = 0; y < map->height; y++){ + Rectangle recta = {.x = 0, .y = 0, + .width = map->width*32, .height = map->height*32}; + Rectangle rectb = recta; + rectb.x = x; + rectb.y = y; + ImageDraw(&mapimg, tile_index[tiles[map->width*y + x]], recta, rectb, WHITE); + } + } + Texture2D maptex = LoadTextureFromImage(mapimg); + UnloadImage(mapimg); + return maptex; +} + void draw_workers(Game *game){ Workers *workers = &game->workers; for(int i = 0; i < workers->worker_n; i++){ @@ -65,6 +93,10 @@ void draw_machines(Game *game){ } } +void draw_map(Game *game){ + DrawTextureV(game->map.maptex, (V2d){0,0}, WHITE); +} + void draw_game(Game *game){ ClearBackground(BLACK); diff --git a/src/map.c b/src/map.c index 63144e1..93608fd 100644 --- a/src/map.c +++ b/src/map.c @@ -10,3 +10,8 @@ #include "types.h" +extern MapCollec mapcollec; + +int load_map(int mapn){ + +} diff --git a/src/types.h b/src/types.h index c817c60..3622f4e 100644 --- a/src/types.h +++ b/src/types.h @@ -119,10 +119,20 @@ typedef struct { } MapCollec; +typedef struct { + + uint8_t *coll; + int32_t width; + int32_t height; + Texture2D maptex; + +} LoadedMap; + typedef struct { Machines machines; Workers workers; + LoadedMap map; Camera2D camera;