Android development _ libgdx game engine tutorial excellent performance game engine-libgdx (5)

Source: Internet
Author: User
Tags libgdx

I,

 

Libgdx is a cross-platform 2D/3D Game Development Framework written in Java/C ++. Ibgdx is compatible with most microcomputer platforms (Standard javase implementation, running on Mac, Linux, windows, and other systems, and recently added HTML5 support) and Android platforms (android1.5 and above, android2.1 and above can be used up to full power ). Libgdx comes with an image decoder in native mode. The pixmap provided by libgdx can perform pixel-level rendering on the specified image, so that it does not depend on the android bitmap to load and process the image. It can support PNG, JPG, and BMP, the support for GIF images has been added to the last two versions (if you want to make a large number of animations, GIF images can be scaled twice when the image quality cannot be reduced too much, you can understand this ).

For libgdx platform construction is not much repeat, you can see another post in the android bus http://www.apkbus.com/android-19745-1-1.html
Some of the content is very detailed and can help you get started quickly. The tutorials in this series are intended to help you further perform operations on the basis of getting started and share some of the problems I have encountered, this prevents you from wasting valuable time on these issues. You can download the latest libgdxat http://code.google.com/p/libgdx. Here, we also want to remind you that libgdx is divided into latest releases and nightlies. The former is a officially released version, and the latter is a version that is updated at any time. We recommend that you do not have any special requirements, it is best to use lastest release to avoid some strange problems. I am using the latest version 0.9.4, which includes a lot of new content (including 3D ), the efficiency is also significantly improved compared with the old version. It is best not to use version 0.9.2, because version 0.9.4 modifies some interfaces, I used to spend some time modifying the code when I updated the libgdx version of my game from 0.9.2 to 0.9.4, because the original interfaces and functions have been deprecated... The first article focuses on the introductory content, here we will first post a simple demo for you to learn (provided that you have set up the libgdx environment ). By the way, you only need:
  • Armeabi/
  • Armeabi-v7a/
  • Gdx-backend-android.jar
  • GDX. Jar

These four files can be, the specific use of the method can be http://www.apkbus.com/android-19745-1-1.htm

Libgdx is a game library that supports 2D and 3D Game Development. It is compatible with most microcomputer platforms (Standard javase implementation and can run on Mac, Linux, windows, and other systems) it can be used with the Android platform (android1.5 or above, and android2.1 or above can be used at full power ).

Logo:

Upload at a.m. on February 16

Download Attachment(5.65 KB)

Address: http://code.google.com/p/libgdx/

First download the latest jar, I downloaded the libgdx-0.9.2.

Upload at a.m. on February 16

Download Attachment(49.84 KB)

Create an android project, File> New> Project> Android project. The sdk I selected is 1.6, and the minimum option is 1.5.

Create a new folder libs (the name is fixed and cannot be changed at will), copy the downloaded jar into, Android platform only need to copy GDX. jar and gdx-backend-android.jar.

Add references, and then copy the armeabi and armeabi-v7a folders to the libs folder.

Strictly follow the steps !!!!!!!!!!!!!!!!!!!!!!

Upload at a.m. on February 16

Download Attachment(46.63 KB)

Then write a small item to test whether the environment is correct.

The new class hellogameactivity inherits the androidapplication class.

  1. Package com. cnblogs. htynkn;
  2. Import com. badlogic. GDX. backends. Android. androidapplication;
  3. Import Android. OS. Bundle;
  4. Public class hellogameactivity extends androidapplication {
  5. @ Override
  6. Public void oncreate (bundle savedinstancestate ){
  7. Super. oncreate (savedinstancestate );
  8. Initialize (New firstgame (), false );
  9. }
  10. }

Copy code

  1. Initialize (New firstgame (), false );

Copy codeIt is the key to starting the game. firstgame is a class that implements applicationlistener. The Code is as follows:

  1. Package com. cnblogs. htynkn;
  2. Import com. badlogic. GDX. applicationlistener;
  3. Import com. badlogic. GDX. GDX;
  4. Import com. badlogic. GDX. Graphics. gl10;
  5. Import com. badlogic. GDX. Graphics. g2d. spritebatch;
  6. Public class firstgame implements applicationlistener {
  7. // Spritebatch for plotting
  8. Private spritebatch batch;
  9. @ Override
  10. Public void create (){
  11. Batch = new spritebatch (); // instantiate
  12. }
  13. @ Override
  14. Public void dispose (){
  15. // Todo auto-generated method stub
  16. }
  17. @ Override
  18. Public void pause (){
  19. // Todo auto-generated method stub
  20. }
  21. @ Override
  22. Public void render (){
  23. GDX. gl. glclear (gl10.gl _ color_buffer_bit); // clear the screen
  24. Batch. Begin ();
  25. Batch. End ();
  26. }
  27. @ Override
  28. Public void resize (INT width, int height ){
  29. // Todo auto-generated method stub
  30. }
  31. @ Override
  32. Public void resume (){
  33. // Todo auto-generated method stub
  34. }
  35. }

Copy codeBecause no actual content is written, the effect is a black box.
Upload at a.m. on February 16

Download Attachment(14.37 KB)
The next article is about image rendering. Note: If you carefully observe logcat, you will find an error: E/libegl (382): couldn 'tload <libhgl. so> Library (cannot load Library: load_library [984]: Library 'libgl. so 'not found) It doesn't matter. This is because the system fails to load the hardware OpenGL driver and returns to the software processing method, not a defect or error. Let's first write a class

  1. Public class mygame implements applicationlistener {
  2. Public void create (){
  3. // Stub
  4. }
  5. Public void render (){
  6. // Stub
  7. }
  8. Public void resize (INT width, int height ){
  9. // Stub
  10. }
  11. Public void pause (){
  12. // Stub
  13. }
  14. Public void resume (){
  15. // Stub
  16. }
  17. Public void dispose (){
  18. // Stub
  19. }
  20. }

Copy code

Applicationlistener is an interface. We can easily see that all classes that implement this interface have a common feature. They all go through a creation, rendering (render), pause, and continue, destruction and other processes, when the corresponding time occurs, the corresponding function will be called for your own use. In particular, the render () function is a function that is constantly called by the system (of course, this instance meets the running conditions). Therefore, we can implement many operations in the render () function, do not perform time-consuming operations to avoid blocking the UI thread. Next we will start a libgdx game! Write another class

  1. Public class libgdxactivity extends androidapplication {
  2. @ Override
  3. Public void oncreate (bundle savedinstancestate ){
  4. Super. oncreate (savedinstancestate );
  5. Initialize (New mygame (), false );
  6. }
  7. }

Copy codeThis completes the compilation of the first libgdx program! Of course, a variety of import XXXX needs to be added to the source code. We will not add it here. In addition, we will attach the source code for you to download.

Query the official documentation and you can see
Java. Lang. Object
| Activity
|Com. badlogic. GDX. backends. Android. androidapplication
What does this mean ?? That is to say, androidapplication inherits from the activity in the API, so we also need to register it in manifest. Here we will not specifically describe how to register it. Everyone should know it! In addition, this provides a better question for everyone to think about. Since androidapplication inherits from activity, can we use intent to redirect like other activities? The answer is yes, but it is not that simple. I will focus on this issue in the subsequent articles.

AsInitialize (New mygame (), false); this statement is an essential condition for starting libgdx. It cannot be started without executing libgdx, the first parameter is an instance of the class that implements the applicationapplication interface. If the second parameter is set to true, OpenGL 2.0 is used when OpenGL 2.0 is available.

Related Article

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.