I’ve been blogging a lot on a different account on a different topic. I wanted to create a new blog, and I knew I had one from my student days, but I didn’t have the student email address anymore, so I sort of had given up on logging into  this. But now that issue has been resolved.

Since I’m currently employed, and a lot of the things we do at work is classified, except for the technology used, I’m not going to talk a lot about that. Instead I want to say more about private projects that I have going on.

Current stuff

I must admit I haven’t done much  in terms of programming outside of work. Work, kids and social life has taken most of the time away. But now and again, whenever I start doing stuff, I’ll post it here if I find it worth posting about.

Yesterday I did something that I have been wanting to do for a while. It’s not really a Matrix effect, but it’s sort of a Matrix like effect. I know that this is easy for people to do. It’s not really tricky, but I thought it was cool that I finally figured out at least one way of doing this.

Moving forward I want to test out if I can create a snake like dos game. It’s a long time away, but I think it would be cool to just test it out, just for the heck of it. In any case, what I did now was just a simple Matrixish thing.

Captured with gifcam (http://blog.bahraniapps.com/gifcam/ )

test4.gif

I know the matrix effect is more 1 and 1 character falling down the screen, but it’s not really about that. When I first was a student the teacher was making the same triangle shapes as the rest of the class. The difference was that they were updating with different random letters. So he found a way to modify the data written on the screen.

I forgot to ask how it was done, but I’ve been thinking of how. I mean all you really need to do is to figure out where in memory it is stored, then change the character. Right now however I haven’t done that and all I do is changing the location of the input and adding a new line on top and pushing the rest down and writing the line over.

The next version I want to get the pointer to the input in memory and changing it directly, if that is possible.

This was also just done to quickly “prototype it” so the code isn’t as much. I’ll post it, critique it if you wish. It looks horrible, but I need to have the paid version in order to install a plugin and I don’t want to do that.

The code

#include “pch.h”
#include <iostream>
#include <Windows.h>
#include <ProcessEnv.h>
#include <time.h>
#include <vector>
#include <string>

HANDLE hStdOut;
DWORD fdwSaveOldMode;

void ErrorExit(std::string lpszMessage)
{
fprintf(stderr, “%s\n”, lpszMessage);

// Restore input mode on exit.

SetConsoleMode(hStdOut, fdwSaveOldMode);

ExitProcess(0);
}
char getRandomCharacter(short min, short max) {
int randomAdd = rand() % (max – min);
char character = (min + randomAdd);
return character;
}

std::vector <char> createRandomCharactersOnConsoleLine(short lineLength) {
std::vector<char> line;
for (int i = 0; i < lineLength; i++) {
if (rand() % 100 > 50) {
line.push_back(‘ ‘);
}
else {
char randomChar = getRandomCharacter(35, 125);
line.push_back(randomChar);
}
}
return line;
}

int main()
{
srand(time(NULL));
hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hStdOut == INVALID_HANDLE_VALUE) {
ErrorExit(“GetStdHandle”);
}

char min = 35;
short max = 125;
COORD largestXY = GetLargestConsoleWindowSize(hStdOut); // window size

// it was not accurate, had to reduce the X and Y.
largestXY.Y -= 33;
std::vector<std::vector<char>> characterMatrix;
SetConsoleTextAttribute(hStdOut, 0x0A); // color

short yCoord = 0;
short x = floor(largestXY.X / 2);
COORD placement;
placement.X = 0;
placement.Y = 0;
for (int i = 0; i < largestXY.Y; i++) {
std::vector<char> charLine;
for (int j = 0; j < largestXY.X; j++) {

charLine.push_back(0);

}
characterMatrix.push_back(charLine);
}

while (true) {
std::vector<char> line = createRandomCharactersOnConsoleLine(floor(largestXY.X/2));

placement.X = 0;
placement.Y = 0;
SetConsoleCursorPosition(hStdOut, placement);
for (unsigned int i = characterMatrix.size() -1; i > 0; i–) {
if (i – 1 > 0) {
characterMatrix[i] = characterMatrix[i – 1];
}
else {
characterMatrix[i] = line;
}
}

for (unsigned int i = 0; i < characterMatrix.size(); i++) {
for (unsigned int j = 0; j < characterMatrix[i].size(); j++) {
std::cout << characterMatrix[i][j];
}
}
}

return 0;

}