Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2011
1,476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.59 KB | None | 0 0
  1. //msoftcon.cpp
  2. //provides routines to access Windows console functions
  3.  
  4. //compiler needs to be able to find this file
  5. //in MCV++, /Tools/Options/Directories/Include/type path name
  6.  
  7. #include "msoftcon.h"
  8. HANDLE hConsole;         //console handle
  9. char fill_char;          //character used for fill
  10. //--------------------------------------------------------------
  11. void init_graphics()
  12.    {
  13.    COORD console_size = {80, 25};
  14.    //open i/o channel to console screen
  15.    hConsole = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,
  16.                    FILE_SHARE_READ | FILE_SHARE_WRITE,
  17.                    0L, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0L);
  18.    //set to 80x25 screen size
  19.    SetConsoleScreenBufferSize(hConsole, console_size);
  20.    //set text to white on black
  21.    SetConsoleTextAttribute( hConsole, (WORD)((0 << 4) | 15) );
  22.  
  23.    fill_char = '\xDB';  //default fill is solid block
  24.    clear_screen();
  25.    }
  26. //--------------------------------------------------------------
  27. void set_color(color foreground, color background)
  28.    {
  29.    SetConsoleTextAttribute( hConsole,
  30.                         (WORD)((background << 4) | foreground) );
  31.    }  //end setcolor()
  32.  
  33. /* 0  Black          8  Dark gray
  34.    1  Dark blue      9  Blue
  35.    2  Dark green     10 Green
  36.    3  Dark cyan      11 Cyan
  37.    4  Dark red       12 Red
  38.    5  Dark magenta   13 Magenta
  39.    6  Brown          14 Yellow
  40.    7  Light gray     15 White
  41. */
  42. //--------------------------------------------------------------
  43. void set_cursor_pos(int x, int y)
  44.    {
  45.    COORD cursor_pos;              //origin in upper left corner
  46.    cursor_pos.X = x - 1;          //Windows starts at (0, 0)
  47.    cursor_pos.Y = y - 1;          //we start at (1, 1)
  48.    SetConsoleCursorPosition(hConsole, cursor_pos);
  49.    }
  50. //--------------------------------------------------------------
  51. void clear_screen()
  52.    {
  53.    set_cursor_pos(1, 25);
  54.    for(int j=0; j<25; j++)
  55.       putch('\n');
  56.    set_cursor_pos(1, 1);
  57.    }
  58. //--------------------------------------------------------------
  59. void wait(int milliseconds)
  60.    {
  61.    Sleep(milliseconds);
  62.    }
  63. //--------------------------------------------------------------
  64. void clear_line()                    //clear to end of line
  65.    {                                 //80 spaces
  66.    //.....1234567890123456789012345678901234567890
  67.    //.....0........1.........2.........3.........4
  68.    cputs("                                        ");
  69.    cputs("                                        ");
  70.    }
  71. //--------------------------------------------------------------
  72. void draw_rectangle(int left, int top, int right, int bottom)
  73.    {
  74.    char temp[80];
  75.    int width = right - left + 1;
  76.  
  77.    for(int j=0; j<width; j++)      //string of squares
  78.       temp[j] = fill_char;  
  79.    temp[j] = 0;                    //null
  80.  
  81.    for(int y=top; y<=bottom; y++)  //stack of strings
  82.       {
  83.       set_cursor_pos(left, y);
  84.       cputs(temp);
  85.       }
  86.    }
  87. //--------------------------------------------------------------
  88. void draw_circle(int xC, int yC, int radius)
  89.    {
  90.    double theta, increment, xF, pi=3.14159;
  91.    int x, xN, yN;
  92.  
  93.    increment = 0.8 / static_cast<double>(radius);
  94.    for(theta=0; theta<=pi/2; theta+=increment)  //quarter circle
  95.       {
  96.       xF = radius * cos(theta);  
  97.       xN = static_cast<int>(xF * 2 / 1); //pixels not square
  98.       yN = static_cast<int>(radius * sin(theta) + 0.5);
  99.       x = xC-xN;
  100.       while(x <= xC+xN)          //fill two horizontal lines
  101.          {                       //one for each half circle
  102.          set_cursor_pos(x,   yC-yN); putch(fill_char);  //top
  103.          set_cursor_pos(x++, yC+yN); putch(fill_char);  //bottom
  104.          }
  105.       }  //end for
  106.    }
  107. //--------------------------------------------------------------
  108. void draw_line(int x1, int y1, int x2, int y2)
  109.    {
  110.  
  111.    int w, z, t, w1, w2, z1, z2;
  112.    double xDelta=x1-x2, yDelta=y1-y2, slope;
  113.    bool isMoreHoriz;
  114.  
  115.    if( fabs(xDelta) > fabs(yDelta) ) //more horizontal
  116.       {
  117.       isMoreHoriz = true;
  118.       slope = yDelta / xDelta;
  119.       w1=x1; z1=y1; w2=x2, z2=y2;    //w=x, z=y
  120.       }
  121.    else                              //more vertical
  122.       {
  123.       isMoreHoriz = false;
  124.       slope = xDelta / yDelta;
  125.       w1=y1; z1=x1; w2=y2, z2=x2;    //w=y, z=x
  126.       }
  127.  
  128.    if(w1 > w2)                       //if backwards w
  129.       {
  130.       t=w1; w1=w2; w2=t;             //   swap (w1,z1)
  131.       t=z1; z1=z2; z2=t;             //   with (w2,z2)
  132.       }
  133.    for(w=w1; w<=w2; w++)            
  134.       {
  135.       z = static_cast<int>(z1 + slope * (w-w1));
  136.       if( !(w==80 && z==25) )        //avoid scroll at 80,25
  137.          {
  138.          if(isMoreHoriz)
  139.             set_cursor_pos(w, z);
  140.          else
  141.             set_cursor_pos(z, w);
  142.          putch(fill_char);
  143.          }
  144.       }
  145.    }
  146. //--------------------------------------------------------------
  147. void draw_pyramid(int x1, int y1, int height)
  148.    {
  149.    int x, y;
  150.    for(y=y1; y<y1+height; y++)
  151.       {
  152.       int incr = y - y1;
  153.       for(x=x1-incr; x<=x1+incr; x++)
  154.          {
  155.          set_cursor_pos(x, y);
  156.          putch(fill_char);
  157.          }
  158.       }
  159.    }
  160. //--------------------------------------------------------------
  161. void set_fill_style(fstyle fs)
  162.    {
  163.    switch(fs)
  164.       {
  165.       case SOLID_FILL:  fill_char = '\xDB'; break;
  166.       case DARK_FILL:   fill_char = '\xB0'; break;
  167.       case MEDIUM_FILL: fill_char = '\xB1'; break;
  168.       case LIGHT_FILL:  fill_char = '\xB2'; break;
  169.       case X_FILL:      fill_char = 'X';    break;
  170.       case O_FILL:      fill_char = 'O';    break;
  171.       }
  172.    }
  173. //--------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement