TapestryEngine

A 2D Platformer Game Engine
Log | Files | Refs

deaf3d04b1dbfd8462cc53f6a7534fc91ec55eaa.svn-base (3508B)


      1 #include "Console.h"
      2 
      3 
      4 Console* gCons = NULL;
      5 
      6 ConsoleStringManager::ConsoleStringManager()
      7 {
      8 	mEndPointer = mText;
      9 	mNextLineIndex = 0;
     10 	memset(mLines, 0, sizeof(mLines));
     11 }
     12 
     13 void ConsoleStringManager::AddLine(char* string, int CharCount)
     14 {
     15 	if (((mEndPointer - mText) + CharCount + 1) > CONSOLE_TEXT_LIMIT)
     16 	{
     17 		mEndPointer = mText;
     18 	}
     19 	
     20 	memcpy(mEndPointer, string, CharCount);
     21 	mEndPointer[CharCount] = '\0';
     22 
     23 	mLines[mNextLineIndex] = mEndPointer;
     24 	mEndPointer = (mEndPointer + CharCount + 1);
     25 	
     26 	mNextLineIndex = mNextLineIndex + 1;
     27 	if (mNextLineIndex == LINE_LIMIT)
     28 	{
     29 		mNextLineIndex = 0;
     30 	}
     31 }
     32 
     33 void ConsoleStringManager::ConsoleLog(char* msg)
     34 {
     35 	while (1)
     36 	{
     37 		char* TermChar = strchr(msg, '\n');
     38 		if (TermChar == NULL) break;
     39 	
     40 		AddLine(msg, (TermChar - msg));
     41 		msg = TermChar + 1;
     42 	}	
     43 }
     44 
     45 void ConsoleStringManager::ConsolePrintf(char* fmt, ...)
     46 {
     47 	char msgf[256];
     48 
     49 	va_list args;
     50 	va_start(args, fmt);
     51 	sprintf(msgf, fmt, args);
     52 	va_end(args);
     53 	
     54 	ConsoleLog(msgf);
     55 }
     56 
     57 Console::Console(char* filename, int ptsize, SDL_Color Text, SDL_Color BG)
     58 {
     59 	mTextRect.w = 0;
     60 	mTextRect.h = 0;
     61 	mTextRect.x = 0;
     62 	mTextRect.y = 0;
     63 
     64 	mLinePosRect.w = 0;
     65 	mLinePosRect.h = 0;
     66 	mLinePosRect.x = 0;
     67 	mLinePosRect.y = 0;
     68 
     69 	mFont = LoadFont(filename, ptsize);
     70 
     71 	mText = Text;
     72 	mBG = BG;
     73 }
     74 
     75 void Console::ConsPrintf(char* fmt, ...)
     76 {
     77 	char msgf[256];
     78 
     79 	va_list args;
     80 	va_start(args, fmt);
     81 	vsprintf(msgf, fmt, args);
     82 	va_end(args);
     83 
     84 	mCSM.ConsoleLog(msgf);
     85 }
     86 
     87 void Console::DrawConsole(SDL_Renderer* ren)
     88 {
     89 
     90 	int PrintIndex = mCSM.GetNextLineIndex();
     91 
     92 	for (int i = 0; i < LINE_LIMIT; i++)
     93 	{
     94 
     95 		int NextLineIndex = (PrintIndex + i);
     96 		if (NextLineIndex >= LINE_LIMIT)
     97 		{
     98 			NextLineIndex = NextLineIndex - LINE_LIMIT;
     99 		}
    100 
    101 		char* line = mCSM.GetLine(NextLineIndex);
    102 
    103 		if (line != NULL)
    104 		{
    105 			const char* error;
    106 
    107 			SDL_Surface* LineSur = TTF_RenderText_Shaded(mFont, line, mText, mBG);
    108 			if (!LineSur)
    109 			{
    110 				error = SDL_GetError();
    111 			}
    112 			SDL_Texture* LineTex = SDL_CreateTextureFromSurface(ren, LineSur);
    113 			if (!LineTex)
    114 			{
    115 				error = SDL_GetError();
    116 			}
    117 			SDL_FreeSurface(LineSur);
    118 			if (SDL_QueryTexture(LineTex, NULL, NULL, &mTextRect.w, &mTextRect.h))
    119 			{
    120 				error = SDL_GetError();
    121 			}
    122 
    123 			mLinePosRect.w = mTextRect.w;
    124 			mLinePosRect.h = mTextRect.h;
    125 			mLinePosRect.x = 0;
    126 			mLinePosRect.y = (mTextRect.h * i);
    127 
    128 			if (SDL_RenderCopy(ren, LineTex, &mTextRect, &mLinePosRect))
    129 			{
    130 				error = SDL_GetError();
    131 			}
    132 			SDL_DestroyTexture(LineTex);
    133 		}
    134 	}
    135 }
    136 
    137 void Console::DrawConsoleReadout(SDL_Renderer* ren, int x, int y)
    138 {
    139 	int PrintIndex = mCSM.GetNextLineIndex();
    140 	for (int i = 0; i < LINE_LIMIT; i++)
    141 	{
    142 		int NextLineIndex = (PrintIndex + i);
    143 		if (NextLineIndex >= LINE_LIMIT)
    144 		{
    145 			NextLineIndex = NextLineIndex - LINE_LIMIT;
    146 		}
    147 
    148 
    149 		char* line = mCSM.GetLine(NextLineIndex);
    150 
    151 		if (line != NULL)
    152 		{
    153 			const char* error;
    154 
    155 			SDL_Surface* LineSur = TTF_RenderText_Shaded(mFont, line, mText, mBG);
    156 			if (!LineSur)
    157 			{
    158 				error = SDL_GetError();
    159 			}
    160 			SDL_Texture* LineTex = SDL_CreateTextureFromSurface(ren, LineSur);
    161 			if (!LineTex)
    162 			{
    163 				error = SDL_GetError();
    164 			}
    165 			SDL_FreeSurface(LineSur);
    166 			if (SDL_QueryTexture(LineTex, NULL, NULL, &mTextRect.w, &mTextRect.h))
    167 			{
    168 				error = SDL_GetError();
    169 			}
    170 
    171 			mLinePosRect.w = mTextRect.w;
    172 			mLinePosRect.h = mTextRect.h;
    173 			mLinePosRect.x = x;
    174 			mLinePosRect.y = y;
    175 
    176 			if (SDL_RenderCopy(ren, LineTex, &mTextRect, &mLinePosRect))
    177 			{
    178 				error = SDL_GetError();
    179 			}
    180 			SDL_DestroyTexture(LineTex);
    181 		}
    182 	}
    183 }