Simple C++ TV Screen Emulation


If you are older enough (born in 1970’s or 80’s) you must be familiar with the following picture, which is what it looks like when TV has paused the program. It has been so well known that many use this screen as a 404 (not found) indicator page in web pages.

tv Simple C++ TV Screen Emulation

The source code is compiled at codeblocks/g++ windows and the zipped executable (168kb) can be downloaded [here] cnt Simple C++ TV Screen Emulation.

Nothing complex here, just print a group of colour squares line by line. The colour index value is from 0 to 15 using SetConsoleTextAttribute.

/* http://HelloACM.com */
#include <iostream>
#include <windows.h>
#include <unistd.h>

using namespace std;

const unsigned char BLACK = 219;

int main()
{
    SetConsoleOutputCP(437);
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    int ret;
    /* get the width of the console */
    ret = GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
    int width = 80;
    if (ret) {
        width = csbi.dwSize.X;
    }
    const int COLORS = 10;
    int t = width / COLORS;
    /* get the number of rows in console */
    int lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
    for (int i = 0; i < lines; i ++) { // row
        for (int j = 1; j < COLORS; j ++) { // color group
            /* set color */
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), j);
            for (int k = 0; k < t; k ++) { // print t squares
                cout << BLACK;
            }
        }
        cout << endl;
    }
    for (;;) usleep(10000);
    return 0;
}

The javascript version can be found here and the x86 16-bit assembly version can be found here.

We use csbi.srWindow.Bottom – csbi.srWindow.Top + 1 to get the number of rows in console. The csbi.dwSize.Y returns the pixels instead.

–EOF (The Ultimate Computing & Technology Blog) —

391 words
Last Post: Simple C++ Starfield Animation Based on Codepage 437
Next Post: Modern gotoxy alternative C++

The Permanent URL is: Simple C++ TV Screen Emulation (AMP Version)

Leave a Reply