summaryrefslogtreecommitdiff
path: root/src/game/interface.cpp
blob: d1734afc9a20928d214c8f8d825796c2fd893256 (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
#include "game.hpp"
#include <iomanip>

namespace interface {

state_t::state_t(sf::RenderWindow *window_, game::state_t *game_)
{
	window = window_;
	game = game_;
}

static sf::Vector2f compute_pan(sf::RenderWindow *window, sf::Vector2f pan_ref)
{
	sf::Vector2i mouse = sf::Mouse::getPosition(*window);
	sf::Vector2f vmouse = window->mapPixelToCoords(mouse);
	return -(vmouse - pan_ref);
}

void state_t::tick(double dt)
{
	sf::Vector2u size;
	sf::Event event;
	sf::Vector2f view_size;
	v2f_t wmouse; // Mouse position in world space;

	size = window->getSize();

	camera.zoom = expfade(camera.zoom, camera.target_zoom, 15, dt);

	{
		float view_scale;

		view_scale = 3 * exp(camera.zoom * 0.12);
		if (size.x > size.y) {
			view_size.y = view_scale;
			view_size.x = view_scale * size.x / size.y;
		} else {
			view_size.x = view_scale;
			view_size.y = view_scale * size.y / size.x;
		}

		window->setView(sf::View(camera.center, view_size));
	}


	while (window->pollEvent(event)) {
		// FIXME: refactor this nested switch clusterfuck
		switch (event.type)  {
		case sf::Event::Closed:
			window->close();
			return;

		case sf::Event::MouseButtonPressed:
			wmouse = window->mapPixelToCoords(sf::Vector2i(event.mouseButton.x, event.mouseButton.y));
 
			switch (event.mouseButton.button) {
			case sf::Mouse::Button::Left:
				select.selecting = true;
				select.rect[0] = wmouse;
				select.rect[1] = wmouse;
				break;

			case sf::Mouse::Button::Right:
				game->command(wmouse);
				break;

			case sf::Mouse::Button::Middle:
				camera.panning = true;
				camera.pan_ref = wmouse;
				break;

			default:;
			}
			break;

		case sf::Event::MouseButtonReleased:
			switch (event.mouseButton.button) {
			case sf::Mouse::Button::Left:
				if (select.selecting)
					game->select(select.rect);
				select.selecting = false;
				break;

			case sf::Mouse::Button::Middle:
				if (camera.panning)
					camera.center += compute_pan(window, camera.pan_ref);
				camera.panning = false;
				break;

			default:;
			}
			break;

		case sf::Event::MouseWheelScrolled:
			camera.target_zoom -= event.mouseWheelScroll.delta;
			if (camera.target_zoom < 0)
				camera.target_zoom = 0;
			if (camera.target_zoom > 17)
				camera.target_zoom = 17;
			break;

		case sf::Event::KeyPressed:
			switch (event.key.code) {
			case sf::Keyboard::Key::F1:
				debug_draw_cmodels ^= 1;
				print("debug_draw_cmodels = " + std::to_string(debug_draw_cmodels));
				break;

			case sf::Keyboard::Key::F2:
				debug_draw_paths ^= 1;
				print("debug_draw_paths = " + std::to_string(debug_draw_paths));
				break;

			case sf::Keyboard::Key::F3:
				debug_draw_tile_coords ^= 1;
				print("debug_draw_tile_coords = " + std::to_string(debug_draw_tile_coords));
				break;

			case sf::Keyboard::Key::F4:
				debug_AI ^= 1;
				print("debug_AI = " + std::to_string(debug_AI));
				break;
			default:;
			}
		default:;
		}
	}

	if (camera.panning) {
		sf::Vector2f delta = compute_pan(window, camera.pan_ref);
		window->setView(sf::View(camera.center + delta, view_size));
	} else
		window->setView(sf::View(camera.center, view_size));

	// Compute this _after_ the setView above.
	wmouse = window->mapPixelToCoords(sf::Mouse::getPosition(*window));

	if (select.selecting)
		select.rect[1] = wmouse;
}

void state_t::print(std::string str)
{
	log.push_back((log_entry_t){game->now, str});
	std::cout << str << std::endl;
}

void state_t::render_to(render::state_t *render)
{
	size_t w = window->getSize().x, h = window->getSize().y;
	v2f_t x;
	std::stringstream ss;
	double fps;

	if (select.selecting)
		render->render_hlrect(select.rect, sf::Color(200, 100, 50));

	window->setView(sf::View(sf::FloatRect(0, 0, w, h)));
	em = std::max(w, h) * 0.017;

	for (auto i = log.begin(); i != log.end(); ) {
		if (i->time + 3 < game->now)
			i = log.erase(i);
		else
			i++;
	}

	x = v2f_t(0.0f, 0.0f);
	for (log_entry_t &entry : log) {
		render->render_text(x, em, entry.text, render::ALIGN_LEFT_TOP, sf::Color::White);
		x[1] += em;
	}

	x = v2f_t(0.0f, h - em * 1.5);
	ss << "S/T/E: ";
	ss << render->stats.sectors << "/";
	ss << render->stats.tiles << "/";
	ss << render->stats.entities;
	render->render_text(x, em, ss.str(), render::ALIGN_LEFT_TOP, sf::Color::White);

	perf_hist_index = (perf_hist_index + 1) % count(perf_hist);
	perf_hist[perf_hist_index] = 1.0 / render->dt;

	fps = 0.0;
	for (size_t i = 0; i < count(perf_hist); i++)
		fps += perf_hist[i];
	fps /= count(perf_hist);

	x[1] -= em;
	ss.str(std::string());
	ss << std::fixed << std::setprecision(1);
	ss << "FPS: " << fps;
	render->render_text(x, em, ss.str(), render::ALIGN_LEFT_TOP, sf::Color::White);
}

} // namespace interface