Mac Windowing issue


#1

Hi All,

I am writing a custom mac window on which i draw stuff using Ogre gaming engine…I would like this window to be a child window of a juce component…
I looked at the juce OpenGL Component code and decided i would need a component somewhat similar…I find that in the OpenGL component “createOpenGLContext” there is no window being created for Mac…Any particular reason why this is done?
What I tried is creating a new Mac window(on which i draw using Ogre later) using HIViewComponentPeer and setting this as a child of my top level component

Here’s the code snippet of what Im doing…

void* juce_createOgreContext (MyOgreComponent* component)
{
jassert (component != 0);

HIViewComponentPeer* const parentPeer = dynamic_cast <HIViewComponentPeer*> (component->getTopLevelComponent()->getPeer());

if (parentPeer == 0)
    return 0;

OgreContextInfo* const oc = new OgreContextInfo();

HIViewComponentPeer* const peer =  new HIViewComponentPeer (component, parentPeer->getStyleFlags(),parentPeer->viewRef);	

   int i = 0;
AGLPixelFormat pixelFormat;
GLint attribs[ 20 ];

attribs[ i++ ] = AGL_NO_RECOVERY;
attribs[ i++ ] = GL_TRUE;
attribs[ i++ ] = AGL_ACCELERATED;
attribs[ i++ ] = GL_TRUE;
attribs[ i++ ] = AGL_RGBA;
attribs[ i++ ] = AGL_DOUBLEBUFFER;
attribs[ i++ ] = AGL_ALPHA_SIZE;
attribs[ i++ ] = 8;
attribs[ i++ ] = AGL_STENCIL_SIZE;
attribs[ i++ ] = 8;
attribs[ i++ ] = AGL_DEPTH_SIZE;
attribs[ i++ ] = 32;

attribs[ i++ ] = AGL_NONE;
pixelFormat = aglChoosePixelFormat (0, 0, attribs);
oc->_pixelFormat = pixelFormat;
   oc->_renderContext = aglCreateContext (pixelFormat, 0);
	
   aglSetDrawable ((AGLContext)oc->_renderContext, GetWindowPort (peer->windowRef));

oc->_nativeMacWindow = peer;   	
oc->_windowHandle = peer->windowRef;
oc->_nativeMacCarbonHandle = peer->viewRef;

return oc;	

}

I see that this new window is independent and not a child of the top level component…Am I doing something wrong?

Regards
Pravi


#2

HIViewComponentPeer? Are you still using 1.46?

If you move up to 1.50 there’s NSViewComponent that will probably do what you need.


#3

Hi Jules,

We still use 1.46 and moving to 1.50 is lots of code change for us…Is there no way I can do it with the older version…

Regards
Praveen


#4

It might be possible with a bit of work, but TBH it’s so long now since I moved away from the carbon code that I can’t really remember how it used to work!


#5

I’ve done this with 1.5. Its seriously easy.

Create a Juce window(works better with Native Title bar)

and then


// I just throw a regular old NSView in...
	ogreView=[[OgreView alloc]initWithFrame:NSMakeRect(0, 0, getWidth(),getHeight())];
	
	ComponentPeer *componentPeer=getPeer();
	jassert(componentPeer);
	NSView* const peer = (NSView*) (componentPeer->getNativeHandle());
	
	
	if (peer != 0)
	{
		[peer addSubview: (OgreView*)ogreView];
	}
	
	// Before CreateRenderWindow is called, it MUST be in a window.
	// If I use an NSViewComponent then is starts calling resize...
	
	Ogre::NameValuePairList misc;
	misc["macAPI"] = "cocoa";
	misc["externalWindowHandle"] = Ogre::StringConverter::toString((size_t)ogreView);
	
	// Create the window and load the params
	Ogre::Root::getSingleton().createRenderWindow("OgreWindow", 0, 0, false, &misc);

here’s my class declaration

[code]
class SMOgreView : public Component
{
public:
SMOgreView();
virtual ~SMOgreView();

	void initOgre();
	
	Ogre::RenderWindow *getRenderWindow();
	
	void resized();
	
private:
	void *ogreView; // Type is OgreView on mac (an NSView sub class)
};[/code]

But as Ogre’s handle has to be cocoa you NEED to be on 1.5!

Benefit to this is I get to use juce keyups/mousedowns which I then pass onto the ogre gui. No mucking about with OIS.

Just another example of something I thought would be hard, but turns out to be easy, thanks to Juce. THANKS JULIAN!


#6

[quote=“justin”]I’ve done this with 1.5. Its seriously easy.

Create a Juce window(works better with Native Title bar)

and then


// I just throw a regular old NSView in...
	ogreView=[[OgreView alloc]initWithFrame:NSMakeRect(0, 0, getWidth(),getHeight())];
	
	ComponentPeer *componentPeer=getPeer();
	jassert(componentPeer);
	NSView* const peer = (NSView*) (componentPeer->getNativeHandle());
	
	
	if (peer != 0)
	{
		[peer addSubview: (OgreView*)ogreView];
	}
	
	// Before CreateRenderWindow is called, it MUST be in a window.
	// If I use an NSViewComponent then is starts calling resize...
	
	Ogre::NameValuePairList misc;
	misc["macAPI"] = "cocoa";
	misc["externalWindowHandle"] = Ogre::StringConverter::toString((size_t)ogreView);
	
	// Create the window and load the params
	Ogre::Root::getSingleton().createRenderWindow("OgreWindow", 0, 0, false, &misc);

here’s my class declaration

[code]
class SMOgreView : public Component
{
public:
SMOgreView();
virtual ~SMOgreView();

	void initOgre();
	
	Ogre::RenderWindow *getRenderWindow();
	
	void resized();
	
private:
	void *ogreView; // Type is OgreView on mac (an NSView sub class)
};[/code]

But as Ogre’s handle has to be cocoa you NEED to be on 1.5!

Benefit to this is I get to use juce keyups/mousedowns which I then pass onto the ogre gui. No mucking about with OIS.

Just another example of something I thought would be hard, but turns out to be easy, thanks to Juce. THANKS JULIAN![/quote]

Hi Justin,
Will this code work if I use it with juce 1.46(Carbon version, it looks unlikely though).

Anyway

returns a window handle, can’t I just do this to place ogre component correctly.

ogreView=[[OgreView alloc]initWithFrame:NSMakeRect(0, 0, getWidth(),getHeight())];
	
	ComponentPeer *componentPeer=getPeer();
	jassert(componentPeer);
	WindowRef parentWindow = (WindowRef) peer->getNativeHandle();
	
	
	if (parentWindow != 0)
	{
		HIViewAddSubview (parentView, ogreView);
	}
	
	// I am hoping this is some ogre stuff that my small brain doesn't have to deal with :wink: 
	
	Ogre::NameValuePairList misc;
	misc["macAPI"] = "cocoa";
	misc["externalWindowHandle"] = Ogre::StringConverter::toString((size_t)ogreView);
	
	// Create the window and load the params
	Ogre::Root::getSingleton().createRenderWindow("OgreWindow", 0, 0, false, &misc);

#7

Don’t think so…

OgreView is an NSView…

Its in OgreOSXCocoaView…

@interface OgreView : NSView
{
	Ogre::RenderWindow *window;
}

- (id)initWithFrame:(NSRect)f;
- (id)initWithGLOSXWindow:(Ogre::RenderWindow*)w;

- (void)setOgreWindow:(Ogre::RenderWindow*)w;
- (Ogre::RenderWindow*)ogreWindow;

- (void)setFrameSize:(NSSize)s;

@end

And below that

@interface OgreWindow : NSWindow
{}

Their sample apps are mostly all carbon based, and there is probably a way to get it into a HIView somehow. I was just attesting that if its done in Cocoa it is possible to do everything you would want. (Event capturing, window resizing, etc)

Perhaps through OSXCGLContext, but that kind of question would be best asked on the OgreForums if no one here answers…

Seeing as Qt was/is carbon based perhaps this will help?

http://www.ogre3d.org/forums/viewtopic.php?f=2&t=27027&start=0[/quote]


#8

Thanks Justin.
All my plans have gone down the drain. :frowning: