Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  A damn good performance improvement  (Read 1723 times)
0 Members and 2 Guests are viewing this topic.
Offline K.I.L.E.R

JGO Strike Force
***

Posts: 764
Medals: 1


Java games rock!


« on: 2004-10-08 04:02:46 »

66fps - 88fps.

Trick?

When you set up a Window ALWAYS use:

SwingUtilities.invokeLater
(
     new Runnable()
     {
           public void run()
           {
                 doWindowSwingInit();
           }
     }
);

I was using J2D.

Code:

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  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
private static Main instance;
    private BufferStrategy bs;
    private Graphics2D g;
   
    //game
   private GameStart game;
   
    private int fps;
   
   
    private void loop()
    {
      g = (Graphics2D)bs.getDrawGraphics();
      g.clearRect(0, 0, getWidth(), getHeight());
     
      //logic
     game.logic();
     
      //rendering
     game.render(g);      
     
      //print fps in bottom corner
     g.setColor(Color.BLACK);
      g.drawString("FPS:" + fps, 15, getHeight() - 15);
     
      bs.show();
      g.dispose();
    }
   
   
    /** Creates a new instance of Main */
    public Main()
    {
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.setSize(640, 480);
      this.setLocationRelativeTo(null);
      this.setLocation(getX() - 100, getY() - 100);
      this.setIgnoreRepaint(true);
      this.setResizable(false);
     
      this.addKeyListener(new Input());
     
      this.setVisible(true);
     
      this.createBufferStrategy(3);
      bs = this.getBufferStrategy();
      g = (Graphics2D)bs.getDrawGraphics();
    }
   
    private void initGame()
    {
      game = new GameStart();
    }
   
    /**
     * @param args the command line arguments
     */

    public static void main(String[] args)
    {
      SwingUtilities.invokeLater
      (
          new Runnable()
          {
            public void run()
            {
                instance = new Main();
            }
          }
      );
     
      instance.initGame();
     
      long cTime, eTime;

      while(true)
      {
          if( instance.isFocusOwner() )
          {
            cTime = System.nanoTime();
            //added to limit framerate
           instance.pause();
            //limiter

            instance.loop();

            eTime = System.nanoTime() - cTime;
            instance.fps = (int)(1000000000/eTime);
          }

          instance.pause();
      }
    }
   
    private void pause()
    {
      try
      {
          Thread.sleep(10);
      }catch(InterruptedException e)
      {
          e.printStackTrace();
      }
    }
   
   
    private class Input implements KeyListener
    {
      public void keyPressed(KeyEvent e)
      {
          switch( e.getKeyCode() )
          {
            case KeyEvent.VK_ESCAPE:
                System.exit(0);
                break;
          }
      }
     
      public void keyTyped(KeyEvent ee)
      {
      }
     
      public void keyReleased(KeyEvent eeee)
      {
      }
    }
   
    public static int getResWidth()
    {
      if( instance == null )
          return 0;
      return instance.getWidth();
    }
   
    public static int getResHeight()
    {
      if( instance == null )
          return 0;
      return instance.getHeight();
    }

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline Herkules

JGO Kernel
*****

Posts: 1522
Medals: 1


Friendly fire isn't friendly!


« Reply #1 on: 2004-10-08 05:20:13 »

Hm, doesn't that block the swing-thread and force it into a loop?

HARDCODE    --     DRTS/FlyingGuns/JPilot/JXInput  --    skype me: joerg.plewe
Offline K.I.L.E.R

JGO Strike Force
***

Posts: 764
Medals: 1


Java games rock!


« Reply #2 on: 2004-10-08 07:00:29 »

I wouldn't have a clue.

I can barely find any information about that method.
All I know is that I have a framerate jump by doing it this way.

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Games published by our own members! Go get 'em!
Offline Herkules

JGO Kernel
*****

Posts: 1522
Medals: 1


Friendly fire isn't friendly!


« Reply #3 on: 2004-10-08 07:47:01 »

Basically I'd feel that blocking the swing thread is not a particularly good idea.

Isn't is possible that you did something before that brought you in concflict with the swing thread  (e.g. you painting AND swing painting) that is now is longer possible because you KILLED the enemy instead of making peace?

HARDCODE    --     DRTS/FlyingGuns/JPilot/JXInput  --    skype me: joerg.plewe
Offline phazer

Full Member
**

Posts: 144


Come get some


« Reply #4 on: 2004-10-08 07:47:16 »

SwingUtilities.invokeLater() posts an event on the AWT event queue and returns immediately. Shouldn't affect your FPS in any way. Your code looks really dangerous since you set Main.instance inside the event and use it without checking that it has been created.

You also draw graphics outside of the AWT event thread which looks dangerous. I would advise you to post events to the AWT event thread instead of looping in the main thread. Look at javax.swing.Timer for example.

Offline Herkules

JGO Kernel
*****

Posts: 1522
Medals: 1


Friendly fire isn't friendly!


« Reply #5 on: 2004-10-08 08:05:22 »

Quote
SwingUtilities.invokeLater() posts an event on the AWT event queue and returns immediately.


Thats not the point. The problem is that run() will be performed in the Swing thread but never ends! Means the event pump will actually stop!

Normally that mechanism is used to perform short actions in the Swing-Thread (set a new model, extend a list, ...).

HARDCODE    --     DRTS/FlyingGuns/JPilot/JXInput  --    skype me: joerg.plewe
Offline phazer

Full Member
**

Posts: 144


Come get some


« Reply #6 on: 2004-10-08 08:23:10 »

ASFAICS he's not blocking the AWT event thread. The loop is outside of the invokeLater().

Offline Herkules

JGO Kernel
*****

Posts: 1522
Medals: 1


Friendly fire isn't friendly!


« Reply #7 on: 2004-10-08 08:44:31 »

oh yes, I'm sorry!!!

Missed one closing curley Smiley

So everything I said is pointless.

this.setIgnoreRepaint(true) .... does that make a difference?




HARDCODE    --     DRTS/FlyingGuns/JPilot/JXInput  --    skype me: joerg.plewe
Offline Herkules

JGO Kernel
*****

Posts: 1522
Medals: 1


Friendly fire isn't friendly!


« Reply #8 on: 2004-10-08 08:47:14 »

What about these multithreading things with threads owning monitors to objects created ... does wait()/notify() come into play somewhere?

HARDCODE    --     DRTS/FlyingGuns/JPilot/JXInput  --    skype me: joerg.plewe
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.084 seconds with 19 queries.