Ooops
March 01, 2021, 10:45:04 PM

Author Topic: [bb] deletes an entry in an array list + reorganize the array list by RemiD [ 1+ years ago ]  (Read 435 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : deletes an entry in an array list + reorganize the array list
Author : RemiD
Posted : 1+ years ago

Description : 0.15ms for a list of 100 entities

Code :
Code: BlitzBasic
  1. ;This code shows you how to delete an entry from an array and then to reorganize the array list.
  2. ;Notice how the meshes are not deleted but rather their references are copied in the Swap_Array and then copied again in the Array
  3.  
  4. Graphics3D(640,480,32,2)  
  5.  
  6. SeedRnd(MilliSecs())  
  7.  
  8. Global Camera = CreateCamera()
  9. CameraRange(Camera,0.1,1000)
  10.  
  11. Global CubicalXMesh = CreateCube()
  12. ScaleMesh(CubicalXMesh,1.0/2,1.0/2,1.0/2)
  13. HideEntity(CubicalXMesh)
  14.  
  15. Global CylindricalXMesh = CreateCylinder(8)
  16. ScaleMesh(CylindricalXMesh,1.0/2,1.0/2,1.0/2)
  17. HideEntity(CylindricalXMesh)
  18.  
  19. Global SphericalXMesh = CreateSphere(8)
  20. ScaleMesh(SphericalXMesh,1.0/2,1.0/2,1.0/2)
  21. HideEntity(SphericalXMesh)
  22.  
  23. Global PyramidalXMesh = CreateCone(4)
  24. ScaleMesh(PyramidalXMesh,1.0/2,1.0/2,1.0/2)
  25. HideEntity(PyramidalXMesh)
  26.  
  27. ;Shapes :
  28. Const CCubical% = 1
  29. Const CCylindrical% = 2
  30. Const CSpherical% = 3
  31. Const CPyramidal% = 4
  32.  
  33. ;Materials :
  34. Const CTin% = 1
  35. Const CCopper% = 2
  36. Const CSilver% = 3
  37. Const CGold% = 4
  38.  
  39. ;Lists to store the properties and the components of the Things
  40. Global ThingsCount% = 0
  41. Dim ThingShape%(100)
  42. Dim ThingMaterial%(100)
  43. Dim ThingName$(100)
  44. Dim ThingMesh(100)
  45.  
  46. ;Lists to temporarily store the properties and the components of the Things
  47. Global Swap_ThingsCount% = 0
  48. Dim Swap_ThingShape%(100)
  49. Dim Swap_ThingMaterial%(100)
  50. Dim Swap_ThingName$(100)
  51. Dim Swap_ThingMesh(100)
  52.  
  53. BuildThings()
  54.  
  55. PositionEntity(Camera,5,10,-5)
  56. RotateEntity(Camera,45,0,0)
  57.  
  58. DLight = CreateLight(1)
  59. LightColor(DLight,250,250,250)
  60. PositionEntity(DLight,-100,100,-100)
  61. RotateEntity(DLight,45,-45,0)
  62.  
  63. AmbientLight(050,050,050)
  64.  
  65. PositionEntity(Camera,50,50,-25)
  66. RotateEntity(Camera,45,0,0)
  67.  
  68. Global RoutineTime%
  69.  
  70. While( KeyDown(1)<>1 )
  71.  
  72.  If(KeyHit(57)>0)
  73.   If(ThingsCount > 0)
  74.    Id% = Rand(1,ThingsCount)
  75.    DeleteThing(Id)
  76.    RoutineStart% = MilliSecs()
  77.    ReorganizeThingsList()
  78.    RoutineTime = MilliSecs() - RoutineStart
  79.   EndIf
  80.  EndIf
  81.  
  82.  UpdateThings()
  83.  
  84.  SetBuffer(BackBuffer())
  85.  RenderWorld()
  86.  
  87.  Text(0,0,"Triangles = "+TrisRendered())
  88.  Text(0,20,"ThingsCount = "+ThingsCount)
  89.  Text(0,40,"RoutineTime = "+RoutineTime)
  90.  Text(0,60,"Press Space to delete a random Thing in the list.")
  91.  
  92.  Flip(1)
  93.  
  94. Wend
  95.  
  96. End()
  97.  
  98. Function BuildThings()
  99.  
  100.  For i% = 1 To 100
  101.  
  102.   ThingsCount = ThingsCount + 1
  103.   Id% = ThingsCount
  104.  
  105.   RandomChoice% = Rand(1,4)
  106.   If(RandomChoice = 1)
  107.    ThingShape(Id) = CCubical
  108.   Else If(RandomChoice = 2)
  109.    ThingShape(Id) = CCylindrical
  110.   Else If(RandomChoice = 3)
  111.    ThingShape(Id) = CSpherical
  112.   Else If(RandomChoice = 4)
  113.    ThingShape(Id) = CPyramidal
  114.   EndIf
  115.  
  116.   If(ThingShape(Id) = CCubical)
  117.    ThingMesh(Id) = CopyEntity(CubicalXMesh)
  118.    ThingName(Id) = "Cube"
  119.   ElseIf(ThingShape(Id) = CCylindrical)
  120.    ThingMesh(Id) = CopyEntity(CylindricalXMesh)
  121.    ThingName(Id) = "Cylinder"
  122.   ElseIf(ThingShape(Id) = CSpherical)
  123.    ThingMesh(Id) = CopyEntity(SphericalXMesh)
  124.    ThingName(Id) = "Sphere"
  125.   ElseIf(ThingShape(Id) = CPyramidal)
  126.    ThingMesh(Id) = CopyEntity(PyramidalXMesh)
  127.    ThingName(Id) = "Pyramid"
  128.   EndIf
  129.  
  130.   RandomChoice% = Rand(1,4)
  131.   If(RandomChoice = 1)
  132.    ThingMaterial(Id) = CTin
  133.    ThingName(Id) = ThingName(Id)+"Of"+"Tin"
  134.   Else If(RandomChoice = 2)
  135.    ThingMaterial(Id) = CCopper
  136.    ThingName(Id) = ThingName(Id)+"Of"+"Copper"
  137.   Else If(RandomChoice = 3)
  138.    ThingMaterial(Id) = CSilver
  139.    ThingName(Id) = ThingName(Id)+"Of"+"Silver"
  140.   Else If(RandomChoice = 4)
  141.    ThingMaterial(Id) = CGold
  142.    ThingName(Id) = ThingName(Id)+"Of"+"Gold"
  143.   EndIf
  144.  
  145.   If(ThingMaterial(Id) = CTin)
  146.    EntityColor(ThingMesh(Id),175,175,175)
  147.   ElseIf(ThingMaterial(Id) = CCopper)
  148.    EntityColor(ThingMesh(Id),160,080,000)
  149.   ElseIf(ThingMaterial(Id) = CSilver)
  150.    EntityColor(ThingMesh(Id),125,125,125)
  151.   ElseIf(ThingMaterial(Id) = CGold)
  152.    EntityColor(ThingMesh(Id),175,165,000)
  153.   EndIf
  154.  
  155.   PositionEntity(ThingMesh(Id),Rnd(0,100),Rnd(0,10),Rnd(0,100))
  156.   RotateEntity(ThingMesh(Id),0,Rnd(-180,180),0)
  157.  
  158.   NameEntity(ThingMesh(Id),"THI"+Str(Id))
  159.  
  160.  Next
  161.  
  162. End Function
  163.  
  164. Function UpdateThings()
  165.  
  166.  For Id% = 1 To ThingsCount
  167.  
  168.   MoveEntity(ThingMesh(Id),0,0,0.1)
  169.  
  170.   If( EntityX(ThingMesh(Id),True) <= 0 Or EntityX(ThingMesh(Id),True) >= 100 Or EntityZ(ThingMesh(Id),True) <= 0 Or EntityZ(ThingMesh(Id),True) >= 100 )
  171.    RotateEntity(ThingMesh(Id),0,Rnd(-180,180),0)
  172.   EndIf
  173.  
  174.  Next
  175.  
  176. End Function
  177.  
  178. Function DeleteThing(Id%)
  179.  
  180.  ThingShape(Id) = 0
  181.  ThingMaterial(Id) = 0
  182.  FreeEntity(ThingMesh(Id))
  183.  ThingMesh(Id) = 0
  184.  ThingName(Id) = ""
  185.  
  186. End Function
  187.  
  188. Function ReorganizeThingsList()
  189.  
  190.  ;Copy the remaining entries in the Thing array to the Swap_Thing array
  191.  Swap_ThingsCount = 0
  192.  For Id% = 1 To ThingsCount
  193.   If(ThingMesh(Id) <> 0)
  194.    Swap_ThingsCount = Swap_ThingsCount + 1
  195.    SId% = Swap_ThingsCount
  196.    Swap_ThingShape(SId) = ThingShape(Id)
  197.    Swap_ThingMaterial(SId) = ThingMaterial(Id)
  198.    Swap_ThingName(SId) = ThingName(Id)
  199.    Swap_ThingMesh(SId) = ThingMesh(Id)
  200.   EndIf
  201.  Next
  202.  
  203.  ;Delete all entries in the Thing array
  204.  For Id% = 1 To ThingsCount
  205.   ThingShape(Id) = 0
  206.   ThingMaterial(Id) = 0
  207.   ThingName(Id) = ""
  208.   ThingMesh(Id) = 0
  209.  Next
  210.  ThingsCount = 0
  211.  
  212.  ;Copy all entries in the Swap_Thing to the Thing array
  213.  ThingsCount = 0
  214.  For SId% = 1 To Swap_ThingsCount
  215.   ThingsCount = ThingsCount + 1
  216.   Id% = ThingsCount
  217.   ThingShape(Id) = Swap_ThingShape(SId)
  218.   ThingMaterial(Id) = Swap_ThingMaterial(SId)
  219.   ThingName(Id) = Swap_ThingName(SId)
  220.   ThingMesh(Id) = Swap_ThingMesh(SId)
  221.  Next
  222.  
  223.  ;Delete all entries in the Swap_Thing array
  224.  For SId% = 1 To Swap_ThingsCount
  225.   Swap_ThingShape(SId) = 0
  226.   Swap_ThingMaterial(SId) = 0
  227.   Swap_ThingName(SId) = ""
  228.   Swap_ThingMesh(SId) = 0
  229.  Next
  230.  Swap_ThingsCount = 0
  231.  
  232.  ;Attribute a new name to each entry (pickable/collidable) in the Thing array
  233.  For Id% = 1 To ThingsCount
  234.   NameEntity(ThingMesh(Id),"THI"+Str(Id))
  235.  Next
  236.  
  237. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal