Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (481)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (548)
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  
  Drawing a Filled rectangle using Pixmap in libGDX?  (Read 3580 times)
0 Members and 1 Guest are viewing this topic.
Offline iwhizkid

Junior Newbie





« Posted 2013-06-13 08:47:34 »

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 Smiley


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  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
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() {
   }
}
Offline davedes
« Reply #1 - Posted 2013-06-13 13:24:25 »

You have a couple of options.

One is to do this:

1  
2  
3  
4  
5  
6  
7  
8  
... 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:
https://github.com/mattdesl/lwjgl-basics/wiki/Batching-Rectangles-and-Lines

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.
https://github.com/mattdesl/lwjgl-basics/wiki/LibGDX-Textures

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:

1  
2  
3  
4  
5  
6  
7  
batch.begin();

batch.draw(ballSprite, x, y);

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

batch.end();


Let me know if you have more questions...

Offline iwhizkid

Junior Newbie





« Reply #2 - Posted 2013-06-14 06:51:15 »

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.
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.

atombrot (26 views)
2014-08-19 09:29:53

Tekkerue (24 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (14 views)
2014-08-16 06:20:21

Tekkerue (22 views)
2014-08-16 06:12:11

Rayexar (60 views)
2014-08-11 02:49:23

BurntPizza (38 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
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!