1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 /*
24  * This code is based on Broken Sword 2.5 engine
25  *
26  * Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
27  *
28  * Licensed under GNU GPL v2
29  *
30  */
31 
32 #include "common/system.h"
33 
34 #include "sword25/sword25.h"	// for kDebugScript
35 #include "sword25/gfx/graphicengine.h"
36 #include "sword25/fmv/movieplayer.h"
37 #include "sword25/input/inputengine.h"
38 #include "sword25/kernel/kernel.h"
39 #include "sword25/kernel/persistenceservice.h"
40 #include "sword25/math/geometry.h"
41 #include "sword25/package/packagemanager.h"
42 #include "sword25/script/luascript.h"
43 #include "sword25/sfx/soundengine.h"
44 
45 namespace Sword25 {
46 
47 Kernel *Kernel::_instance = 0;
48 
Kernel()49 Kernel::Kernel() :
50 	_resourceManager(NULL),
51 	_initSuccess(false),
52 	_gfx(0),
53 	_sfx(0),
54 	_input(0),
55 	_package(0),
56 	_script(0),
57 	_fmv(0),
58 	_rnd("sword25")
59 	{
60 
61 	_instance = this;
62 
63 	// Create the resource manager
64 	_resourceManager = new ResourceManager(this);
65 
66 	// Initialize the script engine
67 	_script = new LuaScriptEngine(this);
68 	if (!_script || !_script->init()) {
69 		_initSuccess = false;
70 		return;
71 	}
72 
73 	// Register kernel script bindings
74 	if (!registerScriptBindings()) {
75 		error("Script bindings could not be registered.");
76 		_initSuccess = false;
77 		return;
78 	}
79 	debugC(kDebugScript, "Script bindings registered.");
80 
81 	_input = new InputEngine(this);
82 	assert(_input);
83 
84 	_gfx = new GraphicEngine(this);
85 	assert(_gfx);
86 
87 	_sfx = new SoundEngine(this);
88 	assert(_sfx);
89 
90 	_package = new PackageManager(this);
91 	assert(_package);
92 
93 	_geometry = new Geometry(this);
94 	assert(_geometry);
95 
96 	_fmv = new MoviePlayer(this);
97 	assert(_fmv);
98 
99 	_initSuccess = true;
100 }
101 
~Kernel()102 Kernel::~Kernel() {
103 	// Services are de-registered in reverse order of creation
104 
105 	delete _input;
106 	_input = 0;
107 
108 	delete _gfx;
109 	_gfx = 0;
110 
111 	delete _sfx;
112 	_sfx = 0;
113 
114 	delete _package;
115 	_package = 0;
116 
117 	delete _geometry;
118 	_geometry = 0;
119 
120 	delete _fmv;
121 	_fmv = 0;
122 
123 	delete _script;
124 	_script = 0;
125 
126 	// Resource-Manager freigeben
127 	delete _resourceManager;
128 }
129 
130 /**
131  * Returns a random number
132  * @param Min       The minimum allowed value
133  * @param Max       The maximum allowed value
134  */
getRandomNumber(int min,int max)135 int Kernel::getRandomNumber(int min, int max) {
136 	assert(min <= max);
137 
138 	return min + _rnd.getRandomNumber(max - min + 1);
139 }
140 
141 /**
142  * Returns the elapsed time since startup in milliseconds
143  */
getMilliTicks()144 uint Kernel::getMilliTicks() {
145 	return g_system->getMillis();
146 }
147 
148 /**
149  * Returns a pointer to the active Gfx Service, or NULL if no Gfx service is active.
150  */
getGfx()151 GraphicEngine *Kernel::getGfx() {
152 	return _gfx;
153 }
154 
155 /**
156  * Returns a pointer to the active Sfx Service, or NULL if no Sfx service is active.
157  */
getSfx()158 SoundEngine *Kernel::getSfx() {
159 	return _sfx;
160 }
161 
162 /**
163  * Returns a pointer to the active input service, or NULL if no input service is active.
164  */
getInput()165 InputEngine *Kernel::getInput() {
166 	return _input;
167 }
168 
169 /**
170  * Returns a pointer to the active package manager, or NULL if no manager is active.
171  */
getPackage()172 PackageManager *Kernel::getPackage() {
173 	return _package;
174 }
175 
176 /**
177  * Returns a pointer to the script engine, or NULL if it is not active.
178  */
getScript()179 ScriptEngine *Kernel::getScript() {
180 	return _script;
181 }
182 
183 /**
184  * Returns a pointer to the movie player, or NULL if it is not active.
185  */
getFMV()186 MoviePlayer *Kernel::getFMV() {
187 	return _fmv;
188 }
189 
sleep(uint msecs) const190 void Kernel::sleep(uint msecs) const {
191 	g_system->delayMillis(msecs);
192 }
193 
194 } // End of namespace Sword25
195