1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
#include "cscreen.h"
// ==== main ==================================================================
int main()
{
// create a screen object
CScreen screen("config.dat");
// have the object fill the screen at random locations with colored
// characters
screen.Scatter();
return 0;
} // end of "main"
CSCREEN.CPP
// Description:
// This is the implementation file for the CScreen class.
// ============================================================================
using namespace std;
#include "cscreen.h"
#include "cmatrix.h"
// ==== CScreen::CScreen ======================================================
//
// This is the constructor for the CScreen class. It uses the parameter to open
// the configuration file and fetch the display character and sleep interval to
// initialize the object. If values cannot be read from the input file, default
// values are used. Then the random number generator is seeded with the current
// system time, and the curses library is initialized before returning.
//
// Input:
// fname [IN] -- a cstring containing the name of the configuration
// file
//
// ============================================================================
CScreen::CScreen(const char fname[])
{
//use fname extraction operator to assign the values to the private data
//members.Then random number generator is seeded for random pairing
//this also calls the InitCurses function before returning back to the caller
ifstream myFile;
myFile.open(fname);
if(myFile.fail())
{
cout << "Error with file name...\n";
}
//get the display character and sleep interval
myFile.get(m_dispChar);
myFile >> m_sleep;
srand(time(NULL));
InitCurses();
} // end of "CScreen::CScreen"
// ==== CScreen::InitCurses ===================================================
//
// This function is responsible for initializing the curses library. It also
// establishes the foreground and background colors for all of the color pair
// structures.
// ====================================================================
void CScreen::InitCurses()
{
//initialize curses library. initscr(), start_color()
//set the foreground and background color.
//use init_pair()
initscr();
start_color();
init_pair(1, COLOR_RED, COLOR_BLUE);
init_pair(2, COLOR_BLACK, COLOR_MAGENTA);
init_pair(3, COLOR_WHITE, COLOR_BLUE);
init_pair(4, COLOR_BLACK, COLOR GREEN);
init_pair(5, COLOR_WHITE, COLOR_CYAN);
init_pair(6, COLOR_BLACK, COLOR_YELLOW);
init_pair(7, COLOR_WHITE, COLOR_BLACK);
} // end of "CScreen::InitCurses"
// ==== CScreen::Scatter ======================================================
//
// This function contains an infinite loop that draws characters to random
// locations on the screen. Inside the loop, a COLOR_PAIR is activated, then a
// local CMatrix object is used to fill the screen with the display character.
// After the screen has been filled, the loop pauses for about a half-second,
// before doing it all over again and again, until the user presses Ctrl+c,
// which terminates the loop so that the function can return to the caller.
//
// ============================================================================
void CScreen::Scatter()
{
//Will create a matix object passing in LINES and COLS as arguments. attron()
//and color_pair() will be used in the infinite loop filling up the matrix
//once the space is used to fill a block in the value of that block will be
//true. If not, then false.
//use usleep function for a pause.
int index;
CMatrix matrix(LINES, COLS);
for(index = 0; index<7 ; ++index)
{
attron(COLOR_PAIR(index));
matrix.fill(m_dispChar, m_sleep);
refresh();
usleep(500000);
}
} // end of "CScreen::Scatter"
CMATRIX.CPP
#include cstdlib
#include ctime
#include fstream
#include ncurses.h
#include unistd.h
using namespace std;
#include "cmatrix.h"
// ==== CMatrix::CMatrix ======================================================
//
// This is the CMatrix constructor, it just uses the parameters to initialize
// the CMatrix data members so that the screen dimensions are known to the
// matrix ADT.
//
// ============================================================================
CMatrix::CMatrix(int numRows, int numCols)
{
m_numRows = numRows;
m_numCols = numCols;
} // end of "CMatrix::CMatrix"
// ==== CMatrix::CMatrix ======================================================
//
// This function is responsible for filling the screen at random locations
// with the character parameter, and pausing the specified number of micro-
// seconds beween the drawing of each character. This drawing of characters
// continues until the screen is completely filled, at which point the
// function returns to the caller.
//
// NOTE: It is assumed that the caller has set the foreground and background
// colors before making this call.
//
// Input:
// dispChar [IN] -- the character to use to fill the screen
//
// msecs [IN] -- the number of microseconds to pause between the
// drawing of each character
//
void CMatrix::Fill(char dispChar, int msecs)
{
???
} // end of "CMatrix::Fill"
|