Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
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  
  BufferStrategy - CPU Usage  (Read 401 times)
0 Members and 1 Guest are viewing this topic.
Offline Dregron
« 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

The trouble with programmers is that you can never tell what a programmer is doing until it’s too late.
Offline Kerai

Junior Member


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
« 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%

The trouble with programmers is that you can never tell what a programmer is doing until it’s too late.
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
« 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

The trouble with programmers is that you can never tell what a programmer is doing until it’s too late.
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.

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

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

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

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

Tekkerue (47 views)
2014-09-09 02:24:56

mitcheeb (68 views)
2014-09-08 06:06:29

BurntPizza (51 views)
2014-09-07 01:13:42

Longarmx (38 views)
2014-09-07 01:12:14

Longarmx (44 views)
2014-09-07 01:11:22

Longarmx (40 views)
2014-09-07 01:10:19
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!