How to remove background from sprite sheets? - java

In the past, I have always made my own sprite sheets, and just made them transparent. But I always see sprite sheets with a pinkish color as a background. How would you key this out in a game?
also which is better? using a transparent .png file? or having a solid background color?
In case anyone is wondering, I use the BufferedImage class and I getSubImage.

There's typically three ways to make sprites transparent, either using a separate alpha channel, by using separate bit mask, or simply using a transparent color. The latter is usually used with indexed color, as it is as simple as replacing the color of one of the indexes (the one with the pink color in your case) with transparency.
The only advantage I see with this technique is preserving memory. But I haven't made many games, so there might be other reasons for doing this.. ;-)

If you dont mind using a library, i use lwjgl,and make spritesheets using texturepacker.
Texturepacker generates xml files for textures automatically, and places the textures all on one png.
Once i do that, i use gimp, and just use the eraser tool until the background is transparent.
Then in your program you just have to type
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
and the transparency should work automatically, its a bit more of a pain to actually set up accessing the spritesheets and such though

Related

Is there a way to draw Strings in libgdx without BitmapFonts?

I want to draw Strings in my Libgdx game but i cant use BitMap Fonts because the scale of my game is to smal to use them.
It sounds like you mean the scale of your viewport is too small to show fonts correctly. There are two solutions. The first is better for legibility while the second is quick and dirty.
One is to use a second viewport for the UI that has an appropriate scale for text. You would first call gameViewport.apply(), draw the game, and end the batch. Then use uiViewport.apply() and then draw the UI. The downside with this method would be if you want to draw text that aligns with moving objects in the game, you would have to use the two viewports to convert coordinates. Otherwise, this is the ideal method to get a crisp looking UI. Ideally you would use a ScreenViewport and select a font size at runtime based on the screen dimensions, either by shipping your game with multiple versions of the font at different scales, or by using FreeTypeFontGenerator.
The second method is to scale down all your text. First call bitmapFont.setUseIntegerPositions(false) do it won't round off positions to integers. Then call bitmapFont.setScale() with however much you want to shrink it to fit in your game viewport.
There is a gdx-freetype project:
https://www.badlogicgames.com/wordpress/?p=2300
and it uses TrueType fonts as source to generate bitmap font on the fly.
Not sure how stable this is - didn't use it.

Anti Aliasing based on colors (not textures)

I was searching for an anti-aliasing algorithm for my OpenGL program (so I searched for a good shader). The thing is, all shaders want to do something with the textures, but I dont use textures, only colors. I looked at FXAA most of the time, so is there a anti-aliasing algorithm that just works with colors? My game, what this is for looks blocky like minecraft, but only works with colors and cubes of different size.
I hope someone can help me.
Greetings
Anti-aliasing has nothing specifically to do with either textures or colors.
Proper anti-aliasing is about sample rate, which while highly technical can be thought of as doing extra work to make a better educated guess at some value that cannot be directly looked up (e.g. a pixel that is only partially covered by a triangle).
Multisample Anti-Aliasing (MSAA) will work nicely for you, it will only anti-alias polygon edges and does nothing for texture aliasing on the interior of a polygon. Since you are not using textures you do not need to worry about aliasing inside a polygon.
Incidentally, FXAA is not proper anti-aliasing. FXAA is basically a shader-based edge detection and blur image processing filter. FXAA will blur any part of the scene with sharp edges, whether it is a polygon edge or an edge due to a mapped texture. It indiscriminately blurs anything it thinks is an aliased edge and gets this wrong often, resulting in blurry textures.
To use MSAA, you need:
A framebuffer with at least 2 samples
Enable multisample rasterization
Satisfying (1) is going to depend on what you used to create your window (in this case LWJGL). Most frameworks let you select the sample count as one of the parameters at the time of creation.
Framebuffer Objects can also be used to do this without messing with your window's parameters, but they are more complicated than need be for this discussion.
(2) is as simple as calling glEnable (GL_MULTISAMPLE).

Java texture png modificaiton

I'm trying to make a simple java game.
I have some png file that i want to draw, but first i want to make some modification on that png. I would like to take a png, and delete to background some parts of it and add some colored lines..
For my game im using libgdx.
I dont know what to use for this, so i can search on google about it and learn.
Few hints, about what functions i should use could be awesome, Ty.
P.S. I tryed to search on internet before post here, but i didnt find something that could help me, probably idk what to search.v
Edit:
I found Pixmap from libgdx, but i can delete to background. Any advices ?
Edit2:
i want to load this texture
multiplicate when needed (no problem here)
and delete some parts of it, to background, so it will take this shape:
by using pixmap, when im drawing background over it, nothing is happen, because its draw over, not instead of.
What i could make, was using pixmap to draw the top part, that i want to delete, and manualy delete it using external programs:
I think you might want to look at a "mask" image. There are some approaches listed here: https://github.com/mattdesl/lwjgl-basics/wiki/LibGDX-Masking
Alternatively, you could use create a mesh that encodes the "ground" (with the bump) and then texture it. OpenGL would take care of truncating the edge of the mesh. Try: Drawing textured polygons with libgdx
Another approach is to just draw the dirt texture on the full screen, and then draw the "background" over it. You will need to make pixels in the "background" image transparent so the dirt will show through. Whatever pixel editor you are using should be able to do that. Alternatively, pick a "key" color and convert those pixels to transparent when you load the image as a pixmap.
I don't know if this question is serious, but you cannot do what you're asking in Java. You need a photo editing software such as Photoshop or GIMP.

TrueType Font rendering in Java?

I have made a GUI that has multiple components with it and now I need to populate those components with text.
My specifications:
Preferably uses LWJGL (OpenGL)
Isn't exceptionally complicated
Doesn't use any other external libraries (bar the LWJGL library)
Has to be fairly optimised, it will be used a lot on a very fps intensive GUI
Has the possibility of being anti-aliased?
Possibly have the ability to run on most major Operating Systems
How could I do this in java?
I would consider using SVG, and particularly Batik for managing the fonts. I am doing quite a lot with this myself and it's somewhat of a learning curve. I would be prepared to start simply and find out what particular features you need before worrying about performance. But until you give clearer ideas I'm not sure we can help much more
You have three options: Bitmap fonts, texture fonts and vector fonts.
Bitmap fonts are only useful if all you want to do is render text for a 2D GUI. However, you can't do antialiasing if you use Bitmap fonts. On the other hand, they're pretty easy to use and they're quick to render.
Texture fonts allow for antialiasing, but again they're best for 2D GUIs. If you want to render text in world space, you'll get lots of artifacts because of the texture scaling that's taking place. To use texture fonts, you have to create a texture atlas that contains an image for each character of a particular font that you want to use (usually you'll want to restrict the character set to ASCII, otherwise your texture will be too large). You can use AWT to create a rectangular image that contains all the characters you need. Then you can render a character by rendering a quad with the appropriate texture coordinates for that character. It is advisable to use a luminance alpha texture so that you can blend it with the color you want the text to be in. You can optimize this by using display lists for each character and possible for each string, but you'll run into problems with kerning etc.
Vector fonts give you the best results if you want to render your text in world space. They will give you perfect font rendering incl. kerning, but they're more expensive to render. My usual approach is to create a path (using AWT) for each string that I want to render, flatten that path and then trace it using the GLU tesselator. This will give you a bunch of triangles, triangle strips and triangle fans which you can put into a VBO for optimal performance. Then you can render that string by issuing the appropriate rendering commands for the VBO. You can optimize this further by using a display list for each string. That way, you will only have to send one command per string, but of course this will still be more expensive than the other methods.

Render string to texture in Android and OpenGL ES

I've googled around everywhere, but cannot find much for rendering strings to textures and then displaying that texture on a quad on the screen. Can someone provide a run-down on the process or provide good resources that describe how? Is rendering strings to textures even the best method for displaying text in an Android OpenGL ES app?
EDIT:
Okay, so LabelMaker interferes with alpha blending, the texture (created from a PNG with a transparent background) now has a solid black background, rather than a transparent background. If I comment out all the LabelMaker-related code, it works fine.
UPDATE:
Nevermind. I took a look at the code to find that LabelMaker was disabling blending after drawing the labels.
I think this is what you are looking for.
If you don't want to use GL extensions you need to create the font as a bitmap and then create a class to convert that string into quads that you can draw.
I use this method with the 2 fonts in my game. I have a class that takes a wide texture with all the letters evenly spaced, and a string that matches the image, then uses lookups on the letters to find out how far in the bitmap it should go.
Your other option is to render your text to a offscreen bitmap using android, and then bind the text as a texture. This will let you use androids built-in font processing and rendering to create texture-based fonts.
The second method I have not used yet, but I have rendered google maps to a offscreen canvas and then bound the bitmap as a GL texture, so doing it for text should be much simpler.
If you are planning to have modifying string data in a gl loop you need to really worry about StringBuilder too, because it causes GC and performance issues. I hardcode all my strings so it doesn't allocate, and all my rapidly numbers are done through a second draw function dedicated to drawing changing numbers without using string-builder.

Categories