Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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  
  Am I double buffering correctly? Do I need to?  (Read 2684 times)
0 Members and 1 Guest are viewing this topic.
Offline crushenator

Junior Newbie





« Posted 2012-03-08 04:36:41 »

Hi guys. Thanks in advance for the help.

I'm whipping up a 2D engine for the first time. I've copied my render loop below. What seeking help for here is a better understanding of BufferStrategy and Buffering to an offscreen Image, when they should be employed, and if I'm doing it wrong.

One approach I've read about is reading images into an array of pixels then using a blit method to insert them on a per pixel basis to an offscreen image, then painting the image. I believe I've began preparing for this method below.

Would it be a better idea just to read the image into a BufferedImage and then use the drawImage() method to draw it onto the Graphics of the BufferStrategy? Moreover, when I draw text and lines would I be drawing them onto the offscreenGraphics? or the Graphics of the BufferStrategy?

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  
    public void render() {
        BufferStrategy bs = getBufferStrategy();
        if (bs == null) {
            createBufferStrategy(3);
            requestFocus();
            return;
        }


        Graphics g = bs.getDrawGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.translate((getWidth() - GAME_WIDTH * SCALE) / 2, (getHeight() - GAME_HEIGHT * SCALE) / 2);
        g.clipRect(0, 0, GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE);


        offscreenGraphics.setBackground(Color.WHITE);
        offscreenGraphics.clearRect(0, 0, getWidth(), getHeight());
        offscreenGraphics.setColor(Color.BLACK);
        offscreenGraphics.drawLine(0, 0, 100, 100);

        // Stress test of changing each pixel individually.
       for (int x = 0; x < GAME_HEIGHT; x++) {
            for (int y = 0; y < GAME_WIDTH; y++) {
                pixels[(x * GAME_WIDTH) + y] = 0xFF3333;
            }
        }

        // Testing drawing a line
       offscreenGraphics.draw(new Line2D.Double(5, 5, 500, 500));

        // Testing drawing a string.
       offscreenGraphics.drawString(Integer.toString(framesPerSecond), 100, 100);

        g.drawImage(offscreenImage, 0, 0, GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE, 0, 0, GAME_WIDTH, GAME_HEIGHT, null);
        g.dispose();
        bs.show();
    }


Other code that I'm using is:

1  
2  
3  
        renderHints.put(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);
        offscreenGraphics.setRenderingHints(renderHints);
Offline JESTERRRRRR

Senior Member


Medals: 7
Exp: 1 year



« Reply #1 - Posted 2012-03-08 16:40:13 »

I'm no expert and I'm sure raking or someone will clarify, but I thought when you used a BufferStrategy, it created your offscreen area for you, and you draw with the 'Graphics g = bs.getDrawGraphics();', and bs.show() will switch the buffer, or blit it to screen. And I think you should just read images into a bufferedimage then draw them with the same 'g' from earlier
Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #2 - Posted 2012-03-08 17:13:34 »

Yup, there is no need to add the extra overhead of another Image. Just draw directly to BufferStrategy's Graphics2D and then call show().
This code shows the best way to setup BufferStrategy.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline crushenator

Junior Newbie





« Reply #3 - Posted 2012-03-09 00:57:12 »

Awesome code. Is it still possible to work on a BufferStrategy on a per pixel basis? Suppose that I wanted to blit all the pixels into black and white for a dream sequence.

Its interesting that I got my Rendering loop ideas largely from Notch's Ludum Dare entries. I wonder if he does his 2D game rendering sub-optimally.
Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2012-03-09 02:36:00 »

No, to work on per pixel basis, you'll have to create a BufferedImage instance. The best and fastest way to create a BufferedImage, is to create a compatible one (line 87).

Offline crushenator

Junior Newbie





« Reply #5 - Posted 2012-03-09 04:59:25 »

Thanks mate I do appreciate the help.
Offline crushenator

Junior Newbie





« Reply #6 - Posted 2012-03-09 07:10:17 »

I jumped from about 80fps to 170fps after implementing your code. Thats without even changing the double handling of the BufferStrategy and BufferedImage.  Grin
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.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

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

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

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

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

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

BurntPizza (54 views)
2014-09-19 03:14:18
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!