Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (489)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (555)
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  
  repaint vs custom draw method performance in ms  (Read 1501 times)
0 Members and 1 Guest are viewing this topic.
Offline jh62

Senior Newbie





« Posted 2013-06-14 07:04:49 »


Okey, so I'm messing around trying to get the best draw method timing out of my gameloop and I found something that I don't quite understand.

Keep in mind this is only a test, not the final code. I'm trying to tweak what I can before starting a real project, so maybe some things are kind of sloppy right now.

This is my main gameloop in Start.class:

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  
@Override
   public void run(){
       final int SKIP_TICKS = 1000000000 / FPS;
       final int MAX_FRAMESKIP = 5;

       int loops = 0;
       
       double before =  System.nanoTime();
       
       while(isRunning){
         
         loops = 0;
         ScreenState state = window.getCurrentState();  
         
         double now = System.nanoTime();
         
           while( now - before > SKIP_TICKS && loops < MAX_FRAMESKIP) {              
               state.run();
               before += SKIP_TICKS;
               loops++;              
           }

           //window.render();
          window.repaint();
           
           double lastRenderTime = now;
           
           while ( now - lastRenderTime < SKIP_TICKS && now - before < SKIP_TICKS)
            {
               Thread.yield();
               try {Thread.sleep(1);} catch(Exception e) {}            
               now = System.nanoTime();
            }
      }
   }


This is the render and paint method in my Screen.class:

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  
@Override
   public void paint(Graphics g) {
      do {
            // The following loop ensures that the contents of the drawing buffer
           // are consistent in case the underlying surface was recreated
           do {
                // Get a new graphics context every time through the loop
               // to make sure the strategy is validated
               Graphics g2 = strategy.getDrawGraphics();

                // Render to graphics
              // currentstate.paintComponents(g);
               currentstate.draw(g2);
                // Dispose the graphics
               g2.dispose();

                // Repeat the rendering if the drawing buffer contents
               // were restored
           } while (strategy.contentsRestored());

            // Display the buffer
           strategy.show();

            // Repeat the rendering if the drawing buffer was lost
       } while (strategy.contentsLost());  
   }
   
   public void render(){
        do {
            // The following loop ensures that the contents of the drawing buffer
           // are consistent in case the underlying surface was recreated
           do {
                // Get a new graphics context every time through the loop
               // to make sure the strategy is validated
               Graphics g = strategy.getDrawGraphics();

                // Render to graphics
              // currentstate.paintComponents(g);
               currentstate.draw(g);
                // Dispose the graphics
               g.dispose();

                // Repeat the rendering if the drawing buffer contents
               // were restored
           } while (strategy.contentsRestored());

            // Display the buffer
           strategy.show();

            // Repeat the rendering if the drawing buffer was lost
       } while (strategy.contentsLost());          
   }


So I execute this in either:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public void draw(Graphics g){
      if(!genDone) return;
     
         start = System.currentTimeMillis();  
   
         g.setColor(Color.red);
         g.fillRect(0, 0, Screen.getInstance().getWidth(), Screen.getInstance().getHeight());
         
         for(Tile t : tiles)
            g.drawImage(t.img.getImage(), t.x+trackX, t.y+trackY, null);
         

         System.out.println("Draw: " + (System.currentTimeMillis() - start));

   }


if i run the code from paint(Graphics g) i get:

1  
2  
3  
20 iterations: 121
20 iterations: 298
20 iterations: 131


Now, if I run the sme code from render(Graphics g):

1  
2  
3  
20 iterations: 296
20 iterations: 297
20 iterations: 280


Why is there that kind of difference?
Offline Kerai

Junior Member


Medals: 4



« Reply #1 - Posted 2013-06-14 09:56:36 »

What are you trying to accomplish here? This looks kinda stupid.

Strategy is used with active rendring, paint (and repaint) is used with passive rendering.

In paint() method you should NOT use buffer strategy - it already gives you Graphics object.

And you should not use passive rendering for game, use active rendering. Draw on canvas, don't even touch the window/frame.

Quote
1  
2  
         for(Tile t : tiles)
            g.drawImage(t.img.getImage(), t.x+trackX, t.y+trackY, null);
This is going to be slow if you render every tile with separate render call. Also, you shouldn't have an instance of class per every tile, that's memory inefficinent. You should hold tile types in array.
Offline jh62

Senior Newbie





« Reply #2 - Posted 2013-06-14 10:33:25 »

What are you trying to accomplish here? This looks kinda stupid.

Strategy is used with active rendring, paint (and repaint) is used with passive rendering.

In paint() method you should NOT use buffer strategy - it already gives you Graphics object.

And you should not use passive rendering for game, use active rendering. Draw on canvas, don't even touch the window/frame.

Quote
1  
2  
         for(Tile t : tiles)
            g.drawImage(t.img.getImage(), t.x+trackX, t.y+trackY, null);
This is going to be slow if you render every tile with separate render call. Also, you shouldn't have an instance of class per every tile, that's memory inefficinent. You should hold tile types in array.

Okey, I'll have to check the code and change things accordingly.

The Tile.Class holds data of every tile in the grid with their own bounding box, so whenever I pass the mouse I know above wich tile I am. This was the simplest solution for me, instead of doing calculations.

I'll try to come up with something like you said, but if you can give me a hint about how to make the Tile array would be much appreciated.

Thanks for the advice.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline davedes
« Reply #3 - Posted 2013-06-14 12:49:10 »

If you are concerned with performance you should really not be using Java2D. Roll Eyes Look into LibGDX or any other OpenGL-based renderer instead.

Offline jh62

Senior Newbie





« Reply #4 - Posted 2013-06-15 04:41:40 »

If you are concerned with performance you should really not be using Java2D. Roll Eyes Look into LibGDX or any other OpenGL-based renderer instead.

Yeah, I know that, but I'm currently fine with the speed of the game. I'm trying to master this first to go to OpenGL and such... The main question was why i was getting a higher delay with my custom paint method using the same code in paint(Graphics g).

Anyway, i'm having good timing now and going to proceed to other things now.

I'm trying to correct what Kerai said about the array having instance of every tile.
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.

Nickropheliac (9 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (27 views)
2014-08-22 19:31:30

atombrot (40 views)
2014-08-19 09:29:53

Tekkerue (38 views)
2014-08-16 06:45:27

Tekkerue (34 views)
2014-08-16 06:22:17

Tekkerue (24 views)
2014-08-16 06:20:21

Tekkerue (34 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (47 views)
2014-08-09 21:09:32
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!