00001 #include "drawing_area.h"
00002
00003 namespace crs {
00004
00005 drawing_area::drawing_area( const chtype Background, unsigned int Width, unsigned int Height ):
00006 width( Width ), height( Height ), background( Background ), buffer(0) {
00007 resize_buffer();
00008 }
00009
00010 void drawing_area::resize_buffer() {
00011 delete [] buffer;
00012 buffer = new chtype[ width * height ];
00013 std::memset( buffer, background, width * height * sizeof( chtype ) );
00014 }
00015
00016 void drawing_area::setText( const std::string &text, const unsigned int x, const unsigned int y ) {
00017 if( width * height == 0 )
00018 return;
00019 if( y >= height )
00020 return;
00021 unsigned int max = text.size() + x < width ? text.size() : width - x;
00022 for( unsigned int i=0; i < max; i++ )
00023 buffer[ x + i + ( y * width ) ] = text[i];
00024 }
00025
00026 void drawing_area::drawHLine( const unsigned int x, const unsigned int y, unsigned int len, const chtype ch ) {
00027 if( width * height == 0 )
00028 return;
00029 if( x + len > width )
00030 len = width - x;
00031 chtype *foo = &buffer[ y * width + x ];
00032 for( unsigned int i = 0; i < len; i++ )
00033 *foo++ = ch;
00034 }
00035
00036 void drawing_area::drawVLine( const unsigned int x, const unsigned int y, unsigned int height, const chtype ch ) {
00037 if( width * height == 0 )
00038 return;
00039 for( unsigned int i = 0; i < height; i++ ) {
00040 buffer[ x + ( i + y ) * width ] = ch;
00041 }
00042 }
00043
00044 void drawing_area::fillRect( const unsigned int x1, const unsigned int y1, const unsigned int x2, const unsigned int y2, const chtype ch ) {
00045 if( width * height == 0 )
00046 return;
00047 if( x1 > x2 || y1 > y2 )
00048 throw bad_coords();
00049 for( unsigned int y = 1; y <= y2 - y1; y++ )
00050 drawHLine( x1, y, x2 - x1, ch );
00051 }
00052
00053 void drawing_area::resize( const unsigned int Width, const unsigned int Height ) {
00054 width = Width;
00055 height = Height;
00056 resize_buffer();
00057 }
00058
00059 void drawing_area::putPixel( const unsigned int x, const unsigned int y, const chtype ch ) {
00060 buffer[ x + ( y * width ) ] = ch;
00061 }
00062
00063 void drawing_area::fill( const chtype ch ) {
00064 std::memset( buffer, ch, width * height * sizeof( chtype ) );
00065 }
00066
00067 }