1 /* $Id: Title.cpp,v 1.28 2003/07/28 16:59:51 nan Exp $ */
2 
3 // Copyright (C) 2000-2003  ���� �ȹ�(Kanna Yoshihiro)
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 
19 #include "ttinc.h"
20 #include "Title.h"
21 #include "BaseView.h"
22 #include "BaseView2D.h"
23 #include "Player.h"
24 #include "Ball.h"
25 #include "TitleView.h"
26 #include "TitleView2D.h"
27 #include "Event.h"
28 #include "MenuItem.h"
29 #include "RCFile.h"
30 
31 extern RCFile *theRC;
32 
33 extern long mode;
34 
35 extern void QuitGame();
36 
37 extern Ball theBall;
38 
Title()39 Title::Title() {
40   m_View = NULL;
41   m_selected = 0;
42   m_selectMode = 0;
43   m_count = 0;
44 
45   for ( int i = 0 ; i < 16 ; i++ )
46     m_menuItem[i] = NULL;
47 }
48 
~Title()49 Title::~Title() {
50   for ( int i = 0 ; i < 16 ; i++ ) {
51     if ( m_menuItem[i] )
52       delete m_menuItem[i];
53   }
54 
55   if ( m_View ){
56     BaseView::TheView()->RemoveView( m_View );
57     delete m_View;
58   }
59 }
60 
61 bool
Init()62 Title::Init() {
63   m_View = (TitleView *)View::CreateView( VIEW_TITLE );
64 
65   m_View->Init( this );
66 
67   BaseView::TheView()->AddView( m_View );
68 
69   m_thePlayer = Player::Create( RAND(3), 1, 1 );
70   m_comPlayer = Player::Create( RAND(3), -1, 1 );
71 
72   m_thePlayer->Init();
73   m_comPlayer->Init();
74 
75   CreateMenu( MENU_MAIN );
76 
77   return true;
78 }
79 
80 void
Create()81 Title::Create() {
82   Control::ClearControl();
83 
84   m_theControl = new Title();
85   m_theControl->Init();
86 
87   // Move it to view?
88   SDL_ShowCursor(1);
89   SDL_WM_GrabInput( SDL_GRAB_OFF );
90 }
91 
92 bool
Move(SDL_keysym * KeyHistory,long * MouseXHistory,long * MouseYHistory,unsigned long * MouseBHistory,int Histptr)93 Title::Move( SDL_keysym *KeyHistory, long *MouseXHistory,
94 	     long *MouseYHistory, unsigned long *MouseBHistory,
95 	     int Histptr ) {
96   long last = Histptr-1;
97   if ( last < 0 )
98     last = MAX_HISTORY-1;
99 
100   theBall.Move();
101   m_thePlayer->Move( NULL, NULL, NULL, NULL, 0 );
102   m_comPlayer->Move( NULL, NULL, NULL, NULL, 0 );
103 
104   switch ( m_selectMode ) {
105   case MENU_MAIN:
106     // Get key input
107     if ( KeyHistory[Histptr].unicode != KeyHistory[last].unicode ) {
108       switch ( KeyHistory[Histptr].unicode ) {
109       case '8':
110       case SDLK_UP:
111 	SetSelected( m_selected-1 );
112 	break;
113       case '2':
114       case SDLK_DOWN:
115 	SetSelected( m_selected+1 );
116 	break;
117       }
118 
119       MouseYHistory[Histptr] = BaseView::GetWinHeight() -
120 	(m_menuItem[m_selected]->GetY()+m_menuItem[m_selected]->GetHeight()/2);
121     } else {
122       SetSelected( HitTest( MouseXHistory[Histptr], MouseYHistory[Histptr] ) );
123     }
124     break;
125   case MENU_CONFIG:
126     // Get key input
127     if ( KeyHistory[Histptr].unicode != KeyHistory[last].unicode ) {
128       switch ( KeyHistory[Histptr].unicode ) {
129       case '8':
130       case SDLK_UP:
131 	SetSelected( m_selected-1 );
132 	break;
133       case '6':
134       case SDLK_RIGHT:
135 	MouseXHistory[Histptr] = BaseView::GetWinWidth()/4*3;
136 	SetSelected( HitTest( MouseXHistory[Histptr], MouseYHistory[Histptr] ) );
137 	break;
138       case '4':
139       case SDLK_LEFT:
140 	MouseXHistory[Histptr] = BaseView::GetWinWidth()/4;
141 	SetSelected( HitTest( MouseXHistory[Histptr], MouseYHistory[Histptr] ) );
142 	break;
143       case '2':
144       case SDLK_DOWN:
145 	SetSelected( m_selected+1 );
146 	break;
147       }
148       MouseYHistory[Histptr] = BaseView::GetWinHeight() -
149 	(m_menuItem[m_selected]->GetY()+m_menuItem[m_selected]->GetHeight()/2);
150       MouseXHistory[Histptr] =
151 	(m_menuItem[m_selected]->GetX()+m_menuItem[m_selected]->GetWidth()/2);
152     } else {
153       SetSelected( HitTest( MouseXHistory[Histptr], MouseYHistory[Histptr] ) );
154     }
155   }
156 
157   if ( m_selectMode == MENU_CONFIG &&
158        KeyHistory[Histptr].unicode == SDLK_ESCAPE ) {
159     m_selectMode = MENU_MAIN;
160     CreateMenu( m_selectMode );
161     m_selected = 0;
162 
163     return true;
164   }
165 
166   if ( m_selected < 0 )
167     SetSelected( 0 );
168   else {
169     if ( m_selectMode == MENU_MAIN && m_selected >= GetMenuNum( MENU_MAIN ) )
170       SetSelected( GetMenuNum( MENU_MAIN )-1 );
171   }
172 
173   if ( ((MouseBHistory[Histptr]&BUTTON_LEFT) &&
174        !(MouseBHistory[last]&BUTTON_LEFT)) ||
175        (KeyHistory[Histptr].unicode == SDLK_RETURN &&
176 	KeyHistory[last].unicode != SDLK_RETURN) ) {
177     switch ( m_selectMode ) {
178     case MENU_MAIN:
179       switch ( m_selected ) {
180       case 0:	// Start
181 	//theBall.EndGame();
182 	theBall.Warp( 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1000 );
183 	mode = MODE_SELECT;
184 	break;
185       case 1:	// Practice
186 	//theBall.EndGame();
187 	theBall.Warp( 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1000 );
188 	mode = MODE_PRACTICESELECT;
189 	break;
190       case 2:	// Training
191 	//theBall.EndGame();
192 	theBall.Warp( 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1000 );
193 	mode = MODE_TRAININGSELECT;
194 	break;
195       case 3:	// Howto
196 	//theBall.EndGame();
197 	theBall.Warp( 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1000 );
198 	mode = MODE_HOWTO;
199 	break;
200       case 4:	// Config...
201 	m_selectMode = MENU_CONFIG;
202 	break;
203       case 5:	// Quit
204 	QuitGame();
205 	break;
206       }
207       break;
208     case MENU_CONFIG:
209       if ( m_selected < GetMenuNum( MENU_CONFIG, MENU_CONFIG_LEVEL ) )
210 	theRC->gameLevel = m_selected;
211       else if ( m_selected < GetMenuNum( MENU_CONFIG, MENU_CONFIG_LEVEL )+
212 		             GetMenuNum( MENU_CONFIG, MENU_CONFIG_MODE ) )
213 	theRC->gameMode = m_selected-GetMenuNum( MENU_CONFIG, MENU_CONFIG_LEVEL );
214       else if ( m_selected < GetMenuNum( MENU_CONFIG, MENU_CONFIG_LEVEL )+
215 		GetMenuNum( MENU_CONFIG, MENU_CONFIG_MODE )+
216 		GetMenuNum( MENU_CONFIG, MENU_CONFIG_PLAYER ) ) {
217 	theRC->myModel = m_selected-GetMenuNum(MENU_CONFIG, MENU_CONFIG_LEVEL)
218 	  -GetMenuNum(MENU_CONFIG, MENU_CONFIG_MODE);
219       } else if ( m_selected == GetMenuNum( MENU_CONFIG, MENU_ALL )-1 )
220 	m_selectMode = MENU_MAIN;
221     }
222 
223     CreateMenu( m_selectMode );
224     m_selected = 0;
225   }
226 
227   m_count++;
228 
229   return true;
230 }
231 
232 MenuItem *
GetSelected()233 Title::GetSelected() {
234   return m_menuItem[m_selected];
235 }
236 
237 long
GetSelectMode()238 Title::GetSelectMode() {
239   return m_selectMode;
240 }
241 
242 long
GetCount()243 Title::GetCount() {
244   return m_count;
245 }
246 
247 long
GetMenuNum(long major,long minor)248 Title::GetMenuNum( long major, long minor ) {
249   switch ( major ) {
250   case MENU_MAIN:
251     return 6;
252   case MENU_CONFIG:
253     switch ( minor ) {
254     case MENU_ALL:
255       return 11;
256     case MENU_CONFIG_LEVEL:
257       return 4;
258     case MENU_CONFIG_MODE:
259       return 3;
260     case MENU_CONFIG_PLAYER:
261       return 3;
262     }
263   }
264 
265   return -1;
266 }
267 
268 bool
LookAt(double & srcX,double & srcY,double & srcZ,double & destX,double & destY,double & destZ)269 Title::LookAt( double &srcX, double &srcY, double &srcZ,
270 	       double &destX, double &destY, double &destZ ) {
271   srcX = TABLELENGTH*2*cos(GetCount()*3.14159265/720.0);
272   srcY = TABLELENGTH*2*sin(GetCount()*3.14159265/720.0) + TABLELENGTH/2;
273   srcZ = TABLEHEIGHT*4;
274 
275   return true;
276 }
277 
278 void
CreateMenu(long menuMajorNum)279 Title::CreateMenu( long menuMajorNum ) {
280   static char menu[][30] = {"images/StartGame", "images/Practice",
281 			    "images/Training",  "images/Howto",
282 			    "images/Config", "images/Quit"};
283   static char configMenu[][30] = {"images/Easy", "images/Normal",
284 				  "images/Hard", "images/Tsuborish",
285 				  "images/5point", "images/11point",
286 				  "images/21point",
287 				  "images/Transparent", "images/WireFrame",
288 				  "images/ArmOnly" };
289 
290   int i, j = 0;
291 
292   if ( m_menuItem[0] ) {
293     for ( i = 0 ; i < 16 ; i++ ) {
294       if ( m_menuItem[i] ) {
295 	delete m_menuItem[i];
296 	m_menuItem[i] = NULL;
297       }
298     }
299   }
300 
301   for ( i = 0 ; i < GetMenuNum( menuMajorNum ) ; i++ )
302     m_menuItem[i] = new MenuItem();
303 
304   switch ( menuMajorNum ) {
305   case MENU_MAIN:
306     for ( i = 0 ; i < GetMenuNum( MENU_MAIN ) ; i++ )
307       m_menuItem[i]->Init( 200, 500-i*90, 400, 70, &menu[i][0], this );
308     break;
309   case MENU_CONFIG:
310     for ( i = 0 ; i < GetMenuNum( MENU_CONFIG, MENU_CONFIG_LEVEL ) ; i++ )
311       m_menuItem[i]->Init( 100, 450-i*60, 200, 35, &configMenu[i][0], this );
312     for ( i = 0 ; i < GetMenuNum( MENU_CONFIG, MENU_CONFIG_MODE ) ; i++ ) {
313       j = i+GetMenuNum( MENU_CONFIG, MENU_CONFIG_LEVEL );
314       m_menuItem[j]->Init( 500, 450-i*60, 200, 35, &configMenu[j][0], this );
315     }
316     for ( i = 0 ; i < GetMenuNum( MENU_CONFIG, MENU_CONFIG_PLAYER ) ; i++ ) {
317       j = i+GetMenuNum( MENU_CONFIG, MENU_CONFIG_LEVEL ) +
318 	GetMenuNum( MENU_CONFIG, MENU_CONFIG_MODE );
319       m_menuItem[j]->Init( 500, 190-i*60, 200, 35, &configMenu[j][0], this );
320     }
321     m_menuItem[j+1]->Init( 300, 20, 400, 70, &menu[5][0], this );
322 
323     m_menuItem[theRC->gameLevel]->SetSelected( true );
324 
325     m_menuItem[GetMenuNum(MENU_CONFIG, MENU_CONFIG_LEVEL)+theRC->gameMode]
326       ->SetSelected( true );
327 
328     m_menuItem[GetMenuNum(MENU_CONFIG, MENU_CONFIG_LEVEL)+
329 	      GetMenuNum(MENU_CONFIG, MENU_CONFIG_MODE)+theRC->myModel]
330       ->SetSelected( true );
331 
332     break;
333   }
334 }
335 
336 
337 long
SetSelected(long selected)338 Title::SetSelected( long selected ) {
339   if ( selected < 0 || selected >= GetMenuNum( m_selectMode ) )
340     return m_selected;
341 
342   switch ( m_selectMode ) {
343   case MENU_MAIN:
344     m_menuItem[m_selected]->SetSelected( false );
345     m_menuItem[selected]->SetSelected( true );
346     break;
347   case MENU_CONFIG:
348     /*
349     if ( selected == 3 )
350       return m_selected;
351     */
352     break;
353   }
354 
355   m_selected = selected;
356   return m_selected;
357 }
358 
359 long
HitTest(long x,long y)360 Title::HitTest( long x, long y ) {
361   if ( theRC->gmode != GMODE_2D )
362     y = BaseView::GetWinHeight()-y;
363   for ( int i = 0 ; i < GetMenuNum( m_selectMode ) ; i++ ) {
364     if ( x > m_menuItem[i]->GetX() &&
365 	 x < m_menuItem[i]->GetX()+m_menuItem[i]->GetWidth() &&
366 	 y > m_menuItem[i]->GetY() &&
367 	 y < m_menuItem[i]->GetY()+m_menuItem[i]->GetHeight() )
368       return i;
369   }
370 
371   return -1;
372 }
373