diff options
| author | Mike Buland <mike@xagasoft.com> | 2015-04-02 15:28:31 -0600 |
|---|---|---|
| committer | Mike Buland <mike@xagasoft.com> | 2015-04-02 15:28:31 -0600 |
| commit | 518619603ab3c49b7fdfcf19c439c1a30668164f (patch) | |
| tree | dda7774f51bde150db91dac14718cc78f7571039 /src/renderascii.cpp | |
| download | lost-518619603ab3c49b7fdfcf19c439c1a30668164f.tar.gz lost-518619603ab3c49b7fdfcf19c439c1a30668164f.tar.bz2 lost-518619603ab3c49b7fdfcf19c439c1a30668164f.tar.xz lost-518619603ab3c49b7fdfcf19c439c1a30668164f.zip | |
Everything works, it could use more stuff.
Diffstat (limited to '')
| -rw-r--r-- | src/renderascii.cpp | 264 |
1 files changed, 264 insertions, 0 deletions
diff --git a/src/renderascii.cpp b/src/renderascii.cpp new file mode 100644 index 0000000..62a7028 --- /dev/null +++ b/src/renderascii.cpp | |||
| @@ -0,0 +1,264 @@ | |||
| 1 | #include "renderascii.h" | ||
| 2 | #include "position.h" | ||
| 3 | #include "map.h" | ||
| 4 | #include "cell.h" | ||
| 5 | |||
| 6 | #include <math.h> | ||
| 7 | #include <stdio.h> | ||
| 8 | #include <string.h> | ||
| 9 | |||
| 10 | RenderAscii::RenderAscii( Map &rMap ) : | ||
| 11 | rMap( rMap ) | ||
| 12 | { | ||
| 13 | } | ||
| 14 | |||
| 15 | RenderAscii::~RenderAscii() | ||
| 16 | { | ||
| 17 | } | ||
| 18 | |||
| 19 | void postProcess( char *buf, int w, int h ) | ||
| 20 | { | ||
| 21 | static const char *cJunct[] = { | ||
| 22 | "", // 0 blank | ||
| 23 | "\xe2\x94\x80", // 1 left | ||
| 24 | "\xe2\x94\x80", // 2 right | ||
| 25 | "\xe2\x94\x80", // 1 2 left right | ||
| 26 | "\xe2\x94\x82", // 4 up | ||
| 27 | "\xe2\x94\x98", // 1 4 left up | ||
| 28 | "\xe2\x94\x94", // 2 4 right up | ||
| 29 | "\xe2\x94\xb4", // 1 2 4 left right up | ||
| 30 | "\xe2\x94\x82", // 8 down | ||
| 31 | "\xe2\x94\x90", // 1 8 left down | ||
| 32 | "\xe2\x94\x8c", // 2 8 right down | ||
| 33 | "\xe2\x94\xac", // 1 2 8 left right down | ||
| 34 | "\xe2\x94\x82", // 4 8 up down | ||
| 35 | "\xe2\x94\xa4", // 1 4 8 left up down | ||
| 36 | "\xe2\x94\x9c", // 2 4 8 right up down | ||
| 37 | "\xE2\x94\xBC", // 1 2 4 8 left right up down | ||
| 38 | "" | ||
| 39 | }; | ||
| 40 | /* | ||
| 41 | // Double lines | ||
| 42 | ' ', // 0 blank | ||
| 43 | '\xcd', // 1 left | ||
| 44 | '\xcd', // 2 right | ||
| 45 | '\xcd', // 1 2 left right | ||
| 46 | '\xba', // 4 up | ||
| 47 | '\xbc', // 1 4 left up | ||
| 48 | '\xc8', // 2 4 right up | ||
| 49 | '\xca', // 1 2 4 left right up | ||
| 50 | '\xba', // 8 down | ||
| 51 | '\xbb', // 1 8 left down | ||
| 52 | '\xc9', // 2 8 right down | ||
| 53 | '\xcb', // 1 2 8 left right down | ||
| 54 | '\xba', // 4 8 up down | ||
| 55 | '\xb9', // 1 4 8 left up down | ||
| 56 | '\xcc', // 2 4 8 right up down | ||
| 57 | '\xce', // 1 2 4 8 left right up down | ||
| 58 | '\0' | ||
| 59 | */ | ||
| 60 | |||
| 61 | for( int y = 0; y < h; y++ ) | ||
| 62 | { | ||
| 63 | for( int x = 0; x < w; x++ ) | ||
| 64 | { | ||
| 65 | if( buf[x+y*w] == '+' ) | ||
| 66 | { | ||
| 67 | int iJunct = 0; | ||
| 68 | if( x > 0 && buf[(x-1)+y*w] == '-' ) | ||
| 69 | iJunct |= 1; | ||
| 70 | if( x < w-1 && buf[(x+1)+y*w] == '-' ) | ||
| 71 | iJunct |= 2; | ||
| 72 | if( y > 0 && buf[x+(y-1)*w] == '|' ) | ||
| 73 | iJunct |= 4; | ||
| 74 | if( y < h-1 && buf[x+(y+1)*w] == '|' ) | ||
| 75 | iJunct |= 8; | ||
| 76 | printf(cJunct[iJunct]); | ||
| 77 | } | ||
| 78 | else | ||
| 79 | { | ||
| 80 | switch( buf[x+y*w] ) | ||
| 81 | { | ||
| 82 | case '-': | ||
| 83 | printf("\xe2\x94\x80"); | ||
| 84 | break; | ||
| 85 | |||
| 86 | case '|': | ||
| 87 | printf("\xe2\x94\x82"); | ||
| 88 | break; | ||
| 89 | |||
| 90 | case '^': | ||
| 91 | printf("\xe2\x86\x91"); | ||
| 92 | break; | ||
| 93 | |||
| 94 | case 'v': | ||
| 95 | printf("\xe2\x86\x93"); | ||
| 96 | break; | ||
| 97 | |||
| 98 | default: | ||
| 99 | printf("%c", buf[x+y*w]); | ||
| 100 | break; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | } | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | void RenderAscii::render() | ||
| 108 | { | ||
| 109 | static char cPassages[] = {"~~~~^v<>/*aAbBcCdDeE"}; | ||
| 110 | |||
| 111 | printf("\xef\xbb\xbf"); | ||
| 112 | |||
| 113 | int iDims = rMap.getDims(); | ||
| 114 | int iSize = 1; | ||
| 115 | if( iDims > 2 ) | ||
| 116 | iSize = (int)ceil(sqrt((rMap.getDims()-2)*2)); | ||
| 117 | |||
| 118 | int iBufWidth = ((iSize+1)*rMap.getSize( 0 )+2); | ||
| 119 | int iBufHeight = (iSize+1)*rMap.getSize( 1 )+1; | ||
| 120 | int iBufSize = iBufWidth*iBufHeight+1; | ||
| 121 | |||
| 122 | char *buf = new char[iBufSize]; | ||
| 123 | memset( buf, '\n', iBufSize ); | ||
| 124 | buf[iBufSize-1] = '\0'; | ||
| 125 | |||
| 126 | Position p( iDims ); | ||
| 127 | for(;;) | ||
| 128 | { | ||
| 129 | int x = p[0]*(iSize+1); | ||
| 130 | int y = p[1]*(iSize+1); | ||
| 131 | int iWalls = rMap[p].iWalls; | ||
| 132 | |||
| 133 | for( int iRow = 0; iRow < iSize+2; iRow++ ) | ||
| 134 | { | ||
| 135 | for( int iCol = 0; iCol < iSize+2; iCol++ ) | ||
| 136 | { | ||
| 137 | char &c = buf[x+iCol+(y+iRow)*iBufWidth]; | ||
| 138 | if( (iRow == 0 || iRow == iSize+1) && | ||
| 139 | (iCol == 0 || iCol == iSize+1) ) | ||
| 140 | c = '+'; | ||
| 141 | else if( iRow == 0 && (iWalls&(1<<2)) == 0 ) | ||
| 142 | c = '-'; | ||
| 143 | else if( iRow == iSize+1 && (iWalls&(1<<3)) == 0 ) | ||
| 144 | c = '-'; | ||
| 145 | else if( iCol == 0 && (iWalls&(1<<0)) == 0 ) | ||
| 146 | c = '|'; | ||
| 147 | else if( iCol == iSize+1 && (iWalls&(1<<1)) == 0 ) | ||
| 148 | c = '|'; | ||
| 149 | else | ||
| 150 | c = ' '; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | for( int j = 2; j < iDims; j++ ) | ||
| 155 | { | ||
| 156 | for( int side = 0; side < 2; side++ ) | ||
| 157 | { | ||
| 158 | int d = j*2+side; | ||
| 159 | if( iWalls&(1<<d) ) | ||
| 160 | { | ||
| 161 | int iSubX = (d-4)%iSize; | ||
| 162 | int iSubY = (d-4)/iSize; | ||
| 163 | char &c = buf[x+1+iSubX+(y+1+iSubY)*iBufWidth]; | ||
| 164 | c = cPassages[d]; | ||
| 165 | } | ||
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 169 | int iDim; | ||
| 170 | for( iDim = 0; iDim < iDims; iDim++ ) | ||
| 171 | { | ||
| 172 | if( ++p[iDim] < rMap.getSize( iDim ) ) | ||
| 173 | break; | ||
| 174 | p[iDim] = 0; | ||
| 175 | if( iDim == 1 ) | ||
| 176 | { | ||
| 177 | if( iDims > 2 ) | ||
| 178 | { | ||
| 179 | printf("Floor: (%d", rMap.getSize(2)-p[2]); | ||
| 180 | for( int j = 3; j < iDims; j++ ) | ||
| 181 | { | ||
| 182 | printf(", %d", rMap.getSize(j)-p[j] ); | ||
| 183 | } | ||
| 184 | printf(")\n"); | ||
| 185 | } | ||
| 186 | |||
| 187 | postProcess( buf, iBufWidth, iBufHeight ); | ||
| 188 | // printf(buf); | ||
| 189 | memset( buf, '\n', iBufSize ); | ||
| 190 | buf[iBufSize-1] = '\0'; | ||
| 191 | } | ||
| 192 | } | ||
| 193 | /* | ||
| 194 | printf("[%d", p[0]); | ||
| 195 | for( int j = 1; j < iDims; j++ ) | ||
| 196 | { | ||
| 197 | printf(", %d", p[j] ); | ||
| 198 | } | ||
| 199 | printf("]\n"); | ||
| 200 | */ | ||
| 201 | if( iDim == iDims ) | ||
| 202 | break; | ||
| 203 | } | ||
| 204 | // printf(buf); | ||
| 205 | |||
| 206 | delete[] buf; | ||
| 207 | } | ||
| 208 | /* | ||
| 209 | int iSizeX = rMap.getSize( 0 ); | ||
| 210 | int iSizeY = 1; | ||
| 211 | if( iDims >= 2 ) | ||
| 212 | iSizeY = rMap.getSize( 1 ); | ||
| 213 | |||
| 214 | for( int y = 0; y < iSizeY; y++ ) | ||
| 215 | { | ||
| 216 | for( int iRow = (y==0)?0:1; iRow < iSize+2; iRow++ ) | ||
| 217 | { | ||
| 218 | for( int x = 0; x < iSizeX; x++ ) | ||
| 219 | { | ||
| 220 | int iWalls = rMap[Position(2, x, y)].iWalls; | ||
| 221 | for( int iCol = (x==0)?0:1; iCol < iSize+2; iCol++ ) | ||
| 222 | { | ||
| 223 | if( (iRow == 0 || iRow == iSize+1 ) && | ||
| 224 | (iCol == 0 || iCol == iSize+1 )) | ||
| 225 | { | ||
| 226 | printf("+"); | ||
| 227 | } | ||
| 228 | else if( iRow == 0 ) | ||
| 229 | { | ||
| 230 | if( (iWalls&(1<<2)) == 0 ) | ||
| 231 | printf("-"); | ||
| 232 | else | ||
| 233 | printf(" "); | ||
| 234 | } | ||
| 235 | else if( iRow == iSize+1 ) | ||
| 236 | { | ||
| 237 | if( (iWalls&(1<<3)) == 0 ) | ||
| 238 | printf("-"); | ||
| 239 | else | ||
| 240 | printf(" "); | ||
| 241 | } | ||
| 242 | else if( iCol == 0 ) | ||
| 243 | { | ||
| 244 | if( (iWalls&(1<<0)) == 0 ) | ||
| 245 | printf("|"); | ||
| 246 | else | ||
| 247 | printf(" "); | ||
| 248 | } | ||
| 249 | else if( iCol == iSize+1 ) | ||
| 250 | { | ||
| 251 | if( (iWalls&(1<<1)) == 0 ) | ||
| 252 | printf("|"); | ||
| 253 | else | ||
| 254 | printf(" "); | ||
| 255 | } | ||
| 256 | else | ||
| 257 | printf(" "); | ||
| 258 | } | ||
| 259 | } | ||
| 260 | printf("\n"); | ||
| 261 | } | ||
| 262 | } | ||
| 263 | } | ||
| 264 | */ | ||
