summaryrefslogtreecommitdiff
path: root/noncore/games/sfcave-sdl/menu.cpp
blob: fb2a63587b98c089dcb4077cef3f727db1b52022 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include <SDL/SDL_image.h>
#include <SDL/SDL_rotozoom.h>

#include "constants.h"
#include "sfcave.h"
#include "game.h"
#include "menu.h"
#include "font.h"
#include "starfield.h"

MenuOption :: MenuOption( string text, int id )
{
	menuText = text;
	menuId = id;
	nextMenu = 0;
	highlighted = false;
}

MenuOption :: ~MenuOption()
{
}


int MenuOption :: draw( SDL_Surface *screen, int y )
{
	if ( highlighted )
	{
		int x = (240 - FontHandler::TextWidth( FONT_MENU_HIGHLIGHTED, (const char *)menuText.c_str() ))/2;
		FontHandler::draw( screen, FONT_MENU_HIGHLIGHTED, (const char *)menuText.c_str(), x, y );
		return FontHandler::FontHeight( FONT_MENU_HIGHLIGHTED );
	}
	else
	{
		int x = (240 - FontHandler::TextWidth( FONT_MENU_UNHIGHLIGHTED, (const char *)menuText.c_str() ))/2;
		FontHandler::draw( screen, FONT_MENU_UNHIGHLIGHTED, (const char *)menuText.c_str(), x, y );
		return FontHandler::FontHeight( FONT_MENU_UNHIGHLIGHTED );
	}
}

void MenuOption :: setNextMenu( Menu *item, bool down )
{
	nextMenu = item;
	downMenuTree = down;
}



//----------------- Menu Class -------------

SDL_Surface * Menu :: sfcaveTextImage;
Menu *Menu :: mainMenu;
Menu *Menu :: currentMenu;

// This is the Master Menu constructor
Menu :: Menu( SFCave *p )
{
	parent = p;
	parentMenu = 0;
	statusText = "";

//	listItems.setAutoDelete( TRUE );

	SDL_Surface *tmp = IMG_Load( IMAGES_PATH "sfcave_text.bmp" );
	sfcaveTextImage = SDL_CreateRGBSurface(SDL_SWSURFACE, tmp->w, tmp->h, 32,
	                                       0x000000ff,0x0000ff00, 0x00ff0000, 0xff000000);
    SDL_BlitSurface(tmp, NULL, sfcaveTextImage, NULL);
	SDL_FreeSurface(tmp);

	// Create menu structure
	// Top level menu has 5 items - Start Game, Replays, Options, Help, and Quit
	// Replays, Option menu items hav submenus
	MenuOption *replayMenu = 0;
	MenuOption *optionsMenu = 0;
	MenuOption *item = 0;
	addMenuOption( "Start Game", MENU_STARTGAME );
	replayMenu = addMenuOption( "Replays", MENU_REPLAYS );
	optionsMenu = addMenuOption( "Options", MENU_OPTIONS );
	addMenuOption( "Help", MENU_HELP );
	addMenuOption( "Quit", MENU_QUIT );

	// Now deal with the Replays Menu
	Menu *replay = new Menu( this );
	replay->addMenuOption( "Play Replay", MENU_PLAY_REPLAY );
	replay->addMenuOption( "Load Replay", MENU_LOAD_REPLAY );
	replay->addMenuOption( "Save Replay", MENU_SAVE_REPLAY );
	item = replay->addMenuOption( "Back", MENU_BACK );
	item->setNextMenu( this, false );
	replayMenu->setNextMenu( replay );

	// Now deal with the Options Menu	currentMenu->currentMenuOption->setHighlighted( false );
	listItems.front()->highlight( true );

	Menu *options = new Menu( this );
	MenuOption *typeMenu = 0;
	MenuOption *difficultyMenu = 0;
	MenuOption *soundsMenu = 0;
	typeMenu = options->addMenuOption( "Game Type", MENU_GAME_TYPE );
	difficultyMenu = options->addMenuOption( "Difficulty", MENU_DIFFICULTY );
	soundsMenu = options->addMenuOption( "Sound", MENU_SOUNDS );
	options->addMenuOption( "Clear Scores", MENU_CLEAR_SCORES );
	item = options->addMenuOption( "Back", MENU_BACK );
	item->setNextMenu( this, false );
	optionsMenu->setNextMenu( options );

	// Game Type menu
	Menu *gameType = new Menu( options );
	item = gameType->addMenuOption( "SFCave", MENU_GAME_SFCAVE );
	item->setNextMenu( options );
	item = gameType->addMenuOption( "Gates", MENU_GAME_GATES );
	item->setNextMenu( options );
	item = gameType->addMenuOption( "Fly", MENU_GAME_FLY );
	item->setNextMenu( options );
	item = gameType->addMenuOption( "Back", MENU_BACK );
	item->setNextMenu( options );
	typeMenu->setNextMenu( gameType );

	// Game Difficulty menu
	MenuOption *customMenu = 0;
	Menu *gameDifficulty = new Menu( options );
	item = gameDifficulty->addMenuOption( "Easy", MENU_DIFFICULTY_EASY );
	item->setNextMenu( options, false );
	item = gameDifficulty->addMenuOption( "Normal", MENU_DIFFICULTY_NORMAL );
	item->setNextMenu( options, false );
	item = gameDifficulty->addMenuOption( "Hard", MENU_DIFFICULTY_HARD );
	item->setNextMenu( options, false );
	customMenu = gameDifficulty->addMenuOption( "Custom", MENU_DIFFICULTY_CUSTOM );
	item = gameDifficulty->addMenuOption( "Back", MENU_BACK );
	item->setNextMenu( options, false );
	difficultyMenu->setNextMenu( gameDifficulty );

	// Sounds Menu
	Menu *sounds = new Menu( options );
	sounds->addMenuOption( "Sound On", MENU_SOUND_ON );
	sounds->addMenuOption( "Sound Off", MENU_SOUND_OFF );
	sounds->addMenuOption( "Music On", MENU_MUSIC_ON );
	sounds->addMenuOption( "Music Off", MENU_MUSIC_OFF );
	item = sounds->addMenuOption( "Back", MENU_BACK );
	item->setNextMenu( options, false );
	soundsMenu->setNextMenu( sounds );
	
	// Custom Menu
	Menu *custom = new Menu( gameDifficulty );
	Menu *updown = new Menu( custom );
	item = custom->addMenuOption( "Thrust", MENU_CUSTOM_THRUST );
	item->setNextMenu( updown );
	item = custom->addMenuOption( "Gravity", MENU_CUSTOM_GRAVITY );
	item->setNextMenu( updown );
	item = custom->addMenuOption( "Max Speed Up", MENU_CUSTOM_MAXSPEEDUP );
	item->setNextMenu( updown );
	item = custom->addMenuOption( "Max Speed Down", MENU_CUSTOM_MAXSPEEDDOWN );
	item->setNextMenu( updown );
	item = custom->addMenuOption( "Back", MENU_BACK );
	item->setNextMenu( gameDifficulty, false );
	customMenu->setNextMenu( custom );

    // Up down menu
	item = updown->addMenuOption( "Increase", MENU_CUSTOM_INCREASE );
	item = updown->addMenuOption( "Decrease", MENU_CUSTOM_DECREASE );
	item = updown->addMenuOption( "Save", MENU_CUSTOM_SAVE );
	item->setNextMenu( custom, false );
	item = updown->addMenuOption( "Cancel", MENU_CUSTOM_CANCEL );
	item->setNextMenu( custom, false );

	// Set static variables for menu selection up
	mainMenu = this;
	currentMenuOption = 0;

	resetToTopMenu();

	angle = 0;

	stars = new StarField;
}

// This is a private constructor used for creating sub menus - only called by the Master Menu
Menu :: Menu( Menu *p )
{
	parentMenu = p;
//	listItems.setAutoDelete( TRUE );
	currentMenuOption = 0;
}

Menu :: ~Menu()
{
	if ( this == mainMenu )
	{
		SDL_FreeSurface( sfcaveTextImage );
		delete stars;
	}
}

void Menu :: draw( SDL_Surface *screen )
{
	// draw stafield
	stars->draw( screen );
	stars->move( );

	// Draw the spinning SFCave logo
	SDL_Surface *rotozoom_pic;
	SDL_Rect dest;

	angle += 2;
	if ( angle > 359 )
		angle = 0;
	if ((rotozoom_pic=rotozoomSurface (sfcaveTextImage, angle*1, 1, SMOOTHING_ON))!=NULL)
	{
		dest.x = (screen->w - rotozoom_pic->w)/2;
		dest.y = 10;
		dest.w = rotozoom_pic->w;
		dest.h = rotozoom_pic->h;
		SDL_BlitSurface( rotozoom_pic, NULL, screen, &dest );
		SDL_FreeSurface(rotozoom_pic);
	}

	// Draw what game is selected
	char text[100];
	sprintf( text, "Current Game: %s", (const char *)parent->getCurrentGame()->getGameName().c_str() );
//	Menu::scoreFont.PutString( screen, (240 - Menu::scoreFont.TextWidth( (const char *)text ))/2, 120, (const char *)text );
	FontHandler::draw( screen, FONT_WHITE_TEXT, (const char *)text, (240 - FontHandler::TextWidth( FONT_WHITE_TEXT,(const char *)text ))/2, 120 );
	sprintf( text, "Difficulty: %s", (const char *)parent->getCurrentGame()->getGameDifficultyText().c_str() );
//	Menu::scoreFont.PutString( screen, (240 - Menu::scoreFont.TextWidth( (const char *)text ))/2, 120 + Menu::scoreFont.FontHeight()+2, (const char *)text );
	FontHandler::draw( screen, FONT_WHITE_TEXT, (const char *)text, (240 - FontHandler::TextWidth( FONT_WHITE_TEXT,(const char *)text ))/2, 120 + FontHandler::FontHeight( FONT_WHITE_TEXT ) );

	if ( statusText != "" )
		FontHandler::draw( screen, FONT_WHITE_TEXT, (const char *)statusText.c_str(), (240 - FontHandler::TextWidth( FONT_WHITE_TEXT,(const char *)statusText.c_str() ))/2, 120 + (2*FontHandler::FontHeight( FONT_WHITE_TEXT ))+6 );
//		Menu::scoreFont.PutString( screen, (240 - Menu::scoreFont.TextWidth( (const char *)statusText ))/2, 120 + (Menu::scoreFont.FontHeight()*2)+6, (const char *)statusText );


	// Loop round each menu option and draw it
	int y = 155;
	list<MenuOption *>::iterator it;
	for ( it = currentMenu->listItems.begin(); it != currentMenu->listItems.end() ; ++it )
	{
		y += (*it)->draw( screen, y ) + 2;
	}
}

int Menu :: handleKeys( SDL_KeyboardEvent &key )
{
	if ( key.type != SDL_KEYDOWN )
		return -1;

	switch( key.keysym.sym )
	{
		case SDLK_DOWN:
		{
			// Move to next menu item
			currentMenu->currentMenuOption->highlight( false );

			list<MenuOption *>::iterator it;
			for ( it = currentMenu->listItems.begin(); it != currentMenu->listItems.end() ; ++it )
			{
				if ( (*it) == currentMenu->currentMenuOption )
				{
					it++;
					break;
				}
			}

			if ( it == currentMenu->listItems.end() )
				it = currentMenu->listItems.begin();

			currentMenu->currentMenuOption = *it;
			currentMenu->currentMenuOption->highlight( true );

			break;
		}
		case SDLK_UP:
		{
			// Move to previous menu item
			currentMenu->currentMenuOption->highlight( false );
			list<MenuOption *>::iterator it;
			bool reachedBeginning = false;
			for ( it = (currentMenu->listItems).end()--;  ; --it )
			{
				if ( (*it) == currentMenu->currentMenuOption )
				{

					if ( it == currentMenu->listItems.begin( ) )
					{
						reachedBeginning = true;
						break;
					}
					else
						it--;
					break;
				}

			}

			if ( reachedBeginning )
				currentMenu->currentMenuOption = currentMenu->listItems.back();
			else
				currentMenu->currentMenuOption = *it;

			currentMenu->currentMenuOption->highlight( true );

			break;
		}
		case SDLK_LEFT:
			if ( currentMenu->parentMenu != 0 )
			{
            	statusText = "";
				currentMenu = currentMenu->parentMenu;

				return -1;
			}
			break;

		case SDLK_RETURN:
		case SDLK_SPACE:
		{
        	statusText = "";
			// select menu item
			int id = currentMenu->currentMenuOption->getMenuId();

			// if the current item has a child menu then move to that menu
			Menu *next = currentMenu->currentMenuOption->getNextMenu();
			if ( next != 0 )
			{
				bool down = currentMenu->currentMenuOption->isDownMenuTree();
				currentMenu = next;
				if ( down )
					initCurrentMenu();
			}

			return id;

			break;
		}

		default:
			break;
	}

	return -1;
}

MenuOption *Menu :: addMenuOption( string text, int id )
{
	MenuOption *item = new MenuOption( text, id );

	listItems.push_back( item );

	return item;
}

void Menu :: resetToTopMenu()
{
	currentMenu = mainMenu;
	initCurrentMenu();
}

void Menu :: initCurrentMenu()
{
	if ( currentMenu->currentMenuOption != 0 )
		currentMenu->currentMenuOption->highlight( false );
	currentMenu->currentMenuOption = currentMenu->listItems.front();
	currentMenu->currentMenuOption->highlight( true );
}