Starter Classes
A Starter Class defines the entry point (starting point) of a Libgdx application.
They are specifically written for a certain platform. Usually, these kinds of classes
are very simple and mostly consist of not more than a few lines of code to set
certain parameters that apply to the corresponding platform. Think of them as
a kind of boot-up sequence for each platform. Once booting has finished, the
Libgdx framework hands over control from the Starter Class (for example, the
demo-desktop project) to your shared application code (for example, the demo
project) by calling the different methods from the ApplicationListener interface
that the MyDemo class implements. Remember that the MyDemo class is where the
shared application code begins.
Running the demo application on a desktop
The Starter Class for the desktop application is called Main.java.
The following listing is Main.java from demo-desktop:
package com.packtpub.libgdx.demo;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
public class Main {
public static void main(String[] args) {
LwjglApplicationConfiguration cfg =
new LwjglApplicationConfiguration();
cfg.title = "demo";
cfg.useGL20 = false;
cfg.width = 480;
cfg.height = 320;
new LwjglApplication(new MyDemo(), cfg);
}
}
In the preceding code listing, you see the Main class, a plain Java class without
the need to implement an interface or inherit from another class. Instead, a new
instance of the LwjglApplication class is created. This class provides a couple
of overloaded constructors to choose from. Here, we pass a new instance of the
MyDemo class as the first argument to the constructor. Optionally, an instance of the
LwjglApplicationConfiguration class can be passed as the second argument.
The configuration class allows you to set every parameter that is configurable for
a Libgdx desktop application. In this case, the window title is set to demo and the
window's width and height is set to 480 by 320 pixels.
This is all you need to write and configure a Starter Class for a desktop.
Let us try to run the application now. To do this, right-click on the demo-desktop
project in Project Explorer in Eclipse and then navigate to Run As | Java Application.
Eclipse may ask you to select the Main class when you do this for the first time. Simply
select the Main class and also check that the correct package name (com.packtpub.
libgdx.demo) is displayed next to it.
The desktop application should now be up and running on your computer. If you
are working on Windows, you should see a window that looks as follows
this picture could change in different versions
A Starter Class defines the entry point (starting point) of a Libgdx application.
They are specifically written for a certain platform. Usually, these kinds of classes
are very simple and mostly consist of not more than a few lines of code to set
certain parameters that apply to the corresponding platform. Think of them as
a kind of boot-up sequence for each platform. Once booting has finished, the
Libgdx framework hands over control from the Starter Class (for example, the
demo-desktop project) to your shared application code (for example, the demo
project) by calling the different methods from the ApplicationListener interface
that the MyDemo class implements. Remember that the MyDemo class is where the
shared application code begins.
Running the demo application on a desktop
The Starter Class for the desktop application is called Main.java.
The following listing is Main.java from demo-desktop:
package com.packtpub.libgdx.demo;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
public class Main {
public static void main(String[] args) {
LwjglApplicationConfiguration cfg =
new LwjglApplicationConfiguration();
cfg.title = "demo";
cfg.useGL20 = false;
cfg.width = 480;
cfg.height = 320;
new LwjglApplication(new MyDemo(), cfg);
}
}
In the preceding code listing, you see the Main class, a plain Java class without
the need to implement an interface or inherit from another class. Instead, a new
instance of the LwjglApplication class is created. This class provides a couple
of overloaded constructors to choose from. Here, we pass a new instance of the
MyDemo class as the first argument to the constructor. Optionally, an instance of the
LwjglApplicationConfiguration class can be passed as the second argument.
The configuration class allows you to set every parameter that is configurable for
a Libgdx desktop application. In this case, the window title is set to demo and the
window's width and height is set to 480 by 320 pixels.
This is all you need to write and configure a Starter Class for a desktop.
Let us try to run the application now. To do this, right-click on the demo-desktop
project in Project Explorer in Eclipse and then navigate to Run As | Java Application.
Eclipse may ask you to select the Main class when you do this for the first time. Simply
select the Main class and also check that the correct package name (com.packtpub.
libgdx.demo) is displayed next to it.
The desktop application should now be up and running on your computer. If you
are working on Windows, you should see a window that looks as follows
this picture could change in different versions
No comments:
Post a Comment