Topic: Irrlicht with HighGUI

Re: Irrlicht with HighGUI

wow!  very cool smile  do you have some sample code or a brief tutorial on how to do this?

Re: Irrlicht with HighGUI

' HighGUI inside a MaxGUI canvas with Irrlicht
' Bashing together of examples (just as proof of concept) by WendellM - Aug 31, 2006
' Requires BlitzMax, MaxGUI, Irrlicht, GMan's Irrlicht 1.0 wrapper, and Diablo's HighGUI 3

Framework gg.IrrBMAX

Import Diablo.HighGUI
Import BRL.Max2D
Import BRL.FreetypeFont
Import BRL.Basic
Import BRL.Win32MaxGUI
Import BRL.BMPLoader
Import BRL.JPGLoader

Import BRL.D3D7Max2D
Import BRL.GLMax2D

Local D3D = True ' choice of D3D or OpenGL, also used for Irrlicht init below
If D3D Then SetGraphicsDriver D3D7Max2DDriver() Else SetGraphicsDriver GLMax2DDriver()
' Only D3D seems to work, but it should work with OpenGL as well (I haven't tried fixing)

Local windowWidth:Int = 800
Local windowHeight:Int = 600
Local window:TGadget=CreateWindow("Irrlicht Win32 BlitzMax meets HighGUI", ..
      20,35,windowWidth,windowHeight,Null,WINDOW_CLIENTCOORDS|WINDOW_HIDDEN|WINDOW_TITLEBAR)

Local hIrrlichtWindow:TGadget=CreatePanel(130,360,320,220,window,PANEL_ACTIVE)'|PANEL_BORDER)

Local param:T_irrSIrrlichtCreationParameters=T_irrSIrrlichtCreationParameters.create()
param.setWindowId(QueryGadget(hIrrlichtWindow,QUERY_HWND))

If D3D Then
    param.setDriverType(EDT_DIRECT3D9)'or whatever driver is desired
Else
    param.setDriverType(EDT_OPENGL)
EndIf

' create the device from the params object
Local device:T_irrIrrlichtDevice= ..
    T_irrIrrlichtDevice.createFromParams(param)

' the original does not have this.  this is just to show that you can mix events
' between MaxGUI and Irrlicht.
Type MyEventReceiver Extends T_irrIEventReceiver

    Field box:T_irrISceneNode

    Method setBox(b:T_irrISceneNode)
        box=b
    EndMethod

    Method OnEvent:Int(event:T_irrSEvent)

        ' check If user presses the key 'W'
        If event.getEventType()=EET_KEY_INPUT_EVENT And event.getKeyPressedDown()=False
        
            Local key:Int=event.getKeyInputKey()

            Select key
                ' switch wire frame mode
                Case EKEY_KEY_W  
                    box.setMaterialFlag(EMF_WIREFRAME, box.getMaterial(0).getWireframe()=False)
                    Return True    
            EndSelect            
        EndIf
        
        Return False
    EndMethod

    ' we must override the generate function in order for instantiation to work properly
    ' must return T_irrIEventReceiver
    Function generate:T_irrIEventReceiver()
        Return T_irrIEventReceiver(New MyEventReceiver)
    EndFunction
EndType


' setup a simple 3d scene

Local smgr:T_irrISceneManager=device.getSceneManager()
Local driver:T_irrIVideoDriver=device.getVideoDriver()

Local cam:T_irrICameraSceneNode=smgr.addCameraSceneNode();
cam.setTarget(T_irrVector3df.createFromVals(0,0,0))

Local anim:T_irrISceneNodeAnimator = ..
       smgr.createFlyCircleAnimator(T_irrVector3df.createFromVals(0,10,0),30.0)
cam.addAnimator(anim)
anim.drop()

Local cube:T_irrISceneNode=smgr.addTestSceneNode(25)
cube.setMaterialTexture(0,driver.getTexture("irrlicht/rockwall.bmp"))

smgr.addSkyBoxSceneNode( ..
    driver.getTexture("irrlicht/irrlicht2_up.jpg"), ..
    driver.getTexture("irrlicht/irrlicht2_dn.jpg"), ..
    driver.getTexture("irrlicht/irrlicht2_lf.jpg"), ..
    driver.getTexture("irrlicht/irrlicht2_rt.jpg"), ..
    driver.getTexture("irrlicht/irrlicht2_ft.jpg"), ..
    driver.getTexture("irrlicht/irrlicht2_bk.jpg"))

' create the event receiver that toggles wireframe
Local receiver:T_irrIEventReceiver=T_irrIEventReceiver.create(MyEventReceiver.generate)
MyEventReceiver(receiver).setBox(cube)
device.setEventReceiver(receiver)

' show And execute dialog
ShowGadget(window)

' do message queue

' Instead of this, you can also simply use your own message loop
' using GetMessage, DispatchMessage And whatever. Calling
' Device->run() will cause Irrlicht To dispatch messages internally too. 
' You need Not call Device->run() If you want To do your own message 
' dispatching loop, but Irrlicht will Not be able To fetch
' user Input Then And you have To do it on your own using the BlitzMax
' event mechanism.

SeedRnd(MilliSecs())
AppTitle = "HighGUI 3: Example 1a - canvas"

'Local win :tgadget = CreateWindow( AppTitle, 10,10, 850, 650)
Local canvasgui:tgadget = CreateCanvas(0,0, 800,600, window )

SetGraphics CanvasGraphics(canvasgui)
EnablePolledInput ' "undocumented" function to allow canvas to received non-event input

SetMaskColor(255, 0, 255)

hi_THighGUI.Init("theme")

Incbin "theme/background1.png"
Global background:TImage = LoadImage("incbin::theme/background1.png")

Local buttonExit:hi_TButton = hi_CreateButton("Exit Button", 10, 10, 140, 28)
Local button1:hi_TButton = hi_CreateButton("Example Button 1", 10, 50, 140, 28)
Local button2:hi_TButton = hi_CreateButton("Example Button 2", 10, 90, 140, 28)
Local button3:hi_TButton = hi_CreateButton("Example Button 3", 10, 130, 140, 28)

Global running% = True

SetBlend(ALPHABLEND)
ShowMouse()

Local angle
While device.run()

    driver.beginScene(True, True)
    smgr.drawAll()
    driver.endScene()

    Cls
    DrawImage background, 0, 0
    
    hi_THighGUI.Render()
    hi_THighGUI.Update()

    SetColor 0, 0, 0
    DrawText "FPS = " + TFPSCounter.FPS, 11, 600 - GetImageFont().height() - 1
    SetColor 0, 173, 0
    DrawText "FPS = " + TFPSCounter.FPS, 10, 600 - GetImageFont().height() - 2
    SetColor 255, 255, 255
    
    Local mem% = GCMemAlloced()
    SetColor 0, 0, 0
    DrawText "Mem = " + mem, 11, 600 - (GetImageFont().height() * 2) - 1
    SetColor 0, 173, 0
    DrawText "Mem = " + mem, 10, 600 - (GetImageFont().height() * 2) - 2
    SetColor 255, 255, 255
    
    Flip 0
    
    TFPSCounter.Update()
    
    GCCollect()

    PollEvent()
    Select EventID()
        Case EVENT_WINDOWCLOSE
            Exit
        Default
            PostEvent(CreateEvent(EventID()),True)
    End Select

    ' HighGUI event handling needs to be improved here to avoid input from Irrlicht panel
    If buttonExit.IsPressed() Then End

Wend


' the alternative, own message dispatching loop without Device->run() would
' look like this:

Rem
'While True
    'If PeekEvent() Then 
        PollEvent()
        Select EventID()
        Case EVENT_WINDOWCLOSE
        End
            Exit
'        Case EVENT_GADGETACTION    
'        End    
'            If EventSource()=Ok
            
'                PostEvent(CreateEvent(EVENT_WINDOWCLOSE,Ok))
'            End If
        End Select
    'End If        
    '// advance virtual time
    device.GetTimer().Tick()
    '// draw engine picture
    driver.beginScene(True, True, 0)
    smgr.drawAll()
    driver.endScene()
Wend
EndRem

device.closeDevice()
device.drop()
End

' Lil helper
Type TFPSCounter
    
    Global FPS%, frameCounter%, frameTimer%, totalFrames%
    
    Function Update()
        frameCounter:+ 1
        totalFrames:+ 1
        If frameTimer < MilliSecs()
            FPS = frameCounter
            frameTimer = 1000 + MilliSecs()
            frameCounter = 0 
        EndIf
    End Function
    
End Type