File: host_item.cpp

package info (click to toggle)
btanks 0.9.8083-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 43,616 kB
  • sloc: cpp: 46,425; ansic: 12,005; xml: 4,262; python: 313; sh: 13; makefile: 13
file content (81 lines) | stat: -rw-r--r-- 2,068 bytes parent folder | download | duplicates (5)
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
#include "host_item.h"
#include "label.h"
#include "resource_manager.h"
#include "i18n.h"
#include "rt_config.h"
#include "sdlx/font.h"

HostItem::HostItem() : ping(0), players(0), slots(0), game_type(GameTypeDeathMatch), 
	_line(new Label("small", "")), _font(ResourceManager->loadFont("small", true)), timer(0) {
	add(0, 0, _line);
}

void HostItem::start(float t) {
	timer = t;
}

void HostItem::tick(const float dt) {
	Container::tick(dt);
	if (timer > 0) {
		timer -= dt;
		if (timer < 0) 
			timer = 0;
	}
}


static const char * get_type(const GameType game_type) {
	switch(game_type) {
	case GameTypeDeathMatch: return "deathmatch"; 
	case GameTypeCooperative: return "cooperative";
	case GameTypeRacing: return "racing";
	case GameTypeCTF: return "ctf"; 
	case GameTypeTeamDeathMatch: return "team-deathmatch";
	default: 
		return "**invalid**";
	}
}

void HostItem::render(sdlx::Surface &surface, const int x, const int y) const {
	Container::render(surface, x, y);
	
	if (timer > 0) {
		int w, h; 
		get_size(w, h);
	
		//const char * slash = "|\\-/";
		//_font->render(surface, x + w, y, std::string(&slash[((int)(timer * 10)) % 4], 1));
		int idx = 8 - (((int)(timer * 5)) % 8);
		std::string str = "-----";
		if (idx >= 5) {
			idx = 8 - idx;
		}
		str[idx] = '=';
		_font->render(surface, x + w, y, str);
	}
}

void HostItem::update() {
	std::string prefix = slots != 0? mrt::format_string("[%d/%d] ", players, slots) : std::string("[-/-] ");
	std::string mapstr;
	
	if (ping > 0) {
		_line->setFont("small_green");
		mapstr = "[";
		if (!map.empty())
			mapstr += mrt::format_string("%s: %s (%s), ", I18n->get("menu", "map").c_str(), map.c_str(), get_type(game_type));
		mapstr += mrt::format_string("%s: %d ms]", I18n->get("menu", "ping").c_str(), ping - 1);
	} else {
		_line->setFont("small");
	}
	
	std::string hoststr = name, ip = addr.getAddr(addr.port != RTConfig->port);
	if (hoststr.empty()) {
		hoststr = ip;
	} else if (!ip.empty()) {
		hoststr += " (" + ip + ") ";
	}
	hoststr += "  ";
	_line->set(prefix + hoststr + mapstr);
	timer = 0;
}