git repos / blockattack-game

blame: source/code/menudef.cpp

normal view · raw

593aeae4 sago007 2011-06-25 22:42 1
/*
593aeae4 sago007 2011-06-25 22:42 2
menudef.cpp
593aeae4 sago007 2011-06-25 22:42 3
Copyright (C) 2011 Poul Sander
593aeae4 sago007 2011-06-25 22:42 4
593aeae4 sago007 2011-06-25 22:42 5
    This program is free software; you can redistribute it and/or modify
593aeae4 sago007 2011-06-25 22:42 6
    it under the terms of the GNU General Public License as published by
593aeae4 sago007 2011-06-25 22:42 7
    the Free Software Foundation; either version 2 of the License, or
593aeae4 sago007 2011-06-25 22:42 8
    (at your option) any later version.
593aeae4 sago007 2011-06-25 22:42 9
593aeae4 sago007 2011-06-25 22:42 10
    This program is distributed in the hope that it will be useful,
593aeae4 sago007 2011-06-25 22:42 11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
593aeae4 sago007 2011-06-25 22:42 12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
593aeae4 sago007 2011-06-25 22:42 13
    GNU General Public License for more details.
593aeae4 sago007 2011-06-25 22:42 14
593aeae4 sago007 2011-06-25 22:42 15
    You should have received a copy of the GNU General Public License
593aeae4 sago007 2011-06-25 22:42 16
    along with this program; if not, write to the Free Software
593aeae4 sago007 2011-06-25 22:42 17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
593aeae4 sago007 2011-06-25 22:42 18
593aeae4 sago007 2011-06-25 22:42 19
    Poul Sander
593aeae4 sago007 2011-06-25 22:42 20
    blockattack@poulsander.com
593aeae4 sago007 2011-06-25 22:42 21
*/
593aeae4 sago007 2011-06-25 22:42 22
26ddb424 sago007 2011-06-09 20:06 23
#include "global.hpp"
26ddb424 sago007 2011-06-09 20:06 24
#include <iostream>
26ddb424 sago007 2011-06-09 20:06 25
#include <stdlib.h>
26ddb424 sago007 2011-06-09 20:06 26
#include "menu/MenuItem.hpp"
26ddb424 sago007 2011-06-09 20:06 27
#include "MenuSystem.h"
c9dd376f sago007 2012-01-20 17:13 28
#include "common.h"
26ddb424 sago007 2011-06-09 20:06 29
26ddb424 sago007 2011-06-09 20:06 30
using namespace std;
26ddb424 sago007 2011-06-09 20:06 31
26ddb424 sago007 2011-06-09 20:06 32
//Menu
26ddb424 sago007 2011-06-09 20:06 33
void PrintHi()
26ddb424 sago007 2011-06-09 20:06 34
{
c53e6443 sago007 2012-04-17 11:04 35
	cout << "Hi" <<endl;
26ddb424 sago007 2011-06-09 20:06 36
}
26ddb424 sago007 2011-06-09 20:06 37
593aeae4 sago007 2011-06-25 22:42 38
//Stores the controls
593aeae4 sago007 2011-06-25 22:42 39
struct control
593aeae4 sago007 2011-06-25 22:42 40
{
c53e6443 sago007 2012-04-17 11:04 41
	SDLKey up;
c53e6443 sago007 2012-04-17 11:04 42
	SDLKey down;
c53e6443 sago007 2012-04-17 11:04 43
	SDLKey left;
c53e6443 sago007 2012-04-17 11:04 44
	SDLKey right;
c53e6443 sago007 2012-04-17 11:04 45
	SDLKey change;
c53e6443 sago007 2012-04-17 11:04 46
	SDLKey push;
593aeae4 sago007 2011-06-25 22:42 47
};
593aeae4 sago007 2011-06-25 22:42 48
26ddb424 sago007 2011-06-09 20:06 49
bool OpenDialogbox(int x, int y, char *name);
26ddb424 sago007 2011-06-09 20:06 50
void OpenScoresDisplay();
593aeae4 sago007 2011-06-25 22:42 51
extern control keySettings[3];
593aeae4 sago007 2011-06-25 22:42 52
593aeae4 sago007 2011-06-25 22:42 53
//Function to return the name of a key, to be displayed...
593aeae4 sago007 2011-06-25 22:42 54
static string getKeyName(SDLKey key)
593aeae4 sago007 2011-06-25 22:42 55
{
c53e6443 sago007 2012-04-17 11:04 56
	string keyname(SDL_GetKeyName(key));
c53e6443 sago007 2012-04-17 11:04 57
	cout << key << " translated to " << keyname << endl;
c53e6443 sago007 2012-04-17 11:04 58
	return keyname;
593aeae4 sago007 2011-06-25 22:42 59
}
593aeae4 sago007 2011-06-25 22:42 60
c53e6443 sago007 2012-04-17 11:04 61
class Button_changekey : public Button
c53e6443 sago007 2012-04-17 11:04 62
{
593aeae4 sago007 2011-06-25 22:42 63
private:
c53e6443 sago007 2012-04-17 11:04 64
	SDLKey *m_key2change;
c53e6443 sago007 2012-04-17 11:04 65
	string m_keyname;
593aeae4 sago007 2011-06-25 22:42 66
public:
c53e6443 sago007 2012-04-17 11:04 67
	Button_changekey(SDLKey *key, string keyname);
c53e6443 sago007 2012-04-17 11:04 68
	void doAction();
593aeae4 sago007 2011-06-25 22:42 69
};
593aeae4 sago007 2011-06-25 22:42 70
c53e6443 sago007 2012-04-17 11:04 71
Button_changekey::Button_changekey(SDLKey* key, string keyname)
c53e6443 sago007 2012-04-17 11:04 72
{
c53e6443 sago007 2012-04-17 11:04 73
	m_key2change = key;
c53e6443 sago007 2012-04-17 11:04 74
	m_keyname = keyname;
c53e6443 sago007 2012-04-17 11:04 75
	setLabel(m_keyname+" : "+getKeyName(*m_key2change));
593aeae4 sago007 2011-06-25 22:42 76
}
593aeae4 sago007 2011-06-25 22:42 77
c53e6443 sago007 2012-04-17 11:04 78
void Button_changekey::doAction()
c53e6443 sago007 2012-04-17 11:04 79
{
c53e6443 sago007 2012-04-17 11:04 80
	SDL_Event event;
c53e6443 sago007 2012-04-17 11:04 81
c53e6443 sago007 2012-04-17 11:04 82
	bool finnish = false;
c53e6443 sago007 2012-04-17 11:04 83
	while (!finnish)
c53e6443 sago007 2012-04-17 11:04 84
	{
c53e6443 sago007 2012-04-17 11:04 85
		SDL_Delay(10);
c53e6443 sago007 2012-04-17 11:04 86
		while ( SDL_PollEvent(&event) )
c53e6443 sago007 2012-04-17 11:04 87
		{
c53e6443 sago007 2012-04-17 11:04 88
			if (event.type == SDL_KEYDOWN)
c53e6443 sago007 2012-04-17 11:04 89
			{
c53e6443 sago007 2012-04-17 11:04 90
				if (event.key.keysym.sym != SDLK_ESCAPE)
c53e6443 sago007 2012-04-17 11:04 91
					*m_key2change = event.key.keysym.sym;
c53e6443 sago007 2012-04-17 11:04 92
				finnish = true;
c53e6443 sago007 2012-04-17 11:04 93
			}
c53e6443 sago007 2012-04-17 11:04 94
		}
c53e6443 sago007 2012-04-17 11:04 95
	}
c53e6443 sago007 2012-04-17 11:04 96
	setLabel(m_keyname+" : "+getKeyName(*m_key2change));
593aeae4 sago007 2011-06-25 22:42 97
}
26ddb424 sago007 2011-06-09 20:06 98
26ddb424 sago007 2011-06-09 20:06 99
void InitMenues()
26ddb424 sago007 2011-06-09 20:06 100
{
c53e6443 sago007 2012-04-17 11:04 101
	ButtonGfx::setSurfaces(menuMarked,menuUnmarked);
c53e6443 sago007 2012-04-17 11:04 102
	ButtonGfx::thefont = nf_scoreboard_font;
26ddb424 sago007 2011-06-09 20:06 103
}
26ddb424 sago007 2011-06-09 20:06 104
26ddb424 sago007 2011-06-09 20:06 105
void runGame(int gametype);
26ddb424 sago007 2011-06-09 20:06 106
c53e6443 sago007 2012-04-17 11:04 107
void runSinglePlayerEndless(Button* b)
c53e6443 sago007 2012-04-17 11:04 108
{
c53e6443 sago007 2012-04-17 11:04 109
	runGame(0);
26ddb424 sago007 2011-06-09 20:06 110
}
26ddb424 sago007 2011-06-09 20:06 111
c53e6443 sago007 2012-04-17 11:04 112
void runSinglePlayerTimeTrial(Button* b)
c53e6443 sago007 2012-04-17 11:04 113
{
c53e6443 sago007 2012-04-17 11:04 114
	runGame(1);
26ddb424 sago007 2011-06-09 20:06 115
}
26ddb424 sago007 2011-06-09 20:06 116
c53e6443 sago007 2012-04-17 11:04 117
void runSinglePlayerPuzzle(Button* b)
c53e6443 sago007 2012-04-17 11:04 118
{
c53e6443 sago007 2012-04-17 11:04 119
	runGame(3);
26ddb424 sago007 2011-06-09 20:06 120
}
26ddb424 sago007 2011-06-09 20:06 121
c53e6443 sago007 2012-04-17 11:04 122
void runSinglePlayerVs(Button* b)
c53e6443 sago007 2012-04-17 11:04 123
{
c53e6443 sago007 2012-04-17 11:04 124
	runGame(4);
26ddb424 sago007 2011-06-09 20:06 125
}
26ddb424 sago007 2011-06-09 20:06 126
c53e6443 sago007 2012-04-17 11:04 127
void buttonActionMusic(Button* b)
c53e6443 sago007 2012-04-17 11:04 128
{
c53e6443 sago007 2012-04-17 11:04 129
	MusicEnabled = !MusicEnabled;
c53e6443 sago007 2012-04-17 11:04 130
	b->setLabel(MusicEnabled? "Music: On" : "Music: Off");
26ddb424 sago007 2011-06-09 20:06 131
}
26ddb424 sago007 2011-06-09 20:06 132
c53e6443 sago007 2012-04-17 11:04 133
void buttonActionSound(Button* b)
c53e6443 sago007 2012-04-17 11:04 134
{
c53e6443 sago007 2012-04-17 11:04 135
	SoundEnabled = !SoundEnabled;
c53e6443 sago007 2012-04-17 11:04 136
	b->setLabel(SoundEnabled? _("Sound: On") : _("Sound: Off") );
26ddb424 sago007 2011-06-09 20:06 137
}
26ddb424 sago007 2011-06-09 20:06 138
c53e6443 sago007 2012-04-17 11:04 139
void buttonActionFullscreen(Button* b)
c53e6443 sago007 2012-04-17 11:04 140
{
c53e6443 sago007 2012-04-17 11:04 141
	bFullscreen = !bFullscreen;
c53e6443 sago007 2012-04-17 11:04 142
	b->setLabel(bFullscreen? _("Fullscreen: On") : _("Fullscreen: Off") );
c53e6443 sago007 2012-04-17 11:04 143
	ResetFullscreen();
26ddb424 sago007 2011-06-09 20:06 144
}
26ddb424 sago007 2011-06-09 20:06 145
c53e6443 sago007 2012-04-17 11:04 146
void buttonActionPlayer1Name(Button *b)
c53e6443 sago007 2012-04-17 11:04 147
{
c53e6443 sago007 2012-04-17 11:04 148
	if (OpenDialogbox(200,100,player1name))
c53e6443 sago007 2012-04-17 11:04 149
		return; //must save if true
26ddb424 sago007 2011-06-09 20:06 150
}
26ddb424 sago007 2011-06-09 20:06 151
c53e6443 sago007 2012-04-17 11:04 152
void buttonActionPlayer2Name(Button *b)
c53e6443 sago007 2012-04-17 11:04 153
{
c53e6443 sago007 2012-04-17 11:04 154
	if (OpenDialogbox(200,100,player2name))
c53e6443 sago007 2012-04-17 11:04 155
		return; //must save if true
26ddb424 sago007 2011-06-09 20:06 156
}
26ddb424 sago007 2011-06-09 20:06 157
c53e6443 sago007 2012-04-17 11:04 158
void buttonActionHighscores(Button *b)
c53e6443 sago007 2012-04-17 11:04 159
{
c53e6443 sago007 2012-04-17 11:04 160
	OpenScoresDisplay();
26ddb424 sago007 2011-06-09 20:06 161
}
26ddb424 sago007 2011-06-09 20:06 162
c53e6443 sago007 2012-04-17 11:04 163
static void ChangeKeysMenu(long playernumber)
c53e6443 sago007 2012-04-17 11:04 164
{
c53e6443 sago007 2012-04-17 11:04 165
	Menu km(&screen,_("Change key bindings"),true);
c53e6443 sago007 2012-04-17 11:04 166
	Button_changekey bLeft(&keySettings[playernumber].left,_("Left") );
c53e6443 sago007 2012-04-17 11:04 167
	Button_changekey bRight(&keySettings[playernumber].right,_("Right") );
c53e6443 sago007 2012-04-17 11:04 168
	Button_changekey bUp(&keySettings[playernumber].up,_("Up") );
c53e6443 sago007 2012-04-17 11:04 169
	Button_changekey bDown(&keySettings[playernumber].down,_("Down") );
c53e6443 sago007 2012-04-17 11:04 170
	Button_changekey bPush(&keySettings[playernumber].push,_("Push") );
c53e6443 sago007 2012-04-17 11:04 171
	Button_changekey bSwitch(&keySettings[playernumber].change,_("Change") );
c53e6443 sago007 2012-04-17 11:04 172
	km.addButton(&bLeft);
c53e6443 sago007 2012-04-17 11:04 173
	km.addButton(&bRight);
c53e6443 sago007 2012-04-17 11:04 174
	km.addButton(&bUp);
c53e6443 sago007 2012-04-17 11:04 175
	km.addButton(&bDown);
c53e6443 sago007 2012-04-17 11:04 176
	km.addButton(&bPush);
c53e6443 sago007 2012-04-17 11:04 177
	km.addButton(&bSwitch);
c53e6443 sago007 2012-04-17 11:04 178
	km.run();
c53e6443 sago007 2012-04-17 11:04 179
}
c53e6443 sago007 2012-04-17 11:04 180
c53e6443 sago007 2012-04-17 11:04 181
static void ChangeKeysMenu1(Button *b)
c53e6443 sago007 2012-04-17 11:04 182
{
c53e6443 sago007 2012-04-17 11:04 183
	ChangeKeysMenu(0);
593aeae4 sago007 2011-06-25 22:42 184
}
593aeae4 sago007 2011-06-25 22:42 185
c53e6443 sago007 2012-04-17 11:04 186
static void ChangeKeysMenu2(Button *b)
c53e6443 sago007 2012-04-17 11:04 187
{
c53e6443 sago007 2012-04-17 11:04 188
	ChangeKeysMenu(2);
593aeae4 sago007 2011-06-25 22:42 189
}
fc8e3d6a sago007 2011-06-25 16:31 190
c53e6443 sago007 2012-04-17 11:04 191
void ConfigureMenu(Button *b)
c53e6443 sago007 2012-04-17 11:04 192
{
c53e6443 sago007 2012-04-17 11:04 193
	Menu cm(&screen,_("Configuration"),true);
c53e6443 sago007 2012-04-17 11:04 194
	Button bMusic,bSound,buttonFullscreen,bPlayer1Name,bPlayer2Name;
c53e6443 sago007 2012-04-17 11:04 195
	Button bPlayer1Keys, bPlayer2Keys;
c53e6443 sago007 2012-04-17 11:04 196
	bMusic.setLabel(MusicEnabled? _("Music: On") : _("Music: Off") );
c53e6443 sago007 2012-04-17 11:04 197
	bMusic.setAction(buttonActionMusic);
c53e6443 sago007 2012-04-17 11:04 198
	bSound.setLabel(SoundEnabled? _("Music: On") : _("Music: Off") );
c53e6443 sago007 2012-04-17 11:04 199
	bSound.setAction(buttonActionSound);
c53e6443 sago007 2012-04-17 11:04 200
	buttonFullscreen.setLabel(bFullscreen? _("Fullscreen: On") : _("Fullscreen: Off") );
c53e6443 sago007 2012-04-17 11:04 201
	buttonFullscreen.setAction(buttonActionFullscreen);
c53e6443 sago007 2012-04-17 11:04 202
	bPlayer1Name.setAction(buttonActionPlayer1Name);
c53e6443 sago007 2012-04-17 11:04 203
	bPlayer1Name.setLabel(_("Change player 1's name") );
c53e6443 sago007 2012-04-17 11:04 204
	bPlayer2Name.setAction(buttonActionPlayer2Name);
c53e6443 sago007 2012-04-17 11:04 205
	bPlayer2Name.setLabel(_("Change player 2's name") );
c53e6443 sago007 2012-04-17 11:04 206
	bPlayer1Keys.setAction(ChangeKeysMenu1);
c53e6443 sago007 2012-04-17 11:04 207
	bPlayer1Keys.setLabel(_("Change player 1's keys") );
c53e6443 sago007 2012-04-17 11:04 208
	bPlayer2Keys.setAction(ChangeKeysMenu2);
c53e6443 sago007 2012-04-17 11:04 209
	bPlayer2Keys.setLabel(_("Change player 2's keys") );
c53e6443 sago007 2012-04-17 11:04 210
	cm.addButton(&bMusic);
c53e6443 sago007 2012-04-17 11:04 211
	cm.addButton(&bSound);
c53e6443 sago007 2012-04-17 11:04 212
	cm.addButton(&buttonFullscreen);
c53e6443 sago007 2012-04-17 11:04 213
	cm.addButton(&bPlayer1Name);
c53e6443 sago007 2012-04-17 11:04 214
	cm.addButton(&bPlayer2Name);
c53e6443 sago007 2012-04-17 11:04 215
	cm.addButton(&bPlayer1Keys);
c53e6443 sago007 2012-04-17 11:04 216
	cm.addButton(&bPlayer2Keys);
c53e6443 sago007 2012-04-17 11:04 217
	cm.run();
26ddb424 sago007 2011-06-09 20:06 218
}
26ddb424 sago007 2011-06-09 20:06 219
26ddb424 sago007 2011-06-09 20:06 220
void MainMenu()
26ddb424 sago007 2011-06-09 20:06 221
{
c53e6443 sago007 2012-04-17 11:04 222
	InitMenues();
c53e6443 sago007 2012-04-17 11:04 223
	Menu m(&screen,_("Block Attack - Rise of the blocks"),false);
c53e6443 sago007 2012-04-17 11:04 224
	Button bHi,bTimetrial1, bPuzzle, bVs1, bConfigure,bHighscore;
c53e6443 sago007 2012-04-17 11:04 225
	bHi.setLabel(_("Single player - endless") );
c53e6443 sago007 2012-04-17 11:04 226
	bHi.setAction(runSinglePlayerEndless);
c53e6443 sago007 2012-04-17 11:04 227
	bTimetrial1.setLabel(_("Single player - time trial") );
c53e6443 sago007 2012-04-17 11:04 228
	bTimetrial1.setAction(runSinglePlayerTimeTrial);
c53e6443 sago007 2012-04-17 11:04 229
	bPuzzle.setLabel(_("Single player - puzzle mode") );
c53e6443 sago007 2012-04-17 11:04 230
	bPuzzle.setAction(runSinglePlayerPuzzle);
c53e6443 sago007 2012-04-17 11:04 231
	bVs1.setLabel(_("Single player - vs") );
c53e6443 sago007 2012-04-17 11:04 232
	bVs1.setAction(runSinglePlayerVs);
c53e6443 sago007 2012-04-17 11:04 233
	bConfigure.setLabel(_("Configure") );
c53e6443 sago007 2012-04-17 11:04 234
	bConfigure.setAction(ConfigureMenu);
c53e6443 sago007 2012-04-17 11:04 235
	bHighscore.setLabel(_("Highscores") );
c53e6443 sago007 2012-04-17 11:04 236
	bHighscore.setAction(buttonActionHighscores);
c53e6443 sago007 2012-04-17 11:04 237
	m.addButton(&bHi);
c53e6443 sago007 2012-04-17 11:04 238
	m.addButton(&bTimetrial1);
c53e6443 sago007 2012-04-17 11:04 239
	m.addButton(&bPuzzle);
c53e6443 sago007 2012-04-17 11:04 240
	m.addButton(&bVs1);
c53e6443 sago007 2012-04-17 11:04 241
	m.addButton(&bConfigure);
c53e6443 sago007 2012-04-17 11:04 242
	m.addButton(&bHighscore);
c53e6443 sago007 2012-04-17 11:04 243
	m.run();
593aeae4 sago007 2011-06-25 22:42 244
}
1970-01-01 00:00 245