Advertisement
Guest User

Greyscale Graphics CLI explanation

a guest
Mar 21st, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. Want to make computer graphics demos, but don't know how to open a window? Don't have time to learn?
  2.  
  3. Use greyscale CLI graphics!
  4.  
  5. Here are 2 grey scales that work well on the terminal:
  6.  
  7. char greyscale[] = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,\"^`'. ";
  8. char short_greyscale[] = " .:-=+*#%@";
  9.  
  10. The larger greyscale is better for very detailed images while the short greyscale is more convincing.
  11.  
  12. Simply make your demo output a floating point number between 0 and 1 for every position, multiply it by strlen(greyscale), shorten it to an int and use it as an index to the array of characters. Write it out with printf() or std::cout if you're using C++. I recommend pushing out all lines of your graphics at once, by creating an array to hold your screen. Allocate it like this:
  13.  
  14. char screen[(screen_width + 1)  * (screen_height) + 1];
  15.  
  16. Initialize it like this:
  17.  
  18. screen[(screen_width + 1)  * (screen_height)]  = '\0';
  19.  
  20. for(int y = 0; y < screen_height; y++)
  21.  
  22. {
  23.  
  24.    screen[screen_width + (screen_width + 1) * y] = '\n';
  25.  
  26. }
  27.  
  28.  
  29.  
  30. When you want to write a float VAL which is between 0 and 1 to position (X, Y) in the screen, write to it like this:
  31.  
  32. screen[X + (screen_width + 1) * Y] = greyscale[VAL * (strlen(greyscale) - 1)];
  33.  
  34. Print it like this:
  35.  
  36. printf("\n%s",screen);
  37.  
  38. Or this:
  39.  
  40. std::cout << "\n" << screen << std::endl;
  41.  
  42. You now have everything you need to make amazing things using only ANSI C on the terminal. Go forth, and make me proud!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement