Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (747)
Games in Android Showcase (226)
games submitted by our members
Games in WIP (829)
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  
  getting very high FPS with standard Java 2D  (Read 6828 times)
0 Members and 1 Guest are viewing this topic.
Offline Cyclonis

Senior Newbie





« Posted 2007-05-28 23:32:56 »

I don't know if this has been posted before, but I get some questions from my friends how I get such high frame rates on my 2D java games. This is the class I use:

/*
 * SpriteManager.java
 *
 * Created on April 28, 2006, 11:28 AM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package bomberdolf;


import java.awt.*;
import java.awt.image.*;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;

import javax.imageio.ImageIO;
/**
 *
 * @author Cyclonis Delta
 */
public class SpriteManager {
   
    private static SpriteManager instance = new SpriteManager();
    private HashMap sprites = new HashMap();
   
    public static SpriteManager get() {
        return instance;
    }
   
    public Sprite getSprite(String ref) {
        if (sprites.get(ref) != null) return (Sprite)sprites.get(ref);
               
        BufferedImage sourceImage = null;
        try {
            URL url = new URL("file:"+System.getProperty("user.dir")+"/gfx/"+ref);
            if (url == null) {
                fail("Can't find ref: "+ref);
            }
            sourceImage = ImageIO.read(url);
        }
        catch (IOException ex) {
            System.out.println("could not read url");
        }
       
        GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
   Image image = gc.createCompatibleImage(sourceImage.getWidth(),sourceImage.getHeight(),Transparency.BITMASK);
       
        image.getGraphics().drawImage(sourceImage,0,0,null);       
       
        Sprite sprite = new Sprite(image);
        sprites.put(ref,sprite);
      
   return sprite;
    }
   
       private void fail(String message) {
      System.err.println(message);
      System.exit(0);
   }
   
}



/*
 * Sprite.java
 *
 * Created on August 21, 2006, 10:31 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package bomberdolf;

import java.awt.*;

/**
 *
 * @author Cyclonis Delta
 */
public class Sprite {
   
    Image img;
   
    /** Creates a new instance of Sprite */
    public Sprite(Image image) {
        img = image;
    }
   
    public void drawMe(Graphics2D gfx, int x, int y) {
        gfx.drawImage(img, x, y, null);
    }
   
}



And in any method that you want to draw a particular sprite you simply call:

SpriteManager.get().getSprite(currentImg).drawMe(gfx, (int)x+15, (int)y);

in which the currentImg is simply a string with the path to your gif image.

I hope it works for you. I got FPS sometimes as high as 400, although they usually hang around at say 200-300
Offline DarkMortar

Junior Devvie




Java Padawan


« Reply #1 - Posted 2007-05-29 01:22:53 »

When I use java2D my fps is also that high, but it will go down down quickly as you draw more stuff. When i enable openGL pipelines they can be as high as 600-1600 , just depends.

Offline Jamison

Junior Devvie




We're all idiots in one way or another.


« Reply #2 - Posted 2007-05-29 02:19:44 »

If I'm using Thread.sleep(1) I get about 550 frames per second, without it I get like 2500 - 5000 fps (starts dropping the more I draw, especially anything other than PNG images). Obviously, I'm not actually getting 5000 fps - it's only as high as the refresh rate. So it's really the UPS im getting so high.

I don't like you. Check out my site Smiley www.gamedevforums.com
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DarkMortar

Junior Devvie




Java Padawan


« Reply #3 - Posted 2007-05-29 04:21:40 »

I actually dont use a sleep at all, i think sleeps are evil. Am I wrong? If the animations are synced with any framerate, there should be no sleeping Smiley

Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #4 - Posted 2007-05-29 10:54:23 »

I actually dont use a sleep at all, i think sleeps are evil. Am I wrong? If the animations are synced with any framerate, there should be no sleeping Smiley

Ideally, using vsync is the best way. Unfortunately, there is no guarantee this will work, so you'll always need to throttle using sleep as a fallback.

Offline broumbroum

Junior Devvie





« Reply #5 - Posted 2007-05-29 12:05:37 »

just a word about Thread.sleep meth: it isn't synch'ed with any object monitor so you won' be able to make the required lap of time with it. Hence you better use wait(long) to do that what you want in a loop.  Tongue

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Offline Kova

Senior Devvie





« Reply #6 - Posted 2007-05-29 16:58:17 »

I actually dont use a sleep at all, i think sleeps are evil. Am I wrong? If the animations are synced with any framerate, there should be no sleeping Smiley

hmm.. if you don't use sleep then you leave very little cpu time for other stuff. For example, if you have another thread for networking or such, it may not respond cleanly or your render thread will have a glitch when it does. Same thing applies for EDT thread responsible for keyboard input (if you use swing). Also without sleep or any timeout from constant rendering and calculations I assume other applications will be highly unresponsive, so for example you couldn't listen mp3s in your favorite player and play the game at the same time.
Offline Abuse

JGO Ninja


Medals: 60


falling into the abyss of reality


« Reply #7 - Posted 2007-05-29 21:31:12 »

If modern operating systems weren't based upon preemptive multitasking, that might be the case.
Offline Jamison

Junior Devvie




We're all idiots in one way or another.


« Reply #8 - Posted 2007-05-29 23:02:19 »

I do not use sleep for the timing. I only use it to prevent the CPU from capping to 100%. That's not a problem on my PC, however, since I have dual core (unless I have a game with multi threading and no sleep).

I don't like you. Check out my site Smiley www.gamedevforums.com
Offline DarkMortar

Junior Devvie




Java Padawan


« Reply #9 - Posted 2007-05-30 03:42:27 »

So should I just use sleep(1) or what? Because i hate loosing frames, even if it doesnt matter, but i have not made a cpu intensive game yet.
I seem to multi task with uncapped frames with no problem, but its early on in development.

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

Junior Devvie




We're all idiots in one way or another.


« Reply #10 - Posted 2007-05-30 11:23:35 »

If your game is using vertical-sync, you shouldn't have the need to sleep. But if your just using say nanoTime() for limiting your frames per second but your running your render loop (drawing not updating) as often as possible, you'll need to sleep for at least 1 millisecond or else you'll cap the CPU to 100%. If you can find an alternative way to prevent your render loop from running as often as possible (like the technique vertical-sync uses but not being in fullscreen), you won't have a need for sleep also.

I don't like you. Check out my site Smiley www.gamedevforums.com
Offline JAW

Senior Devvie


Medals: 2



« Reply #11 - Posted 2007-06-05 14:13:57 »

The operating system will still give cpu time to other processes too. I read once in a book that sleep can be required for some garbage collection and Java background processing. When your main thread takes all the time, any Java deamon and other threads cant process. But this information is based on Java 1.4. Dont know if it is true and if it is still relevant.

On the other side, the eye can see fluid motion at about 22 fps. I dont know the refresh rates of monitors, 60 HZ to 75 HZ is usual, Im not sure if it is the "fps" of the monitor.

Whatever, anything more than 30 or maybe 50 fps is just not visible to the human eye. Computing 100 frames or more would be a waste of processing. A sleep call gives other Threads room to work and doesnt do damage to your game, as long as the fps are steady above 30.

-JAW
Offline Kova

Senior Devvie





« Reply #12 - Posted 2007-06-05 14:50:39 »

24fps is for human eye, but due to how monitors work 40-100 fps is needed. Best to vsync it (fps equal to monitor refresh rate). I would aim for 100 and not beyond.
Offline keldon85

Senior Devvie


Medals: 1



« Reply #13 - Posted 2007-06-05 20:19:52 »

Let me start by saying the following. You don't need to sleep, but you do need to sleep. Basically you run an iteration of your game/rendering loop and then rest until the next frame begins. You do not do this by a particular time, instead you do something like this:
1  
2  
3  
4  
5  
while ( looping ){
   waitVBL();        // wait for the screens vertical blank
   updateScreenOutput();
   doProcessing();
}


My waitVBL method goes through my own synchronization mechanism since I need a set UPS rate.

Ideally, using vsync is the best way. Unfortunately, there is no guarantee this will work, so you'll always need to throttle using sleep as a fallback.
That's pretty much how it should be done. Like I said above, I do my own vertical syncing.

just a word about Thread.sleep meth: it isn't synch'ed with any object monitor so you won' be able to make the required lap of time with it. Hence you better use wait(long) to do that what you want in a loop.  Tongue
Yes, that's part of how my method works.

hmm.. if you don't use sleep then you leave very little cpu time for other stuff....
You can also Object.wait();

So should I just use sleep(1) or what? Because i hate loosing frames, even if it doesnt matter, but i have not made a cpu intensive game yet.
I seem to multi task with uncapped frames with no problem, but its early on in development.
See my method above; in fact here is an example ...

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
package javaThreadSynchronization;

public class WaitVBLSynchronization implements Runnable {
   public boolean running = true;
   public Object waitingObject=null;
   public static void main ( String argv [] ) throws InterruptedException {
      WaitVBLSynchronization sync = new WaitVBLSynchronization();
      for ( int i = 0; i < 6; i ++ ) {
         sync.waitVBL();
         System.out.println(i);
      }
      sync.running = false;
   }
   public WaitVBLSynchronization (){
      Thread t = new Thread(this);
      t.start();
     
      // this high priority is valid because of how little the thread does
      t.setPriority(Thread.MAX_PRIORITY);
   }
   
   public void waitVBL () throws InterruptedException{
      waitingObject =  new Object();
      synchronized ( waitingObject ) {
         
         waitingObject.wait();
         waitingObject = null;
      }
   }
   
   public void run() {
      while ( running ){
         try {
            Thread.sleep(1000);
         } catch (InterruptedException e) { }
         
         if ( waitingObject != null ) {
            synchronized (waitingObject){
               waitingObject.notify();
            }
         }
      }
   }
   
}

Pages: [1]
  ignore  |  Print  
 
 

 
xxMrPHDxx (133 views)
2017-12-31 17:17:51

xxMrPHDxx (43 views)
2017-12-31 17:15:51

xxMrPHDxx (53 views)
2017-12-28 18:11:33

Ecumene (219 views)
2017-09-30 02:57:34

theagentd (327 views)
2017-09-26 18:23:31

cybrmynd (371 views)
2017-08-02 12:28:51

cybrmynd (408 views)
2017-08-02 12:19:43

cybrmynd (375 views)
2017-08-02 12:18:09

Sralse (346 views)
2017-07-25 17:13:48

Archive (1113 views)
2017-04-27 17:45:51
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
java-gaming.org 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‑gaming.org
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!