Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: ae4f232849246f778bf6ad4d4e551b30642123c4 (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
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
/*
   client/client.cc
   This file is part of the Osirion project and is distributed under 
   the terms and conditions of the GNU General Public License version 2 
*/

#include <SDL/SDL.h>

#include <iostream>
#include <cmath>
#include <iomanip>

#include "audio/audio.h"
#include "audio/sources.h"
#include "client/client.h"
#include "client/video.h"
#include "client/console.h"
#include "client/input.h"
#include "client/view.h"
#include "core/core.h"
#include "core/zone.h"
#include "render/render.h"
#include "ui/ui.h"

namespace client 
{

core::Cvar *cl_framerate = 0;
core::Cvar *cl_frametime = 0;

Client app;

//--- engine functions --------------------------------------------

void func_snd_restart(std::string const &args)
{
	// unload entity sounds
	for (core::Entity::Registry::iterator it = core::Entity::registry().begin(); it != core::Entity::registry().end(); it++) {
		core::Entity *entity = (*it).second;
		if (entity->state())
			entity->state()->clearsound();
	}

	audio::shutdown();

	audio::init();
}

void func_r_restart(std::string const &args)
{
	video::restart();
}

void func_ui_chat(std::string const &args)
{
	if (core::application()->connected()) {
		client()->view()->chat()->toggle();
	}
}

//--- public ------------------------------------------------------

void client_main(int count, char **arguments)
{
	std::cout << core::name() << " " << core::version() << std::endl;

	for (int i =0; i < count; i++)
		std::cout << arguments[i] << " ";
	std::cout << std::endl;

	app.init(count, arguments);
	app.run();
	app.shutdown();
}

Client *client()
{
	return &app;
}

//--- private -----------------------------------------------------

void Client::quit(int status)
{
	SDL_Quit();
	core::Application::quit(status);
}

void Client::init(int count, char **arguments)
{
	con_print << "^BInitializing client..." << std::endl;
	
	// initialize core
	core::Cvar::sv_dedicated = core::Cvar::set("sv_private", "0");
	core::Application::init(count, arguments);
	Console::init();

	// client variables
	core::Cvar *cvar = 0;
	cvar = core::Cvar::get("cl_name", "Player", core::Cvar::Archive | core::Cvar::Info);
	cvar->set_info("[str] player name");

	cvar = core::Cvar::get("cl_color", "1.0 1.0 1.0", core::Cvar::Archive | core::Cvar::Info);
	cvar->set_info("[r g b] player primary color");

	cvar = core::Cvar::get("cl_colorsecond", "1.0 1.0 1.0", core::Cvar::Archive | core::Cvar::Info);
	cvar->set_info("[r g b] player secondary color");
	
	cl_framerate = core::Cvar::get("cl_framerate", "125", core::Cvar::Archive);
	cl_framerate->set_info("[int] client framerate in frames/sec");

	// initialize SDL, but do not initialize any subsystems
	SDL_Init(0);

	// initialize user interface
	ui::init();
	client_view = new View(ui::root());

	// Initialize the video subsystem
	if (!video::init()) {
		quit(1);
	}

	// initialize input
	input::init();

	// initialize audio
	audio::init();
	
	// add engine functions
	core::Func *func = 0;

	func = core::Func::add("r_restart", (core::FuncPtr) func_r_restart);
	func->set_info("restart render subsystem");
	
	func = core::Func::add("ui_chat", func_ui_chat);
	func->set_info("toggle chat window");

	//func = core::Func::add("snd_restart", (core::FuncPtr) func_snd_restart);
	//func->set_info("restart audio subsystem");
}

void Client::run()
{
	con_print << "^BRunning client..." << std::endl;

	// default framerate 125fps, 8 milliseconds
	Uint32 client_frame_lenght = 8;
	
	Uint32 client_current_timestamp = 0;
	Uint32 client_previous_timestamp = 0;
	
	
	console()->flush();
	console()->clear_notify();

	while (true) {
		// current time in microseconds
		client_current_timestamp = SDL_GetTicks();
			
		// calculate the desired frame length
		if (cl_framerate->value() < 0) {
			(*cl_framerate) = 0.0f;
		} else if (cl_framerate->value() > 1000.0f) {
			(*cl_framerate) = 1000.0f;
		}

		if (cl_framerate->value()) {
			client_frame_lenght = (Uint32) roundf(1000.0f / cl_framerate->value());
		} else {
			client_frame_lenght = 0;
		}
		
		// only advance per microsecond frame
		Uint32 d = client_current_timestamp - client_previous_timestamp;
		if ((d > 0)) {
			if (d >= client_frame_lenght) {
				float elapsed = (float)(d) / 1000.f;

				frame(elapsed);	

				client_previous_timestamp = client_current_timestamp;
			} else {
				SDL_Delay(client_frame_lenght - d);
			}
		} else {
			SDL_Delay(1);
		}
	}

}

void Client::frame(float seconds)
{
	core::Application::frame(seconds);

	if (!core::application()->connected()) {
		// load the intro if nothing is running
		if (core::application()->load("intro")) {
			core::application()->connect("");
		}
		// show the console if everything fails
		if (!core::application()->connected() && !console()->visible()) {
			console()->toggle();
		}
	} else {
		// show the main menu on non-interactive modules
		if (!core::game()->interactive() && !ui::root()->active()) {
			ui::root()->show_menu("main");
		}
	}

	video::frame(seconds);
	input::frame(seconds);
}

void Client::shutdown()
{
	con_print << "^BShutting down client..." << std::endl;

	if (connected()) disconnect();

	core::Func::remove("r_restart");
	core::Func::remove("ui_chat");
	//core::Func::remove("snd_restart");

	audio::shutdown();

	Console::shutdown();

	input::shutdown();

	video::shutdown();

	ui::shutdown();

	core::Application::shutdown();

	quit(0);
}

/* -- notifications from core::Application ------------------------- */

void Client::notify_connect()
{
	ui::root()->hide_menu();
}

void Client::notify_disconnect()
{
	// FIXME unload sounds
	render::reset();
	input::reset();
}

void Client::notify_zonechange()
{
	render::unload();
}

void Client::notify_sound(const char * name)
{
	audio::play(name);
}

void Client::notify_message(core::Message::Channel const channel, std::string const message)
{

	switch(channel) {

		case core::Message::Info:	// Info message
			break;

		case core::Message::Local:	// Chat message in the local zone
			break;

		case core::Message::RCon:	// RCon message
			break;

		case core::Message::Public:	// Public chat message
			audio::play("com/chat");
			break;

		case core::Message::Private:	// Private chat message
			audio::play("com/priv");
			break;

		default:
			break;
	}

	con_print << message << std::endl;
	console()->notify(message);
}

/* FIXME

	these notifications are hacks and need to be fixed
*/

void Client::notify_remove_sound(size_t source)
{
	audio::Sources::remove(source);
}

} // namespace client