Texture Region 2D Array - Why are my sprites drawing so small? - java

Using the Libgdx Framework, I'm generating an Actor from an Assets.java class and then drawing him depending on the state he is currently in. The problem i'm having is that he's coming out ridiculously small. I'm noticing that when I split the Texture into regions the .Split() function doesn't provide width and height parameters in comparison to a singular declaration of the texture region function here: new TextureRegion(Texture, x, y, width, height);
Note: I've also set the size of the world, the actor size etc they don't need to change how small he is. I'm assuming it's something to do with the sprite size or assets class. But the way i'm slicing the Texture works perfectly for the engine i've created.
I'm wondering is there something I'm missing or something to add to define the width/height of the TextureRegions inside theAssets.java code snippet?
All my sprites are in a 512x512 Texture with a height and width of 85px
Relevant Code:
rubenSprite = loadTexture("data/rubenSprite.png");
rubenFrames = TextureRegion.split(rubenSprite, 85, 85);
WalkF1 = rubenFrames[0][0];
WalkF2 = rubenFrames[0][1];
WalkF3 = rubenFrames[0][2];
WalkF4 = rubenFrames[0][3];
WalkF5 = rubenFrames[0][4];
... More TextureRegions
If more code is needed, or anything elaborated let me know. Thanks.

The size of actor is not size of texture inside. Actor has boundaries. When this boundaries are too low image cannot fit inside. On to the contrary, it is analogously.
You should set your actor as:
setWidth(width);
setHeight(height);
setBounds(left, right, width, height);
And make sure, that texture is also set to width and height.
btw.
I recommend, use drawables with Actors. It is more clearer. You create skin, where you define all your moves and then you call your textures from the skin.

Related

libgdx optimal Height and Width

Does it matter what height and width I use for my game? In my game I have a few polygons, they all are connected to images that I have painted, but when I have width and height as 800x480 I have to paint the images very small, this causes them to get blurry. Also, I don't really understand how this behaves on different sized phone screens.., does the images I have painted get streched, or do they stay small, even on big tablets? So my question is basically what is the optimal width and height to have on a libgdx game?
This is a part of my code, to maybe help you understand what I mean
WIDTH = Gdx.graphics.getWidth();
HEIGHT = Gdx.graphics.getHeight();
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480 );
What will happen if I change these values? What would it be best to change them to?
I was reading on a tutorial of sorts, but they didn't really talk about this part.
Do I have to do something complicated to get my painted images to look decent on all devices?
EDIT:
This is how I currently implement a cloud into my game:
public class AnotherScreen implements Screen{
Polygon cloudPoly;
Texture cloud;
#Override
public void render(float delta) {
batch.setProjectionMatrix(camera.combined);
batch.begin();
renderBackground();
batch.draw(cloud,cloudPoly.getX(),cloudPoly.getY());
batch.end();
}
#Override
public void show() {
cloud = new Texture(Gdx.files.internal("assets/textures/cloud.png"));
cloudPoly = new Polygon();
cloudPoly.setPosition(MathUtils.random(350,600),MathUtils.random(380,440));
// theses vertices are just a little square atm
float[] cloudVertices = new float[] {
0,0,
0,20,
20,20,
20,0
};
cloudPoly.setVertices(cloudVertices);
}
}
The cloud image has width 256p and height 128p, when I set 'camera.setToOrtho(false, 800, 480 );' won't this cloud always stay in proportion?
I am sorry if I am unclear, I am a new programmer and my english could be better, love you doh for staying with me!
does the images I have painted get streched, or do they stay small, even on big tablets?
One of the greatest features of libgdx is that it lets you do things exactly as you want them. So, it is really your choice. With this snippet
WIDTH = Gdx.graphics.getWidth();
HEIGHT = Gdx.graphics.getHeight();
camera = new OrthographicCamera();
camera.setToOrtho(false, WIDTH, HEIGHT);
You would have a different viewport size in every different screen size. So your images would stay small.
In the other hand, if you use predefined values for the viewport like:
WIDTH = 800;
HEIGHT = 480;
camera = new OrthographicCamera();
camera.setToOrtho(false, WIDTH, HEIGHT);
You would get the same viewport for every screen and the images would stretch to occupy the same space in each axis. This won't necessarily be the same for each axis. So a perfect square image in one screen may look like a rectangle in another screen with a different x-y ratio.
There is a third option, you can fix one side and let the other be calculated, so you keep the ratio of your images (a square stays as square). You must decide what will be the fixed side. For a game like Super Mario Bros I would recommend you to fix the height and calculate the width:
HEIGHT = 480;
WIDTH = (Gdx.graphics.getWidth()/Gdx.graphics.getHeight())*HEIGHT;
camera = new OrthographicCamera();
camera.setToOrtho(false, WIDTH, HEIGHT);
Every screen would see the same height in the game world. But some screens would see more in the width than others. If thats a problem for you (you want them to see the same ammount of the world but keep the ratio too), than adding black bars to each side is an option.
So my question is basically what is the optimal width and height to have on a libgdx game?
The optimal solution is having a different set of images for several resolutions. And choosing whats the most appropiate at runtime. But that increases the game size a lot.
Another dirty way is having only one big set and let the images shrink in lower resolutions. It looks better than the inverse. But some people think this is just too dirty :p (not me, so I recommend it).

Does everything have to be a polygon in openGL (java)?

I am extremely new to JOGL, and I have two main questions about JOGL, the first is about sprites and animations and the next is about textures.
(I am not doing 3d, this is all in the contexts of 2d)
1.) I have been able to create quads, polygons, and so forth in JOGL, but I am very curious, is that the only way to get a type of image that you can manipulate out there? For instance if you want a sprite of a man to appear in JOGL, and when you move the sprite it will move in the direction you make it but also display an animation. Is there a way to do this efficiently in JOGL, or do I have to scalp a shape out of polygons, one vertex at a time, and then paint a texture over it? Or is there a simpler method of just loading the sprite and displaying it right then and there and of course changing the image depending on user input. If there is a way to do it without creating a vertex for every corner of the sprite could you display some code, or a tutorial so I can grasp the idea of what you are doing.
2.) I have been searching around for a way of displaying textures in JOGL, but I am still confused on how to do so. Let me first show you the way I found of loading and display a texture on a shape. First I create 7 variables:
Texture image;
private String textureFileName = "images/crate.png";
private String textureFileType = ".png";
private float textureTop, textureBottom, textureLeft, textureRight;
Next in my init() method I load my image by doing the following:
GL2 gl = drawable.getGL().getGL2();
gl.glShadeModel(GL_SMOOTH);
image = TextureIO.newTexture(getClass().getClassLoader().getResource(textureFileName),false, textureFileType);
// Use linear filter for texture if image is larger than the original texture
gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Use linear filter for texture if image is smaller than the original texture
gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
TextureCoords textureCoords = image.getImageTexCoords();
textureTop = textureCoords.top();
textureBottom = textureCoords.bottom();
textureLeft = textureCoords.left();
textureRight = textureCoords.right();
Then in the display method I do the following
GL2 gl = drawable.getGL().getGL2();
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT);
image.enable(gl);
image.bind(gl);
gl.glBegin(gl.GL_QUADS);
gl.glTexCoord2f(textureLeft,textureTop);
gl.glVertex2d(c,a); // vertex 1
gl.glTexCoord2f(textureLeft, textureBottom);
gl.glVertex2d(c,b); // vertex 2
gl.glTexCoord2f(textureRight, textureBottom);
gl.glVertex2d(d,b); // vertex 3
gl.glTexCoord2f(textureRight, textureTop);
gl.glVertex2d(d,a); // vertex 4
gl.glEnd();
//Variables a,b,c,d are just doubles with certain values for each point
Now I just wish to know if this is an effective way to load images and display them if I am going to be having multiple textures. Seeing this way I have devised a plan on how to do that using this method but I want to know if this is the best way of doing it. I have see people talk about bmp loaders but hearing of what they do, mapping everything single integer value the image has, seems really complex. Is there no simple way to do it? But if not could someone show me how or point me into a direction of the most correct, and efficient way to load textures and display those textures.
One last thing, when I put the line:
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT);
Isnt this just basically just clearing out an integers for pixels, etc. in the buffer? But what does the | do anyways? Does it shift the bits?
Also what do these two lines of code do? Im confused by the comments. These two lines appear in my init() method for loading a texture:
// Use linear filter for texture if image is larger than the original texture
gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Use linear filter for texture if image is smaller than the original texture
gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Thank you for taking the time to read and respond to this message, Thank you.
-Dan

Libgdx Window Resizing: Keeping Aspect Ratio

I'm currently making a game using the Libgdx library, and have currently hit a small hurdle.
I've disabled the ability to resize currently, in my Main.java class in the desktop project.
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
cfg.resizable = false;
I'm wondering whether there is a simple way to make the window resize whilst keeping the aspect ratio. (Like when you shift+resize)
What are my options? :)
that function does not give you the ability to keep the aspect ratio or even something of that what you think. It does just disables the possebilty to resize the screen at the desktop application.
Please take a look at the libGDX wiki especialy the Scene2D page.
Take a look at the Viewport stuff from the Stage. It's explained how you do keep the aspect ratio with the current libGDX. There are tutorials out there who do explain a different way with the help of a virtual resolution and the screen resize method. It's outdate!
from the wiki
This example also uses a fixed stage size with "black bars" on either
side, this time using glViewport. First the stage size of 800x480 is
scaled to fit the screen size using the Scaling class. The result is
used to configure glViewport, which changes which part of the screen
OpenGL will use. Lastly, setViewport is passed the viewport position
and size. The result is the same as the last example, the stage has
"black bars" as necessary to keep the aspect ratio, but no drawing can
occur outside the viewport.
public void resize (int width, int height) {
Vector2 size = Scaling.fit.apply(800, 480, width, height);
int viewportX = (int)(width - size.x) / 2;
int viewportY = (int)(height - size.y) / 2;
int viewportWidth = (int)size.x;
int viewportHeight = (int)size.y;
Gdx.gl.glViewport(viewportX, viewportY, viewportWidth, viewportHeight);
stage.setViewport(800, 480, true, viewportX, viewportY, viewportWidth, viewportHeight);
}
regards

LWJGL Camera stretches and shrinks shapes

I've been looking around and i couldn't find an answer to this but what I have done is create a cube / box and the camera will squash and stretch depending on where I am looking at. This all seems to resolve it self when the screen is perfectly square but when I'm using 16:9 it stretches and squashes the shapes. Is it possible to change this?
16:9
and this is 500px X 500px
As a side question would it be possible to change the color of background "sky"?
OpenGL uses a cube [-1,1]^3 to represent the frustum in normalized device coordinates. The Viewport transform strechtes this in x and y direction to [0,width] and [0,height]. So to get the correct output aspect ratio, you have to take the viewport dimensions into account when transfroming the vertices into clip space. Usually, this is part of the projection matrix. The old fixed-function gluPerspective() function has a parameter to directly create a frustum for a given aspect ratio. As you do not show any code, it is hard to suggest what you actually should change, but it should be quite easy, as it boils down to a simple scale operation along x and y.
To the side question: That color is defined by the values the color buffer is set to when clearing the it. You can set the color via glClearColor().

How to decide width and height for SVG sprite in AndEngine

I am using AndEngine to load a svg images as my Sprites.
The problem I am having with this is that i can't figure out how to scale the image to fit the particular device on which it is being run.
sprite= new Sprite(positionX, positionY, WIDTH,HEIGHT,TextureRegion);
The parameters that it takes are the position's on x and y coordinates, and then width, and height.
The problem I am having is that I can't figure out how to scale the sprite to the right width and height for how I want it.
For example I have a title, and I want the title to be bigger on bigger devices, how would I decide if the device is bigger and scale it up for a bigger screen and down for a smaller screen?
If you use a constant camera size, all of the screen is scaled (Hence all of the entities).
Decide on a constant size, for exaple 720x480.
final static int CAMERA_WIDTH = 720;
final static int CAMERA_HEIGHT = 480;
Then, when creating the engine options, use:
... new EngineOptions(... new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), ...);
On each device, the camera will scale itself to fit the resolution as good possible, while keeping the ratio between width & height.
Now, when creating a sprite, just do:
Sprite sprite = new Sprite(x, y, TextureRegion);
Don't give the sprite size! Its size comparing to the camera size will be decided according to the texture region size, and it will be a constant one. Then, it will be scaled by the camera-to-device-screen scaling, which changes from one device to another, but the ratio will remain constant - the game will not look like a bad scaled image on devices with a different resoltuion that the camera width/height ratio.
I think that's a great method that AndEngine provides to deal with different screen resolution; IMHO, it is the best to use.

Categories

Resources