Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (756)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (842)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Accelerated Java?  (Read 2255 times)
0 Members and 1 Guest are viewing this topic.
Offline manisto

Junior Newbie

« Posted 2006-05-11 19:18:17 »


I've just begun creating a framework for 2D games in Java, and I have read some of the posts here. I have set up some code to test the things I have learned so far, but something seems på be wrong. It's just a small demo that runs for 5 seconds and then shows you the framerate. It draws 1000 sprites @ 128 x 128 as fast as possible (no sleeps). I just made it to find out how to get the best performance from Java2D.

This is the testcode (I outcommented fullscreen for testing purposes):

package main;

import java.awt.*;
import java.awt.image.*;
import javax.swing.JFrame;

public class Main extends JFrame implements Runnable
   private static final long serialVersionUID = 1L;
   private GraphicsConfiguration gc;
   private GraphicsDevice gd;
   private BufferStrategy buffer;
   private BufferedImage bImage;
   public Main()
      setSize(800, 600);
   public void start()
      gc = getGraphicsConfiguration();
      gd = gc.getDevice();
      //gd.setDisplayMode(new DisplayMode(800, 600, 16, 60));
      buffer = getBufferStrategy();
      bImage = gc.createCompatibleImage(128, 128);
      new Thread(this).start();
   public static void main(String args[])
      Main main = new Main();

   public void run()
      double fps = 0;
      long frames = 0;
      Graphics2D g;
      long startTime = System.nanoTime() / 1000000L;
      long lastFrame = startTime;
      while (lastFrame <= (startTime + 5000))
         g = (Graphics2D)buffer.getDrawGraphics();
         g.clearRect(0, 0, 800, 600);
         for (int i = 0; i < 1000; i++)
            g.drawImage(bImage, 100, 100, null);
         g.drawString(String.valueOf(fps), 50, 50);
         if (!buffer.contentsLost())
         lastFrame = System.nanoTime() / 1000000L;
         fps = (double)frames / (lastFrame - startTime) * 1000;

And this is the trace count:

220 calls to sun.java2d.loops.DrawGlyphList::DrawGlyphList(OpaqueColor, SrcNoEa, AnyInt)
220220 calls to"Integer RGB DirectDraw", SrcNoEa, "Integer RGB DirectDraw")
1 call to GDIFillRect
2 calls to sun.java2d.loops.Blit::Blit(IntRgb, SrcNoEa, IntRgb)
226 calls to DDFillRect
220669 total calls to 5 different primitives

But from what I've read on the forums, I shouldn't have those Win32BlitLoops, the should be D3DBlitLoops, right? So, what am I doing wrong? I'm not using any -D (other than trace). I'm running Java 1.5 on an XP Pro machine @ Athlon XP 2500+ with 1 GB RAM, with a GeForce FX 5700 Ultra, for those of you who just have to know! Smiley

Please help me, it's driving me insane! Smiley And by the way, is there anything that can be done better? (Except the fact that I need to do some Thread.sleep() between render cycles)?
Offline fletchergames

Senior Devvie

« Reply #1 - Posted 2006-05-12 02:50:17 »

I can't see any problem with your code, though creating the main window should be in a seperate class instead of the main one.  It doesn't make much difference in a demo, but it's much easier in real programs.

Are the results similar for full-screen mode?  I don't know much about it, but maybe DirectDraw is only for full-screen mode.

Your code doesn't seem to fundamentally different from the code I wrote for the same thing, so I don't know what to tell you.  If you haven't already, you should buy a book about Java game programming.  It seems likely that you already have.

If you like, I could post my own code, which has some additional code that makes it safe to use with Swing, but I doubt that would effect your problem.

My advice to you is not to worry.  It's fast enough, isn't it?  Does it really matter whether it uses DirectDraw or not?
Offline CommanderKeith
« Reply #2 - Posted 2006-05-12 03:06:55 »

get the BufferCapabilities of your BufferStrategy and BufferedImage and find out whether they're accelerated.  If they are, then it's apples, acceleration is being used. 

On windows, setting fullscreen and doing bufferStrat.createBufferStrategy(3) may give you page-flipping rather than blitting (for the buffer strat only, not the BufferedImage), which may be faster, and it will lock the frame rate to the vertical retrace of the screen, preventing 'tearing' (but then you can't get FPS > screen refresh).

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline trembovetski

Senior Devvie

If only I knew what I'm talking about!

« Reply #3 - Posted 2006-05-12 21:46:36 »

In 1.5 the Direct3D pipeline is very incomplete, so you won't get much out of it.

Were you not satisfied with the DirectDraw pipeline results? Win32BlitLoops::Blit
is a DirectDraw blit.

You can try the Direct3D pipeline in 1.6 (with -Dsun.java2d.d3d=true), or you can also
try the OpenGL pipeline in 1.5 or 1.6. Note that there may be driver issues with the OGL
pipeline, so make sure you have the latest drivers (although some problems aren't fixed
even in those).

Offline Linuxhippy

Senior Devvie

Medals: 1

Java games rock!

« Reply #4 - Posted 2006-05-13 08:09:17 »


Just because of interest, the D3D pipeline does basically the same things that the OpenGL pipeline does (except the stuff you mentioned in your article), using more or less the same functionality of the underlayig graphic apis, right?
But why do even many really old boards work fine with the d3d pipeline whereas the OpenGL pipeline only works on some cards and only with latest drivers installed?

Is D3D easier to implement for driver programmers, or is OpenGL more complex.
Or are OpenGL implementations more likely to be broken since they don't get that much attention from consumers?

Thank you in advance, lg Clemens
Offline manisto

Junior Newbie

« Reply #5 - Posted 2006-05-13 11:49:00 »

Thank you for your replies.

And to the most of you; yes, I do get the performance I need. But I just became a bit confused because of the many threads mentioning the D3DBlitLoops. It's simply because I prefer to do things "as right as possible" from the start, to prevent a lot of re-coding.

Once again, thank you all! Smiley
Pages: [1]
  ignore  |  Print  

DesertCoockie (54 views)
2018-05-13 18:23:11

nelsongames (86 views)
2018-04-24 18:15:36

nelsongames (76 views)
2018-04-24 18:14:32

ivj94 (761 views)
2018-03-24 14:47:39

ivj94 (93 views)
2018-03-24 14:46:31

ivj94 (645 views)
2018-03-24 14:43:53

Solater (106 views)
2018-03-17 05:04:08

nelsongames (187 views)
2018-03-05 17:56:34

Gornova (428 views)
2018-03-02 22:15:33

buddyBro (1088 views)
2018-02-28 16:59:18
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!