Matrix Effect Main Loop Help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int _tmain(int argc, _TCHAR* argv[])
{
	int j = 7;
	while (true)
	{
		int i = 0;
		// Output a random row of characters
		while (i < 80)
		{
			std::cout << GetChar(j + i*i, 33, 30);
			++i;
		}
		j = (j + 31);
		// Delay
		i = 0;
		while (i < 300000)
		{
			GetChar(1, 1, 1);
			++i;
		}
	}
	std::cin.get();
	return 0;
}


http://www.youtube.com/watch?v=Uihegplp0H4

It's from this tutorial here but Xoax doesn't explain it too well. I've understood his previous tutorials absolutely fine but it's rushed in too quickly to new concepts without building me up to fully understand it.

But yeah to be honest I don't get any of the main block. If you want to see all of the code it's here:
http://xoax.net/cpp/crs/console/lessons/Lesson13/

If someone could explain it to me it would be fantastic.
Last edited on
When you add numbers to a char, it increments the char by the ASCII table, a char might be 70 in the ASCII table. If you add 4 to it, now it'll display char ASCII 74. Getchar function does that. The function modulus is not needed because the operator '%' is here. We get randomness using the getChar function by incrementing the variables. Use a forever for loop. Use sleep() to delay. Use this header to use sleep and color. Use using namespace std; and int main(int nNumberofArgs,char* pszArgs[])
{

}

Revised code:

main.cpp
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
#include <iostream>
#include <cstdio>
#include <cstdlib>

#include "concor.h"

using namespace std;

char GetChar(int iGenerator, char cBase, int iRange)
{
	return (cBase + (iGenerator % iRange));
}

int main(int nNumberofArgs,char* pszArgs[])
{
	char caRow[80];
	int j = 7;
	int k = 2;
	int l = 5;
	int m = 1;
	for(;;)
    {
		int i = 0;
		// Output a random row of characters
		while (i < 80)
        {
			if (caRow[i] != ' ')
            {
				caRow[i] = GetChar(j + i*i, 33, 30);
			}
			cout << caRow[i];
			++i;
		}
		j = (j + 31);
		k = (k + 17);
		l = (l + 47);
		m = (m + 67);
		caRow[j % 80] = '-';
		caRow[k % 80] = ' ';
		caRow[l % 80] = '-';
		caRow[m % 80] = ' ';
		// Delay
		i = 0;
		Sleep(10);
	}
    return 0;
}


concor.h //color and sleep()

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#ifndef _EKU_CONCOL
#define _EKU_CONCOL

#ifndef _INC_WINDOWS
#include <windows.h>
#endif /*_INC_WINDOWS*/

bool textcolorprotect=true;
/*doesn't let textcolor be the same as backgroung color if true*/

enum concol
{
	black=0,
	dark_blue=1,
	dark_green=2,
	dark_aqua,dark_cyan=3,
	dark_red=4,
	dark_purple=5,dark_pink=5,dark_magenta=5,
	dark_yellow=6,
	dark_white=7,
	gray=8,
	blue=9,
	green=10,
	aqua=11,cyan=11,
	red=12,
	purple=13,pink=13,magenta=13,
	yellow=14,
	white=15
};

inline void setcolor(concol textcolor,concol backcolor);
inline void setcolor(int textcolor,int backcolor);
int textcolor();/*returns current text color*/
int backcolor();/*returns current background color*/

#define std_con_out GetStdHandle(STD_OUTPUT_HANDLE)

//-----------------------------------------------------------------------------

int textcolor()
{
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	GetConsoleScreenBufferInfo(std_con_out,&csbi);
	int a=csbi.wAttributes;
	return a%16;
}

int backcolor()
{
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	GetConsoleScreenBufferInfo(std_con_out,&csbi);
	int a=csbi.wAttributes;
	return (a/16)%16;
}

inline void setcolor(concol textcol,concol backcol)
{setcolor(int(textcol),int(backcol));}

inline void setcolor(int textcol,int backcol)
{
	if(textcolorprotect)
	{if((textcol%16)==(backcol%16))textcol++;}
	textcol%=16;backcol%=16;
	unsigned short wAttributes= ((unsigned)backcol<<4)|(unsigned)textcol;
	SetConsoleTextAttribute(std_con_out, wAttributes);
}

#if defined(_INC_OSTREAM)||defined(_IOSTREAM_)
ostream& operator<<(ostream& os,concol c)
{os.flush();setcolor(c,backcolor());return os;}
#endif

#if defined(_INC_ISTREAM)||defined(_IOSTREAM_)
istream& operator>>(istream& is,concol c)
{cout.flush();setcolor(c,backcolor());return is;}
#endif

#endif /*_EKU_CONCOL*/
Thanks. How does caRow[i] = GetChar(j + i*i, 33, 30); work with these declarations?:
1
2
3
4
5
6
7
8
		j = (j + 31);
		k = (k + 17);
		l = (l + 47);
		m = (m + 67);
		caRow[j % 80] = '-';
		caRow[k % 80] = ' ';
		caRow[l % 80] = '-';
		caRow[m % 80] = ' ';


That's the thing that I get stuck on. How does the actual randomness work and how do the random ASCII figures get outputted? When do the spaces occur and when do the - occur? Why is the ++i present (does it print out the values onto the screen one by one in a row format)?
Last edited on
1
2
3
j = (j + 31); // Making new value for j

caRow[j % 80] = '-'; //pretend j = 80, '%' is dividing j by 80 and only              outputting the remainder. caRow[0] (because remainder of 80 / 80 = 0) will now have the char '-' 


 
It should be i++ for the usual incrementing. caRow[0] will be different than caRow[1], or caRow[n] (n being anything). pretend the 'n' is an address for every space in the array. When you first declare an array such as array[256], the array 'array' will have 256 spaces to put your inputs. So, caRow[0] is holding a different char then caRow[1]. When you do caRow[i], and if 'i' was 3, it will translate to caRow[3] and therefore 'cout'ing caRow[3]. i++ is to make the 'i' go up by one.
^
|
|


*facepalm*

epic explanation fail....
Hi DELB,

I really think you need to go back to basics with C++
You've jumped in way too deep before you're understanding the basics; these examples you're trying to understand seem to be too complex.

Having said that, it's great that you're keen to learn and we'll continue to give you all the help we can. I just feel you'll get much more from it if you revisit the basics.

I've just scanned through the 1st page of this tutorial ...

http://www.cprogramming.com/tutorial/lesson1.html

It seems to be covering all the important stuff.

Jim
Topic archived. No new replies allowed.