File: config.cc

package info (click to toggle)
xgalaga%2B%2B 0.8.4-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 336 kB
  • ctags: 557
  • sloc: cpp: 2,715; makefile: 128
file content (98 lines) | stat: -rw-r--r-- 1,947 bytes parent folder | download | duplicates (3)
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
#include "config.h"
#include "gfxinterface.h"
#include "instance.h"
#include <pwd.h>
#include <cstdlib>
#include <sys/types.h>
#include <unistd.h>


namespace {

std::string GetUserFullName()
{
	const passwd *const pwd (getpwuid(getuid()));
	const char * gecos (pwd->pw_gecos);
	if (!gecos || !gecos[0]) return pwd->pw_name;
	while (*gecos && *gecos != ',') ++gecos;
	return std::string(pwd->pw_gecos, gecos - pwd->pw_gecos);
}


std::string PlayerName()
{
	const char *const env_name (std::getenv("XGAL_PSEUDO"));
	return env_name ? env_name : GetUserFullName();
}


#define TO_STRING(s) #s
#define MAKE_STRING(s) TO_STRING(s)

#ifndef HIGH_SCORES_FILE
	#define HIGH_SCORES_FILE .xgalaga++.scores
#endif

// If absolute path, return as-is, else prefix with home dir.
std::string ScoreFileName()
{
	static const char scores_file_name[] = MAKE_STRING(HIGH_SCORES_FILE);
	if (scores_file_name[0] != '/') {
		const passwd *const pwd (getpwuid(getuid()));
		if (pwd->pw_dir) return std::string(pwd->pw_dir) + '/' + scores_file_name;
	}
	return scores_file_name;
}

}



/*
 * Config
 */

Config * Config::singleton_ (0);


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


Config::Config()
: details_level_  (max_details)
, player_name_    (PlayerName())
, score_file_name_(ScoreFileName())
{
}


void Config::AddDetailsLevel(int i) {
	if (details_level_ + i >= min_details &&
	    details_level_ + i <= max_details) {
		details_level_ = details_level_ + i;
	}
}	



/*
 *
 */

void SetStandardWindowSize(unsigned win_size)
{
	static const unsigned widths[]  = { 640, 800, 1024, 1280, 1600 };
	static const unsigned heights[] = { 480, 600,  768, 1024, 1200 };
	if (win_size > 0 && win_size <= sizeof widths / sizeof widths[0]) {
		--win_size;
		X11::Inst().ResizeWindow(widths[win_size], heights[win_size]);
		X11::Inst().ClearWindow();
	}
}