Permalink
Browse files

use WriteConsoleOutput to render text. Writer interprets simple comma…

…nds directly in string
  • Loading branch information...
Tommislav committed Apr 19, 2018
1 parent 0c4bc17 commit 1110c6c53b7abb51c5c5a34364d7f450046d8c35
Showing with 45 additions and 35 deletions.
  1. +4 −12 game.cpp
  2. +15 −20 main_win.cpp
  3. +26 −3 writer.cpp
View
@@ -40,28 +40,20 @@ std::string readFromFile(std::string fileName) {
std::string str1 = "I know I said that if I lived to 100 I'd not regret what happened last night. But I woke up this morning and a century had passed. Sorry. -Geoff Dyer";
std::string str2 = "What scares me?#n Oh, I thought you'd never ask.#n Reality scares me.#n I'm afraid of the possibility that our conversation isn't real.#n I'm afraid of the possibility that all of my friends and relatives, and even my girlfriend aren't real. I'm afraid of the possibility that this world isn't real.#n Or that I'm not real.#n Just your reflection.";
std::string str2 = "What scares me?#n #p2Oh, I thought you'd never ask.#n Reality scares me.#n #p2#f0I'm afraid of the possibility that our conversation isn't real.#n I'm afraid of the possibility that all of my friends and relatives, and even my girlfriend aren't real#p2#f3......#p2#f1 I'm afraid of the possibility that this world isn't real.#n Or that I'm not real.#n Just your reflection.#p3#p3#p3 ";
std::string str3 = "Line 1#nLine2#nLine3";
void test(){
clearScreen();
setCursorPos(0,0);
print(str2);
print(str3);
flushScreen();
}
void initGame() {
wrSetText(str2);
/*
sleepMs = 50;
cnt = 0;
waitingForInput = false;
data = readFromFile("data");
gameIsRunning = true;
//std::cout << data << std::endl;
*/
}
bool tick(int millisec) {
View
@@ -12,6 +12,7 @@ const int MAX_WIDTH = 400;
const int MAX_HEIGHT = 40;
const int BUFF_SIZE = MAX_WIDTH * MAX_HEIGHT;
char textBuffer[BUFF_SIZE] = {};
CHAR_INFO outputBuffer[BUFF_SIZE] = {};
termSize currSize;
int currX;
int currY;
@@ -72,6 +73,9 @@ void print(string str) {
int i = currY * currSize.width + currX;
int len = str.length();
for (int j=0; j<len; j++) {
//outputBuffer[i+j].Char.AsciiChar = str[j];
outputBuffer[i+j].Char.UnicodeChar = str[j];
outputBuffer[i+j].Attributes = 8;
textBuffer[i+j] = str[j];
}
}
@@ -84,6 +88,9 @@ void print(char c) {
currY++;
}
textBuffer[i] = c;
//outputBuffer[i].Char.AsciiChar = c;
outputBuffer[i].Char.UnicodeChar = c;
outputBuffer[i].Attributes = 1 | 2 | 4;
}
void setColor(Color col) {
@@ -92,40 +99,28 @@ void setColor(Color col) {
LARGE_INTEGER freq;
void flushScreen() {
// http://cecilsunkure.blogspot.se/2011/11/windows-console-game-writing-to-console.html
LARGE_INTEGER start;
QueryPerformanceCounter(&start);
termSize size = getTermSize();
for (SHORT y=0; y<size.height; y++) {
COORD pos = {0, y};
SetConsoleCursorPosition(hOut, pos);
char line[BUFF_SIZE] = {0};
for (int x=0; x<size.width; x++) {
int i = y * size.width + x;
line[x] = textBuffer[i] == 0 ? ' ' : textBuffer[i];
}
// write line
DWORD charsWritten = 0;
bool success = WriteConsole(hOut, &line, size.width, &charsWritten, NULL);
if (!success) {
pos = {0,0};
SetConsoleCursorPosition(hOut, pos);
std::cout << "ERROR WRITING TO OUTPUT BUFFER: " << GetLastError() << std::endl;
}
}
COORD buffSize = {(SHORT)size.width,(SHORT)size.height};
COORD buffPos = {0,0};
SMALL_RECT smallRect = {0,0,(SHORT)size.width,(SHORT)size.height};
WriteConsoleOutput(hOut, &outputBuffer[0], buffSize, buffPos, &smallRect);
COORD pos = {0, 0};
COORD pos = {(SHORT)currX, (SHORT)currY};
SetConsoleCursorPosition(hOut, pos);
LARGE_INTEGER end;
QueryPerformanceCounter(&end);
std::cout << (1000*(end.QuadPart - start.QuadPart))/freq.QuadPart << " " << std::flush;
//std::cout << (1000*(end.QuadPart - start.QuadPart))/freq.QuadPart << " " << std::flush;
}
int main() {
sleep(5000);
// https://docs.microsoft.com/en-us/windows/console/writeconsole
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
hIn = GetStdHandle(STD_INPUT_HANDLE);
View
@@ -7,8 +7,9 @@ using namespace std;
string wrText = "";
int wrPos = 0;
int wrLen = 0;
int wrPause = 30;
int wrWait = 30;
int wrCounter = 0;
int wrPause = 0;
void wrSetText(string text) {
wrText = text;
@@ -29,6 +30,19 @@ void wrParseCommand(char command, char instr) {
coord c = getCursorPos();
setCursorPos(0, c.y + 1);
}
if (command == 'p') { // pause
if (instr == '0') { wrPause = 100; }
else if (instr == '1') { wrPause = 500; }
else if (instr == '2') { wrPause = 1000; }
else if (instr == '3') { wrPause = 1500; }
}
if (command == 'f') { // framerate
if (instr == '0') { wrWait = 10; }
else if (instr == '1') { wrWait = 30; }
else if (instr == '2') { wrWait = 50; }
else if (instr == '3') { wrWait = 150; }
}
}
@@ -37,18 +51,27 @@ void wrParseCommand(char command, char instr) {
bool wrPutChar(int millisec) {
if (wrAtEnd()) {return true;}
if (wrPause > 0) {
wrPause -= millisec;
if (wrPause > 0) {
return false;
}
}
wrCounter += millisec;
if (wrCounter < wrPause) {
if (wrCounter < wrWait) {
return false;
}
wrCounter -= wrPause;
wrCounter -= wrWait;
if (wrCounter < 0) {wrCounter = 0;}
char c = wrText[wrPos];
if (c == '^') {
wrParseCommand('n', ' ');
wrPos++;
return wrPutChar(millisec);
}
else if (c == '#') {
wrParseCommand(wrText[wrPos+1], wrText[wrPos+2]);

0 comments on commit 1110c6c

Please sign in to comment.