java - Understanding libgdx -


i understand framework; more open-source cross-platform game development library. went libgdx homepage , followed instruction on video tutorial. after setting project able run default my-gdx-game project on multiple supported platforms. great, fine , dandy...now what?

i have been scouring forums, wikis, javadocs, , many many more sites looking decent straightforward how-to's. unfortunately couldn't find any, of out there assumes have basic knowledge of library.

i feel video tutorial showed me how set project correctly, getting feet wet, assumed know how swim, , left me 300 miles out sea or something. i'm having trouble digesting library, because started using yesterday, i'm complete newcomer when comes libgdx.

i want move existing projects on libgdx, i'm use bufferedimages, jframes , things that. experienced veterans nice.

by way posted core project below, guys can walk me through going on here...

<code> package com.me.mygdxgame;  import com.badlogic.gdx.applicationlistener; import com.badlogic.gdx.gdx; import com.badlogic.gdx.graphics.gl20; import com.badlogic.gdx.graphics.orthographiccamera; import com.badlogic.gdx.graphics.texture; import com.badlogic.gdx.graphics.texture.texturefilter; import com.badlogic.gdx.graphics.g2d.sprite; import com.badlogic.gdx.graphics.g2d.spritebatch; import com.badlogic.gdx.graphics.g2d.textureregion;  public class mygdxgame implements applicationlistener {     private orthographiccamera camera;     private spritebatch batch;     private texture texture;     private sprite sprite;      @override     public void create() {          float w = gdx.graphics.getwidth();         float h = gdx.graphics.getheight();          camera = new orthographiccamera(1, h/w);         batch = new spritebatch();          texture = new texture(gdx.files.internal("data/libgdx.png"));         texture.setfilter(texturefilter.linear, texturefilter.linear);          textureregion region = new textureregion(texture, 0, 0, 512, 275);          sprite = new sprite(region);         sprite.setsize(0.9f, 0.9f * sprite.getheight() / sprite.getwidth());         sprite.setorigin(sprite.getwidth()/2, sprite.getheight()/2);         sprite.setposition(-sprite.getwidth()/2, -sprite.getheight()/2);          }      @override     public void dispose() {         batch.dispose();         texture.dispose();     }      @override     public void render() {               gdx.gl.glclearcolor(1, 1, 1, 1);         gdx.gl.glclear(gl20.gl_color_buffer_bit);          batch.setprojectionmatrix(camera.combined);         batch.begin();         sprite.draw(batch);         batch.end();     }      @override     public void resize(int width, int height) {     }      @override     public void pause() {     }      @override     public void resume() {     } } </code> 

if want port javax.swing game libgdx have know how made game. did separate logic , view? if yes, fine have rewrite view. if not, well, maybe better start scratch. first basics know: entrypoint game applicationlistener interface. if don't need every method inside it, extend game, gives deffault behaivor. suggest use that. game or applicationlistener gives possibility react on application events:

  1. create(): called when app started
  2. dispose(): called when game closed (not crashed :p)
  3. pause(): called on android, when call incoming/home button pressed. on desktop when minimize window/when game lost focus.
  4. resume(): called when come after pause state.
  5. resize(): called when resize window , on android, when screen rotates (you have allow in manifest file)
  6. render(): called every gameloop (if enabled), used update objects in game , draw them screen.

the default render() implementation in game class calles render current screen.

screen? whats that? think normal game. start , showes menu, buttons start game, settings... menu screen. screens parts of game different logic , view, have in sepparate classes. screens implement screen interface, again gives useful methods:

  1. show(): called, when set screen games screen mygdxgame.setscreen().
  2. hide(): called, when screen set games screen.

so if current screen menuscreen , press play game button, show() playscreen called , hide() menuscreen called. screen has methods pause(), resume() , resize(), called if same methods in game called (always current screen). if implement applicationlistener have write default behaivor yourself.
note, dispose() screen not called automatically.

next thing drawing. drawing in 2d there the:

  1. shaperenderer, times used debug only. can render simple shapes circles, squares...
  2. spritebatch, used render textures sprites...

so times render using spritebatch. render in block made of:

  1. spritebatch.begin(), starts spritebatch. set viewport, matrices... before call
  2. spritebatch.draw(), draws thing give parameter (there many different draw() methods, @ api) buffer, , flush() them gpu full or spritebatch.end() called.
  3. spritebatch.end(), flush() buffer gpu, drawn on screen.

notes:

  1. never have 2 spritebatches in begin state @ once. before begin()ing spritebatch call end() on other running spritebatch
  2. if possible use 1 spritebatch , try call end() once per render loop, cost performance.
  3. don't call draw() outside begin-end block.

last not least "tutorial": camera. helps calculating movements... in own worldunits instead of pixels. can use show different parts of gameworld moving arround. first: set camerasviewport in construcotr:

camera = new orthographiccamera(16, 9); 

this sets viewport of 16 width , 9 height. physical screen/monitor 16 units width , 9 units heigh. apply camera spritebatch call:

`spritebatch.setprojectionmatrix(cam.combined)` 

by doing spritebatch renders things camera looking at. cameras 0,0 point deffault in middle. draw in middle use:

batch.draw(yourtexture, 0, 0); 

to draw in upper right corner use:

 batch.draw(yourtexture, 16 / 2, 9 / 2); 

to move camera use:

cam.translate(16/2, 9/2); 

make sure call cam.update() after change position!

now right upper corner of camera @ 16,9 instead of 16/2, 9/2. , 0,0 point left lower corner.

i hope not complicated begining. tutorials, helped me learn:

  1. starassault
  2. steigert's blog

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -