Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/skybox/src/orxonox.cc @ 444

Last change on this file since 444 was 428, checked in by gabrieho, 17 years ago

skybox buggy

File size: 8.9 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#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
42#include <CoreFoundation/CoreFoundation.h>
43#include "windows.h"
44
45
46
47// This function will locate the path to our application on OS X,
48// unlike windows you can not rely on the curent working directory
49// for locating your configuration files and resources.
50std::string macBundlePath()
51{
52  char path[1024];
53  CFBundleRef mainBundle = CFBundleGetMainBundle();
54  assert(mainBundle);
55
56  CFURLRef mainBundleURL = CFBundleCopyBundleURL(mainBundle);
57  assert(mainBundleURL);
58
59  CFStringRef cfStringRef = CFURLCopyFileSystemPath( mainBundleURL, kCFURLPOSIXPathStyle);
60  assert(cfStringRef);
61
62  CFStringGetCString(cfStringRef, path, 1024, kCFStringEncodingASCII);
63
64  CFRelease(mainBundleURL);
65  CFRelease(cfStringRef);
66
67  return std::string(path);
68}
69#endif
70
71namespace orxonox
72{
73      Ogre::RenderWindow * mWindow;
74      Ogre::SceneManager *mgr;
75      int stopper = 1;
76
77
78  class OrxApplication
79  {
80    public:
81      void go()
82      {
83        createRoot();
84        defineResources();
85        setupRenderSystem();
86        createRenderWindow();
87        initializeResourceGroups();
88        createScene();
89        setupScene();
90        setupInputSystem();
91        setupCEGUI();
92        createFrameListener();
93        startRenderLoop();
94      }
95
96      OrxApplication()
97      {
98        OrxApplication::pointer_s = this;
99        this->x = 0;
100      }
101
102      ~OrxApplication()
103      {
104        mInputManager->destroyInputObject(mKeyboard);
105        OIS::InputManager::destroyInputSystem(mInputManager);
106
107        delete mListener;
108        delete mRoot;
109      }
110
111
112      void createRoot()
113      {
114#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
115        mRoot = new Ogre::Root(macBundlePath() + "/Contents/Resources/plugins.cfg");
116#else
117        mRoot = new Ogre::Root();
118#endif
119      }
120
121      void defineResources()
122      {
123        Ogre::String secName, typeName, archName;
124        Ogre::ConfigFile cf;
125#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
126        cf.load(macBundlePath() + "/Contents/Resources/resources.cfg");
127#else
128        cf.load("resources.cfg");
129#endif
130
131        Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
132        while (seci.hasMoreElements())
133        {
134          secName = seci.peekNextKey();
135          Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
136          Ogre::ConfigFile::SettingsMultiMap::iterator i;
137          for (i = settings->begin(); i != settings->end(); ++i)
138          {
139            typeName = i->first;
140            archName = i->second;
141#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
142            Ogre::ResourceGroupManager::getSingleton().addResourceLocation( String(macBundlePath() + "/" + archName), typeName, secName);
143#else
144            Ogre::ResourceGroupManager::getSingleton().addResourceLocation( archName, typeName, secName);
145#endif
146          }
147        }
148      }
149
150      void setupRenderSystem()
151      {
152        if (!mRoot->restoreConfig() && !mRoot->showConfigDialog())
153          throw Ogre::Exception(52, "User canceled the config dialog!", "OrxApplication::setupRenderSystem()");
154      }
155
156      void createRenderWindow()
157      {
158        mWindow = mRoot->initialise(true, "Ogre Render Window");
159      }
160
161      void initializeResourceGroups()
162      {
163        Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
164        Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
165      }
166
167      void createScene(void)
168      {
169
170      }
171
172      void setupScene()
173      {
174        mgr = mRoot->createSceneManager(Ogre::ST_GENERIC, "Default SceneManager");
175        cam = mgr->createCamera("Camera");
176        cam->setPosition(Ogre::Vector3(0,0,0));
177        //cam->lookAt(Ogre::Vector3(0,0,0));
178        cam->setFOVy(Ogre::Degree(90));
179        Ogre::Viewport *vp = mRoot->getAutoCreatedWindow()->addViewport(cam);
180        mgr->setSkyBox(true, "Examples/SpaceSkyBox");
181
182        // camera pitch here works
183
184       
185      }
186
187      void setupInputSystem()
188      {
189        size_t windowHnd = 0;
190        std::ostringstream windowHndStr;
191        OIS::ParamList pl;
192        Ogre::RenderWindow *win = mRoot->getAutoCreatedWindow();
193
194        win->getCustomAttribute("WINDOW", &windowHnd);
195        windowHndStr << windowHnd;
196        pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
197        mInputManager = OIS::InputManager::createInputSystem(pl);
198
199        try
200        {
201          mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject(OIS::OISKeyboard, false));
202          mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject(OIS::OISMouse, false));
203        }
204        catch (const OIS::Exception &e)
205        {
206          throw new Ogre::Exception(42, e.eText, "OrxApplication::setupInputSystem");
207        }
208      }
209
210      void setupCEGUI()
211      {
212        Ogre::SceneManager *mgr = mRoot->getSceneManager("Default SceneManager");
213        Ogre::RenderWindow *win = mRoot->getAutoCreatedWindow();
214      }
215     
216      void screenShots()
217      {
218
219        Ogre::Radian x1 = Ogre::Radian(x);
220
221        unsigned int indice = 1;
222        char filename[30];
223        char fn[1];
224        // generate new names...
225        sprintf(filename, "SkyBox%d.png", stopper);
226        //sprintf(filename, "screenshot_%d.png", ++indice);
227        //sprintf(filename, fn, ++indice);
228       
229
230        Ogre::Radian x2 = Ogre::Radian((3.141592653589/2.0)*(stopper+1));
231
232
233        if(stopper == 1){
234                        cam->yaw(Ogre::Degree(90));
235        sprintf(filename, "SkyBox%d.png", stopper+1035);
236        mWindow->writeContentsToFile(filename);
237       
238        }
239        else if(stopper == 2){
240                        cam->yaw(Ogre::Degree(180));
241        mWindow->writeContentsToFile(filename);
242       
243        }
244        else if(stopper == 3){
245                        cam->yaw(Ogre::Degree(270));   
246        mWindow->writeContentsToFile(filename);
247
248        }
249        else if(stopper == 4){
250                        cam->yaw(Ogre::Degree(0));     
251        mWindow->writeContentsToFile(filename);
252
253        }
254
255        else if(stopper == 5){
256
257                        cam->yaw(Ogre::Degree(0));     
258        mWindow->writeContentsToFile(filename);
259
260        }
261        else if(stopper == 6){
262                        cam->yaw(Ogre::Degree(180));   
263        mWindow->writeContentsToFile(filename);
264
265        }
266
267        /*else if(stopper == 6){
268                        cam->roll(Ogre::Degree(270));   
269        }*/
270
271        //cam->pitch(Ogre::Degree(90)*stopper);
272
273        stopper+=1;
274        if(stopper >= 7)
275        stopper = 1;
276      }
277
278  class OrxExitListener : public Ogre::FrameListener
279  {
280    public:
281      OrxExitListener(OIS::Keyboard *keyboard)
282    : mKeyboard(keyboard)
283      {
284      }
285      bool frameEnded(const Ogre::FrameEvent& evt)
286        {
287                if(mKeyboard->isKeyDown(OIS::KC_RETURN) or stopper > 1){
288                        OrxApplication::pointer_s->screenShots();
289                }
290
291                return true;
292        }
293     
294      bool frameStarted(const Ogre::FrameEvent& evt)
295      {
296        mKeyboard->capture();
297        return !mKeyboard->isKeyDown(OIS::KC_ESCAPE);
298      }
299
300       
301    private:
302      OIS::Keyboard *mKeyboard;
303  };
304
305      void createFrameListener()
306      {
307        mListener = new OrxExitListener(mKeyboard);
308        mRoot->addFrameListener(mListener);
309      }
310
311      void startRenderLoop()
312      {
313        mRoot->startRendering();
314      }
315
316
317    private:
318      Ogre::Root *mRoot;
319      OIS::Keyboard *mKeyboard;
320      OIS::Mouse *mMouse;
321      OIS::InputManager *mInputManager;
322//      CEGUI::OgreCEGUIRenderer *mRenderer;
323//      CEGUI::System *mSystem;
324      OrxExitListener *mListener;
325      Ogre::Camera * cam;
326      double x;
327
328    public:
329      static OrxApplication* pointer_s;
330  };
331
332  OrxApplication* OrxApplication::pointer_s = 0;
333}
334
335using namespace Ogre;
336
337#if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32
338#define WIN32_LEAN_AND_MEAN
339#include "windows.h"
340
341             INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT)
342#else
343             int main(int argc, char **argv)
344#endif
345{
346  try
347  {
348    orxonox::OrxApplication orxonox;
349    orxonox.go();
350  }
351  catch(Exception& e)
352  {
353#if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32
354    MessageBoxA(NULL, e.getFullDescription().c_str(), "An exception has occurred!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
355#else
356    fprintf(stderr, "An exception has occurred: %s\n",
357            e.getFullDescription().c_str());
358#endif
359  }
360
361  return 0;
362}
363
Note: See TracBrowser for help on using the repository browser.