Author Topic: [C] matrix effect printing text  (Read 14131 times)

0 Members and 1 Guest are viewing this topic.

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
[C] matrix effect printing text
« on: December 19, 2012, 07:02:52 pm »
A function that I made to print given text in a cool style... doesn't have a particular use, but it was fun to write it :)
Does not handle newlines so you have to give it one line at a time.



The code is very simple as well:
Code: (c) [Select]
/*
  Author: Kulverstukas; http://9v.lt/

  text -- text to print;
  loops - times to loop over char
          array before putting the
          right char. Increase the
          count to increase delay.
*/
void printText(char * text, int loops) {
    // char matrix
    const char chars[52] = {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o',
                            'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k',
                            'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm', '1',
                            '2', '3', '4', '5', '6', '7', '8', '9', '-',
                            '=', '[', ']', ';', '/', '?', ':', '}', '{',
                            '+', '*', '0', '!', '@', '#', '\0'};

    int totalChars = strlen(text); // total chars in a given string
    int charCount = 0; // counter that keeps track of scrolling chars
    int loopCount = 0; // counter that counts loops
    char newArr[totalChars]; // new char array to build new string
    int i = 0;  // index counter, keeps track of given text index
    for (i; i <= totalChars; i++) {
        newArr[i] = text[i];
        newArr[i+1] = '\0'; // must be terminated by nullbyte, else garbage will be shown
        while (loopCount <= loops) {
            printf("%s%c\r",newArr,chars[charCount]);
            if (charCount == 51) {
                charCount = 0;
                loopCount++;
            }
            charCount++;
            int c;
            for (c = 0 ; c <= 999999; c++) {} // work emulator to delay execution
        }
        loopCount = 0;
    }
}

Usage:
Code: (c) [Select]
int main() {
    printText("Enter the matrix", 2);
    return 0;
}
« Last Edit: December 19, 2012, 07:05:16 pm by Kulverstukas »

Offline Polyphony

  • VIP
  • Knight
  • *
  • Posts: 178
  • Cookies: 23
    • View Profile
Re: [C] matrix effect printing text
« Reply #1 on: December 20, 2012, 04:56:43 am »
I messed with it and toned down the mega for loop because it was lagging on my laptop, but I also changed the last char to a space, because for some reason it would print everything and then an extra '#' at the end.  Nice work Kulverstukas :) it's a neat function to put in the "c/snippets" directory lol
Code: [Select]
<Spacecow_> for that matter I have trouble believing bitches are made out of ribs
<Gundilido> we are the revolutionary vanguard fighting for the peoples right to display sombrero dawning poultry
<Spacecow> did they see your doodle?
<~phage> Maybe
<+Unresolved> its just not creative enough for me
<+Unresolved> my imagination is to big to something so simple

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: [C] matrix effect printing text
« Reply #2 on: December 20, 2012, 05:57:06 am »
Might be issues with different compilers... I was using MinGW.

Offline Uriah

  • Sir
  • ***
  • Posts: 454
  • Cookies: 42
  • άξονας
    • View Profile
Re: [C] matrix effect printing text
« Reply #3 on: December 20, 2012, 06:53:36 am »
This is pretty cool Kulverstukas! There are some pretty cool graphical ideas you can implement this with.

Offline Polyphony

  • VIP
  • Knight
  • *
  • Posts: 178
  • Cookies: 23
    • View Profile
Re: [C] matrix effect printing text
« Reply #4 on: December 21, 2012, 06:18:48 am »
yeah, you could make a login and once you enter the right password then it could
Code: (c) [Select]
printText("ACCESS GRANTED");or something like that, especially if it was some software cracking challenge or something lol.
« Last Edit: December 21, 2012, 06:19:31 am by Polyphony »
Code: [Select]
<Spacecow_> for that matter I have trouble believing bitches are made out of ribs
<Gundilido> we are the revolutionary vanguard fighting for the peoples right to display sombrero dawning poultry
<Spacecow> did they see your doodle?
<~phage> Maybe
<+Unresolved> its just not creative enough for me
<+Unresolved> my imagination is to big to something so simple

Offline Live Wire

  • Knight
  • **
  • Posts: 189
  • Cookies: 4
  • Up on your Net
    • View Profile
Re: [C] matrix effect printing text
« Reply #5 on: December 21, 2012, 07:34:16 am »
Matrix Random Character Scrolling effect.
 
Code: [Select]

#include <iostream.h>
 
int main(){
    int input;
    int y = 0;
    char numbers[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()-=_+";
    cout << "Please enter a number: ";
    cin >> input;
    system("color 2");
   
    for (int x=0;x<=input; x++){
        system("color 5");
        cout << numbers[rand() % 78] ;
        system("color 28");
        cout << " ";   
        system("color 5");   
        cout << numbers[rand() % 78] ;
        system("color 23");
        cout << "*";
        system("color 59");
        cout << numbers[rand() % 78] ;
        system("color 71");
        if (y%7 == 1){
                 cout << "\t";
                 system("color 18");
                 y = 4;
        }else{
                 y++;
                 system("color 61");
        }
    }
   
    system("color 7");
   
    return 0;
}

this should work, at least. Oh, the colors will rape your mind.  If you want just straight up green, comment all other system("color s") but the first one, it also goes a lot faster if you do this. DO NOT WATCH WHILE HIGH!!!! YOU WILL PROBABLY DIE.
 
 
 
But Kulver, yours is cool too :)
« Last Edit: December 21, 2012, 07:34:58 am by Live Wire »
"There is no right or wrong, there is only fun and boring."

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: [C] matrix effect printing text
« Reply #6 on: December 21, 2012, 10:22:04 am »
derpaherp
I don't see any random characters, or any matrix, or scrolling for that matter.
Just flashes for a second in few colors and that's it...

Offline Live Wire

  • Knight
  • **
  • Posts: 189
  • Cookies: 4
  • Up on your Net
    • View Profile
Re: [C] matrix effect printing text
« Reply #7 on: December 21, 2012, 12:45:37 pm »
for matrix comment out all color but the first one, and set the value to like 100000000
"There is no right or wrong, there is only fun and boring."

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: [C] matrix effect printing text
« Reply #8 on: December 21, 2012, 01:01:27 pm »
for matrix comment out all color but the first one, and set the value to like 100000000
It's still garbage because it just print random chars in a line...

Offline Live Wire

  • Knight
  • **
  • Posts: 189
  • Cookies: 4
  • Up on your Net
    • View Profile
Re: [C] matrix effect printing text
« Reply #9 on: December 21, 2012, 01:13:37 pm »
It's still garbage because it just print random chars in a line...

hmm. on my pc, it scrolls fast enough that i actually get columns. anyway, just found it on my comp, and felt like sharing.
"There is no right or wrong, there is only fun and boring."

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: [C] matrix effect printing text
« Reply #10 on: December 21, 2012, 01:35:29 pm »
Useless, but funny. I have something similar from a good c coder. Just can't find it atm.

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: [C] matrix effect printing text
« Reply #11 on: January 04, 2013, 05:14:05 pm »
Nice work, Kulverstukas.  I would suggest that you look at changing the 'execution delay' loop for something that is far less resource-intensive.  Currently, it's what's known as a 'busy loop'.  This keeps the processor going and wastes energy by generating heat for no real purpose.  There are better ways to let the OS handle other process requests during this delay time so that you have a smoother experience overall.

Also, it may be to your benefit to use '++var' rather than 'var++'.  You should be familiar with the difference between the mechanics of the two but know that '++var' should never be slower than 'var++' while it could be, potentially, faster.
-Xires

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: [C] matrix effect printing text
« Reply #12 on: January 04, 2013, 05:57:24 pm »
Thanks for the tips, Xires.
I tried searching for alternate solutions as I don't like the work emulating loop as well, but only way I could think of was to use Timers, and C doesn't have such stuff and I didn't find any other solutions... I'll take a look again if you say there is a better solution :)

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: [C] matrix effect printing text
« Reply #13 on: January 05, 2013, 06:51:36 am »
C does have timers, actually.  But what you probably want to look for is some form of 'sleep' function.  There are even nanosecond resolution ones, if needed.  Even a nanosecond can provide the CPU some time to do other things.
-Xires

Offline bluechill

  • Cybermancer
  • Royal Highness
  • ****
  • Posts: 682
  • Cookies: 344
  • I am the existence in these walls
    • View Profile
Re: [C] matrix effect printing text
« Reply #14 on: January 05, 2013, 05:36:26 pm »
C does have timers, actually.  But what you probably want to look for is some form of 'sleep' function.  There are even nanosecond resolution ones, if needed.  Even a nanosecond can provide the CPU some time to do other things.

Kulverstukas, look up sleep().  The only timer defined by the standard is clock() but on a multithreaded system it may not be real-time as it's process time not real-time..... But that's probably good enough for what you want.
I have dreamed a dream, but now that dream has gone from me.  In its place now exists my own reality, a reality which I have created for myself by myself.