File: menu.cc

package info (click to toggle)
xgalaga%2B%2B 0.9-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 376 kB
  • sloc: cpp: 2,785; makefile: 242
file content (315 lines) | stat: -rw-r--r-- 8,796 bytes parent folder | download | duplicates (2)
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
#include "menu.h"
#include "config.h"
#include "constants.h"
#include "gfxinterface.h"
#include "highscore.h"
#include "input.h"
#include "instance.h"
#include "pix.h"
#include "score.h"
#include "stars.h"
#include "time_util.h"
#include "xpms.h"
#include <cstring>
#include <iomanip>
#include <stdexcept>
#include <sstream>


StartMenu * StartMenu::singleton_ (0);


StartMenu & StartMenu::Instance() {
	if (!singleton_) {
		singleton_ = new StartMenu;
		InstancesManager::Instance().Push(DestroyInstance);
	}
	return *singleton_;
}


const char *const menu_lines[] = {
	"XGalaga++ v0.9 (2017-12-10)",
	"2003-2017 Marc Mongenet, graphics from XGalaga 2.0.34",
	"This game is free software, covered by the GPL.",
	"",
	"Welcome player %p!",
	"",
	"KEY                 ACTION",
	"s                   start game",
	"q                   quit",
	"h                   toggle high scores / menu",
	"p                   pause",
	"1,2,3,4,5           set window size",
	"+                   increase details",
	"-                   decrease details",
	"r                   refresh rate %r1->%r2 Hz",
	"<space bar>         fire",
	"<left/right arrow>  move left/right",
	"<up/down arrow>     scroll high scores up/down",
	"",
	"BONUS",
	"",
	"extra shield every 1000 points"
};


StartMenu::StartMenu()
: font_(X11::Inst().LoadQueryFont("*-fixed-bold-r-*-*-14-*-*-*-*-*-iso8859-1"))
, text_width_      (0)
, wait_scores_     (5000)
, print_scores_    (false)
, scroll_scores_   (0)
, can_scroll_      (true)
, last_window_size_(0, 0)
, rGb_             (0)
, rGb_add_         (1024)
{
	if (!font_) {
		font_ = X11::Inst().LoadQueryFont("*-fixed-*");
		if (!font_) throw std::runtime_error ("X font error");
	}
	for (size_t i (0); i < sizeof menu_lines / sizeof menu_lines[0]; ++i) {
		text_width_ = std::max(text_width_, XTextWidth(font_, menu_lines[i], std::strlen(menu_lines[i])));
	}
}


StartMenu::~StartMenu()
{
	X11::Inst().FreeFont(font_);
}


bool StartMenu::Display()
{
	Input input;
	bool updated (false);
	double frame_time (0);

	for (;;) {

		input.Update();

		if (input.Quit()) return false;

		if (input.Start()) {
			print_scores_ = true;
			wait_scores_ = 5000;
			return true;
		}

		Config::Instance().AddDetailsLevel(input.Details());
		
		if (input.IncRefreshRate()) {
			Config::Instance().SetNextRefreshRate();
			X11::Inst().ClearWindow();
		}
		
		SetStandardWindowSize(input.WindowSize());
		X11::Inst().GetWindowAttributes();

		if (--wait_scores_ < 0 || input.HighScores()) {
			print_scores_ = !print_scores_;
			wait_scores_ = 1000;
			X11::Inst().ClearWindow();
		}

		StarsFields::Instance().Scroll();
		Score::Instance().Refresh();

		if (last_window_size_ != Coord(X11::Inst().WindowWidth(), X11::Inst().WindowHeight())) {
			last_window_size_ = Coord(X11::Inst().WindowWidth(), X11::Inst().WindowHeight());
			scroll_scores_ = 0;
		}

		if (print_scores_) {
			try {
				if (!updated) {
					HighScores::Instance().Update();
					updated = true;
				}
				if (input.VMove()) {
					if (can_scroll_ || input.VMove() < 0) scroll_scores_ += input.VMove();
					if (scroll_scores_ < 0) scroll_scores_ = 0;
					wait_scores_ = 1000;
				}
				PrintScores(NextColor());
			} catch (const std::exception & e) {
				X11::Inst().SetForeground(X11::Inst().GetWhite());
				X11::Inst().SetClipMask(None);
				X11::Inst().SetFont(font_->fid);
				X11::Inst().DrawString(Coord(1, last_window_size_.y / 2), e.what());
			}
		}
		else PrintHelp(NextColor());

		X11::Inst().Sync(False);

		frame_time = SleepTimeInterval(frame_time, 1.0 / Config::Instance().RefreshRate());
	}
}


XColor StartMenu::NextColor()
{
	XColor color;
	color.red = 48*1024;
	color.blue = 65535;
	rGb_ += rGb_add_;
	if (rGb_ == 0) {
		rGb_add_ = -rGb_add_;
		rGb_ += rGb_add_;
	}
	color.green = rGb_;
	return color;
}


void StartMenu::SetFontContext(XColor & color) const
{
	X11::Inst().AllocColorAlways(&color);
	X11::Inst().SetForeground(color.pixel);
	X11::Inst().SetClipMask(None);
	X11::Inst().SetFont(font_->fid);
}


void StartMenu::PrintHelp(XColor color)
{
	Coord pos (std::max(0, (last_window_size_.x - text_width_) / 2), 20);
	SetFontContext(color);

	// Draw help text
	for (size_t i (0); i < sizeof menu_lines / sizeof menu_lines[0]; ++i) {
		std::string line (menu_lines[i]);

		while (line.find("%p") != std::string::npos) {
			line.replace(line.find("%p"), 2, Config::Instance().GetPlayerName());
		}

		std::ostringstream refresh_rate;
		refresh_rate << Config::Instance().RefreshRate();
		while (line.find("%r1") != std::string::npos) {
			line.replace(line.find("%r1"), 3, refresh_rate.str().c_str());
		}

		std::ostringstream next_refresh_rate;
		next_refresh_rate << Config::Instance().NextRefreshRate();
		while (line.find("%r2") != std::string::npos) {
			line.replace(line.find("%r2"), 3, next_refresh_rate.str().c_str());
		}

		X11::Inst().DrawString(pos, line);
		
		pos.y += LineHeight();
		if (line.empty()) {
			color.red -= 8192;
			X11::Inst().AllocColorAlways(&color);
			X11::Inst().SetForeground(color.pixel);
		}
	}
	// Draw bonus pixs with help text
	static const char *const *const bonuses[] =
		{ bonus_speed_xpm, bonus_fire_xpm, bonus_shield_xpm, bonus_multi_xpm };
	static const char *const bonus_text[] =
		{ "extra speed", "extra fire", "extra shield", "multi fire" };
	for (size_t i (0); i < sizeof bonuses / sizeof bonuses[0]; ++i) {
		const Pix *const bonus (PixKeeper::Instance().Get(bonuses[i]));
		bonus->Draw(pos + Coord(bonus->Width() / 2, -bonus->Height() / 2));
		X11::Inst().SetClipMask(None);
		X11::Inst().DrawString(pos + Coord(2 * bonus->Width(), 0), bonus_text[i]);
		pos.y += std::max(bonus->Height(), LineHeight());
	}
}


void StartMenu::PrintScores(XColor color)
{
	Coord pos (std::max(0, (last_window_size_.x - text_width_) / 2), 20);
	SetFontContext(color);
	// Draw high scores title
	std::ostringstream ost;
	ost <<"High scores for "
	    <<last_window_size_.x
	    <<''
	    <<last_window_size_.y
	    <<'@'
	    <<Config::Instance().RefreshRate()
	    <<" window";
	X11::Inst().DrawString(pos, ost.str().c_str());
	pos.y += LineHeight();
	ost.str(std::string(ost.str().size(), ''));
	X11::Inst().DrawString(pos, ost.str().c_str());
	pos.y += LineHeight();

	// get high scores for current window size
	const std::multiset<HighScore> *const high_scores (HighScores::Instance().Get(last_window_size_, Config::Instance().RefreshRate()));
	if (!high_scores) {
		score_lines_.clear();
		ost.str("None.");
		X11::Inst().DrawString(pos, ost.str().c_str());
		return;
	}
	// Display high scores
	// The first line is always displayed
	// The best player is always displayed (on last line if necessary).
	int rank (0);
	size_t line (0);
	int last_score (-1);
	const std::string player_name (Config::Instance().GetPlayerName());
	bool look_for_player (false);
	bool player_displayed (false);
	for (std::multiset<HighScore>::const_reverse_iterator it (high_scores->rbegin());
	     (can_scroll_ = it != high_scores->rend()) &&
	     (look_for_player || pos.y < last_window_size_.y - LineHeight());
	     ++it) {
		++rank;
		if ((rank > scroll_scores_ && !look_for_player) ||
		    (look_for_player && it->Name() == player_name)) {
			// Construct this line
			ost.str("");
			if (it->Value() != last_score) {
				last_score = it->Value();
				ost << std::setfill(' ') << std::setw(2) << rank << ". ";
			} else {
				ost << "    ";
			}
			char score_date[20];
			const time_t score_date_tm = it->Date();
			
			strftime(score_date, sizeof score_date, "%x", localtime(&score_date_tm));
			// Null score date means no date recorded -> deplace date by spaces.
			if (it->Date() == 0) {
				for (int i = 0; score_date[i] != '\0'; ++i) {
					score_date[i] = ' ';
				}
			}
			ost << std::setfill(' ') << std::setw(6) << last_score
			    << "   " << score_date << ' ' << it->Name();
			// Undraw old line if is different
			const std::string score_line (ost.str());
			if (line == score_lines_.size()) score_lines_.push_back(score_line);
			else if (score_lines_[line] != score_line) {
				X11::Inst().SetForeground(X11::Inst().GetBlack());
				X11::Inst().DrawString(pos, score_lines_[line].c_str());
				score_lines_[line] = score_line;
			}
			// text color with special color for current player
			if (it->Name() == player_name) {
				player_displayed = true;
				if (color.green) color.green += (65535 - color.green) / 2;
			}
			if (color.red >= 1024) color.red -= 1024;
			X11::Inst().AllocColorAlways(&color);
			X11::Inst().SetForeground(color.pixel);
			if (it->Name() == player_name) {
				if (color.green) color.green -= 65535 - color.green;
			}
			X11::Inst().DrawString(pos, score_line.c_str());
			++line;
			pos.y += LineHeight();
			look_for_player = !player_displayed && pos.y >= last_window_size_.y - 2 * LineHeight();
		}
	}
}