Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

ascii animation

Name: polymorph !3GqYIJ3Obs 2007-03-30 8:15 ID:P2cRkSb8

here's a little something i whipped up
( just a side note - ifstream file(filename.c_str()); has a problem with spaces in paths, so if you're running windows just put the text file you want animated in your C:\ directory.)

    /*
    ascii animated video program
    To create an AAV, simply create a text file with the following format
    3 <--- height of current frame
    000   \
    000    |---content of frame (doesn't matter how wide, as long as it's the same hight as specified
    000   /
    1 <--- how long the frame lasts in seconds (can be a decimal value, e.g 0.04 is 1/25th of a second

    just keep on adding sections like this to build it up frame by frame
    enjoy!
    (copyright 2007 dominic rudkin, distriputed under the lesser GPL)
    */
   
    #include <iostream>
    #include <stdio.h>
    #include <time.h>
    #include<cstdlib>
    #include <fstream>
    #include <string>
    #include <vector>
    #include <sstream>
    using namespace std;
   
    void wait ( double seconds )
    {
      clock_t endwait;
      endwait = clock () + static_cast<long int>(seconds * CLOCKS_PER_SEC) ;
      while (clock() < endwait) {}
    }
   
    int main()
    {
    string line,filename;
    int height;
    double frame;
   
    cout << "Enter filename: ";
    cin >> filename;
    ifstream file(filename.c_str());
   
    if(!file)
    {
        cerr << "Error opening file " << filename <<endl;
        return EXIT_FAILURE;
    }
    vector<std::string> contents_of_file;
   
    while(getline(file, line))
    {
        contents_of_file.push_back(line);
        height = static_cast<int>(strtod(line.c_str(), NULL));
        while (height > 0)
            {
            system("cls"); // Use this if you're on windows
            //system("clear"); comment out above and uncomment this if you're running linux/unix
            getline(file, line);
            contents_of_file.push_back(line);
            cout << line << endl;
            height = height-1;
            }
        getline(file, line);
        contents_of_file.push_back(line);
        frame = strtod(line.c_str(), NULL);
        wait (frame);
    }
    }

Name: Anonymous 2007-03-31 23:38 ID:he6TyR4e

--height woudl generate the same unoptimized opcodes:

mov reg,[ebp+height+stktmp]
dec reg
mov [ebp+height+stktmp],reg

same as height--

now if this were to be used in a real expresion, then it would have made a lot more sense... i suppose its a matter of preference wether you increment before or after, most compilers
would discard the 'reg' used for height-- after its usage, while --height would keep the reg IF used, however if it wasnt used in any expresion, it would have been pointless
so there would be no speed differences between the two
however if you use
height = height - 1
then the following code would be generated(HOPEFULLY, with an optimizing compiler):

mov reg,[ebp+height+stktmp]
sub reg,1 ; much slower than a simple dec or inc
mov [ebp+height+stktmp],reg

this actually performs a real substraction , not a optimized incrememnt/decrement instruction... for more details check out the microcode involved with each in your assembly book(preferably the one from intel)
substractions are only slightly slower than compares(actually a compare is the same thing as a substraction, just the result is not saved and only the flags are affected)

this applies to IA-32 Assembler.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List