4.2 libgdx modules Overview

Source: Internet
Author: User
Tags libgdx

(Original: http://www.libgdx.cn/topic/34/4-2-libgdx%E5%90%84%E4%B8%AA%E6%A8%A1%E5%9D%97%E6%A6%82%E8%A7%88)

Introduction
Libgdx integrates several modules to provide services for every step of building a typical game architecture.

  • Input: provides unified models and handler for all platforms. Supports keyboard, touch screen, sensor, and mouse.

  • Graphics: uses the OpenGL ES interface provided by hardware to draw images on the screen.

  • Files: provides a simple method for read/write operations. All platforms are supported.

  • Audio: Recording and playback of sound. All platforms are supported.

  • Networking: Provides network operations. For example, a simple http get and POST request communicates with a TCP server/client socket.

Generally, a simple game architecture can call the libgdx module through the following example:

Next we will introduce the most common functions of each module.

Module

Input
The input module checks different input states on each platform. It allows detection of the keyboard, touch screen, and sensor status. In a desktop environment (such as Windows or Linux), the touch screen is replaced by the mouse, and of course the sensor cannot be used.
It also provides an Input Processing Method Based on Input Model registration, which can be event-driven.
The following code checks whether a user has a touch event (the desktop platform clicks the mouse). If yes, the user obtains the current coordinates:
If (GDX. Input. istouched ()){
System. Out. println ("input occurred at x =" + GDX. Input. getx () + ", y =" + GDX. Input. Gety ());
}

Graphics
The graphics module abstracts GPU communications and provides a simple way to obtain OpenGL ES encapsulated instances.
These functions can be unavailable on some platforms.
The graphics module also provides methods for generating pixmaps and textures.
For example, you can use the following method to obtain an OpenGL API 2.0 instance:
Gl20 GL = GDX. Graphics. getgl20 () http://Gdx.graphics. getgl20gl20 ();
This method returns an instance, which can be drawn to the screen. If the device does not support OpenGL ES 2.0, null is returned.
The following code clears the screen and draws the screen in Red:
Gl. glclearcolor (0.1f, 0.0f, 0.0f, 1 );
Gl. glclear (gl1_gl _ color_buffer_bit );
All these operations are implemented through interfaces, so you don't have to worry about whether a platform supports this function.
Supported OpenGL API versions:
In fact, libgdx has abandoned support for OpenGL API 1.0, which is not described in the official English wiki.
Currently, only OpenGL API 2.0 and OpenGL API 3.0 are supported.
Files
The files module accesses files through a unified API and has nothing to do with the platform. This makes it easier to read and write files. Due to the security of the platform, file write operations are subject to certain restrictions.
The most common function of the files module is to load game resources (images and sounds) and write game scores and game statuses.
Next, we will show you how to obtain a texture through the project/assets/textures directory:
Texture mytexture = new texture (GDX. Files. Internal ("assets/texture/brick.png "));

Audio
Audio is used to create and play back sounds, especially simple sounds. At the same time, you can directly access audio hardware devices through APIS.
It contains two types of sound files: music and sound. Supports WAV, MP3, and Ogg formats.
Sound effects can be loaded into memory and can be played back at any time. Generally, the playback duration is short and used for multiple times. Such as explosion and gunshots.
Music refers to files placed on SD cards or other storage devices. The playback duration is long. The sample code is as follows:

Music = GDX. Audio. newmusic (GDX. Files. getfilehandle ("Data/mymusicfile#", filetype. Internal ));
Music. setvolume (0.5f );
Music. Play ();
Music. setlooping (true );

(Www.libgdx.cn is copyrighted. If you need to reprint it, indicate the source)

4.2 libgdx modules Overview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.