Java-Gaming.org Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (565)
Games in Android Showcase (151)
games submitted by our members
Games in WIP (606)
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  
  BufferStrategy - CPU Usage  (Read 474 times)
0 Members and 1 Guest are viewing this topic.
Offline Dregron

Senior Newbie


Medals: 1
Exp: 4 years



« Posted 2013-04-28 21:32:26 »

Is there a less CPU intensive way to create a double buffer, bec just now when am doing it this way it takes up 50-60% of CPU without it, it's 10-20%.
I know i could use OpenGL or Slick etc.. but am trying to create this whole thing without any Libraries (except System Library).

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  
   public void draw() {
      if (this.getBufferStrategy() == null) {
         createBufferStrategy(2);
         return;
      }
     
      Graphics2D g = (Graphics2D) getBufferStrategy().getDrawGraphics();
      g.setColor(Color.BLACK);
      g.fillRect(0, 0, WIDTH+2, HEIGHT);
      g.setColor(Color.white);
      g.setFont(new Font("Serif", Font.PLAIN, 14));
      g.drawString("Ups: " + this.Ups , 4, 12);
      g.drawString("FPS: " + this.frameRate , 4, 24);
//      for(int i = 0; i < test.length; i++){
//         if(getCurrentState().toLowerCase().contains(test[i].getClass().getSimpleName().toLowerCase())){
//            try {
//               Method m = test[i].getClass().getDeclaredMethod("draw", Graphics2D.class);
//               m.invoke(test[i], g);
//            }catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
//               System.out.println("graphics Error!");
//               System.out.println(e.toString());
//            }
//         }
//      }
      g.dispose();
      getBufferStrategy().show();
   }


Any information would be appreciated
Cheers
Offline Kerai

Junior Devvie


Medals: 4



« Reply #1 - Posted 2013-04-28 21:48:03 »

Do you limit FPS in any way? If you don't, then you'll always eat cpu...

also, the code you have commented uses reflection in a way that will be rather cpu intensive
Offline Dregron

Senior Newbie


Medals: 1
Exp: 4 years



« Reply #2 - Posted 2013-04-28 21:54:57 »

Yeah, i have a limit to FPS,
The commented code isn't really the problem i tested it out.
I've commented out each part to see what could be causing the issue and when i took away the BufferStrat it went down to 10-20%
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline davedes
« Reply #3 - Posted 2013-04-28 22:52:04 »

Java2D often uses software (CPU) when rendering and requires high CPU usage for rendering a typical game scene (with many sprites, alpha, etc).

The best way to reduce load on CPU is to do more heavy lifting on the GPU. This means using an OpenGL based renderer.

"Performance" and "Java2D" don't go so well together. Choose one or the other. Smiley

With that said, here is how I used BufferStrategy in my last Java2D project:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
 ... init ...
      //needs to be called after JFrame is valid and visible
      canvas.createBufferStrategy(2);
      strategy = canvas.getBufferStrategy();


 ... game loop ...

         update(deltaTime);

         do {
            do {
               Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
               //clear screen
               g.setColor(Color.white);
               g.clearRect(0, 0, width, height);
               render(g);
               g.dispose();
            } while (strategy.contentsRestored());
            strategy.show();
         } while (strategy.contentsLost());

Offline Dregron

Senior Newbie


Medals: 1
Exp: 4 years



« Reply #4 - Posted 2013-04-28 23:01:14 »

Ah yeah should have known that Tongue

Cheers for the code (nice way of doing it Smiley ) & info
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 
ags1 (20 views)
2015-03-31 10:55:12

theagentd (13 views)
2015-03-27 23:08:20

wxwsk8er (54 views)
2015-03-20 15:39:46

Fairy Tailz (47 views)
2015-03-15 21:52:20

Olo (29 views)
2015-03-13 17:51:59

Olo (32 views)
2015-03-13 17:50:51

Olo (39 views)
2015-03-13 17:50:16

Olo (44 views)
2015-03-13 17:47:07

ClaasJG (59 views)
2015-03-10 11:36:42

ClaasJG (43 views)
2015-03-10 11:33:01
How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27
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!