[SOLVED] Ogre Hangs in main loop

Problems building or running the engine, queries about how to use features etc.
semihandy
Gnoblar
Posts: 5
Joined: Thu Feb 09, 2012 12:05 am

[SOLVED] Ogre Hangs in main loop

Post by semihandy »

Hi everyone!

I've been banging my head on the keyboard almost all day with this issue, and have tried searching the forums / wiki / other resources to figure out what could be going on with no answers. Basically, the application runs fine (main loop is running, I can see the scene being drawn, etc.), but the window is not responsive, it shows the waiting cursor, and if I minimize it, I can't alt+tab back to it.

I've tried using Root->StartRendering() and using my own loop and calling Ogre::WindowEventUtilities::messagePump() manually, but both present the same issue. Here's some code with what I'm doing to init Ogre:

Code: Select all

    mResourcePath = resourcePath;
    mRoot = new Ogre::Root(resourcePath + "/plugins.cfg", 
                           resourcePath + "/ogre.cfg", 
                           resourcePath + "/ogre.log");

    if (!mRoot->showConfigDialog()) {
        return false;
    }
    // Window
    mWindow = mRoot->initialise(true);
    mSceneMgr = mRoot->createSceneManager(Ogre::ST_GENERIC, 
                                          "Main Scene Manager");
    
    // Camera
    mCam = mSceneMgr->createCamera("Main Camera");
    mCam->setPosition(Ogre::Vector3(0, 0, 80));
    mCam->lookAt(Ogre::Vector3(0, 0, -300));
    mCam->setNearClipDistance(5);
    
    // Viewport
    mViewport = mWindow->addViewport(mCam);
    mViewport->setBackgroundColour(Ogre::ColourValue(0, 0, 0));
    mCam->setAspectRatio(Ogre::Real(mViewport->getActualWidth()) /
                         Ogre::Real(mViewport->getActualHeight()));
    
    // Resources
    Ogre::ResourceGroupManager::getSingleton()
        .addResourceLocation(mResourcePath, "FileSystem", "General");
    Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
    Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
    
    // Input
    OIS::ParamList pl;
    size_t windowHnd = 0;
    std::ostringstream windowHndStr;
    
    mWindow->getCustomAttribute("WINDOW", &windowHnd);
    windowHndStr << windowHnd;
    pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
    
    mInputMgr = OIS::InputManager::createInputSystem(pl);
    
    mKeyboard = static_cast<OIS::Keyboard*>(mInputMgr->createInputObject(OIS::OISKeyboard, true));
    mMouse = static_cast<OIS::Mouse*>(mInputMgr->createInputObject(OIS::OISMouse, true));
    
    mMouse->setEventCallback(this);
    mKeyboard->setEventCallback(this);
    
    windowResized(mWindow);
    
    Ogre::WindowEventUtilities::addWindowEventListener(mWindow, this);
    
    mRoot->addFrameListener(this);
    mRunning = true;
//    mRoot->startRendering();
    while (mRunning) {
        Ogre::WindowEventUtilities::messagePump();
        
        if (!mRoot->renderOneFrame()) {
            mRunning = false;
        }
        
        mWindow->update();
    }

Am I missing / forgetting something? I'm pretty new to this, so if I apologize if I am making a silly or obvious mistake.

Thanks so much!
Last edited by semihandy on Thu Feb 09, 2012 1:33 am, edited 1 time in total.
User avatar
Jabberwocky
OGRE Moderator
OGRE Moderator
Posts: 2819
Joined: Mon Mar 05, 2007 11:17 pm
Location: Canada
x 218

Re: Ogre Hangs in main loop

Post by Jabberwocky »

What happens if you remove that call to

Code: Select all

mWindow->update();
Image
semihandy
Gnoblar
Posts: 5
Joined: Thu Feb 09, 2012 12:05 am

Re: Ogre Hangs in main loop

Post by semihandy »

Jabberwocky wrote:What happens if you remove that call to

Code: Select all

mWindow->update();
Ah, sorry forgot that was still in there. Nothing changes, I added that to see if it would have any effect.
semihandy
Gnoblar
Posts: 5
Joined: Thu Feb 09, 2012 12:05 am

Re: Ogre Hangs in main loop

Post by semihandy »

Also, just realized that this might be relevant. I'm using a regular Cocoa AppDelegate to start the application. Which then calls the code I included in the original post.

Code: Select all


#import "AppDelegate.h"
#include <Ogre/Ogre.h>
#include "Mabe.h"

@implementation AppDelegate

- (void)dealloc
{
    [super dealloc];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    std::string resourcePath = [[[NSBundle mainBundle] resourcePath] 
                                cStringUsingEncoding:NSUTF8StringEncoding];
    Mabe app(resourcePath);
    
    try {
        app.run();
    } catch (Ogre::Exception& e) {
        std::cerr << "An exception has occured: " << 
            e.getFullDescription().c_str() << std::endl;
    }
}

@end
User avatar
duststorm
Minaton
Posts: 921
Joined: Sat Jul 31, 2010 6:29 pm
Location: Belgium
x 80

Re: Ogre Hangs in main loop

Post by duststorm »

I actually don't know anything of that framework you are using so I can only answer in general.
Does your Ogre code open its own window, or does it operate within a window of another application?

If the framework you are using has its own event loop you probably need to return after invoking the method that initializes ogre, and call the renderOneFrame() from within the event loop of your original application.
Because you're running an endless render loop within an event of the parent application's event loop, it will seem as if your program is blocking because the parent event loop is stalled.

Another alternative would be to make sure that the parent application creates a new thread in which ogre is started, and then returns to its own event loop.

This is of course just speculation, as I don't know the details.
Developer @ MakeHuman.org
semihandy
Gnoblar
Posts: 5
Joined: Thu Feb 09, 2012 12:05 am

Re: Ogre Hangs in main loop

Post by semihandy »

duststorm wrote:I actually don't know anything of that framework you are using so I can only answer in general.
Does your Ogre code open its own window, or does it operate within a window of another application?

If the framework you are using has its own event loop you probably need to return after invoking the method that initializes ogre, and call the renderOneFrame() from within the event loop of your original application.
Because you're running an endless render loop within an event of the parent application's event loop, it will seem as if your program is blocking because the parent event loop is stalled.

Another alternative would be to make sure that the parent application creates a new thread in which ogre is started, and then returns to its own event loop.

This is of course just speculation, as I don't know the details.
Oh, I hadn't thought of this! Ogre is opening up it's own window, but this is probably the case. Let me research a bit and see if I can find out more about it (not really an OS X developer, so I don't know much about the Cocoa API). Thanks!
User avatar
masterfalcon
OGRE Team Member
OGRE Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126

Re: Ogre Hangs in main loop

Post by masterfalcon »

Being Cocoa is very relevant to this. And Ogre opening it's own window isn't.

Cocoa dispatches events differently than Carbon or Win32. You don't pump messages, instead you wait for them to be delivered to you. If you are running a never ending loop for rendering and events handling, your app never has a chance for those events to be delivered to it. Which is why it is hanging.

Check out SampleBrowser_OSX.h to see an example of using renderOneFrame with Cocoa.
semihandy
Gnoblar
Posts: 5
Joined: Thu Feb 09, 2012 12:05 am

Re: Ogre Hangs in main loop

Post by semihandy »

masterfalcon wrote:Being Cocoa is very relevant to this. And Ogre opening it's own window isn't.

Cocoa dispatches events differently than Carbon or Win32. You don't pump messages, instead you wait for them to be delivered to you. If you are running a never ending loop for rendering and events handling, your app never has a chance for those events to be delivered to it. Which is why it is hanging.

Check out SampleBrowser_OSX.h to see an example of using renderOneFrame with Cocoa.
Ah, this makes perfect sense. What duststorm suggested is exactly the issue, which goes in hand with what you described here. If I run a timer instead (I found an example here http://will.thimbleby.net/ogre/tutorial1.html) of running my own loop, and call renderOneFrame from there it works perfectly.

I will take a look at SampleBrowser_OSX.h as well, to see if there are other things that I might be doing wrong.

Thanks so much! You guys are awesome :)