I'm new in libgdx and I'm trying to do some tutorials. I have problem with loading my own .png file.
Standard project contains libgdx.png file and it works. When I run it on desktop or android it works fine. Images display well.
The same code, just another file (also png).
Works fine on desktop version, but it cannot run on android.
I really dont understand what happens, because files (libgdx.png and my.png) are in the same folder and first one works on android but the second one doesnt...
public class AwesomeGame implements ApplicationListener {
SpriteBatch batch;
Texture man;
Texture abc;
Vector2 position;
#Override
public void create() {
batch = new SpriteBatch();
man = new Texture(Gdx.files.internal("my.png"));
position = new Vector2(50,50);
}
#Override
public void dispose() {
}
#Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
if(Gdx.input.isKeyPressed(Input.Keys.W)){
position.y+=1f;
}
if(Gdx.input.isKeyPressed(Input.Keys.S)){
position.y-=1f;
}
if(Gdx.input.isKeyPressed(Input.Keys.D)){
position.x+=1f;
}
if(Gdx.input.isKeyPressed(Input.Keys.A)){
position.x-=1f;
}
if(Gdx.input.getAccelerometerX()!=0){
position.x-=Gdx.input.getAccelerometerX();
}
if(Gdx.input.getAccelerometerY()!=0){
position.y+=Gdx.input.getAccelerometerY();
}
batch.setColor(position.x,position.y,10,10);
batch.begin();
batch.draw(man, position.y, position.x);
batch.end();
}
Can someone explain me that ? ;(
omg, I got it.
Texture width and height must be a power of 2.
but if I set
cfg.useGL20 = true;
everything works without power of 2.
Related
I am having a problem in with this part of the code, I am creating a very simple game in Java with the implementation of LibGDX.
I have three different game states that are handled in three different classes:
StateCreation
MenuState
PlayState
They all work perfectly except for one thing. The music that I have inserted and that is created in the StateCreation should start over, when the player gets to the gameover and the state changes the mode to start over or go back to the menu. This does not happen and the music overlaps
public class GameBarbo extends ApplicationAdapter {
//impostazioni base dell'applicazione
public static final int WIDTH = 480;
public static final int HEIGHT = 800;
public static final String TITLE = "Cavaliere";
private GameStateManager gsm = new GameStateManager();
private SpriteBatch batch;
private Music music;
#Override
public void create () {
batch = new SpriteBatch();
music = Gdx.audio.newMusic(Gdx.files.internal("sound.mp3"));
music.setVolume(0.1f);
music.play();
Gdx.gl.glClearColor(1, 0, 0, 1);
gsm.push(new MenuState(gsm));
}
#Override
public void render () {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gsm.update(Gdx.graphics.getDeltaTime());
gsm.render(batch);
}
#Override
public void dispose() {
super.dispose();
music.dispose();
}
}
You code looks very much like what I found in this guide (though they don't include super.dispose().)
Is it possible that when your game state changes, the dispose() method does not execute? I haven't used libgdx in ages, but it could be the case that dispose() only executes when you exit the program.
I see from this wiki that the Music class has a stop() method. Perhaps that needs to be called directly when the game ends. But IDK if stopping the file and executing a reload for another iteration is asking for a memory leak or not.
Hopefully someone with more experience will answer, but maybe these thoughts will help you figure something out in the meantime.
I have seen it done in libgdx projects before so I know it is possible. I would like to display the FPS of the game as the title of the application. I just can't seem to figure out how to do this as it seems the title of my game is always static. It always says 0. Any help is extremely appreciated.
use bitmapfont to display your FPS
BitmapFont font;
#Override
public void create() {
font = new BitmapFont();
batch = new SpriteBatch();
}
#Override
public void render() {
//OpenGL settings
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
batch.begin();
Gdx.graphics.setTitle(""+Gdx.graphics.getFramesPerSecond());
batch.end();
}
if any trouble leave a comment ! enjoy :)
Updated
I suppose you can add in your render/controller method something like this:
Gdx.graphics.setTitle("your fps");
I'm making a splash screen (for my game) using LibGDX.
I'm using a .png format images, and with few of them - it works (my images are properly located in the assets folder)
I'm getting this error with few of my images I want to use:
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: devlogo.png
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:140)
at com.badlogic.gdx.graphics.TextureData$Factory.loadFromFile(TextureData.java:98)
at com.badlogic.gdx.graphics.GLTexture.createTextureData(GLTexture.java:185)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:103)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:95)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:91)
at com.GunRun.Screens.Splash.show(Splash.java:34)
at com.badlogic.gdx.Game.setScreen(Game.java:61)
at com.mynewgame.game.GunRun.create(GunRun.java:16)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:143)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
Caused by: java.io.IOException: Error loading pixmap: decoder init failed for stream
at com.badlogic.gdx.graphics.g2d.Gdx2DPixmap.<init>(Gdx2DPixmap.java:57)
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:138)
... 10 more
Code from splash screen class:
public class Splash implements Screen{
private Sprite devlogo;
private SpriteBatch batch;
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
devlogo.draw(batch);
batch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void show() {
batch = new SpriteBatch();
Texture texture = new Texture("devlogo.png");
devlogo = new Sprite(texture);
devlogo.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
}
}
The images with which works has 24 bit depth, and others who doesn't has 64 bit depth, can be there a problem?
In my case I had to save image 8bit/channel instead of 16bit/channel.
I hit this problem recently too, so I did some digging into where that error message comes from. The error comes from this line in jpgd.cpp:
jpeg_decoder decoder(pStream);
if (decoder.get_error_code() != JPGD_SUCCESS) {
err_reason = "decoder init failed for stream";
return NULL;
}
The interesting thing there is that since these are PNGs, it's pretty suspect that it's trying to load the image as a JPG. It turns out that will happen here if LibGDX fails to load the image into memory on the first try:
const unsigned char* pixels = stbi_load_from_memory(buffer, len, &width, &height, &format, 0);
if (pixels == NULL) {
pixels = jpgd_decompress_jpeg_image_from_memory(buffer, len, &width, &height, &format, 3);
}
It turned out that I was hitting this because I had a massive memory leak in part of my game, so LibGDX was simply failing to allocate memory to load the image. Fixing the memory leak corrected the problem for me.
Since this is happening on your splash screen, I'm guessing a memory leak isn't the cause of your issue, but it might be worth checking that you have enough memory allocated for the application.
Simple, Change the RGB profile with any photo editor (I used photoshop).
When you go to save the image, just exclude the previous RGB profile.
I'm not entirely sure what I'm doing wrong. I have written this code multiple times before without problems. The problem is that the physics world just does not seem to be updating and I don't know why! Right now I have a gameScene class which extends a normal scene, this is the onCreate method:
#Override
public void create() {
ParallaxBackground background = new ParallaxBackground(0, 0, 0);
background.attachParallaxEntity(new ParallaxEntity(0, new Sprite(0, 0, res.BasicBackground, res.vbom)));
this.setBackground(background);
test = new Sprite(0, 0, res.GrassRegion, res.vbom);
FixtureDef fix = PhysicsFactory.createFixtureDef(10f, 0f, 10f);
Body bod = PhysicsFactory.createBoxBody(physicsWorld, test, BodyType.DynamicBody, fix);
attachChild(test);
physicsWorld = new PhysicsWorld(new Vector2(SensorManager.GRAVITY_EARTH, SensorManager.GRAVITY_EARTH), false);
physicsWorld.registerPhysicsConnector(new PhysicsConnector(test, bod, true, true));
this.registerUpdateHandler(physicsWorld);
}
(I removed a lot of the unnecessary things) I just have a basic test sprite to make sure that the physics is working (which it is not). The app runs without any errors, its just that the sprite will not move, leading me to think that its a problem with the physics world being update.
As you can see, I'm setting the gravity to be accelerating in both the X and Y directions. I did this because I thought maybe it wasn't working in one direction for some reason. I'm thinking that maybe I have to set the scene to update in the main activity? But I don't remember having to do that with any of my other apps. Any help is appreciated thank you :)
No, this.registerUpdateHandler(physicsWorld); will do just fine. Try adding this:
physicsWorld = new PhysicsWorld(new Vector2(0, AHC.WORLD_GRAVITY), false) {
#Override
public void onUpdate(float pSecondsElapsed) {
super.onUpdate(pSecondsElapsed);
}
};
And put a breakpoint or Log.i(TAG, "hey"); inside onUpdate to check if it hits it
EDIT:
Try registering a "normal" UpdateHandler, you can do it in 2 ways:
public class YourGameActivity implements IUpdateHandler{
...
private void yourSetupMethod(){
registerUpdateHandler(this);
}
#Override
public void reset() {}
#Override
public void onUpdate(float pSecondsElapsed) {
Log.i(TAG, "hey");
}
}
or you can create a class that implements IUpdateHandler and then:
public class YourGameActivity{
private MyClassThatImplementsIUpdateHandler mInstance;
...
private void yourSetupMethod(){
registerUpdateHandler(mInstance);
}
}
It's the same.
Try it and see if it logs something, if for some reason you can't see the log use a breakpoint just to be sure.
I'm developing a Tower Defense game using libGDX. I've just started and I am able to display the path, the "environment" and the enemies walking along the path.
I displayed the environment using a SpriteBatch-Object, like this:
public LevelController(Level level) {
spriteBach = new SpriteBach();
atlas = new TextureAtlas(Gdx.files.internal("images/textures/textures.pack"));
}
public void setSize() {
spriteBatch.setProjectionMatrix(this.cam.combined);
}
public void render() {
spriteBatch.begin();
drawTowerBases();
spriteBatch.end();
}
private void drawTowerBases() {
// for each tower base (=environment square)
TextureRegion towerBaseTexture = atlas.findRegion("TowerBase");
if(towerBaseTexture != null) {
spriteBatch.draw(towerBaseTexture, x, y, 1f, 1f);
}
}
This is working properly and the textures are displayed well: Tower Defense using spriteBatch
Now, I was wondering if it is possible to cache the background. Because it stays the same, there is no need to calculate it every time. I've found the SpriteCache through Google-Search. So I changed the code as follows:
public LevelController(Level Level) {
spriteCache= new SpriteCache();
atlas = new TextureAtlas(Gdx.files.internal("images/textures/textures.pack"));
spriteCache.beginCache();
this.drawTowerBases();
this.spriteCacheEnvironmentId = spriteCache.endCache();
}
public void setSize() {
this.cam.update();
spriteCache.setProjectionMatrix(this.cam.combined);
}
public void render() {
spriteCache.begin();
spriteCache.draw(this.spriteCacheEnvironmentId);
spriteCache.end();
}
private void drawTowerBases() {
// for each tower base (=environment square)
TextureRegion towerBaseTexture = atlas.findRegion("TowerBase");
if(towerBaseTexture != null) {
spriteCache.add(towerBaseTexture, x, y, 1f, 1f);
}
}
And now the game looks like this: Tower Defense using spriteCache
For me it seems as if the transparency is not rendered properly. If I take images without transparency, everything works fine. Does anyone have an idea, why this happens and how I can fix this?
Thank you in advance.
Taken from the SpriteCache documentation:
Note that SpriteCache does not manage blending. You will need to enable blending (Gdx.gl.glEnable(GL10.GL_BLEND);) and set the blend func as needed before or between calls to draw(int).
I guess there is not much more to say.