1 #include <assert.h> 2 #include <SDL.h> 3 #include <SDL_image.h> 4 #include "cube.h" 5 #include "font.h" 6 #include "soundDev.h" 7 #include "view.h" 8 #include "tileView.h" TileView(SDL_Surface * _screen,SoundDev * _sound,Cube * _cube,unsigned int _dims,unsigned int _skillLevel,bool _wrap)9 NKlein_54321::TileView::TileView( 10 SDL_Surface* _screen, 11 SoundDev* _sound, 12 Cube* _cube, 13 unsigned int _dims, 14 unsigned int _skillLevel, 15 bool _wrap 16 ) : View( _screen, _sound, _cube, _dims, _skillLevel, _wrap ), 17 showGoal( false ) 18 { 19 assert( this->screen != 0 ); 20 assert( this->dims <= 4 ); 21 22 this->centers = ::IMG_Load( "/usr/local/share/54321/data/centers.png" ); 23 this->borders = ::IMG_Load( "/usr/local/share/54321/data/borders.png" ); 24 this->blank = ::SDL_MapRGB( this->screen->format, 0, 0, 0 ); 25 26 this->font = new Font(); 27 } ~TileView(void)28 NKlein_54321::TileView::~TileView( void ) 29 { 30 delete this->font; 31 32 ::SDL_FreeSurface( this->borders ); 33 ::SDL_FreeSurface( this->centers ); 34 } 35 void redraw(void)36 NKlein_54321::TileView::redraw( void ) 37 { 38 this->View::redraw(); 39 40 unsigned int maxIndex 41 = NKlein_54321::Cube::arrayLengths[ this->dims ]; 42 43 for ( unsigned int index=0; index < maxIndex; ++index ) { 44 this->drawCell( index, false ); 45 } 46 47 this->font->centerMessage( 48 this->screen, false, 49 700, 434, 50 "Click on a tile to slide it." 51 ); 52 this->font->centerMessage( 53 this->screen, false, 54 700, 474, 55 "Right-click or Shift-click" 56 ); 57 this->font->centerMessage( 58 this->screen, false, 59 700, 498, 60 "to see the winning state." 61 ); 62 63 ::SDL_UpdateRect( this->screen, 0, 0, 0, 0 ); 64 } 65 void redraw(unsigned int index)66 NKlein_54321::TileView::redraw( unsigned int index ) 67 { 68 this->drawCell( index ); 69 } 70 void drawCell(unsigned int index,bool update)71 NKlein_54321::TileView::drawCell( 72 unsigned int index, bool update 73 ) 74 { 75 unsigned int xx; 76 unsigned int yy; 77 78 View::cellToScreen( index, this->dims, &xx, &yy ); 79 80 SDL_Rect dst; 81 dst.x = xx; 82 dst.y = yy; 83 dst.w = SQUARE; 84 dst.h = SQUARE; 85 unsigned int value; 86 if ( this->showGoal ) { 87 value = index; 88 } else { 89 value = (*this->cube)[ index ]; 90 } 91 92 unsigned int blankCell 93 = NKlein_54321::Cube::arrayLengths[ this->dims ] - 1; 94 95 if ( value != blankCell ) { 96 unsigned int coords[ NKlein_54321::Cube::DIMENSIONS ]; 97 NKlein_54321::Cube::indexToVector( value, coords ); 98 SDL_Rect src; 99 100 src.x = coords[ 2 ] * SQUARE; 101 src.y = coords[ 3 ] * SQUARE; 102 src.w = SQUARE; 103 src.h = SQUARE; 104 105 ::SDL_BlitSurface( this->borders, &src, this->screen, &dst ); 106 src.x = coords[ 0 ] * SQUARE; 107 src.y = coords[ 1 ] * SQUARE; 108 src.w = SQUARE; 109 src.h = SQUARE; 110 111 ::SDL_BlitSurface( this->centers, &src, this->screen, &dst ); 112 } else { 113 ::SDL_FillRect( this->screen, &dst, this->blank ); 114 } 115 116 if ( update ) { 117 ::SDL_UpdateRect( this->screen, xx, yy, SQUARE, SQUARE ); 118 } 119 } 120 bool isShowingGoalState(void) const121 NKlein_54321::TileView::isShowingGoalState( void ) const 122 { 123 return this->showGoal; 124 } 125 void showGoalState(bool _state)126 NKlein_54321::TileView::showGoalState( bool _state ) 127 { 128 if ( _state != this->showGoal ) { 129 this->showGoal = _state; 130 this->redraw(); 131 } 132 } 133