diff options
Diffstat (limited to 'src/renderpng.cpp')
-rw-r--r-- | src/renderpng.cpp | 251 |
1 files changed, 160 insertions, 91 deletions
diff --git a/src/renderpng.cpp b/src/renderpng.cpp index 21d35ba..6ff9fbe 100644 --- a/src/renderpng.cpp +++ b/src/renderpng.cpp | |||
@@ -3,11 +3,15 @@ | |||
3 | #include "map.h" | 3 | #include "map.h" |
4 | #include "cell.h" | 4 | #include "cell.h" |
5 | 5 | ||
6 | #include "image.h" | ||
7 | #include "palette.h" | ||
8 | #include "font.h" | ||
9 | |||
6 | #include <math.h> | 10 | #include <math.h> |
7 | #include <string.h> | 11 | #include <string.h> |
8 | #include <stdint.h> | 12 | #include <stdint.h> |
9 | #include <stdlib.h> | 13 | #include <stdlib.h> |
10 | #include <png.h> | 14 | #include <stdio.h> |
11 | 15 | ||
12 | RenderPng::RenderPng( Map &rMap ) : | 16 | RenderPng::RenderPng( Map &rMap ) : |
13 | rMap( rMap ) | 17 | rMap( rMap ) |
@@ -25,16 +29,18 @@ void RenderPng::render() | |||
25 | if( iDims > 2 ) | 29 | if( iDims > 2 ) |
26 | iSize = (int)ceil(sqrt((rMap.getDims()-2)*2)); | 30 | iSize = (int)ceil(sqrt((rMap.getDims()-2)*2)); |
27 | 31 | ||
28 | int iBufWidth = ((iSize+1)*rMap.getSize( 0 )+1)*5; | 32 | int iCellSize = 11; |
29 | int iBufHeight = ((iSize+1)*rMap.getSize( 1 )+1)*5; | 33 | int iCellMid = iCellSize/2; |
30 | int iBufSize = iBufWidth*iBufHeight; | 34 | int iBufWidth = ((iSize+1)*rMap.getSize( 0 )+1)*iCellSize; |
31 | uint8_t *pBuf = new uint8_t[iBufSize]; | 35 | int iBufHeight = ((iSize+1)*rMap.getSize( 1 )+1)*iCellSize+12; |
32 | uint8_t **pRows = new uint8_t*[iBufHeight]; | 36 | int ox = 0; |
33 | for( int r = 0; r < iBufHeight; r++ ) | 37 | int oy = 12; |
34 | { | 38 | Image im( iBufWidth, iBufHeight ); |
35 | pRows[r] = pBuf+(iBufWidth*r); | 39 | Font fnt("ter-u12n.bdf"); |
36 | } | 40 | im.clear( 1 ); |
37 | memset( pBuf, 1, iBufSize ); | 41 | Palette pal; |
42 | pal.addColor( 0, 0, 0 ); | ||
43 | pal.addColor( 255, 255, 255 ); | ||
38 | 44 | ||
39 | Position p( iDims ); | 45 | Position p( iDims ); |
40 | for(;;) | 46 | for(;;) |
@@ -50,86 +56,138 @@ void RenderPng::render() | |||
50 | if( (iRow == 0 || iRow == iSize+1) && | 56 | if( (iRow == 0 || iRow == iSize+1) && |
51 | (iCol == 0 || iCol == iSize+1) ) | 57 | (iCol == 0 || iCol == iSize+1) ) |
52 | { | 58 | { |
53 | pRows[(y+iRow)*5+2][(x+iCol)*5+2] = 0; | 59 | im.set( |
60 | ox+(x+iCol)*iCellSize+iCellMid, | ||
61 | oy+(y+iRow)*iCellSize+iCellMid, | ||
62 | 0 | ||
63 | ); | ||
54 | if( iRow == 0 && iCol == 0 ) | 64 | if( iRow == 0 && iCol == 0 ) |
55 | { | 65 | { |
56 | if( (iWalls&(1<<2)) == 0 ) | 66 | if( (iWalls&(1<<2)) == 0 ) |
57 | { | 67 | { |
58 | for( int i = 0; i < 3; i++ ) | 68 | for( int i = 0; i < iCellMid+1; i++ ) |
59 | pRows[(y+iRow)*5+2][(x+iCol)*5+i+3] = 0; | 69 | im.set( |
70 | ox+(x+iCol)*iCellSize+i+iCellMid+1, | ||
71 | oy+(y+iRow)*iCellSize+iCellMid, | ||
72 | 0 | ||
73 | ); | ||
60 | } | 74 | } |
61 | if( (iWalls&(1<<0)) == 0 ) | 75 | if( (iWalls&(1<<0)) == 0 ) |
62 | { | 76 | { |
63 | for( int i = 0; i < 3; i++ ) | 77 | for( int i = 0; i < iCellMid+1; i++ ) |
64 | pRows[(y+iRow)*5+i+3][(x+iCol)*5+2] = 0; | 78 | im.set( |
79 | ox+(x+iCol)*iCellSize+iCellMid, | ||
80 | oy+(y+iRow)*iCellSize+i+iCellMid+1, | ||
81 | 0 | ||
82 | ); | ||
65 | } | 83 | } |
66 | } | 84 | } |
67 | else if( iRow == 0 && iCol == iSize+1 ) | 85 | else if( iRow == 0 && iCol == iSize+1 ) |
68 | { | 86 | { |
69 | if( (iWalls&(1<<2)) == 0 ) | 87 | if( (iWalls&(1<<2)) == 0 ) |
70 | { | 88 | { |
71 | for( int i = 0; i < 3; i++ ) | 89 | for( int i = 0; i < iCellMid+1; i++ ) |
72 | pRows[(y+iRow)*5+2][(x+iCol)*5+i] = 0; | 90 | im.set( |
91 | ox+(x+iCol)*iCellSize+i, | ||
92 | oy+(y+iRow)*iCellSize+iCellMid, | ||
93 | 0 | ||
94 | ); | ||
73 | } | 95 | } |
74 | if( (iWalls&(1<<1)) == 0 ) | 96 | if( (iWalls&(1<<1)) == 0 ) |
75 | { | 97 | { |
76 | for( int i = 0; i < 3; i++ ) | 98 | for( int i = 0; i < iCellMid+1; i++ ) |
77 | pRows[(y+iRow)*5+i+3][(x+iCol)*5+2] = 0; | 99 | im.set( |
100 | ox+(x+iCol)*iCellSize+iCellMid, | ||
101 | oy+(y+iRow)*iCellSize+i+iCellMid+1, | ||
102 | 0 | ||
103 | ); | ||
78 | } | 104 | } |
79 | } | 105 | } |
80 | else if( iRow == iSize+1 && iCol == 0 ) | 106 | else if( iRow == iSize+1 && iCol == 0 ) |
81 | { | 107 | { |
82 | if( (iWalls&(1<<3)) == 0 ) | 108 | if( (iWalls&(1<<3)) == 0 ) |
83 | { | 109 | { |
84 | for( int i = 0; i < 3; i++ ) | 110 | for( int i = 0; i < iCellMid+1; i++ ) |
85 | pRows[(y+iRow)*5+2][(x+iCol)*5+i+3] = 0; | 111 | im.set( |
112 | ox+(x+iCol)*iCellSize+i+iCellMid+1, | ||
113 | oy+(y+iRow)*iCellSize+iCellMid, | ||
114 | 0 | ||
115 | ); | ||
86 | } | 116 | } |
87 | if( (iWalls&(1<<0)) == 0 ) | 117 | if( (iWalls&(1<<0)) == 0 ) |
88 | { | 118 | { |
89 | for( int i = 0; i < 3; i++ ) | 119 | for( int i = 0; i < iCellMid+1; i++ ) |
90 | pRows[(y+iRow)*5+i][(x+iCol)*5+2] = 0; | 120 | im.set( |
121 | ox+(x+iCol)*iCellSize+iCellMid, | ||
122 | oy+(y+iRow)*iCellSize+i, | ||
123 | 0 | ||
124 | ); | ||
91 | } | 125 | } |
92 | } | 126 | } |
93 | else if( iRow == iSize+1 && iCol == iSize+1 ) | 127 | else if( iRow == iSize+1 && iCol == iSize+1 ) |
94 | { | 128 | { |
95 | if( (iWalls&(1<<3)) == 0 ) | 129 | if( (iWalls&(1<<3)) == 0 ) |
96 | { | 130 | { |
97 | for( int i = 0; i < 3; i++ ) | 131 | for( int i = 0; i < iCellMid+1; i++ ) |
98 | pRows[(y+iRow)*5+2][(x+iCol)*5+i] = 0; | 132 | im.set( |
133 | ox+(x+iCol)*iCellSize+i, | ||
134 | oy+(y+iRow)*iCellSize+iCellMid, | ||
135 | 0 | ||
136 | ); | ||
99 | } | 137 | } |
100 | if( (iWalls&(1<<1)) == 0 ) | 138 | if( (iWalls&(1<<1)) == 0 ) |
101 | { | 139 | { |
102 | for( int i = 0; i < 3; i++ ) | 140 | for( int i = 0; i < iCellMid+1; i++ ) |
103 | pRows[(y+iRow)*5+i][(x+iCol)*5+2] = 0; | 141 | im.set( |
142 | ox+(x+iCol)*iCellSize+iCellMid, | ||
143 | oy+(y+iRow)*iCellSize+i, | ||
144 | 0 | ||
145 | ); | ||
104 | } | 146 | } |
105 | } | 147 | } |
106 | } | 148 | } |
107 | else if( iRow == 0 && (iWalls&(1<<2)) == 0 ) | 149 | else if( iRow == 0 && (iWalls&(1<<2)) == 0 ) |
108 | { | 150 | { |
109 | for( int i = 0; i < 5; i++ ) | 151 | for( int i = 0; i < iCellSize; i++ ) |
110 | { | 152 | { |
111 | pRows[(y+iRow)*5+2][(x+iCol)*5+i] = 0; | 153 | im.set( |
154 | ox+(x+iCol)*iCellSize+i, | ||
155 | oy+(y+iRow)*iCellSize+iCellMid, | ||
156 | 0 | ||
157 | ); | ||
112 | } | 158 | } |
113 | } | 159 | } |
114 | else if( iRow == iSize+1 && (iWalls&(1<<3)) == 0 ) | 160 | else if( iRow == iSize+1 && (iWalls&(1<<3)) == 0 ) |
115 | { | 161 | { |
116 | for( int i = 0; i < 5; i++ ) | 162 | for( int i = 0; i < iCellSize; i++ ) |
117 | { | 163 | { |
118 | pRows[(y+iRow)*5+2][(x+iCol)*5+i] = 0; | 164 | im.set( |
165 | ox+(x+iCol)*iCellSize+i, | ||
166 | oy+(y+iRow)*iCellSize+iCellMid, | ||
167 | 0 | ||
168 | ); | ||
119 | } | 169 | } |
120 | } | 170 | } |
121 | else if( iCol == 0 && (iWalls&(1<<0)) == 0 ) | 171 | else if( iCol == 0 && (iWalls&(1<<0)) == 0 ) |
122 | { | 172 | { |
123 | for( int i = 0; i < 5; i++ ) | 173 | for( int i = 0; i < iCellSize; i++ ) |
124 | { | 174 | { |
125 | pRows[(y+iRow)*5+i][(x+iCol)*5+2] = 0; | 175 | im.set( |
176 | ox+(x+iCol)*iCellSize+iCellMid, | ||
177 | oy+(y+iRow)*iCellSize+i, | ||
178 | 0 | ||
179 | ); | ||
126 | } | 180 | } |
127 | } | 181 | } |
128 | else if( iCol == iSize+1 && (iWalls&(1<<1)) == 0 ) | 182 | else if( iCol == iSize+1 && (iWalls&(1<<1)) == 0 ) |
129 | { | 183 | { |
130 | for( int i = 0; i < 5; i++ ) | 184 | for( int i = 0; i < iCellSize; i++ ) |
131 | { | 185 | { |
132 | pRows[(y+iRow)*5+i][(x+iCol)*5+2] = 0; | 186 | im.set( |
187 | ox+(x+iCol)*iCellSize+iCellMid, | ||
188 | oy+(y+iRow)*iCellSize+i, | ||
189 | 0 | ||
190 | ); | ||
133 | } | 191 | } |
134 | } | 192 | } |
135 | } | 193 | } |
@@ -142,43 +200,73 @@ void RenderPng::render() | |||
142 | int d = j*2+side; | 200 | int d = j*2+side; |
143 | if( iWalls&(1<<d) ) | 201 | if( iWalls&(1<<d) ) |
144 | { | 202 | { |
145 | int iSubX = (x+1+((d-4)%iSize))*5; | 203 | int iSubX = (x+1+((d-4)%iSize))*iCellSize; |
146 | int iSubY = (y+1+((d-4)/iSize))*5; | 204 | int iSubY = (y+1+((d-4)/iSize))*iCellSize; |
147 | // | 205 | |
148 | // char &c = buf[x+1+iSubX+(y+1+iSubY)*iBufWidth]; | ||
149 | // c = cPassages[d]; | ||
150 | switch( d ) | 206 | switch( d ) |
151 | { | 207 | { |
152 | case 4: | 208 | case 4: |
153 | pRows[iSubY+1][iSubX+2] = 0; | 209 | im.set( ox+iSubX+iCellMid, oy+iSubY+1, 0 ); |
154 | pRows[iSubY+2][iSubX+1] = 0; | 210 | for( int m = 0; m < iCellMid; m++ ) |
155 | pRows[iSubY+2][iSubX+3] = 0; | 211 | { |
156 | pRows[iSubY+3][iSubX+0] = 0; | 212 | im.set( ox+iSubX+m, oy+iSubY+iCellMid-m+1, 0 ); |
157 | pRows[iSubY+3][iSubX+4] = 0; | 213 | im.set( ox+iSubX+iCellMid+m+1, oy+iSubY+m+2, 0 ); |
214 | } | ||
215 | /* | ||
216 | im.set( iSubX+2, iSubY+1, 0 ); | ||
217 | im.set( iSubX+1, iSubY+2, 0 ); | ||
218 | im.set( iSubX+3, iSubY+2, 0 ); | ||
219 | im.set( iSubX+0, iSubY+3, 0 ); | ||
220 | im.set( iSubX+4, iSubY+3, 0 ); | ||
221 | */ | ||
158 | break; | 222 | break; |
159 | 223 | ||
160 | case 5: | 224 | case 5: |
161 | pRows[iSubY+3][iSubX+2] = 0; | 225 | im.set( ox+iSubX+iCellMid, oy+iSubY+iCellSize-2, 0 ); |
162 | pRows[iSubY+2][iSubX+1] = 0; | 226 | for( int m = 0; m < iCellMid; m++ ) |
163 | pRows[iSubY+2][iSubX+3] = 0; | 227 | { |
164 | pRows[iSubY+1][iSubX+0] = 0; | 228 | im.set( ox+iSubX+m, oy+iSubY+iCellMid+m-1, 0 ); |
165 | pRows[iSubY+1][iSubX+4] = 0; | 229 | im.set( ox+iSubX+iCellMid+m+1, oy+iSubY+iCellSize-m-3, 0 ); |
230 | } | ||
231 | /* | ||
232 | im.set( iSubX+2, iSubY+3, 0 ); | ||
233 | im.set( iSubX+1, iSubY+2, 0 ); | ||
234 | im.set( iSubX+3, iSubY+2, 0 ); | ||
235 | im.set( iSubX+0, iSubY+1, 0 ); | ||
236 | im.set( iSubX+4, iSubY+1, 0 ); | ||
237 | */ | ||
166 | break; | 238 | break; |
167 | 239 | ||
168 | case 6: | 240 | case 6: |
169 | pRows[iSubY+2][iSubX+1] = 0; | 241 | im.set( ox+iSubX+1, oy+iSubY+iCellMid, 0 ); |
170 | pRows[iSubY+1][iSubX+2] = 0; | 242 | for( int m = 0; m < iCellMid; m++ ) |
171 | pRows[iSubY+3][iSubX+2] = 0; | 243 | { |
172 | pRows[iSubY+0][iSubX+3] = 0; | 244 | im.set( ox+iSubX+iCellMid-m+1, oy+iSubY+m, 0 ); |
173 | pRows[iSubY+4][iSubX+3] = 0; | 245 | im.set( ox+iSubX+m+2, oy+iSubY+iCellMid+m+1, 0 ); |
246 | } | ||
247 | /* | ||
248 | im.set( iSubX+1, iSubY+2, 0 ); | ||
249 | im.set( iSubX+2, iSubY+1, 0 ); | ||
250 | im.set( iSubX+2, iSubY+3, 0 ); | ||
251 | im.set( iSubX+3, iSubY+0, 0 ); | ||
252 | im.set( iSubX+3, iSubY+4, 0 ); | ||
253 | */ | ||
174 | break; | 254 | break; |
175 | 255 | ||
176 | case 7: | 256 | case 7: |
177 | pRows[iSubY+2][iSubX+3] = 0; | 257 | im.set( ox+iSubX+iCellSize-2, oy+iSubY+iCellMid, 0 ); |
178 | pRows[iSubY+1][iSubX+2] = 0; | 258 | for( int m = 0; m < iCellMid; m++ ) |
179 | pRows[iSubY+3][iSubX+2] = 0; | 259 | { |
180 | pRows[iSubY+0][iSubX+1] = 0; | 260 | im.set( ox+iSubX+iCellMid+m-1, oy+iSubY+m, 0 ); |
181 | pRows[iSubY+4][iSubX+1] = 0; | 261 | im.set( ox+iSubX+iCellSize-m-3, oy+iSubY+iCellMid+m+1, 0 ); |
262 | } | ||
263 | /* | ||
264 | im.set( iSubX+3, iSubY+2, 0 ); | ||
265 | im.set( iSubX+2, iSubY+1, 0 ); | ||
266 | im.set( iSubX+2, iSubY+3, 0 ); | ||
267 | im.set( iSubX+1, iSubY+0, 0 ); | ||
268 | im.set( iSubX+1, iSubY+4, 0 ); | ||
269 | */ | ||
182 | break; | 270 | break; |
183 | } | 271 | } |
184 | } | 272 | } |
@@ -187,6 +275,7 @@ void RenderPng::render() | |||
187 | 275 | ||
188 | int iDim; | 276 | int iDim; |
189 | char fname[1024]; | 277 | char fname[1024]; |
278 | char title[1024]; | ||
190 | for( iDim = 0; iDim < iDims; iDim++ ) | 279 | for( iDim = 0; iDim < iDims; iDim++ ) |
191 | { | 280 | { |
192 | if( ++p[iDim] < rMap.getSize( iDim ) ) | 281 | if( ++p[iDim] < rMap.getSize( iDim ) ) |
@@ -200,43 +289,25 @@ void RenderPng::render() | |||
200 | char buf[1024]; | 289 | char buf[1024]; |
201 | sprintf( buf, "-%d", rMap.getSize(2)-p[2] ); | 290 | sprintf( buf, "-%d", rMap.getSize(2)-p[2] ); |
202 | strcat( fname, buf ); | 291 | strcat( fname, buf ); |
292 | sprintf( buf, "Floor: %d", rMap.getSize(2)-p[2] ); | ||
293 | strcpy( title, buf ); | ||
203 | for( int j = 3; j < iDims; j++ ) | 294 | for( int j = 3; j < iDims; j++ ) |
204 | { | 295 | { |
205 | sprintf( buf, "-%d", rMap.getSize(j)-p[j] ); | 296 | sprintf( buf, "-%d", rMap.getSize(j)-p[j] ); |
206 | strcat( fname, buf ); | 297 | strcat( fname, buf ); |
298 | sprintf( buf, ", %d", rMap.getSize(j)-p[j] ); | ||
299 | strcat( title, buf ); | ||
207 | } | 300 | } |
208 | } | 301 | } |
302 | else | ||
303 | title[0] = '\0'; | ||
304 | im.drawText( fnt, ox, oy-2, 0, title ); | ||
209 | strcat( fname, ".png"); | 305 | strcat( fname, ".png"); |
210 | printf("Output: %s\n", fname ); | 306 | printf("Output: %s\n", fname ); |
211 | 307 | ||
212 | png_structp png_ptr = png_create_write_struct( | 308 | im.save( fname, pal ); |
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 | 309 | ||
239 | memset( pBuf, 1, iBufSize ); | 310 | im.clear( 1 ); |
240 | } | 311 | } |
241 | } | 312 | } |
242 | if( iDim == iDims ) | 313 | if( iDim == iDims ) |
@@ -260,7 +331,5 @@ void RenderPng::render() | |||
260 | } | 331 | } |
261 | // printf(buf); | 332 | // printf(buf); |
262 | 333 | ||
263 | delete[] pBuf; | ||
264 | delete[] pRows; | ||
265 | } | 334 | } |
266 | 335 | ||