Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's an example to show you how to animate something. This is exactly what that Star Wars animation is. It's a gigantic text file split up into individual frames. Read the FAQ at the <a href="http://www.asciimation.co.nz/" rel="nofollow noreferrer" title="Star Wars ASCIImation">Star Wars ASCIImation page</a> to find out more about it.</p> <p>The general idea is that you have a set of frames. Each frame contains one image in the animation. You clear the screen, show a frame, and then wait for some time period. Do that over and over, and you have a text animation.</p> <p>So first you create a file with your frames. Call it frames.txt. To allow variable-length frames, we follow each frame by a line that begins with <strong>@!</strong> followed by the number of animation ticks that frame should stay on the screen. For example, if you're drawing at 15 frames per second and the line is <strong>@! 15</strong>, then the frame will be on screen for 1 second (15 ticks).</p> <pre><code>+---------+ | frame 1 | +---------+ @! 15 .-----------. | frame 2 | | | `-----------' @! 15 +-------------+ | frame 3 | | | | | | | +-------------+ @! 15 .-----------. | frame 4 | | | .-----------. @! 15 +---------+ | frame 5 | +---------+ @! 3 +---------+ | rame 5 | +---------+ @! 3 +---------+ | ame 5 | +---------+ @! 3 +---------+ | me 5 | +---------+ @! 3 +---------+ | e 5 | +---------+ @! 3 +---------+ | 5 | +---------+ @! 3 +---------+ | | +---------+ @! 3 </code></pre> <p>Then compile and run this program. On Linux or OSX, I just save it as <em>text_animate.cpp</em> and run <em>make text_animate</em>. On Windows maybe the only thing you'll have to do is change the line that says system("clear") to system("cls"), but I don't know for sure.</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; #include &lt;ctime&gt; #include &lt;fstream&gt; const char *FRAME_DELIM = "@!"; unsigned int FRAME_DELIM_LEN = strlen(FRAME_DELIM); int main(int argc, char *argv[]) { if( argc != 2 ) { std::cout &lt;&lt; "Usage: text_animate &lt;frames file&gt;\n"; exit(1); } std::string frames_fn = argv[1]; struct timespec sleep_time = {0, 1000000000 / 15}; // 15 fps std::ifstream file_stream; file_stream.open(frames_fn.c_str(), std::ios::in); if( !file_stream.is_open() ) { std::cout &lt;&lt; "Couldn't open [" &lt;&lt; frames_fn.c_str() &lt;&lt; "]\n"; return -1; } std::string frame_line; unsigned int frame_ticks = 0; while( true ) { system("clear"); bool is_frame_delim = false; do { getline(file_stream, frame_line); if( file_stream.fail() &amp;&amp; file_stream.eof()) { file_stream.clear(); file_stream.seekg(0, std::ios::beg); getline(file_stream, frame_line); } else if( file_stream.fail() ) { std::cout &lt;&lt; "Error reading from file.\n"; break; } is_frame_delim = strncmp(frame_line.c_str(),FRAME_DELIM, FRAME_DELIM_LEN) == 0; if( !is_frame_delim ) { std::cout &lt;&lt; frame_line &lt;&lt; "\n"; } } while( !is_frame_delim ); frame_ticks = atoi(&amp;(frame_line.c_str()[FRAME_DELIM_LEN + 1])); while( frame_ticks-- &gt; 0 ) { nanosleep(&amp;sleep_time, NULL); } } file_stream.close(); return 0; } </code></pre> <p>Then just run <strong>./text_animate frames.txt</strong>. Press CTRL-C to exit since it loops forever.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload