Games [XUI2D] [solved] AsyncDrawTasks and BCPath

Gunther

Active Member
Licensed User
Longtime User
Hi,

we can add a async drawing task with adding a path and than adding the final path as clone to the task list with:
B4X:
myPath.Initialize(p1.X, p1.Y)
myPath.LineTo(p2.X,p2.Y).LineTo(p3.X,p3.Y).LineTo(p1.X, p1.Y)

GS.DrawingTasks.Add(X2.MainBC.AsyncDrawPath(myPath.Clone, BrushLineColor, True, 1))

Well, how we can be sure that the path can be safely reused (and so its clone) or the drawtask is finished?

Can we remove that draw task from the draw task list(= that special one, do we have a reference to the added the task to be found in the list?) assuming that there will be some more draw tasks on that list added?

Thanks.
 

Gunther

Active Member
Licensed User
Longtime User
Thanks, a newly cloned path is NOT overwriting the path clone of the previous one, correct?

Since I do have about 40 paths in the loop of 5 paths each loop and sometimes I am getting a null pointer exception at different times in the game play.
 

Gunther

Active Member
Licensed User
Longtime User
Ok than it is an diffrent reason for the exception.
 

Gunther

Active Member
Licensed User
Longtime User
They are modified every loop.

the non-optimized code in question is:

B4X:
Private Sub Drawitems(GS As X2GameStep, mbw As X2BodyWrapper)
 
    If mbw=Null Or mbw.IsVisible=False Then Return

    Dim Rhombus, Rhombus2, Rhombus3, Rhombus4, Rhombus5  As BCPath
    Dim p5, p6, p7, p8 As B2Vec2
 
    Dim f As B2Fixture = mbw.Body.FirstFixture

    Dim HalbBreite As Float = mbw.X2.GetShapeWidthAndHeight(f.Shape).X/2.
    Dim HalbHohe As Float =  mbw.X2.GetShapeWidthAndHeight(f.Shape).Y/2.
 
    Dim p1 As B2Vec2 = X2.WorldPointToMainBC(mbw.Body.Position.X-HalbBreite, mbw.Body.Position.Y-HalbHohe)
    Dim p2 As B2Vec2 = X2.WorldPointToMainBC(mbw.Body.Position.X+HalbBreite, mbw.Body.Position.Y-HalbHohe)
    Dim p3 As B2Vec2 = X2.WorldPointToMainBC(mbw.Body.Position.X+HalbBreite, mbw.Body.Position.Y+HalbHohe)
    Dim p4 As B2Vec2 = X2.WorldPointToMainBC(mbw.Body.Position.X-HalbBreite, mbw.Body.Position.Y+HalbHohe)
    '
    '
    'Punkte definition:

    ' calculating a small offset
    Punkte(p1, p5)
    Punkte(p2, p6)
    Punkte(p3, p7)
    Punkte(p4, p8)

    ' top
    If p3.Y > Click.Y Then
        Rhombus.Initialize(p3.X, p3.Y).LineTo(p7.X, p7.Y).LineTo(p8.X,p8.Y).LineTo(p4.X,p4.Y)
        GS.DrawingTasks.Add(X2.MainBC.AsyncDrawPath(Rhombus.Clone, BrushLineColor1, True, 0))
    
    End If
    ' bottom
    If p1.Y < Click.Y Then
        Rhombus2.Initialize(p2.X, p2.Y).LineTo(p6.X, p6.Y).LineTo(p5.X,p5.Y).LineTo(p1.X,p1.Y)
        GS.DrawingTasks.Add(X2.MainBC.AsyncDrawPath(Rhombus2.Clone, BrushLineColor2, True, 0))
    End If
    ' left
    If p1.X > Click.X Then
        Rhombus3.Initialize(p4.X, p4.Y).LineTo(p8.X, p8.Y).LineTo(p5.X,p5.Y).LineTo(p1.X,p1.Y)
        GS.DrawingTasks.Add(X2.MainBC.AsyncDrawPath(Rhombus3.Clone, BrushLineColor3, True, 0))
    End If
    ' right
    If p2.X < Click.X Then
        Rhombus4.Initialize(p2.X, p2.Y).LineTo(p6.X, p6.Y).LineTo(p7.X,p7.Y).LineTo(p3.X,p3.Y)
        GS.DrawingTasks.Add(X2.MainBC.AsyncDrawPath(Rhombus4.Clone, BrushLineColor4, True, 0))
    End If
'    ' back
'    Rhombus5.Initialize(p1.X, p1.Y).LineTo(p2.X, p2.Y).LineTo(p3.X,p3.Y).LineTo(p4.X,p4.Y)
'    GS.DrawingTasks.Add(X2.MainBC.AsyncDrawPath(Rhombus5.Clone, BrushLineColor5, True, 0))
 
 
End Sub

And the error log:

B4X:
0
0
0
1
2
3
5
6
7
8
10
12
13
14
java.lang.NegativeArraySizeException
    at b4j.example.bitmapcreator._createcolumn(bitmapcreator.java:677)
    at b4j.example.bitmapcreator._drawfullpath(bitmapcreator.java:2012)
    at b4j.example.bitmapcreator._drawpath2(bitmapcreator.java:2187)
    at b4j.example.bitmapcreator._drawcanvasasync(bitmapcreator.java:1406)
    at b4j.example.bitmapcreator$1.run(bitmapcreator.java:4566)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
skipping frame!!!
skipping frame!!!
skipping frame!!!
skipping frame!!!
skipping frame!!!
skipping frame!!!
skipping frame!!!
 
Last edited:

Gunther

Active Member
Licensed User
Longtime User
it may be that is throuwing that error when the fixtures are outside the screen but only in Y-direction.

If they are outside the screen in X-direction but the Y-Position is in the range of the Min/Max plus a little more of the screen it works fine.

Could also be the hight of the TiledMap behind. If the total hight of the TiledMap is visible on the screen than it works (width of the Map is 6x the hight). If the visible hight smaller than the Map than it crashes.
 
Last edited:

Gunther

Active Member
Licensed User
Longtime User
let me try to shrink the bigger project (already) to the necessary ... here it is...

In the Initialize Procedure you may change the factor behind WorldHeight to a lower one (more zoomed in) this will create an NullPointer Exception.

B4X:
Public Sub Initialize (Parent As B4XView)
    Parent.LoadLayout("1")
    world.Initialize("world", world.CreateVec2(0, -20))
    X2.Initialize(Me, ivForeground, world)
    WorldHeight = 40 '
    WorldWidth  = WorldHeight * 1.3333
   
    X2.ConfigureDimensions(world.CreateVec2(WorldWidth / 2, WorldHeight / 2), WorldHeight * 1.16) '<------ lower Then 1.15 will produce the error
   
    lblStats.TextColor = xui.Color_Black
    lblStats.Color = 0x88ffffff
    lblStats.Font = xui.CreateDefaultBoldFont(20)
   
   
    'comment to disable debug drawing
'    X2.EnableDebugDraw
    TileMap.Initialize(X2, File.DirAssets, "tank.json", Null)
    TileMap.SetSingleTileDimensionsInMeters(WorldHeight / TileMap.TilesPerColumn, WorldHeight / TileMap.TilesPerColumn)
    TileMap.PrepareObjectsDef(ObjectLayer)
   
'    TileMap.SetBackgroundColor(0xFF008AFF)  ' <--------------------- first error when uncommented
   
    Dim ol As X2ObjectsLayer = TileMap.Layers.Get(ObjectLayer)
    For Each template As X2TileObjectTemplate In ol.ObjectsById.Values
        If template.Name <> "hinge" Then 
            Dim bw As X2BodyWrapper = TileMap.CreateObject(template)
            If bw.Name = "tank" Then 
                Tank = bw
            End If
        End If
    Next
   
   
    TankMovementForce = X2.CreateVec2(12,0)
   
    GroundLevel = X2.ScreenAABB.BottomLeft.Y 
   
    BrushLineColor1 = X2.MainBC.CreateBrushFromColor(xui.Color_LightGray)
   
    X2.Start
End Sub
 

Attachments

  • TestProject.zip
    53.6 KB · Views: 348
Last edited:

Gunther

Active Member
Licensed User
Longtime User
Unfortunately, the project logic asks for bodies with are outside and not visible. This bodies edge points are start or end points of the drawn lines.

Is this issue only related to lines which are 100% out of BC?

If the Y-Direction behavious like the X-Direction then it is fine.

So, I need to wait for the update.
 
Last edited:

Gunther

Active Member
Licensed User
Longtime User
Well, I think yes. It looks very wirred if the extruded drawing is disappering with the box going out to the left. Or the drawing is first drawn if the box is seen on the right.

2.7D.PNG
 

Gunther

Active Member
Licensed User
Longtime User
With the new X2 in the x-platform X2 b4x library this issue is solved.
 
Top