If I was making a 2D game in Java, would using the default Graphics2D be fast enough? Would I get good FPS if it's just 2D? (even if there is a good amount of sprites and other things going on)
Or when your doing any kind of game programming, 2d or 3d, should you opt for opengl/directx?
Also when you render stuff with Graphics2D, is it all rendered on the CPU? Or is it hardware accelerated?
You should use opengl/directx, or some java binding if you must use java (such as JMonkey). If you are new to game programming, I'd suggest using the XNA framework, as it simplifies a lot of things. I have used XNA, and it is fairly easy to use, and plug in different libraries. If you want a browser based game, javascript + HTML5 Canvas2d, or webgl may be the best route.
You will always get better performance out of using direct binding libraries over a 2D Graphics API type library. The main thing to do is make sure you know how images and textures are being stored / rendered, so you aren't making the framework do extra work. Extra work will result in low FPS.
Related
I am developing a shoot'em up type video game using Java Swing and I'm using a Java Swing Timer to control all of the screen updating. The main screen uses a BorderLayout, and I perform my graphics on a Panel contained within the BorderLayout, and use the AWT Graphics calls to draw.
Now as my game progresses, I would like to speed up the movement of my screen objects (ships), yet I still want them to smoothly cross the screen. I thought I could speeed things up by dropping the timeout value for the Java Swing Timer, down to around 5ms. But, I've noticed when I set it to anything less than 15ms, there does not seem to be much difference. Once you cross that threshold, there is almost no noticeable difference in performance. -Why is that?-
Another option would be to increase how many pixels each ship moves per update, but anything beyond 3 or 4 pixels, and things start to look jumpy.
Any ideas? And really want to keep the game in Swing, would prefer at this point not porting to a 3rd party library.
Thanks.
In all likelihood, this isn't a software issue, nor is it fixable with software. Your screen probably only refreshes about 60 times a second, meaning that the frames are only drawn 60 times per second, or once every (approximately) 16 milliseconds. Since this is a hardware barrier, there's no way to get it to update faster with software. You can probably count on your users only having 60Hz monitors, too, so it's more worthwhile to look into other solutions.
One solution that pops to mind is adding in motion blur, to make it seem like the ships are moving faster when they really aren't. It'll allow you to 'jump' a greater distance before it looks jumpy, since the blur tricks the eye into thinking it's going really fast instead of hopping across the screen. Unfortunately, the only things that I see to do motion blur are third-party libraries, though you may have better luck Googling.
I advise you that you change your Swing engine for JavaFX (at least), a technology with better performance and more tools to your disposal. Swing nowadays is considered a dead technology, as well as with the AWT before it. A great place to study and start JavaFX would be this one:
http://docs.oracle.com/javase/8/javase-clienttechnologies.htm
The ideal for the development of games, would be to use a library prepared for it, such as libGDX:
http://libgdx.badlogicgames.com/
But if your desire is to create a game in Swing, who am I to judge? Sometimes it is good to see old things. I myself still like Swing, even being obsolete compared to other things.
I think you may be implementing a wrong gameloop. Swing Timer does not really seem like a good way to update game logic. Even with different settings between different computers, is generally practical and easy to implement a gameloop to work properly, especially for the correct movement of the characters.
You see, a gameloop is the heart of a game, and needs to be implemented straight otherwise the game is developed wrong and with several limitations. Generally, in gameloop we have to take into account the desired FPS. Among game updates, we must take the time elapsed between the last update and the current update, so-called delta by many developers. You will find many materials about it on the internet, which I recommend:
http://dewitters.koonsolo.com/gameloop.html
http://www.java-gaming.org/index.php?topic=21919.0
https://www.youtube.com/watch?v=9dzhgsVaiSo
The past links will help you for sure, but if you want more about it, I recommend that you take a look at this book:
http://www.brackeen.com/javagamebook/
Even the material being old (as Swing is) you'll find details of how to implement a good gameloop with a keyboard and mouse iinput system, sound, the use of the best possible performance swing has to offer, among other cool things. And one more thing... To give you a better result with respect to the movement of the characters, I advice you to use position variables (X and Y) with decimal types such as float or double.
I hope you can do what you want. Good luck!
I'm a novice programmer and recently I came across many methods of animation:
Using BufferedImages , ie. draw to image and display using double buffering or triple buffering methods .
Making my sprites components by extending Component or Button . and repainting by repaint(g).
Rastering, using rasters and integer arrays, bitmaps and the like.
I realise that method 1 and 2 are similar as they use paint() methods , however Rastering involves self-made functions , eg. creating functions that set background by traversing the whole array representing each pixel and setting colour to desired colour .
I've seen many folks online use raster methods even though 1 & 2 seem simpler .
Please help me out here guys and tell me what path i should follow and why .
The first and second methods are very similar and would come down to the context of the problem.
The third option would, for the most part come down to providing a flexible API which was independent of the toolkit or framework which was to be used to dipslay it. One could argue that there is a speed increase, but with Swing and JavaFX working more directly with the DirectX and OpenGL pipelines, this is less of an issue.
Unless your planning on dong some seriously low level functionality or interfacing to a non-standard API I wouldn't worry to much about it (some effects painting might be faster done this way, but it's all about context)
Personally, it would focus on developing an understanding of the basics and principles involved and how they might be implemented using the first two options as it allows you to focus on those concepts with a relatively easy API
As time progress or you come across a situation that can't be resolved using these techniques, then it might be time to consider playing with the rastering approach.
Remember though, you still need to get that byte array into a format that the API wants which in of itself, will add overheads
I was given the task of improving the performance of a stock market charting software that uses SWT's GC to draw the charts. The chart drawing needs to be improved because the charts sometimes are redrawn many times per second, and it consumes lots of processor time.
After googling a little, I found a blog entry that suggests the direct modification of ImageData objects instead of using GC's methods, promising great performance gains.
It's an easy task to draw horizontal and vertical straight lines and square shapes using this technique, but when it comes to drawing circles and other irregular shapes there is no easy way.
Does anyone know if there is a library to draw shapes on ImageData objects, just like GC's methods do on Image objects?
Also, does anyone know another way to improve SWT performance?
Thanks in advance.
Measure the performance of your solution. Where is most time spent? Guessing is not enough. In 90% of the cases, your guesses will be wrong. If you don't know, you can't solve the issue.
SWT itself is not slow. In fact, SWT is just a very thin layer over the respective OS system calls to draw.
One of the problems of SWT is that it's synchronized. To make sure that threading issues cause no problems, there is a global lock. So if you render from several threads at once, this can be a problem.
Or maybe you're not caching resources like colors and fonts properly. These are expensive to create. How many GCs do you create? Do you keep one around or do you create a new one per frame?
But I'm just guessing here. Unless you can prove with a performance monitor "most time is spent in ...", there is no way to help you.
Instead of improving the performance of the drawing routines I would concentrate on the drawing logic. Maybe you can just redraw the difference between the old and the new chart? This of course largely depends on how the charts look like and what data they present.
Try to reduce the drawing operations. Do not try to make them faster.
It may seem like a big step (and it is), but the best approach to improve SWT drawing performance for me was to switch to OpenGL rendering. I do not imply that you should draw your whole UI with it, but the charts part.
There are many approaches to do that. My choice was to use the JOGL library. There are also some examples around the net, showing how to integrate with SWT.
The downside to this approach is that you have to learn and use a new API which is very different from what one knows from java.
On the other hand, as your scenes are getting more complex, the gains of externalizing the rendering to the GPU are getting bigger. I have experienced FPS gains between 2x and 10x. Another good thing is that you don't have to make a deep dive into OpenGL, there are great libraries like jMonkeyEngine, hiding much of the underlying complexity.
Does anyone knows a good 2D engine for Java with sprites, animations and collisions handling?
JGame is probably what you're looking for.
You might also want to check out this question ( https://stackoverflow.com/questions/293079/java-2d-game-frameworks ) that has a list of Engines out there and a bit of feedback on some of them. Hope it's helpful.
Slick2D seems to be a pretty solid choice.
It's widely used and it is based on OpenGL (via LWJGL) so you can get some pretty good performance if you need it.
Greenfoot, from the makers of BlueJ, would be a good choice if it is your first time with game-development in Java. It is not even an easy-to-learn API, but also comes with a development-environment with fully integrated Greenfoot surface.
The game-environment is the greenfoot.World while every element in the game is a greenfoot.Actor instance. The Actor class provides a method for true bitmap-intersection (greeenfoot.Actor.intersects()).
jGame
Arianne
Tangent: You'd be better off branching away from Java. The game development industry is C++/Python heavy, with C# in third.
I know how to draw basic objects using JOGL or LWJGL to connect to OpenGL. What I would like is something that can generate some kind of geometry for trees, similar to what SpeedTree is famous for. Obviously I don't expect the same quality as SpeedTree.
I want the trees to not look repetitive. Speed is not a concern, I do not expect to need more than 100 trees on screen at one time.
Are there free tree-drawing libraries available in Java? Or sample code or demos?
Is there anything in other languages which I could port or learn from?
http://arbaro.sourceforge.net/
http://www.propro.ru/go/Wshop/povtree/povtree.html
Non java: http://www.aust-manufaktur.de/austt.html
There are thousands of methods. A better question would define 'best' in a more confined way. Are we talking 'best' as in speed of drawing (suitable for thousands or millions of trees)? Best as in best-looking? etc.
2D or 3D?
In 2D, a common way is to use L-systems.
I also tried an OO approach, defining objects for trunk, branches, leaves, all extending an abstract class and implementing a Genotype interface (to vary the kind of trees).
Not sure if it is efficient (lot of objects created, particularly if I animate the tree) but interesting to do.
Here are a couple resources that may be helpful:
gamedev thread on the subject - contains some useful advice/suggestions
ngPlant - an open-source procedural plant generation tool. It is not written in Java, but you may be able to find ideas in its algorithms.
If you are using eclipse/SWT, try Draw 2D.
If you're serious about getting good-looking, fast trees, there's a commercial C++ library SpeedTree. Lots of big-time games use it (e.g., GTA, Elder Scrolls).
A combination of OpenSceneGraph and SpeedTree has worked for me.
I know of two libraries that enable the usage of OpenGl with java.
LWJGL (Light Weight Java Gaming Library), which imo is the better one due to its simplicity and its similarity to using opengl with c/c++.
JOGL If you want to mix swing components with opengl this may be the better choice, I've never used it but several years ago it was known to be pretty buggy, I don't know if its matured since then.
As for drawing trees, there are many ways to do so like the other poster said, you might want to be more specific.
edit: I guess I misunderstood the question a bit, oh well : / You can load in a 3d model of a tree and display that.
http://www.codeplex.com/LTrees has some source code on that. it's c++ though.