Page 1 of 1

Any good examples of structures and lists

Posted: Mon Aug 26, 2019 3:55 pm
by jchase1970
I am looking for any good examples of using structures, storing them in lists, adding and removing from the list.

I think I have structures down but not lists and not using them together.

Thank you

Re: Any good examples of structures and lists

Posted: Mon Aug 26, 2019 5:01 pm
by Mijikai
Mby it helps:

Code: Select all

EnableExplicit

Structure DUMMY_STRUCT
  name.s
EndStructure

Global NewList dummy.DUMMY_STRUCT();<- list with a struct
Global *entry

AddElement(dummy());<- add entry
*entry = dummy();<- store the ptr of the created struct (for quick access)
dummy()\name = "Hello! 1"

AddElement(dummy());<- add another entry
dummy()\name = "Hello! 2"

ForEach dummy();<- show what has been stored in the list
  Debug dummy()\name
Next

;we can only delete the currently active element!
;to delete the 1st entry we can use the ptr stored before (*entry)

ChangeCurrentElement(dummy(),*entry);<- select entry 1
DeleteElement(dummy());<- delete entry

ForEach dummy()
  Debug dummy()\name
Next

;to get rid of all entries
ClearList(dummy())

;to free the allocated memory
FreeList(dummy())

End

Re: Any good examples of structures and lists

Posted: Mon Aug 26, 2019 6:29 pm
by Joubarbe
You could also assign the structure to your pointer:

Code: Select all

Global *entry.DUMMY_STRUCTURE
[...]
Debug *entry\name
You can also use FirstElement() (and LastElement()) instead of ChangeCurrentElement().

Re: Any good examples of structures and lists

Posted: Mon Aug 26, 2019 7:13 pm
by jchase1970
";we can only delete the currently active element!"

assuming i'm moving images and when they move off the screen I can do it like,

cycle thru a list with foreach and checking say boundries of the screen and if the current object is out of boundry and I want to get rid of it I can just DeleteElement(dummy()) because it will be the current element in the list?

Re: Any good examples of structures and lists

Posted: Mon Aug 26, 2019 7:16 pm
by wombats
jchase1970 wrote:";we can only delete the currently active element!"

assuming i'm moving images and when they move off the screen I can do it like,

cycle thru a list with foreach and checking say boundries of the screen and if the current object is out of boundry and I want to get rid of it I can just DeleteElement(dummy()) because it will be the current element in the list?
Yes.

Re: Any good examples of structures and lists

Posted: Mon Aug 26, 2019 11:44 pm
by Andre
Maybe this chapter from the "UserGuide" (part of the PB refererence manual) gives also some ideas:
https://www.purebasic.com/documentation ... _data.html

Re: Any good examples of structures and lists

Posted: Wed Aug 28, 2019 3:12 am
by jchase1970
Thank you for your help I was able to get it to work and understand a little more. I made a little snow falling effect to try it out

Code: Select all

If InitSprite() = 0 Or InitKeyboard() = 0
  MessageRequester("Error", "Can't open the sprite system", 0)
  End
EndIf

Global screenwidth=1280
Global screenheight=1024
Global gravity.f=0.5
Global wind.f
Structure snow
  x.f
  y.f
  weight.f
EndStructure

Global NewList snowflakes.snow()



Procedure CreateSnow()
  AddElement(snowflakes())
 
  snowflakes()\x = Random(screenwidth)
  snowflakes()\y = 0
  snowflakes()\weight = Random(100)/100
EndProcedure


Procedure updatesnow()
ForEach snowflakes()
  snowflakes()\x=snowflakes()\x+wind*snowflakes()\weight
  If snowflakes()\x<0
    snowflakes()\x=screenwidth
  ElseIf snowflakes()\x>ScreenWidth()
    snowflakes()\x=0
  EndIf
  snowflakes()\y=snowflakes()\y+gravity+snowflakes()\weight
  If snowflakes()\y<screenheight
    Circle(snowflakes()\x,snowflakes()\y,snowflakes()\weight*3,RGB(255,255,255))
  ElseIf snowflakes()\y>=screenheight
    DeleteElement(snowflakes())
  EndIf
Next  
EndProcedure

If OpenScreen(screenwidth, screenheight, 32, "My Screen")
  Repeat
    wind=wind+0.5-(Random(100)/100)
    If wind<-4 
      wind=wind+0.5
    ElseIf wind>4
      wind=wind-0.5
    EndIf
    ClearScreen(RGB(0,0,0))  
    createsnow()
    StartDrawing(ScreenOutput())
    updatesnow()
    StopDrawing()
    FlipBuffers()
    ExamineKeyboard()
  Until  Quit Or KeyboardPushed(#PB_Key_Escape)
Else
  MessageRequester("Error", "Can't open windowed screen!", 0)
EndIf
I do have 2 questions that I came across as I did this and I am coming from using Blitzmax so

question 1 is there a random float function? I used random(100)/100 to get a random float because I could not find another random function.

question 2 is there a simple syntax to preform operation to variable self? In Blitzmax if I want to do a=a+5 for example I can write a:+5

Re: Any good examples of structures and lists

Posted: Wed Aug 28, 2019 3:34 am
by wombats
jchase1970 wrote:question 2 is there a simple syntax to preform operation to variable self? In Blitzmax if I want to do a=a+5 for example I can write a:+5/
It's even simpler than in BlitzMax. You just use the operators on the variable:

Code: Select all

Define a = 10
a + 10
a - 5
a * 10
a / 5

Re: Any good examples of structures and lists

Posted: Wed Aug 28, 2019 2:33 pm
by Joubarbe
And no, there's no native RandomFloat() function.

Code: Select all

Random(2147483647)/2147483647
Even better :)

Re: Any good examples of structures and lists

Posted: Wed Aug 28, 2019 4:41 pm
by mk-soft
Update :wink:

Code: Select all

Macro RandomFloat(Steps)
  (1.0 * Random(Steps) / Steps#.0)
EndMacro

For i = 1 To 100
  Debug StrF(RandomFloat(1000), 3)
Next
Debug "**********"
For i = 1 To 100
  Debug StrF(RandomFloat(5), 3)
Next

Re: Any good examples of structures and lists

Posted: Wed Aug 28, 2019 5:23 pm
by Mijikai
Nice Macro @mk-soft thx 8)