purebasic code help

Archives Forums/Blitz3D SDK Programming/purebasic code help

Staton_Richardson(Posted 2007) [#1]
Any Ideas on how to get the bbkeydown event check
working in purebasic. I think it has something to
do with a callback but I'm not quite sure how to
make it work?

just save the code into the directory with the purebasic
examples


; PureBasic Visual Designer v3.95 build 1485 (PB4Code)

Enumeration
  #Window_0
EndEnumeration

;- Gadget Constants
;
Enumeration
    #Button_0
  #Image_2
EndEnumeration

;- Image Plugins

;- Image Globals
Global Image0

;- Catch Images
Image0 = CatchImage(0, ?Image0)

;- Images
DataSection
Image0:
  IncludeBinary "boing.png"
EndDataSection

Procedure Open_Window_0()
  If OpenWindow(#Window_0, 209, -11, 1024, 768, "New window ( 0 )",  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar )
    If CreateGadgetList(WindowID(#Window_0))
      ButtonGadget(#Button_0, 900, 20, 45, 25, "")
      ImageGadget(#Image_2, 128, 24, 800, 600, Image0)
     EndIf
  EndIf
EndProcedure


Declare blitzroutine()
Open_Window_0()
; Play with this value!

#NUM_CUBES = 200 ; 1000 - 2000 fine here on Core 2 Duo 6300 / Geforce 6800 GS

; Include Blitz3D engine...

IncludeFile "blitz3dsdk.pbi"
bbSetBlitz3DHWND ( WindowID(#Window_0))

; Initialise Blitz3D engine...

bbBeginBlitz3D ()

; Set debug mode (it's on by default -- just added this so you can turn it off)...

bbSetBlitz3DDebugMode (0) ; 1 = DEBUG ON, 0 = DEBUG OFF


; Open dislay...

bbGraphics3D (800,600,32,2 )

; Camera...

Global cam = bbCreateCamera ()
bbCameraClsColor (cam, 64, 128, 180)
bbCameraRange (cam, 0.1, 1000)
bbMoveEntity (cam, 0, 5, 0)

; Light...

light = bbCreateLight ()
bbMoveEntity (light, -10, 5, -1)

; Grass...

grasstex = bbLoadTexture ("grass.png")
plane = bbCreatePlane ()
bbEntityTexture (plane, grasstex)

; Cube (will be hidden and 20 copies made)...

cubetex = bbLoadTexture ("boing.png")
Global cube = bbCreateCube ()
bbMoveEntity (cube, 0, 0, 5)
bbEntityTexture (cube, cubetex)
bbHideEntity (cube)

; Point light to centre of world...

bbPointEntity (light, cube)

; Create duplicates of cube entity...

Global Dim cubes (#NUM_CUBES)

For dupe = 1 To #NUM_CUBES
    cubes (dupe) = bbCopyEntity (cube)
    bbPositionEntity (cubes (dupe), Random (50) - 25, Random (25) + 2, Random (50))
    bbTurnEntity (cubes (dupe), Random (720) - 360, Random (720) - 360, Random (720) - 360)
Next



Repeat ; Start of the event loop

  blitzroutine()
   
  Event = WindowEvent() ; This line waits until an event is received from Windows
  

 If Event<>0 
  WindowID = EventWindow() ; The Window where the event is generated, can be used in the gadget procedures
  
  GadgetID = EventGadget() ; Is it a gadget event?
  
  EventType = EventType() ; The event type
  
  ;You can place code here, and use the result as parameters for the procedures
  
  If Event = #PB_Event_Gadget
    
    If GadgetID = #Button_0
      
    ElseIf GadgetID = #Image_2
      
    EndIf
    
  EndIf
 EndIf
 
Until Event = #PB_Event_CloseWindow ; End of the event loop

bbEndBlitz3D ()

End
;
Procedure blitzroutine()
   ; Rotate all cubes...
    
    For dupe = 1 To #NUM_CUBES
        bbTurnEntity (cubes (dupe), 0.1, 0.2, 0.3)
    Next

    ; Camera control...
    
    ; Turn left/right...
    
    If bbKeyDown (#KEY_LEFT)
        bbTurnEntity (cam, 0, 1, 0)
    Else
        If bbKeyDown (#KEY_RIGHT)
            bbTurnEntity (cam, 0, -1, 0)
        EndIf
    EndIf
    
    ; Tilt up/down...
    
    If bbKeyDown (#KEY_UP)
        bbTurnEntity (cam, 1, 0, 0, 0) ; The last parameter here makes sure we rotate
    Else                                ; on the 3D world's y-axis. Change to 1 to see
        If bbKeyDown (#KEY_DOWN)        ; the undesired effect of rotating on the entity's
            bbTurnEntity (cam, -1, 0, 0, 0) ; own y-axis (at least in this case)...
        EndIf
    EndIf

    ; Forwards/backwards...
    
    If bbKeyDown (#KEY_A)
        bbMoveEntity (cam, 0, 0, 0.25)
    Else
        If bbKeyDown (#KEY_Z)
            bbMoveEntity (cam, 0, 0, -0.25)
        EndIf
    EndIf
    
    ; Render 3D world to back buffer...

    bbRenderWorld ()
    
    ; Show the rendered buffer (flips back buffer to front buffer)...
    
    bbFlip ()
    
 EndProcedure



skidracer(Posted 2007) [#2]
You may want a timer event to go off so that your blitzroutine is called repeatedly. The bbkeydown code should just work unless you have taken the focus away from the graphics3d window in which case you could be processing arrow keys etc as hotkeys or keydown pb windowevent handlers.

Alternatively find a command like WindowEvent that doesn't wait but comes back immediately if there are no pending events. If there is one call that instead until it doesn't return any events then call your blitzroutine and then use a Sleep(10) which should put your app back in system friendly wait mode unless an event is outstanding (a PB WindowEvent command with a specified timeout option would also be a good choice if it has one).


BlitzSupport(Posted 2007) [#3]
The PB non-waiting event command is WindowEvent, or you can use WaitWindowEvent with a timeout value, eg. WaitWindowEvent (10).

I think you will need to write a custom PB event handler. You'd use bbSetBlitz3DEventCallback to point to the custom PB event handler, but I don't know enough about low-level event processing to do it myself.


Mesh(Posted 2007) [#4]
WaitWindowEvent (10) - bad method, program loses events...
______________________________
All simply =)


Staton_Richardson(Posted 2007) [#5]
It's not the WaitWindowEvent()
I'm using the WindowEvent()

It's this line
bbSetBlitz3DHWND ( WindowID(#Window_0))

if you comment it out the bbgraphic3d window opens in
a separate window. By using the command it puts it inside
the PB window. try commenting and then use the arrow keys
and it works fine. When you leave it in as in the code
above it kills the input going to the blitzwindow that's
why I think it needs a callback.

this command may or may not hold the key
bbSetBlitz3DEventCallback ( callback )

"The following BlitzMax code demonstrates the use of an eventhandler which enables standard BlitzMax input commands to be used with the Blitz3DSDK."

but the code I can't figure out how to use in purebasic

pb also has a callback method and it's not documented or
else I don't understand the documentation.

I know that the windows messages don't make it back to pb
to be handled by the bb functions. so with a callback you
would be redirecting those messages back to where they need
to go after you change the handle of bbGraphic3d window.