Any good examples of structures and lists

Just starting out? Need help? Post your questions and find answers here.
jchase1970
New User
New User
Posts: 9
Joined: Fri Nov 27, 2015 4:53 pm

Any good examples of structures and lists

Post 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
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Any good examples of structures and lists

Post 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
Joubarbe
Enthusiast
Enthusiast
Posts: 555
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: Any good examples of structures and lists

Post 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().
jchase1970
New User
New User
Posts: 9
Joined: Fri Nov 27, 2015 4:53 pm

Re: Any good examples of structures and lists

Post 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?
wombats
Enthusiast
Enthusiast
Posts: 663
Joined: Thu Dec 29, 2011 5:03 pm

Re: Any good examples of structures and lists

Post 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.
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2056
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: Any good examples of structures and lists

Post 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
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
jchase1970
New User
New User
Posts: 9
Joined: Fri Nov 27, 2015 4:53 pm

Re: Any good examples of structures and lists

Post 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
Last edited by jchase1970 on Wed Aug 28, 2019 3:44 am, edited 1 time in total.
wombats
Enthusiast
Enthusiast
Posts: 663
Joined: Thu Dec 29, 2011 5:03 pm

Re: Any good examples of structures and lists

Post 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
Joubarbe
Enthusiast
Enthusiast
Posts: 555
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: Any good examples of structures and lists

Post by Joubarbe »

And no, there's no native RandomFloat() function.

Code: Select all

Random(2147483647)/2147483647
Even better :)
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Any good examples of structures and lists

Post 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
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Any good examples of structures and lists

Post by Mijikai »

Nice Macro @mk-soft thx 8)
Post Reply