1\part{The Main Menu} 2\section{The Main Menu Controller} 3 4The namespace inside the Main Menu controller class is a concatenation 5of the general namespace and the name of the Main Menu controller 6class. 7<<MainMenuCNameSpace>>= 8<<NameSpace>>::MainMenuController 9@ 10 11The MainMenu controller inherits from the generic controller 12of \S\ref{sec:controller}. It simply fields mouse events for 13the main menu screen. And, it fields clicks on the sidebar 14``Quit'' button. 15 16The MainMenu game controller contains an instance of the 17MainMenu game view. 18<<MainMenuC View>>= 19 MainMenuView view; 20@ 21 22%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 23\subsection{The Constructor and Destructor} 24 25The constructor for the MainMenu controller class takes 26two arguments. It takes a pointer to the screen as its first argument 27and a pointer to the game cube as the second argument. It won't 28actually make any use of the game cube, but it passes it into the 29[[Controller]] base class so that the base class can feel happy 30about life. 31<<MainMenuC Constructor Declaration>>= 32 MainMenuController( 33 SDL_Surface* _screen, Cube* cube 34 ); 35@ 36 37The constructor for the MainMenu controller class simply passes 38its arguments to the [[Controller]] constructor and the [[MainMenuView]] 39member. Then, it tells the view to redraw itself. 40<<MainMenuC Constructor Implementation>>= 41 <<MainMenuCNameSpace>>::MainMenuController( 42 SDL_Surface* _screen, 43 Cube* _cube 44 ) : Controller( _cube ), 45 view( _screen ) 46 { 47 this->view.redraw(); 48 } 49@ 50 51The destructor for the MainMenu controller does nothing 52at the moment. 53<<MainMenuC Destructor Declaration>>= 54 virtual ~MainMenuController( void ); 55@ 56<<MainMenuC Destructor Implementation>>= 57 <<MainMenuCNameSpace>>::~MainMenuController( void ) 58 { 59 } 60@ 61 62%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 63\subsection{The Mouse Event Interface} 64 65The routine which handles mouse events needs to know whether the 66event is a mouse press or mouse release. It also needs to know 67where the event happened. And, it needs to know which mouse button 68was pressed. 69<<MainMenuC Mouse Click Declaration>>= 70 virtual void handleMouseClick( 71 bool isMouseUp, 72 unsigned int xx, 73 unsigned int yy, 74 unsigned int buttonNumber 75 ); 76@ 77 78In this case, it simply checks which area on the screen has been 79clicked. Then, it sends off the appropriate user-event to tell 80the main loop to reset the current controller. 81<<MainMenuC Mouse Click Implementation>>= 82 void 83 <<MainMenuCNameSpace>>::handleMouseClick( 84 bool isMouseUp, 85 unsigned int xx, 86 unsigned int yy, 87 unsigned int buttonNumber 88 ) 89 { 90 unsigned int index; 91 bool hit; 92 93 hit = this->view.handleMouseClick( 94 this, isMouseUp, xx, yy, buttonNumber 95 ); 96 97 if ( !hit ) { 98 <<MainMenuC Find Which Item Clicked>> 99 if ( hit ) { 100 <<MainMenuC Fire Off Change Event>> 101 } 102 } 103 } 104@ 105 106This loop runs through each game selectable from the main 107menu. Then, it checks the mouse click against the bounding 108box of the corresponding game. 109<<MainMenuC Find Which Item Clicked>>= 110 unsigned int maxGame = <<MainMenuVNameSpace>>::MAX_GAME; 111 unsigned int chosen; 112 for ( unsigned int ii=0; !hit && ii < maxGame; ++ii ) { 113 if ( this->view.pointInBox( xx, yy, ii ) ) { 114 chosen = ii; 115 hit = true; 116 } 117 } 118@ 119 120For our purposes, the event that gets sent is a very simple event 121whose code completely identifies the game mode to switch into. We 122use [[SDL_USEREVENT]] to signal the game change. The code [[-1]] 123is used to switch to the main menu. After that, the games are 124coded in order: FlipFlop, BombSquad, MazeRunner, PegJumper, and 125TileSlider. 126<<MainMenuC Fire Off Change Event>>= 127 SDL_Event change; 128 change.type = SDL_USEREVENT; 129 change.user.code = chosen; 130 ::SDL_PushEvent( &change ); 131@ 132 133%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 134\subsection{The Game Setting Interface} 135 136The main menu isn't really a game. Therefore, it has 137really wimpy implementations of all of the game setting 138methods. They don't do anything. 139<<MainMenuC Game Setting Interface>>= 140 virtual void setDimension( unsigned int _dims ); 141 virtual void setSkillLevel( unsigned int _skillLevel ); 142 virtual void setWrap( bool _wrap ); 143 virtual void newGame( void ); 144@ 145<<MainMenuC Game Setting Implementation>>= 146 void 147 <<MainMenuCNameSpace>>::setDimension( 148 unsigned int _dims 149 ) 150 { 151 } 152@ 153<<MainMenuC Game Setting Implementation>>= 154 void 155 <<MainMenuCNameSpace>>::setSkillLevel( 156 unsigned int _skillLevel 157 ) 158 { 159 } 160@ 161<<MainMenuC Game Setting Implementation>>= 162 void 163 <<MainMenuCNameSpace>>::setWrap( 164 bool _wrap 165 ) 166 { 167 } 168@ 169<<MainMenuC Game Setting Implementation>>= 170 void 171 <<MainMenuCNameSpace>>::newGame( void ) 172 { 173 } 174@ 175 176%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 177\subsection{The [[MainMenuController]] class} 178 179In this section, we assemble the [[MainMenuController]] class from 180the pieces in the sections above. 181 182We include, in the [[MainMenuController]] class, the constructor 183and the destructor. 184<<MainMenuC Class Definition>>= 185 public: 186 <<MainMenuC Constructor Declaration>> 187 <<MainMenuC Destructor Declaration>> 188@ 189 190The [[MainMenuController]] class also declares 191the methods that would be used by a more involved 192[[View]] class to change the game state. 193<<MainMenuC Class Definition>>= 194 public: 195 <<MainMenuC Game Setting Interface>> 196@ 197 198We include, in the [[MainMenuController]] class, the method used 199for mouse clicks. 200<<MainMenuC Class Definition>>= 201 public: 202 <<MainMenuC Mouse Click Declaration>> 203@ 204 205The [[MainMenuController]] class also contains the member variables 206which were defined at the beginning of this section of the document. 207<<MainMenuC Class Definition>>= 208 private: 209 <<MainMenuC View>> 210@ 211 212Once these declarations are all done, we throw all of these together 213into the class declaration itself. The [[MainMenuController]] 214inherits directly from the [[Controller]] class of \S\ref{sec:controller} 215so that the main loop of the program doesn't have to do anything 216special for it. 217<<MainMenuC Class Declaration>>= 218 class MainMenuController : public Controller { 219 <<MainMenuC Class Definition>> 220 }; 221@ %def MainMenuController 222 223%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 224\subsection{The [[mainmenuController.h]] file} 225 226In this section, we assemble the header file for the [[MainMenuController]] 227class. It is really straightforward since we assembled the class 228declaration in the previous section. The only thing that we add 229to the class declaration is that we tuck it into our own name space 230so that we can keep the global namespace squeaky clean. 231<<mainmenuController.h>>= 232 namespace <<NameSpace>> { 233 <<MainMenuC Class Declaration>> 234 }; 235@ 236 237%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 238\subsection{The [[mainmenuController.cpp]] file} 239 240In this section, we assemble the MainMenu controller source file. 241It requires the header files for the [[Cube]] class, the [[SoundDev]] 242class, the [[View]] class, the [[Controller]] class, and the 243[[MainMenuView]] classes in addition to its own header file. 244<<mainmenuController.cpp>>= 245 #include <SDL.h> 246 #include "cube.h" 247 #include "soundDev.h" 248 #include "view.h" 249 #include "controller.h" 250 #include "mainmenuView.h" 251 #include "mainmenuController.h" 252@ 253 254After the header files, we include the implementations of the 255constructor and destructor. 256<<mainmenuController.cpp>>= 257 <<MainMenuC Constructor Implementation>> 258 <<MainMenuC Destructor Implementation>> 259@ 260 261After the constructor and destructor, the implementations of 262the bogus game state methods are also included. 263<<mainmenuController.cpp>>= 264 <<MainMenuC Game Setting Implementation>> 265@ 266 267Then, we include the implementation of the method used to field 268mouse clicks. 269<<mainmenuController.cpp>>= 270 <<MainMenuC Mouse Click Implementation>> 271@ 272