From 518619603ab3c49b7fdfcf19c439c1a30668164f Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 2 Apr 2015 15:28:31 -0600 Subject: Everything works, it could use more stuff. --- src/renderascii.cpp | 264 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 264 insertions(+) create mode 100644 src/renderascii.cpp (limited to 'src/renderascii.cpp') 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 @@ +#include "renderascii.h" +#include "position.h" +#include "map.h" +#include "cell.h" + +#include +#include +#include + +RenderAscii::RenderAscii( Map &rMap ) : + rMap( rMap ) +{ +} + +RenderAscii::~RenderAscii() +{ +} + +void postProcess( char *buf, int w, int h ) +{ + static const char *cJunct[] = { + "", // 0 blank + "\xe2\x94\x80", // 1 left + "\xe2\x94\x80", // 2 right + "\xe2\x94\x80", // 1 2 left right + "\xe2\x94\x82", // 4 up + "\xe2\x94\x98", // 1 4 left up + "\xe2\x94\x94", // 2 4 right up + "\xe2\x94\xb4", // 1 2 4 left right up + "\xe2\x94\x82", // 8 down + "\xe2\x94\x90", // 1 8 left down + "\xe2\x94\x8c", // 2 8 right down + "\xe2\x94\xac", // 1 2 8 left right down + "\xe2\x94\x82", // 4 8 up down + "\xe2\x94\xa4", // 1 4 8 left up down + "\xe2\x94\x9c", // 2 4 8 right up down + "\xE2\x94\xBC", // 1 2 4 8 left right up down + "" + }; + /* +// Double lines + ' ', // 0 blank + '\xcd', // 1 left + '\xcd', // 2 right + '\xcd', // 1 2 left right + '\xba', // 4 up + '\xbc', // 1 4 left up + '\xc8', // 2 4 right up + '\xca', // 1 2 4 left right up + '\xba', // 8 down + '\xbb', // 1 8 left down + '\xc9', // 2 8 right down + '\xcb', // 1 2 8 left right down + '\xba', // 4 8 up down + '\xb9', // 1 4 8 left up down + '\xcc', // 2 4 8 right up down + '\xce', // 1 2 4 8 left right up down + '\0' + */ + + for( int y = 0; y < h; y++ ) + { + for( int x = 0; x < w; x++ ) + { + if( buf[x+y*w] == '+' ) + { + int iJunct = 0; + if( x > 0 && buf[(x-1)+y*w] == '-' ) + iJunct |= 1; + if( x < w-1 && buf[(x+1)+y*w] == '-' ) + iJunct |= 2; + if( y > 0 && buf[x+(y-1)*w] == '|' ) + iJunct |= 4; + if( y < h-1 && buf[x+(y+1)*w] == '|' ) + iJunct |= 8; + printf(cJunct[iJunct]); + } + else + { + switch( buf[x+y*w] ) + { + case '-': + printf("\xe2\x94\x80"); + break; + + case '|': + printf("\xe2\x94\x82"); + break; + + case '^': + printf("\xe2\x86\x91"); + break; + + case 'v': + printf("\xe2\x86\x93"); + break; + + default: + printf("%c", buf[x+y*w]); + break; + } + } + } + } +} + +void RenderAscii::render() +{ + static char cPassages[] = {"~~~~^v<>/*aAbBcCdDeE"}; + + printf("\xef\xbb\xbf"); + + 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 )+2); + int iBufHeight = (iSize+1)*rMap.getSize( 1 )+1; + int iBufSize = iBufWidth*iBufHeight+1; + + char *buf = new char[iBufSize]; + memset( buf, '\n', iBufSize ); + buf[iBufSize-1] = '\0'; + + 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++ ) + { + char &c = buf[x+iCol+(y+iRow)*iBufWidth]; + if( (iRow == 0 || iRow == iSize+1) && + (iCol == 0 || iCol == iSize+1) ) + c = '+'; + else if( iRow == 0 && (iWalls&(1<<2)) == 0 ) + c = '-'; + else if( iRow == iSize+1 && (iWalls&(1<<3)) == 0 ) + c = '-'; + else if( iCol == 0 && (iWalls&(1<<0)) == 0 ) + c = '|'; + else if( iCol == iSize+1 && (iWalls&(1<<1)) == 0 ) + c = '|'; + else + c = ' '; + } + } + + for( int j = 2; j < iDims; j++ ) + { + for( int side = 0; side < 2; side++ ) + { + int d = j*2+side; + if( iWalls&(1< 2 ) + { + printf("Floor: (%d", rMap.getSize(2)-p[2]); + for( int j = 3; j < iDims; j++ ) + { + printf(", %d", rMap.getSize(j)-p[j] ); + } + printf(")\n"); + } + + postProcess( buf, iBufWidth, iBufHeight ); +// printf(buf); + memset( buf, '\n', iBufSize ); + buf[iBufSize-1] = '\0'; + } + } + /* + printf("[%d", p[0]); + for( int j = 1; j < iDims; j++ ) + { + printf(", %d", p[j] ); + } + printf("]\n"); + */ + if( iDim == iDims ) + break; + } +// printf(buf); + + delete[] buf; +} +/* + int iSizeX = rMap.getSize( 0 ); + int iSizeY = 1; + if( iDims >= 2 ) + iSizeY = rMap.getSize( 1 ); + + for( int y = 0; y < iSizeY; y++ ) + { + for( int iRow = (y==0)?0:1; iRow < iSize+2; iRow++ ) + { + for( int x = 0; x < iSizeX; x++ ) + { + int iWalls = rMap[Position(2, x, y)].iWalls; + for( int iCol = (x==0)?0:1; iCol < iSize+2; iCol++ ) + { + if( (iRow == 0 || iRow == iSize+1 ) && + (iCol == 0 || iCol == iSize+1 )) + { + printf("+"); + } + else if( iRow == 0 ) + { + if( (iWalls&(1<<2)) == 0 ) + printf("-"); + else + printf(" "); + } + else if( iRow == iSize+1 ) + { + if( (iWalls&(1<<3)) == 0 ) + printf("-"); + else + printf(" "); + } + else if( iCol == 0 ) + { + if( (iWalls&(1<<0)) == 0 ) + printf("|"); + else + printf(" "); + } + else if( iCol == iSize+1 ) + { + if( (iWalls&(1<<1)) == 0 ) + printf("|"); + else + printf(" "); + } + else + printf(" "); + } + } + printf("\n"); + } + } +} +*/ -- cgit v1.2.3