Using LWJGL and Slick2d, how do I load up a large image(sprite sheet) and store it into smaller images? - lwjgl

I have done a little bit of basic stuff, I can tiles a grass tile across the screen evenly, I can display a transparent texture to the screen but I can't seem to find any documentation on loading sprites from a larger sprite sheet.
How would I dice the image up into smaller images? Or is there a different way to do it?

There is a SpriteSheet class in Slick that does exactly this.
SpriteSheets, in Slick, are large images made up of a series of uniformly sized tiles. Each tile is typically an animation frame in a Sprite. In the SpriteSheet constructor you specify the image (which has all the tiles), and the width/height of the tiles in the sheet, along with any spacing and margin, if you have/need that.
Finally, the getSprite(x, y) method allows you to retrieve the specified tile, as if it were an element in a 2D array. In other words, if you have a SpriteSheet of 16 tiles, that are arranged in a 4x4 grid of tiles, then to get the tile in column 3, row 2, you would call getSprite(3, 2);
I believe the indexes in getSprite(x, y) are zero-based, just like arrays in Java.

Related

Libgdx Individually reference textures

In my game I have some gold that the player can collect. My problem is I want to individually refernce textures of gold so i can take that particular instance of gold off the screen (player picked it up). How would I go about doing this as i am pretty sure you cannot do this with regular textures. Would i have to create a shape and fill it with the gold texture so I can delete that particualr gold piece? Thanks
I think you confuse Texture (which is basically a loaded image that you can draw) with game entities. Depending on how you implement your game, you can spawn multiple bodies (Box2D), actors (Scene2D) or your simple data containers (width, height, x, y) representing each coin and draw each one on the screen using the same texture. Actually, that's the preferred way to handle assets: you load images once and then simply reuse them for each sprite.
I suggest looking into the following classes:
Sprite (makes it easier to draw textures).
Image (allows to display simple images on Scene2D stage).
Box2DSprite (third party utility class that makes it easier to draw Box2D worlds).

Extracting Sprites from Spritesheets in AS3

Recently I've been experimenting with spritesheets, particularly in AS3. I've managed to extract standard sprites and develop functional animations through sheets with equal sized squares. A lot of spritesheets on the internet aren't properly organized with equal sized squares though. Is there a way to manipulate pixels to obtain the location and dimensions of sprites from these sheets dynamically? Or must I modify the spritesheet itself to a 'square format'?. I want to avoid hard-coding as much as possible.
The point of spritesheets is often to reduce the overhead of data. Loading one image is much faster than loading hundreds of images. Because you can have variable sized images, most spritesheets generated will also come with a secondary file that defines where in the spritesheet you can find each image. Then, it's just a matter of referencing the x, y, width, and height values for that image.
TexturePacker will generate these, as I imagine most other apps do.
Typically if you have non-regular sized and/or spaced sprites, you'd also have an accompanying data file (XML or JSON probably) that defines the locations and sizes of the different sprites.

When should I decide to use Sprite over Texture and vice versa?

Say I have a main menu screen which has a Logo which is png. Would I draw it as a texture or convert it into a sprite then draw it? This logo would have no interactivity on it, it's just sitting there looking pretty.
Thanks!
It's up to you which you prefer. Sprite is a convenience class that allows a Texture or TextureRegion store its position, rotation and scale, etc. If you don't use Sprite, that just means you plan to store that info elsewhere to use when you call draw().
Typically, you should try to put a groups of 2D images into a single TextureAtlas using TexturePacker, from which you can extract TextureRegions or Sprites (which are TextureRegions with features) to draw as a batch with SpriteBatch. If you only have a couple images on screen, such as on the initial loading screen, then you might want to go with just a Texture, but generally, you want to minimize the number of unique Textures on screen at once for performance reasons.
For instance, the low-res 2D game I'm working on has only three image files. One is the logo that appears before anything else is loaded, so I just draw it as a Texture, centered on screen. The other two are TextureAtlases, containing hundreds of sprites. One that is loaded with nearest filtering, and the other that is loaded with linear filtering.

Starling move around huge Image

I'm using Starling framework and Flash Builder.
I have big map image(around 5500x4500px) where player is flying around. It is non repeative, not tile based but vector based.
I'm moving camera around following player on each enterFrame event, camera displays approx 800x400 part of map per frame. Camera movemet is simulated by changing X and Y coordinate of maps parent DisplayObject.
What is the best way to draw this and have stable 60fps?
What I have now(attemp with max FPS on mobile):
9 Starling Images with max texture size: 2048x2048(or less on edges). I get 53-60 FPS, but I need stability, I feel like I'm hitting rendering limits already.
What I tried(gives less FPS):
Drawing sprites for each seperate map object(much more than 9 but smaller size)
Using CullingSprite(not rendering itsel when not visible)
Really you only need 4 images the size of your screen which wrap around and sample from the texture atlas. And for a starling implementation movieclips are great because you can just change its contents to a different frame or portion of the texture atlas. This way you aren't deleting and creating new images every time you need to wrap.
Batching is also one way you can improve on it, moving all samples as a single unit.

Drawing over terrain with depth test?

i'm trying to render geometrical shapes over uneven terrain (loaded from heightmap / shapes geometry is also generated based on averaged heights across the heightmap however they do not fit it exactly). I have the following problem - somethimes the terrain shows through the shape like showed on the picture.
Open Image
I need to draw both terrain and shapes with depth testing enabled so they do not obstruct other objects in the scene.. Could someone suggest a solution to make sure the shapes are always rendered on top ? Lifting them up is not really feasible... i need to replace the colors of actual pixel on the terrain and doing this in pixel shader seems too expensive..
thanks in advance
I had a similar problem and this is how I solved it:
You first render the terrain and keep the depth buffer. Do not render
any objects
Render solid bounding box of the shape you want to put on the terrain.
You need to make sure that your bounding box covers all
the height range the shape covers
An over-conservative estimation is to use the global minimum and maximum elevation of the entire
terrain
In the pixel shader, you read depth buffer and reconstructs world space position
You check if this position is inside your shape
In your case you can check if its xy (xz) projection is within the given distance from
the center of your given circle
Transform this position into your shape's local coordinate system and compute the desired color
Alpha-blend over the render target
This method results in shapes perfectly aligned with the terrain surface. It also does not produce any artifacts and works with any terrain.
The possible drawback is that it requires using deferred-style shading and I do not know if you can do this. Still, I hope this might be helpful for you.