/////////////////////////////////////////////////////////////// // Kenneth Ma // CSE143 Summer 98 // Homework 2 // MY Screen! /////////////////////////////////////////////////////////////// #include #include "screen.h" // Construct a new screen, initialize it to the char ' '. Screen::Screen(){ fill(' '); } // Construct a new screen, initialize it to the char 'ch'. Screen::Screen( char ch ){ fill(ch); } // Put the character 'ch' at position (col, row). bool Screen::putChar( int col, int row, char ch ){ if( col >= 0 && col < SCREEN_WIDTH && row >= 0 && row < SCREEN_HEIGHT ){ data[col][row] = ch; return true; } return false; } // Get the contents of the screen at position (col, row); bool Screen::getChar( int col, int row, char& ch ){ if( col >= 0 && col < SCREEN_WIDTH && row >= 0 && row < SCREEN_HEIGHT ){ ch = data[col][row]; return true; } return false; } // Draw a horizontal line starting at position (col, row), // extending to the right 'length' units, made out of the character 'ch'. // Checks to make sure that length is not negative and all // coordinates are within bounds bool Screen::horizontalLine( int col, int row, int length, char ch ){ if( col < 0 || col >= SCREEN_WIDTH || length <= 0 ) return false; if( (col+length) >= 0 && (col+length-1) < SCREEN_WIDTH && row >= 0 && row < SCREEN_HEIGHT ){ for(int i=col; i<(col+length); i++){ if( !putChar(i, row, ch) ) return false; } return true; } return false; } // Draw a vertical line starting at position (col, row), // extending down 'length' units, made out of the character 'ch'. // Checks to make sure that length is not negative and all // coordinates are within bounds bool Screen::verticalLine( int col, int row, int length, char ch ){ if( row < 0 || row >= SCREEN_HEIGHT || length <= 0 ) return false; if( (row+length) >= 0 && (row+length-1) < SCREEN_HEIGHT && col >= 0 && col < SCREEN_WIDTH ){ for(int i=row; i<(row+length); i++){ if( !putChar(col, i, ch) ) return false; } return true; } return false; } // Draw a rectangle, with top left corner at position (col, row), // of dimension 'width' x 'height' units. The vertical lines in the // border are made out of '|'. The horizontal lines are made out of // '-'. The corners are made out of '+'. The rectangle is filled // with the character 'fill'. // This does not accept negative widths or heights; it will return false bool Screen::rectangle( int col, int row, int width, int height, char fill ){ // This checks to make sure that the initial col and row, and also // the width and height are >= 0 if( col < 0 || col >= SCREEN_WIDTH || row < 0 || row >= SCREEN_HEIGHT || width < 0 || height < 0 ) return false; // This checks to make sure that the new coordinates are with the boundaries if( (row+height) >= 0 && (row+height-1) < SCREEN_HEIGHT && (col+width) >= 0 && (col+width-1) < SCREEN_WIDTH ){ // If any of the corners or sides return false, then return false if( !putChar(col, row, '+') || !putChar(col+width-1, row, '+') || !putChar(col+width-1, row+height-1, '+') || !putChar(col, row+height-1, '+') || !horizontalLine(col+1, row, width-2, '-') || !horizontalLine(col+1, row+height-1, width-2, '-') || !verticalLine(col, row+1, height-2, '|') || !verticalLine(col+width-1, row+1, height-2, '|')){ return false; } // Fill in the middle of the rectangle for(int i=1; i<=height-2; i++){ if( !horizontalLine(col+1, row+i, width-2, fill)) return false; } return true; } return false; } // Draw the text contained in the buffer, starting at position (col, row) bool Screen::putText( int col, int row, char text[] ){ int len = strlen(text); // Make sure we are inbounds if( col < 0 || col >= SCREEN_WIDTH || len <= 0 ) return false; if( (col+len) >= 0 && (col+len-1) < SCREEN_WIDTH && row >= 0 && row < SCREEN_HEIGHT ){ // Insert the text character by character for(int i=0; i