Seite 1 von 1

[Irrlicht] Simples Start'ers Framework.

Verfasst: 05.01.2008 20:33
von Thalius
Hi!

Hier ein kleines Beispiel wie man seine Irrlicht-App / Spiel sauber aufbauen könnte.

PS. weiss jemand wer das Z Model gemacht hat ? ^^

Bild

>>>> Download

Source:

Code: Alles auswählen

;; ----------------------------------------------------------------------------
;; 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

Verfasst: 06.01.2008 09:04
von tft
Hi ...

ja .. sehr aufgeräumt. Sieht gut aus.

Verfasst: 06.01.2008 19:07
von STARGÅTE
Kleiner Hinweis:

Wenn man in diesem Einstellungsmenü auf abbrechen klickt stürtzt die exe ab.

Verfasst: 06.01.2008 19:25
von Andreas_S
läuft herforagend :allright:


hat das einen grud, wiso es bei höchster einstellung bei dx9 laggt und bei OpenGL nicht?

Verfasst: 06.01.2008 19:33
von STARGÅTE
hängt denke ich mal von der Grafikkarte ab.

Bei mir läuft es mit DX9 (70FPS) ehr besser als mit OpenGL (50FPS) wenn ich alle Optionenne auf AN schalte

Verfasst: 06.01.2008 21:27
von ZeHa
Ich glaub DarkDragon hat mal ein Z-Modell gemacht, sieht sehr stark nach seinem Werk aus.

Verfasst: 06.01.2008 21:58
von Thalius
Wenn man in diesem Einstellungsmenü auf abbrechen klickt stürtzt die exe ab.
Ha danke! IrrStop() sollte natürlich noch im IF liegen. Fixe das grad schnell. :)

hat das einen grud, wiso es bei höchster einstellung bei dx9 laggt und bei OpenGL nicht?
Treiberproblem , unkommentier mal des delay oder stells auf 1 und teste nochmals. DX braucht einfach bruteforce für n gescheites timing - nehm ich an. Mit OpenGL gehts schon in nem synchronen Renderloop ( extrem niedrige CPU Last ). Gibt halt so viele Karten und Treiber mittlerweile ( gute sowie schlechte ).

Denke als Anmerkung ists mal gut sich ein subsystem auszusuchen und dann dieses zu benutzen ( pers. hab ich lieber OGL , da Linuxtauglich und CPU schoneneder - ok DX exes unter wine/Linux laufen auch ).

Danke! :)

EDIT: Kleiner Crashbug & Delay fixed :)

Thalius

Verfasst: 14.02.2008 18:12
von Modder
Sehr schön gemacht. :allright:

Verfasst: 14.02.2008 19:45
von bommel
Super gemacht! Da kann selbst ich mich mal in die Materie einarbeiten.

Liegt das an meinem Ati-Treiber, daß sich OpenGL nicht um die VSync-Option kümmert. bei 800x600 Fenstermodus OpenGL mit oder ohne Vsync ca. 140 Frames/s.
Bei DirectX sind es ohne VSync ca. 150 und mit VSync 75 (Bei mit die Bildwiederholrate) Frames/s.
Angesichts meiner doch schon betagten ATI 9600er find ich die Werte nicht schlecht.

Die Option "FPU nutzen" macht übrigens keinen unterschied in der Framerate aus.