Simple C++ Starfield Animation Based on Codepage 437


My second son, Ryan was born today 9/Feb/2014, so this is a gift for him.

The single binary executable on Windows platform can be downloaded here [zipped (168KB)] Simple C++ Starfield Animation windows executable download Simple C++ Starfield Animation Based on Codepage 437 32 bit algorithms beginner c / c++ code code library implementation programming languages Win32 API .

The file was built on windows/codeblocks/g++ compiler and the animation is pretty simple made by a few lines C++ source code, which works on Codepage 437.

It is a vertically-oriented starfield animation: randomly stars (actually squares) flowing upwards (so the object, spaceship is moving downwards fast)

starfield Simple C++ Starfield Animation Based on Codepage 437 32 bit algorithms beginner c / c++ code code library implementation programming languages Win32 API

The C++ source code is pretty self-explanatory.

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
/* http://HelloACM.com */
#include <iostream>  // cout
#include <windows.h>  // SetConsoleOutputCP etc
#include <time.h>  // time
#include <unistd.h> // usleep
 
using namespace std;
 
const unsigned char BLACK = 219;
 
int main()
{
    /* initialize random seed: */
    srand (time(NULL));
    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;
    }
    /* jump to bottom */
    for (int i = 0; i < 50; i ++) cout << endl;
    char *row = new char[width + 1];
    row[width] = '\0'; // terminate the string
    const int N = 5;
    for (;;) {
        /* empty the row */
        memset(row, 32, sizeof(char) * width);
        for (int i = 0; i < N; i ++) {
            /* randomly put squares */
            row[(int)(rand() % width)] = BLACK;
        }
        cout << row;
        usleep(20000); // adjust the animation speed
    }
    return 0;
}
/* http://HelloACM.com */
#include <iostream>  // cout
#include <windows.h>  // SetConsoleOutputCP etc
#include <time.h>  // time
#include <unistd.h> // usleep

using namespace std;

const unsigned char BLACK = 219;

int main()
{
    /* initialize random seed: */
    srand (time(NULL));
    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;
    }
    /* jump to bottom */
    for (int i = 0; i < 50; i ++) cout << endl;
    char *row = new char[width + 1];
    row[width] = '\0'; // terminate the string
    const int N = 5;
    for (;;) {
        /* empty the row */
        memset(row, 32, sizeof(char) * width);
        for (int i = 0; i < N; i ++) {
            /* randomly put squares */
            row[(int)(rand() % width)] = BLACK;
        }
        cout << row;
        usleep(20000); // adjust the animation speed
    }
    return 0;
}

We get the number of characters per row in the 437 code page mode, create a row vector (length + 1), the last character being ‘\0’ which terminates the string, every row by row, we empty the row vector (ASCII 32 is the white space) and randomly put the squares… adjust the animation speed by changing the time interval. usleep can be replaced by Sleep in windows.h i.e. Sleep(20)

Continuously print rows by rows forever will scroll the screen upwards and make it an animation!

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
448 words
Last Post: Simple C/C++ Rocket Animation
Next Post: Simple C++ TV Screen Emulation

The Permanent URL is: Simple C++ Starfield Animation Based on Codepage 437

Leave a Reply