Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (542)
Games in Android Showcase (133)
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  
  Properly using VolatileImage  (Read 2344 times)
0 Members and 1 Guest are viewing this topic.
Offline fictiveLaark

Junior Newbie





« Posted 2011-03-24 06:46:28 »

I've been reading about VolatileImage but I'm not sure I know how to use it properly yet, so I was hoping somebody could look at this program I wrote.  Is there anything I should be checking but aren't?  Things that I'm checking but I shouldn't?  If it means anything, the picture I'm trying to draw is translucent.

Also, I read I should use sun.java2d.trace to detect software rendering, but I don't quite understand it.  I set it to "count" and I see the following when my program ends.

1 call to sun.java2d.loops.Blit$GeneralMaskBlit::Blit(FourByteAbgr, SrcNoEa, IntArgbPre)
101 calls to D3DFillRect
1 call to sun.java2d.d3d.D3DSurfaceToGDIWindowSurfaceBlit::Blit("D3D Surface", AnyAlpha, "GDI")
89 calls to sun.java2d.d3d.D3DRTTSurfaceToSurfaceBlit::Blit("D3D Surface (render-to-texture)", AnyAlpha, "D3D Surface")
1 call to sun.java2d.d3d.D3DGeneralBlit::Blit(Any, AnyAlpha, "D3D Surface")
3 calls to sun.java2d.loops.FillRect::FillRect(AnyColor, SrcNoEa, AnyInt)
1 call to sun.java2d.loops.MaskBlit::MaskBlit(IntArgb, AnyAlpha, IntArgbPre)
1 call to sun.java2d.windows.GDIBlitLoops::Blit(IntRgb, SrcNoEa, "GDI")
1 call to sun.java2d.loops.MaskBlit$General::MaskBlit(FourByteAbgr, SrcNoEa, IntArgbPre)
1 call to sun.java2d.loops.Blit::Blit(FourByteAbgr, SrcNoEa, IntArgb)
200 total calls to 10 different primitives

I've assumed that "sun.java2d.loops" indicates software rendering but...well I'm not sure what to do about that.

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  
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.VolatileImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.Timer;

public class Main implements ActionListener {
    static final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    static final GraphicsDevice gd = ge.getDefaultScreenDevice();
    static final GraphicsConfiguration gc = gd.getDefaultConfiguration();
    static BufferedImage bufferedImage;
    static VolatileImage image;
    static BufferStrategy bs;
    static JFrame frame;

    public static void fillImage(){
        Graphics2D g = image.createGraphics();
        g.setComposite(AlphaComposite.Src);
        g.drawImage(bufferedImage, null, 0, 0);
        g.dispose();
    }

    public static void main(String[] args) throws IOException {
        String os = System.getProperty("os.name");
        System.setProperty("sun.java2d.trace", "count");
        if(os.contains("Windows")){
            System.setProperty("sun.java2d.d3d", "True");
        }else{
            System.setProperty("sun.java2d.opengl", "True");
        }
        bufferedImage = ImageIO.read(new File("fig6.png"));
        do{
            image = gc.createCompatibleVolatileImage(bufferedImage.getWidth(), bufferedImage.getHeight(), bufferedImage.getTransparency());
        }while(image.validate(gc) == VolatileImage.IMAGE_INCOMPATIBLE);
        fillImage();

        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setIgnoreRepaint(true);
        frame.createBufferStrategy(2);
        bs = frame.getBufferStrategy();
        frame.setSize(500, 500);
        frame.setLocationRelativeTo(null);
        new Timer(16, new Main()).start();
    }

    public void actionPerformed(ActionEvent e) {
        while(image.validate(gc) != VolatileImage.IMAGE_OK){
            while(image.validate(gc) == VolatileImage.IMAGE_INCOMPATIBLE || image.contentsLost()){
                image = gc.createCompatibleVolatileImage(bufferedImage.getWidth(), bufferedImage.getHeight(), bufferedImage.getTransparency());
            }
            fillImage();
        }
        do{
            Graphics g = bs.getDrawGraphics();
            g.setColor(Color.black);
            g.fillRect(frame.getInsets().left, frame.getInsets().top, frame.getContentPane().getWidth(), frame.getContentPane().getHeight());
            g.drawImage(image, 0, 0, null);
            g.dispose();
        }while(bs.contentsLost());
        bs.show();
    }
}
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.

Elsealabs (18 views)
2014-12-28 10:39:27

CopyableCougar4 (20 views)
2014-12-28 02:10:29

BurntPizza (25 views)
2014-12-27 22:38:51

Mr.CodeIt (15 views)
2014-12-27 04:03:04

TheDudeFromCI (20 views)
2014-12-27 02:14:49

Mr.CodeIt (26 views)
2014-12-23 03:34:11

rwatson462 (58 views)
2014-12-15 09:26:44

Mr.CodeIt (47 views)
2014-12-14 19:50:38

BurntPizza (98 views)
2014-12-09 22:41:13

BurntPizza (116 views)
2014-12-08 04:46:31
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

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

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50
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!