Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (568)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Atlas Bug. When i Draw my Image, it draw all the images  (Read 802 times)
0 Members and 1 Guest are viewing this topic.
Offline Andre Lopes
« Posted 2013-05-19 12:12:44 »

Hi Guys. Im having a little trouble with my Atlas class.
I tried solving by myself but, still NO-OK.

What happens, is that, when i ask to draw anything on screen, it draws all the images in atlas together, side-by-side.

So i tried to remove the static method and pass it as a parameter...

So i did this :

SpriteManager class creates the Atlas Class.

Plane Class Receives SpriteManager as a paramater.


Well, i will post those 3 classes involved. I would love if someone can give me a hand... I starting to think in making without Atlas idk...

So, if anyone could have a look :


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
package Atlas;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.PixmapPacker;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.math.Rectangle;
import java.util.HashMap;

public class Atlas {
        private TextureAtlas atlas ;
        private final PixmapPacker packer = new PixmapPacker(512, 512, Format.RGBA8888, 0, false);
        private final HashMap<String, AtlasRegion> regions = new HashMap<>();
        private final HashMap<String, Rectangle> rectangles = new HashMap<>();
         
        public void generateAtlas()
        {
               
                packer.pack("plane", new Pixmap(Gdx.files.internal("planeTR.png")));
                packer.pack("missileArthur", new Pixmap(Gdx.files.internal("missileArthurTR.png")));
                packer.pack("enemyOranglePlane", new Pixmap(Gdx.files.internal("enemyOrangePlane.jpg")));
 
                atlas = packer.generateTextureAtlas(TextureFilter.Nearest, TextureFilter.Nearest, true);
               
                for (AtlasRegion r : atlas.getRegions()) {
                    regions.put(r.name, r);
                    rectangles.put(r.name,getRepRectangle(r.getRegionWidth(),r.getRegionWidth()));
                }
               
        }
 
        public void dispose() {
                atlas.dispose();
                packer.dispose();
        }
 
        public AtlasRegion get(String name) {
                return regions.get(name);
        }
       
        //Recupera os retangulos para usar na colisao
       public Rectangle getRectangle(String name,int posX,int posY)
        {
            Rectangle rectGet = rectangles.get(name);
            rectGet.setX(posX);
            rectGet.setY(posY);
           
            return rectGet;
        }
       
        //Guarda os retangulos na Lista
       private Rectangle getRepRectangle(int width,int height)
        {
           Rectangle rect = new Rectangle(0,0,width,height);
           return rect;
        }
}




1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
package Atlas;

import View.ShooterGame;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;

/**
 *
 * @author André Vinícius Lopes
 */

public class SpriteManager {

    private Atlas atlas;
    private Texture explosionTexture;
    private ExplosionModel explosionModel;

    private ShooterGame shooterGame;
   
    public SpriteManager(ShooterGame sg) {
        atlas = new Atlas();
        //
       atlas.generateAtlas();
        explosionTexture = new Texture(Gdx.files.internal("Explosion2spritesheet.png"));
        //
       explosionModel = new ExplosionModel(this);
        //
       shooterGame = sg;
    }

    public Atlas getAtlas() {
        return atlas;
    }

    public Texture getExplosion() {
        return explosionTexture;
    }

    public ExplosionModel getExplosionModel() {
        return explosionModel;
    }
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
package Models;

import Atlas.SpriteManager;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.TextureData;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.Rectangle;


/**
 *
 * @author André Vinícius Lopes
 */

public class Plane {

    private int xPos, yPos;

    private Texture planeTexture;
   
    private int TEXTURE_WIDTH,TEXTURE_HEIGHT;
   
    private Rectangle planeCollisionRectangle;
   
    public int MOVE_RATE = 2;
   
    public Plane(SpriteManager sm) {
     
        planeTexture = sm.getAtlas().get("plane").getTexture();
        TEXTURE_HEIGHT = planeTexture.getHeight();
        TEXTURE_WIDTH = planeTexture.getWidth();
       
        xPos = 5;
        yPos = 8;
        planeCollisionRectangle = new Rectangle(xPos,yPos,TEXTURE_WIDTH,TEXTURE_HEIGHT);
    }

   
    public void render(SpriteBatch sb)
    {
      sb.draw(planeTexture, xPos, yPos);
    }
   
    public void moveRight()
    {
      xPos = xPos + MOVE_RATE;
     
      if(xPos > Gdx.graphics.getWidth())
      {
          xPos = - TEXTURE_WIDTH + MOVE_RATE;
      }
     
    }
   
    public void moveLeft()
    {
        xPos = xPos - MOVE_RATE;
       
        if(xPos < 0)
        {
         xPos = Gdx.graphics.getWidth() - MOVE_RATE;
        }
       
    }
   
   
   
   
    public int getTEXTURE_WIDTH() {
        return TEXTURE_WIDTH;
    }

    public int getTEXTURE_HEIGHT() {
        return TEXTURE_HEIGHT;
    }

    public Rectangle getPlaneCollisionRectangle() {
        planeCollisionRectangle.x = (xPos);
        planeCollisionRectangle.y = (yPos);
        return planeCollisionRectangle;
    }

    public Texture getPlaneTexture() {
        return planeTexture;
    }

    public int getxPos() {
        return xPos;
    }

    public void setxPos(int xPos) {
        this.xPos = xPos;
    }

    public int getyPos() {
        return yPos;
    }

    public void setyPos(int yPos) {
        this.yPos = yPos;
    }
}



Heres the Bug :


Offline Andre Lopes
« Reply #1 - Posted 2013-05-19 12:13:39 »

Ah, i forgot this Class :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
package View;


import Atlas.Explosion;
import Atlas.SpriteManager;
import Logic.GameUpdate;
import Models.Plane;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import java.awt.Font;
import java.util.ArrayList;


/**
 *
 * @author André Vinícius Lopes
 */

public class ShooterGame implements ApplicationListener {

    private SpriteBatch spritebatch;
    private BitmapFont font;
    private SpriteManager spriteManager;
    private GameUpdate gameUpdate;
   
    private Plane plane;
   
   
    @Override
    public void create()
    {
       spriteManager = new SpriteManager(this);
       
       spritebatch = new SpriteBatch();
       
       gameUpdate = new GameUpdate(this);
       gameUpdate.addNewExplosion(0,0);
       font = new BitmapFont();
       
       //
      plane = new Plane(spriteManager);
       
    }

    @Override
    public void render() {
        gameUpdate.update();
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
     
        //START DRAW//
       spritebatch.begin();
        drawExplosions();
        plane.render(spritebatch);
       
        //End Draw ::
       spritebatch.flush();
        spritebatch.end();
        //END DRAW//
   }


    private void drawExplosions()
    {
        ArrayList<Explosion> explosions = gameUpdate.getExplosions();
        for(int i = 0; i < explosions.size();i++)
        {
         explosions.get(i).render(spritebatch);
        }
    }
 



    //////////////////////////////////////////////////////////////////
   //////////////////////////////////////////////////////////////////
   //////////////////////////////////////////////////////////////////
   @Override
    public void resize(int width, int height) {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
    }
   
    /////////////////////////////////////////////////
   public SpriteBatch getSpritebatch() {
        return spritebatch;
    }

    public SpriteManager getSpriteManager() {
        return spriteManager;
    }
   
   
}
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 133
Projects: 4
Exp: 3 years



« Reply #2 - Posted 2013-05-19 14:43:05 »

Looks like it's because all your plane images have the word "plane" in their names, so
1  
planeTexture = sm.getAtlas().get("plane").getTexture();
is getting all the plane images and stuffing it into one texture...not absolutely sure but that's what it looks like. Instead, better to load the default sprite
1  
sm.getAtlas().get("plane/default").getTexture()
and then just get different entries from the Atlas when needed.
1  
Texture planeLeft = sm.getAtlas().get("plane/left").getTexture()
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Andre Lopes
« Reply #3 - Posted 2013-05-19 15:46:37 »

yay ty for the reply.

But what about the missileArthur.
It doesnt have plane on it Tongue

I will try what u said.
Offline Andre Lopes
« Reply #4 - Posted 2013-05-19 17:53:51 »

Same error.
I noticed that, if i make this value = to the plane img size, it stops happening...

1  
  private  PixmapPacker packer = new PixmapPacker(64,64, Format.RGBA8888, 0, false);


So i better not use Atlas. I dont trust it Tongue

Or maybe im doing something wrong, but idk what.
Offline davedes
« Reply #5 - Posted 2013-05-26 17:45:23 »

The problem is that you are drawing a Texture object (planeTexture), which renders the whole texture atlas. Instead, you need to draw a region, i.e. the region you got from the TextureAtlas.

Learn about the differences between pixmaps, textures, and texture regions here:
https://github.com/mattdesl/lwjgl-basics/wiki/LibGDX-Textures

Offline Andre Lopes
« Reply #6 - Posted 2013-05-26 18:08:23 »

Oh! ok i printed that tutorial, gonna read it!

Thanks man Cheesy
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (40 views)
2014-09-24 16:13:29

Pippogeek (31 views)
2014-09-24 16:12:22

Pippogeek (21 views)
2014-09-24 16:12:06

Grunnt (47 views)
2014-09-23 14:38:19

radar3301 (29 views)
2014-09-21 23:33:17

BurntPizza (65 views)
2014-09-21 02:42:18

BurntPizza (37 views)
2014-09-21 01:30:30

moogie (44 views)
2014-09-21 00:26:15

UprightPath (53 views)
2014-09-20 20:14:06

BurntPizza (55 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!