HGE Wrapper tip #1,#1A (update 12 JUN 2008)

Share your advanced PureBasic knowledge/code with the community.
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

HGE Wrapper tip #1,#1A (update 12 JUN 2008)

Post by Rook Zimbabwe »

I have just now started poking into the HGE wrapper located (for DL purposes) on thread:

http://www.purebasic.fr/english/viewtop ... ge+wrapper

And I have chopped together a PLINKO style starter to display how to set up objects and utilize them...

This example meerly shows how to create a series of sprites that you can bounce boxes off of...

I had to redo the includes because they kept giving me LINK errors as someone seems to think we are all going to dump every library in a folder called /include/Libraries/PBLIBS/... etc!!!

If you need help on that, post I will attempt to remember what I did and if I can't I will post the includes I modified.

Code: Select all

; adapted from Tutorial 9 with the boxes and chain bridge
; just wanted to see how it works - Rook Zimbabwe (Ralph Dunn)
; cannot seem to set up walls!

XIncludeFile "HGEWrapper_include.pbi"

Structure sPh
  *spr
  *body
EndStructure

Global *fnt
Global Dim phObj.sPh(1)
Global NumPhObj=0,Car,Wheel1,Wheel2,Axel1,Axel2,*spr1,*spr2
Global tex1,tex2,tex3,tex4,tex5
Global *sprCur, *gui
Global mx.f,my.f
Global *phWorld

Macro RandomArea(StartValue, EndValue, StepValue=0.1)
  ( Random(Int(((EndValue)-(StartValue))/(StepValue)))*(StepValue)+(StartValue) )
EndMacro

Procedure.d rnd_range(first.d, last.d)
  ProcedureReturn RandomArea(first,last)
EndProcedure

Procedure CreatePhObjBox(tex.l, ScaleX.f, ScaleY.f, PosX.f, PosY.f, Angle.f, Density.f=0, Restitution.f=0, Friction.f=0)
  ReDim phObj.sPh(NumPhObj)
  phObj(NumPhObj)\spr = hge_Sprite(tex,0,0,ScaleX,ScaleY)
  hge_Sprite_SetHotSpot(phObj(NumPhObj)\spr,ScaleX*0.5,ScaleY*0.5)      
  phObj(NumPhObj)\body = Ph_CreateBoxBody(*phWorld,ScaleX,ScaleY,Density,Restitution,Friction)      
  Ph_SetPosBody(phObj(NumPhObj)\body,PosX,PosY)
  Ph_SetAngleBody(phObj(NumPhObj)\body,Angle)
  NumPhObj = NumPhObj + 1
  ProcedureReturn phObj(NumPhObj-1)\body
EndProcedure

Procedure CreatePhObjCircle(tex.l, Radius.f, PosX.f,PosY.f,Angle.f, Density.f=0, Restitution.f=0, Friction.f=0)
  ReDim phObj.sPh(NumPhObj)
  phObj(NumPhObj)\spr = hge_Sprite(tex,96,64,Radius,Radius)
;  phObj(NumPhObj)\spr = hge_Sprite(tex1,0,0,Radius,Radius)
  hge_Sprite_SetColor(phObj(NumPhObj)\spr,$FF000000| RGB(rnd_range(1,255),rnd_range(1,255),rnd_range(1,255)))
  hge_Sprite_SetHotSpot(phObj(NumPhObj)\spr,Radius*0.5,Radius*0.5)      
  
  phObj(NumPhObj)\body = Ph_CreateCircleBody(*phWorld,Radius,Density,Restitution,Friction)
  Ph_SetPosBody(phObj(NumPhObj)\body,PosX,PosY)
  Ph_SetAngleBody(phObj(NumPhObj)\body,Angle)
  ;CallDebugger   
  
  NumPhObj = NumPhObj + 1
  ProcedureReturn phObj(NumPhObj-1)\body
  
EndProcedure

Procedure UpdatePhObj()
  For i = 0 To NumPhObj-1
    hge_Sprite_RenderEx(phObj(i)\spr,Ph_GetBodyX(phObj(i)\body),Ph_GetBodyY(phObj(i)\body),Ph_GetBodyAngle(phObj(i)\body))
  Next 
EndProcedure

ProcedureCDLL frameFunc()
  dt.f = hge_Timer_GetDelta()
  
  Ph_WorldStep(*phWorld,dt)
  
  hge_Input_GetMousePos(@mx,@my)
  
  If (hge_Input_GetKeyState(#HGEK_ESCAPE)) : ProcedureReturn #True : EndIf
 	
	If (hge_Input_KeyDown(#HGEK_LBUTTON)) ; mouse LFT button is pressed
	    CreatePhObjCircle(tex4,32,mx,my,0,1,0.5,2) ; make a circle
    EndIf
  
    If (hge_Input_KeyDown(#HGEK_RBUTTON))
        CreatePhObjBox(tex2,32,32,mx,my,0,1,0.5,2) ; make a square
    EndIf
   
  id=hge_GUI_Update(*gui,dt)  
  ProcedureReturn #False
  
EndProcedure

ProcedureCDLL RenderFunc()  
  ; // Render graphics
  hge_Gfx_BeginScene(#Null)
  hge_Gfx_Clear(0)
  UpdatePhObj()
  hge_GUI_Render(*gui)
  fps.l =  hge_Timer_GetFPS()
  hge_Font_SetColor(*fnt, ARGB(255,255,0,0));  RGB(255,255,255))
  
  hge_Font_Render(*fnt,10,10, #HGETEXT_LEFT, "FPS: "+Str(fps)+"(const)")

  hge_Gfx_EndScene()
  ProcedureReturn #False
EndProcedure


Procedure Main()
  hge_Create(#HGE_VERSION)
  
  ; // maybe a bug - somtime hge could Not load from current dir
  Path.s = GetCurrentDirectory()+"\Res\";
  Debug "path opened..."
  ; // Set up log file, frame function, render function And window title
  hge_System_SetStateString(#hge_LOGFILE, Path+"pbhge_plonk.log");
  hge_System_SetStateFunc(#HGE_FRAMEFUNC, @frameFunc())
  hge_System_SetStateFunc(#hge_RENDERFUNC, @RenderFunc())
  hge_System_SetStateString(#HGE_TITLE, "PloNK 0.0a LOG")
  ; // Set up video mode
  hge_System_SetStateBool(#HGE_WINDOWED,#True)
  hge_System_SetStateInt(#hge_SCREENWIDTH,1024)
  hge_System_SetStateInt(#hge_SCREENHEIGHT,768)
  hge_System_SetStateInt(#hge_SCREENBPP,32)
  hge_System_SetStateInt(#hge_FPS, 100)
  Debug "System set..."
  hge_System_SetStateBool(#HGE_USESOUND , #False)
  
  If hge_System_Initiate()
    ; // Load sound And texture
    tex1=hge_Texture_Load(Path+"bg2.png",0,#False);
    tex2=hge_Texture_Load(Path+"bg.png",0,#False);
    tex3=hge_Texture_Load(Path+"cursor.png",0,#False);
    tex4=hge_Texture_Load(Path+"particles.png",0,#False);
    tex5=hge_Texture_Load(Path+"zazaka.png",0,#False);  buggy.png
    Debug "Textures loaded..."
    *fnt = hge_Font(Path+"font2.fnt",0)
    *phWorld=Ph_CreateWorld(0,0,1024,768,0,100,1)
    Debug "Font loaded..."
; *** CreatePhObjBox(tex.l = texture name
                                    ;ScaleX.f = how wide object is
                                    ; ScaleY.f = how thick object is (theese two I am going to play with
                                    ; PosX.f = X start position
                                    ; PosY.f = Y start position
                                    ; Angle.f = angle of object (especially if rectangle!) for some reason 45 does not = up down nor does 90
                                    ; **** angle appears to work from CENTER of object 
                                    ; Density.f= how heavy the object is (0 = no move or 0 gravity effect!)
                                    ; Restitution.f= I HAVE NO IDEA YET
                                    ; Friction.f= how well objects collide with it... I think it is amount of rebound!
    CreatePhObjBox(tex1,1024,32,512,752,0,0,0.25,6) ; bottom was 0.3
    CreatePhObjBox(tex1,15,720,10,375,0,0,0.25,1) ; my really cruddy attempt at a wall 66 is closest to vertical
    CreatePhObjBox(tex1,15,720,1015,375,0,0,0.25,1)
    y = 130
    For times = 0 To 5
        For x = 35 To 960 Step 100
            CreatePhObjBox(tex2,6,6,x,y,40, 0, 0.2, 1)
            CreatePhObjBox(tex2,6,6,x+50,y+50,40, 0, 0.2, 1)
        Next
        y = y + 100
    Next
    
    *sprCur=hge_Sprite(tex3, 0, 0, 32, 32)
    *gui=hge_GUI()
    hge_GUI_SetCursor(*gui,*sprCur)
    Debug "cursor init..."
    hge_System_Start()
    
    
    hge_Font_Delete(*fnt)
    For i  = 0 To NumPhObj-1
	   	hge_Sprite_Delete(phObj(i)\spr)
    Next 
    hge_Sprite_Delete(*sprCur) 
    hge_GUI_Delete(*gui) 
		hge_Texture_Free(tex1)
		hge_Texture_Free(tex2)
		hge_Texture_Free(tex3)
		hge_Texture_Free(tex4)
  EndIf
    
  Ph_DeleteWorld(*phWorld)
  hge_System_Shutdown()
  hge_Release()    
EndProcedure

Main()
; next we feature a complete rewrite to see if we can get this into a REAL framework like PB uses
; in stead of this bastard C style version!!! (WHICH I HATE!!!)

End
I didn't do anything amazing... I was just playing with it to see how it was constructed. 8)
Last edited by Rook Zimbabwe on Thu Jun 12, 2008 5:40 pm, edited 2 times in total.
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

OK tip 1A... This lib needs some work... (BYO where are you?)

The following code is a modification. I used two images you can DL below the code... I just pulled them out of the same directory since this is a incredibly basic example!

Now you press LEFT mouse to fire the ball... If you press too quickly you will overwhelm the program... I know some of you are oging to do that anyway thinking this is some kind of PACHINKO machine!!!

Code: Select all

; adapted from Tutorial 9 with the boxes and chain bridge
; just wanted to see how it works - Rook Zimbabwe (Ralph Dunn)
; added a weird trigger that works on rebound.
; cannot get the bumper to actually be an angle

XIncludeFile "HGEWrapper_include.pbi"

Structure sPh
  *spr
  *body
EndStructure

Global *fnt
Global Dim phObj.sPh(1)
Global NumPhObj=0,Car,Wheel1,Wheel2,Axel1,Axel2,*spr1,*spr2
Global tex1,tex2,tex3,tex4,tex5
Global *sprCur, *gui
Global *phWorld

Macro RandomArea(StartValue, EndValue, StepValue=0.1)
  ( Random(Int(((EndValue)-(StartValue))/(StepValue)))*(StepValue)+(StartValue) )
EndMacro

Procedure.d rnd_range(first.d, last.d)
  ProcedureReturn RandomArea(first,last)
EndProcedure

Procedure CreatePhObjBox(tex.l, ScaleX.f, ScaleY.f, PosX.f, PosY.f, Angle.f, Density.f=0, Restitution.f=0, Friction.f=0)
  ReDim phObj.sPh(NumPhObj)
  phObj(NumPhObj)\spr = hge_Sprite(tex,0,0,ScaleX,ScaleY)
  hge_Sprite_SetHotSpot(phObj(NumPhObj)\spr,ScaleX*0.5,ScaleY*0.5)      
  phObj(NumPhObj)\body = Ph_CreateBoxBody(*phWorld,ScaleX,ScaleY,Density,Restitution,Friction)      
  Ph_SetPosBody(phObj(NumPhObj)\body,PosX,PosY)
  Ph_SetAngleBody(phObj(NumPhObj)\body,Angle)
  NumPhObj = NumPhObj + 1
  ProcedureReturn phObj(NumPhObj-1)\body
EndProcedure

Procedure CreatePhObjCircle(tex.l, Radius.f, PosX.f,PosY.f,Angle.f, Density.f=0, Restitution.f=0, Friction.f=0)
  ReDim phObj.sPh(NumPhObj)
  phObj(NumPhObj)\spr = hge_Sprite(tex,96,64,Radius,Radius)
;  phObj(NumPhObj)\spr = hge_Sprite(tex1,0,0,Radius,Radius)
  hge_Sprite_SetColor(phObj(NumPhObj)\spr,$FF000000| RGB(rnd_range(10,255),rnd_range(10,255),rnd_range(10,255)))
  hge_Sprite_SetHotSpot(phObj(NumPhObj)\spr,Radius*0.5,Radius*0.5)      
  
  phObj(NumPhObj)\body = Ph_CreateCircleBody(*phWorld,Radius,Density,Restitution,Friction)
  Ph_SetPosBody(phObj(NumPhObj)\body,PosX,PosY)
  Ph_SetAngleBody(phObj(NumPhObj)\body,Angle)
  ;CallDebugger   
  
  NumPhObj = NumPhObj + 1
  ProcedureReturn phObj(NumPhObj-1)\body
  
EndProcedure

Procedure UpdatePhObj()
  For i = 0 To NumPhObj-1
    hge_Sprite_RenderEx(phObj(i)\spr,Ph_GetBodyX(phObj(i)\body),Ph_GetBodyY(phObj(i)\body),Ph_GetBodyAngle(phObj(i)\body))
  Next 
EndProcedure

ProcedureCDLL frameFunc()
  ; this is where all your program really runs... This is what communicates with HGE
  ; all the bits of your code have to work in here.
  ; : (
  ; I must say HGE is a bit more limited than I had thought!
  
  dt.f = hge_Timer_GetDelta()
  
  Ph_WorldStep(*phWorld,dt)
  
    If hge_Input_GetKeyState(#HGEK_ESCAPE)
        ProcedureReturn #True
    EndIf

    If (hge_Input_KeyDown(#HGEK_LBUTTON)) ; mouse LFT button is pressed
	    CreatePhObjCircle(tex4,31,31,Random(80)+160,0,99,0.78,2) ; make a circle DENISTY 99
	    Debug "made a new ball"
	    ; CreatePhObjCircle(tex4,32,mx,my,0,10,0.5,2) ; ORIGINAL SET
    EndIf
   
; id=hge_GUI_Update(*gui,dt) 
  
  ProcedureReturn #False
  
EndProcedure

ProcedureCDLL RenderFunc()  
  ; // Render graphics
  hge_Gfx_BeginScene(#Null)
  hge_Gfx_Clear(0)
  UpdatePhObj()
  ;hge_GUI_Render(*gui)
  fps.l =  hge_Timer_GetFPS()
  hge_Font_SetColor(*fnt, ARGB(255,255,0,0));  RGB(255,255,255))
  
  hge_Font_Render(*fnt,5,0, #HGETEXT_LEFT, "FPS: "+Str(fps)+"(const)")

  hge_Gfx_EndScene()
  ProcedureReturn #False
EndProcedure


Procedure Main()
  hge_Create(#HGE_VERSION)
  
  ; // maybe a bug - somtime hge could Not load from current dir
  Path.s = GetCurrentDirectory()+"\Res\";
  Debug "path opened..."
  ; // Set up log file, frame function, render function And window title
  hge_System_SetStateString(#hge_LOGFILE, Path+"pbhge_plonk.log");
  hge_System_SetStateFunc(#HGE_FRAMEFUNC, @frameFunc())
  hge_System_SetStateFunc(#hge_RENDERFUNC, @RenderFunc())
  hge_System_SetStateString(#HGE_TITLE, "PloNK 0.0a LOG")
  ; // Set up video mode
  hge_System_SetStateBool(#HGE_WINDOWED,#True)
  hge_System_SetStateInt(#hge_SCREENWIDTH,1024)
  hge_System_SetStateInt(#hge_SCREENHEIGHT,768)
  hge_System_SetStateInt(#hge_SCREENBPP,32)
  hge_System_SetStateInt(#hge_FPS, 100)
  Debug "System set..."
  hge_System_SetStateBool(#HGE_USESOUND , #False)
  
  If hge_System_Initiate()
    ; // Load sound And texture
    tex1=hge_Texture_Load(Path+"bg2.png",0,#False);
    tex2=hge_Texture_Load(Path+"bg.png",0,#False);
    tex4=hge_Texture_Load(Path+"blue2.png",0,#False); particles.png
    tex5=hge_Texture_Load(Path+"corner3.png",0,#False);  OK False = [load into memory once?] and True = [load each time?]
    Debug "Textures loaded..."
    *fnt = hge_Font(Path+"font2.fnt",0)
    *phWorld=Ph_CreateWorld(0,0,1024,768,0,100,1)
    Debug "Font loaded..."
; *** CreatePhObjBox(tex.l = texture name
                                    ;ScaleX.f = how wide object is
                                    ; ScaleY.f = how thick object is (theese two I am going to play with
                                    ; PosX.f = X start position
                                    ; PosY.f = Y start position
                                    ; Angle.f = angle of object (especially if rectangle!) for some reason 45 does not = up down nor does 90
                                    ; **** angle appears to work from CENTER of object 
                                    ; Density.f= how heavy the object is (0 = no move or 0 gravity effect!)
                                    ; Restitution.f= I HAVE NO IDEA YET
                                    ; Friction.f= how well objects collide with it... I think it is amount of rebound!
    CreatePhObjBox(tex1,1024,32,512,752,0,0,0.25,6) ; bottom was 0.3
    CreatePhObjBox(tex1,15,720,10,375,0,0,1,0) ; my really cruddy attempt at a LEFT wall 
    CreatePhObjBox(tex1,10,600,55,435,0,0,1,0) ; the RIGHT wall of the CANNON
    CreatePhObjBox(tex1,15,720,1015,375,0,0,1,0) ; the right WALL
    CreatePhObjBox(tex5,120,60,40,10,5.490,0,1,0) ; the CORNER object (someone explain to me how 5.490 is about 45 degrees rotation!)
    CreatePhObjBox(tex2,35,10,35,720,0,0,2.5,1) ; bouncy box to fire ball
    CreatePhObjBox(tex1,1024,32,512,1,0,0,0.25,6) ; top
    ; *** LATER an object is created affects Z Order in drawing object
    y = 130
    For times = 0 To 5 ; create the PLINKS
        For x = 100 To 960 Step 100
            CreatePhObjBox(tex2,6,6,x,y,40, 0, 0.2, 1)
            CreatePhObjBox(tex2,6,6,x+50,y+50,40, 0, 0.2, 1)
        Next
        y = y + 100
    Next
; ***
Debug "look 3"
    hge_System_Start() ; and here HGE takes over until program is killed
                                    ; all your program controls would go in framefunc()
                                    ; the rest of this is just cleanup
    Debug "look 2"
    hge_Font_Delete(*fnt)
    For i  = 0 To NumPhObj-1
	   	hge_Sprite_Delete(phObj(i)\spr)
    Next 
    ;hge_Sprite_Delete(*sprCur) 
   ; hge_GUI_Delete(*gui) 
		hge_Texture_Free(tex1)
		hge_Texture_Free(tex2)
		hge_Texture_Free(tex3)
		hge_Texture_Free(tex4)
  EndIf
  Debug "look 1"

  Ph_DeleteWorld(*phWorld)
  hge_System_Shutdown()
  hge_Release()    
EndProcedure
Debug "Starting MAIN..."
Main()
; next we feature a complete rewrite to see if we can get this into a REAL framework like PB uses
; in stead of this bastard C style version!!! (WHICH I HATE!!!)

End

The two images are:

Image

and

Image

Have fun...

8)

I must say that HGE seems a bit limited and very blocky... I am rather disappointed in it now! :shock: Perhaps it is me... But it seems your entire program is basically just sending messages to HGE to do things. You have so little control it is funny!
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
User avatar
IceSoft
Addict
Addict
Posts: 1695
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Post by IceSoft »

Please add the precompiled examples for downloading.
Belive! C++ version of Puzzle of Mystralia
Bug Planet
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
byo
Enthusiast
Enthusiast
Posts: 635
Joined: Mon Apr 02, 2007 1:43 am
Location: Brazil

Post by byo »

Hey, Rook. The examples are looking good so far. I can't download external sites files at work right now but I'll try to find my way around it. :D
Proud registered Purebasic user.
Because programming should be fun.
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

I would truly love to post the compiled examples. I will post a ZIP file. I wish HGE would let you include the graphics in your program. This is one of the reasons I am dissappointed in it!

{{EDIT}} DOWNLOAD HERE (only for a while!)

http://www.bluemesapc.com/Downloads/p10nK.zip
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Re: HGE Wrapper tip #1,#1A (update 12 JUN 2008)

Post by NoahPhense »

You can just include the images inside the exe .. VERY nice btw!!

- np
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

I am currently attempting to figure out the MAZE GAME example... I see how it reads the map... but I cannot understand how it reads the map... Nor can I increase the size of the game yet.

{SIGH}

I am attempting, first, to increase the output of the MAZE GAME to 800 X 600... Then I will attack map format.
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

AFAIK the numbers in the map.dat file are the following:
401 301 (dimension of map)
079 placeholder

all other numbers refer to 32 X 32 tiles in the map.png image... 017 for instance is the START square and 001 for the animated EXIT

Looks like it is just one long row of info... read control by the first two numbers in the map.

Too simple really...
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
neotoma
User
User
Posts: 84
Joined: Sun Dec 14, 2003 6:38 pm
Location: Germany, Mechernich
Contact:

Post by neotoma »

@Rook
You are right with the Maze-Example. I converted it from C without any tranlation or comments. It was a very easy format.
BTW:If you look at the HGE-Fotum there are a Tile-Editor coming (if they finish them..).

And thanks for the great examples !

Mike
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

I have been waiting for that mapeditor for almost 2 years now. Time to crunch my own! :D
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
Hurga
Enthusiast
Enthusiast
Posts: 148
Joined: Thu Jul 17, 2003 2:53 pm
Contact:

Post by Hurga »

If you makle a map editor, I´ll consider using the HGE... ;-)
byo
Enthusiast
Enthusiast
Posts: 635
Joined: Mon Apr 02, 2007 1:43 am
Location: Brazil

Post by byo »

Very nice examples. :shock:

How complete is the HGE port?
Proud registered Purebasic user.
Because programming should be fun.
neotoma
User
User
Posts: 84
Joined: Sun Dec 14, 2003 6:38 pm
Location: Germany, Mechernich
Contact:

Post by neotoma »

byo wrote:Very nice examples. :shock:

How complete is the HGE port?
Hi byo - it is not a port - it is only a wrapper.
So HGE is stable as the released version. The wrapper is nearly complete to cover HGE. But the Physics-Part (Box2D) is not complete - maybe i spend some time to add more wrapped functions.

I thinking about adding more extension or ability for extensions. special for GUI-Elements.

Mike
Post Reply