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  
  [Solved] Scaling a display with libGDX  (Read 849 times)
0 Members and 1 Guest are viewing this topic.
Offline Nanor

Senior Newbie


Exp: 2 years



« Posted 2014-05-08 09:24:51 »

I've been making a game for Android in my spare time for the past couple of weeks. It was going well until I realised I was only testing on one device. Upon creating an emulator with a smaller resolution than my physical device I noticed the game didn't scale correctly and as a result the gameplay wasn't the same. I stumbled upon this tutorial. I implemented it but it's not working as desired, namely it only seems to use the top right corner of the screen. I'm not sure if I've implemented Gemserk's solution incorrectly or it's something else but frankly I think I just need a fresh set of eyes. The entirety of my main code can be found here in case I miss anything out and you can find Gemserk's implementation in the previous link but I'll go through what I think are the most important methods below:


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  
public void create() {
      gameState = states.menu;
      multipleVirtualViewportBuilder = new MultipleVirtualViewportBuilder(800, 480, 1280, 1024);  
      VirtualViewport virtualViewport = multipleVirtualViewportBuilder.getVirtualViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());  
       
      camera = new OrthographicCameraWithVirtualViewport(virtualViewport);
      camera.position.set(0f, 0f, 0f);
     
      font = new BitmapFont();
      font.setColor(Color.WHITE);
             
      spriteBatch = new SpriteBatch();
     
      playerTexture = new Texture(Gdx.files.internal("data/cube.png"));
      playerTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
     
      diamondTexture = new Texture(Gdx.files.internal("data/diamond.png"));
      diamondTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
     
      player = new Player((int) (Gdx.graphics.getWidth()/2 - playerTexture.getWidth()), 0);      

      screenWidth = Gdx.graphics.getWidth();
      screenHeight = Gdx.graphics.getHeight();
     
      playerSprite = new Sprite(playerTexture);
      playerSprite.setOrigin(0, 0);
      playerSprite.setPosition(player.getPosX(), player.getPosY());
      playerSprite.scale(scale);
     
      playerWidth = (int) (playerSprite.getWidth() * scale);
      playerHeight = (int) (playerSprite.getHeight() * scale);
     
      Gdx.input.setInputProcessor(new GestureDetector(this));
     
   }


As you can see here I'm instantiating the virtual viewport as well as the camera. I have a feeling that the parameters I'm passing when creating a new MultipleVirtualViewportBuilder might have something to do with it but any experimenting I'm having has proven fruitless.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public void render() {
      camera.update();
      Gdx.gl.glClearColor(0.204f, 0.255f, 0.255f, 1);
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
      spriteBatch.setProjectionMatrix(camera.combined);
      spriteBatch.begin();

//Draw things

                spriteBatch.end();


All I'm really doing here is updating the camera and drawing the player so I'm fairly certain my issue is in the create() method. Either that or I've implemented it wrong altogether.

Anyone able to shed some light on this problem?
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #1 - Posted 2014-05-08 10:11:30 »

See ExtendViewport, it does what you want. Also see the viewport wiki page.

Offline Nanor

Senior Newbie


Exp: 2 years



« Reply #2 - Posted 2014-05-08 11:31:12 »

Should I be using Stage?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline PaulReeves

Senior Newbie





« Reply #3 - Posted 2014-05-08 12:06:57 »

Nope, See here https://github.com/libgdx/libgdx/wiki/Viewports and there are some examples linked to at the bottom of the page. Some don't use a stage, although if using a stage makes you life easier...
Offline Nanor

Senior Newbie


Exp: 2 years



« Reply #4 - Posted 2014-05-08 14:05:24 »

I'm checking out this example and it's making me very confused. My create method looks like partially like this:

1  
2  
      camera = new OrthographicCamera();
      viewport = new ExtendViewport(100, 200, camera);


I previously thought that it would set the viewport to be 100, 200 but it doesn't seem to do anything. In the example above the only time a viewport method is called is in the resize(). I can actually comment out those two lines of code above and I have no problems which leads me to believe I'm missing either more code in the create() or some viewport code in the render(). Any idea?

EDIT

This example suited my needs better as it uses the same camera. However now nothing is drawn at all. Bah!
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #5 - Posted 2014-05-08 19:48:29 »

You need the code from the examples in resize(). You also need: batch.setProjectionMatrix(camera.projection);

Offline Nanor

Senior Newbie


Exp: 2 years



« Reply #6 - Posted 2014-05-09 11:52:11 »

My create() method now looks party like this:

1  
2  
3  
4  
5  
6  
7  
...
    camera = new OrthographicCamera(1, screenHeight/screenWidth);
    camera.position.set(0, 0, 0);
    camera.update();

    viewport = new ExtendViewport(100, 200, camera);
...


and my render() method contains
1  
spriteBatch.setProjectionMatrix(camera.combined);


I also have the code in the resize() method but from what I can see it's never called on Android.

The issue I'm having now is that nothing seems to appear on the screen. I think the camera is in the right position because things are being drawn, I just can't see them. The game plays as normal when I check various values as I debug. Any ideas?

Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #7 - Posted 2014-05-09 15:29:54 »

resize() is always called, make sure it is. Run the examples, they work, then apply that to your game. Simplify and check your assumptions.

Offline Nanor

Senior Newbie


Exp: 2 years



« Reply #8 - Posted 2014-05-12 16:31:49 »

I created a separate project so I could test it with an image being drawn to the screen instead of relatively complicated movement logic. I've got it scaling as desired and my simplified code is here. The issue I had with the examples was they iterated through every type of viewport when I just wanted an example of a single viewport.

The issue I have no is the image variable 'img' I draw to the screen is draw with
1  
batch.draw(img, 0, 0);

This draws it to the center of the screen. How do I position it at the bottom left of the screen where 0, 0 would normally be on a graph?
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #9 - Posted 2014-05-12 16:39:04 »

The camera has 0,0 at the center of the screen by default. You can adjust it so 0,0 is the bottom left corner.

1  
camera.position.set(worldWidth / 2, worldHeight / 2, 0);


Viewport#update(int, int, boolean) can do this for you if you pass true.

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

Senior Newbie


Exp: 2 years



« Reply #10 - Posted 2014-05-14 11:25:26 »

Fantastic, this works perfectly. How do I mark this post as solved?
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #11 - Posted 2014-05-14 18:31:14 »

Fantastic, this works perfectly. How do I mark this post as solved?
By prefixing/inserting/adding "[solved]" to your topic title.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Pages: [1]
  ignore  |  Print  
 
 

 

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!