Note: I am new to LibGdx with android.
I am Creating a clone of the famous game Color Switch for practice purposes.
I have used ShapeRenderer for creating the ball and moving circles.
After reading more about ShapeRenderer, I realized it is mostly used for debugging purposes, so I searched for other alternatives to it and I got to know about Pixmaps but I am stuck on how to use it and cannot find any source.
So I want to know if there any other alternatives to ShapeRenderer or any sources for Pixmaps to get started?
ShapeRenderer is perfectly fine for your use-case. So, unless you have an actual problem with it, there is no need to switch to something else.
The reason that ShapeRenderer is primarily used for debugging is because it's limited to basic shapes and colors. In many use-cases this is insufficient and e.g. images (like .png files) are used instead. In those cases SpriteBatch is typically used to draw the images, which is optimized for (rectangular) images instead of shapes.
A Pixmap is the raw image data in CPU memory (RAM), which is practically an interim step between the image on disk (e.g. the .png file) and the image (Texture) in GPU memory (VRAM). LibGDX handles this interim step for you, so you should never have to deal with Pixmap in most use-cases yourself. Manually manipulating a Pixmap is very costly operation and worse than using ShapeRenderer for your use-case.
Related
I was working on a 2D game and everything was fine until I simply added about ~100 Rectangle2D.Double's to render (with 64x64 images). I was shocked to realize how slow it was for just about 100 objects. I immediately added precautions including drawing only if on screen, making sure the images didn't load every update, use double buffering, etc. I also found out using transformations like scale, translate, and rotate incredibly slow down performance, so I made all of the rectangles not use them. I have found sources that say Graphics2D does not utilize the GPU, so I looked up how to use the graphics card and ended up using System.setProperty("sun.java2d.opengl", "True"); It sounded promising because I thought of C++ OpenGL and the incredible speed it had, but nevertheless, it did not speed up. I still think it is using the CPU.
TL;DR:
How do I utilize the graphics card for optimal performance in Java?
Alright, I couldn't find a good name for this, so I will explain in a bit further detail.
I am making a game using LWJGL and I have gotten some basic rendering done, but now I want to do something a bit more advanced.
Here is the situation:
I have a mesh (positions, normals, texture coords, indices) I generate which can currently support 1 texture, this would be great if I had a single image containing all of the textures, but sadly that isn't the case. I have a individual image for each texture which needs to be loaded individually.
Now, I see a way how I could do this, but it doesn't seem practical or like a good usage of memory.
-Load all the textures into one image and save where each one is in that image for usage with the texture coords.
The textures should NOT blend together, hard coding anything is not an option as I wish to allow modding to be easy to implement, and anywhere from 1 (best case scenario) to 65,536+ textures (worst case scenario) are able to be used in the same "mesh".
I am simply going to use a Texture Atlas as doing anything else seems impractical. Thanks #httpdigest for the suggestion.
I'm going through tutorials to learn Libgdx and every time I see, use same instances or variable to output your sprite objects. So, I can understand that if we want to create and render the same image or sprite, we can simply use the same variables. But now, I need to display several objects (different players/images). Can I still use the same variables to output them or should I create variables for each of them? Cause I've seen it saves space and memory. To output sprites I'm using SpriteBatch, Texture, Sprite and TextureRegion(for moving sprites) objects.
No you cannot use same sprite object for different entities (Players etc) This is not where you will be saving memory. Creation of Sprite object is not heavy weight operation.
Where you can save memory is Textures. You should reuse Textures wherever possible. You do not want to load new Textures every time you create a new Sprite. Let's say there is 1 image which represents a player, you should not be creating that image again and again. You should rather just load it once in memory using AssetLoader and then re-use it.
If you have multiple texture of small sizes, then you should use libgdx's texture packer to pack them into a single image because loading multiple small textures is not a great idea, rather loading a bigger texture sheet is fine because binding Textures is an expensive operation. Read this Texture Atlas for more detailed information.
Also once you don't need a Texture, you should dispose them.
Read this page Memory Management in Libgdx for more information about Disposable interface which is implemented by many of the Libgdx classes.
I also feel that you are trying to optimise your game prematurely. Don't worry about object creations at all at such an early stage. And remember one more thing - Premature optimization is the root of all evil.
I'm making a 2D game in Java and one of the main issues causing low FPS (on my slow laptop) is having to re-draw complex structures to a Graphics instance, such as dials with markings.
The dial and its markings will never change unless the window is resized, so I thought it would be a good idea to draw to a BufferedImage and just re-draw the image rather than re-drawing the details. The position of the needle obviously changes, so this can just be drawn on top.
I've never heard about this being done to improve the FPS of 2D games so I'm wondering if it's actually good practice to store a cache of images or if there's a better way to solve this sort of problem? Are there any issues associated with this that I haven't considered?
Caching images isn't a bad idea: you can rely on raster rendering to be pretty well optimised on most any platform. In my experience (which is admittedly mostly on mobile devices where 2D graphics are concerned) the Graphics.drawXXX() methods are often considerably slower than Graphics.drawImage().
In my experience the vast majority of 2D games out there make use of sprites (i.e. images) for rendering just about everything. Often that's true even when the graphics look like they are rendered using primitives!
Another useful technique to think about is not redrawing regions at all unless you really need to!
EDIT:
As others have mentioned, the major tradeoff is that you're going to be using more memory. You're also going to have to make sure you free up those images once you no longer need them.
Is it good practice to cache parts of a 2D drawing?
You're making a trade-off between drawing speed and storage space. Only you can determine which is more important.
You might consider rendering your dials in advance and saving the images as GIF, JPG, or PNG files. You would have to scale these images to your window size before you draw them.
Are you using double buffering for your Graphics panel?
Yes, that is a good practice, and it's done all the time. Drawing to an image first before displaying it on the screen is called double buffering, and that method can be used in different ways according to the needs of the program.
The downside of double buffering is memory, since it takes more memory to store the second image, but that sounds like a trade-off you'll need to make.
I'm writing a game in Java, LJGWL (OpenGL). I'm using a library that handles a lot of messy details for me, but need to find a lot faster way to do this.
Basically I want to set every pixel on the screen to say a random color as fast a possible. The "random colors" is just an Array [][] that gets updated every 2-3 seconds. I've tried drawing rects and using images, both are pretty slow for what I want to do.
I think I want to learn how to write a GPU shader? That is the fastest way to do this? LJGWL exposes OpenGL api to java. Any basic tutorials on how to get started with OpenGL shaders? Or should I dynamically create a texture of some sort and then just throw up the entire texture, would that be faster?
If it were the case that you were statically displaying the same image, than using a texture or display list would suffice. But as you want to frequently update it, shaders really are the best option. Shader code executes on the GPU and modifies data in GRAM, so you have no bottle neck transferring from CPU to GPU. The next best thing would probably be a Pixel or Frame Buffer Object. Buffer Objects let you read/write to GRAM via DMA (without having to go through the CPU) so they can be pretty fast.
I haven't written any shaders yet, so I can't recommend any good resources. But SongHo's OpenGL pages are a good place to learn about Buffer Objects. (His examples are in C++ though)
Textures are the fastest way to draw something on screen, draw a texture mapped quad into the screen, it should be fast enough. When you need to reupload the texture data, use glTexSubimage2D to update it.
No need to use shaders.
I've yet to do any work with shaders in OpenGL, but given the same scenario in multiple occasions, I handled it with a texture I threw up across the screen on top, and it worked quite effectively.
I don't know how you are drawing your pixels exactly, but this limit you hit could be because of the amount of data you transfer (inefficiently?). Updating a screen full of pixels every 2-3 seconds shouldn't be hard at all. Although shaders bring you closer to the graphics card, they will never make inefficient methods fast, so...
Why is your code so slow?
What code? What code exactly did you try? What texture did you use, render to, ...?
Is it slow? How slow? How fast do you expect it to be?
How quickly can one get 1920x1080(?) pixels in video ram, what's your hardware, drivers, OS?
I think you need to edit/repost before we can help you solve your problem. Just because it is slow, is no guarantee at all that shaders will even be one bit faster.