Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  Having trouble getting started..(Noob libGDX question)  (Read 1150 times)
0 Members and 1 Guest are viewing this topic.
Offline Bassex96

Senior Member


Medals: 4



« Posted 2013-03-31 01:17:47 »

Hey guys. I'm new to LibGDX and fairly new to Java. I do however have experience in other languages such as C#. I've read through a basic java book and am now moving on to libgdx..It seems a lot of the tutorials are outdated..I'm working on a basic brick breaker game and using this tutorial: http://code.google.com/p/libgdx/wiki/SimpleApp

I'm just using the camera and spritebatch code and such from it...but it doesn't seem to be working correctly. I'm getting an exception on the batch.draw method. When I run it, the application shows briefly, and it looks like the paddle is stretched across the screen, but then it shuts down with the exception. I've been searching around to no avail. I appreciate any help. I'll post my code below as it isn't much.

{EDIT} Actually, it doesn't throw an exception with the code below..but the paddle disappears and it's just a white screen.

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  
package com.psillicoder.brickbreaker;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
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.math.Rectangle;


public class Game implements ApplicationListener {
   private OrthographicCamera camera;
   private SpriteBatch batch;
   //private Texture ballImage;
  private Texture paddleImage;
   
   //rectangles
  //Rectangle ballRect;
  Rectangle paddleRect;
   
   @Override
   public void create() {      
      float w = Gdx.graphics.getWidth();
      float h = Gdx.graphics.getHeight();
     
      paddleImage = new Texture(Gdx.files.internal("data/paddle.PNG"));
      //ballImage = new Texture(Gdx.files.internal("data/ball.png"));
     
      camera = new OrthographicCamera(1, h/w);
      batch = new SpriteBatch();
     
      //paddle
     paddleRect = new Rectangle();
      paddleRect.x = 400/2 - 64/2;
      paddleRect.y = 20;
      paddleRect.width = 64;
      paddleRect.height = 16;
     
     
     
     
     
     
      }
     

   @Override
   public void dispose() {
      batch.dispose();
      //ballImage.dispose();
     paddleImage.dispose();
   }

   @Override
   public void render() {      
      Gdx.gl.glClearColor(1, 1, 1, 1);
      Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
      camera.update();

     
      batch.setProjectionMatrix(camera.combined);
      batch.begin();
     
      batch.draw(paddleImage, paddleRect.x, paddleRect.y);
      batch.end();
   }

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

   @Override
   public void pause() {
   }

   @Override
   public void resume() {
   }

   

}
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #1 - Posted 2013-03-31 01:27:55 »

are you using opengl1.0 when loading the desktop version? Because if your images are not to the power of two the will not work. So make sure in your desktop file that this is in there:
1  
cfg.useGL20 = true;
 If that is not the case I am not sure...Maybe share the .java file of your desktop project and ill take a look, and also what is the resolution of your image?

Offline Bassex96

Senior Member


Medals: 4



« Reply #2 - Posted 2013-03-31 01:34:32 »

usegl20 was set to false. I set it to true but got the same result. The size of the paddle image is 64x16. I'm not sure honestly what resolution..I just opened up paint and made a paddle image and saved as PNG. I'm guessing that is 24 bit?

Here is my new desktop file.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
package com.psillicoder.brickbreaker;

import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;

public class Main {
   public static void main(String[] args) {
      LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
      cfg.title = "BrickBreaker";
      cfg.useGL20 = true;
      cfg.width = 400;
      cfg.height = 600;
     
      new LwjglApplication(new Game(), cfg);
   }
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #3 - Posted 2013-03-31 01:43:58 »

Just went and tried your code in one of my games and commenting out this line of code seemed to solve it:
1  
 // batch.setProjectionMatrix(camera.combined);

thats in the render method btw

Offline Bassex96

Senior Member


Medals: 4



« Reply #4 - Posted 2013-03-31 01:51:49 »

Your exactly right..Thanks. I've been searching everywhere trying to figure it out. I've got a lot to learn.
I see from that tutorial that it sets the coordinate system specified by the camera..I guess the camera didn't have a coordinate system specified. I should probably check into that next. Lol.

Sorry for such a dumb question, but again, thanks!
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #5 - Posted 2013-03-31 01:55:06 »

No problem, remember Things Take Time to learn If you liked my help would you be kind enough to hit the appreciation button on that post?Smiley

Offline Bassex96

Senior Member


Medals: 4



« Reply #6 - Posted 2013-03-31 01:58:02 »

No problem, remember Things Take Time to learn If you liked my help would you be kind enough to hit the appreciation button on that post?Smiley

No problem, but I don't see an appreciate button?
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #7 - Posted 2013-03-31 01:58:51 »

its in the top right corner of that particular post

Offline Bassex96

Senior Member


Medals: 4



« Reply #8 - Posted 2013-03-31 02:00:13 »

All I see is Reply and Quote. This is literally my first post here. Are there any post count restrictions? If so, i'll do it as soon as i'm allowed.

[EDIT] I guess there is a post count restriction, but this post put me over it. The button is there now. Again, thanks!
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #9 - Posted 2013-03-31 02:01:28 »

Its usually right next to those two so I suppose that may be the case.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Bassex96

Senior Member


Medals: 4



« Reply #10 - Posted 2013-03-31 02:07:38 »

Got ya figured out.

Now onto separating the ball and paddle into separate classes...

do I need to pass the spritebatch to a render method in my ball and paddle class? or do I do it in the main render class?
Offline davedes
« Reply #11 - Posted 2013-03-31 16:50:56 »

Typically you would use OrthographicCamera like so:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
public void create() {
    //sets up a camera to the viewport width/height
   cam = new OrthographicCamera();
}

public void resize(int width, int height) {
    //update the camera with new viewport
   cam.setToOrtho(false, width, height);
}

public void render() {
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    //before rendering, you need to update the batch with the new camera matrix
   batch.setProjectionMatrix(cam.combined);

    batch.begin();
    // ... render sprites ... //
   batch.end();
}


The projection matrix now expects coordinates in "screen-space" from the lower-left origin. So rendering a 25x25 sprite at (0, 0) will render it in the lower left corner, and rendering it at (5, 10) will render it 5 pixels to the right, and 10 pixels up.

Typically you would pass SpriteBatch to other classes (like your Ball or Paddle class's render method).

Another solution with LibGDX is to use the Stage and scene2D package. It has some added benefits that might come in handy down the road like hit-testing, integration with UI elements (tables, layouts, skins, etc), easing/tweening, etc.

If you choose to use Stage, your code will look a little different:
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  
Stage stage;
Image sprite;

public void create() {
    stage = new Stage();

    //load the PNG texture
   Texture tex = new Texture("data/paddle.png");

    //create an Image which wraps the texture with position/size/etc
   image = new Image(tex);

    //set screen coordinates from lower-left origin
   image.setPosition(25, 25);

    //add the image to the screen
   stage.addActor(image);
}

public void resize(int width, int height) {
    stage.setViewport(width, height, false);
}

public void render() {
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    stage.act(Gdx.graphics.getDeltaTime());
    stage.draw();
}

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.

Dwinin (28 views)
2014-09-12 09:08:26

Norakomi (57 views)
2014-09-10 13:57:51

TehJavaDev (75 views)
2014-09-10 06:39:09

Tekkerue (38 views)
2014-09-09 02:24:56

mitcheeb (57 views)
2014-09-08 06:06:29

BurntPizza (45 views)
2014-09-07 01:13:42

Longarmx (28 views)
2014-09-07 01:12:14

Longarmx (34 views)
2014-09-07 01:11:22

Longarmx (35 views)
2014-09-07 01:10:19

mitcheeb (40 views)
2014-09-04 23:08:59
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!