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 "audio/Ambient.h" |
---|
44 | |
---|
45 | |
---|
46 | // some tests to see if enet works without includsion |
---|
47 | //#include <enet/enet.h> |
---|
48 | //#include <enet/protocol.h> |
---|
49 | |
---|
50 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
51 | #include <CoreFoundation/CoreFoundation.h> |
---|
52 | |
---|
53 | // This function will locate the path to our application on OS X, |
---|
54 | // unlike windows you can not rely on the curent working directory |
---|
55 | // for locating your configuration files and resources. |
---|
56 | std::string macBundlePath() |
---|
57 | { |
---|
58 | char path[1024]; |
---|
59 | CFBundleRef mainBundle = CFBundleGetMainBundle(); |
---|
60 | assert(mainBundle); |
---|
61 | |
---|
62 | CFURLRef mainBundleURL = CFBundleCopyBundleURL(mainBundle); |
---|
63 | assert(mainBundleURL); |
---|
64 | |
---|
65 | CFStringRef cfStringRef = CFURLCopyFileSystemPath( mainBundleURL, kCFURLPOSIXPathStyle); |
---|
66 | assert(cfStringRef); |
---|
67 | |
---|
68 | CFStringGetCString(cfStringRef, path, 1024, kCFStringEncodingASCII); |
---|
69 | |
---|
70 | CFRelease(mainBundleURL); |
---|
71 | CFRelease(cfStringRef); |
---|
72 | |
---|
73 | return std::string(path); |
---|
74 | } |
---|
75 | #endif |
---|
76 | |
---|
77 | namespace orxonox |
---|
78 | { |
---|
79 | class OrxExitListener : public Ogre::FrameListener |
---|
80 | { |
---|
81 | public: |
---|
82 | OrxExitListener(OIS::Keyboard *keyboard) |
---|
83 | : mKeyboard(keyboard) |
---|
84 | { |
---|
85 | } |
---|
86 | |
---|
87 | bool frameStarted(const Ogre::FrameEvent& evt) |
---|
88 | { |
---|
89 | mKeyboard->capture(); |
---|
90 | return !mKeyboard->isKeyDown(OIS::KC_ESCAPE); |
---|
91 | } |
---|
92 | |
---|
93 | private: |
---|
94 | OIS::Keyboard *mKeyboard; |
---|
95 | }; |
---|
96 | |
---|
97 | class OrxApplication |
---|
98 | { |
---|
99 | public: |
---|
100 | void go() |
---|
101 | { |
---|
102 | createRoot(); |
---|
103 | defineResources(); |
---|
104 | setupRenderSystem(); |
---|
105 | createRenderWindow(); |
---|
106 | initializeResourceGroups(); |
---|
107 | createScene(); |
---|
108 | setupScene(); |
---|
109 | setupInputSystem(); |
---|
110 | setupCEGUI(); |
---|
111 | createFrameListener(); |
---|
112 | startRenderLoop(); |
---|
113 | } |
---|
114 | |
---|
115 | ~OrxApplication() |
---|
116 | { |
---|
117 | mInputManager->destroyInputObject(mKeyboard); |
---|
118 | OIS::InputManager::destroyInputSystem(mInputManager); |
---|
119 | |
---|
120 | // delete mRenderer; |
---|
121 | // delete mSystem; |
---|
122 | |
---|
123 | delete mListener; |
---|
124 | delete mRoot; |
---|
125 | } |
---|
126 | |
---|
127 | private: |
---|
128 | Ogre::Root *mRoot; |
---|
129 | OIS::Keyboard *mKeyboard; |
---|
130 | OIS::Mouse *mMouse; |
---|
131 | OIS::InputManager *mInputManager; |
---|
132 | CEGUI::OgreCEGUIRenderer *mRenderer; |
---|
133 | CEGUI::System *mSystem; |
---|
134 | OrxExitListener *mListener; |
---|
135 | |
---|
136 | void createRoot() |
---|
137 | { |
---|
138 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
139 | mRoot = new Ogre::Root(macBundlePath() + "/Contents/Resources/plugins.cfg"); |
---|
140 | #else |
---|
141 | mRoot = new Ogre::Root(); |
---|
142 | #endif |
---|
143 | } |
---|
144 | |
---|
145 | void defineResources() |
---|
146 | { |
---|
147 | Ogre::String secName, typeName, archName; |
---|
148 | Ogre::ConfigFile cf; |
---|
149 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
150 | cf.load(macBundlePath() + "/Contents/Resources/resources.cfg"); |
---|
151 | #else |
---|
152 | cf.load("resources.cfg"); |
---|
153 | #endif |
---|
154 | |
---|
155 | Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator(); |
---|
156 | while (seci.hasMoreElements()) |
---|
157 | { |
---|
158 | secName = seci.peekNextKey(); |
---|
159 | Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext(); |
---|
160 | Ogre::ConfigFile::SettingsMultiMap::iterator i; |
---|
161 | for (i = settings->begin(); i != settings->end(); ++i) |
---|
162 | { |
---|
163 | typeName = i->first; |
---|
164 | archName = i->second; |
---|
165 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
166 | Ogre::ResourceGroupManager::getSingleton().addResourceLocation( String(macBundlePath() + "/" + archName), typeName, secName); |
---|
167 | #else |
---|
168 | Ogre::ResourceGroupManager::getSingleton().addResourceLocation( archName, typeName, secName); |
---|
169 | #endif |
---|
170 | } |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | void setupRenderSystem() |
---|
175 | { |
---|
176 | if (!mRoot->restoreConfig() && !mRoot->showConfigDialog()) |
---|
177 | throw Ogre::Exception(52, "User canceled the config dialog!", "OrxApplication::setupRenderSystem()"); |
---|
178 | } |
---|
179 | |
---|
180 | void createRenderWindow() |
---|
181 | { |
---|
182 | mRoot->initialise(true, "Ogre Render Window"); |
---|
183 | } |
---|
184 | |
---|
185 | void initializeResourceGroups() |
---|
186 | { |
---|
187 | Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5); |
---|
188 | Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); |
---|
189 | } |
---|
190 | |
---|
191 | void createScene(void) |
---|
192 | { |
---|
193 | audio::Ambient* bgsound = new audio::Ambient(); |
---|
194 | |
---|
195 | string levelFile = "sp_level_moonstation.oxw"; |
---|
196 | loader::LevelLoader* loader = new loader::LevelLoader(levelFile); |
---|
197 | |
---|
198 | |
---|
199 | |
---|
200 | } |
---|
201 | |
---|
202 | void setupScene() |
---|
203 | { |
---|
204 | Ogre::SceneManager *mgr = mRoot->createSceneManager(Ogre::ST_GENERIC, "Default SceneManager"); |
---|
205 | Ogre::Camera *cam = mgr->createCamera("Camera"); |
---|
206 | Ogre::Viewport *vp = mRoot->getAutoCreatedWindow()->addViewport(cam); |
---|
207 | } |
---|
208 | |
---|
209 | void setupInputSystem() |
---|
210 | { |
---|
211 | size_t windowHnd = 0; |
---|
212 | std::ostringstream windowHndStr; |
---|
213 | OIS::ParamList pl; |
---|
214 | Ogre::RenderWindow *win = mRoot->getAutoCreatedWindow(); |
---|
215 | |
---|
216 | win->getCustomAttribute("WINDOW", &windowHnd); |
---|
217 | windowHndStr << windowHnd; |
---|
218 | pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); |
---|
219 | mInputManager = OIS::InputManager::createInputSystem(pl); |
---|
220 | |
---|
221 | try |
---|
222 | { |
---|
223 | mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject(OIS::OISKeyboard, false)); |
---|
224 | mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject(OIS::OISMouse, false)); |
---|
225 | } |
---|
226 | catch (const OIS::Exception &e) |
---|
227 | { |
---|
228 | throw new Ogre::Exception(42, e.eText, "OrxApplication::setupInputSystem"); |
---|
229 | } |
---|
230 | } |
---|
231 | |
---|
232 | void setupCEGUI() |
---|
233 | { |
---|
234 | Ogre::SceneManager *mgr = mRoot->getSceneManager("Default SceneManager"); |
---|
235 | Ogre::RenderWindow *win = mRoot->getAutoCreatedWindow(); |
---|
236 | |
---|
237 | // CEGUI setup |
---|
238 | // mRenderer = new CEGUI::OgreCEGUIRenderer(win, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mgr); |
---|
239 | // mSystem = new CEGUI::System(mRenderer); |
---|
240 | |
---|
241 | // Other CEGUI setup here. |
---|
242 | } |
---|
243 | |
---|
244 | void createFrameListener() |
---|
245 | { |
---|
246 | mListener = new OrxExitListener(mKeyboard); |
---|
247 | mRoot->addFrameListener(mListener); |
---|
248 | } |
---|
249 | |
---|
250 | void startRenderLoop() |
---|
251 | { |
---|
252 | mRoot->startRendering(); |
---|
253 | } |
---|
254 | }; |
---|
255 | } |
---|
256 | |
---|
257 | using namespace Ogre; |
---|
258 | |
---|
259 | #if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32 |
---|
260 | #define WIN32_LEAN_AND_MEAN |
---|
261 | #include "windows.h" |
---|
262 | |
---|
263 | INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT) |
---|
264 | #else |
---|
265 | int main(int argc, char **argv) |
---|
266 | #endif |
---|
267 | { |
---|
268 | try |
---|
269 | { |
---|
270 | orxonox::OrxApplication orxonox; |
---|
271 | orxonox.go(); |
---|
272 | } |
---|
273 | catch(Exception& e) |
---|
274 | { |
---|
275 | #if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32 |
---|
276 | MessageBoxA(NULL, e.getFullDescription().c_str(), "An exception has occurred!", MB_OK | MB_ICONERROR | MB_TASKMODAL); |
---|
277 | #else |
---|
278 | fprintf(stderr, "An exception has occurred: %s\n", |
---|
279 | e.getFullDescription().c_str()); |
---|
280 | #endif |
---|
281 | } |
---|
282 | |
---|
283 | return 0; |
---|
284 | } |
---|
285 | |
---|