Dark Bit Factory & Gravity

PROGRAMMING => C / C++ /C# => Topic started by: Rbz on July 17, 2006

Title: ASCII Matrix - C code
Post by: Rbz on July 17, 2006
Here's a little code I did for ASCII effect challenge, that I post here just to make it ease to find for C coders. The code it's so simple that I didn't make any comments.

Compile with Dev-Cpp 4.9.9.2


Code: [Select]
/*
    ASCII Matrix
    Code: Rbraz - 2006
*/

#include <stdio.h>
#include <conio.h>
#include <windows.h>


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

#define MAX_CODE 70
#define MAX_LENGTH 25
typedef struct {
    int   x;
    float y;
    float speed;
    int   length;
    char  ch[MAX_LENGTH];
}
matrixcode;
matrixcode mtxcode[MAX_CODE];


float Random(float min, float max)
{
  return ((float)rand())/RAND_MAX * (max-min) + min;
}

void gotoxy(int x, int y)
{
    HANDLE hConsoleOutput;
    COORD dwCursorPosition;
    dwCursorPosition.X = x;
    dwCursorPosition.Y = y;
    hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hConsoleOutput,dwCursorPosition);
}

void textcolor (int color, int bgcolor)
{
    static int __BACKGROUND = bgcolor;
    static int __FOREGROUND = color;
   
    SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE),
    color + (__BACKGROUND << 4));
}

void initconsole()
{
SetConsoleTitle("ASCII Matrix by Rbraz - Press Alt+Enter for FullScreen");

COORD wh = {80,50};
    SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), wh);
   
SMALL_RECT rect1={1,1,80,50};
    SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), TRUE, &rect1);

    CONSOLE_CURSOR_INFO cursor = {10,FALSE};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor);   
}

void plotchar(char *ch, int x, int y, int color, int bgcolor)
{   
    if( ((x < 79) & (x > 1)) & ((y < 49) & (y > 1)) ) {
        gotoxy(x, y);
        textcolor(color,bgcolor);
        printf(ch);
    }
}

void initMatrix()
{
    int i,j;
   
    for(i=0; i<MAX_CODE; i++) {
        mtxcode[i].x = 1 + rand()%78;
        mtxcode[i].y = (float)(1 + rand()%48);
        mtxcode[i].speed = Random(0.4f, 1.0f);
       
        for(j=0; j<MAX_LENGTH; j++) mtxcode[i].ch[j] = 0;
       
        mtxcode[i].length = rand()%MAX_LENGTH;
       
        for(j=0; j<mtxcode[i].length; j++) {
            mtxcode[i].ch[j] = 33 + rand()%222;
        }
       
    } 
}

void plotMatrix()
{
    int i,j;
    char tmp[2];

    for(i=0; i<2; i++) tmp[i] = 0;
   
    for(i=0; i<MAX_CODE; i++) {
       
        for(j=0; j<mtxcode[i].length+2; j++) {
           
            tmp[0] = 33 + rand()%222;   //mtxcode[i].ch[j];
           
            if(j >= mtxcode[i].length-1) {
                plotchar(" ", mtxcode[i].x, (int)mtxcode[i].y, 0, 0);
                plotchar(tmp, mtxcode[i].x, (int)mtxcode[i].y+j, 10, 0);
            }
            else {
                plotchar(" ", mtxcode[i].x, (int)mtxcode[i].y, 0, 0);
                plotchar(tmp, mtxcode[i].x, (int)mtxcode[i].y+j, 2, 0);
            }
                       
        }
       
        mtxcode[i].y += mtxcode[i].speed;
       
        if(mtxcode[i].y > 49) {
            mtxcode[i].x = 1 + rand()%78;
            mtxcode[i].y = (float)-mtxcode[i].length;
            mtxcode[i].speed = Random(0.4f, 1.0f);
        }
           
    }
}

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

int main(int argc, char *argv[])
{   
    srand(GetTickCount());   
     
    initconsole();   

    initMatrix();
       
    do
    {     
        plotMatrix();
    }
    while (!kbhit());
}
Title: Re: ASCII Matrix - C code
Post by: xteraco on July 22, 2006
hey sweet! good job man, i love it :)
Title: Re: ASCII Matrix - C code
Post by: Rbz on July 23, 2006
Thanks!

 :cheers:
Title: Re: ASCII Matrix - C code
Post by: Dad1916 on August 03, 2006
Really nic Rbraz.I compiled this in DevCPP without hitch, screenshot attached. Time to dissect the code now.  O0 O0
Title: Re: ASCII Matrix - C code
Post by: taj on August 29, 2006
Thanks for the code...glad to see someone else is gcc/Devcpp addict
Title: Re: ASCII Matrix - C code
Post by: saida on October 18, 2006
Really nice work there!