Writing to the Console Buffer

Hi,

My C++ skills are improving but I want to write a fair bit of general code before branching into GUI.
Unfortunately, there is only so much you can do by writing text to a line-by-line printing terminal.

I have seen that it is possible to address the screen buffer like an array (thanks to Javidx9's YouTube videos) but I can't seem to find any solid tutorials or example code that I can play with.

Can anyone point me to a good tutorial with code to start me off?

Many thanks.
Last edited on
ncurses or windows' "gotoxy" functions can do this to some extent if you want an easy answer. But this is 'unusual' and not necessary. I think you have a misconception about what you can do and learn without doing oddball output stuff.
- you can learn OOP fully with normal cin/cout
- you can learn all about the c++ containers with cin/cout
- you can do algorithms/data structures with cin cout...
and so on. there is very little, apart from making a text based game, that can't be done easily with just cin/cout and in the field you are unlikely to be writing fancy console programs. Its fun to play with, but its not a critical learning path, in other words.


@againtry: Yes, OneLoneCoder (Javidx9) is who I was talking about in my OP and he only points to his video on FPS which gives a very quick word about it.
I've found his code and tried to play with it but I'm not sure I really get it as well as I thought I would.

@jonnin - Yes, my initial coding years ago at university in C was all to terminal (we used dumb terminals to a Unix mainframe) but I have a few ideas that I'd like to try nonetheless whilst building up my C++ skills.

I have trimmed out the code from Javidx9's FPS game and tried to simply set array content as things to write to the screen buffer but it didn't work.

Any pointers and explanation of how it works would be appreciated.

Many thanks.
he has a wiki tutorial? It has a tiny example.
https://github.com/OneLoneCoder/olcPixelGameEngine/wiki
Last edited on
Beyond looking at one of javid's videos and finding his website + his github stuff I have no knowledge of this and certainly wouldn't endorse it to either learn anything about C++, upskill from C (albeit years ago) or anything whatsoever to do with graphics of any kind.

Qt and/or VStudio cover all of that admirably.

There's absolutely nothing wrong with picking up from C (albeit years ago) but the experience of many people here has shown that the best shot is to use the excellent tutorials here on this site or https://www.learncpp.com/ among others and get up to speed that way. To a large extent celebrate the history but forget about it

Your choice :)


I've found his code and tried to play with it but I'm not sure I really get it as well as I thought I would.

What have you tried? (code please)
What problems do you have?
What do you want to do?
Hi,

Below is the heavily edited code that I got from OLC's github site.
The intended effect is to have some double-vertical line ASCII characters down the left hand side of the screen.
It doesn't work.
Also, I don't understand exactly how the code works,

Any tips would be appreciated.

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

// ScreenBuffer.cpp - Testing out the Screen Buffer and writing to it.

#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <Windows.h>

using namespace std;

int nScreenWidth = 120;			// Console Screen Size X (columns)
int nScreenHeight = 40;			// Console Screen Size Y (rows)
char c;

int main()
{
	// Create Screen Buffer
	wchar_t* screen = new wchar_t[nScreenWidth * nScreenHeight];
	HANDLE hConsole = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
	SetConsoleActiveScreenBuffer(hConsole);
	DWORD dwBytesWritten = 0;

	while (c=_getch() != 'x')
	{
		// Display Frame
		screen[0] = '\201';
		screen[120] = '\186';
		screen[240] = '\186';
		screen[nScreenWidth * nScreenHeight - 1] = '\0';
		WriteConsoleOutputCharacter(hConsole, screen, nScreenWidth * nScreenHeight, { 0,0 }, &dwBytesWritten);
	}
	return 0;
}
 
Last edited on
This might get you an idea.
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
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <Windows.h>
#include <algorithm>

using namespace std;

int nScreenWidth = 120;			// Console Screen Size X (columns)
int nScreenHeight = 40;			// Console Screen Size Y (rows)
char c;

void write(wchar_t *buffer, int row, int col, wchar_t ch)
{
	buffer[row*nScreenWidth + col] = ch;
}

void write(wchar_t *buffer, int row, int col, const std::wstring& s)
{
	swprintf(&buffer[row * nScreenWidth + col], s.size()+1,  L"%s", s.c_str());
}
int main()
{
	// Create Screen Buffer
	wchar_t* screen = new wchar_t[nScreenWidth * nScreenHeight];
	std::fill_n(screen, nScreenHeight * nScreenWidth, L' ');

	HANDLE hConsole = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
	if (hConsole == INVALID_HANDLE_VALUE)
	{
		std::cerr << "CreateConsoleScreenBuffer win error: " << GetLastError() << '\n';
		return EXIT_FAILURE;
	}
	if (!SetConsoleActiveScreenBuffer(hConsole))
	{
		std::cerr << "CreateConsoleScreenBuffer win error: " << GetLastError() << '\n';
		return EXIT_FAILURE;
	}
	DWORD dwBytesWritten = 0;

	write(screen, 0, 0, L'╔');
	for (int row = 1; row < nScreenHeight -1; ++row)
		write(screen, row, 0, L'║');

	write(screen, 1, 2, L"Hello world");

	if (WriteConsoleOutputCharacter(hConsole, screen, nScreenWidth * nScreenHeight, { 0,0 }, &dwBytesWritten) == 0)
	{
		std::cerr << "CreateConsoleScreenBuffer win error: " << GetLastError() << '\n';
		return EXIT_FAILURE;
	}
	
	_getch();
}


Why don't you use the whole GameEngine instead of trying to get some parts?
Many thanks.

I'll give this a run and go through it.


Why don't you use the whole GameEngine instead of trying to get some parts?


Baby steps.
First I wanted to understand the screen buffer part.
At this time, the whole game engine isn't so much useful to me.

Maybe once I'm adept at this, I'll move on to the rest.

Everyone learns differently, I guess.

Topic archived. No new replies allowed.