Android Question libGDX - RequestRendering

jtare

Active Member
Licensed User
Longtime User
In my project I have 2 activities in which both have an libGDX view, one in my main activity and the other one in a secondary activity which is not always in use (only if user wants), the problem I encounter is that when I come back from the secondary activity to the main activity the
B4X:
lGdx.Graphics.RequestRendering
does not work, it seems that the secondary activity is messing up something, if I change to any activity without an libGDX view and the come back to my main activity, this issue does not occur.

The request rendering is triggered by a button.

Some code from the main activity(my code is very large to paste it all here):
B4X:
Sub Activity_Resume
    If lGdx.IsInitialized  Then lGdx.Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If lGdx.IsInitialized Then lGdx.Pause
End Sub

B4X:
Sub LG_Create
    ShRdr.Initialize
    GD.Initialize("GD")
    Camera.Initialize
    Camera.SetToOrtho2(False,lGdx.Graphics.Width,lGdx.Graphics.Height)
    Camera.Position.set(0, 0, 1)

    If Not(CameraGroupStrategy.IsInitialized) Then CameraGroupStrategy.Initialize(Camera)
    If Not(Batch.IsInitialized) Then Batch.Initialize(CameraGroupStrategy)
    'Creates the five decals
    Tex.Initialize("image1.png")
        Tex.SetFilter(Tex.FILTER_Linear, Tex.FILTER_Linear)
        TR.InitializeWithTexture(Tex)
        Decal.Initialize2(0.6, 1.7, TR, True)
        lGdx.Graphics.ContinuousRendering = False
End Sub

B4X:
Sub LG_Dispose
    ShRdr.dispose
    Batch.dispose
    CameraGroupStrategy.dispose
    If Tex.IsInitialized Then Tex.dispose
End Sub

The code for the secondary activity is almost the same, I only changed the variables names (thought this was messing up something).
Is there a reason why this happen?
 
Last edited:

Informatix

Expert
Licensed User
Longtime User
There's not enough code to understand what could be wrong. I see no RequestRendering above.
Anyway, I don't recommend to use different activities as the libGDX view should be unique in the app. You should try to use screens instead (you can put the screen code in another module if you want).
 
Upvote 0

jtare

Active Member
Licensed User
Longtime User
There's not enough code to understand what could be wrong. I see no RequestRendering above.
Anyway, I don't recommend to use different activities as the libGDX view should be unique in the app. You should try to use screens instead (you can put the screen code in another module if you want).

The RequestRendering is triggered by a button
B4X:
Sub button_Click
lGdx.Graphics.RequestRendering
End Sub

In LG_Render I have

B4X:
Sub LG_Render
    GL.glClearColor(0.98,0.98,0.98,0)
    GL.glClear(Bit.Or(GL.GL10_COLOR_BUFFER_BIT, GL.GL10_DEPTH_BUFFER_BIT))
    Log("Update")
    Camera.Update
    ShRdr.ProjectionMatrix = Camera.Combined
    GL.glLineWidth(8)
    ShRdr.Begin(ShRdr.SHAPETYPE_Line)
        ShRdr.Color = Colour.BLACK
        ShRdr.Line(-5,10,-5,5)   
    ShRdr.End

    ShRdr.Begin(ShRdr.SHAPETYPE_Line)
        ShRdr.Color = Colour.BLACK
        ShRdr.Line(5,10,5,5)
    ShRdr.End

    ShRdr.Begin(ShRdr.SHAPETYPE_Line)
        ShRdr.Color = Colour.DARK_GRAY
        ShRdr.Line(-30,0,30,0)
    ShRdr.End

        Decal.Position.set(0,0,0)
        Decal.Height = 1.7
        Decal.Width = 0.6
        Batch.Add(Decal)
    Batch.Flush
End Sub

That everything related with lgGDX view, also in my main activity the view does not cover the entire screen, only 100%x and 40%y from the top. And in my secondary activity the libGDX view covers all the screen.

I will try to use only one activity and change the height of the libGDX view to fill the screen, I dont know if its possible to change from lgOrthographicCamera to lgPerspectiveCamera and viceversa, I dont use the same settings for the 2 activities, they render different things, I will check out what you suggested and use screens.

When ContinuousRendering is enable this problem does not occur.
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
I was able to reproduce your case and, for now, I have no solution to offer. As some classes (or fields) are static, when you modify a property of an instance of this class, all instances get the same value, and a perfect example is the lGdx.Graphics class. This is why we should avoid having different libGDX views in the same app.

You can change your camera type or your view dimensions without issue.
 
Upvote 0

jtare

Active Member
Licensed User
Longtime User
I did what you suggested (screens) and it worked, not completely, but after a few changes it worked.
Changing between screens with LGSM.CurrentScreen = LG_Screen(ScreenN) didn't worked for me, I have more views in the activity and appcompat and these messed up after changing screens( dont know why), luckly for me, the screen change happens when the device is rotated and this trigger the Activity_Create so I can hide somes views and everything works perfectly.
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Changing between screens with LGSM.CurrentScreen = LG_Screen(ScreenN) didn't worked for me
There's no other way to change the current screen, so I wonder how you do.

I have more views in the activity and appcompat and these messed up after changing screens( dont know why)
I can't see the relationship. When you use screens, you just redirect events (e.g. YourScreen_Show is called instead of LG_Create; that's all). More generally, libGDX cannot affect your other views.
 
Upvote 0

jtare

Active Member
Licensed User
Longtime User
There's no other way to change the current screen, so I wonder how you do.

To change the screen first I change the orientation of the device, the second screen is always displayed with the device in landscape orientation.
B4X:
Sub button2_Click
    Dim p As Phone
    LastScreen = 1
    p.SetScreenOrientation(0)
End Sub
Then when the device is rotated the Activity_Create is triggered again and I use the lastscreen value to decide the height of the libGDX view and decide if the scrollview will be displayed or not.
B4X:
Sub Activity_Create(FirstTime As Boolean)
....
Dim V As View = lGdx.InitializeView("LG")
If LastScreen = 0 Then
        Activity.AddView(V, 0, 0, 100%x, 40%y)
Else If LastScreen = 1 Then
        Activity.AddView(V, 0, 0, 100%x, 100%y)
End If
If LastScreen = 0 Then Activity.AddView(ScrollView1,0,40%y,100%x,60%y)
....
End Sub
Then the LG_Create is triggered and it will load the "lastscreen"
B4X:
Sub LG_Create
    LGSM.Initialize(lGdx)
    LG_Screen(0) = LGSM.AddScreen("LGS1")
    LG_Screen(1) = LGSM.AddScreen("LGS2")
    LGSM.CurrentScreen = LG_Screen(LastScreen)
End Sub
This is how it worked for me. Then to return to the first screen I do the same, except I pass 0 as a value for LastScreen instead of 1 before rotate the screen to portrait.
Like I said before the LGSM.CUrrentScreen = LG_Screen(1) didn't work for me I meant that if I use LGSM.CUrrentScreen = LG_Screen(1) inside the button2_Click the change does not happen, it turns all the views black, including the action bar and the libgdx display nothing.
 
Last edited:
Upvote 0

Informatix

Expert
Licensed User
Longtime User
To change the screen first I change the orientation of the device, the second screen is always displayed with the device in landscape orientation.
It's not really what I call a solution to change the current screen.

if I use LGSM.CUrrentScreen = LG_Screen(1) inside the button2_Click the change does not happen, it turns all the views black, including the action bar and the libgdx display nothing.
Trying to set the new screen from outside the libGDX thread cannot work. As a rule of thumb, it's not a good idea to mix standard views and libGDX because their code is not run in the same thread. A possible solution is to set a boolean and call RequestRendering when you click on the button, and read this boolean in the Render event of libGDX to activate the screen change.
 
Upvote 0
Top