From e6f3e6cdaad01d5fd62ec2a9d935d86c0c449475 Mon Sep 17 00:00:00 2001 From: Milang Date: Sat, 1 Feb 2020 16:39:34 +0100 Subject: [PATCH] adding compressed level format (incomplete) --- include/level.h | 22 ++++- levelconverter/1-2.png.c | Bin 27869 -> 3989 bytes levelconverter/1-2.png.packlvl | Bin 0 -> 3519 bytes levelconverter/levelconv.py | 125 +++++++------------------ src/level.c | 163 +++++++++++++++++++++++++++++---- 5 files changed, 202 insertions(+), 108 deletions(-) create mode 100644 levelconverter/1-2.png.packlvl diff --git a/include/level.h b/include/level.h index b1ea253..14323ec 100644 --- a/include/level.h +++ b/include/level.h @@ -1,8 +1,26 @@ #ifndef LEVEL_H #define LEVEL_H -void set_level(int w, int l); -void get_lvl_id(int w, int l, char * str); +// Utilitaire de décompression de level + +#include + +typedef struct +{ + uint16_t width; + uint16_t height; + uint8_t data[]; // rough data +} packed_level_t; + +void unpack_level(packed_level_t const * const packed_level_t); + + +void set_level(int w, int l); // Configures the level + + + +void get_lvl_id(int w, int l, char * str); // Retruns level ID into a char [4] + void malloc_error(); #endif \ No newline at end of file diff --git a/levelconverter/1-2.png.c b/levelconverter/1-2.png.c index a1fba4de9b26108fe4b38fb6e46abf8fabca3bd7..85b525529dfb166c077df03bb9d14110f90520f0 100644 GIT binary patch literal 3989 zcmb_f+iuf95M4JwN+kZlyrq&+y-od>LOv@t=6Gj>1Eu9Rw!BolK|0 zf}n#3b3rQ^>@WKufb+JMq8m7Gqcu?@0(*fcN?z4GegM$+Hk)>h?MlX^su@gqGCH+a zrpBAG#+2G471)#}v~IWF8xpq)Ac^Uw8tsqzGpxNSlNlofJ7VZ46G$?HDnS1DcbK4urIcd+gAht!dkvvuxUuB8RrW z16u(*N`QQ4=?H9yxZ%}-XNf4NeayDLjI^#exdH%i?#Yaqf`Vpeq5{a28DwsN`aa$a zOfGAOfT~d<0N!dG*hF;+Mq*>xChpqV;h??-_enG8<#WpH&ET`6%JlBk1Eps-5ncgJ z0F+r;6$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l b/levelconverter/1-2.png.packlvl new file mode 100644 index 0000000000000000000000000000000000000000..5fb0d46b471e45e9a7836c21605abc4223ac4051 GIT binary patch literal 3519 zcmb_fOHRWu5Vad1-EfB11F_BrP94nXB~lh#spj}fZRFjZiPn#By%CZ&b!_K+vK)=pN}!?m$+)PO~S zk`lfZ1BW9iv#eTS6aioWv%wHZ1|sVjh&39ko5@pTXfn$jut%9|%d$BP-wlj0fLDW8 z#sMj!kTmZAU9*1!%2deX8MpD%K(I3JmeI|mWtsFu?3NyJ?*wZhuyuN5WKA>fQM?@YBUbE4c5&5RU z8UWzFhm3T=B-5Ul0V|PVWd{uBDKgNxv>XTeg*pSMZJ+`r`b!Xt&a!3RmUE+m;TYa0 zQusfg3Dd(+Dkq20?hJ$Mr<(|`fGGgNRMrPkN%(a#Skpbh7=MF2;;K4U+j4Y+5N58eg#0W6U3HG~okR*l9%WVsSMOJ9G zMrPYVfDWK(Z97PeatK(N0U-hzF+k*jW`Se5ET}KXx7V-J@#A=c|0deD* ") img = Image.open(filename) print("Converting", filename, "to", "\"" + filename + ".c\",", "size =", img.size) -code = "// !b Here the generated section begins (see the end at \"!e\")\nmap_current->w = " + str(img.size[0]) + ";\nmap_current->h = " + str(img.size[1]) + ";\ncell_t lvl[]=\n{" +#rough binary +code = write_char(img.size[0] - img.size[0]%256) +code += write_char(img.size[0]%256) +code += write_char((img.size[1] - img.size[1]%256)/256) +code += write_char(img.size[1]%256) + + nombre_ennemis=0 ennemis = "\nennemi_t ennemies0[]={" @@ -39,102 +50,35 @@ ennemis = "\nennemi_t ennemies0[]={" # Balayage des pixels : passe pour dessiner les murs pixels = img.load() for x in range(0,img.size[0]): - code += "\n " for i in range(0, img.size[1]): y=img.size[1]-i-1 - if color_compare(pixels[x,y], pierre): - hexa="0x" + if color_compare(pixels[x,y],empty): + code += write_char(0) - left=1 - right=1 - - if x != 0: - left = (color_compare(pixels[x-1,y], pierre)) - if x != img.size[0]-1: - right = (color_compare(pixels[x+1,y], pierre)) - - sx=0 - if left and right: - sx=1 - elif left and right==0: - sx=2 - elif left==0 and right: - sx=0 - hexa+= str(sx) - - - up=0 # vide par defaut - down=1 - - if y != 0: - up = (color_compare(pixels[x,y-1], pierre)) - if y != img.size[1]-1: - down = (color_compare(pixels[x,y+1], pierre)) - - sy = int(bool(up)) - hexa += str(sy) - - code += "{EARTH," + hexa + "}, " + elif color_compare(pixels[x,y], pierre): + code += write_char(1) elif color_compare(pixels[x,y], piece): - code += "{COIN,0}, " + code += write_char(2) + elif color_compare(pixels[x,y], brique): - code += "{BRICK,0}, " + code += write_char(3) + elif color_compare(pixels[x,y],brique_piece): - code += "{BRICK,0x15}, " + code += write_char(4) + elif color_compare(pixels[x,y],boite_piece): - code += "{GIFT,0x11}, " + code += write_char(5) + elif color_compare(pixels[x,y],boite_champi): - code += "{GIFT,0x21}, " - elif color_compare(pixels[x,y],empty): - code += "{0,0}, " + code += write_char(6) + elif color_compare(pixels[x,y],beton): - code += "{BLOC,0}, " + code += write_char(7) elif color_compare(pixels[x,y],tuyau_bout): - left=0 - right=0 - up=0 # vide par defaut - down=0 - tx=0 - ty=0 - if x: - left = (color_compare(pixels[x-1,y], tuyau_bout)) or (color_compare(pixels[x-1,y], tuyau_milieu)) - if x != img.size[0]-1: - right = (color_compare(pixels[x+1,y], tuyau_bout)) or (color_compare(pixels[x+1,y], tuyau_milieu)) - - if y: - up = (color_compare(pixels[x,y-1], tuyau_bout)) or (color_compare(pixels[x,y-1], tuyau_milieu)) - if y != img.size[1]-1: - down = (color_compare(pixels[x,y+1], tuyau_bout)) or (color_compare(pixels[x,y+1], tuyau_milieu)) - - if right and down: - tx=0 - if (color_compare(pixels[x,y+1], tuyau_milieu)): - ty=2 - elif left and down: - if (color_compare(pixels[x,y+1], tuyau_milieu)): - tx=1 - ty=2 - else: - tx=2 - ty=0 - elif up and right: - tx=0 - if (color_compare(pixels[x,y-1], tuyau_milieu)): - ty=4 - else: - ty=1 - elif up and left: - if (color_compare(pixels[x,y-1], tuyau_milieu)): - tx=1 - ty=4 - else: - tx=2 - ty=1 - hexa="0x"+str(tx)+str(ty) - code += "{TUYAU,"+hexa+"}, " + code += write_char(8) elif color_compare(pixels[x,y], tuyau_milieu): left=0 right=0 @@ -182,7 +126,7 @@ for x in range(0,img.size[0]): else: code+= "{END_LEVEL,0x1102}," else: - code += "{0,'?'}, " + code += write_char(0) #code += str(pixels[x,y]) if color_compare(pixels[x,y], goomba): @@ -192,11 +136,12 @@ for x in range(0,img.size[0]): ennemis += "\n KOOPA_V(" + str(8*x) + ", " + str(8*i) + ", -1)," elif color_compare(pixels[x,y], koopa_rouge): ennemis += "\n KOOPA_R(" + str(8*x) + ", " + str(8*i) + ", -1)," -code += "\n};\ninit_level(lvl);\n" ennemis += "\n};\nennemis_global_size=" + str(nombre_ennemis) + ";\ninit_ennemies(ennemies0);\n" -f = open(filename+".c", 'w') -f.write(code + ennemis + "\n// !e End of generated section") -f.close() +#f = open(filename+".packlvl", 'w') +#f.write(code) +#f.close() + +build(bytes(code, 'utf-8'), filename+".o", "_lvl_"+filename.replace(".png","")) print("Converted succesfully !") \ No newline at end of file diff --git a/src/level.c b/src/level.c index 13e012a..585073b 100644 --- a/src/level.c +++ b/src/level.c @@ -14,6 +14,18 @@ #include #include +#define PACKED_EMPTY 0 +#define PACKED_STONE 1 +#define PACKED_COIN 2 +#define PACKED_BRICK 3 +#define PACKED_BRICK_COIN 4 +#define PACKED_BOX_COIN 5 +#define PACKED_BOX_CHAMPI 6 +#define PACKED_BETON 7 + +#define PACKED_TUYAU_BOUT 8 +#define PACKED_TUYAU_MIDDLE 9 + void malloc_error() { extern image_t img_ram; @@ -24,22 +36,141 @@ void malloc_error() mkb_getkey(); } - -void unpack_level(packed_level_t const * const packed_level_t p) +static uint8_t pack_access(packed_level_t const * const p, int x, int y) { - unsigned int const size = p->width*p->height; - map_t * m = (map_t *) malloc( 4*sizeof(int) + size*sizeof(cell_t) ); - if (m==0) - malloc_error(); - if (map_current) - { - free(map_current); - map_current=0; - } - - map_current=m; - - map_current->w = p->width; - map_current->h = p->height; + if (0<=x && xdata[x*p->height+y]; + else + return PACKED_EMPTY; +} +static void cell_set(cell_t *const array, int w, int h, int x, int y, cell_t const cell) +{ + if (0<=x && xwidth; + const unsigned int h = p->height; + + cell_t * c = (cell_t *) malloc(sizeof(cell_t) * w * h); + if (c==0) + malloc_error(); + + int sx=0, sy=p->height; // Mario start coords + + for (int x=0; x