steamengines

run this shit

Nov 13th, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <windows.h>
  4. #include <time.h> // for time
  5.  
  6. using namespace std;
  7.  
  8. const unsigned char BLACK = 219;
  9.  
  10. /* type of each pixel */
  11. typedef struct {
  12.     unsigned char elem;
  13.     unsigned char color;
  14. } SCREENELEMENT;
  15.  
  16. BOOL gotoxy(const WORD x, const WORD y) {
  17.     COORD xy;
  18.     xy.X = x;
  19.     xy.Y = y;
  20.     return SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), xy);
  21. }
  22.  
  23. /* print the screen to console */
  24. inline
  25. void printScreen(int width, int height, SCREENELEMENT **screen) {
  26.     gotoxy(1, 1); // top-left corner
  27.     for (int i = 0; i < height; i++) {
  28.         for (int j = 0; j < width; j++) {
  29.             SCREENELEMENT *cur = &screen[i][j];
  30.             SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), cur->color);
  31.             printf("%c", cur->elem); // may be faster than cout
  32.         }
  33.     }
  34. }
  35.  
  36. /* initialize the screen to element se */
  37. inline
  38. void initScreen(int width, int height, SCREENELEMENT **screen, SCREENELEMENT se) {
  39.     for (int i = 0; i < height; i++) {
  40.         for (int j = 0; j < width; j++) {
  41.             screen[i][j] = se;
  42.         }
  43.     }
  44. }
  45.  
  46. /* draw a rectangle to (x, y), size (szx, szy) */
  47. inline
  48. void putRectangle(int x, int y, int szx, int szy, int color, int height, SCREENELEMENT **screen) {
  49.     for (int i = y; i < y + szy; i++) {
  50.         for (int j = x; j < x + szx; j++) {
  51.             screen[i][j].color = color;
  52.         }
  53.     }
  54. }
  55.  
  56. int main() {
  57.     SetConsoleOutputCP(437);
  58.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  59.     int ret;
  60.     /* get the width of the console */
  61.     ret = GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
  62.     int width = 80;
  63.     if (ret) {
  64.         width = csbi.dwSize.X;
  65.     }
  66.     int height = csbi.srWindow.Bottom - csbi.srWindow.Top;
  67.     SCREENELEMENT **s = new SCREENELEMENT*[height];
  68.     for (int i = 0; i < height; i++) {
  69.         s[i] = new SCREENELEMENT[width];
  70.     }
  71.     SCREENELEMENT se;
  72.     se.elem = BLACK;
  73.     se.color = 0;
  74.     initScreen(width, height, s, se);
  75.     se.color = 15;
  76.     srand(time(NULL));
  77.     for (;;) {
  78.         int x = rand() % width;
  79.         int y = rand() % height;
  80.         int szx = rand() % (width - x);
  81.         int szy = rand() % (height - y);
  82.         /* color from 0 to 15 */
  83.         int c = rand() % 15 + 1;
  84.         /* put a random rectangle */
  85.         putRectangle(x, y, szx, szy, c, height, s);
  86.         printScreen(width, height, s);
  87.     }
  88.     for (int i = 0; i < height; i++) {
  89.         delete[] s[i];
  90.     }
  91.     delete[] s;
  92.     return 0; /* use Ctrl+C, never returns */
  93. }
Advertisement
Add Comment
Please, Sign In to add comment