Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should I use to display game graphics?

Tags:

java

graphics

I have a system in place for a game yet I don't know what I should use to display it. I am making a vertical shooter game and I have written the methods for all the classes controlling enemies and players, but I have not idea how to efficiently display the game. I was thinking a Canvas, that would repaint every frame, but is that really the most efficient method?

Important details:

  1. ideal framerate: 25fps
  2. It is a 2d game
  3. There are anywhere between 25-100 objects on the screen at any one time, all of which are moving
  4. All objects being displayed are images, all in PNG format
  5. The window is 640px by 480px
  6. Right now all the images are loaded as BufferedImage, although I could easily change this

7. I need a coordinate plane. This is the only fundamental part that cannot be changed without completely restructuring my code.

Most importantly the way I have everything set up, every frame all of the objects move and interact in a coordinate plane I devised (deals with collision detection and movement, no graphical component), then everything should get painted to the screen, simply by going through the ArrayLists which keep track of all moving objects and painting them one by one.

like image 531
Will Avatar asked Jun 06 '11 01:06

Will


People also ask

Which graphics settings affect FPS?

Anti-aliasing and shadows are usually the two biggest settings that affect FPS. Anisotropic filtering could also be a setting that can affect FPS depending on the game. Textures could affect your game based on what your CPU/GPU is, but as long as it's not a really low end one it won't affect it that much.


3 Answers

If Swing is acceptable, JPanel is double-buffered by default, and a javax.swing.Timer with a period of 40 ms will give you ~25 Hz updates. This example shows the basic approach, while this example shows a number of images in motion.

I need a coordinate plane.

It's not unusual to have the the model and view use different coordinates; all that's needed are functions to map one system to the other. In this game, the view relies on four methods that map tiles to pixels and vice-versa. The same approach is outlined here for map tiles.

like image 172
trashgod Avatar answered Oct 14 '22 02:10

trashgod


You have a number of options available to you:

Firstly, you could use one of the existing Java game frameworks:

  1. JMonkeyEngine (http://jmonkeyengine.com/)
  2. Slick (http://slick.cokeandcode.com/index.php)

(Slick is aimed at 2D graphics, while JMonkey is aimed at 3D and uses OpenGL - While I have looked into their use, I've not actually used them myself)

Alternatively you can code everything yourself. From the sounds of things this is your first (graphical) game, so you may want to read up on a technique known as double buffering, whereby you write each frame off-screen and the just paint the whole thing to screen, as this can lead to smoother animation.

To get you into games development a bit more, I would highly recommend reading this site, Killer Game Programming in Java by Dr Andrew Davison, as he gives some good pointers, and also provides a good progressive learning path for new game developers, and moving them into 2D and then 3D development.

HTH

like image 23
Crollster Avatar answered Oct 14 '22 03:10

Crollster


The answer really depends on whether this game is 2D or 3D.

If your game is 2D, an easy way to do what you want is to use Java's own 2D Graphics API. A good tutorial to start you off (at least in my opinion) can be found at The Java Tutorials. In my experience I have found that Java's graphics API is easy to learn, and is more efficient than one might expect. The basic technique is to have your game code keep track of the positions of all your objects, then translate those coordinates into screen coordinates and display appropriate images at those locations. I made a (very, very simple) game in Java once, and this is the method I used.

If your game is in 3D, OpenGL is definetly the way to go. I have only limited experience with OpenGL, so I'm not sure how easy the Java bindings are to work with. In general, 3D programming is a massive topic, so if this is a first-time project or you aren't prepared for a major time investment, I would attempt to find a good game framework to use or make the game 2D. If you are interested in OpenGL, or 3D programming in general, a quick Google turned up the JOGL project. I would recommend investigating JOGL as a way to access the OpenGL API from within Java code, but for actually learning OpenGL I recommend The OpenGL SuperBible(5th edition), commonly known as "The Blue Book". The code examples are all in C++, but for the OpenGL functions it could possibly be just a matter of using a wrapper library. For example:

glDrawElements(...);

May become:

JavaGLWrapperObject.glDrawElements(...);

Unfortunately I can't give concrete examples because I haven't used OpenGL with a Java program, but the above example very coarsely approximates how OpenGL ES is used on the Android platform.

As far as performance... Java's API comes with a non-trivial ammount of overhead, but I could see it doing alright for your purposes. You may have to put a little more effort into making your algorithms efficient, and your game may not run as well on less-capable hardware. If you do decide to go the OpenGL route, it will almost certainly be much faster (again, depending on how efficient your algorithms are), but is correspondingly much harder to learn and get started with. Certainly doable, but it will be a challenge.

like image 44
rjacks Avatar answered Oct 14 '22 01:10

rjacks