Gdx

1. Tổng quan

Class cần implements ApplicationListener và override các phương thức.

- onCreate(): khởi tạo các lớp nền như SpriteBatch, Texture, BitmapFont,...

- onRender(): các đối tượng trên màn hình sẽ được vẽ lại trong hàm này.

+ Được bắt đầu bằng lệnh xóa màn hình

+ Các câu lệnh cần vẽ lại sẽ nằm giữa:

    spriteBatch.begin();
    // code here
    spriteBatch.end();


2. Vẽ String

Ta sẽ tạo một đối tượng BitmapFont:

- Sử dụng câu lệnh:

    bitmapFont.draw(spriteBatch, "Text", int x, int y);

** Code ví dụ: **

    SpriteBatch batch;
    BitmapFont font;

    @Override
    public void create()
  {        
        batch = new SpriteBatch();    
        font = new BitmapFont();
        font.setColor(Color.BLUE);
        font.setScale(5);
    }

    @Override
    public void render()
 {        
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        batch.begin();
        font.draw(batch, "Vinh IT !", 200, 400);
        batch.end();
    }



1. Tổng quan

- Texture tạm hiểu là 1 bức hình (Image) được lấy từ thư mục assets

- TextureRegion là 1 vùng ảnh của Texture. 1 Texture có thể chứa nhiều ảnh và để cắt ảnh ra sử dụng, ta cần sử dụng TextureRegion.

- OrthographicCamera: là màn hình hiển thị, lớp này có chức năng thay đổi cách vẽ lên màn hình, có thể set theo chiều dọc/ngang.


2. Texture

    Texture texture = new Texture(Gdx.files.internal("link.png"));

- Định dạng ảnh có thể là png, jpg,...

- Ảnh được lưu tại thư mục assets.


3. TextureRegion

    TextureRegion textureRegion = new TextureRegion(texture, int x, int y, int width, int height);
    TextureRegion full = new TextureRegion(texture);


4. OrthographicCamera

private void configureCamera(){
    float size=800;
    if(Gdx.graphics.getHeight() < Gdx.graphics.getWidth()){
        // màn hình ngang
         camera.setToOrtho(false, size, size * Gdx.graphics.getHeight() / Gdx.graphics.getWidth());
    } else {
        camera.setToOrtho(false, size * Gdx.graphics.getWidth() / Gdx.graphics.getHeight(), size);
    }
}

@Override
public void resize(int w, int h){
    configureCamera();
}



*** Code ví dụ: ***

   SpriteBatch batch;
  OrthographicCamera camera;
 TextureRegion rockTexture;
 TextureRegion backgroundTexture;

    @Override
    public void create()
 {
  camera = new OrthographicCamera();
  configureCamera();
        batch = new SpriteBatch();
  
  Texture texture = new Texture(Gdx.files.internal("rock.png"));
  rockTexture = new TextureRegion(texture, 25, 0, 250, 250);
  backgroundTexture=new TextureRegion(new Texture(Gdx.files.internal("skyBackground.jpg")),0,0,2048,543);
    }

    @Override
    public void render()
 {
  Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
  
  camera.update();
  batch.setProjectionMatrix(camera.combined);
        batch.begin();
        batch.draw(backgroundTexture,0,0,2900,800);
  batch.draw(rockTexture, 0, 0, 100, 100);
  batch.draw(rockTexture, 200, 0, 100, 100);
        batch.end();
    }
 
 private void configureCamera()
 {
  if (Gdx.graphics.getHeight() < Gdx.graphics.getWidth())
   camera.setToOrtho(false, 800, 800 * Gdx.graphics.getHeight() / Gdx.graphics.getWidth());
  else
   camera.setToOrtho(false, 800 * Gdx.graphics.getWidth() / Gdx.graphics.getHeight(), 800);
 }

 @Override
    public void dispose()
 {
        batch.dispose();
    }

    @Override
    public void resize(int width, int height)
 {
  configureCamera();
    }



1. Tổng quan

- Để vẽ các hình khối cơ bản như hình chữ nhật, tam giác, hình tròn, đường thẳng,.... thì ta sử dụng lớp ShapeRenderer.

- ShapeRenderer cũng tương tự SpriteBatch, để vẽ các hình khối thì các câu lệnh vẽ cần nằm giữa 2 câu lệnh:

shape.begin(ShapeRenerder.ShapeType.Filled);

shape.end();


Có 3 kiểu vẽ: Filled, Line, Point;


Cần setColor trước khi vẽ, mã màu có thể là hexa hoặc argb.


** Code ví dụ: ***

ShapeRenderer shapeRenderer;

OrthographicCamera camera;


@Override

public void create()

{

camera = new OrthographicCamera();

configureCamera();

shapeRenderer = new ShapeRenderer();

}


@Override

public void render()

{

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

Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);


camera.update();

shapeRenderer.setProjectionMatrix(camera.combined);

shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);

shapeRenderer.setColor(0, 0.5f, 0, 1);

shapeRenderer.circle(140, 50, 40);

shapeRenderer.setColor(0.5f, 0, 0, 1);

shapeRenderer.rect(10, 100, 80, 80);

shapeRenderer.setColor(0, 0, 0.5f, 1);

shapeRenderer.triangle(10, 200, 90, 200, 50, 270);

shapeRenderer.end();

}


private void configureCamera()

{

float size = 1600;

if (Gdx.graphics.getHeight() < Gdx.graphics.getWidth())

camera.setToOrtho(false, size, size * Gdx.graphics.getHeight() / Gdx.graphics.getWidth());

else

camera.setToOrtho(false, size * Gdx.graphics.getWidth() / Gdx.graphics.getHeight(), size);

}

@Override

public void dispose()

{

shapeRenderer.dispose();

}


@Override

public void resize(int width, int height)

{

configureCamera();

}





Trong render() ta sẽ thay đổi tọa độ các object bằng cách sử dụng thêm:

Gdx.graphics.getDeltaTime();


Ví dụ:


float x;

float speed=50;

x=x+speed*Gdx.graphics.getDeltaTime();

if(x>camera.viewportWidth) x=-50;

1. Đầu tiên, ta sử dụng 1 Texture với nhiều khung frame, sau đó tách nó ra và lưu vào mảng 1 chiều TextureRegion[]

- Ví dụ Texture có dạng các khung hình sắp xếp theo ma trận, ta sec dùng phương thức split để tách Texture thành mảng 2 chiều TextureRegion[][], rồi sau đó mới gộp nó lại thành mảng 1 chiều.


** Code: **

Texture walkSheet = new Texture(Gdx.files.internal("runAnimation.png"));

int FRAME_COLS = 6;

int FRAME_ROWS = 5;

TextureRegion[][] tmp = TextureRegion.split(walkSheet, walkSheet.getWidth() / FRAME_COLS, walkSheet.getHeight() / FRAME_ROWS);

TextureRegion[] walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];

int index = 0;

for (int i = 0; i < FRAME_ROWS; i++)

for (int j = 0; j < FRAME_COLS; j++)

walkFrames[index++] = tmp[i][j];

walkAnimation = new Animation(0.01f, walkFrames);


2. Kế tiếp, ta sử dụng lớp Animation tạo hoạt ảnh.

- Sử dụng câu lệnh:

Animation anim = new Animation(float speed, TextureRegion[] textureRegion);


3. Vẽ animation

- Trong render(), ta sẽ vẽ nó lên SpriteBatch.

batch.begin();

float time;

batch.draw(anim.getKeyFrame(time, true), 0, 0, 200, 200);

batch.end();


time += Gdx.graphics.getDeltaTime();



- Dùng phương thức translate của OrthographicCamera.


SpriteBatch batch;

OrthographicCamera camera;

TextureRegion rockTexture;

TextureRegion backgroundTexture;


@Override

public void create()

{

camera = new OrthographicCamera();

configureCamera();

batch = new SpriteBatch();

Texture texture = new Texture(Gdx.files.internal("rock.png"));

rockTexture = new TextureRegion(texture, 25, 0, 250, 250);

Texture texture2 = new Texture(Gdx.files.internal("skyBackground.jpg"));

backgroundTexture = new TextureRegion(texture2, 0, 0, 2048, 563);

}


@Override

public void render()

{

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

Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

camera.update();

batch.setProjectionMatrix(camera.combined);

batch.begin();

for (int i = 0; i < 30; i++)

batch.draw(backgroundTexture, i * 2900, 0, 2900, 800);

for (int i = 0; i < 100; i++)

batch.draw(rockTexture, i * 800, 0, 100, 100);

batch.end();

camera.translate(500 * Gdx.graphics.getDeltaTime(), 0);

}