13 thg 3, 2012

[Tut]_libGDX : Vòng đời của 1 ứng dụng


Bài này mô tả cấu trúc của 1 ứng dụng phát triển từ thư viện libGDX như thế nào và sự khác nhau giữa các playforms. 

The Application

Đây là điểm quan trọng của dự án libGDX. giao diện của ứng dụng được cung cấp các phương thức truy cập vào các thành phần chung như Graphices, Audio, Input, File I/O. Nó cũng cho phép truy cập đến thành phần Logging làm việc với tất cả các platforms.
LibGDX hiện tại đã hỗ trợ 2 thành phần cho ứng dụng desktop là lwjgl và jogl , cùng với đó là 1 thành phần cho android.
Để tạo 1 ứng dụng libGDX, cần phải implement phương thức ApplicationListener đầu tiên. 

The Application Listener

Bộ lắng nghe này phản hồi những khởi tạo của ứng dụng, cập nhật trạng thái game, rendering the output gamem tạm dừng game, lưu trạng thái game, hay giải phóng tài nguyên khi thoát ứng dụng.

Khi xây dựng 1 ứng dụng hay 1 game nào đó, chúng ta đều phải implements ApplicationListener interfaces. 
It is also the place where the application life-cycle events are handled. Every application/game, regardless of the back-end and/or target platform will have to implement the ApplicationListener interface. The implementation is exactly the same for all platforms.

ApplicationListener có dạng sau :

public class MyGame implements ApplicationListener {
        public void create () {
                // STUB
        }

        public void render () {
                // STUB
        }

        public void resize (int width, int height) { 
                // STUB
        }

        public void pause () { 
                // STUB
        }

        public void resume () {
                // STUB
        }

        public void dispose () { 
                // STUB
        }
}

Method signatureDescription
create ()Method được gọi 1 lần khi ứng dụng được tạo  ra.
resize(int width, int height)Method này được gọi tại mọi thời gian mà màn hình game được re-sized và game không ở trạng thái pause. Nó được gọi sau create() method.
render ()Method được gọi bởi vòng lặp của game từ mọi thời điểm của ứng dụng khi rendering xảy ra. 
pause ()Phương thức này được gọi trước khi game bị hủy. Androi gọi nó khi phím Home được nhấn xuống hoặc có cuộc gọi đến. Trên desktop game thì được gọi trước phương thức despose() khi thoát game.
resume ()Phương thức này chỉ có trên android.
dispose ()Gọi khi game bị hủy.

Application life-cycle


Creating the Desktop Application

The following class creates a desktop application using lwjgl from the previously created MyGame listener.
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
public class MyDesktopGame {
        public static void main(String[] args) {
                new LwjglApplication(new MyGame(), "My Game", 480, 320, false);
        }
}
Chạy game.
It creates a lwjgl backed application 480 pixels wide, 320 pixels tall. The last parameter indicates that OpenGL ES 2 is NOT to be used.
The following class creates a desktop application using JOGL as the back-end from the same MyGame listener.
import com.badlogic.gdx.backends.jogl.JoglApplication;
public class MyDesktopGame {
        public static void main(String[] args) {
                new JoglApplication(new MyGame(), "My Game", 480, 320, false);
        }
}
In both cases the appropriate libraries have to be used. In the first case the following jar files need to be included:
  • gdx.jar
  • gdx-natives.jar
  • gdx-backend-lwjgl.jar
  • gdx-backend-lwjgl-natives.jar
In the second case, for jogl, the following jars need to be included:
  • gdx.jar
  • gdx-natives.jar
  • gdx-backend-jogl.jar
  • gdx-backend-jogl-natives.jar
The constructor for the desktop application takes 5 parameters:
ApplicationListener listenerThis is an instance of the ApplicationListener created for the application game
String titleThe title of the application which will be shown on the title bar
int widthThe screen width of the application window
int heightThe screen height of the application window
boolean useGL2true if the we want the renderer to use OpenGL 2, or false to stick with OpenGL 1.x


Không có nhận xét nào:

Đăng nhận xét

Bản beta đầu tiên

Sau 6 tháng cả team cặm cụi làm việc điên cuồng, bản alpha cũng được giới thiệu ra toàn bộ công ty và được testing nội bộ công ty mà thôi. ...