Put Blitz 3d inside of anything

Community Forums/Showcase/Put Blitz 3d inside of anything

Picklesworth(Posted 2005) [#1]
I dug this up and it took me two days to post here for some reason...

This is quite simple, you may have thought of it or done it in fact.

Basically, if you happen to make a program/applet/plugin and you want a part of it done in Blitz, Look No Further! I hope. It has to be a fully self-contained blitz app though; don't expect the other program to pass you variables and stuff. I may make a cross-program data sending dll in the next 30 years if there isn't one easily findable though to solve that... But that has nothing to do with this.

Now, what this does is the main program executes the blitz program, and passes it a single command line value, which is its own window handle (in the user32 dll).
Then the blitz program uses that command line value to pop itself inside of its new parent window (using win32 dll), thus creating a nice effect, useful for stuff. For instance, if you made a web plugin with c++, you could grab the window handle and actually have blitz doing all the plugin stuff. Useful for online game applications, vrml plugins, etc.
That is all in theory, but I feel that it should work unless Windows and web browsers are stupid.

So the BB part pretty much embeds itself, with only a tiny bit of help from the parent window's end. That help isn't even needed, if you use various other methods to find the parent's handle, which can be found.


Anyway, here's my code:

Main window:
MyAppTile$ = "Embedder_MainWindow"
Include "Consts_Win32.bb"

Graphics 800,600,0,2
AppTitle MyAppTile$

Global hWnd = SystemProperty("AppHWND")
;NOTE: Now covered by self-embedder
;style=GetWindowLong(hWnd ,GWL_STYLE)
;style = style Xor WS_CLIPCHILDREN 
;SetWindowLong hwnd,GWL_STYLE,style

ExecFile "SelfEmbedder.exe " + hWnd

; First parameter is the scancode to fake, the second is the window name
;InstallCloseHandler(222, MyAppTile$) ;Removed. Blitzclose.dll
;Using key 222 because that's the Power key. 
;If someone were to press it, they would want the app gone anyway :)

exfg = 0
While (Not KeyHit(1))
	;If KeyHit(222) Then exfg = 1 ;for clicking on [X]

	;If exfg = 1 Then Exit
Wend

;UnInstallCloseHandler()
End


Child window. Note: MUST be compiled as SelfEmbedder.exe to work in this example.
MyAppTile$ = "Embedder_ChildWindow"
Include "Consts_Win32.bb"

Graphics3D 500,500,0,2
AppTitle MyAppTile$
SetBuffer BackBuffer()

Global hWnd = SystemProperty("AppHWND") ;the blitz runtime window
style=GetWindowLong(hWnd ,GWL_STYLE)
style = style Xor WS_BORDER + WS_CAPTION ;style = style Xor WS_DISABLED
SetWindowLong hwnd,GWL_STYLE,style

Global Parent = CommandLine() ;handle of parent window passed to embedder via the command line
style=GetWindowLong(Parent,GWL_STYLE)
style = style Xor WS_CLIPCHILDREN 
SetWindowLong Parent,GWL_STYLE,style
	
api_SetParent hWnd,Parent

;useless 3d test stuff
AmbientLight 200,200,200
camera = CreateCamera()
PositionEntity camera,-5,0,0
CameraClsColor camera,255,255,255
cube = CreateCube()
PointEntity camera,cube

; First parameter is the scancode to fake, the second is the window name
;InstallCloseHandler(222, MyAppTile$)

exfg = 0
While (Not KeyHit(1))
	;Exiting the app
	;If KeyHit(222) Then exfg = 1 ;for clicking on [X]
	If Not api_IsWindow(Parent) Then exfg = 1
	
	;Program stuff
	MXsp# = -MouseXSpeed()
	MYsp# = MouseYSpeed()
	If MouseDown(1) Then TurnEntity camera,MYsp#,MXsp#,0
	If KeyDown(17) Then MoveEntity camera,0,0,0.1
	If KeyDown(31) Then MoveEntity camera,0,0,-0.1
	If KeyDown(30) Then MoveEntity camera,-0.1,0,0
	If KeyDown(32) Then MoveEntity camera,0.1,0,0
	TurnEntity cube,1,2,3
	RenderWorld	
	;end 3d stuff
	
	Flip
	;Cls
	If exfg = 1 Then Exit
Wend

;UnInstallCloseHandler()
End


So there you are. Please help me test this to see if it actually does work, and give me any suggestions/ideas you have.


Picklesworth(Posted 2005) [#2]
Woops!
And Consts_Win32.bb:



And if User3d.decls doesn't have everything:
http://www.blitzbasic.com/Community/posts.php?topic=27586


Please don't comment too much on these two bits of stuff. I'd like to hear about my thing :)


Beaker(Posted 2005) [#3]
Got it working by removing the CloseHandler calls (I don't have it installed anymore).

Is it possible to position the embedded window?

Will it work with a B+ window as the parent?


xmlspy(Posted 2005) [#4]
Check this one:
http://www.blitzbasic.com/codearcs/codearcs.php?code=1092


Damien Sturdy(Posted 2005) [#5]
excexerlent dude :D


Picklesworth(Posted 2005) [#6]
Yup, there's a window x and y thing in the win32 constants (or maybe somewhere else in the api). Presumably, you just use setwindowlong and that stuff to position the window.
Removing closehandler calls in the posted code right now actually to make it easier to get going.


TeaVirus(Posted 2005) [#7]
Cool! I'll give this a shot this evening. Using something similar to this, do you know if it would be possible to embed the contents of another (non blitz) window in the Blitz3d window?


Damien Sturdy(Posted 2005) [#8]
if the anser to what Tea Virus asked is Yes,This would be great for imbedding my VB Synth app GUI Into my blitz app... :)


jfk EO-11110(Posted 2005) [#9]
Yes, the BlitzWin3D lib shows it how to hook the Blitz Window to a parent window, indeed very useful stuff.

BTW. I'd recomend to get the Blitz HWND Handle this way:

myHWND=SystemProperty$("AppHWND")

, that is much saver than GetActiveWindow since GetActiveWindow only returns the active Window of the desktop and depending on an insanely clicking user this could be any window.


aab(Posted 2005) [#10]
Thanks! I could use this for Drawing movies in my (mini) games in C++ using Blitz movie commands. and Blitz_Movie (the lib). Id just be putting the main thread on hold, and creating a new window on top the size of the screens fullscreen mode.


Picklesworth(Posted 2005) [#11]
Thanks jfk, that's the function I was hoping for :)
Changed source on top. May want to make sure you have that one :)

Yes Tea, you can easily switch this thing over to most languages, assuming it has access to user32.dll, because it's all done with user32.

Thanks for the comments!
Glad I made something of use :)
Sorry it took me so long to show you guys.


Caff(Posted 2005) [#12]
That is neat...


Picklesworth(Posted 2005) [#13]
I just realized a practical use for this :)
I can make a loading game! It will slow down the loading process a bit, but who cares about that other game when you've got a nice Pong match going?


Does anyone know how I can get the position of the parent window without the use of crazy pointer stuff?

Edit: (5 months later)... Ah, blitzsys :)


John J.(Posted 2005) [#14]
Thank you so much for this! This is a huge help for my editor. Now I can use a nice easy-to-use windows GUI for my editor while still using Blitz3D for rendering.

I simply had to use api_SetWindowPos to position the window where I wanted after using your code to embed the window:
api_SetWindowPos hwnd, HWND_TOP, 1, 1, 640, 480, 0	;HWND_TOP, xpos, ypos, xsize, ysize, 0


I connect my GUI app to the embedded blitz3D app now by TCP/IP. Closing the Blitz3D window is handled by tests every 30 frames for TCP connection. If the connection is lost (in other words, if the GUI is closed), then the Blitz3D window closes too.


John J.(Posted 2005) [#15]
Oops.. did I just dig up a 7 month old post? Sorry.


Picklesworth(Posted 2005) [#16]
Sorry?

Thank you!

:)


John J.(Posted 2005) [#17]
:)

Hey.. maybe you should post this in the code archives if you haven't already so it won't get "lost". I'm sure a lot of people will benifit from this if you do.


John Blackledge(Posted 2005) [#18]
JohnJ-
Thank you so much for this! This is a huge help for my editor. Now I can use a nice easy-to-use windows GUI for my editor while still using Blitz3D for rendering.

How are you going to do that?
How are you going to pass changes from the (non-Blitz) editor through to the objects in the Blitz window/program?


Gabriel(Posted 2005) [#19]
TCP?


John J.(Posted 2005) [#20]
Yeah... I plan on using TCP
Unfortunately, I seem to be having problems with Blitz3D communicating by TCP with my app. Eof(tcp_stream) keeps returning -1 after reading from the stream. But if I use a C++ client program to connect to the GUI, it works fine. Maybe I'm using Blitz's TCP commands wrong. :(


Boiled Sweets(Posted 2005) [#21]
This looks pretty cool - so how can I embed blitz into a vb app?


John J.(Posted 2005) [#22]
Arrgh.. I wasted last evening trying to get blitz's TCP connection to work properly - FINIALLY I found that I needed to check ReadAvail() before reading and use ReadByte() instead of using ReadLine(). I wish the manual would have warned that ReadLine() destroys a TCP connection if there's nothing to read or nothing to read with a line feed on the end. Anyway, I'm happy now and my app is communicating with the embedded blitz3D program perfectly. :)

Boiled Sweets:

You'll just need to do the equivelant of this in Visual Basic:

hWnd = SystemProperty("AppHWND")
ExecFile "SelfEmbedder.exe " + hWnd


The blitz executable is the one that really does the work (it puts itself into the VB/etc. window). All you need to do is execute the blitz EXE, passing the handle of the VB window. I'm not sure about VB, but you can get a form's window handle in Borland C++ Builder like this: "Form1->Handle".


slenkar(Posted 2005) [#23]
can you a blitz game into a java applet inside of a web page?


Picklesworth(Posted 2005) [#24]
can you a blitz game into a java applet inside of a web page?

That's what I built this for!

I have run a few rough tests with the Blitz window shoved straight into a web browser, which worked.
(Both IE and Firefox... I also wrote a program which is in another thread that embeds IE or Firefox into the Blitz window, with a changed style to make it look like a part of the program)

Sadly, it turned out that Java was limited in two very specific ways: It can not get its own window handle, and it can not execute a file without a digital signature... And even then it's doubtful.

Thus, Java is obviously built for the shear purpose of causing my demise....

So you'd need to do a web plugin with a different language (c++ perhaps) in order to execute the Blitz program.


KuRiX(Posted 2005) [#25]
I have been looking for some 3d friendly library to work with c++ builder 5 interface, and i think this will help me a lot!

Really really thanks Mr PicklesWorth!

PD: 6 months ago and i see it now... wow


slenkar(Posted 2005) [#26]
unfortunate, it cant run inside java,theres always the 100dollar thingy that someone release a few months ago that lets it run in IE


KuRiX(Posted 2005) [#27]
But if you could get the window handle from internet explorer... should it work?


Picklesworth(Posted 2005) [#28]
But if you could get the window handle from internet explorer... should it work?

Yes :)
http://www.blitzbasic.com/Community/posts.php?topic=48327&hl=embed%20web%20browser
The code there grabs the window handle for IE or Firefox to do what it does. It could be made better, and you could add more web browsers.
That code could also be used to get the window handle for a Java applet, but it would require a lot of fiddling, digging, and other likely to fail things. (If civilization was built like a computer program, the introduction of the woodpecker would cause its demise).
And with that same style of fiddling and digging you can actually get Blitz to shove itself inside of a web browser's main viewing window. (But such a process is a bit more tricky, because you need a different to use FindWindowEx for non-top-level windows).

6 months ago and i see it now... wow
Yah... this is a very pleasant little surprise. Over the last week or so, I've been shocked to realize that people actually are using the things that I'm contributing. (Even my mesh position getting commands...).