Hi guys!
Here's a minimalistic libGDX template which can be useful when starting a new project.
It includes a background, a title and an array of objects. It does not include any animation.
The code should be pretty simple to understand, but if you're not familiar with the libGDX lifecycle, you should read Informatix's tutorial first.
DOWNLOAD PROJECT
DOWNLOAD libGDX
In this example, the camera coordinates are vertically flipped, so that the x,y origin is at the top left corner. If you follow this method, whenever you set a new texture region, you'll have to flip it as well, otherwise it will appear upside-down.
To keep things tidy, I've created a custom type of variable, the GDXObject2D type.
It contains the following properties:
Source code:
Here's a minimalistic libGDX template which can be useful when starting a new project.
It includes a background, a title and an array of objects. It does not include any animation.
The code should be pretty simple to understand, but if you're not familiar with the libGDX lifecycle, you should read Informatix's tutorial first.
DOWNLOAD PROJECT
DOWNLOAD libGDX
In this example, the camera coordinates are vertically flipped, so that the x,y origin is at the top left corner. If you follow this method, whenever you set a new texture region, you'll have to flip it as well, otherwise it will appear upside-down.
To keep things tidy, I've created a custom type of variable, the GDXObject2D type.
It contains the following properties:
Texture_File: A string containing the file name.
X: Location on screen.
Y: Location on screen.
Source_Pixel_Size_X: Image file size in pixels.
Source_Pixel_Size_Y: Image file size in pixels.
On_Screen_Size_X: Sprite output size on screen.
On_Screen_Size_Y: Sprite output size on screen.
Texture_Filter: Yes or No.
I've also added a dual-analog control scheme, which is composed by two transparent panels, each covering one half of the screen. The left analog controls Actor 0 and the right analog controls Actor 1.X: Location on screen.
Y: Location on screen.
Source_Pixel_Size_X: Image file size in pixels.
Source_Pixel_Size_Y: Image file size in pixels.
On_Screen_Size_X: Sprite output size on screen.
On_Screen_Size_Y: Sprite output size on screen.
Texture_Filter: Yes or No.
Source code:
B4X:
#Region Project Attributes
#ApplicationLabel: LibGDX Template
#VersionCode: 1
#VersionName: 1
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: Landscape
#CanInstallToExternalStorage: True
#AdditionalRes: C:\Android\android-sdk\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms
#End Region
#Region Activity Attributes
#FullScreen: True
#IncludeTitle: False
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Type GDXObject2D(Texture_File As String, X As Float, Y As Float, Source_Pixel_Size_X As Float, Source_Pixel_Size_Y As Float, On_Screen_Size_X As Float, On_Screen_Size_Y As Float, Texture_Filter As Boolean)
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
'LibGDX Setup
Dim lGdx As LibGDX
Dim Gl As lgGL
Dim Camera As lgOrthographicCamera
Dim Batch As lgSpriteBatch
Dim GLView As View
Dim lgStep As Float
'LibGDX Objects
Dim background_setup As GDXObject2D
Dim background_source As lgTexture
Dim background As lgTextureRegion
Dim title_setup As GDXObject2D
Dim title_source As lgTexture
Dim title As lgTextureRegion
Dim actor_setup(6) As GDXObject2D
Dim actor_source(6) As lgTexture
Dim actor(6) As lgTextureRegion
background_setup.texture_file = "background.png"
background_setup.texture_filter = True
background_setup.source_pixel_size_x = 1280
background_setup.source_pixel_size_y = 720
background_setup.on_screen_size_x = 100%x
background_setup.on_screen_size_y = 100%y
background_setup.x = 0%x
background_setup.y = 0%y
title_setup.texture_file = "title.png"
title_setup.texture_filter = True
title_setup.source_pixel_size_x = 400
title_setup.source_pixel_size_y = 200
title_setup.on_screen_size_x = 30%x
title_setup.on_screen_size_y = Same_Proportion_as_X(title_setup)
title_setup.x = Screen_Center_X(title_setup)
title_setup.y = 8%y
For i = 0 To (actor.Length - 1)
actor_setup(i).texture_file = "actor_" & i & ".png"
actor_setup(i).texture_filter = True
actor_setup(i).source_pixel_size_x = 200
actor_setup(i).source_pixel_size_y = 200
actor_setup(i).on_screen_size_x = 10%x
actor_setup(i).on_screen_size_y = Same_Proportion_as_X(actor_setup(i))
actor_setup(i).x = Anywhere_On_Screen_X(actor_setup(i))
actor_setup(i).y = Anywhere_On_Screen_Y(actor_setup(i))
Next
'Setup Physics
'Setup Touch Controls
Private Analog_Left As Panel
Dim Analog_Left_StartX As Float
Dim Analog_Left_StartY As Float
Dim Analog_Left_DeltaX As Float
Dim Analog_Left_DeltaY As Float
Dim Analog_Left_Active As Boolean
Dim Analog_Left_TouchRatio = 1.8 As Float
Private Analog_Right As Panel
Dim Analog_Right_StartX As Float
Dim Analog_Right_StartY As Float
Dim Analog_Right_DeltaX As Float
Dim Analog_Right_DeltaY As Float
Dim Analog_Right_Active As Boolean
Dim Analog_Right_TouchRatio = 1.8 As Float
'Debug
Private Debug As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Initialize Layout
Activity.LoadLayout("Main")
'Initialize libGDX
GLView = lGdx.InitializeView("LG")
Activity.AddView(GLView, 0%x, 0%y, 100%x, 100%y)
'Analog_Left
Analog_Left.Left = 0%x
Analog_Left.Top = 0%y
Analog_Left.Width = 50%x
Analog_Left.Height = 100%y
'Analog_Right
Analog_Right.Left = 50%x
Analog_Right.Top = 0%y
Analog_Right.Width = 50%x
Analog_Right.Height = 100%y
'Debug
Debug.Left = 0%x
Debug.Top = 0%x
Debug.Width = 100%x
Debug.Height = 100%y
'BTFs
Analog_Left.BringToFront
Analog_Right.BringToFront
Debug.BringToFront
End Sub
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
Sub LG_Create
Batch.Initialize
'Initialize Object Textures
background_source.Initialize(background_setup.Texture_File)
If background_setup.Texture_Filter = True Then background_source.SetFilter(background_source.FILTER_Linear, background_source.FILTER_Linear)
title_source.Initialize(title_setup.Texture_File)
If title_setup.Texture_Filter = True Then title_source.SetFilter(title_source.FILTER_Linear, title_source.FILTER_Linear)
For i = 0 To actor.Length - 1
actor_source(i).Initialize(actor_setup(i).Texture_File)
If actor_setup(i).Texture_Filter = True Then actor_source(i).SetFilter(actor_source(i).FILTER_Linear, actor_source(i).FILTER_Linear)
Next
'Initialize Objects
background.InitializeWithTexture(background_source)
background.Flip(False, True)
title.InitializeWithTexture(title_source)
title.Flip(False, True)
For i = 0 To (actor.Length - 1)
actor(i).InitializeWithTexture(actor_source(i))
actor(i).Flip(False, True)
Next
End Sub
Sub LG_Resize(Width As Int, Height As Int)
'Sets the camera viewport
Camera.Initialize
Camera.SetToOrtho2(True, lGdx.graphics.Width, lGdx.Graphics.Height)
End Sub
Sub LG_Render
'Clears the screen
Gl.glClear(Gl.GL10_COLOR_BUFFER_BIT)
'Updates the matrices of the camera
Camera.Update
'Uses the coordinate system specified by the camera
Batch.ProjectionMatrix = Camera.Combined
'Step Setup
If lGdx.Graphics.DeltaTime > (1/60) AND lGdx.Graphics.DeltaTime <= (1/30) Then
lgStep = lGdx.Graphics.DeltaTime
Else If lGdx.Graphics.DeltaTime <= (1/60) Then
lgStep = (1/60)
Else If lGdx.Graphics.DeltaTime > (1/30) Then
lgStep = (1/30)
End If
'Display Objects on Screen
Batch.Begin
Batch.DrawRegion2(background, background_setup.X, background_setup.Y, background_setup.On_Screen_Size_X, background_setup.On_Screen_Size_Y)
For i = 0 To (actor.Length - 1)
Batch.DrawRegion2(actor(i), actor_setup(i).X, actor_setup(i).Y, actor_setup(i).On_Screen_Size_X, actor_setup(i).On_Screen_Size_Y)
Next
Batch.DrawRegion2(title, title_setup.X, title_setup.Y, title_setup.On_Screen_Size_X, title_setup.On_Screen_Size_Y)
Batch.End
'Call the Auxiliary Engine
lGdx.CallSubUI("Aux_Engine", Null)
End Sub
Sub Aux_Engine(param As Object)
'Debug
Dim centerX0 = Round(actor_setup(0).X + (actor_setup(0).On_Screen_Size_X / 2)) As Float
Dim centerY0 = Round(actor_setup(0).Y + (actor_setup(0).On_Screen_Size_Y / 2)) As Float
Dim centerX1 = Round(actor_setup(1).X + (actor_setup(1).On_Screen_Size_X / 2)) As Float
Dim centerY1 = Round(actor_setup(1).Y + (actor_setup(1).On_Screen_Size_Y / 2)) As Float
Debug.text = "LibGDX Speed: " & lGdx.Graphics.FramesPerSecond & " FPS / " & Round2(1 / lGdx.Graphics.DeltaTime, 2) & " Hz" & CRLF & _
"Actor 0: " & centerX0 & ", " & centerY0 & CRLF & _
"Actor 1: " & centerX1 & ", " & centerY1
End Sub
Sub LG_Dispose
background_source.dispose
title_source.dispose
For i = 0 To (actor.Length - 1)
actor_source(i).dispose
Next
End Sub
Sub LG_Pause
End Sub
Sub LG_Resume
End Sub
Sub Analog_Left_Touch (Action As Int, X As Float, Y As Float)
Select Action
Case Activity.ACTION_DOWN
Analog_Left_StartX = X
Analog_Left_StartY = Y
Analog_Left_Active = True
Case Activity.ACTION_MOVE
Analog_Left_DeltaX = X - Analog_Left_StartX
Analog_Left_DeltaY = Y - Analog_Left_StartY
Analog_Left_StartX = X
Analog_Left_StartY = Y
actor_setup(0).X = actor_setup(0).X + (Analog_Left_DeltaX * Analog_Left_TouchRatio)
actor_setup(0).Y = actor_setup(0).Y + (Analog_Left_DeltaY * Analog_Left_TouchRatio)
Case Activity.ACTION_UP
Analog_Left_DeltaX = 0
Analog_Left_DeltaY = 0
Analog_Left_Active = False
End Select
End Sub
Sub Analog_Right_Touch (Action As Int, X As Float, Y As Float)
Select Action
Case Activity.ACTION_DOWN
Analog_Right_StartX = X
Analog_Right_StartY = Y
Analog_Right_Active = True
Case Activity.ACTION_MOVE
Analog_Right_DeltaX = X - Analog_Right_StartX
Analog_Right_DeltaY = Y - Analog_Right_StartY
Analog_Right_StartX = X
Analog_Right_StartY = Y
actor_setup(1).X = actor_setup(1).X + (Analog_Right_DeltaX * Analog_Right_TouchRatio)
actor_setup(1).Y = actor_setup(1).Y + (Analog_Right_DeltaY * Analog_Right_TouchRatio)
Case Activity.ACTION_UP
Analog_Right_DeltaX = 0
Analog_Right_DeltaY = 0
Analog_Right_Active = False
End Select
End Sub
Sub Same_Proportion_as_X(object_name As GDXObject2D) As Float
Return (object_name.source_pixel_size_y * object_name.on_screen_size_x) / object_name.source_pixel_size_x
End Sub
Sub Same_Proportion_as_Y(object_name As GDXObject2D) As Float
Return (object_name.source_pixel_size_x * object_name.on_screen_size_y) / object_name.source_pixel_size_y
End Sub
Sub Screen_Center_X(object_name As GDXObject2D) As Float
Return 50%x - (object_name.on_screen_size_x / 2)
End Sub
Sub Screen_Center_Y(object_name As GDXObject2D) As Float
Return 50%y - (object_name.on_screen_size_y / 2)
End Sub
Sub Anywhere_On_Screen_X(object_name As GDXObject2D) As Float
Dim minimum = Round(object_name.on_screen_size_x) As Int
Dim maximum = Round(100%x - object_name.on_screen_size_x) + 1 As Int
Return Rnd(minimum, maximum)
End Sub
Sub Anywhere_On_Screen_Y(object_name As GDXObject2D) As Float
Dim minimum = Round(object_name.on_screen_size_y) As Int
Dim maximum = Round(100%y - object_name.on_screen_size_y) + 1 As Int
Return Rnd(minimum, maximum)
End Sub
Last edited: