libGDX এ কোন টেক্সট দেখানোর জন্য দরকার ফন্ট আর পজিশন। BitmapFont দিয়ে আমরা ডিফল্ট ফন্ট সেট করতে পারি। ইচ্ছে করলে আমাদের নিজেদের ফন্টও ব্যবহার করতে পারি। এরপর পজিশন এবং কি টেক্সট দেখাতে চাই, তা বলে দিলে আমাদের ফন্ট দেখাবে। নিচের সম্পুর্ণ একটি কোডঃ
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class TextDemo extends ApplicationAdapter {
SpriteBatch batch;
BitmapFont font;
@Override
public void create() {
batch = new SpriteBatch();
// Create the default font
font = new BitmapFont();
// Scale it up
font.getData().setScale(3);
// Set the filter
font.getRegion().getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
}
/**
* Remember to dispose of SpriteBatches and fonts!
*/
@Override
public void dispose() {
batch.dispose();
font.dispose();
}
@Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// We begin batches just like with a ShapeRenderer, though there's no mode
batch.begin();
font.draw(batch, "Hello Gaming World!", 100, 100);
// Remember to end the batch
batch.end();
}
}
এখানে setScale(3) দিয়ে ফন্ট কত বড় দেখাবে, তা বলে দিয়েছি। কালার দিতে চাইলে আমরা কালার সেট করে দিতে পারি। নিচের কোড দেখিঃ
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class TextDemo extends ApplicationAdapter {
SpriteBatch batch;
BitmapFont font;
@Override
public void create() {
batch = new SpriteBatch();
// Create the default font
font = new BitmapFont();
// Scale it up
font.getData().setScale(5);
// Set the filter
font.getRegion().getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
}
/**
* Remember to dispose of SpriteBatches and fonts!
*/
@Override
public void dispose() {
batch.dispose();
font.dispose();
}
@Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// We begin batches just like with a ShapeRenderer, though there's no mode
batch.begin();
font.setColor(Color.RED);
font.draw(batch, "Hello Gaming World!", 300, 300);
// Remember to end the batch
batch.end();
}
}
প্রজেক্টের সোর্স কোড গুলো ডাউনলোড করা যাবে গিটহাব থেকে। প্রত্যেকটা প্রজেক্ট আলাদা আলাদা ফোল্ডারে রয়েছে। ঐখান থেকে ডাউনলোড করে নিয়ে কাজ করা যাবে। গিটহাব লিঙ্ক।