fully added level decompression

dev
Milang 3 years ago
parent e6f3e6cdaa
commit 71f3e778bb
  1. BIN
      SuperCbr.g1a
  2. BIN
      assets-fx/bin/lvl_1_2.png
  3. 6
      include/level.h
  4. 2
      include/world.h
  5. BIN
      levelconverter/1-2.png.c
  6. BIN
      levelconverter/1-2.png.packlvl
  7. 0
      levelconverter/1_2.png
  8. 73
      levelconverter/levelconv.py
  9. 1
      src/config.c
  10. 10
      src/keyboard.c
  11. 163
      src/level.c
  12. 2
      src/world.c

Binary file not shown.

Binary file not shown.

@ -7,12 +7,12 @@
typedef struct
{
uint16_t width;
uint16_t height;
uint8_t width;
uint8_t height;
uint8_t data[]; // rough data
} packed_level_t;
void unpack_level(packed_level_t const * const packed_level_t);
//void unpack_level(packed_level_t const * const packed_level_t);
void set_level(int w, int l); // Configures the level

@ -103,7 +103,7 @@ cell_t* world_get(int x, int y);
void display_cell(int cx, int cy, int sx, int sy, int plan);
void world_set(cell_t * w);
void world_set(int w, int h, int x, int y, cell_t const * a);
void world_draw();
void world_move();

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.7 KiB

After

Width:  |  Height:  |  Size: 3.7 KiB

@ -28,18 +28,22 @@ def color_compare(color1, color2):
return 0
def write_char(val):
return '%c' % int(val%256)
a = val%256
return a.to_bytes(1,'little')
#if a>128:
# a -= 256
# return chr(a)
# return '%c' % int(val%256)
# Load image
filename = input("File name ?\n> ")
img = Image.open(filename)
print("Converting", filename, "to", "\"" + filename + ".c\",", "size =", img.size)
print("Converting " + filename + " to assets-fx/" + filename + " //size =", img.size)
#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)
# code = str().encode('latin1')
code = write_char(int(img.size[0]))
code += write_char(int(img.size[1]))
@ -79,52 +83,11 @@ for x in range(0,img.size[0]):
elif color_compare(pixels[x,y],tuyau_bout):
code += write_char(8)
elif color_compare(pixels[x,y], tuyau_milieu):
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 left==0:
tx=0
ty=3
elif left and right==0:
tx=1
ty=3
elif up==0 and down:
tx=1
ty=0
elif up and down==0:
tx=1
ty=1
hexa="0x"+str(tx)+str(ty)
code += "{TUYAU,"+hexa+"}, "
code += write_char(9)
elif color_compare(pixels[x,y], drapeau):
if not color_compare(pixels[x,y-1], drapeau):
code += "{END_LEVEL,0x1400}, "
elif not color_compare(pixels[x,y-2], drapeau):
code+= "{END_LEVEL,0x1401}, "
elif not color_compare(pixels[x,y-3], drapeau):
code+= "{END_LEVEL,0x1302}, "
elif not color_compare(pixels[x,y-4], drapeau):
code+= "{END_LEVEL,0x1202}, "
elif not color_compare(pixels[x,y-5], drapeau):
code+= "{END_LEVEL,0x1202}, "
elif not color_compare(pixels[x,y-6], drapeau):
code+= "{END_LEVEL,0x1202}, "
else:
code+= "{END_LEVEL,0x1102},"
code += write_char(10) # temporary code+= "{END_LEVEL,0x1102},"
else:
code += write_char(0)
#code += str(pixels[x,y])
@ -138,10 +101,8 @@ for x in range(0,img.size[0]):
ennemis += "\n KOOPA_R(" + str(8*x) + ", " + str(8*i) + ", -1),"
ennemis += "\n};\nennemis_global_size=" + str(nombre_ennemis) + ";\ninit_ennemies(ennemies0);\n"
#f = open(filename+".packlvl", 'w')
#f.write(code)
#f.close()
build(bytes(code, 'utf-8'), filename+".o", "_lvl_"+filename.replace(".png",""))
f = open("../assets-fx/bin/lvl_"+filename, 'wb')
f.write(code)
f.close()
print("Converted succesfully !")
print("Converted succesfully ! (", os.path.getsize("../assets-fx/bin/lvl_"+filename), "bytes )")

@ -24,6 +24,7 @@ void configmenu()
dtext(40,9,&loglevels[ll_get_level()][0],C_BLACK,C_NONE);
dupdate();
ll_pause();
int key=mkb_getkey();
if (key==KEY_EXIT)
break;

@ -6,6 +6,7 @@
//#include <gint/timer.h>
#include <gint/clock.h>
#include <config.h>
#include <liblog.h>
static mkb_t keys[6]={0};
@ -30,6 +31,8 @@ int mkb_getkey()
while (1)
{
key_event_t const e=pollevent();
if (e.key==KEY_ARROW)
ll_pause();
if (e.type==KEYEV_DOWN) // Returns only whan a key is down
return e.key;
else
@ -119,7 +122,7 @@ void mkb_update()
{
key_event_t e;
e=pollevent();
int menu=0;
int menu=0, log=0;
for (int i=0; i<6; i++)
if (keys[i]==2) keys[i]=1;
while(e.type!=KEYEV_NONE)
@ -145,7 +148,8 @@ void mkb_update()
if(keydown(KEY_EXIT))
menu=1; // Displays menu after updating the keyboard
if (e.key==KEY_ARROW)
log=1;
if (k!=MK_NONE)
{
@ -167,6 +171,8 @@ void mkb_update()
finish_level=-1; // Exits the level
return;
}
if (log)
ll_pause();
}

@ -13,6 +13,7 @@
#include <gint/timer.h>
#include <gint/std/string.h>
#include <gint/std/stdio.h>
#include <liblog.h>
#define PACKED_EMPTY 0
#define PACKED_STONE 1
@ -26,6 +27,8 @@
#define PACKED_TUYAU_BOUT 8
#define PACKED_TUYAU_MIDDLE 9
#define PACKED_FLAG 10
void malloc_error()
{
extern image_t img_ram;
@ -36,37 +39,61 @@ void malloc_error()
mkb_getkey();
}
static uint8_t pack_access(packed_level_t const * const p, int x, int y)
static uint8_t pack_access(packed_level_t const * const p, unsigned int x, unsigned int y)
{
if (0<=x && x<w && 0<=y && y<h)
if (x<p->width && y<p->height)
{
ll_sendp(LEVEL_INFO, "\n[i]pack access at %d,%d",x,y);
return p->data[x*p->height+y];
}
else
{
ll_sendp(LEVEL_WARNING, "\n[w]pack access out of range at %d,%d",x,y);
return PACKED_EMPTY;
}
}
static void cell_set(cell_t *const array, int w, int h, int x, int y, cell_t const cell)
static void cell_set(cell_t *const array, unsigned int w, unsigned int h, unsigned int x, unsigned int y, cell_t const cell)
{
if (0<=x && x<w && 0<=y && y<h)
array[x*w+y] = cell;
if (x<w && y<h)
array[x*h+y] = cell;
}
void unpack_level(packed_level_t const * const p)
static void unpack_level(packed_level_t * p)
{
const unsigned int w = p->width;
const unsigned int h = p->height;
unsigned int w = p->width;
unsigned int h = p->height;
ll_sendp(LEVEL_INFO, "\nUnpacking level %dx%d", w,h);
cell_t * c = (cell_t *) malloc(sizeof(cell_t) * w * h);
ll_sendp(LEVEL_INFO, "\n[i]malloc %d", sizeof(cell_t) * w * h);
if (c==0)
malloc_error();
{ // overriding parameters
int sx=0, sy=p->height; // Mario start coords
w = 232;//p->width;
h = 13;//p->height;
ll_sendp(LEVEL_CRITICAL, "\n[e]malloc failed !\n[i]Trying defaults:\n %dx%d", w,h);
ll_sendp(LEVEL_WARNING, "\n[w]writing into pack new coordinates:\n %dx%d", w,h);
p->width=w;
p->height=h;
cell_t * c = (cell_t *) malloc(sizeof(cell_t) * w * h);
if (c==0)
malloc_error();
}
ll_set_level(LEVEL_WARNING);
for (int x=0; x<w; x++)
int sx=0, sy=p->height; // Mario start coords
ll_sendp(LEVEL_INFO, "\n[i]Converting...", w,h);
for (unsigned int x=0; x<w; x++)
{
for (int y=0; y<h; y++)
for (unsigned int y=0; y<h; y++)
{
int contents = pack_access(p,x,y);
unsigned int contents = pack_access(p,x,y);
cell_t cell = {0,0};
if (contents==PACKED_EMPTY)
{
@ -125,52 +152,134 @@ void unpack_level(packed_level_t const * const p)
}
else if (contents==PACKED_TUYAU_BOUT)
{
int x=0, y=0;
int px=0, py=0;
int props=0;
{ // haut du tuyau horizontal
if (pack_access(p,x-1,y)==PACKED_TUYAU_BOUT)
{
x=1;
px=1;
if (pack_access(p,x,y-1)==PACKED_TUYAU_MIDDLE)
y=2;
py=2;
if (pack_access(p,x,y+1)==PACKED_TUYAU_MIDDLE)
y=4;
py=4;
}
if (pack_access(p,x+1,y)==PACKED_TUYAU_BOUT)
{
x=0;
px=0;
if (pack_access(p,x,y-1)==PACKED_TUYAU_MIDDLE)
y=2;
py=2;
if (pack_access(p,x,y+1)==PACKED_TUYAU_MIDDLE)
y=4;
py=4;
}
}
{ // bout de tuyau vertical
if (pack_access(p,x,y-1)==PACKED_TUYAU_BOUT)
{
y=0;
py=0;
if (pack_access(p,x+1,y)==PACKED_TUYAU_MIDDLE)
x=0;
px=0;
if (pack_access(p,x-1,y)==PACKED_TUYAU_MIDDLE)
x=2;
px=2;
}
if (pack_access(p,x,y+1)==PACKED_TUYAU_BOUT)
{
y=1;
py=1;
if (pack_access(p,x+1,y)==PACKED_TUYAU_MIDDLE)
x=0;
px=0;
if (pack_access(p,x-1,y)==PACKED_TUYAU_MIDDLE)
x=2;
px=2;
}
}
props=16*px+py;
cell_t t={TUYAU,props};
cell = t;
}
else if (contents==PACKED_TUYAU_MIDDLE)
{
int px=0, py=0;
int props=0;
if (/*pack_access(p,x-1,y)==PACKED_TUYAU_MIDDLE && */pack_access(p,x+1,y)!=PACKED_TUYAU_MIDDLE)
{
if (pack_access(p,x+1,y)==PACKED_TUYAU_BOUT)
{
px=1;
if (pack_access(p,x,y+1)!=PACKED_TUYAU_MIDDLE)
py=0;
else
py=1;
}
else
{
px=1;
py=3;
}
}
if (/*pack_access(p,x+1,y)==PACKED_TUYAU_MIDDLE && */pack_access(p,x-1,y)!=PACKED_TUYAU_MIDDLE)
{
if (pack_access(p,x-1,y)==PACKED_TUYAU_BOUT)
{
px=1;
if (pack_access(p,x,y+1)!=PACKED_TUYAU_MIDDLE)
py=0;
else
py=1;
}
else
{
px=0;
py=3;
}
}
props=16*x+y;
props=16*px+py;
cell_t t={TUYAU,props};
cell = t;
}
else if (contents==PACKED_FLAG)
{
int props=0;
if (pack_access(p,x,y-1)!=PACKED_FLAG)
props=0x1400;
else if (pack_access(p,x,y-2)!=PACKED_FLAG)
props=0x1401;
else if (pack_access(p,x,y-3)!=PACKED_FLAG)
props=0x1302;
else if (pack_access(p,x,y-4)!=PACKED_FLAG)
props=0x1302;
else if (pack_access(p,x,y-5)!=PACKED_FLAG)
props=0x1202;
else if (pack_access(p,x,y-6)!=PACKED_FLAG)
props=0x1202;
else
props=0x1102;
cell_t t={END_LEVEL,props};
cell = t;
}
ll_sendp(LEVEL_INFO, "\n[i]Converted cell %d,%d\n {%d,%p}\n[i]Writing cell...", x,y, cell.type, cell.data);
cell_set(c,w,h,x,y,cell);
ll_sendp(LEVEL_INFO, "\n[i]Wrote cell", x,y, cell.type, cell.data);
}
}
ll_sendp(LEVEL_INFO, "\n[i]Converted!\n[i]Sending to level zone...", w,h);
world_set(w, h, sx, sy, c);
ll_sendp(LEVEL_INFO, "\n[i]Achieved unpacking", w,h);
free(c);
}
void set_level(int w, int l)
{
extern packed_level_t bin_lvl_1_2;
unpack_level(&bin_lvl_1_2);
}
void get_lvl_id(int w, int l, char * str)
{
str[3]='\0';
sprintf(str, "%d-%d", w+1, l+1);
}

@ -219,7 +219,7 @@ void world_move()
}
void init_level(int w, int h, int x, int y, cell_t const * a)
void world_set(int w, int h, int x, int y, cell_t const * a)
{
// Resets mario's vx
mario.p.vx=0; mario.p.vy=0;

Loading…
Cancel
Save