Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (116)
games submitted by our members
Games in WIP (563)
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  
  A damn good performance improvement  (Read 2403 times)
0 Members and 1 Guest are viewing this topic.
Offline K.I.L.E.R

Senior Member




Java games rock!


« Posted 2004-10-08 08: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

Senior Member




Friendly fire isn't friendly!


« Reply #1 - Posted 2004-10-08 09: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

Senior Member




Java games rock!


« Reply #2 - Posted 2004-10-08 11: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! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Herkules

Senior Member




Friendly fire isn't friendly!


« Reply #3 - Posted 2004-10-08 11: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

Junior Member




Come get some


« Reply #4 - Posted 2004-10-08 11: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

Senior Member




Friendly fire isn't friendly!


« Reply #5 - Posted 2004-10-08 12: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

Junior Member




Come get some


« Reply #6 - Posted 2004-10-08 12:23:10 »

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

Offline Herkules

Senior Member




Friendly fire isn't friendly!


« Reply #7 - Posted 2004-10-08 12: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

Senior Member




Friendly fire isn't friendly!


« Reply #8 - Posted 2004-10-08 12: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]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (30 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (28 views)
2014-09-20 20:14:06

BurntPizza (32 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!