Sorting list of structure

Just starting out? Need help? Post your questions and find answers here.
FranzWesten
New User
New User
Posts: 4
Joined: Sat Dec 16, 2023 8:58 am

Sorting list of structure

Post by FranzWesten »

Hi there, I started using PureBasic yesterday for the first time.

I like it so far, but I have to get used to the syntax.

But I have a weird problem here, but perhaps I am overlookign something.

I created a function to define RenderObjects by passing position to draw a sprite. )With Z I want to sort the drawing order later):

Code: Select all

Procedure DefineRenderObjectAsImage(*rdrObject.RenderObject, ident$, x.i, y.i, sizeX.i, sizeY.i, z.i)
  Static objId.i
  *rdrObject\Id = objId.i
  *rdrObject\ValId = GetImage(ident$)
  *rdrObject\X = x
  *rdrObject\Y = y
  *rdrObject\Width = sizeX
  *rdrObject\Height = sizeY
  *rdrObject\Z = z
  *rdrObject\Type = #Sprite
  objId.i = objId.i + 1
  EndProcedure
Here are three objects I use:

Code: Select all

DefineRenderObjectAsImage(rdrObj1, "bia", 5, 5, 300, 300, 1)
DefineRenderObjectAsImage(rdrObj2, "bia", 155, 5, 600, 200, 25)
DefineRenderObjectAsImage(rdrObj3, "bia", 175, 85, 600, 200, 20)
But when I execute the drawing like that:

Code: Select all

Procedure RenderAll()
  SortStructuredList(RENDERCONT\RenderObjs(), #PB_Sort_Ascending, OffsetOf(RenderObject\Z), TypeOf(RenderObject\Z))
  Protected c.i = 0
  ForEach RENDERCONT\RenderObjs()
    Debug Str(c) + " . " + Str(RENDERCONT\RenderObjs()\Z) + " . " + Str(RENDERCONT\RenderObjs()\Id)
    Select RENDERCONT\RenderObjs()\Type
      Case #Sprite
        DrawSprite(RENDERCONT\RenderObjs())
    EndSelect
    c = c + 1
    ; FreeMemory(RENDERCONT\RenderObjs())
  Next
  ClearList(RENDERCONT\RenderObjs())
EndProcedure
Thing is, the drawing order becomes something like that, here see the debug:

0 . 20 . 2
1 . 25 . 1
2 . 1 . 0

So apparently in second place is z=25, so depending on ascending or descending it should either be first or last, but ends up in the middle. Am I missing something?
infratec
Always Here
Always Here
Posts: 7665
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Sorting list of structure

Post by infratec »

I hate to create my own code to reproduce the behavior.
And I'm not sure if my version is like yours:

Code: Select all

EnableExplicit

#Sprite = 1

Structure RenderObject
  Id.i
  ValId.i
  X.i
  Y.i
  Width.i
  Height.i
  Z.i
  Type.i
  objId.i
EndStructure


Structure Test_Structure
  List RenderObjs.RenderObject()
EndStructure



Global RENDERCONT.Test_Structure

Procedure DefineRenderObjectAsImage(*rdrObject.RenderObject, ident$, x.i, y.i, sizeX.i, sizeY.i, z.i)
  Static objId.i
  *rdrObject\Id = objId.i
  *rdrObject\ValId = 1
  *rdrObject\X = x
  *rdrObject\Y = y
  *rdrObject\Width = sizeX
  *rdrObject\Height = sizeY
  *rdrObject\Z = z
  *rdrObject\Type = #Sprite
  objId.i = objId.i + 1
EndProcedure


Procedure RenderAll()
  SortStructuredList(RENDERCONT\RenderObjs(), #PB_Sort_Ascending, OffsetOf(RenderObject\Z), TypeOf(RenderObject\Z))
  Protected c.i = 0
  ForEach RENDERCONT\RenderObjs()
    Debug Str(c) + " . " + Str(RENDERCONT\RenderObjs()\Z) + " . " + Str(RENDERCONT\RenderObjs()\Id)
    Select RENDERCONT\RenderObjs()\Type
      Case #Sprite
        ;DrawSprite(RENDERCONT\RenderObjs())
    EndSelect
    c = c + 1
    ; FreeMemory(RENDERCONT\RenderObjs())
  Next
  ClearList(RENDERCONT\RenderObjs())
EndProcedure


Define.i rdrObj1, rdrObj2, rdrObj3

rdrObj1 = AddElement(RENDERCONT\RenderObjs())
rdrObj2 = AddElement(RENDERCONT\RenderObjs())
rdrObj3 = AddElement(RENDERCONT\RenderObjs())

DefineRenderObjectAsImage(rdrObj1, "bia", 5, 5, 300, 300, 1)
DefineRenderObjectAsImage(rdrObj2, "bia", 155, 5, 600, 200, 25)
DefineRenderObjectAsImage(rdrObj3, "bia", 175, 85, 600, 200, 20)

RenderAll()
This results in;
0 . 1 . 0
1 . 20 . 2
2 . 25 . 1
So ... no problem here,
FranzWesten
New User
New User
Posts: 4
Joined: Sat Dec 16, 2023 8:58 am

Re: Sorting list of structure

Post by FranzWesten »

Thanks :D , worked for me to this way.

I think I found the problem.

I was doing it like that, passing the structure objects to a procedure:

Code: Select all

Structure RenderContainer
  List *RenderObjs.RenderObject()
EndStructure

Global RENDERCONT.RenderContainer

Procedure AddRenderObject(*rdrObj.RenderObject)
  AddElement(RENDERCONT\RenderObjs())
  RENDERCONT\RenderObjs() = *rdrObj
EndProcedure
as you see, i am using a pointer to add. apparently sorting this does not work correctly.

On the other hand, is there 1) a way and how do i pass a structure to a procedure where it is processed and then added to some list as a value type?
infratec
Always Here
Always Here
Posts: 7665
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Sorting list of structure

Post by infratec »

In this case you need CopyStructure() because else you copy only the address to an object which maybe freed later.
With CopyStructure() you copy the content.

Code: Select all

Procedure AddRenderObject(*rdrObj.RenderObject)
  AddElement(RENDERCONT\RenderObjs())
  CopyStructure(*rdrObj, RENDERCONT\RenderObjs(), RenderObject)
EndProcedure
infratec
Always Here
Always Here
Posts: 7665
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Sorting list of structure

Post by infratec »

Btw. your structure should look like this:

Code: Select all

Structure RenderContainer
  List RenderObjs.RenderObject()
EndStructure
not List *RenderObjs.RenderObject()
FranzWesten
New User
New User
Posts: 4
Joined: Sat Dec 16, 2023 8:58 am

Re: Sorting list of structure

Post by FranzWesten »

Makes sense. Thx. 8)
FranzWesten
New User
New User
Posts: 4
Joined: Sat Dec 16, 2023 8:58 am

Re: Sorting list of structure

Post by FranzWesten »

another question!

I defined a prototype with

Code: Select all

Prototype.i PRenderFunc(*rdrObj.RenderObject)
Now I tried to assign it:

Code: Select all

        test.PRenderFunc = @DrawSprite
        test(RENDERCONT\RenderObjs())
What is the correct syntax to create this function pointer? Can function pointers also be used in lists or maps?
User avatar
Mijikai
Addict
Addict
Posts: 1520
Joined: Sun Sep 11, 2016 2:17 pm

Re: Sorting list of structure

Post by Mijikai »

FranzWesten wrote: Sat Dec 16, 2023 2:44 pm What is the correct syntax to create this function pointer?
@DrawSprite()
FranzWesten wrote: Sat Dec 16, 2023 2:44 pm Can function pointers also be used in lists or maps?
Yes :)
Post Reply