introduction to libgdx

22
Intro to libGDX Jussi Pohjolainen Tampere University of Applied Sciences

Upload: jussi-pohjolainen

Post on 22-Apr-2015

214 views

Category:

Technology


3 download

DESCRIPTION

Introduction to libGDX

TRANSCRIPT

Page 1: Introduction to libGDX

Intro  to  libGDX  

Jussi  Pohjolainen  Tampere  University  of  Applied  Sciences  

Page 2: Introduction to libGDX

libGDX  and  Android  •  Streaming  music,  sound  effects  – Wav,  mp3,  ogg  

•  Audio  Recording  support  •  Accelerometer,  Compass,  Gestures  (taps,  pinch  zooming)  

•  High-­‐level  2D  APIs  –  Sprites,  Textures,  Fonts,  Tiles,  UI  -­‐  library  

•  High-­‐level  3D  APIs  •  UOliOes:  JSON,  XML,  File  I/O  (preferences),  Math..  

Page 3: Introduction to libGDX

Community  &  Support  

•  Forum  – http://badlogicgames.com/forum/  

•  IRC  – irc.freenode.net,  #libgdx  

•  Blog  – http://www.badlogicgames.com/wordpress/  

•  Twi0er  – https://twitter.com/badlogicgames  

Page 4: Introduction to libGDX
Page 5: Introduction to libGDX

App  Framework:  Applica'on  

•  Applica'on  –  main  entry  point  for  your  app  –  hZp://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/ApplicaOon.html  

•  Equivalent  to  JFrame  (Swing)  or  Activity  (Android)  •  Informs  your  game  about  events  such  as  window  resizing  

•  Developer  creates  a  class  that  implements  ApplicationListener,  methods  are  called  by  Application  –  ApplicaOon  can  be  GwtApplication,  IOSApplication  …  

Page 6: Introduction to libGDX
Page 7: Introduction to libGDX
Page 8: Introduction to libGDX
Page 9: Introduction to libGDX

public class MyGame extends ApplicationAdapter { SpriteBatch batch; Texture img; @Override public void create () { Debug.print("MyGdxGame", "create()", 1); batch = new SpriteBatch(); img = new Texture("badlogic.jpg"); }

@Override public void render () { Debug.print("MyGdxGame", "render()", 2);

Gdx.gl.glClearColor(1, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); batch.begin(); batch.draw(img, 0, 0); batch.end(); } @Override public void resize(int w, int h) { Debug.print("MyGdxGame", "resize(), w = " + w + "h = " + h, 1); }

}

Page 10: Introduction to libGDX

STARTER  CLASSES  

Page 11: Introduction to libGDX

About  Starter  Classes  

•  For  each  pla^orm  (iOS,  Android,  Desktop  ..)  a  starter  class  must  be  wriZen  

•  Starter  classes  are  pla^orm  dependent  •  We  will  focus  on    – Desktop  (LWJGL)  – Android  

Page 12: Introduction to libGDX

LWJGL?  

•  Lightweight  Java  Game  Library  (LWJGL)  framework  for  creaOng  games  with  Java  

•  libGDX  is  built  on  top  of  LWJGL  •  See  more:  – hZp://www.lwjgl.org/  

Page 13: Introduction to libGDX

Starter  Classes:  Desktop  // This is platform specific: Java SE public class DesktopStarter { public static void main(String[] argv) { LwjglApplicationConfiguration config = new LwjglApplicationConfiguration(); config.title = “…”; config.width = 480; config.heigth = 320; new LwjglApplication(new MyGame(), config); } }

Page 14: Introduction to libGDX

Starter  Classes:  Desktop  public class DesktopLauncher {

public static void main (String[] arg) {

LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();

MyGame game = new MyGame();

LwjglApplication app = new LwjglApplication(game, config);

if(app.getApplicationListener() == game) {

app.log("test", "success!");

}

}

}

Page 15: Introduction to libGDX

Starter  Classes:  Android  import android.os.Bundle;

import com.badlogic.gdx.backends.android.AndroidApplication;

import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;

// This is platform specific: Android

// No main

public class AndroidLauncher extends AndroidApplication {

@Override

protected void onCreate (Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();

MyGame game = new MyGame();

initialize(game, config);

if(this.getApplicationListener() == game) {

this.log("test", "success");

}

}

}

Page 16: Introduction to libGDX

Android  Manifest  <?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.mygdx.game.android" android:versionCode="1"

android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="20" />

<application

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/GdxTheme" >

<activity

android:name="com.mygdx.game.android.AndroidLauncher" android:label="@string/app_name"

android:screenOrientation="landscape"

android:configChanges="keyboard|keyboardHidden|orientation|screenSize">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

</manifest>

Page 17: Introduction to libGDX

Android  Permissions  

•  Add  permissions  if  your  android  app  requires  certain  funcOonality  –  <uses-permission android:name="android.permission.RECORD_AUDIO"/>

–  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

–  <uses-permission android:name="android.permission.VIBRATE"/>

•  Add  these  to  manifest  file

•  See  – hZp://developer.android.com/guide/topics/manifest/manifest-­‐intro.html#perms  

Page 18: Introduction to libGDX

Other  important  Modules  (Interfaces)  

•  ApplicaOon  –  Informs  your  game  about  events  

•  Files  –  Exposes  the  underlying  file  system  

•  Input  –  Mouse,  keyboard,  touch,  accelerometer  

•  Net*  –  Access  HTTP(S)  

•  Audio  –  Enables  sound  

•  Graphics  –  Exposes  OpenGL  ES  2.0  where  available  

Page 19: Introduction to libGDX

Accessing  Modules  

•  All  modules  (previous  slide)  can  be  accessed  via  staOc  fields  of  Gdx  –  class  – Global  public  variables  for  easy  access  

•  Example  – AudioDevice audioDevice = Gdx.audio.newAudioDevice(44100, false);

Page 20: Introduction to libGDX

Javadoc:  Gdx  

Gdx.audio  (Interface)  is  a  reference  to  the  backend  implementaOon  that  has  been  instanOated  on  

applicaOon  startup  by  the  ApplicaOon  instance.    

Page 21: Introduction to libGDX

Javadoc:  Audio  

Page 22: Introduction to libGDX

Querying  

•  ApplicaOon  (Gdx.app)  interface  provides  various  methods  to  query  properOes  – If(Gdx.app.getType() == Android) { … }