Results 1 to 8 of 8

Thread: Unlimited loops

  1. #1

    Unlimited loops

    Hi all,

    im not sure how i can explain this but i will try..

    i have a structure which basicly looks like this:

    TStructureList = class;

    TStructure = class
    StructureType {either stGroup, stPolyhedron, stFace or stEntity}
    children: TStructureList;
    .. Other properties ..
    end;

    Now in my editor, i have a Treeview, which i want to display all these structure objects
    eg.


    Groups can contain: Groups, Polyhedrons, and Entities
    Polyhedrons can only contain faces
    Faces and Entites do not contain anything.

    The only way i can think of doing this is with lots of If and For Loops?

    Please can anyone give their ideas of how i can do this??

    Thanks for any help,
    M109uk
    M109uk
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

  2. #2
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Unlimited loops

    I'm a bit rusty on TreeView objects, but as far as I know and recall; You would need one main for loop and a set of if statements each with a secondary. It would be best focus on keeping track of the object's parent rather than it's child since naturally all objects can have only one parent, by many children.

    Something like:
    [pascal]for i := lower(List.Items) to upper(List.Items) do
    begin
    if (List.Items[i].StructureType = stGroup) then
    begin
    for j := lower(List.Items) to upper(List.Items) do
    {find parent in List, if any, and stick it inside that one}
    end;
    if (List.Items[i].StructureType = stPolyhedron) then
    begin
    for j := lower(List.Items) to upper(List.Items) do
    {find parent in List and stick it inside that one}
    end;
    if (List.Items[i].StructureType = stFace) then
    begin
    for j := lower(List.Items) to upper(List.Items) do
    {find parent in List and stick it inside that one}
    end;
    {etc...}
    end;[/pascal]

    I believe you get the point right? This is the most straitforward way to do it, I think. There may be a better way to do it, but if someone can correct me, I'd be most interested. This is the way I've always done this type of structure listing.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  3. #3

    Unlimited loops

    Hi, Thanks for your reply..

    in the end i tried something very similar, and i got curious to see if something would work so i tried the following code::

    [pascal]
    procedure Update_Structure;
    var
    i: Integer;

    procedure AddFace(aParent: TElTreeItem; Face: TcxWorldData);
    begin
    With StructureET.Items.AddItem(aParent) Do
    Begin
    Text := Face.Text;
    ImageIndex := iiFace;
    StateImageIndex := siFace;
    Data := Face;
    End;
    end;

    procedure AddPolyhedron(aParent: TElTreeItem; Polyhedron: TcxWorldData);
    var
    anItem: TElTreeItem;
    i: Integer;
    begin
    anItem := StructureET.Items.AddItem(aParent);
    With anItem Do
    Begin
    Text := Polyhedron.Text;
    ImageIndex := iiPolyhedron;
    StateImageIndex := siPolyhedron;
    Data := Polyhedron;
    End;
    For i := 0 To Polyhedron.Children.Count-1 Do
    AddFace(anItem, Polyhedron.Children[i]);
    end;

    procedure AddGroup(aParent: TElTreeItem; Group: TcxWorldData);
    var
    anItem: TElTreeItem;
    i: Integer;
    begin
    anItem := StructureET.Items.AddItem(aParent);
    With anItem Do
    Begin
    Text := Group.Text;
    ImageIndex := iiGroup;
    StateImageIndex := siGroup;
    Data := Group;
    End;

    For i := 0 To Group.Children.Count-1 Do
    Case Group.Children[i].DataType Of
    dtGroup: AddGroup(anItem,Group.Children[i]);
    dtPolyhedron: AddPolyhedron(anItem,Group.Children[i]);
    dtFace: AddFace(anItem,Group.Children[i]);
    dtEntity: ;//AddEntity(Nil, World.Data[i]);
    End;
    end;

    begin
    StructureET.Items.Clear;

    With StructureET.Items.AddItem(Nil) Do
    Begin
    Text := 'worldspawn';
    ImageIndex := iiWorldspawn;
    StateImageIndex := siWorldspawn;
    End;

    With StructureET.Items.AddItem(Nil) Do
    Begin
    Text := 'environment';
    ImageIndex := iiEnvironment;
    StateImageIndex := siEnvironment;
    End;

    ShowMessage('World Data Count :: '+InttoStr(World.Data.Count));
    For i := 0 To World.Data.Count-1 Do
    Begin
    Case World.Data[i].DataType Of
    dtGroup: AddGroup(Nil, World.Data[i]);
    dtPolyhedron: AddPolyhedron(Nil, World.Data[i]);
    dtEntity: ;//AddEntity(Nil, World.Data[i]);
    End;
    End;
    [/pascal]

    I have tested, and so far it seems to be working great, but the only problem i have with doing it like this is where i clear the list out so when ever i want to add a new group or something the whole list is recreated and it just looks crap do you have any ideas around this? i tried to do the obvious just adding it to the list when they add the new object, but it causes problems when using a selected object :s

    also would you know a function that can format a float to a float, im using FormatFloat and it works great but when i convert it back to a float using StrtoFloat i get something like 0.50000003456 but all i want is something like 0.5!
    M109uk
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

  4. #4

    Unlimited loops

    ah no worries, i think i found away around it, just a few dozen extra lines of code and it works great :lol:

    But im still having problems with the FloatFormat :x
    M109uk
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

  5. #5
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Unlimited loops

    Are you saying that you want to round off the number of digits to the right of the decimal point? If so then try this:
    [pascal]Val := 10.009;
    roundedValue := StrToFloat(FormatFloat('#.##', Val));[/pascal]
    roundedValue will return 10.01. Just use the proper number of #s you want after the decimal point. There is faster ways of rounding off floating point values though. I wouldn't reccomend using this in a game where code speed is a priority.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  6. #6

    Unlimited loops

    at the moment i have:

    [pascal]
    Str1,Str2: String;
    V2F: Array[0..1] of Single;

    Str1 := FormatFloat('0.00', cxwV2FEditorF.JvSpinEdit1.Value);
    Str2 := FormatFloat('0.00', cxwV2FEditorF.JvSpinEdit2.Value);

    V2F[0] := StrtoFloat(Str1);
    V2F[1] := StrtoFloat(Str2);
    [/pascal]

    but when i check the values you a treeview item i get:
    -0.2000000002980232 instead of -0.20

    if tried using FormatFloat('#.##', ....); but thats the same but it also causes a problem when the value is 0.00.

    at the moment it doesnt really mater for my editor, but im concerned that it could cause problems when im rendering it in my engine??
    M109uk
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

  7. #7
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Unlimited loops

    Ok well in that case, you can easily check to see if FormatFloat('#.##', Value) returns a string length of 0.

    [pascal]Str1 := FormatFloat('#.##', cxwV2FEditorF.JvSpinEdit1.Value);
    if (Length(Str1) = 0) then
    V2F[0] := 0
    else
    V2F[0] := StrtoFloat(Str1);[/pascal]

    That should work flawlessly.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  8. #8
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Unlimited loops

    Ok try using '0.##' instead. Then you'll at least get a 0 returned. With only #s it doesn't bother to hold the value and you end up with a 0 length string.

    You know what the difference between using the 0 and # characters are right? # doesn't care if there are values that remain there. 0 makes sure that a a value of 0 is there in the string.

    So...
    [pascal]StrToFloat(FormatFloat('0.##', 0.00));[/pascal]
    will return 0. No errors, one line.
    Jason McMillen
    Pascal Game Development
    Co-Founder





Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •