Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 8970deac0f59b7b44eb23acc3e13b5359106247d (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
/*
   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 
*/

// project headers
#include "client/client.h"
#include "client/video.h"
#include "client/camera.h"
#include "client/console.h"
#include "client/input.h"
#include "client/view.h"
#include "core/core.h"
#include "game/game.h"

// SDL headers
#include <SDL/SDL.h>

// C++ headers
#include <iostream>
#include <cmath>

namespace client 
{

//--- private definition ------------------------------------------
/// client application implementation
class Client : public core::Application
{
public:
	/// initialize the client Client
	virtual void init();

	/// run the client Client
	virtual void run();

	/// shutdown the client Client
	virtual void shutdown();

	/// quit the client Client
	virtual void quit(int status);
};

Client app;

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

extern "C" void func_r_restart(std::stringstream &args)
{
	video::shutdown();
	console::flush();

	if (!video::init()) {
		console::flush();
		app.quit(1);
	}
}

//--- public ------------------------------------------------------
game::Game game;

void main(int count, char **arguments)
{
	std::cout << "The Osirion Project " << VERSION << std::endl;
	for (int i =0; i < count; i++)
		std::cout << arguments[i] << " ";
	std::cout << std::endl;

	app.init();
	app.run();
	app.shutdown();
}

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

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

void Client::init()
{
	con_print << "Initializing client..." << std::endl;
	// initialize core
	core::Application::init();

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

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

	// initialize console
	console::init();
	
	// initialize input
	input::init();
	
	// add engine functions
	core::func::add("r_restart", func_r_restart);
}

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

	Uint32 chrono = SDL_GetTicks();

	while (true) {
		Uint32 current = SDL_GetTicks();

		// overflow protection ~49 days
		if (current < chrono) {
			chrono = current;
		}

		// run a core frame
		float seconds = ((float)(current - chrono)) / 1000.0f;
		core::Application::frame(seconds);
		
		// run a video frame
		video::frame(seconds);
		
		// process input
		input::frame(seconds);
		
		// update the main loop chronometer
		chrono = current;
	}

}

void Client::shutdown()
{
	con_print << "Shutting down client..." << std::endl;
	console::flush();

	// remove engine functions
	core::func::remove("r_restart");

	console::shutdown();
	console::flush();

	input::shutdown();
	console::flush();

	video::shutdown();
	console::flush();

	core::Application::shutdown();
	console::flush();

	quit(0);
}

} // namespace client