1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * |
---|
4 | * |
---|
5 | * License notice: |
---|
6 | * |
---|
7 | * This program is free software: you can redistribute it and/or modify |
---|
8 | * it under the terms of the GNU General Public License as published by |
---|
9 | * the Free Software Foundation, either version 3 of the License, or |
---|
10 | * (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, see <http://www.gnu.org/licenses/>. |
---|
19 | * |
---|
20 | * |
---|
21 | * Author: |
---|
22 | * Benjamin Knecht <beni_at_orxonox.net>, (C) 2007 |
---|
23 | * Co-authors: |
---|
24 | * ... |
---|
25 | * |
---|
26 | */ |
---|
27 | |
---|
28 | /** |
---|
29 | @file orxonox.cc |
---|
30 | @brief Orxonox Main File |
---|
31 | */ |
---|
32 | |
---|
33 | #include <Ogre.h> |
---|
34 | #include <OIS/OIS.h> |
---|
35 | #include <CEGUI/CEGUI.h> |
---|
36 | #include <OgreCEGUIRenderer.h> |
---|
37 | |
---|
38 | #include <string> |
---|
39 | #include <iostream> |
---|
40 | |
---|
41 | #include "xml/xmlParser.h" |
---|
42 | #include "loader/LevelLoader.h" |
---|
43 | #include "Flocking.h" |
---|
44 | |
---|
45 | // some tests to see if enet works without includsion |
---|
46 | //#include <enet/enet.h> |
---|
47 | //#include <enet/protocol.h> |
---|
48 | |
---|
49 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
50 | #include <CoreFoundation/CoreFoundation.h> |
---|
51 | |
---|
52 | // This function will locate the path to our application on OS X, |
---|
53 | // unlike windows you can not rely on the curent working directory |
---|
54 | // for locating your configuration files and resources. |
---|
55 | std::string macBundlePath() |
---|
56 | { |
---|
57 | char path[1024]; |
---|
58 | CFBundleRef mainBundle = CFBundleGetMainBundle(); |
---|
59 | assert(mainBundle); |
---|
60 | |
---|
61 | CFURLRef mainBundleURL = CFBundleCopyBundleURL(mainBundle); |
---|
62 | assert(mainBundleURL); |
---|
63 | |
---|
64 | CFStringRef cfStringRef = CFURLCopyFileSystemPath( mainBundleURL, kCFURLPOSIXPathStyle); |
---|
65 | assert(cfStringRef); |
---|
66 | |
---|
67 | CFStringGetCString(cfStringRef, path, 1024, kCFStringEncodingASCII); |
---|
68 | |
---|
69 | CFRelease(mainBundleURL); |
---|
70 | CFRelease(cfStringRef); |
---|
71 | |
---|
72 | return std::string(path); |
---|
73 | } |
---|
74 | #endif |
---|
75 | |
---|
76 | using namespace Ogre; |
---|
77 | |
---|
78 | //my-stuff |
---|
79 | //globale definition eines Arrays welches alle nodes enthält |
---|
80 | Vector3 ElementLocationArray[2]; |
---|
81 | Vector3 ElementSpeedArray[2]; |
---|
82 | Vector3 ElementAccelerationArray[2]; |
---|
83 | |
---|
84 | Element* arrayOfElements[2]; |
---|
85 | |
---|
86 | |
---|
87 | class OrxExitListener : public FrameListener |
---|
88 | { |
---|
89 | public: |
---|
90 | OrxExitListener(OIS::Keyboard *keyboard, Root* root) |
---|
91 | : mKeyboard(keyboard) |
---|
92 | { |
---|
93 | root_ = root; |
---|
94 | } |
---|
95 | |
---|
96 | bool frameStarted(const FrameEvent& evt) |
---|
97 | { |
---|
98 | moving(evt); |
---|
99 | mKeyboard->capture(); |
---|
100 | return !mKeyboard->isKeyDown(OIS::KC_ESCAPE); |
---|
101 | } |
---|
102 | |
---|
103 | //code the movments of the nodes here |
---|
104 | void moving(const FrameEvent& evt) { |
---|
105 | SceneManager *mgr = root_->getSceneManager("Default SceneManager"); |
---|
106 | arrayOfElements[0]->update(*arrayOfElements); |
---|
107 | mgr->getSceneNode("HeadNode1")->translate(0.000000001*evt.timeSinceLastFrame*arrayOfElements[0]->location); |
---|
108 | arrayOfElements[1]->update(*arrayOfElements); |
---|
109 | mgr->getSceneNode("HeadNode2")->translate(0.000000001*evt.timeSinceLastFrame*arrayOfElements[1]->location); |
---|
110 | arrayOfElements[2]->update(*arrayOfElements); |
---|
111 | mgr->getSceneNode("HeadNode3")->translate(0.000000001*evt.timeSinceLastFrame*arrayOfElements[2]->location); |
---|
112 | //mgr->getSceneNode("HeadNode1")->yaw((Radian)10*evt.timeSinceLastFrame); |
---|
113 | } |
---|
114 | |
---|
115 | private: |
---|
116 | OIS::Keyboard *mKeyboard; |
---|
117 | Root* root_; |
---|
118 | }; |
---|
119 | |
---|
120 | class OrxApplication |
---|
121 | { |
---|
122 | public: |
---|
123 | void go() |
---|
124 | { |
---|
125 | createRoot(); |
---|
126 | defineResources(); |
---|
127 | setupRenderSystem(); |
---|
128 | createRenderWindow(); |
---|
129 | initializeResourceGroups(); |
---|
130 | createScene(); |
---|
131 | setupScene(); |
---|
132 | setupInputSystem(); |
---|
133 | setupCEGUI(); |
---|
134 | createFrameListener(); |
---|
135 | startRenderLoop(); |
---|
136 | } |
---|
137 | |
---|
138 | ~OrxApplication() |
---|
139 | { |
---|
140 | mInputManager->destroyInputObject(mKeyboard); |
---|
141 | OIS::InputManager::destroyInputSystem(mInputManager); |
---|
142 | |
---|
143 | // delete mRenderer; |
---|
144 | // delete mSystem; |
---|
145 | |
---|
146 | delete mListener; |
---|
147 | delete mRoot; |
---|
148 | } |
---|
149 | |
---|
150 | private: |
---|
151 | Root *mRoot; |
---|
152 | OIS::Keyboard *mKeyboard; |
---|
153 | OIS::Mouse *mMouse; |
---|
154 | OIS::InputManager *mInputManager; |
---|
155 | CEGUI::OgreCEGUIRenderer *mRenderer; |
---|
156 | CEGUI::System *mSystem; |
---|
157 | OrxExitListener *mListener; |
---|
158 | |
---|
159 | void createRoot() |
---|
160 | { |
---|
161 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
162 | mRoot = new Root(macBundlePath() + "/Contents/Resources/plugins.cfg"); |
---|
163 | #else |
---|
164 | mRoot = new Root(); |
---|
165 | #endif |
---|
166 | } |
---|
167 | |
---|
168 | void defineResources() |
---|
169 | { |
---|
170 | String secName, typeName, archName; |
---|
171 | ConfigFile cf; |
---|
172 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
173 | cf.load(macBundlePath() + "/Contents/Resources/resources.cfg"); |
---|
174 | #else |
---|
175 | cf.load("resources.cfg"); |
---|
176 | #endif |
---|
177 | |
---|
178 | ConfigFile::SectionIterator seci = cf.getSectionIterator(); |
---|
179 | while (seci.hasMoreElements()) |
---|
180 | { |
---|
181 | secName = seci.peekNextKey(); |
---|
182 | ConfigFile::SettingsMultiMap *settings = seci.getNext(); |
---|
183 | ConfigFile::SettingsMultiMap::iterator i; |
---|
184 | for (i = settings->begin(); i != settings->end(); ++i) |
---|
185 | { |
---|
186 | typeName = i->first; |
---|
187 | archName = i->second; |
---|
188 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
189 | ResourceGroupManager::getSingleton().addResourceLocation( String(macBundlePath() + "/" + archName), typeName, secName); |
---|
190 | #else |
---|
191 | ResourceGroupManager::getSingleton().addResourceLocation( archName, typeName, secName); |
---|
192 | #endif |
---|
193 | } |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | void setupRenderSystem() |
---|
198 | { |
---|
199 | if (!mRoot->restoreConfig() && !mRoot->showConfigDialog()) |
---|
200 | throw Exception(52, "User canceled the config dialog!", "OrxApplication::setupRenderSystem()"); |
---|
201 | } |
---|
202 | |
---|
203 | void createRenderWindow() |
---|
204 | { |
---|
205 | mRoot->initialise(true, "Ogre Render Window"); |
---|
206 | } |
---|
207 | |
---|
208 | void initializeResourceGroups() |
---|
209 | { |
---|
210 | TextureManager::getSingleton().setDefaultNumMipmaps(5); |
---|
211 | ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); |
---|
212 | } |
---|
213 | |
---|
214 | void createScene(void) |
---|
215 | { |
---|
216 | |
---|
217 | string levelFile = "sp_level_moonstation.oxw"; |
---|
218 | loader::LevelLoader* loader = new loader::LevelLoader(levelFile); |
---|
219 | } |
---|
220 | |
---|
221 | void setupScene() |
---|
222 | { |
---|
223 | SceneManager *mgr = mRoot->createSceneManager(ST_GENERIC, "Default SceneManager"); |
---|
224 | Camera *cam = mgr->createCamera("Camera"); |
---|
225 | cam->setPosition(Vector3(0,0,1000)); |
---|
226 | cam->lookAt(Vector3(0,0,0)); |
---|
227 | Viewport *vp = mRoot->getAutoCreatedWindow()->addViewport(cam); |
---|
228 | example(); //my stuff |
---|
229 | } |
---|
230 | |
---|
231 | void setupInputSystem() |
---|
232 | { |
---|
233 | size_t windowHnd = 0; |
---|
234 | std::ostringstream windowHndStr; |
---|
235 | OIS::ParamList pl; |
---|
236 | RenderWindow *win = mRoot->getAutoCreatedWindow(); |
---|
237 | |
---|
238 | win->getCustomAttribute("WINDOW", &windowHnd); |
---|
239 | windowHndStr << windowHnd; |
---|
240 | pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); |
---|
241 | mInputManager = OIS::InputManager::createInputSystem(pl); |
---|
242 | |
---|
243 | try |
---|
244 | { |
---|
245 | mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject(OIS::OISKeyboard, false)); |
---|
246 | mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject(OIS::OISMouse, false)); |
---|
247 | } |
---|
248 | catch (const OIS::Exception &e) |
---|
249 | { |
---|
250 | throw new Exception(42, e.eText, "OrxApplication::setupInputSystem"); |
---|
251 | } |
---|
252 | } |
---|
253 | |
---|
254 | void setupCEGUI() |
---|
255 | { |
---|
256 | SceneManager *mgr = mRoot->getSceneManager("Default SceneManager"); |
---|
257 | RenderWindow *win = mRoot->getAutoCreatedWindow(); |
---|
258 | |
---|
259 | // CEGUI setup |
---|
260 | // mRenderer = new CEGUI::OgreCEGUIRenderer(win, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mgr); |
---|
261 | // mSystem = new CEGUI::System(mRenderer); |
---|
262 | |
---|
263 | // Other CEGUI setup here. |
---|
264 | } |
---|
265 | |
---|
266 | void createFrameListener() |
---|
267 | { |
---|
268 | mListener = new OrxExitListener(mKeyboard, mRoot); |
---|
269 | mRoot->addFrameListener(mListener); |
---|
270 | } |
---|
271 | |
---|
272 | void startRenderLoop() |
---|
273 | { |
---|
274 | mRoot->startRendering(); |
---|
275 | } |
---|
276 | |
---|
277 | //declaration of the 3 Ogreheads |
---|
278 | //muss leider global sein..... |
---|
279 | //Element* arrayOfElements[2]; |
---|
280 | |
---|
281 | void example() { |
---|
282 | SceneManager *mgr = mRoot->getSceneManager("Default SceneManager"); |
---|
283 | mgr->setAmbientLight(ColourValue(1.0,1.0,1.0)); |
---|
284 | Entity* ent1 = mgr->createEntity("Head1", "ogrehead.mesh"); |
---|
285 | Entity* ent2 = mgr->createEntity("Head2", "ogrehead.mesh"); |
---|
286 | Entity* ent3 = mgr->createEntity("Head3", "ogrehead.mesh"); |
---|
287 | SceneNode *node1 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode1", Vector3(0,100,0)); |
---|
288 | SceneNode *node2 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode2", Vector3(100,0,0)); |
---|
289 | SceneNode *node3 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode3", Vector3(-100,0,0)); |
---|
290 | node1->attachObject(ent1); |
---|
291 | node2->attachObject(ent2); |
---|
292 | node3->attachObject(ent3); |
---|
293 | //Camera* cam = mgr->getCamera("Camera"); |
---|
294 | //node1->attachObject(cam); |
---|
295 | ElementLocationArray[0] = node1->getPosition(); |
---|
296 | ElementLocationArray[1] = node2->getPosition(); |
---|
297 | ElementLocationArray[2] = node3->getPosition(); |
---|
298 | ElementSpeedArray[0] = (0,0,0); |
---|
299 | ElementSpeedArray[1] = (0,0,0); |
---|
300 | ElementSpeedArray[2] = (0,0,0); |
---|
301 | ElementAccelerationArray[0] = (0,0,0); |
---|
302 | ElementAccelerationArray[1] = (0,0,0); |
---|
303 | ElementAccelerationArray[2] = (0,0,0); |
---|
304 | arrayOfElements[0] = new Element( ElementLocationArray[0], ElementSpeedArray[0], ElementAccelerationArray[0] ); |
---|
305 | arrayOfElements[1] = new Element( ElementLocationArray[1], ElementSpeedArray[1], ElementAccelerationArray[1] ); |
---|
306 | arrayOfElements[2] = new Element( ElementLocationArray[2], ElementSpeedArray[2], ElementAccelerationArray[2] ); |
---|
307 | |
---|
308 | |
---|
309 | |
---|
310 | |
---|
311 | /* for (int i=0; i<3; i++) { |
---|
312 | Element* arrayOfElements[i] = new Element( ElementLocationArray[i], ElementSpeedArray[i], ElementAccelerationArray[i] ); |
---|
313 | } */ |
---|
314 | /* for (int i=0; i<3; i++) { |
---|
315 | arrayOfElements[i]->update(arrayOfElements); |
---|
316 | } */ |
---|
317 | } |
---|
318 | }; |
---|
319 | |
---|
320 | |
---|
321 | |
---|
322 | #if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32 |
---|
323 | #define WIN32_LEAN_AND_MEAN |
---|
324 | #include "windows.h" |
---|
325 | |
---|
326 | INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT) |
---|
327 | #else |
---|
328 | int main(int argc, char **argv) |
---|
329 | #endif |
---|
330 | { |
---|
331 | try |
---|
332 | { |
---|
333 | OrxApplication orxonox; |
---|
334 | orxonox.go(); |
---|
335 | } |
---|
336 | catch(Exception& e) |
---|
337 | { |
---|
338 | #if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32 |
---|
339 | MessageBoxA(NULL, e.getFullDescription().c_str(), "An exception has occurred!", MB_OK | MB_ICONERROR | MB_TASKMODAL); |
---|
340 | #else |
---|
341 | fprintf(stderr, "An exception has occurred: %s\n", |
---|
342 | e.getFullDescription().c_str()); |
---|
343 | #endif |
---|
344 | } |
---|
345 | |
---|
346 | return 0; |
---|
347 | } |
---|
348 | |
---|