Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/audio/src/orxonox/orxonox.cc @ 335

Last change on this file since 335 was 334, checked in by nicolape, 17 years ago

Sound is now working in Orxonox! Added a test background sound that plays when starting the game

File size: 7.6 KB
Line 
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.
56std::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
77namespace 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                                bgsound->play();
195                                       
196        string levelFile = "sp_level_moonstation.oxw";
197        loader::LevelLoader* loader = new loader::LevelLoader(levelFile);
198       
199       
200       
201      }
202
203      void setupScene()
204      {
205        Ogre::SceneManager *mgr = mRoot->createSceneManager(Ogre::ST_GENERIC, "Default SceneManager");
206        Ogre::Camera *cam = mgr->createCamera("Camera");
207        Ogre::Viewport *vp = mRoot->getAutoCreatedWindow()->addViewport(cam);
208      }
209
210      void setupInputSystem()
211      {
212        size_t windowHnd = 0;
213        std::ostringstream windowHndStr;
214        OIS::ParamList pl;
215        Ogre::RenderWindow *win = mRoot->getAutoCreatedWindow();
216
217        win->getCustomAttribute("WINDOW", &windowHnd);
218        windowHndStr << windowHnd;
219        pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
220        mInputManager = OIS::InputManager::createInputSystem(pl);
221
222        try
223        {
224          mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject(OIS::OISKeyboard, false));
225          mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject(OIS::OISMouse, false));
226        }
227        catch (const OIS::Exception &e)
228        {
229          throw new Ogre::Exception(42, e.eText, "OrxApplication::setupInputSystem");
230        }
231      }
232
233      void setupCEGUI()
234      {
235        Ogre::SceneManager *mgr = mRoot->getSceneManager("Default SceneManager");
236        Ogre::RenderWindow *win = mRoot->getAutoCreatedWindow();
237
238        // CEGUI setup
239//        mRenderer = new CEGUI::OgreCEGUIRenderer(win, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mgr);
240//        mSystem = new CEGUI::System(mRenderer);
241
242        // Other CEGUI setup here.
243      }
244
245      void createFrameListener()
246      {
247        mListener = new OrxExitListener(mKeyboard);
248        mRoot->addFrameListener(mListener);
249      }
250
251      void startRenderLoop()
252      {
253        mRoot->startRendering();
254      }
255  };
256}
257
258using namespace Ogre;
259
260#if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32
261#define WIN32_LEAN_AND_MEAN
262#include "windows.h"
263
264             INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT)
265#else
266             int main(int argc, char **argv)
267#endif
268{
269  try
270  {
271    orxonox::OrxApplication orxonox;
272    orxonox.go();
273  }
274  catch(Exception& e)
275  {
276#if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32
277    MessageBoxA(NULL, e.getFullDescription().c_str(), "An exception has occurred!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
278#else
279    fprintf(stderr, "An exception has occurred: %s\n",
280            e.getFullDescription().c_str());
281#endif
282  }
283
284  return 0;
285}
286
Note: See TracBrowser for help on using the repository browser.