Am i good student :D ? Hows the code? background drawing

Hey again;
First i want to say hi to reBirth,haha.
Anyway,im loving this forum. :stuck_out_tongue: anyway, college and work eats my time but im trying to learn java stuff nonetheless, anyway, i just wanted some judgment on this code, how can i improve it?

package mainLoop;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import models.Background;
import models.Plane;

/**
 *
 * @author André 
 */
public class ShooterGame implements ApplicationListener {

    // constant useful for logging
    public static final String LOG = ShooterGame.class.getSimpleName();
    // a libgdx helper class that logs the current FPS each second
    private FPSLogger fpsLogger;
    private Plane plane;
    private Background background;

    public ShooterGame() {
        plane = new Plane();
        background = new Background();
    }

    @Override
    public void create() {
        fpsLogger = new FPSLogger();
        
        //Start bg draw
        Texture backgroundTexture = background.getBackgroundTexture();
        drawBackground(backgroundTexture,512,512);
        //End bg draw
        
        Texture planeTexture = plane.getPlaneTexture();
        draw(planeTexture,plane.getxPos(),plane.getyPos());
    }

    @Override
    public void render() {
        fpsLogger.log();
        update();
       
        //the following code clears the screen with the given RGB color (green)
        Gdx.gl.glClearColor( 0f, 1f, 0f, 1f );
        Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT );
        
        //Start bg draw
        Texture backgroundTexture = background.getBackgroundTexture();
        drawBackground(backgroundTexture,512,512);
        //End bg draw
        
        Texture planeTexture = plane.getPlaneTexture();
        draw(planeTexture,plane.getxPos(),plane.getyPos());
    }

    public void update()
    {
        plane.setxPos(plane.getxPos() + 1);
        plane.setyPos(plane.getyPos() + 1);
    }
    
    
    public void drawBackground(Texture texture,int width,int height)
    {
        //Start bg draw
        Texture backgroundTexture = background.getBackgroundTexture();
        SpriteBatch bgBatch = new SpriteBatch();
        bgBatch.begin();
        bgBatch.draw(backgroundTexture,0,0,width,height);
        bgBatch.end();
        //End bg draw
    }
    
    public void draw(Texture texture,int xPos,int yPos)
    {
        Sprite sprite = new Sprite(texture);
        sprite.setX(xPos);
        sprite.setY(yPos);
        //
        SpriteBatch spriteBatch = new SpriteBatch();
        spriteBatch.begin();
        

        //
        sprite.draw(spriteBatch);
        spriteBatch.flush();
        spriteBatch.dispose();
        //
        spriteBatch.end();
        
    }
    
    
    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
    }
}

You create a s******d of avoidable object garbage.
Better reuse SpriteBatch and Sprite during each rendering pass.

Hi!
:stuck_out_tongue:
Is this better?

package mainLoop;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import models.Background;
import models.Plane;

/**
 *
 * @author André 
 */
public class ShooterGame implements ApplicationListener {

    // constant useful for logging
    public static final String LOG = ShooterGame.class.getSimpleName();
    // a libgdx helper class that logs the current FPS each second
    private FPSLogger fpsLogger;
    private Plane plane;
    private Background background;
    SpriteBatch spritebatch;
    public ShooterGame() {
        plane = new Plane();
        background = new Background();
        //spritebatch = new SpriteBatch(); // This gives Error
    }

    @Override
    public void create() {
        fpsLogger = new FPSLogger();
        
        //Start bg draw
        Texture backgroundTexture = background.getBackgroundTexture();
        drawBackground(backgroundTexture,512,512);
        //End bg draw
        
        Texture planeTexture = plane.getPlaneTexture();
        draw(planeTexture,plane.getxPos(),plane.getyPos());
    }

    @Override
    public void render() {
        fpsLogger.log();
        update();
       
        //the following code clears the screen with the given RGB color (green)
        Gdx.gl.glClearColor( 0f, 1f, 0f, 1f );
        Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT ); // Clear the Screen
        
        //Start bg draw
        Texture backgroundTexture = background.getBackgroundTexture();
        drawBackground(backgroundTexture,512,512);
        //End bg draw
        
        Texture planeTexture = plane.getPlaneTexture();
        draw(planeTexture,plane.getxPos(),plane.getyPos());
    }

    public void update()
    {
        plane.setxPos(plane.getxPos() + 1);
        plane.setyPos(plane.getyPos() + 1);
    }
    
    
    public void drawBackground(Texture texture,int width,int height)
    {
        //Start bg draw
        Texture backgroundTexture = background.getBackgroundTexture();
        spritebatch.begin();
        spritebatch.draw(backgroundTexture,0,0,width,height);
        spritebatch.end();
        //End bg draw
    }
    
    public void draw(Texture texture,int xPos,int yPos)
    {
        Sprite sprite = new Sprite(texture);
        sprite.setX(xPos);
        sprite.setY(yPos);
        //
        spritebatch.begin();
        

        //
        sprite.draw(spritebatch);
        spritebatch.flush();
        spritebatch.dispose();
        //
        spritebatch.end();
        
    }
    
    
    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
    }
}

You still create a new Sprite each frame. :slight_smile:

Ok, i will try to fix that but, since it doesnt let me initialize some objects before the create() is called, it really makes things harder.
You have any suggestions please ? >P

ty

Ok, that one was incorrect.
This one is.

Now i will mess with the sprites.

package mainLoop;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import models.Background;
import models.Plane;

/**
 *
 * @author André 
 */
public class ShooterGame implements ApplicationListener {

    // constant useful for logging
    public static final String LOG = ShooterGame.class.getSimpleName();
    // a libgdx helper class that logs the current FPS each second
    private FPSLogger fpsLogger;
    private Plane plane;
    private Background background;
    private SpriteBatch spritebatch;
    //private Sprite sprite;
    public ShooterGame() {
        plane = new Plane();
        background = new Background();
        //spritebatch = new SpriteBatch(); // This gives Error
        
    }

    @Override
    public void create() {
        fpsLogger = new FPSLogger();
       
        
        //Start bg draw
        Texture backgroundTexture = background.getBackgroundTexture();
        drawBackground(backgroundTexture,512,512);
        //End bg draw
        
        Texture planeTexture = plane.getPlaneTexture();
        draw(planeTexture,plane.getxPos(),plane.getyPos());
    }

    @Override
    public void render() {
        fpsLogger.log();
        update();
       
        //the following code clears the screen with the given RGB color (green)
        Gdx.gl.glClearColor( 0f, 1f, 0f, 1f );
        Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT ); // Clear the Screen
        
        //Start bg draw
        Texture backgroundTexture = background.getBackgroundTexture();
        drawBackground(backgroundTexture,512,512);
        //End bg draw
        
        Texture planeTexture = plane.getPlaneTexture();
        draw(planeTexture,plane.getxPos(),plane.getyPos());
    }

    public void update()
    {
        plane.setxPos(plane.getxPos() + 1);
        plane.setyPos(plane.getyPos() + 1);
    }
    
    
    public void drawBackground(Texture texture,int width,int height)
    {
        //Start bg draw
        spritebatch = new SpriteBatch();
        Texture backgroundTexture = background.getBackgroundTexture();
        spritebatch.begin();
        spritebatch.draw(backgroundTexture,0,0,width,height);
        spritebatch.end();
        //End bg draw
    }
    
    public void draw(Texture texture,int xPos,int yPos)
    {
        spritebatch = new SpriteBatch();
        Sprite sprite = new Sprite(texture);
        sprite.setX(xPos);
        sprite.setY(yPos);
        //
        spritebatch.begin();
        

        //
        sprite.draw(spritebatch);
        spritebatch.flush();
        spritebatch.dispose();
        //
        spritebatch.end();
        
    }
    
    
    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
    }
}

Yes. Now what could the create() method be for? :wink:


[...]
public class ShooterGame implements ApplicationListener {

[...]
    private Sprite sprite;
    private SpriteBatch spritebatch;

    public ShooterGame() {
        plane = new Plane();
        background = new Background();
    }

    @Override
    public void create() {
        fpsLogger = new FPSLogger();
        spritebatch = new SpriteBatch();
        Sprite sprite = new Sprite(texture);
        
        //Start bg draw
        Texture backgroundTexture = background.getBackgroundTexture();
        //Don't draw in create(), draw in render(), as you already do it:
        // drawBackground(backgroundTexture,512,512);
        //End bg draw
        
        Texture planeTexture = plane.getPlaneTexture();
        draw(planeTexture,plane.getxPos(),plane.getyPos());
    }
[...]
    public void draw(Texture texture,int xPos,int yPos)
    {
        sprite.setX(xPos);
        sprite.setY(yPos);
        //
        spritebatch.begin();
        

        //
        sprite.draw(spritebatch);
        spritebatch.flush();
        spritebatch.dispose();
        //
        spritebatch.end();
    }
[...]
}

I thought about that… but i had already posted. Sorry for my stupidity. :stuck_out_tongue:

If you really want to learn how OpenGL works, don’t use LibGDX. But it sounds that you want to get done quickly, so LibGDX may be a good choice in this case for you …

You can make it simpler by using Actor and encapsulate draw inside it.

heya.
Do you you have any example?
:stuck_out_tongue:

You still creating a new spritebatch every draw call. Initialize it once and use it twice. Maybe you could keep it in the Create method. Or in the draw method, use


if (spritebatch == null)
    spritebatch = new SpriteBatch();

Actor is really easy to use if you understand encapsulation. It has position and size getters and all you need to concern are act() and render() method.