Jump to content

Recommended Posts

Posted

[C++] The "Matrix" in C++ (Win32 Console)

 

 

 

main.cpp

(cpp):
#include <iostream>
#include <vector>

#include "Matrix.h"

int main() {

    RemoveCursor();

    // Set the console title and double the consoles height
    #if (_WIN32_WINNT == _WIN32_WINNT_WINXP)
        if (!SetConsoleTitle("Matrix - Win32 Console")) {
            std::cout << "SetConsoleTitle returned an error: " << GetLastError();
        }

        SMALL_RECT windowSize = { 0, 0, 79, 49 };
        if (!SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), TRUE, &windowSize)) {
            std::cout << "SetConsoleWindowInfo returned an error: " << GetLastError();
        }
    #else
        // Windows Vista/7 have disabled FULL SCREEN
        ShowWindow(GetConsoleWindow(), SW_MAXIMIZE);
    #endif


    std::vector<Matrix>matrix;
/*
    matrix.push_back(Matrix(0, 0, 5, 15));
    matrix.push_back(Matrix(65, 0, 5, 15));
    matrix.push_back(Matrix(0, 0, 5, 15));
    matrix.push_back(Matrix(65, 0, 5, 15));

    matrix.push_back(Matrix(0, 0, 5, 15));  matrix.back().setErase(true);
    matrix.push_back(Matrix(65, 0, 5, 15)); matrix.back().setErase(true);
    matrix.push_back(Matrix(0, 0, 5, 15));  matrix.back().setErase(true);
    matrix.push_back(Matrix(65, 0, 5, 15)); matrix.back().setErase(true);
*/

    for (int y = 0; y < 15; y++) {
        matrix.push_back(Matrix());
    }

    for (int z = 0; z < 5; z++) {
        matrix.push_back(Matrix()); matrix.at(z).setErase(true);
    }

    while (1) {
        Sleep(1);

        for (int x = 0; x < matrix.size(); x++) {
            matrix.at(x).display();
        }
    }

    return 0;
}

 

Matrix.cpp

 

(cpp):
#include "Matrix.h"

using std::cout;

Matrix::Matrix() {

    // The initial seed value of the object
    srand(static_cast<unsigned int>(getpid()) ^ static_cast<unsigned int>(clock()) ^ static_cast<unsigned int>(time(NULL)));

    // Default constructor (no arguments)
    setDefault(true);

    // Default state for 'matrix lines'
    setErase(false);

    // 80 wide, 25 tall
    setPosition((rand() % 80 + 0), (rand() % 22 + 0));

    // Based on position Y
    setLength((rand() % (getPosition().Y + 1) + 0));

    // Speed of each letter being displayed
    setSpeed((rand() % 50 + 50), (rand() % 100 + 100), (rand() % 150 + 150));

    // Time between each iteration of a new color
    loopWhite_ = GetTickCount();
    loopLgreen_ = GetTickCount();
    loopDgreen_ = GetTickCount();
    lenCnt_ = 0;
};

Matrix::Matrix(const SMALL_RECT box) {

    // The initial seed value of the object
    srand(static_cast<unsigned int>(getpid()) ^ static_cast<unsigned int>(clock()) ^ static_cast<unsigned int>(time(NULL)));

    // Copy the argument to class member attribute
    setMatrixBox(box);

    // Default constructor (no arguments)
    setDefault(false);

    // Default state for 'matrix lines'
    setErase(false);

    // 80 wide, 25 tall
    setPosition((rand() % getMatrixBox().Right + getMatrixBox().Left), (rand() % getMatrixBox().Bottom + getMatrixBox().Top));

    // Based on position Y
    setLength((rand() % (getPosition().Y + 1) + 0));

    // Speed of each letter being displayed
    setSpeed((rand() % 50 + 50), (rand() % 100 + 100), (rand() % 150 + 150));

    // Time between each iteration of a new color
    loopWhite_ = GetTickCount();
    loopLgreen_ = GetTickCount();
    loopDgreen_ = GetTickCount();
    lenCnt_ = 0;
};

Matrix::Matrix(const unsigned __int8 left, const unsigned __int8 top,
               const unsigned __int8 bottom, const unsigned __int8 right) {

    // The initial seed value of the object
    srand(static_cast<unsigned int>(getpid()) ^ static_cast<unsigned int>(clock()) ^ static_cast<unsigned int>(time(NULL)));

    // Copy the argument to class member attribute
    setMatrixBox(top, bottom, left, right);

    // Default constructor (no arguments)
    setDefault(false);

    // Default state for 'matrix lines'
    setErase(false);

    // 80 wide, 25 tall
    setPosition((rand() % getMatrixBox().Right + getMatrixBox().Left), (rand() % getMatrixBox().Bottom + getMatrixBox().Top));

    // Based on position Y
    setLength((rand() % (getPosition().Y + 1) + 0));

    // Speed of each letter being displayed
    setSpeed((rand() % 50 + 50), (rand() % 100 + 100), (rand() % 150 + 150));

    // Time between each iteration of a new color
    loopWhite_ = GetTickCount();
    loopLgreen_ = GetTickCount();
    loopDgreen_ = GetTickCount();
    lenCnt_ = 0;
};

void Matrix::randLength() {
    setLength((rand() % (getPosition().Y + 1) + 5));
}

void Matrix::randSpeed() {
    setSpeed((rand() % 50 + 50), (rand() % 100 + 100), (rand() % 150 + 150));
    //setSpeed(0);
}

 

 

Matrix.h

(cpp):
#pragma once

#include <time.h>
#include <process.h>
#include <iostream>

#include "TextControl.h"

class Matrix {
    private:
        bool isDefault_; // Flag to signal using default "0 to 80" width, and "0 to 25" height
        bool erase_; // Flag to signal erasing of text (black matrix lines)

        // Timers for each color (no alternative)
        unsigned __int32 loopWhite_;
        unsigned __int32 loopLgreen_;
        unsigned __int32 loopDgreen_;

        unsigned __int16 speed_[3]; // Delay between printing a new line
        unsigned __int8 length_; // Max. length before it quits
        unsigned __int8 lenCnt_; // Counter for length

        COORD position_; // Position on the console

        SMALL_RECT matrixBox_; // Box for the "Matrix" to be in

    public:
        Matrix::Matrix();
        Matrix::Matrix(const SMALL_RECT);
        Matrix::Matrix(const unsigned __int8, const unsigned __int8, const unsigned __int8, const unsigned __int8);

        void display();

        void setErase(const bool);
        bool getErase();

        void setSpeed(const unsigned __int16, const unsigned __int16, const unsigned __int16);
        unsigned __int16 getSpeed(const unsigned __int8);

    private:
        void randLength();
        void randSpeed();
        void randPos();

        void setDefault(const bool);
        bool getDefault();

        void setLength(const unsigned __int8);
        unsigned __int8 getLength();

        void setPosition(const COORD);
        void setPosition(const unsigned __int8, const unsigned __int8);
        COORD getPosition();

        void setMatrixBox(const SMALL_RECT);
        void setMatrixBox(const unsigned __int8, const unsigned __int8, const unsigned __int8, const unsigned __int8);
        SMALL_RECT getMatrixBox();
};

 

TextControl.cpp

(cpp):
#include "TextControl.h"

void RemoveCursor() {

    /* Remove the cursor (does not work in full screen) */
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO CursoInfo;
    CursoInfo.dwSize = 1;         /* The size of caret */
    CursoInfo.bVisible = false;   /* Caret is visible? */
    SetConsoleCursorInfo(hConsole, &CursoInfo);

    return;
}

void SetColor(const int foreground) {

    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hConsole, foreground);

    return;
}

void PlaceCursor(const int x, const int y) {

    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    COORD PlaceCursorHere;
    PlaceCursorHere.X = x;
    PlaceCursorHere.Y = y;

    SetConsoleCursorPosition(hConsole, PlaceCursorHere);
    return;
}

 

 

TextControl.h

(cpp):
#pragma once

#include <windows.h>

enum COLORS {
    BLACK       = 0,
    DARK_BLUE   = 1,
    DARK_GREEN  = 2,
    TEAL        = 3,
    DARK_RED    = 4,
    DARK_PURPLE = 5,
    GOLD        = 6,
    GREY        = 7,
    DARK_WHITE  = 8,
    BLUE        = 9,
    GREEN       = 10,
    CYAN        = 11,
    RED         = 12,
    PURPLE      = 13,
    YELLOW      = 14,
    WHITE       = 15
};

void RemoveCursor();
void SetColor(const int);
void PlaceCursor(const int, const int);

 

 

 

by Lightness

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×
×
  • Create New...

Important Information

This community uses essential cookies to function properly. Non-essential cookies and third-party services are used only with your consent. Read our Privacy Policy and We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue..