#include "renderpng.h" #include "position.h" #include "map.h" #include "cell.h" #include #include #include #include #include RenderPng::RenderPng( Map &rMap ) : rMap( rMap ) { } RenderPng::~RenderPng() { } void RenderPng::render() { int iDims = rMap.getDims(); int iSize = 1; if( iDims > 2 ) iSize = (int)ceil(sqrt((rMap.getDims()-2)*2)); int iBufWidth = ((iSize+1)*rMap.getSize( 0 )+1)*5; int iBufHeight = ((iSize+1)*rMap.getSize( 1 )+1)*5; int iBufSize = iBufWidth*iBufHeight; uint8_t *pBuf = new uint8_t[iBufSize]; uint8_t **pRows = new uint8_t*[iBufHeight]; for( int r = 0; r < iBufHeight; r++ ) { pRows[r] = pBuf+(iBufWidth*r); } memset( pBuf, 1, iBufSize ); Position p( iDims ); for(;;) { int x = p[0]*(iSize+1); int y = p[1]*(iSize+1); int iWalls = rMap[p].iWalls; for( int iRow = 0; iRow < iSize+2; iRow++ ) { for( int iCol = 0; iCol < iSize+2; iCol++ ) { if( (iRow == 0 || iRow == iSize+1) && (iCol == 0 || iCol == iSize+1) ) { pRows[(y+iRow)*5+2][(x+iCol)*5+2] = 0; if( iRow == 0 && iCol == 0 ) { if( (iWalls&(1<<2)) == 0 ) { for( int i = 0; i < 3; i++ ) pRows[(y+iRow)*5+2][(x+iCol)*5+i+3] = 0; } if( (iWalls&(1<<0)) == 0 ) { for( int i = 0; i < 3; i++ ) pRows[(y+iRow)*5+i+3][(x+iCol)*5+2] = 0; } } else if( iRow == 0 && iCol == iSize+1 ) { if( (iWalls&(1<<2)) == 0 ) { for( int i = 0; i < 3; i++ ) pRows[(y+iRow)*5+2][(x+iCol)*5+i] = 0; } if( (iWalls&(1<<1)) == 0 ) { for( int i = 0; i < 3; i++ ) pRows[(y+iRow)*5+i+3][(x+iCol)*5+2] = 0; } } else if( iRow == iSize+1 && iCol == 0 ) { if( (iWalls&(1<<3)) == 0 ) { for( int i = 0; i < 3; i++ ) pRows[(y+iRow)*5+2][(x+iCol)*5+i+3] = 0; } if( (iWalls&(1<<0)) == 0 ) { for( int i = 0; i < 3; i++ ) pRows[(y+iRow)*5+i][(x+iCol)*5+2] = 0; } } else if( iRow == iSize+1 && iCol == iSize+1 ) { if( (iWalls&(1<<3)) == 0 ) { for( int i = 0; i < 3; i++ ) pRows[(y+iRow)*5+2][(x+iCol)*5+i] = 0; } if( (iWalls&(1<<1)) == 0 ) { for( int i = 0; i < 3; i++ ) pRows[(y+iRow)*5+i][(x+iCol)*5+2] = 0; } } } else if( iRow == 0 && (iWalls&(1<<2)) == 0 ) { for( int i = 0; i < 5; i++ ) { pRows[(y+iRow)*5+2][(x+iCol)*5+i] = 0; } } else if( iRow == iSize+1 && (iWalls&(1<<3)) == 0 ) { for( int i = 0; i < 5; i++ ) { pRows[(y+iRow)*5+2][(x+iCol)*5+i] = 0; } } else if( iCol == 0 && (iWalls&(1<<0)) == 0 ) { for( int i = 0; i < 5; i++ ) { pRows[(y+iRow)*5+i][(x+iCol)*5+2] = 0; } } else if( iCol == iSize+1 && (iWalls&(1<<1)) == 0 ) { for( int i = 0; i < 5; i++ ) { pRows[(y+iRow)*5+i][(x+iCol)*5+2] = 0; } } } } for( int j = 2; j < iDims; j++ ) { for( int side = 0; side < 2; side++ ) { int d = j*2+side; if( iWalls&(1< 2 ) { char buf[1024]; sprintf( buf, "-%d", rMap.getSize(2)-p[2] ); strcat( fname, buf ); for( int j = 3; j < iDims; j++ ) { sprintf( buf, "-%d", rMap.getSize(j)-p[j] ); strcat( fname, buf ); } } strcat( fname, ".png"); printf("Output: %s\n", fname ); png_structp png_ptr = png_create_write_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL ); png_infop info_ptr = png_create_info_struct(png_ptr); png_set_IHDR(png_ptr, info_ptr, iBufWidth, iBufHeight, 8, PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT ); png_color col[2]; col[0].red = col[0].green = col[0].blue = 0; col[1].red = col[1].green = col[1].blue = 255; png_set_PLTE( png_ptr, info_ptr, col, 2); png_set_rows( png_ptr, info_ptr, pRows ); FILE *fp = fopen(fname, "wb"); if( fp == NULL ) { printf("Error opening file!\n"); exit( 1 ); } png_init_io( png_ptr, fp ); png_write_png( png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL ); png_destroy_write_struct( &png_ptr, &info_ptr ); fclose( fp ); memset( pBuf, 1, iBufSize ); } } if( iDim == iDims ) break; /* for( int j = 2; j < iDims; j++ ) { for( int side = 0; side < 2; side++ ) { int d = j*2+side; if( iWalls&(1<