Code archives/3D Graphics - Misc/B+ GUI controlling a B3D program

This code has been declared by its author to be Public Domain code.

Download source code

B+ GUI controlling a B3D program by Skitchy2003
Skitchy's B+ GUI in B3D Example.
** Save BOTH programs in the SAME DIRECTORY or it wont work **

Demonstrates how to use a floating GUI with your Blitz 3D program using a 'swapfile'.
In order to pass data back into the GUI use EXACTLY the same method with a second swapfile (written by the 3D window). Don't forget that the GUI could send 'requests' for information that would prompt the 3D window to pass information back using it's own equivelant of the guiout() function (b3d_window_out() or whatever)- you just have to code it yourself. Believe me - it's all do-able because I've done it! ;)
*In this example you need to run the GUI first as it creates a blank swapfile.*
;THE BLITZ3D PART

Graphics3D 800,600,16,2
AppTitle("Skitchy's B+ GUI with B3D example")
;these variables get passed in from the GUI swapfile
Global guimsgtype
Global guistring$
Global guistring2$
Global guifloat#
Global guifloat2#
Global guifloat3#

;make a simple scene
cam=CreateCamera()
cube=CreateCube()
light=CreateLight()
PositionEntity light,-3,0,3
PositionEntity cam,-3,5,-8
PointEntity cam,cube

;main loop begins
While Not KeyHit(1)

;this bit makes the program read the "guiout.swp" once every 10 updates.
readtimer=readtimer+1
If readtimer>10 Then readtimer=1

If readtimer=1
	Repeat
		guiout=OpenFile("guiout.swp")
		If guiout=0 
			DebugLog "unable to open swapfile - probably in use by GUI window - retrying in 1/2 sec - have you run the GUI first"
			Delay 500
		EndIf
	Until guiout<>0

	guimsgtype=ReadInt(guiout)
	guistring$=ReadString(guiout)
	guistring2$=ReadString(guiout)
	guifloat#=ReadFloat(guiout)
	guifloat2#=ReadFloat(guiout)
	guifloat3#=ReadFloat(guiout)

	CloseFile(guiout)
	;if there was an instruction in the file then overwrite it to ensure that the instruction is not repeated
	If guimsgtype<>0 Then clearguioutfile()
	
EndIf

;check what was pressed in the gui and carry out the instructions
If guistring$="changecolor" Then EntityColor(cube,guifloat,guifloat2,guifloat3)

;clear the variables from the gui to ensure instructions are not repeated
guimsgtype=0
guistring=""
guistring2=""
guifloat=0
guifloat2=0
guifloat3=0

;update the scene
TurnEntity cube,1,2,3
UpdateWorld
RenderWorld
Flip
Wend
End


Function clearguioutfile()
Repeat
swapout=WriteFile("guiout.swp")
Until swapout<>0

WriteInt (swapout,0)
WriteString(swapout," ")
WriteString(swapout," ")
WriteFloat(swapout,0)
WriteFloat(swapout,0)
WriteFloat(swapout,0)
WriteFloat(swapout,0)
WriteFloat(swapout,0)
CloseFile swapout
End Function




-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------


;THE BLITZPLUS PART

;create a blank output file. this will also stop the last command you did on the last run from being reproduced
guiout(0," "," ")
;the globals that the gui passes out to the 3D window.
Global guistring$
Global guistring2$
Global guifloat#
Global guifloat2#
Global guifloat3#
;create a simple gui
Win1 = CreateWindow("Skitchy B+ GUI in B3D Window Example",250,10,250,178,Desktop(),1)
changecolor = CreateButton("Change the box color",20,20,150,30,Win1,0)
;main gui handler loop
While WaitEvent()<>$803
	If EventID()=$401
		;if the button is pressed
		If EventSource()=changecolor
			;ask for a color
			RequestColor( 128,128,128 )
			r#=RequestedRed()
			g#=RequestedGreen()
			b#=RequestedBlue()
			;write the information to the output file 
			guiout(1,"changecolor"," ",r,g,b)
		EndIf
	EndIf
Wend

;function to write a TINY file that the 3D window will read.
Function guiout(msgtype,string1$,string2$=" ",float1#=0,float2#=0,float3#=0,float4#=0,float5#=0)
swapout=WriteFile("guiout.swp")
WriteInt (swapout,msgtype)
WriteString(swapout,string1$)
WriteString(swapout,string2$)
WriteFloat(swapout,float1#)
WriteFloat(swapout,float2#)
WriteFloat(swapout,float3#)
WriteFloat(swapout,float4#)
WriteFloat(swapout,float5#)
CloseFile swapout
End Function

Comments

None.

Code Archives Forum