diff options
Diffstat (limited to '')
| -rw-r--r-- | src/renderpng.cpp | 266 |
1 files changed, 266 insertions, 0 deletions
diff --git a/src/renderpng.cpp b/src/renderpng.cpp new file mode 100644 index 0000000..21d35ba --- /dev/null +++ b/src/renderpng.cpp | |||
| @@ -0,0 +1,266 @@ | |||
| 1 | #include "renderpng.h" | ||
| 2 | #include "position.h" | ||
| 3 | #include "map.h" | ||
| 4 | #include "cell.h" | ||
| 5 | |||
| 6 | #include <math.h> | ||
| 7 | #include <string.h> | ||
| 8 | #include <stdint.h> | ||
| 9 | #include <stdlib.h> | ||
| 10 | #include <png.h> | ||
| 11 | |||
| 12 | RenderPng::RenderPng( Map &rMap ) : | ||
| 13 | rMap( rMap ) | ||
| 14 | { | ||
| 15 | } | ||
| 16 | |||
| 17 | RenderPng::~RenderPng() | ||
| 18 | { | ||
| 19 | } | ||
| 20 | |||
| 21 | void RenderPng::render() | ||
| 22 | { | ||
| 23 | int iDims = rMap.getDims(); | ||
| 24 | int iSize = 1; | ||
| 25 | if( iDims > 2 ) | ||
| 26 | iSize = (int)ceil(sqrt((rMap.getDims()-2)*2)); | ||
| 27 | |||
| 28 | int iBufWidth = ((iSize+1)*rMap.getSize( 0 )+1)*5; | ||
| 29 | int iBufHeight = ((iSize+1)*rMap.getSize( 1 )+1)*5; | ||
| 30 | int iBufSize = iBufWidth*iBufHeight; | ||
| 31 | uint8_t *pBuf = new uint8_t[iBufSize]; | ||
| 32 | uint8_t **pRows = new uint8_t*[iBufHeight]; | ||
| 33 | for( int r = 0; r < iBufHeight; r++ ) | ||
| 34 | { | ||
| 35 | pRows[r] = pBuf+(iBufWidth*r); | ||
| 36 | } | ||
| 37 | memset( pBuf, 1, iBufSize ); | ||
| 38 | |||
| 39 | Position p( iDims ); | ||
| 40 | for(;;) | ||
| 41 | { | ||
| 42 | int x = p[0]*(iSize+1); | ||
| 43 | int y = p[1]*(iSize+1); | ||
| 44 | int iWalls = rMap[p].iWalls; | ||
| 45 | |||
| 46 | for( int iRow = 0; iRow < iSize+2; iRow++ ) | ||
| 47 | { | ||
| 48 | for( int iCol = 0; iCol < iSize+2; iCol++ ) | ||
| 49 | { | ||
| 50 | if( (iRow == 0 || iRow == iSize+1) && | ||
| 51 | (iCol == 0 || iCol == iSize+1) ) | ||
| 52 | { | ||
| 53 | pRows[(y+iRow)*5+2][(x+iCol)*5+2] = 0; | ||
| 54 | if( iRow == 0 && iCol == 0 ) | ||
| 55 | { | ||
| 56 | if( (iWalls&(1<<2)) == 0 ) | ||
| 57 | { | ||
| 58 | for( int i = 0; i < 3; i++ ) | ||
| 59 | pRows[(y+iRow)*5+2][(x+iCol)*5+i+3] = 0; | ||
| 60 | } | ||
| 61 | if( (iWalls&(1<<0)) == 0 ) | ||
| 62 | { | ||
| 63 | for( int i = 0; i < 3; i++ ) | ||
| 64 | pRows[(y+iRow)*5+i+3][(x+iCol)*5+2] = 0; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | else if( iRow == 0 && iCol == iSize+1 ) | ||
| 68 | { | ||
| 69 | if( (iWalls&(1<<2)) == 0 ) | ||
| 70 | { | ||
| 71 | for( int i = 0; i < 3; i++ ) | ||
| 72 | pRows[(y+iRow)*5+2][(x+iCol)*5+i] = 0; | ||
| 73 | } | ||
| 74 | if( (iWalls&(1<<1)) == 0 ) | ||
| 75 | { | ||
| 76 | for( int i = 0; i < 3; i++ ) | ||
| 77 | pRows[(y+iRow)*5+i+3][(x+iCol)*5+2] = 0; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | else if( iRow == iSize+1 && iCol == 0 ) | ||
| 81 | { | ||
| 82 | if( (iWalls&(1<<3)) == 0 ) | ||
| 83 | { | ||
| 84 | for( int i = 0; i < 3; i++ ) | ||
| 85 | pRows[(y+iRow)*5+2][(x+iCol)*5+i+3] = 0; | ||
| 86 | } | ||
| 87 | if( (iWalls&(1<<0)) == 0 ) | ||
| 88 | { | ||
| 89 | for( int i = 0; i < 3; i++ ) | ||
| 90 | pRows[(y+iRow)*5+i][(x+iCol)*5+2] = 0; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | else if( iRow == iSize+1 && iCol == iSize+1 ) | ||
| 94 | { | ||
| 95 | if( (iWalls&(1<<3)) == 0 ) | ||
| 96 | { | ||
| 97 | for( int i = 0; i < 3; i++ ) | ||
| 98 | pRows[(y+iRow)*5+2][(x+iCol)*5+i] = 0; | ||
| 99 | } | ||
| 100 | if( (iWalls&(1<<1)) == 0 ) | ||
| 101 | { | ||
| 102 | for( int i = 0; i < 3; i++ ) | ||
| 103 | pRows[(y+iRow)*5+i][(x+iCol)*5+2] = 0; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | } | ||
| 107 | else if( iRow == 0 && (iWalls&(1<<2)) == 0 ) | ||
| 108 | { | ||
| 109 | for( int i = 0; i < 5; i++ ) | ||
| 110 | { | ||
| 111 | pRows[(y+iRow)*5+2][(x+iCol)*5+i] = 0; | ||
| 112 | } | ||
| 113 | } | ||
| 114 | else if( iRow == iSize+1 && (iWalls&(1<<3)) == 0 ) | ||
| 115 | { | ||
| 116 | for( int i = 0; i < 5; i++ ) | ||
| 117 | { | ||
| 118 | pRows[(y+iRow)*5+2][(x+iCol)*5+i] = 0; | ||
| 119 | } | ||
| 120 | } | ||
| 121 | else if( iCol == 0 && (iWalls&(1<<0)) == 0 ) | ||
| 122 | { | ||
| 123 | for( int i = 0; i < 5; i++ ) | ||
| 124 | { | ||
| 125 | pRows[(y+iRow)*5+i][(x+iCol)*5+2] = 0; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | else if( iCol == iSize+1 && (iWalls&(1<<1)) == 0 ) | ||
| 129 | { | ||
| 130 | for( int i = 0; i < 5; i++ ) | ||
| 131 | { | ||
| 132 | pRows[(y+iRow)*5+i][(x+iCol)*5+2] = 0; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | } | ||
| 136 | } | ||
| 137 | |||
| 138 | for( int j = 2; j < iDims; j++ ) | ||
| 139 | { | ||
| 140 | for( int side = 0; side < 2; side++ ) | ||
| 141 | { | ||
| 142 | int d = j*2+side; | ||
| 143 | if( iWalls&(1<<d) ) | ||
| 144 | { | ||
| 145 | int iSubX = (x+1+((d-4)%iSize))*5; | ||
| 146 | int iSubY = (y+1+((d-4)/iSize))*5; | ||
| 147 | // | ||
| 148 | // char &c = buf[x+1+iSubX+(y+1+iSubY)*iBufWidth]; | ||
| 149 | // c = cPassages[d]; | ||
| 150 | switch( d ) | ||
| 151 | { | ||
| 152 | case 4: | ||
| 153 | pRows[iSubY+1][iSubX+2] = 0; | ||
| 154 | pRows[iSubY+2][iSubX+1] = 0; | ||
| 155 | pRows[iSubY+2][iSubX+3] = 0; | ||
| 156 | pRows[iSubY+3][iSubX+0] = 0; | ||
| 157 | pRows[iSubY+3][iSubX+4] = 0; | ||
| 158 | break; | ||
| 159 | |||
| 160 | case 5: | ||
| 161 | pRows[iSubY+3][iSubX+2] = 0; | ||
| 162 | pRows[iSubY+2][iSubX+1] = 0; | ||
| 163 | pRows[iSubY+2][iSubX+3] = 0; | ||
| 164 | pRows[iSubY+1][iSubX+0] = 0; | ||
| 165 | pRows[iSubY+1][iSubX+4] = 0; | ||
| 166 | break; | ||
| 167 | |||
| 168 | case 6: | ||
| 169 | pRows[iSubY+2][iSubX+1] = 0; | ||
| 170 | pRows[iSubY+1][iSubX+2] = 0; | ||
| 171 | pRows[iSubY+3][iSubX+2] = 0; | ||
| 172 | pRows[iSubY+0][iSubX+3] = 0; | ||
| 173 | pRows[iSubY+4][iSubX+3] = 0; | ||
| 174 | break; | ||
| 175 | |||
| 176 | case 7: | ||
| 177 | pRows[iSubY+2][iSubX+3] = 0; | ||
| 178 | pRows[iSubY+1][iSubX+2] = 0; | ||
| 179 | pRows[iSubY+3][iSubX+2] = 0; | ||
| 180 | pRows[iSubY+0][iSubX+1] = 0; | ||
| 181 | pRows[iSubY+4][iSubX+1] = 0; | ||
| 182 | break; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 188 | int iDim; | ||
| 189 | char fname[1024]; | ||
| 190 | for( iDim = 0; iDim < iDims; iDim++ ) | ||
| 191 | { | ||
| 192 | if( ++p[iDim] < rMap.getSize( iDim ) ) | ||
| 193 | break; | ||
| 194 | p[iDim] = 0; | ||
| 195 | if( iDim == 1 ) | ||
| 196 | { | ||
| 197 | strcpy(fname, "floor"); | ||
| 198 | if( iDims > 2 ) | ||
| 199 | { | ||
| 200 | char buf[1024]; | ||
| 201 | sprintf( buf, "-%d", rMap.getSize(2)-p[2] ); | ||
| 202 | strcat( fname, buf ); | ||
| 203 | for( int j = 3; j < iDims; j++ ) | ||
| 204 | { | ||
| 205 | sprintf( buf, "-%d", rMap.getSize(j)-p[j] ); | ||
| 206 | strcat( fname, buf ); | ||
| 207 | } | ||
| 208 | } | ||
| 209 | strcat( fname, ".png"); | ||
| 210 | printf("Output: %s\n", fname ); | ||
| 211 | |||
| 212 | png_structp png_ptr = png_create_write_struct( | ||
| 213 | PNG_LIBPNG_VER_STRING, NULL, NULL, NULL | ||
| 214 | ); | ||
| 215 | png_infop info_ptr = png_create_info_struct(png_ptr); | ||
| 216 | png_set_IHDR(png_ptr, info_ptr, iBufWidth, iBufHeight, | ||
| 217 | 8, PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE, | ||
| 218 | PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT | ||
| 219 | ); | ||
| 220 | png_color col[2]; | ||
| 221 | col[0].red = col[0].green = col[0].blue = 0; | ||
| 222 | col[1].red = col[1].green = col[1].blue = 255; | ||
| 223 | png_set_PLTE( png_ptr, info_ptr, col, 2); | ||
| 224 | |||
| 225 | png_set_rows( png_ptr, info_ptr, pRows ); | ||
| 226 | |||
| 227 | FILE *fp = fopen(fname, "wb"); | ||
| 228 | if( fp == NULL ) | ||
| 229 | { | ||
| 230 | printf("Error opening file!\n"); | ||
| 231 | exit( 1 ); | ||
| 232 | } | ||
| 233 | |||
| 234 | png_init_io( png_ptr, fp ); | ||
| 235 | png_write_png( png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL ); | ||
| 236 | png_destroy_write_struct( &png_ptr, &info_ptr ); | ||
| 237 | fclose( fp ); | ||
| 238 | |||
| 239 | memset( pBuf, 1, iBufSize ); | ||
| 240 | } | ||
| 241 | } | ||
| 242 | if( iDim == iDims ) | ||
| 243 | break; | ||
| 244 | /* | ||
| 245 | for( int j = 2; j < iDims; j++ ) | ||
| 246 | { | ||
| 247 | for( int side = 0; side < 2; side++ ) | ||
| 248 | { | ||
| 249 | int d = j*2+side; | ||
| 250 | if( iWalls&(1<<d) ) | ||
| 251 | { | ||
| 252 | int iSubX = (d-4)%iSize; | ||
| 253 | int iSubY = (d-4)/iSize; | ||
| 254 | char &c = buf[x+1+iSubX+(y+1+iSubY)*iBufWidth]; | ||
| 255 | c = cPassages[d]; | ||
| 256 | } | ||
| 257 | } | ||
| 258 | } | ||
| 259 | */ | ||
| 260 | } | ||
| 261 | // printf(buf); | ||
| 262 | |||
| 263 | delete[] pBuf; | ||
| 264 | delete[] pRows; | ||
| 265 | } | ||
| 266 | |||
