1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
#include <iostream>
#include "ASCIIRenderer.h"
#include <windows.h>
#include <assert.h>
using namespace std;
HANDLE m_hConsole;
void ASCIIRenderer::GetConsoleHandle()
{
m_hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
}
void ASCIIRenderer::SetScreenSize()
{
COORD MaxWindowSize = GetLargestConsoleWindowSize(m_hConsole);
int targetWidth = min(MaxWindowSize.X, SCREEN_WIDTH);
int targetHeight = min(MaxWindowSize.Y, SCREEN_HEIGHT);
SMALL_RECT rect = { 0, 0, targetWidth - 1, targetHeight - 1 };
COORD coord = { targetWidth, targetHeight };
bool bufferSizeSet = SetConsoleScreenBufferSize(m_hConsole, coord);
bool windowInfoSet = SetConsoleWindowInfo(m_hConsole, TRUE, &rect);
}
void ASCIIRenderer::InitialiseScreenBuffer(CHAR asciiChar, WORD attributes)
{
for (int i = 0; i < SCREEN_WIDTH; i++)
{
for (int j = 0; j < SCREEN_HEIGHT; j++)
{
m_ScreenBuffer[SCREEN_WIDTH * j + i].Char.AsciiChar = asciiChar;
m_ScreenBuffer[SCREEN_WIDTH * j + i].Attributes = attributes;
}
}
}
void ASCIIRenderer::Initialise()
{
GetConsoleHandle();
SetScreenSize();
InitialiseScreenBuffer();
SetConsoleTitle("4107COMP PONGOUT");
}
void ASCIIRenderer::RenderBufferToConsole()
{
COORD coord = { SCREEN_WIDTH, SCREEN_HEIGHT };
COORD coord2 = { 0, 0 };
SMALL_RECT write = { 0, 0, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1 };
WriteConsoleOutput(m_hConsole, (const CHAR_INFO *)(&m_ScreenBuffer), coord, coord2, &write);
}
void ASCIIRenderer::ClearScreen()
{
InitialiseScreenBuffer();
}
void ASCIIRenderer::FillScreen(CHAR asciiChar, WORD attributes)
{
InitialiseScreenBuffer(asciiChar, attributes);
}
void ASCIIRenderer::SetPixel(int x, int y, CHAR_INFO pixelData)
{
const int MAX_INDEX = SCREEN_HEIGHT * SCREEN_WIDTH - 1;
int index = x + (y*SCREEN_WIDTH);
assert(x >= 0);
assert(y >= 0);
assert(index >= 0 && index <= MAX_INDEX);
m_ScreenBuffer[x + (y*SCREEN_WIDTH)].Char.AsciiChar = pixelData.Char.AsciiChar; //problem occurs when its hits this line
m_ScreenBuffer[x + (y*SCREEN_WIDTH)].Attributes = pixelData.Attributes;
}
ASCIIRenderer::ASCIIRenderer()
{
Initialise();
}
ASCIIRenderer::~ASCIIRenderer()
{
}
|