Get Window Handle in OpenGL (and on Mac)

BlitzMax Forums/BlitzMax Programming/Get Window Handle in OpenGL (and on Mac)

Hi, does anyone know how to get the Window Handle of a Graphics window made in BMax in OpenGL using Graphics W,H,0 ?

I can do it in DirectX like this:

	Method GetActiveWindowSafe%()
		'For Win32 only.
		'GetActiveWindow() 'This old method is sometimes unreliable! (e.g. if another window gets focused when this one is being created.)
		'Use new more reliable method.
		Local my_driver:TD3D7Graphicsdriver=D3D7GraphicsDriver()
		Local my_graphics:TD3D7Graphics=my_driver.Graphics()
		Return my_graphics._hwnd
	End Method



And as you can see I don't want to use GetActiveWindow() as it is not always accurate (I can mess it up by clicking away from the MaxIDE as the game is compiling for example).

I recall the person who helped me with this code in the first place saying that the hwnd for OpenGL was not stored in the Blitz modules, so would I need to make a module mod to get it? (Maybe BRL can, fingers crossed!)

Also how can a similar thing be done on Mac? I'm using this Objective C code in a .m file:

NSWindow* macGetActiveWindow(){
    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
    NSApplication *myApp;
    myApp=[NSApplication sharedApplication];
    NSWindow *myWindow = [myApp keyWindow];
    [pool release];
	return myWindow;
}


Then I'm passing that to another objective C function to get the window origin:

void macGetWindowOrigin(NSWindow *myWindow, int *x, int *y){
    NSRect myFrame = [myWindow frame];
    *x = myFrame.origin.x;
    *y = myFrame.origin.y;
}


But basically it has the same issues where you can take the focus away from the game as it compiles and then you end up getting the window stats for a different window!

Any help much appreciated thanks! This code will make it into the next framework update for my customers.

I would love to help GA... but I haven't got a clue! Anyone else?

it has the same issues where you can take the focus away from the game

Well, indeed... because *your* window isn't always the active window :-p


Ideally, retrieving the window handle would be part of the API - why not, it's probably useful to some people.
It's actually very easy then to access a handle if you have a built-in function to get it from :-)


However, I dunno if Mark will be taking any more of my "hacks" for a while, since I've kind of flooded him with lots of them lately...

In answer to the original question:
does anyone know how to get the Window Handle of a Graphics window made in BMax..

the answer is yes, but best implementation would be to add a new Function you can call :-)

Perhaps something like this knocked together snippet :

SuperStrict

Import "glue.m"

Extern
	Function macGetWindowOrigin(window:Int, x:Int Var, y:Int Var)
End Extern



Graphics 800, 600, 0


Local handle:Int = GraphicsWindowHandle()


SetColor 255, 255, 255

While Not KeyDown(key_escape)

	Local x:Int, y:Int
	macGetWindowOrigin(handle, x, y)


	Cls
	
	DrawText "hello", 10, 10
	
	DrawText x + ", " + y, 200, 200
	
	Flip


Wend

where GraphicsWindowHandle() returns the handle to the window - if there is one.
Running here on the Mac (OpenGL) :-)
(note that on OS X, origin is in bottom-left corner of the screen, so you would need to do some math on y)

Requires changes to BRL.Graphics, BRL.GLGraphics, BRL.GLMax2D ... and related modules.

With modifications to BRL.dxgraphics and BRL.d3d7max2d, GraphicsWindowHandle() seems to cover all Windows possibilities.


What would one really want the window handle for anyway?

You can get the hwnd without modifying BRL module. Just do some copy work.
C side:
#include <windows.h>
/* copied from BRL.glgraphics */
typedef struct BBGLContext{
	struct BBGLContext *succ;
	int mode,width,height,depth,hertz,flags;
	HDC hdc;
	HWND hwnd;
	HGLRC hglrc;
} BBGLContext;


HWND getGLhwnd(BBGLContext *c){
	return c->hwnd;
}


BMX side:

Import "glhwnd.c"
Extern
	Function getGLhwnd:int(c:Int)
End Extern

'test
Local g:TGLGraphics = TGLGraphics(GLGraphics(640, 480)) 'get GLGraphics
print getGLhwnd(g._context)


This works on win32. No idea about Macos (since I don't have one).

Yeah, or you can have lots of different functions to get the same information :-)

@Tommo: Wow that seems pretty neat! I haven't tried it out yet though. So that's a non-module hack solution, which is my perference. I can try it on Mac too.

@Brucey: Yeah that command you've invented would be fantastic. I often use the window handle to pass to various Windows and Mac API functions. For example I get the moues cursor relative to the desktop so I can slide it smoothly in and out of the BMax window. Also I like to reposition the window centrally on the desktop. I know there are module hacks for those (which you specialise in) but I prefer not to do them as I'd have to re-do them after every BMax update and also get anyone else using my framework to do them. I know it's a self-imposed resistance to a useful solution though...

I can try it on Mac too.

You'll need even more code for the Mac version, I'm afraid :-)

Also I like to reposition the window centrally on the desktop.

I've been trying to get those hacks integrated officially, but things are going slowly...

but I prefer not to do them

I completely understand... which is why these things should really be part of BlitzMax proper.

You'll need even more code for the Mac version, I'm afraid :-)
Ah yes, should have noticed the include windows.h

which is why these things should really be part of BlitzMax proper.
Indeed. Let's hope they get added one day...

Brucey: Do you think it's possible to do a similar thing to Tommo's code on the Mac? As in not have to make module changes?

Probably :-)

OK cool, we'll this may have to be something I investigate later.