Results 1 to 16 of 16

Thread: Need a piece of code optimized for my need!

  1. #1

    Thread Starter
    Lively Member kivisoft@'s Avatar
    Join Date
    Jul 2011
    Location
    Iran
    Posts
    96

    Unhappy Need a piece of code optimized for my need!

    I have 2 arrays. the first array holds the IDs to my sprites and the second array holds the IDs to my objects. and in the main loop, everything is Rendered this way:


    Code:
    '.....................
    For c = 1 To Obj_Count                                                                                   'Loops from the first object to the last
    
    For cc = 1 To Obj_IndexC(c)                    'Each object might have some instances of it's own. loops from the first to the last instance
    
    If Obj_Vis(c, cc) = True And Obj_Removed(c, cc) = False Then           'Check to see if object is visible and it's not removed.
    
    If Obj_Scene(c, cc) = SceneACTV Then                                            'Checks to see if this object belong to this Scene
    
        If JSprA(Obj_Spr(c)).Seq = 1 Then                                                'See if the current object is marked as animation
    
       'Each object has an sprite. Obj_Spr(c) holds their sprite ID. Animate and draw object
    
        JSprA(Obj_Spr(c)).AnimateS Me.hdc, Obj_X(c, cc), Obj_Y(c, cc),
    
        ElseIf JSprA(Obj_Spr(c)).Seq = 0 Then                                                         'If not an animation
    
        JSprA(Obj_Spr(c)).TDraw Me.hdc, Obj_X(c, cc), Obj_Y(c, cc), 0, 0, JSprA(Obj_Spr(c)).TransparentColor             'Simply just draw it.
    
        End If
    
    End If
    End If
    Next
    Next
    '.....................

    in this piece of code I just loop from the first object to the last and the Draw order is not handled.

    Each object has a Obj_ZDepth value which is an array too. I used bubble sort to sort this array but I have no Idea how to associate it with my code in the main loop (above code)

    Need help desperately!

  2. #2
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Need a piece of code optimized for my need!

    What is contained in the Obj_ZDepth array ? If it's just the Obj_Index element number then that should be used in the outer loop
    Code:
    For c1 = 1 To UBound(obj_ZDepth)                                                                                   'Loops from the first object to the last
        c = obj_ZDepth(c1)
        For cc = 1 To obj_Index(c)                    'Each object might have some instances of it's own. loops from the first to the last instance
            If Obj_Vis(c, cc) = True And Obj_Removed(c, cc) = False Then           'Check to see if object is visible and it's not removed.
                If Obj_Scene(c, cc) = SceneACTV Then                                            'Checks to see if this object belong to this Scene
                    If JSprA(Obj_Spr(c)).Seq = 1 Then                                                'See if the current object is marked as animation
                        'Each object has an sprite. Obj_Spr(c) holds their sprite ID. Animate and draw object
                        JSprA(Obj_Spr(c)).AnimateS Me.hDC, Obj_X(c, cc), Obj_Y(c, cc)
                    ElseIf JSprA(Obj_Spr(c)).Seq = 0 Then                                                         'If not an animation
                        JSprA(Obj_Spr(c)).TDraw Me.hDC, Obj_X(c, cc), Obj_Y(c, cc), 0, 0, JSprA(Obj_Spr(c)).TransparentColor             'Simply just draw it.
                    End If
                End If
            End If
        Next
    Next
    '.....................

  3. #3

    Thread Starter
    Lively Member kivisoft@'s Avatar
    Join Date
    Jul 2011
    Location
    Iran
    Posts
    96

    Re: Need a piece of code optimized for my need!

    Obj_ZDepth array is used to determine the order which the objects should be drawn. I used bubble sort to sort this array. for example

    Obj_ZDepth(1) = -50 which means Object1 has a ZDepth of -50 and so on.
    Obj_ZDepth(2) = -100
    Obj_ZDepth(3) = 1
    Obj_ZDepth(4) = 20
    Obj_ZDepth(5) = 10

    The bubble sort, will then sort them from lowest to highest:

    Obj_ZDepth(1) = -100 (2)
    Obj_ZDepth(2) = -50 (1)
    Obj_ZDepth(3) = 1 (3)
    Obj_ZDepth(4) = 10 (5)
    Obj_ZDepth(5) = 20 (4)

    now I Also want the list of my objects to be ordered in the same way. I want the objects to be drawn in the order shown in front of each value.

  4. #4
    Computer Science BS Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: Need a piece of code optimized for my need!

    Like I said in the PM, if its a top view RPG such as Legend of Zelda, then you don't need to sort them. I would have to see a screenshot of your game to really see if you need one or not

    Not having the bubble sort would be a huge optimization since it can get pretty intensive.

  5. #5

    Thread Starter
    Lively Member kivisoft@'s Avatar
    Join Date
    Jul 2011
    Location
    Iran
    Posts
    96

    Re: Need a piece of code optimized for my need!

    OK! I quickly Wrapped up this very simple demo to better explain the situation

    Name:  Capture1.PNG
Views: 350
Size:  260.6 KB

    Do you see that little Adventurer behind the house? this adds a lot of allusion of depth to the game. in a game like this, this is usually what you want to happen when the characters go behind something like that house.

    Name:  Capture2.PNG
Views: 540
Size:  252.2 KB


    When you bring that character in front of the house you definitely want it to appear in the front but because the sprite of the character is drawn first in the objects list then in each frame it'll appear behind the house.

    The theory is to sort the array of objects based on another array (Obj_ZDepth) from lowest ZDepth to highest. this adds a lot of power because then you can assign the objects ZDepth to their Y position and sort them based on their Y position. as the values are sorted from lowest to highest, if an object has lower Y than another, then it will appear behind that object and if it has more Y then it will appear on top of the object.

    This is my problem. I can sort them using bubble sort, but that is just for the Obj_Zdepth array and the objects array is not effected by that at all. my question is how to associate both of them. really don't know how to explain that anymore!!!!!

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Need a piece of code optimized for my need!

    Why are you using separate arrays?

    It would probably make much more sense to use a Type (or possibly class) which contains all the information for the object (Z, sprite, presumably X and Y, ...), and have an array of that (which you can sort by any of the properties you like).

  7. #7

    Thread Starter
    Lively Member kivisoft@'s Avatar
    Join Date
    Jul 2011
    Location
    Iran
    Posts
    96

    Re: Need a piece of code optimized for my need!

    JSprA is a Class I wrote for my objects. it handles most of the drawing functions and has properties like Transparent Color , Animation, ....... . But I decided to Store information like X, Y,Z, Sprite, ...... in arrays for performance sakes. besides, this is not a Game! this is a Game Engine (Jupiter) I abandoned the project for two years and when I started the project again, I had all this cool new Ideas to add to it but most of it require me to change a lot of the code or even rewrite it (The code is a mess! it was written 2 years ago!), Which is why using Types are not an option.

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Need a piece of code optimized for my need!

    I don't see how separate arrays (as opposed to one array of a Type) could be better for performance - particularly now that you need to sort them in unison (that alone will probably more than offset any other gains).

    I suspect using the class for those extra properties would be slightly slower, but that depends on how frequently sorting happens... the more frequent it is, the more likely that storing the data inside the class would be best (as you would only need to move one pointer, rather than a chunk of data). Even if the class is slower, I'm not sure whether the difference would be enough to be worth the extra hassle of keeping things separated.


    If you can't be bothered to alter the existing code (which could be understandable if there is already lots of code using those arrays), you will need to modify the sorting so that it re-positions items in all of the relevant arrays at the same time (deciding when to swap based on the data in just one).


    If speed is a concern, I strongly recommend optimising the code you showed in post #1... there are several unoptimised things there that slow it down (such as the checks for .Seq inside the CC loop when they depend on C, and the repeated use of JSprA(Obj_Spr(c)) in the inner loop [which again only relies on C]).

  9. #9

    Thread Starter
    Lively Member kivisoft@'s Avatar
    Join Date
    Jul 2011
    Location
    Iran
    Posts
    96

    Re: Need a piece of code optimized for my need!

    I considered using a different array which holds The IDs of each object.

    Obj_ZDraw(1) = 1
    Obj_Zdraw(2) = 6
    Obj_Zdraw(3) = 9

    Which tells to draw the objects in this order (1, 6, 9)

    I tried to handle that in the sort sub itself and there were some minor success but not so much.

    Code:
    Public Sub bs(ByRef iaArray() As Long)
    
        Dim n As Long, i As Long, j As Long
    
        n = UBound(iaArray)
    
        For i = (n - 1) To 0 Step -1
    
            For j = 1 To i
    
                If iaArray(j) > iaArray(j + 1) Then
                    Swap iaArray(j), iaArray(j + 1)
    
                    Obj_Zdraw(j) = j + 1
                    Obj_Zdraw(j+ 1) = j
    
                End If
    
            Next j
    
        Next i
    
    End Sub
    And instead of passing c to JSprA(Obj_Spr(c)) You pass JSprA(Obj_Spr(Obj_Zdraw(c))

    But the too lines of code in the Sort sub which sort the Obj_Zdraw is not correctly sorted.

  10. #10
    Computer Science BS Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: Need a piece of code optimized for my need!

    I have a different approach. Draw all the buildings before you draw the sprites. You dont need a Z order at all in this case and its a waste trying to implement it unless your going for 2.5D, but theres no point of your sprite being behind the building. Just have the entire building be like a giant wall barrier they cant pass unless they enter the door. And i dont mean a rectangle or square. In my rigid body collision example it can be any shape for collision. You can find it in my sig.

  11. #11

    Thread Starter
    Lively Member kivisoft@'s Avatar
    Join Date
    Jul 2011
    Location
    Iran
    Posts
    96

    Re: Need a piece of code optimized for my need!

    I've checked your rigid body collision detection, I'm definitely gonna add it to the engine but as I mentioned in the pm, the engine could be used for all kinds of games (Strategy, RPG, Arcade, .......) But what your suggesting is not appropriate for strategy games.
    Code is the Void

  12. #12
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Need a piece of code optimized for my need!

    That is debatable, but your choice.


    Think about this section of your code:
    Quote Originally Posted by kivisoft@ View Post
    Code:
                    Swap iaArray(j), iaArray(j + 1)
    
                    Obj_Zdraw(j) = j + 1
                    Obj_Zdraw(j+ 1) = j
    You should be swapping the items in Obj_Zdraw, not setting the values to 'random' things.

  13. #13

    Thread Starter
    Lively Member kivisoft@'s Avatar
    Join Date
    Jul 2011
    Location
    Iran
    Posts
    96

    Re: Need a piece of code optimized for my need!

    Quote Originally Posted by si_the_geek View Post
    That is debatable, but your choice.


    Think about this section of your code:
    You should be swapping the items in Obj_Zdraw, not setting the values to 'random' things.
    This is not random! works but the last objects is not drawn! also does not work when you change the Z_Depth in Run-Time

    Obj_Zdraw() contain Index numbers of objects, so I thought in the process of sorting the Z_Depth, if for example Z_Depth(j) is higher than Z_Depth(j +1) , then you have to switch places, now in the process, save their index numbers (j) in the Obj_ZDraw which then you pass them to JSprA (JSprA(Obj_Zdraw(c)))
    Code is the Void

  14. #14
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Need a piece of code optimized for my need!

    You are not saving their index numbers, you are saving the positions involved during a swap operation.

    I've had a quick think about what it does, and I can't fully predict what that will do (which is unusual)... and as it isn't working properly, it is safe to assume that you don't fully understand it either.


    What I would recommend is getting rid of the Obj_Zdraw array, and inside the bs routine sort the array(s) you actually need - swapping the items at the same time as you swap items in the other array.

  15. #15

    Thread Starter
    Lively Member kivisoft@'s Avatar
    Join Date
    Jul 2011
    Location
    Iran
    Posts
    96

    Re: Need a piece of code optimized for my need!

    Success! I ran a benchmark for 20 to 50 sprites and it ran smoothly, but when it comes to 100 and higher, it gets shaky! hints on how to increase performance is welcome. thank you!

    Code:
    'in the main loop
    bs Obj_ZDepth, c
    
    Public Sub bs(ByRef iaArray() As Long, ByVal cIndex As Long)
    
        Dim n As Long, i As Long, j As Long
    
        n = UBound(iaArray)
    
        For i = (n - 1) To 0 Step -1
    
            For j = 1 To i
    
                If iaArray(j) < iaArray(j + 1) Then
                    Swap iaArray(j), iaArray(j + 1)
    
                    Obj_ZDraw(cIndex) = j + 1
                    Obj_ZDraw(cIndex + 1) = j
                    
                End If
    
            Next j
    
        Next i
    
    End Sub
    Code is the Void

  16. #16
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Need a piece of code optimized for my need!

    I've already given several suggestions.

    In addition to that, don't sort items more often than you need to (because sorting is slow), and make sure that you are using an appropriate sorting method for the type of data you are most likely to have (because a 'bad' sorting method can be 100 times slower than a 'good' one).

    In terms of what you posted, Swap should not be a separate routine (because extra time is used to call it), it should be put in-line instead.




    Note that this code needs thinking about:
    Code:
        For i = (n - 1) To 0 Step -1
            For j = 1 To i
    (hint: what happens when i is 0?)

Posting Permissions

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



Click Here to Expand Forum to Full Width