Using SDL2 With Ogre 2.0?

Discussion area about developing with Ogre2 branches (2.1, 2.2 and beyond)
Post Reply
User avatar
excaliburHisSheath
Gnoblar
Posts: 18
Joined: Sat Feb 15, 2014 8:06 pm

Using SDL2 With Ogre 2.0?

Post by excaliburHisSheath »

I recently started a project using Ogre 1.9, but I figure it would be a good idea to switch to 2.0 now while my project is small. I seem to recall that there was mention of switching to using SDL2 for windowing in 2.0. What's the status of that? Are there examples of how to setup Ogre 2.0 to use SDL2 for windowing? I know I had trouble finding a up-to-date example of how to use 1.9 with SDL2.

Thanks for the help. I'm new to Ogre, but I love the project and would love to get more familiar with it so that I could eventually contribute back.

TheSHEEEP
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 972
Joined: Mon Jun 02, 2008 6:52 pm
Location: Berlin
x 63

Re: Using SDL2 With Ogre 2.0?

Post by TheSHEEEP »

I do not know if there are official examples for that, but this is the code we use in our project, which is using Ogre 1.9:

Code: Select all

_root = new Ogre::Root("", "", "");
    // Load required plugins
    #ifdef _DEBUG
    _root->loadPlugin("RenderSystem_GL_d");
    #else
    _root->loadPlugin("RenderSystem_GL");  
    #endif

    // Configure
    // Grab the OpenGL RenderSystem, or exit
    Ogre::RenderSystem* rs = _root->getRenderSystemByName("OpenGL Rendering Subsystem");
    if (!(rs->getName() == "OpenGL Rendering Subsystem")) 
    {
        return false; //No RenderSystem found
    }
    // Configure our RenderSystem
    _root->setRenderSystem(rs);
    _window = _root->initialise(false);
    
    // Use SDL2 to create a window
    bool fullscreen = false;
    int width = 1280;
    int height = 800;
    _SDLWindow = SDL_CreateWindow(
        "Two Suns",     // window title
        25,             // initial x position - of the actual render area, not the border
        25,             // initial y position - of the actual render area, not the border
        width,           // width, in pixels
        height,            // height, in pixels
        SDL_WINDOW_SHOWN
        | (fullscreen ? SDL_WINDOW_FULLSCREEN : 0) | SDL_WINDOW_RESIZABLE
    );
    
    // Give the correct window handle to Ogre
    SDL_SysWMinfo wmInfo;
    SDL_GetVersion(&wmInfo.version);
    if (SDL_GetWindowWMInfo(_SDLWindow, &wmInfo) == SDL_FALSE)
    {
        return false; // Couldn't get WM Info!
    }
    Ogre::String winHandle;
    switch (wmInfo.subsystem)
    {
    #ifdef __MINGW32__
        case SDL_SYSWM_WINDOWS:
            // Windows code
            winHandle = Ogre::StringConverter::toString((unsigned long)wmInfo.info.win.window);
            break;
    #elif __MACOSX__
        case SDL_SYSWM_COCOA:
            //required to make OGRE play nice with our window
            params.insert(std::make_pair("macAPI", "cocoa"));
            params.insert(std::make_pair("macAPICocoaUseNSView", "true"));
            winHandle = Ogre::StringConverter::toString(WindowContentViewHandle(wmInfo));
            break;
    #else
        case SDL_SYSWM_X11:
            winHandle = Ogre::StringConverter::toString((unsigned long)wmInfo.info.x11.window);
            break;
    #endif
    default:
        return false; // Unexpected window subsystem
        break;
    }
    
    Ogre::NameValuePairList params;
    params["externalWindowHandle"] = winHandle;
    _window = _root->createRenderWindow("Two Suns", width, height, fullscreen, &params);

    // Get the SceneManager
    _sceneMgr = _root->createSceneManager("DefaultSceneManager", "DefaultSceneManager");  
I don't think this is much different from SDL1, though I am not sure if it works exactly like that for Ogre 2.0.
It would not surprise me, though, as the main changes in Ogre 2.0 are the internal graphics pipeline, not the windowing.
My site! - Have a look :)
Also on Twitter - extra fluffy

User avatar
Herb
Orc
Posts: 412
Joined: Thu Jun 04, 2009 3:21 am
Location: Kalamazoo,MI
x 38

Re: Using SDL2 With Ogre 2.0?

Post by Herb »

@TheSHEEP - thanks for posting this code as I was looking at trying to integrate SDL and that makes it really easy. :D Maybe you could dump that snippet on the wiki somewhere for future reference.

User avatar
excaliburHisSheath
Gnoblar
Posts: 18
Joined: Sat Feb 15, 2014 8:06 pm

Re: Using SDL2 With Ogre 2.0?

Post by excaliburHisSheath »

:D Excellent, thank you! That looks to be about the same as with previous versions of Ogre, so I'm hoping the transition will be relatively painless.

TheSHEEEP
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 972
Joined: Mon Jun 02, 2008 6:52 pm
Location: Berlin
x 63

Re: Using SDL2 With Ogre 2.0?

Post by TheSHEEEP »

Oh my, I just noticed you wanted that for Ogre 2.0.
The code I posted is for SDL2, but Ogre 1.9!

My mistake, sorry. I was so focused on SDL2, that I just skipped the Ogre 2.0 part :oops:
Also edited my post to reflect that.
My site! - Have a look :)
Also on Twitter - extra fluffy

User avatar
Daixiwen
Greenskin
Posts: 104
Joined: Fri Feb 08, 2013 11:30 am
Location: Oslo
x 14

Re: Using SDL2 With Ogre 2.0?

Post by Daixiwen »

I have a question about that... I'm using a similar procedure on my project, with SDL2 and Ogre 1.9. I open the window first with SDL and use the externalWindowHandle parameter to have Ogre use it. But then I noticed that the display is never antialiased, even if I set it in the configuration window. I don't know if it is because the configuration isn't read by the render plugin or if something else is interfering. I have the problem with both OpenGL and DirectX 9 plugins.
Does anyone know what the problem could be, or how I could debug it? Thanks!

As for compatibility with Ogre 2.0, I think that as long as the new renderers still recognize the externalWindowHandle parameter, it should work.
Hardware, n.: part of the computer you can kick

User avatar
excaliburHisSheath
Gnoblar
Posts: 18
Joined: Sat Feb 15, 2014 8:06 pm

Re: Using SDL2 With Ogre 2.0?

Post by excaliburHisSheath »

I made the upgrade to 2.0 and my existing code from 1.9 for hooking up to SDL2 seems to be working just fine. :D

User avatar
Daixiwen
Greenskin
Posts: 104
Joined: Fri Feb 08, 2013 11:30 am
Location: Oslo
x 14

Re: Using SDL2 With Ogre 2.0?

Post by Daixiwen »

Daixiwen wrote:I have a question about that... I'm using a similar procedure on my project, with SDL2 and Ogre 1.9. I open the window first with SDL and use the externalWindowHandle parameter to have Ogre use it. But then I noticed that the display is never antialiased, even if I set it in the configuration window.
Answering my own question after may months... ;) if someone else is having this problem, the solution is in the new 2.1 samples that also use SDL:

Code: Select all

  Ogre::ConfigOptionMap& cfgOpts = mRoot->getRenderSystem()->getConfigOptions();
[...]
  params.insert(std::make_pair("FSAA", cfgOpts["FSAA"].currentValue));
  params.insert(std::make_pair("vsync", cfgOpts["VSync"].currentValue));
Those lines must be before the creation of the RenderWindow. It looks like those parameters need to be transmitter to the RenderWindow at creation for anti-aliasing to work.
Hardware, n.: part of the computer you can kick

rassweiler
Gnoblar
Posts: 13
Joined: Mon Aug 04, 2014 10:28 pm
Location: Ottawa

Re: Using SDL2 With Ogre 2.0?

Post by rassweiler »

Has anyone seen an issue where x11 is not a member of SysWMinfo?

I tried using win.window but the ogre render part is offset to the sdl window.

windows 10 x64
vs2013 community
Opengl3+
Win 10 x64 Pro
vs2013 Community
x64 Build debug and release
GTX 680
Maximus VI Hero
i7-4790k @ 4GHz
32GB @ 1333 Patriot

User avatar
Daixiwen
Greenskin
Posts: 104
Joined: Fri Feb 08, 2013 11:30 am
Location: Oslo
x 14

Re: Using SDL2 With Ogre 2.0?

Post by Daixiwen »

x11 is only a member of SysWMinfo on the Linux platform. On Windows you have to use win.window
Hardware, n.: part of the computer you can kick

rassweiler
Gnoblar
Posts: 13
Joined: Mon Aug 04, 2014 10:28 pm
Location: Ottawa

Re: Using SDL2 With Ogre 2.0?

Post by rassweiler »

Daixiwen wrote:x11 is only a member of SysWMinfo on the Linux platform. On Windows you have to use win.window
Ahhhhhhhh right thank you!
Win 10 x64 Pro
vs2013 Community
x64 Build debug and release
GTX 680
Maximus VI Hero
i7-4790k @ 4GHz
32GB @ 1333 Patriot

Post Reply