Drawing a Filled rectangle using Pixmap in libGDX?

hey guys, I am pretty new to LibGDX and also to Over ALL programming yet i consider my self intermediate in programming. I started exploring LibGDX a few days ago, and read theory, along with some practice of creating shapes and running some loops. So I decided to code a simple PONG game to see how things work. I have created a Ball using Pixmap, and its randomly moving on the screen colliding with the ends of the screen etc, so i tried to make a Paddle using fill rectangle and new Pixmap but its not working, i mean if i ma increasing its width its increasing fine but if i am increasing its height its not working.

Is it okay to use 2 Pixmaps in a game? I know pixmaps a pretty slow but for a beginner like me isn’t it a good way to know the basics?

is there any other way i can draw shapes more efficiently ?

PS: Pardon me for my bad english, Its not my first language. Thanks.

Below is the code: Any suggestions about improving the writing style would also be appreciated :slight_smile:

package com.me.mygdxgame;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GLCommon;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap;
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;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Vector2;

public class MyGdxGame implements ApplicationListener {
	
	public static int SCREEN_WIDTH = 800;
	public static int SCREEN_HEIGHT = 480;
	
   SpriteBatch batch;
   Texture texture;
   Pixmap pixmap;
   Vector2 Ball_pos;
   int SpeedX = 5;
   int SpeedY = 5;
   int rev = -1;
   Texture T2;
   Pixmap Paddle;
   Vector2 Paddle_pos;
   ShapeRenderer shape;
   @Override
	public void create() {		
	    SCREEN_WIDTH = Gdx.graphics.getWidth();
		SCREEN_HEIGHT = Gdx.graphics.getHeight();
		
		
		batch = new SpriteBatch();
		pixmap = new Pixmap(256,256,Pixmap.Format.RGBA8888);
		texture = new Texture(pixmap);
		Paddle = new Pixmap(256,256,Pixmap.Format.RGBA8888);
		T2 = new Texture(Paddle);
		Ball_pos = new Vector2(SCREEN_WIDTH/2-25,SCREEN_HEIGHT-190);
		Paddle_pos = new Vector2(10, SCREEN_HEIGHT/2);
		shape = new ShapeRenderer();
		drawBall();
		drawPaddle();
		
	}
	
	public void drawBall(){
		
		Gdx.app.log("MyGdxGame", "drawSmily()");
		pixmap.setColor(1, 1, 0, 1);
		pixmap.fillCircle(256/2, 256/2, 20);
		
		
		texture.draw(pixmap, 0, 0);
		texture.bind();
		
		
		
		
	}
	
	public void drawPaddle(){
		
		Gdx.app.log("Simple Pong", "Paddle()");
	   Paddle.setColor(Color.WHITE);
	   Paddle.fillRectangle((int)Paddle_pos.x, (int)Paddle_pos.y, 20, 122);
	   
	   T2.draw(Paddle, 0, 0);
	   T2.bind();
	   
	}

	@Override
	public void dispose() {
		Gdx.app.log("MyGdxGame", "Dispose()");
		batch.dispose();
		texture.dispose();
		pixmap.dispose();
		Paddle.dispose();
	}

	@Override
	public void render() {		
		
		float deltatime = Gdx.graphics.getDeltaTime();
		
		GLCommon gl = Gdx.gl;
		gl.glClearColor(0, 0, 0,1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT | GL10.GL_STENCIL_BUFFER_BIT);
		
		
		batch.begin();
		batch.draw(texture, Ball_pos.x, Ball_pos.y);
		batch.draw(T2, Paddle_pos.x, Paddle_pos.y);
		batch.end();
		
		Ball_pos.x += SpeedX;
		Ball_pos.y += SpeedY;
		
		if(Ball_pos.x + 120 > SCREEN_WIDTH ){
			
			Ball_pos.x = SCREEN_WIDTH - 120;
			SpeedX *= rev;
		}
		else if (Ball_pos.x  < -90){
			
			SpeedX *= rev;
			
		}
		if (Ball_pos.y + 120 > SCREEN_HEIGHT){
			Ball_pos.y = SCREEN_HEIGHT - 120;
			SpeedY *= rev;
		}
		else if(Ball_pos.y < -90){
			//smily_pos.y = 0 + 100;
			SpeedY *= rev;
		}
		
		
		
	}
		

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

	@Override
	public void pause() {
		Gdx.app.log("MyGdxGame", "Game.pause()");
	}

	@Override
	public void resume() {
	}
}

You have a couple of options.

One is to do this:

... create ...
    shapes = new ShapeRenderer();


... render ...
    shapes.begin(ShapeType.Filled);
    shapes.rect(x, y, width, height);
    shapes.end();

It follows the same principles as SpriteBatch, so you should try to squeeze as many shapes in between begin() and end() for best performance.

Another potentially more efficient option is to use a plain white texture and tint it a different color. This article explains the process:

Also you should read up on pixmaps and textures. For android you need to be careful with dynamically created pixmaps, as they aren’t “managed” – when the user pauses/resumes and the GL context is lost, you need to re-recreate your pixmaps.

If you are only targeting desktop, this isn’t such a big deal, but you should still read through the article to get a better understanding of everything. Pixmaps aren’t “slow” in the way you are using them.

Ultimately the best thing to do in your case is just to use textures; i.e. a circle ball texture and a rectangular paddle texture. Usually you would use something like TexturePacker, but for a simple pong game you could just use separate textures. Then you would render them like this:

batch.begin();

batch.draw(ballSprite, x, y);

batch.draw(paddleSprite, px, py, width, height);

batch.end();

Let me know if you have more questions…

Thanks you so much for the reply davedes, I really appreciate your quick response. I’ll read the articles, Explore more about the shape renderer and will bug you more if i have any queries, Thanks once again.