2
\$\begingroup\$

I am learning AndEngine. I am displaying Hello world using this code:

public class myactivity extends SimpleBaseGameActivity
{

 private final int CAMERA_WIDTH = 320;
 private final int CAMERA_HEIGHT = 480;

 private Camera  m_Camera;
 private Scene  m_Scene;

 private Font  font;
 private Text  text;

 @Override
 public EngineOptions onCreateEngineOptions()
 {
  m_Camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
  EngineOptions en = new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new RatioResolutionPolicy(
    CAMERA_WIDTH, CAMERA_HEIGHT), m_Camera);

  return en;
 }

 @Override
 protected void onCreateResources()
 {
    //determine the density
 WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
 Display display = windowManager.getDefaultDisplay();
 DisplayMetrics displayMetrics = new DisplayMetrics();
 display.getMetrics(displayMetrics);
 int density = (int)(displayMetrics.density);

 //scale desired size 25 by density
     int fontSize = (int) (25 * density);
  font = FontFactory.createFromAsset(this.getFontManager(), this.getTextureManager(), 1024, 1024, this.getAssets(),
    "times.ttf", fontSize, true, android.graphics.Color.BLACK);
  font.load();



//Texture fontTexture = new BitmapTextureAtlas(1024, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
//font = new Font(fontTexture, Typeface.create(Typeface.DEFAULT, Typeface.NORMAL), fontSize, true, Color.WHITE);
//mEngine.getTextureManager().loadTexture(fontTexture);
//mEngine.getFontManager().loadFont(font);

 }

 @Override
 protected Scene onCreateScene()
 {
  m_Scene = new Scene();
  m_Scene.setBackground(new Background(Color.WHITE));

  text = new Text(0, 0, font, "Hello Android", this.getVertexBufferObjectManager());
  m_Scene.attachChild(text);

  text.setPosition(CAMERA_WIDTH/2 - (text.getWidth()/2), CAMERA_HEIGHT/2 - (text.getHeight()/2));
  return m_Scene;
 }

}

It is displaying the font like this: Hello Android displayed but with pixelated edges

As you can see that pixels are stretching and font is looking ugly with too much pixelation. How do I display proper font text with no pixelation or add anti-aliasing to the font?

\$\endgroup\$
  • \$\begingroup\$ Is it possible it is to do with the scaling you are applying to the texture generated from the font? \$\endgroup\$ – Tom 'Blue' Piddock Jan 2 '14 at 11:12
2
\$\begingroup\$

It actually looks like you are already on the right track with your comments.

Try changing your font initialisation to this:

//scale desired size 25 by density
int fontSize = (int) (25 * density);
//define texture for font with BILINEAR scaling
final ITexture fontTexture = new BitmapTextureAtlas(this.getTextureManager(), 1024, 1024, TextureOptions.BILINEAR);
font = FontFactory.createFromAsset(this.getFontManager(), fontTexture, this.getAssets(),
     "times.ttf", fontSize, true, android.graphics.Color.BLACK);
font.load();

Try it out and let us know the results.

\$\endgroup\$
  • \$\begingroup\$ you save my hours infect i am thinking to switch engine :) \$\endgroup\$ – Noaman Akram Jan 4 '14 at 9:32
  • 1
    \$\begingroup\$ Glad to have helped. There are many engines out there but if it isn't broke - don't fix it. Stick with something you know and you'll be fine. That's why we are here to help you stay on track. \$\endgroup\$ – Tom 'Blue' Piddock Jan 4 '14 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.