HGE Example 12 - Motion Blur

Share your advanced PureBasic knowledge/code with the community.
neotoma
User
User
Posts: 84
Joined: Sun Dec 14, 2003 6:38 pm
Location: Germany, Mechernich
Contact:

HGE Example 12 - Motion Blur

Post by neotoma »

Hi,

i made the 12. Example. And while the next realease not on the way,
i put it here in the Forum.

It is a Motion-Blur effect based on the Render-To-Target Function.

Code: Select all

XIncludeFile #PB_Compiler_Home + "Includes\HGEWrapper_include.pbi"

; /*
; ** Haaf's Game Engine 1.8
; ** Copyright (C) 2003-2007, Relish Games
; ** HGE.relishgames.com
; **
; ** hge_tut04 - Using input, sound And rendering
; */
; * motion blur demo
; * based off of tutorial4..



; // Copy the files "particles.png" And "menu.wav"
; // from the folder "precompiled" To the folder With
  ; // executable file. Also Copy HGE.dll And BASS.dll
  ; // To the same folder.
  
  

;// Handles For HGE resourcces

*spr    ;hgeSprite
*spt    ;hgeSprite
*tar1   ;hgeSprite
*tar2   ;hgeSprite
*fnt    ;hgeFont
*par    ;hgeParticleSystem

tex.l     ;HTEXTURE
snd.l     ;HEFFECT

index.l;
; // hge render target Handle
target1.l ;HTARGET
target2.l ;HTARGET


;Some "gameplay" variables And constants
x.f=100.0:  y.f=100.0;
dx.f=0.0  : dy.f=0.0;

speed.f=90;
friction.f=0.98;

; // This function plays collision sound With
  ; // parameters based on Sprite Position And speed
ProcedureCDLL boom()      
  Shared x.f,y.f,dx.f,dy.f
  Shared snd
  pan.l = (x-400.0)/4.0
  pitch.f = (dx*dx+dy*dy)*0.0005+0.2;
  hge_Effect_PlayEx(snd,100,pan,pitch,#False)
EndProcedure

; This function will be called bY HGE when
; render targets were lost And have been just created
; again. We use it Here To update the render
; target's texture handle that changes during recreation.
ProcedureCDLL GfxRestoreFunc()
  Shared *tar1, target1
  Shared *tar2, target2
  If (*tar1 And target1)
    hge_Sprite_SetTexture(*tar1, hge_Target_GetTexture(target1))
  EndIf
  If (*tar2 And target2)
    hge_Sprite_SetTexture(*tar2, hge_Target_GetTexture(target2))
  EndIf
  ProcedureReturn #False
EndProcedure


ProcedureCDLL frameFunc()
  Shared x.f,y.f,dx.f,dy.f
  Shared speed.f, friction.f
  Shared *par
  ; // Get the time elapsed since last call of frameFunc().
  ; // This will help us To synchronize on different
  ; // machines And video modes.
  
  dt.f  = hge_Timer_GetDelta()
  
  If hge_Input_GetKeyState(#HGEK_ESCAPE)
    ProcedureReturn #True;
  EndIf
  If hge_Input_GetKeyState(#HGEK_LEFT) 
    dx=dx-speed*dt
  EndIf
  If hge_Input_GetKeyState(#HGEK_RIGHT) 
    dx=dx+speed*dt
  EndIf
  If hge_Input_GetKeyState(#HGEK_UP) 
    dy=dy-speed*dt
  EndIf
  If hge_Input_GetKeyState(#HGEK_DOWN) 
    dy=dy+speed*dt
  EndIf
  
  ; // Do Some movement calculations And collision detection	
  dx=dx*friction: dy=dy*friction: x=x+dx: y=y+dy;
  If(x>496) : x=496-(x-496) : dx=-dx : boom() : EndIf
  If(x<16)  : x=16+16-x     : dx=-dx : boom() : EndIf
  If(y>496) : y=496-(y-496) : dy=-dy : boom() : EndIf
  If(y<16)  : y=16+16-y     : dy=-dy : boom() : EndIf
  
  
	; Update particle System
  
  res.l =  hge_PartSys_GetInfo(*par, @mypsi.hgeParticleSystemInfo)
  mypsi\nEmission=(dx*dx+dy*dy)*2;
  hge_PartSys_SetInfo(*par,@mypsi)
  hge_PartSys_MoveTo(*par,x,y,#False)
  hge_PartSys_Update(*par,dt)
  
  ; Continue execution
  ProcedureReturn #False
EndProcedure
  
; // This function will be called bY HGE when
; // the application window should be redrawn.
; // Put your rendering code Here.
ProcedureCDLL RenderFunc()
  Shared x.f,y.f,dx.f,dy.f
  Shared *par, *spr,*fnt,*tar1,*tar2
  Shared target1,target2,index
  ; // Begin rendering quads.
  ; // This function MUST be called
  ; // before any actual rendering.
  Select index
    Case 1:
      hge_Gfx_BeginScene(target1)
      hge_Gfx_Clear(0)
      hge_Sprite_RenderStretch(*tar2,0,0,800,600)
      hge_PartSys_Render(*par) 
      hge_Sprite_Render(*spr,x,y) 
      hge_Gfx_EndScene()
    Case 2 :
      hge_Gfx_BeginScene(target2)
      hge_Gfx_Clear(0)
      hge_Sprite_RenderStretch(*tar1,0,0,800,600)
      hge_PartSys_Render(*par) 
      hge_Sprite_Render(*spr,x,y) 
      hge_Gfx_EndScene()
  EndSelect
  
	; // Now put several instances of the rendered texture To the screen
  hge_Gfx_BeginScene(#Null)
  hge_Gfx_Clear(0)
  If index = 1
    hge_Sprite_RenderStretch(*tar1,0,0,800,600)
    index = 2;
  Else
    hge_Sprite_RenderStretch(*tar2,0,0,800,600)
    index = 1;
  EndIf
  ;For i = 0 To 16
   ;  hge_Sprite_SetColor(*tar,$FFFFFF | (((5-i)*40+55)<<24),-1);
    ; 
    ; hge_Sprite_RenderEx(*tar,i*100.0, i*50.0, i*#PI/8, 1.0-i*0.1,0.0);
  ; Next
  hge_PartSys_Render(*par) 
  hge_Sprite_Render(*spr,x,y) 

  
  fps.l =  hge_Timer_GetFPS()
  hge_Font_Printf(*fnt, 5,5,0, "FPS:"+Str(fps),0);

  
  hge_Gfx_EndScene()
  ; // RenderFunc should always Return false
  ProcedureReturn #False;
EndProcedure  


Procedure Main()
  Shared x.f,y.f,dx.f,dy.f
  Shared speed.f, friction.f
  Shared *par,*spr,*spt,*fnt, tex, target1,*tar1, target2,*tar2
  Shared index 
  ; // maybe a bug - somtime hge could Not load from current dir
  Path.s = GetCurrentDirectory()+"Res\";
  
  hge_Create(#HGE_VERSION)
  
  ; // Set up log file, frame function, render function And window title
  hge_System_SetStateString(#hge_LOGFILE, Path+"hge_tut12.log");
  hge_System_SetStateFunc(#HGE_FRAMEFUNC, @frameFunc())
  hge_System_SetStateFunc(#hge_RENDERFUNC, @RenderFunc())
  hge_System_SetStateFunc(#hge_GFXRESTOREFUNC, @GfxRestoreFunc())
  hge_System_SetStateString(#HGE_TITLE, "HGE Tutorial 12- Motion Blur");
  ; // Set up video mode
  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_FPS,100)
  
  
  
  If hge_System_Initiate()
    snd = hge_Effect_Load(Path+"\menu.wav")
    
    tex = hge_Texture_Load(Path+"particles.png");
    
    If snd = 0 Or tex = 0
      ; // an error message And shutdown.
      error.s = PeekS(hge_System_GetErrorMessage(),-1,#PB_Ascii)
      Debug(error)
      MessageRequester("Error","Can't load MENU.WAV or PARTICLES.PNG", #PB_MessageRequester_Ok)
      
      hge_System_Shutdown()
      hge_Release()
      End
    EndIf
    
    
		; // Create And set up a Sprite
		*spr= hge_Sprite(tex, 96, 64, 32, 32);
    hge_Sprite_SetColor(*spr,$FFFFA000,-1);
    hge_Sprite_SetHotSpot(*spr,16,16)
    
		; // Load a font
		*fnt= hge_Font(Path+"font1.fnt");
    
		; //Create And set up a particle System
		*spt=hge_Sprite(tex, 32, 32, 32, 32);
    hge_Sprite_SetBlendMode(*spt, #BLEND_COLORMUL | #BLEND_ALPHAADD | #BLEND_NOZWRITE);
    hge_Sprite_SetHotSpot(*spt,16,16)
		*par=hge_PartSys(Path+"trail.psi",*spt);
    hge_PartSys_Fire(*par)
    
		; Create a render target And a Sprite For it
    target1=hge_Target_Create(800,600,#False);
    *tar1 = hge_Sprite(hge_Target_GetTexture(target1),0,0,800,600);
    hge_Sprite_SetBlendMode(*tar1,#BLEND_COLORMUL | #BLEND_ALPHAADD | #BLEND_NOZWRITE);    
    hge_Sprite_SetColor(*tar1,ARGB(250,255,255,255))
    
    target2=hge_Target_Create(800,600,#False);
    *tar2 = hge_Sprite(hge_Target_GetTexture(target2),0,0,800,600);
    hge_Sprite_SetBlendMode(*tar2,#BLEND_COLORMUL | #BLEND_ALPHAADD | #BLEND_NOZWRITE);    
    hge_Sprite_SetColor(*tar2,ARGB(250,255,255,255))
    
    index=1
    ; // Let's rock now!
    hge_System_Start()
    
    
    hge_Sprite_Delete(*spr)
    hge_Sprite_Delete(*spt)
    hge_Font_Delete(*fnt)
    
    hge_Sprite_Delete(*tar1)
    hge_Sprite_Delete(*tar2)
    hge_Target_Free(target1)
    hge_Target_Free(target2)
    
    ;// free loaded texture And sound
    hge_Texture_Free( tex)
    hge_Effect_Free(snd)
    
  Else
    error.s = PeekS(hge_System_GetErrorMessage(),-1,#PB_Ascii)
    MessageRequester("Error",error, #PB_MessageRequester_Ok) 
  EndIf
  
  ; // Clean up And shutdown
  hge_System_Shutdown()
  hge_Release()
  
  
EndProcedure


Main()
End
Mike
untune
User
User
Posts: 56
Joined: Sun Jul 06, 2008 10:07 pm
Location: Bolton, UK

Post by untune »

Great work! Effect looks really cool (as long as the FPS is high enough! I set it to 60 to see how it looked but it's nowhere near as good :P)

Any updates on the way then? Are you planning on adding anything else from Box2d to it? I really want to delve into the physics stuff, it looks really fun :) I know theres a Chipmunk wrapper to use as an alternative but I'm sure I read box2d was better :D

I've been trying to convert the 'Big calm' sun/moon demo over to PB using the wrapper, had it almost there but the lack of the SetHWColor, GetHWColor and Clamp function is making it nigh on impossible... I found the source in the hge code for those functions but don't do C++ so I can't really tell what's going on :S
User avatar
zxtunes.com
Enthusiast
Enthusiast
Posts: 375
Joined: Wed Apr 23, 2008 7:51 am
Location: Saint-Petersburg, Russia
Contact:

Post by zxtunes.com »

You is necessary to clear a texture at the first start.

And that appears trash.
byo
Enthusiast
Enthusiast
Posts: 635
Joined: Mon Apr 02, 2007 1:43 am
Location: Brazil

Post by byo »

Amazing!
Proud registered Purebasic user.
Because programming should be fun.
Post Reply