1
votes

This is from day 6 of the flappy bird recreation tutorial -http://www.kilobolt.com/day-6-adding-graphics---welcome-to-the-necropolis.html

Here is the image file i am using for texture in my game. It is a 256px x 64px .png file.

enter image description here

Here is the class that I used for loading the texture and the specific TextureRegion(part of the texure) that I want the SpriteBatch to draw.

    public class AssetLoader {
          public static Texture texture;
          public static TextureRegion bg;

          public static void load() {
                  texture = new Texture(Gdx.files.internal("data/texture.png"));
                  bg = new TextureRegion(texture, 0, 0, 136, 43);
          }

   }
      And I call AssertLoader.load(), along with setting up game screen from
     public class MyGdxGame extends Game{
          @Override
         public void create() {
                  AssetLoader.load();
                 setScreen(new GameScreen());
         }
      }

      And inside GameScreen.java
      public class GameScreen implements Screen {
           //delegate render task
           private GameRenderer renderer;
          public GameScreen() {
               float screenHeight = Gdx.graphics.getHeight();
               float screenWidth = Gdx.graphics.getWidth();
               float gameWidth = 136;
               float gameHeight = screenHeight / (screenWidth / gameWidth);
               renderer = new GameRenderer((int)gameHeight);
           }
       }
      And inside GameRederer, the class I delegate to render the game
      public class GameRenderer {
            private int gameHeight;
            private SpriteBatch batch;
            private OrthographicCamera cam;
            public GameRenderer(int gameHeight) {
                   this.gameHeight = gameHeight;
                  cam = new OrthographicCamera();
                   batch = new SpriteBatch();
                   batch.setProjectionMatrix(cam.combined);
                    cam.setToOrtho(true, 136, gameHeight);
            }
             public void render() {
                  Gdx.gl.glClearColor(0, 0, 0, 1);
                  Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
                  batch.begin();
                  batch.disableBlending();
                  batch.draw(AssetLoader.bg, 0, (gameHeight/2) + 23, 136, 43);
                  batch.end()
             }
        }

enter image description here

What I get when I run the desktop version of the game is the black screen shown above(black because i set the background to black with these lines of code

  Gdx.gl.glClearColor(0, 0, 0, 1);
  Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

Does anyone know why the SpritchBatch drawing isn't showing up? I extracted the texture portion of the texture I wanted with this line of code(starts from 0,0, width of 136, height of 43) - used GIMP - to find out portion to cut.

  bg = new TextureRegion(texture, 0, 0, 136, 43);

I also put a few log statements(removed them from view) to ensure that before drawing, bg's width and height were set correctly, which they were. And the issue can't be game height because I used a print statement and found that to be 204 which means that this expression, (gameHeight/2) + 23 will evaluate to 125 which is in bounds between 0 and game height.

I checked out other threads as well. My issue can't be libgdx spritebatch not rendering textures because the SpriteBatch should overwrite the background. And it can't be LibGDX: Android SpriteBatch not drawing because i am running mine on desktop, not andorid.

2
Ok, let me give you some general advice. I suggest you to read "Learning libGDX Game Development". It's nice book for beginners and it will give you basic knowledges about libgdx. If you don't have money you know what to do, right? Did you also tried to download source code from that tutorial? Maybe you missed something. And this is my source code github.com/nikoliazekter/JustSimpleTD . It can help you.nikoliazekter
where are you calling gameRenderer.render()?matthewtory
grimdradar22, I know it's being called because the background is the black background that I specified.committedandroider
nikoliazekter, I think this tutorial series is great for beginners. But yeah, I downloaded the source code. I am still trying to find the discrepancycommittedandroider
Note that the tutorial is for an older version of libgdx than the current one. You would have to fix some other things along the way.dawez

2 Answers

2
votes

could be that you have to first put cam.setToOrtho(true, 136, gameHeight);before the batch, so I can not confirm hopefully help

 public GameRenderer(int gameHeight) {
                   this.gameHeight = gameHeight;
                  cam = new OrthographicCamera();
                   batch = new SpriteBatch();
                   cam.setToOrtho(true, 136, gameHeight);
                   batch.setProjectionMatrix(cam.combined);

            }
0
votes

If anyone's having a similar issue, the way I solved the problem was to call

        batch.setProjectionMatrix(cam.combined);

after

   cam.setToOrtho(true, 136, gameHeight);

Which didn't really make sense to me because it's still the same Matrix4 in Camera.java, that is

    public final Matrix4 combined = new Matrix4();

Was hoping someone else could clarify that.