Page 1 of 1

[Irrlicht] Simple Starters Skeleton

Posted: Sat Jan 05, 2008 7:51 pm
by Thalius
Simple Purebasic & Irrlicht Starters Framework.

Image

>>>> Download

German:
See also: http://www.purebasic.fr/german/viewtopic.php?t=15360

Source:

Code: Select all

;; ----------------------------------------------------------------------------
;; Irrlicht Wrapper For Imperative Languages - Purebasic Examples
;; IrrlichtWrapper and FreeBasic-Example by Frank Dodd (2006)
;; Improved IrrlichtWrapper - Michael Taupitz (2007)
;; PureBasic-Examples by Michael Taupitz (neotoma) & Marius Eckardt (Thalius)
;; ----------------------------------------------------------------------------
;; Very Simple Irrlicht Starters base-example on how to setup/handle a scene.
;; Last Edit: 07.01.2008 - Thalius

;- Includes
XIncludeFile "Irr3DRequester.pb"

;- Defines
; = System =
; Globals
Global Quit.l = #False
; Locals
*MyEvent = 0

;= Irrlicht =
; Camera / Action
Global *camera.irr_camera
; Nodes
Global *cube.irr_node, *cube2.irr_node, *cube3.irr_node, *cube4.irr_node
; Textures
Global *CubeTexture.irr_texture, *CubeTexture2.irr_texture, *LogoTexture.irr_texture
; Animators
Global *irb.irr_animator, *irb2.irr_animator, *irb3.irr_animator, *irb4.irr_animator
; Media
Global *BitmapFont

;- Functions
; Init
Procedure InitScene()
  ; Init Camera
  *camera = IrrAddCamera(0,0,4,0,0,0)
  If *camera
    IrrSetCameraClipDistance(*camera,10) ; We set this to 10 to save some extra cpu, since our Scene is very small
    IrrSetCameraNearValue(*camera,0.1)
  Else
    ProcedureReturn #False
  EndIf
 
  ; Get Media
  *BitmapFont = IrrGetFont( "media/bitmapfont.bmp" )
  If Not *BitmapFont
    ProcedureReturn #False
  EndIf
 
  ; Add SceneNode (ofc i could copy the first here, but since Cube is a Buildin scene node ... )
  *cube = IrrAddCubeSceneNode(2)
  *cube2 = IrrAddCubeSceneNode(2)
  *cube3 = IrrAddCubeSceneNode(2)
  *cube4 = IrrAddCubeSceneNode(2)
  If *cube
    IrrSetNodePosition(*cube,0,0,4)
    IrrSetNodePosition(*cube2,0,0,4)
    IrrSetNodePosition(*cube2,0,0,1)
    IrrSetNodePosition(*cube4,0,0,4)
  Else
    ProcedureReturn #False
  EndIf
 
  ; Load Textures
  *CubeTexture = IrrGetTexture( "media/logo.png" )
  *CubeTexture2 = IrrGetTexture( "media/fire.bmp" )
  *LogoTexture = IrrGetTexture( "media/irrlichtlogo.png" )
  If *CubeTexture And *CubeTexture2 And *LogoTexture
    ; assign Textures
    IrrSetNodeMaterialTexture( *cube, *CubeTexture2, 0 )
    IrrSetNodeMaterialTexture( *cube2, *CubeTexture, 0 )
    IrrSetNodeMaterialTexture( *cube3, *CubeTexture, 1 )
    IrrSetNodeMaterialTexture( *cube3, *LogoTexture, 0 )
    IrrSetNodeMaterialTexture( *cube4, *CubeTexture2, 0 )
  Else
    ProcedureReturn #False
  EndIf
 
  ; Set Material Flags
  IrrSetNodeMaterialFlag( *cube, #IRR_EMF_LIGHTING, #IRR_OFF )         ; Ignore Lighting ( as we have no lights here duh ! )
  IrrSetNodeMaterialFlag( *cube, #IRR_EMF_BACK_FACE_CULLING, #IRR_OFF) ; Draw Backfaces ( This disables some Optimization
                                                                       ; but makes it possible to see our Cube from the Inside )
  IrrSetNodeMaterialType( *cube, #IRR_EMT_TRANSPARENT_ADD_COLOR)       ; Set our Material rendering Method -> Add Colour
  IrrSetNodeMaterialFlag( *cube2, #IRR_EMF_LIGHTING, #IRR_OFF )
  IrrSetNodeMaterialFlag( *cube2, #IRR_EMF_BACK_FACE_CULLING, #IRR_OFF)
  IrrSetNodeMaterialType( *cube2, #IRR_EMT_TRANSPARENT_ADD_COLOR)
  IrrSetNodeMaterialFlag( *cube3, #IRR_EMF_LIGHTING, #IRR_OFF )
  IrrSetNodeMaterialFlag( *cube3, #IRR_EMF_BACK_FACE_CULLING, #IRR_OFF)
  IrrSetNodeMaterialType( *cube3, #IRR_EMT_TRANSPARENT_REFLECTION_2_LAYER)  ; Set our Material rendering Method -> 2 Texture Layer / Reflective Material
  IrrSetNodeMaterialFlag( *cube4, #IRR_EMF_LIGHTING, #IRR_OFF )
  IrrSetNodeMaterialFlag( *cube4, #IRR_EMF_BACK_FACE_CULLING, #IRR_OFF)
  IrrSetNodeMaterialType( *cube4, #IRR_EMT_TRANSPARENT_ADD_COLOR)
 
  ; Add Animator to each entity
  *irb = IrrAddRotationAnimator(*cube, 1,0.1,0)   
  *irb2 = IrrAddRotationAnimator(*cube2, 0.3,0.4,0) 
  *irb3 = IrrAddRotationAnimator(*cube3, 0.1,0.3,0.1)
  *irb4 = IrrAddRotationAnimator(*cube4, 0.8,0.2,0.3)
 
  If *irb And *irb2 And *irb3 And *irb4
    ProcedureReturn #True  ; Everything's fine !
  Else
    ProcedureReturn #False
  EndIf
 
EndProcedure

; Event Receiver Function ( ProcedureC !! )
ProcedureC _My_Events(*event.SEvent)
  Protected key.l
 
  If *event\EventType = #IRR_EET_KEY_INPUT_EVENT         
    ; arbitrate based on the key that was pressed
    key.l = *event\KeyEvent\key
   
    Select key
      Case #IRR_KEY_ESCAPE  ; -> Escape Quits
        If *event\KeyEvent\PressedDown
          If Quit.l = #False
            Quit.l = #True 
          EndIf
        EndIf       
       
      Case #IRR_KEY_SPACE
      ; Do something fancy here ;)   
        MessageRequester("Hello World!","- Game Paused -")
    EndSelect
   
  EndIf     
 
  ProcedureReturn #False
EndProcedure


;- Program Startup

; Startup
If Irr3DRequester()
  ; Set Title
  IrrSetWindowCaption("Purebasic & Irrlicht - Simple Starters Skeleton")
  ; Begin Scene
  If InitScene()
    ; Create an Custom Event Receiver ( here you can add your own eventextras also )
    *MyEvent = IrrCreateEventReceiver(@_My_Events())
   
    ; Activate our Eventreceiver
    IrrSetEventReceiver(*MyEvent)
   
    ;- Mainloop
    While IrrRunning() And Quit = #False
      ; Grab FPS
      fps.l = IrrGetFPS()
      ; Initialize Backgroundfill
      IrrBeginScene(0, 0, 55 )
      ; Start Drawing all 3D
      IrrDrawScene()
      ; Overlay Sprites
      IrrDraw2DImage( *LogoTexture, 0, 0)
      ; Now .. Overlay Draw 2D Font
      Irr2DFontDraw ( *BitmapFont,"FPS: "+Str(fps),4,80,0,0)
      ; End Scene and Flip Buffers
      IrrEndScene()
      ; Dont Idleburn CPU
      Delay(1)
    Wend
  Else
    MessageRequester("Hepl!","Unable to inizialise Scene - see Console for Details.")
  EndIf
  IrrStop()
EndIf
;- End
End 
Thalius