libgdx: screens, fonts and preferences

17
libGDX: Screens, Fonts and Pref Jussi Pohjolainen Tampere University of Applied Sciences

Upload: jussi-pohjolainen

Post on 15-Jul-2015

363 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: libGDX: Screens, Fonts and Preferences

libGDX:  Screens,  Fonts  and  Pref  

Jussi  Pohjolainen  Tampere  University  of  Applied  Sciences  

Page 2: libGDX: Screens, Fonts and Preferences

SCREENS  

Page 3: libGDX: Screens, Fonts and Preferences

Screen  and  Game  

•  Screen – Use  to  add  different  screens  to  your  game  – Screen  is  a  interface  and  contains  all  the  same  methods  than  ApplicationListener.  Also  show() and  hide() when  screen  is  shown  and  hidden  

•  Game – Game  is  an  ApplicationListener  that  holds  also  methods  for  changing  the  screen:  setScreen(Screen s)  

Page 4: libGDX: Screens, Fonts and Preferences

public class ScreensFontsPref extends Game {

private MainMenuScreen mainmenu;

// Common objects to all screens! private SpriteBatch batch;

// Returns the SpriteBatch public SpriteBatch getBatch() {

return batch; }

@Override public void create () { batch = new SpriteBatch();

// Create MainMenuScreen mainmenu = new MainMenuScreen(this); // Set it visible setScreen(mainmenu); }

@Override

public void render () { super.render(); // Remember this, otherwise nothing is shown! }}  

Page 5: libGDX: Screens, Fonts and Preferences

public class GameScreen implements Screen {

private ScreensFontsPref game; private SpriteBatch batch; private OrthographicCamera camera;

public GameScreen(ScreensFontsPref g) { game = g; batch = game.getBatch(); camera = new OrthographicCamera(); camera.setToOrtho(false, 800, 480); }

@Override public void render(float delta) { batch.setProjectionMatrix(camera.combined);

Gdx.gl.glClearColor(1, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

batch.begin(); // Do same drawing Gdx.app.log("GameScreen", "render"); batch.end();

if (Gdx.input.isTouched()) { game.setScreen(new GameScreen(game)); } }  

Page 6: libGDX: Screens, Fonts and Preferences

FONTS  

Page 7: libGDX: Screens, Fonts and Preferences

About  Fonts  •  libGDX  does  not  support  TrueType  Fonts  (TTF)  

–  Rendering  vector  graphics  is  costly    •  libGDX  makes  use  of  bitmap  files  (pngs)  to  render  fonts  

•  Use  external  tool  to  convert  TTF  to  PNG!  •  Each  glyph  in  the  font  has  a  corresponding  TextureRegion  

•  Default  font  Arial  provided,  other  fonts  you  have  to  create!  

•  If  you  use  same  font  in  different  screens,  create  font  once  and  share  it  with  other  screens.  

Page 8: libGDX: Screens, Fonts and Preferences

Simple  to  Use  BitmapFont f = new BitmapFont();f.draw(batch, "Hello World", 10, 10);f.dispose();

Page 9: libGDX: Screens, Fonts and Preferences

Tools  for  CreaPng  own  Fonts  

•  Hiero  (http://bit.ly/16EpREP) – Free  and  buggy?  Java  app  

•  Glyphite  (https://www.glyphite.com/)  – Free  web  app  

•  LiFera  (http://kvazars.com/littera/) – Free  web  app    

Page 10: libGDX: Screens, Fonts and Preferences

LiFera  Usage  

1.  Upload  some  .S  font  2.  Export  format  .txt  3.  Add  to  assets  (.png  

and  .txt)  to  libGDX  project  

Page 11: libGDX: Screens, Fonts and Preferences

Simple  to  Use  BitmapFont f = new new BitmapFont(Gdx.files.internal("font.txt"));f.draw(batch, "Hello World", 10, 10);f.dispose();

Page 12: libGDX: Screens, Fonts and Preferences

Calculate  PosiPon  

Libgdx  getAscent()  

getDescent()  

getLineHeight()  

Page 13: libGDX: Screens, Fonts and Preferences

Calculate  PosiPon  String fontText = "Main Menu";

float fontX = game.getDefaultFont().getBounds(fontText).width;

float fontY = game.getDefaultFont().getBounds(fontText).height;

Page 14: libGDX: Screens, Fonts and Preferences

PREF  

Page 15: libGDX: Screens, Fonts and Preferences

Persistent  Storage  

•  Simple  preferences  mechanism  for  storing  and  retrieving  

•  Windows,  Linux,  OS  X:  xml-­‐file  in  home  dir  – Windows:  %UserProfile%/.prefs/My Preferences – Mac:  ~/.prefs/My Preferences

•  On  Android,  the  system's  SharedPreferences  class  is  used.    –  Preferences  will  survive  app  updates,  but  are  deleted  when  the  app  is  uninstalled.

Page 16: libGDX: Screens, Fonts and Preferences

Usage  Preferences prefs = Gdx.app.getPreferences("MyPreferences");

prefs.putString("name", "Donald Duck");

String name = prefs.getString("name", "No name stored");prefs.putBoolean("soundOn", true);prefs.putInteger("highscore", 10);

prefs.flush();

Page 17: libGDX: Screens, Fonts and Preferences

file  <?xml version="1.0" encoding="UTF-8" standalone="no"?>

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

<properties>

<entry key="score">221</entry>

</properties>