What's libGDX 2. Standing on the Shoulders of Giants 3Copy the code

What’s libGDX

A high-performance, cross-platform game development framework that serves as the basis for engines and games. This framework allows us to focus on the basics rather than the new features of the game engine. It has great flexibility.

  • LibGDX is a cross-platform development framework. It is currently supported on Windows, Linux, Mac OS X, Android, blackberry, iOS and HTML5.
  • LibGDX allows you to deploy code to multiple platforms without any modifications, rather than modifying your code for different platforms or compiling directly to HTML5. You can quickly deploy your code in a desktop development environment. You can use all the development tools in the Java ecosystem.
  • Libgdx can operate at the bottom. Allows you direct access to file systems, input devices, output devices, audio devices and access to OpenGL through the unified OpenGL ES interface.
  • Based on these low-level operations, libgdx builds powerful apis to help you accomplish common development tasks. Examples include rendering sprites, drawing text, building user interfaces, playing sound effects and music streams, linear algebra and trigonometry, parsing JSON and XML data, and more.
  • When necessary, libGDX can achieve better performance through native code. All of this functionality is hidden in Java API functions, so you don’t have to worry about cross-compiling native code for different platforms. Many parts of libGDX run on known platforms, so don’t worry.
  • LibGDX is intended to be a framework, not an engine. Instead, Libgdx’s powerful abstraction lets you write the game or application you want.

Standing on the Shoulders of Giants

LibGDX is based on so many third-party libraries:

  • OpenGL (a professional graphical programming interface specification across programming languages and platforms)
  • FreeType (completely free (open source), high quality, portable font engine)
  • Mpg123 (fast, portable MPEG player and decoder)
  • Xiph.org (Open codec)
  • Soundtouch (open source audio processing library, the main implementation contains variable speed, variable speed modulation, simultaneous modulation and other three function modules)
  • Box2d (engine for simulating 2D rigid-body objects)
  • LWJGL (provides developers with easy-to-use apis to access OpenGL (Open Graphics Library) and OpenAL (Open Audio Library) as well as manipulation controllers (Gamepads, Steering Wheel and joystick API)
  • OpenAL (Cross-platform Sound API for free software)
  • KissFFT (famous KISS FFT algorithm, optimization for floating-point arithmetic, and optimization for fixed-point arithmetic)

HelloWorld

Execution steps:

  1. Download the etf has – setup. The jar

  2. Start the etf has – setup. The jar

  3. Android needs to configure SDK, execute “Generate”

  4. AndroidStudio Open folder

  5. Run

Preview Code:



public class AndroidLauncher extends AndroidApplication {

@Override

protected void onCreate (Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();

initialize(new MyGdxGame(), config);

}

}

Copy the code








public class MyGdxGame extends ApplicationAdapter {

SpriteBatch batch;

Texture img;

@Override

public void create () {

batch = new SpriteBatch();

img = new Texture("badlogic.jpg");

}

@Override

public void render () {

Gdx.gl.glClearColor(1, 0, 0, 1);

Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

batch.begin();

batch.draw(img, 0, 0);

batch.end();

}

}

Copy the code


Core

LibGDX graph drawing

Learn four classes: SpriteBatch, Texture, TextureRegion and SpriteCopy the code
1. The Texture class
Definition: An image is called a texture when it is decoded from its original format and uploaded to the GPU. * Function: in fact, the container is loaded to get the target image. <code><pre> new Texture("badlogic.jpg"); </code></pre>Copy the code
2. The SpriteBatch class
* Definition: SpriteBatch is used to draw 2d rectangular reference textures (regions). The SpriteBatch class is a GPU that can be used for batch drawing commands and optimization processing. Features: SpriteBatch can describe many of the same textures together and feed them into the GPU together, while giving textures and coordinates for each graphic to draw. -------------SpriteBatch can be understood as a brush. <code><pre> SpriteBatch Batch; . public void render () { batch.begin(); batch.draw(img, 0, 0); batch.end(); } </code></pre>Copy the code
3. TextureRegion class
* API definition: defines the texture of a rectangular area. Use the origin of the coordinate system with the X-axis pointing to the right and the Y-axis pointing down in the upper left corner. * Functional use: In practice, we often use a part of the image, or multiple image resources in a picture file. To display a portion of the image, use the TextureRegion class.Copy the code
4. The Sprite class
* API definition: holds geometry, color, and texture information using loading to draw 2D sprites. A Sprite has positioning and given width and height dimensions. TextureRegion is an enhanced version of TextureRegion. It has more features than TextureRegion, such as position, color, rotation, etc. In fact, Sprite is a collection of the above. But Sprite is much more convenient. It describes everything in one object, but adds a lot of things that TextureRegion and Texture don't, like position, color, rotationCopy the code

LibGDX stage and actors

1. Stage class
  • Definition: contains a two-dimensional scene with a hierarchy of actors. Handles views and allocates input events. The stage is responsible for manipulating perspectives and handling assigned input events.

  • What it does: A Stage fills the entire screen. Set the Angle of view (usually floating point and Boolean), set the camera to use, and adjust the relationship conversion between Actor, Group and Screen (including coordinates). A Stage must be responsible for receiving input events and assigning them to actors. This is usually through stages of etf has. Input. Setinputprocessor to implement. An inputmultiplexer can be used to process the different stages of the input event, i.e., before or after. If an actor processes an event from the input method and returns TRUE, then Stage’s input method will also return TRUE, causing subsequent InputProcessors to receive no events.

  • Use method: Stage(float width, float height, Boolean keepAspectRatio, SpriteBatch Batch)

    (1) First parameter: set the width of the stage.

    (2) The second parameter: set the stage height.

    (3) The third parameter: is responsible for whether the stage covers the screen, if this value is true, the stage size is the size you set. If false, it covers the screen.

    (4) The fourth parameter: pass in the Spirtibatch you declared.

2. Actors
  • Definition: In a 2d scene diagram, an actor has attributes such as position, rectangle size, starting point, scale, rotation, and color. The actor’s position is relative to the actor’s parents, and its starting point is relative position, which can be used for scaling and rotation at the same time. An actor also has an action that can join a listener to enable the actor to receive event notifications.

  • What it does: Actors are a basic element in a project. Libgdx has lots of useful methods for actors. Common ones are Draw, Addlistrener, Fire, AddAction, etc.

3. The concept of stage and actor abstracts the concept of elements in the game, realizing the random configuration and switching of different actors in the same scene and the same actor appearing in different scenes.

LibGDX sound

1. The Audio interface

Supported formats: MP3, OGG, WAV, etc.



public interface Audio {

public Sound newSound(FileHandle fileHandle);

public Music newMusic(FileHandle file);

public AudioDevice newAudioDevice(boolean isMono);

}

Copy the code

2. As an interface
  • The Sound class is designed to play Sound effects
  • Definition: a short audio clip that can be played multiple times. It is fully loaded into memory and is only responsible for loading small audio files. When the audio is finished, the Dispose method is called to destroy it.
  • Supported format: SUPPORTS WAV format, but does not support OGG format.



Sound sound = Gdx.audio.newSound(Gdx.files.internal("sound.wav"));

sound.setLooping(true);

sound.play();

sound.setVolume(15);

Copy the code

3. The Music class
  • Definition: A long audio clip that can be played multiple times. It is partially loaded into memory and is only responsible for loading large audio files. When the audio is finished, dispose method is called to destroy it.
  • Supported formats: MP3, OGG, WAV, etc.



Music music = Gdx.audio.newMusic(Gdx.files.internal("xxx.ogg"));

music.setLooping(true);

music.setVolume(15);

music.play();

Copy the code

LibGDX resource loader

The AssetManager class is responsible for loading files like Textures, BitmapFonts, Tile maps, Sounds, music, etc.
API

(1) Clear () method: Clear and delete all loaded resources and preloaded queues.

(2) containsAsset(T asset) : Returns a Boolean type that checks if any resources are loaded.

(3)finishLoading() : after all resources are loaded, the loading of resources is finished. You can also manually load a certain item to finishLoading.

GetAssetFileName (T asset) : getAssetFileName(T asset) : getAssetFileName(T asset) : getAssetFileName(T asset)

(4) getLoadedAssets() : Get the number of loaded resources.

(5) getProgress() : Get the loading progress, return a decimal between 0 and 1, this can generally be used to make progress bar.

(6) isLoaded(java.lang.String fileName) : returns a Boolean to check whether the file you passed was already loaded.

(7) load(java.lang.String fileName, java.lang.Class type) : adds the passed fileName and type to the preloading queue.

(8) setLoader(java.lang.Class type, AssetLoader) : Set the loader, in fact, the AssetManager class wrapped in the need for Assetloader object, so that the completion of different types of file loading, of course, AssetManager can also be added to the loader.

(9) Unload (java.lang.String fileName) : Unloads resources. If a resource is no longer in use, it can be unloaded for better game flow.

(10) Update () : the Load method actually loads the preloaded resource into the preload queue, but does not Load the resource. Update () method is used to Load the resources stored in the preloaded pair.

Functional advantages :(official translation)

Most resources are loaded asynchronously, so that they can be loaded without blocking the renderer process.

(2) implementation of reference counting, when A and B are dependent on C material,C will only be destroyed when A and B are destroyed. This also means that even if a resource is loaded many times, there will be a later copy in memory.

(3) Use a single manager to manage all materials.

(4) It can realize the system memory occupied when loading materials.

LibGDX Particle Effects & Particle Editor

For this example editor, I personally recommend watching the video I mentioned below, which has a more intuitive tutorial on using the particle editor.

The official website explains each parameter in the software as follows:

  • Delay: How long does the transmitter wait to start firing after the particle system starts
  • Duration: The Duration of the emitter effect. Note that this is different from the Duration of the particle
  • Count: As the name implies, the number of particles that can be present at any one time, with an upper limit and a lower limit.
  • Emission: Every second launch how many particles, this parameter with a chart, the chart and write his duration, on behalf of the chart the X-axis represents the launcher survival time, mean launcher survival time, various points in the chart control particle emission per second, how many on the left side of the upper and lower two text box used to control the scope of the initial value, The ‘>’ character on the left is used to enter another value, such as the “>” character below, and the emitter will select a random value between the two values as the upper limit, Relative. If selected, this indicates that the values in the chart are Relative to the initial value. Otherwise it’s an absolute value.
  • Life: the Life time of a particle,
  • Life Offset: Determines how much of a particle’s Life has been spent before being displayed. This allows a particle to appear at 50% of its Life
  • X Offset and Y Offset: the pixel Offset at which the particle appears relative to the center
  • Spawn: Shape of the emitter used to produce particles
  • Spawn Width and Spawn Height: The Width and Height of the emitter’s shape
  • Size: indicates the Size of a particle
  • Velocity:
  • Angle:
  • Rotation: These values are used to control the particle’s trajectory:

  • Wind and Gravity: Smoke is the pixel offset per second on the X and Y axes during the lifetime

  • Tint: The color of a particle, which can change as many colors as it wants during the lifetime of the particle

  • Transparency: particle Transparency


    recommended

  • The bibliography

    LibGDX Game Development Primer

  1. The url
[*] [libGDX website (https://libgdx.badlogicgames.com/index.html) * # # Mkey Testin cup Libgdx tutorial] (http://www.apkbus.com/android-57355-1-1.html) * * article cited the post of the translation content * * * [struggling small potatoes, blog] (http://blog.sina.com.cn/s/articlelist_2483934474_1_1.html)Copy the code
  1. LibGDX Development Tutorial – Struggle with little Potatoes