FB HGEWrapper 2D Game Engine

User contributed sources that have become inactive, deprecated, or generally unusable. But ... we don't really want to throw them away either.
Dalex
Posts: 46
Joined: Dec 09, 2006 9:46
Contact:

FB HGEWrapper 2D Game Engine

Post by Dalex »

HgeWrapper for FreeBasic(0.18.2) (Win32)

(HGE version 1.8) doc:http://hge.relishgames.com/doc/index.html
==================
Actual:Version 0.1.7
==================
hge_System
hge_Gfx
hge_Ini
hge_Input
hge_Texture
hge_Timer
hge_Random
hge_Resource
hge_Sprite
hge_Font
hge_Music
hge_Channel
hge_Effect
hge_Target
hge_ParticleSystem
hge_ParticleManager
hge_DistortionMesh
hge_Animation
hge_GUI
hge_StringTable
hge_Rect
hge_Vector
2DPhysics
==================
Status: 312 Function
==================
Site: http://dalex.ucoz.ru/
==================
source code
hge.dll
bass.dll
HgeWrapper.dll
HgeWrapper.bi
HgeType.bi
libHgeWrapper.a
FBhge_tut01.bas - Minimal HGE application.
FBhge_tut02.bas - Using input, sound and rendering.
FBhge_tut03.bas - Using helper classes.
FBhge_tut04.bas - Using render targets.
FBhge_tut05.bas - Using distortion mesh.
FBhge_tut06.bas - Creating menus.
FBhge_tut07.bas - Thousand of Hares.
FBhge_tut09.bas - 2DPhysics ,Body,Joint,Car
==================
Static lib project http://dalex.ucoz.ru/load/4-1-0-6
==================
Please test...
Last edited by Dalex on Dec 18, 2007 13:32, edited 14 times in total.
duke4e
Posts: 717
Joined: Dec 04, 2005 0:16
Location: Varazdin, Croatia, Europe
Contact:

Post by duke4e »

Works good, but there are still much things to do, like hgecolor, hgeparticle... Also you're missing tons of system constants (like HGE_SHOWSPLASH, etc...)

Damn, I just wish this turns out into complete HGE wrapper and keeps up with upcoming updates...


Anyways, nice work so far...
v1ctor
Site Admin
Posts: 3804
Joined: May 27, 2005 8:08
Location: SP / Bra[s]il
Contact:

Post by v1ctor »

Cool library, didn't know about it (or rather, it wasn't OSS when i checked it ages ago). It's nice how you can define a fixed FPS and such.

I hope you will release the wrapper sources later so people can learn how to write a C++ wrapper and could keep updating it if you don't want to maintain it.

Btw, i noticed there's no import library. While LD will be able to find the symbols directly in the DLL, it takes much longer than when using an imp library.
livewire
Posts: 78
Joined: Dec 07, 2006 3:43
Location: Orlando, FL

Post by livewire »

Works great. I was able to port the Tutorial number 2 to FreeBASIC very easily. Could you post the source to the wrapper?

Code: Select all

#Include "HgeWrapper.bi"

Declare Function FrameFunc() As Integer
Declare Function RenderFunc() As Integer
Declare Sub Boom()
Declare Sub Main()

Dim Shared As long   fnt
Dim Shared As long   tex1
Dim Shared As long   tex2
Dim Shared As long   spr1
Dim Shared As long   spr2
Dim Shared As string FPS

Dim Shared quad As hgeQuad
Dim Shared snd As HEFFECT
Dim Shared As Single x,y
Dim Shared As Single dx,dy
Const speed As Single = 90
Const friction As Single = 0.98

Main()

Sub Boom()
    Dim pan As Integer
    Dim pitch As Single
    
    pan = Cast(Integer,(x-400)/4)
	pitch = (dx*dx + dy*dy) * 0.0005 + 0.2
	hge_Effect_PlayEx(snd,100,pan,pitch,0)
End Sub

Function FrameFunc() As Integer
    Dim dt As Single
    dt = hge_Timer_GetDelta()
    
	If (hge_Input_GetKeyState(HGEK_ESCAPE)) Then return 1
	If (hge_Input_GetKeyState(HGEK_LEFT)) Then dx-=speed*dt
	If (hge_Input_GetKeyState(HGEK_RIGHT)) Then dx+=speed*dt
	If (hge_Input_GetKeyState(HGEK_UP)) Then dy-=speed*dt
	If (hge_Input_GetKeyState(HGEK_DOWN)) Then dy+=speed*dt
    
	dx*=friction
    dy*=friction
    x+=dx
    y+=dy
    
	If (x>784) Then
        x=784-(x-784)
        dx=-dx
        boom()
    End If
	If (x<16) Then
        x=16+16-x
        dx=-dx
        boom()
    End If
	If (y>584) Then
        y=584-(y-584)
        dy=-dy
        boom()
    End If
	If (y<16) Then
        y=16+16-y
        dy=-dy
        boom()
    End If
    
	quad.v(0).x=x-16
    quad.v(0).y=y-16
	quad.v(1).x=x+16
    quad.v(1).y=y-16
	quad.v(2).x=x+16
    quad.v(2).y=y+16
	quad.v(3).x=x-16
    quad.v(3).y=y+16
    
	return 0
End Function

Function RenderFunc() As Integer
	hge_Gfx_BeginScene()
	hge_Gfx_Clear(0)
	hge_Gfx_RenderQuad(@quad)
	hge_Gfx_EndScene()
	return 0
End Function

Sub Main()
    Dim i As Integer
    
    hge_Create(hge_VERSION)
    hge_System_SetStateString(HGE_LOGFILE, "hge_tut02.log")
	hge_System_SetStateFunc(HGE_FRAMEFUNC, @FrameFunc)
	hge_System_SetStateFunc(HGE_RENDERFUNC, @RenderFunc)
    hge_System_SetStateString(HGE_TITLE, "HGE Tutorial 02 - Using input, sound and rendering")
	hge_System_SetStateBool(HGE_WINDOWED, 1)
	hge_System_SetStateInt(HGE_SCREENWIDTH, 800)
	hge_System_SetStateInt(HGE_SCREENHEIGHT, 600)
	hge_System_SetStateInt(HGE_SCREENBPP, 32)
    
    If (hge_System_Initiate()) Then
        snd = hge_Effect_Load("menu.wav",0)
        quad.tex = hge_Texture_Load("particles.png")
        
        If (snd = 0) Or (quad.tex = 0) Then
            MessageBox(NULL,"Can't load MENU.WAV or PARTICLES.PNG","Error",MB_OK)
            hge_System_Shutdown()
            hge_Release()
            Return
        End If
        
        quad.blend = BLEND_ALPHAADD Or BLEND_COLORMUL Or BLEND_ZWRITE
        
        For i = 0 To 3
            quad.v(i).z = 0.5
            quad.v(i).col = &HFFFFA000
        Next
        
        quad.v(0).tx=96.0/128.0
        quad.v(0).ty=64.0/128.0
        quad.v(1).tx=128.0/128.0
        quad.v(1).ty=64.0/128.0
        quad.v(2).tx=128.0/128.0
        quad.v(2).ty=96.0/128.0
        quad.v(3).tx=96.0/128.0
        quad.v(3).ty=96.0/128.0
        
        hge_System_Start()
        
        hge_Texture_Free(quad.tex)
        hge_Effect_Free(snd)
    Else
        MessageBox(NULL, "", "Error", MB_OK )
    End If
    
    hge_System_Shutdown()
    hge_Release()
End Sub
Dalex
Posts: 46
Joined: Dec 09, 2006 9:46
Contact:

Post by Dalex »

Well...
All thank you...
Continuation follows...
Dalex
Posts: 46
Joined: Dec 09, 2006 9:46
Contact:

Post by Dalex »

Update Version 0.1.1b first post ...(+29 function)
There is problems...?
speedlemon
Posts: 55
Joined: Sep 21, 2005 22:19

Post by speedlemon »

Hey Dalex,
Thanks for doing this project. It is very interesting.

I noticed a problem with hgetype.bi when I was trying to use the ARGB macro.. I realized that the whole color section of the header had the same issue, so I fixed it by changing it to the following:

Code: Select all

#Define ARGB(a,r,g,b)  ((cast(dword,a) shl 24)+(cast(dword,r) shl 16)+(cast(dword,g) shl 8)+cast(dword,b))
#Define GETA(col)      ((col) shr 24)
#Define GETR(col)      (((col) shr 16) & &hFF)
#Define GETG(col)      (((col) shr 8) & &hFF)
#Define GETB(col)      ((col) & &hFF)
#Define SETA(col,a)    (((col) & &h00FFFFFF) + (cast(dword,a) shl 24))
#Define SETR(col,r)    (((col) & &hFF00FFFF) + (cast(dword,r) shl 16))
#Define SETG(col,g)    (((col) & &hFFFF00FF) + (cast(dword,g) shl 8))
#Define SETB(col,b)    (((col) & &hFFFFFF00) + cast(dword,b))
There wasn't a function called 'dword'.
and
'<<' and '>>' should've been 'shl' and 'shr'.
and
on the last line of the snippet, you had 'word' when it should be 'dword' (unless you did 'word' for some reason, they used 'dword' in the hge docs)...

Thanks again,
Erik
Dalex
Posts: 46
Joined: Dec 09, 2006 9:46
Contact:

Post by Dalex »

Update Version 0.1.2b first post ...(+71 function)

P.S livewire, speedlemon... thank you...
duke4e
Posts: 717
Joined: Dec 04, 2005 0:16
Location: Varazdin, Croatia, Europe
Contact:

Post by duke4e »

thanks alot. if i'll have some free time, i'll translate few tutorials.

edit: found a bug. in tutorial 2 and 3 when ball hits the wall, pitch is always same while in c++ pitch varies.

edit 2: here's a a render target tutorial... seems like something isn't well translated or i have screwed something while translating.

Code: Select all

'*************************************************
' FBhge_tut04 - Using render targets
'*************************************************

#Include  "inc/HgeType.bi"
#Include  "inc/HgeWrapper.bi"

Dim Shared As Long   spr
Dim Shared As Long   spt
Dim Shared As Long   tar
Dim Shared As Long   fnt
Dim Shared As Long   par
Dim Shared As Long   tex
Dim Shared As Long   snd
Dim Shared As Long   target
Dim Shared As hgeParticleSystemInfo  psi

Dim Shared As Single  x=100.0f
Dim Shared As Single  y=100.0f
Dim Shared As Single  dx=0.0f 
Dim Shared As Single  dy=0.0f
Const As Single       speed=90
Const As Single       friction=0.98

function GfxRestoreFunc() as integer
	if(tar and target) then
        hge_Sprite_SetTexture(tar,hge_Target_GetTexture(target))
    end if
	return false
end function

Sub Boom()    
    Dim As Integer pan = int((x-256)/2.56f)
    Dim As Single pitch = pitch=(dx*dx+dy*dy)*0.0005f+0.2f
    hge_Effect_PlayEx(snd,100,pan,pitch,0)
End Sub

Function FrameFunc() As Integer 
	
	Dim As Single dt=hge_Timer_GetDelta()
    
	''Process keys
	If (hge_Input_GetKeyState(HGEK_ESCAPE))Then Return true
	If (hge_Input_GetKeyState(HGEK_LEFT))  Then dx-=speed*dt
	If (hge_Input_GetKeyState(HGEK_RIGHT)) Then dx+=speed*dt
	If (hge_Input_GetKeyState(HGEK_UP))    Then dy-=speed*dt
	If (hge_Input_GetKeyState(HGEK_DOWN))  Then dy+=speed*dt
    
	'' Do some movement calculations and collision detection	
	dx*=friction: dy*=friction: x+=dx: y+=dy
	If(x>784)Then x=784-(x-784):dx=-dx:boom()
	If(x<16) Then x=16+16-x:dx=-dx:boom()
	If(y>584)Then y=584-(y-584):dy=-dy:boom()
	If(y<16) Then y=16+16-y:dy=-dy:boom()   
    
    psi.nEmission=Int(dx*dx+dy*dy)
    hge_PartSys_SetInfo(par,psi)
    hge_PartSys_MoveTo(par,x,y)
    hge_PartSys_Update(par,dt)
    
	Return false
End Function

Function  RenderFunc()As  Integer 
	hge_Gfx_BeginScene(target)
	hge_Gfx_Clear(0)
	hge_PartSys_Render(par)
	hge_Sprite_Render(spr,x,y)
    hge_Gfx_EndScene()
    

	hge_Gfx_BeginScene()
	hge_Gfx_Clear(0)
	for i as integer = 0 to 5
		hge_Sprite_SetColor(tar, &hFFFFFF or (((5-i)*40+55) shl 24))
        hge_Sprite_RenderEx(tar, i*100.0f, i*50.0f, i*M_PI/8, 1.0f-i*0.1f)
	next
    
    hge_Gfx_EndScene()
    Return 0
End  Function

hge_Create(hge_VERSION)
hge_System_SetStateInt(hge_SHOWSPLASH, 0)
hge_System_SetStateString(HGE_LOGFILE, "FBhge_tut04.log")
hge_System_SetStateFunc(HGE_FRAMEFUNC, @FrameFunc)
hge_System_SetStateFunc(HGE_RENDERFUNC, @RenderFunc)
hge_System_SetStateFunc(HGE_GFXRESTOREFUNC, @GfxRestoreFunc)
hge_System_SetStateString(HGE_TITLE, "FreeBasic-HGE Tutorial 04 - Using render targets")
hge_System_SetStateInt(HGE_FPS, 100)
hge_System_SetStateBool(HGE_WINDOWED, true)
hge_System_SetStateInt(HGE_SCREENWIDTH, 800)
hge_System_SetStateInt(HGE_SCREENHEIGHT, 600)
hge_System_SetStateInt(HGE_SCREENBPP, 32)


If (hge_System_Initiate()) Then
    snd = hge_Effect_Load("Res/menu.wav",0)
    tex = hge_Texture_Load("Res/particles.png")
    
    If (snd = 0) Or (tex = 0) Then
        MessageBox(NULL,"Can't load MENU.WAV or PARTICLES.PNG","Error",MB_OK)
        hge_System_Shutdown()
        hge_Release()
    End If
    
    
    
    spr = hge_Sprite(tex, 96, 64, 32, 32)
    hge_Sprite_SetColor(spr,&hFFFFA000)
    hge_Sprite_SetHotSpot(spr,16,16)
    
    spt=hge_Sprite(tex, 32, 32, 32, 32)
    hge_Sprite_SetBlendMode(spt,BLEND_COLORMUL Or BLEND_ALPHAADD Or BLEND_NOZWRITE)
    hge_Sprite_SetHotSpot(spt,16,16)
    par = hge_PartSys("Res/trail.psi",spt)
    hge_PartSys_GetInfo(par,psi)
    hge_PartSys_Fire(par)
    
    tar=hge_Sprite(hge_Target_GetTexture(target),0,0,512,512)

    hge_System_Start()
	
    hge_Texture_Free(target)
    hge_Texture_Free(tex)
    hge_Effect_Free(snd)
End If

hge_System_Shutdown()
hge_Release()
edit 3: here's a not working distortion mesh tutorial

Code: Select all

'*************************************************
' FBhge_tut05 - Using distortion mesh
'*************************************************

#Include  "inc/HgeType.bi"
#Include  "inc/HgeWrapper.bi"

Dim Shared As Long   tex 'htexture
Dim Shared As Long   dis 'hgeDistortionMesh
Dim Shared As Long   fnt 'hgeFont

Const nRows=16
Const nCols=16
Const As Single cellw=512.0f/(nCols-1)
Const As Single cellh=512.0f/(nRows-1)

Const As Single meshx=144
Const As Single meshy=44

Dim Shared As String FPS


Function FrameFunc() As Integer 
	
	Dim As Single dt=hge_Timer_GetDelta()
    Static As Single t = 0.0f
    Static As Integer trans = 0
    
	Dim As Integer i, j, col
	Dim As Single r, a, dx, dy
	t += dt
    
	' Process keys
	Select Case hge_Input_GetKey
		Case HGEK_ESCAPE
			Return true
            
        Case HGEK_SPACE
            trans += 1
			If trans > 2 Then trans=0
            'hge_DistortionMesh_Clear(dis, &hFF000000, 0)
    End Select
    
	' Calculate new displacements and coloring for one of the three effects
	Select Case trans
		Case 0
            For i = 1 To nRows - 2
                For j = 1 To nCols - 2
                    hge_DistortionMesh_SetDisplacement(dis, j,i,cos(t*10+(i+j)/2)*5,sin(t*10+(i+j)/2)*5,HGEDISP_NODE)
                Next
            Next
            
        Case 1
            For i = 0 To nRows - 1
                For j = 0 To nCols - 1
                    hge_DistortionMesh_SetDisplacement(dis, j,i,cos(t*5+j/2)*15,0,HGEDISP_NODE)
                    col=Int((cos(t*5+(i+j)/2)+1)*35)
                    hge_DistortionMesh_SetColor(dis, j,i,&hFF shl 24 or col shl 16 or col shl 8 or col)
                Next
            Next
            
        Case 2
            For i = 0 To nRows - 1
                For j = 0 To nCols - 1
                    r=sqr(((j-nCols/2)*(j-nCols/2)) + (i-nRows/2)*(i-nRows/2))
                    a=r*cos(t*2)*0.1f
                    dx=sin(a)*(i*cellh-256)+cos(a)*(j*cellw-256)
                    dy=cos(a)*(i*cellh-256)-sin(a)*(j*cellw-256)
                    hge_DistortionMesh_SetDisplacement(dis, j,i,dx,dy,HGEDISP_CENTER)
                    col=Int((Cos(r+t*4)+1)*40)
                    hge_DistortionMesh_SetColor(dis, j,i, &hFF shl 24 or col shl 16 or (col/2) shl 8)
                Next
            Next
            
    End Select
    
	Return false
End  Function

Function  RenderFunc()As  Integer 
	hge_Gfx_BeginScene()
	hge_Gfx_Clear(0)
    hge_DistortionMesh_Render(dis, meshx, meshy)
    
	FPS = Str(hge_Timer_GetFPS())
	hge_Font_SetColor(fnt,Rgb(255,255,255))
    hge_Font_Render(fnt,6, 6, HGETEXT_LEFT, "FPS: " + FPS)
    hge_Font_Render(fnt,6, 58, HGETEXT_LEFT, "Use your")
    hge_Font_Render(fnt,6, 84, HGETEXT_LEFT, "SPACE")
    hge_Gfx_EndScene()
    Return 0
End  Function

hge_Create(hge_VERSION)
hge_System_SetStateString(HGE_LOGFILE, "FBhge_tut05.log")
hge_System_SetStateFunc(HGE_FRAMEFUNC, @FrameFunc)
hge_System_SetStateFunc(HGE_RENDERFUNC, @RenderFunc)
hge_System_SetStateString(HGE_TITLE, "FreeBasic-HGE Tutorial 05 - Using distortion mesh")
hge_System_SetStateBool(HGE_WINDOWED, true)
hge_System_SetStateInt(HGE_SCREENWIDTH, 800)
hge_System_SetStateInt(HGE_SCREENHEIGHT, 600)
hge_System_SetStateInt(HGE_SCREENBPP, 32)
hge_System_SetStateInt(HGE_USESOUND, false)


If (hge_System_Initiate()) Then
    tex = hge_Texture_Load("Res/texture.jpg")
    
    If tex = 0 Then
        MessageBox(NULL,"Can't load TEXTURE.JPG","Error",MB_OK)
        hge_System_Shutdown()
        hge_Release()
    End If
    
	hge_DistortionMesh(dis, nCols, nRows)
    hge_DistortionMesh_SetTexture(dis, tex)
    hge_DistortionMesh_SetTextureRect(dis,0,0,512,512)
    hge_DistortionMesh_SetBlendMode(dis, BLEND_COLORADD or BLEND_ALPHABLEND or BLEND_ZWRITE)
    hge_DistortionMesh_Clear(dis, &hFF000000, 0)
    
    fnt = hge_Font("Res/font1.fnt",0)
    hge_System_Start()
	
    hge_Texture_Free(tex)
End If

hge_System_Shutdown()
hge_Release()
edit 4: and finally a working tutorial (7)

Code: Select all

'*************************************************
' FBhge_tut07 - Thousand of Hares
'*************************************************

#Include  "inc/HgeType.bi"
#Include  "inc/HgeWrapper.bi"

#undef color
#define SCREEN_WIDTH  800
#define SCREEN_HEIGHT 600

#define MIN_OBJECTS	100
#define MAX_OBJECTS 2000

type sprObject
	as single x,y
	as single dx,dy
	as single scale,rot
	as single dscale,drot
	as DWORD color
end type

dim shared as sprObject pObjects(MAX_OBJECTS)
dim shared as integer nObjects
dim shared as integer nBlend

Dim Shared As Long   tex, bgtex
Dim Shared As Long   spr, bgspr
Dim Shared As Long   fnt

sub SetBlend(blend as integer)
	static as integer sprBlend(5) = _
    {_
		BLEND_COLORMUL or BLEND_ALPHABLEND or BLEND_NOZWRITE,_
		BLEND_COLORADD or BLEND_ALPHABLEND or BLEND_NOZWRITE,_
		BLEND_COLORMUL or BLEND_ALPHABLEND or BLEND_NOZWRITE,_
		BLEND_COLORMUL or BLEND_ALPHAADD   or BLEND_NOZWRITE,_
		BLEND_COLORMUL or BLEND_ALPHABLEND or BLEND_NOZWRITE _
	}
    
	static as DWORD fntColor(5)= {&hFFFFFFFF, &hFF000000, &hFFFFFFFF, &hFF000000, &hFFFFFFFF}

	static as DWORD sprColors(5, 5) = _
	{_
		{ &hFFFFFFFF, &hFFFFE080, &hFF80A0FF, &hFFA0FF80, &hFFFF80A0 }, _
		{ &hFF000000, &hFF303000, &hFF000060, &hFF006000, &hFF600000 }, _
		{ &h80FFFFFF, &h80FFE080, &h8080A0FF, &h80A0FF80, &h80FF80A0 }, _
		{ &h80FFFFFF, &h80FFE080, &h8080A0FF, &h80A0FF80, &h80FF80A0 }, _
		{ &h40202020, &h40302010, &h40102030, &h40203010, &h40102030 } _
	}

	if blend > 4 then blend=0
	nBlend=blend

	hge_Sprite_SetBlendMode(spr, sprBlend(blend))
    hge_Font_SetColor(fnt, fntColor(blend))
	for i as integer = 0 to MAX_OBJECTS - 1
        pObjects(i).color=sprColors(blend, hge_Random_Int(0,4))
    next
end sub





Dim Shared As String FPS


Function FrameFunc() As Integer 
	
	Dim As Single dt=hge_Timer_GetDelta()
    dim as integer i
    
	' Process keys
    
	Select Case hge_Input_GetKey
		case HGEK_UP
            if nObjects < MAX_OBJECTS then nObjects += 100
            
		case HGEK_DOWN
            if nObjects > MIN_OBJECTS then nObjects -= 100
		
        case HGEK_SPACE
            nblend += 1
            SetBlend(nBlend)
            
		Case HGEK_ESCAPE
			Return true
    End Select
    
	' Update the scene
	
	for i = 0 to nObjects - 1
		pObjects(i).x+=pObjects(i).dx*dt
		if (pObjects(i).x>SCREEN_WIDTH or pObjects(i).x<0) then pObjects(i).dx=-pObjects(i).dx
        
		pObjects(i).y+=pObjects(i).dy*dt
		if (pObjects(i).y>SCREEN_HEIGHT or pObjects(i).y<0) then pObjects(i).dy=-pObjects(i).dy
        
		pObjects(i).scale+=pObjects(i).dscale*dt
		if (pObjects(i).scale>2 or pObjects(i).scale<0.5) then pObjects(i).dscale=-pObjects(i).dscale
		
        pObjects(i).rot+=pObjects(i).drot*dt
	next

	return false
End Function

Function RenderFunc() As Integer

	dim as integer i
	
	' Render the scene
	
	hge_Gfx_BeginScene()
    hge_Sprite_Render(bgspr, 0, 0)
	
	for i = 0 to nObjects - 1
        hge_Sprite_SetColor(spr, pObjects(i).color)
        hge_Sprite_RenderEx(spr, pObjects(i).x, pObjects(i).y, pObjects(i).rot, pObjects(i).scale)
	next
    
    
	FPS = Str(hge_Timer_GetFPS())
    hge_Font_SetColor(fnt,Rgb(255,255,255))
    hge_Font_Render(fnt, 7, 7, HGETEXT_LEFT, "UP and DOWN to adjust number of hares: " + str(nObjects))
    hge_Font_Render(fnt, 7, 7 + 12, HGETEXT_LEFT, "SPACE to change blending mode: " + str(nBlend))
    hge_Font_Render(fnt, 7, 7 + 24, HGETEXT_LEFT, "FPS: " + FPS)
    
    
    
    hge_Gfx_EndScene()
    Return false
End Function

hge_Create(hge_VERSION)
hge_System_SetStateString(HGE_LOGFILE, "FBhge_tut07.log")
hge_System_SetStateFunc(HGE_FRAMEFUNC, @FrameFunc)
hge_System_SetStateFunc(HGE_RENDERFUNC, @RenderFunc)
hge_System_SetStateString(HGE_TITLE, "FreeBasic-HGE Tutorial 07 - Thousand of Hares")
hge_System_SetStateInt(HGE_USESOUND, false)
hge_System_SetStateBool(HGE_WINDOWED, false)
hge_System_SetStateInt(HGE_SCREENWIDTH, SCREEN_WIDTH)
hge_System_SetStateInt(HGE_SCREENHEIGHT, SCREEN_HEIGHT)
hge_System_SetStateInt(HGE_SCREENBPP, 32)


If (hge_System_Initiate()) Then
    bgtex = hge_Texture_Load("Res/bg2.png")
    tex = hge_Texture_Load("Res/zazaka.png")
    
    If (bgtex = 0) Or (tex = 0) Then
        MessageBox(NULL,"Can't load BG2.PNG or ZAZAKA.PNG","Error",MB_OK)
        hge_System_Shutdown()
        hge_Release()
    End If
    
    fnt = hge_Font("Res/font2.fnt", 0)
  
    spr = hge_Sprite(tex, 0, 0, 64, 64)
    hge_Sprite_SetHotSpot(spr, 16, 16)


    bgspr = hge_Sprite(bgtex, 0, 0, 800, 600)
    hge_Sprite_SetBlendMode(bgspr, BLEND_COLORADD or BLEND_ALPHABLEND or BLEND_NOZWRITE)
    hge_Sprite_SetColor(bgspr, &hFF000000, 0)
    hge_Sprite_SetColor(bgspr, &hFF000000, 1)
    hge_Sprite_SetColor(bgspr, &hFF000040, 2)
    hge_Sprite_SetColor(bgspr, &hFF000040, 3)

    ' Initialize objects list

    nObjects=1000

    for i as integer = 0 to MAX_OBJECTS - 1
        pObjects(i).x=hge_Random_Float(0,SCREEN_WIDTH)
        pObjects(i).y=hge_Random_Float(0,SCREEN_HEIGHT)
        pObjects(i).dx=hge_Random_Float(-200,200)
        pObjects(i).dy=hge_Random_Float(-200,200)
        pObjects(i).scale=hge_Random_Float(0.5f,2.0f)
        pObjects(i).dscale=hge_Random_Float(-1.0f,1.0f)
        pObjects(i).rot=hge_Random_Float(0,M_PI*2)
        pObjects(i).drot=hge_Random_Float(-1.0f,1.0f)
    next
    
    SetBlend(0)
    hge_System_Start()
	
    hge_Texture_Free(bgtex)
    hge_Texture_Free(tex)
End If

hge_System_Shutdown()
hge_Release()
Dalex
Posts: 46
Joined: Dec 09, 2006 9:46
Contact:

Post by Dalex »

duke4e thanks...

HgeWrapper.bi
Old function bug

Code: Select all

Declare Function hge_DistortionMesh  Cdecl Alias  "hge_DistortionMesh" (ByVal As Dword,ByVal As Integer,ByVal As Integer)As long
HgeWrapper.bi
New function

Code: Select all

Declare Function hge_DistortionMesh Cdecl Alias  "hge_DistortionMesh" (ByVal As Integer,ByVal As Integer)As long


New Tutorial04

Code: Select all

'*************************************************
' FBhge_tut04 - Using render targets
'*************************************************

#Include  "inc/HgeType.bi"
#Include  "inc/HgeWrapper.bi"

Dim Shared As Long   spr
Dim Shared As Long   spt
Dim Shared As Long   tar
Dim Shared As Long   fnt
Dim Shared As Long   par
Dim Shared As Long   tex
Dim Shared As Long   snd
Dim Shared As Long   target
Dim Shared As hgeParticleSystemInfo  psi
Dim Shared As string FPS
Dim Shared As Single dt

Dim Shared As Single  x=100.0f
Dim Shared As Single  y=100.0f
Dim Shared As Single  dx=0.0f 
Dim Shared As Single  dy=0.0f
Const As Single       speed=90
Const As Single       friction=0.98

Function GfxRestoreFunc() As Integer
        If(tar And target) Then
        hge_Sprite_SetTexture(tar,hge_Target_GetTexture(target))
        End If
        Return false
End Function

Sub Boom()    
    Dim As Integer pan = Int((x-256)/2.56)
    Dim As Single pitch = pitch=(dx*dx+dy*dy)*0.0005+0.2
    hge_Effect_PlayEx(snd,100,pan,pitch,0)
End Sub

Function FrameFunc() As Integer 
        
        dt=hge_Timer_GetDelta()
    
        ''Process keys
        If (hge_Input_GetKeyState(HGEK_ESCAPE))Then Return true
        If (hge_Input_GetKeyState(HGEK_LEFT))  Then dx-=speed*dt
        If (hge_Input_GetKeyState(HGEK_RIGHT)) Then dx+=speed*dt
        If (hge_Input_GetKeyState(HGEK_UP))    Then dy-=speed*dt
        If (hge_Input_GetKeyState(HGEK_DOWN))  Then dy+=speed*dt
    
        '' Do some movement calculations and collision detection        
        dx*=friction: dy*=friction: x+=dx: y+=dy
        If(x>496)Then x=496-(x-496):dx=-dx:boom()
        If(x<16) Then x=16+16-x:dx=-dx:boom()
        If(y>496)Then y=496-(y-496):dy=-dy:boom()
        If(y<16) Then y=16+16-y:dy=-dy:boom()   
    
        psi.nEmission=Int(dx*dx+dy*dy)
        hge_PartSys_SetInfo(par,psi)
        hge_PartSys_MoveTo(par,x,y)
        hge_PartSys_Update(par,dt)
    
        Return false
End Function

Function  RenderFunc()As  Integer 
        hge_Gfx_BeginScene(target)
        hge_Gfx_Clear(0)
        hge_PartSys_Render(par)
        hge_Sprite_Render(spr,x,y)
        hge_Gfx_EndScene()
    
        FPS = str(hge_Timer_GetFPS())
        
        hge_Gfx_BeginScene()
        hge_Gfx_Clear(0)
        For i As Integer = 0 To 5
                hge_Sprite_SetColor(tar, &hFFFFFF Or (((5-i)*40+55) Shl 24))
                hge_Sprite_RenderEx(tar, i*100.0f, i*50.0f, i*M_PI/8, 1.0f-i*0.1f)
        Next
        hge_Font_Render(fnt,5, 5, HGETEXT_LEFT, "FPS: "+FPS+"(constant)")
        hge_Font_Render(fnt,5, 35, HGETEXT_LEFT, "dt: "+wstr(dt))
        hge_Gfx_EndScene()
    Return 0
End  Function

hge_Create(hge_VERSION)
hge_System_SetStateInt(hge_SHOWSPLASH, 0)
hge_System_SetStateString(HGE_LOGFILE, "FBhge_tut04.log")
hge_System_SetStateFunc(HGE_FRAMEFUNC, @FrameFunc)
hge_System_SetStateFunc(HGE_RENDERFUNC, @RenderFunc)
hge_System_SetStateFunc(HGE_GFXRESTOREFUNC, @GfxRestoreFunc)
hge_System_SetStateString(HGE_TITLE, "FreeBasic-HGE Tutorial 04 - Using render targets")
hge_System_SetStateInt(HGE_FPS, 100)
hge_System_SetStateBool(HGE_WINDOWED,true)
hge_System_SetStateInt(HGE_SCREENWIDTH, 800)
hge_System_SetStateInt(HGE_SCREENHEIGHT, 600)
hge_System_SetStateInt(HGE_SCREENBPP, 32)


If (hge_System_Initiate()) Then
    snd = hge_Effect_Load("Res/menu.wav",0)
    tex = hge_Texture_Load("Res/particles.png")
    
    If (snd = 0) Or (tex = 0) Then
        MessageBox(NULL,"Can't load MENU.WAV or PARTICLES.PNG","Error",MB_OK)
        hge_System_Shutdown()
        hge_Release()
    End If
    
    fnt = hge_Font("Res/font1.fnt",0)
    
    spr = hge_Sprite(tex, 96, 64, 32, 32)
    hge_Sprite_SetColor(spr,&hFFFFA000)
    hge_Sprite_SetHotSpot(spr,16,16)
    
    spt=hge_Sprite(tex, 32, 32, 32, 32)
    hge_Sprite_SetBlendMode(spt,BLEND_COLORMUL Or BLEND_ALPHAADD Or BLEND_NOZWRITE)
    hge_Sprite_SetHotSpot(spt,16,16)
    par = hge_PartSys("Res/trail.psi",spt)
    hge_PartSys_GetInfo(par,psi)
    hge_PartSys_Fire(par)
    
    target=hge_Target_Create(512,512,false)
    tar=hge_Sprite(hge_Target_GetTexture(target),0,0,512,512)
	 hge_Sprite_SetBlendMode(tar,BLEND_COLORMUL Or BLEND_ALPHAADD Or BLEND_NOZWRITE)

    

    hge_System_Start()
        
    hge_Target_Free(target)
    hge_Texture_Free(tex)
    hge_Effect_Free(snd)
End If

hge_System_Shutdown()
hge_Release()
New Tutorial05

Code: Select all

'*************************************************
' FBhge_tut05 - Using distortion mesh
'*************************************************

#Include  "inc/HgeType.bi"
#Include  "inc/HgeWrapper.bi"

Dim Shared As Long   tex 'htexture
Dim Shared As Long   dis 'hgeDistortionMesh
Dim Shared As Long   fnt 'hgeFont

Const nRows=16
Const nCols=16
Const As Single cellw=512.0/(nCols-1)
Const As Single cellh=512.0/(nRows-1)

Const As Single meshx=144
Const As Single meshy=44

Dim Shared As String FPS


Function FrameFunc() As Integer 
        
        Dim As Single dt=hge_Timer_GetDelta()
        Static As Single t = 0
        Static As Integer trans = 0
    
        Dim As Integer i, j, col
        Dim As Single r, a, dx, dy
        t += dt
    
        ' Process keys
        Select Case hge_Input_GetKey
               Case HGEK_ESCAPE
                    Return true
            
               Case HGEK_SPACE
                    trans += 1
                     If trans > 2 Then trans=0
            hge_DistortionMesh_Clear(dis, &hFF000000, 0)
       End Select
    
        ' Calculate new displacements and coloring for one of the three effects
        Select Case trans
                Case 0
            For i = 1 To nRows - 2
                For j = 1 To nCols - 2
                    hge_DistortionMesh_SetDisplacement(dis, j,i,Cos(t*10+(i+j)/2)*5,Sin(t*10+(i+j)/2)*5,HGEDISP_NODE)
                Next
            Next
            
        Case 1
            For i = 0 To nRows - 1
                For j = 0 To nCols - 1
                    hge_DistortionMesh_SetDisplacement(dis, j,i,Cos(t*5+j/2)*15,0,HGEDISP_NODE)
                    col=Int((Cos(t*5+(i+j)/2)+1)*35)
                    hge_DistortionMesh_SetColor(dis, j,i,&hFF Shl 24 Or col Shl 16 Or col Shl 8 Or col)
                Next
            Next
            
        Case 2
            For i = 0 To nRows - 1
                For j = 0 To nCols - 1
                    r=Sqr(((j-nCols/2)*(j-nCols/2)) + (i-nRows/2)*(i-nRows/2))
                    a=r*Cos(t*2)*0.1f
                    dx=Sin(a)*(i*cellh-256)+Cos(a)*(j*cellw-256)
                    dy=Cos(a)*(i*cellh-256)-Sin(a)*(j*cellw-256)
                    hge_DistortionMesh_SetDisplacement(dis, j,i,dx,dy,HGEDISP_CENTER)
                    col=Int((Cos(r+t*4)+1)*40)
                    hge_DistortionMesh_SetColor(dis, j,i, &hFF Shl 24 Or col Shl 16 Or (col/2) Shl 8)
                Next
            Next
            
    End Select
    
        Return false
End  Function

Function  RenderFunc()As  Integer 
        hge_Gfx_BeginScene()
        hge_Gfx_Clear(0)
        hge_DistortionMesh_Render(dis, meshx, meshy)
    
        FPS = Str(hge_Timer_GetFPS())
        hge_Font_SetColor(fnt,Rgb(255,255,255))
        hge_Font_Render(fnt,6, 6, HGETEXT_LEFT, "FPS: " + FPS)
        hge_Font_Render(fnt,6, 58, HGETEXT_LEFT, "Use your")
        hge_Font_Render(fnt,6, 84, HGETEXT_LEFT, "SPACE")
        hge_Gfx_EndScene()
    Return 0
End  Function

hge_Create(hge_VERSION)
hge_System_SetStateString(HGE_LOGFILE, "FBhge_tut05.log")
hge_System_SetStateFunc(HGE_FRAMEFUNC, @FrameFunc)
hge_System_SetStateFunc(HGE_RENDERFUNC, @RenderFunc)
hge_System_SetStateString(HGE_TITLE, "FreeBasic-HGE Tutorial 05 - Using distortion mesh")
hge_System_SetStateBool(HGE_WINDOWED, true)
hge_System_SetStateInt(HGE_SCREENWIDTH, 800)
hge_System_SetStateInt(HGE_SCREENHEIGHT, 600)
hge_System_SetStateInt(HGE_SCREENBPP, 32)
hge_System_SetStateInt(HGE_USESOUND, false)


If (hge_System_Initiate()) Then
    tex = hge_Texture_Load("Res/texture.jpg")
    
    If tex = 0 Then
        MessageBox(NULL,"Can't load TEXTURE.JPG","Error",MB_OK)
        hge_System_Shutdown()
        hge_Release()
    End If
    fnt = hge_Font("Res/font1.fnt",0)
    
    dis=hge_DistortionMesh(nCols, nRows)
    hge_DistortionMesh_SetTexture(dis, tex)
    hge_DistortionMesh_SetTextureRect(dis,0,0,512,512)
    hge_DistortionMesh_SetBlendMode(dis, BLEND_COLORADD Or BLEND_ALPHABLEND Or BLEND_ZWRITE)
    hge_DistortionMesh_Clear(dis, &hFF000000, 0)
    
    
    hge_System_Start()
        
    hge_Texture_Free(tex)
End If

hge_System_Shutdown()
hge_Release()
good luck...
duke4e
Posts: 717
Joined: Dec 04, 2005 0:16
Location: Varazdin, Croatia, Europe
Contact:

Post by duke4e »

wow, this is becoming the most powerful FB gfx library EVER!

do you plan to release the sources once the library is completely translated? i'm planning to make a wrapper for your wrapper, to simlify things even more and to bring the commands closer to FBGFX users...
v1ctor
Site Admin
Posts: 3804
Joined: May 27, 2005 8:08
Location: SP / Bra[s]il
Contact:

Post by v1ctor »

To make LD (the GNU linker) use the import library instead of trying to look at the DLL's (what takes much longer), add this to the wrapper.bi file:

Code: Select all

#libpath "lib"
#inclib "HgeWrapper"
To compile the examples included for anywhere else, the -p must be used or the wrapper.a file copied to the [fbc-dir]/lib/win32 path.
Dalex
Posts: 46
Joined: Dec 09, 2006 9:46
Contact:

Post by Dalex »

Update Version 0.1.3b first post ...(+41 function)


duke4e
do you plan to release the sources once the library is completely translated?
Yes...

v1ctor thanks...
livewire
Posts: 78
Joined: Dec 07, 2006 3:43
Location: Orlando, FL

Post by livewire »

Excellent work Dalex. This will be a very useful library for the FB users.

-Livewire
Ryan
Posts: 695
Joined: Jun 10, 2005 2:13
Location: Louisville, KY
Contact:

Post by Ryan »

Found this thread accidentally when searching for something else... super impressed and look forward to playing around with it. Thanks for posting!
Post Reply