Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (602)
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  
  Am i good student :D ? Hows the code? background drawing  (Read 1372 times)
0 Members and 1 Guest are viewing this topic.
Offline Andre Lopes
« Posted 2013-05-08 15:52:42 »

Hey again;
First i want to say hi to reBirth,haha.
Anyway,im loving this forum. 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?


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  
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() {
    }
}
Offline 65K
« Reply #1 - Posted 2013-05-08 15:59:59 »

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

Offline Andre Lopes
« Reply #2 - Posted 2013-05-08 16:25:58 »

Hi!
:p
Is this better?

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  
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() {
    }
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline matheus23

JGO Kernel


Medals: 114
Projects: 3


You think about my Avatar right now!


« Reply #3 - Posted 2013-05-08 16:26:46 »

Hi!
:p
Is this better?

You still create a new Sprite each frame. Smiley

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline Andre Lopes
« Reply #4 - Posted 2013-05-08 16:32:50 »

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
Offline Andre Lopes
« Reply #5 - Posted 2013-05-08 16:45:51 »

Ok, that one was incorrect.
This one is.

Now i will mess with the sprites.
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  
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() {
    }
}
Offline matheus23

JGO Kernel


Medals: 114
Projects: 3


You think about my Avatar right now!


« Reply #6 - Posted 2013-05-08 16:50:55 »

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

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  
[...]
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();
    }
[...]
}

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline Andre Lopes
« Reply #7 - Posted 2013-05-08 16:55:50 »

I thought about that.. but i had already posted. Sorry for my stupidity. Tongue

Offline jhffvcc

Senior Newbie







« Reply #8 - Posted 2013-05-08 17:52:45 »


[...] but im trying to learn java stuff nonetheless [...]


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 ...
Offline ReBirth
« Reply #9 - Posted 2013-05-09 02:17:02 »

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

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Andre Lopes
« Reply #10 - Posted 2013-05-09 03:01:21 »

heya.
Do you you have any example?
Tongue
Offline SHC
« Reply #11 - Posted 2013-05-09 03:46:30 »

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

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

Offline ReBirth
« Reply #12 - Posted 2013-05-09 05:53:09 »

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.

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.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (42 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (51 views)
2014-12-03 16:27:13

CopyableCougar4 (49 views)
2014-11-29 21:32:03

toopeicgaming1999 (115 views)
2014-11-26 15:22:04

toopeicgaming1999 (103 views)
2014-11-26 15:20:36

toopeicgaming1999 (31 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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