determine entity to update next depending on milli values

Started by RemiD, September 13, 2019, 21:16:03

Previous topic - Next topic

RemiD


;determine entity to update next depending on milli values by RemiD (20190913)
;imagine you have to update the lighting shading, or the shadow caster, or the shadow, or a special effect like glow, on all turning moving animating entities of a scene, it may take too much time to do that each frame for all entities.
;however, using milli values, you can determine which was the entity which was updated the longest time ago, and only update this entity this frame. (or several, as you want...)
Graphics3D(854,480,32,2)

SeedRnd(MilliSecs())

Global X16Font = LoadFont("System",16,False,False,False)

Global EntitiesCount%
Dim EntityMilli%(10)

;to store the temps indexes, properties
Global TempsCount%
Dim TempI%(100)
Dim TempValue%(100)
Dim TempState%(100)

;to order values (from lowest to highest or from heighest to lowest)
Global OrderedsCount%
Dim OrderedI%(100)
Dim OrderedValue%(100)

For n% = 1 To 10 Step 1
EntitiesCount = EntitiesCount + 1 : EI% = EntitiesCount
EntityMilli(EI) = -2147483647+Rand(1,1000)
Next

Global DebugImage = CreateImage(300,16*25)

Global MainLoopTimer = CreateTimer(30)

Main()

End()

Function Main()

Repeat

  MainLoopMilliStart% = MilliSecs()

  If( KeyHit(57)=1 ) ;key space
   SetBuffer(ImageBuffer(DebugImage))
   ClsColor(010,010,010) : Cls()
   SetFont(X16Font) : Color(255,255,255)
   PX% = 0 : PY% = -16
   ;list all entities in area and in view
   TempsCount = 0
   For EI% = 1 To EntitiesCount Step 1
    TempsCount = TempsCount + 1 : TI% = TempsCount
    TempI(TI) = EI
    TempValue(TI) = EntityMilli(EI)
    TempState(TI) = True
   Next
   PX% = 0 : PY% = PY+16 : TStr$ = "listed values : " : CText(TStr,PX,PY)
   For TI% = 1 To TempsCount Step 1
    PX% = 0 : PY% = PY+16 : TStr$ = TempValue(TI) : CText(TStr,PX,PY)
   Next
   ;order the listed entities by Milli values (from lowest to highest)
   MaxLoops% = TempsCount
   Loops% = 0
   OrderedsCount = 0
   While( Loops < MaxLoops )
    Loops = Loops + 1
    SValue% = 2147483647-1
    SI% = 0
    For TI% = 1 To TempsCount Step 1
     If( TempState(TI) = True )
      If( TempValue(TI) < SValue )
       SI = TI
       SValue = TempValue(TI)
      EndIf
     EndIf
    Next
    TempState(SI) = False
    OrderedsCount = OrderedsCount + 1 : OI% = OrderedsCount
    OrderedValue(OI) = SValue
    OrderedI(OI) = SI
   Wend
   PX% = 0 : PY% = PY+16 : TStr$ = "" : CText(TStr,PX,PY)
   PX% = 0 : PY% = PY+16 : TStr$ = "ordered values : " : CText(TStr,PX,PY)
   For OI% = 1 To OrderedsCount Step 1
    PX% = 0 : PY% = PY+16 : TStr$ = OrderedValue(OI) : CText(TStr,PX,PY)
   Next
   ;choose the lowest Milli value and the corresponding entity
   OI% = 0
   OI = OI + 1
   If( OI <= OrderedsCount )
    SValue = OrderedValue(OI) : SI% = OrderedI(OI)   
    EI% = TempI(SI)
    PX% = 0 : PY% = PY+16 : TStr$ = "" : CText(TStr,PX,PY)
    PX% = 0 : PY% = PY+16 : TStr$ = "entity to update : "+EI : CText(TStr,PX,PY)
    ;update this entity
    ;
    ;update Milli value of entity
    EntityMilli(EI) = MilliSecs()
   EndIf
  EndIf

  SetBuffer(BackBuffer())
  ClsColor(000,000,000) : Cls()

  DrawImage(DebugImage,GraphicsWidth()/2-ImageWidth(DebugImage)/2,GraphicsHeight()/2-ImageHeight(DebugImage)/2)

  SetFont(X16Font) : Color(255,255,255)
  CText("FPS = "+FPS,0,0)

  ;Flip(1)
  WaitTimer(MainLoopTimer)
  VWait():Flip(False)

  MainLoopMilliTime = MilliSecs() - MainLoopMilliStart
  If( MainLoopMilliTime < 1 )
   MainLoopMilliTime = 1
  EndIf

  FPS% = 1000.0/MainLoopMilliTime

Until( KeyDown(1)=1 )

End Function

Function CText(TextStr$,PX%,PY%)

Text(PX,PY,TextStr,False,False)

End Function

Santiago