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:
create()
: called when app starteddispose()
: called when game closed (not crashed :p)pause()
: called on android, when call incoming/home button pressed. on desktop when minimize window/when game lost focus.resume()
: called when come after pause state.resize()
: called when resize window , on android, when screen rotates (you have allow in manifest file)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. screen
s parts of game different logic , view, have in sepparate classes. screen
s implement screen
interface, again gives useful methods:
show()
: called, when setscreen
game
sscreen
mygdxgame.setscreen()
.hide()
: called, whenscreen
setgame
sscreen
.
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:
shaperenderer
, times used debug only. can render simple shapes circles, squares...spritebatch
, used rendertexture
ssprite
s...
so times render using spritebatch
. render in block made of:
spritebatch.begin()
, starts spritebatch. set viewport, matrices... before callspritebatch.draw()
, draws thing give parameter (there many different draw() methods, @ api) buffer, ,flush()
them gpu full orspritebatch.end()
called.spritebatch.end()
,flush()
buffer gpu, drawn on screen.
notes:
- never have 2 spritebatches in
begin
state @ once. beforebegin()
ing spritebatch callend()
on other running spritebatch - if possible use 1 spritebatch , try call
end()
once per render loop, cost performance. - 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:
Comments
Post a Comment