Games how to draw a line?

Gunther

Active Member
Licensed User
Longtime User
Adding this in the game tick doesn't do the job:

B4X:
X2.MainBC.DrawLine(revjoint.BodyA.Position.X,revjoint.BodyA.Position.Y, revjoint.BodyB.Position.X,revjoint.BodyB.Position.Y, 1, 2)
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
I'll soon release a major update to BitmapCreator that will offer more and better options. Stay tuned.

Testing it as we speak...

test.gif
 

Gunther

Active Member
Licensed User
Longtime User
Unfortunately I am not getting a line drawn with BC 4.5

I put this in the Tick of the Game:

B4X:
        Dim bc As BitmapCreator
        bc.Initialize(X2.MainBC.mWidth , X2.MainBC.mHeight)
        bc.DrawCircle(100dip, 100dip, 50dip, xui.Color_Red, True, 10dip)
        bc.DrawLine(0, 0, bc.mWidth, bc.mHeight, 0xff0000ff, 5dip)
        X2.MainBC.DrawBitmap(bc.Bitmap,X2.MainBC.TargetRect,True)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I put this in the Tick of the Game:
Your code will completely kill the game performance.

1. You don't need to create a new BitmapCreator for the drawings. This cannot work inside the Ticks event.
2. You should never use 'dip' units in XUI2D.
3. The async methods work with brushes. It will not create the brush for you as it is a mistake to create a new brush every tick.


Correct code:
B4X:
'BrushBackwards is a global variable.
'Created like this: BrushBackwards = X2.MainBC.CreateBrushFromColor(xui.Color_Red)
Public Sub Tick (GS As X2GameStep)
   If GS.ShouldDraw Then
       GS.DrawingTasks.Add(X2.MainBC.AsyncDrawCircle(100, 100, 50, BrushBackwards, True, 10))
       GS.DrawingTasks.Add(X2.MainBC.AsyncDrawLine(0, 0, X2.MainBC.mWidth, X2.MainBC.mHeight, BrushBackwards, 5))
   End If
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
BTW, if you want to make static drawings that are not being updated too frequently then you can add another layer (another ImageView) and draw over this layer. This way you will not need to redraw everything each step.
Similar to the "create static background" that is inside many of the example.

For drawings that are related to the world bodies then it is better to use the async approach and draw directly to MainBC.
 

Gunther

Active Member
Licensed User
Longtime User
Dear Erel,

thanks for your hints and information. I already thought about async drawings but I could it done only after I checked throu your MouseFollowing Example.

Actually it is the same line as it is drawn in DebugMode between Joints but permanently also in release mode.

B4X:
    Public Sub Draw(GS As X2GameStep)
    '
    Dim p1 As B2Vec2 = X2.WorldPointToMainBC(revjoint.AnchorA.X, revjoint.AnchorA.Y)
    Dim p2 As B2Vec2 = X2.WorldPointToMainBC(revjoint.AnchorB.X, revjoint.AnchorB.Y)
   
    GS.DrawingTasks.Add(X2.MainBC.AsyncDrawLine(p2.X, p2.Y, p1.X, p1.Y,  BrushLineColor,  0.2))
    '
End Sub
 
Last edited:

Gunther

Active Member
Licensed User
Longtime User
Sure!!

It was only to tell what is the aim to archive with this line on the MainBC.

Thanks
 
Last edited:
Top