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   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Re: Screen tearing with BufferStrategy on: 2012-04-05 01:19:50
Well it looks that I am doing something wrong. Now I/We have to figure out what is it Wink
2  Game Development / Newbie & Debugging Questions / Re: Research for 2D Java Game on: 2012-04-04 18:08:59
IntelliJ is really fast (especially with auto completion) and highly customizable.

For example, you don't have to type BufferStrategy or SuperLongClassWhichNameIsWayTooLong but just upper case letters to get completion.
If you want to surround code with something (try/catch, if () { } or so) just select code and press ctrl-alt-t and IntelliJ will do it for you.

Those examples are just drops in the ocean of IntelliJ's possibilites. Just read the keymap, add your own shortcuts and code fast Smiley
If you write tests you will even get code coverage.

And it is integrated with various SCMs like SVN/GIT.
3  Game Development / Newbie & Debugging Questions / Re: Screen tearing with BufferStrategy on: 2012-04-04 12:45:54
Well on my machine even Catacomb Snatch have issues with vsync so I guess it is OS fault.
4  Game Development / Newbie & Debugging Questions / Re: Screen tearing with BufferStrategy on: 2012-04-04 09:03:09
Linux Ubuntu/Xubuntu and Windows 7. Now I am on the machine with Linux, tested it on friend's xubuntu as well (maybe it is a xubuntu issue then).

Game loop implementation:
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  
public class FixedLengthGameLoop implements GameLoop {
    private static final Logger LOGGER = LoggerFactory.getLogger(FixedLengthGameLoop.class);

    // Limits the update amounts before render
   private static final int MAX_UPDATES_PER_RENDER = Integer.MAX_VALUE;

    private static final double NANOS_PER_SECOND = 1000000000;

    // desired updates rate
   private static final double UPDATES_RATE = 30;

    // time between game updates
   private static final double UPDATES_INTERVAL = NANOS_PER_SECOND / UPDATES_RATE;

    // desired frame rate
   private static final double FRAMES_RATE = 60;

    // time between each render
   private static final double RENDERS_INTERVAL = NANOS_PER_SECOND / FRAMES_RATE;

    private GameContext context;

    public FixedLengthGameLoop(GameContext context) {
        LOGGER.info("Initializing default game loop");
        this.context = context;
    }

    public void run() {
        LOGGER.info("Executing main loop");

        double lastUpdateTime = System.nanoTime();
        double lastRenderTime = System.nanoTime();

        double now;
        int updatesCount;

        int frames = 0;
        int fps = (int) FRAMES_RATE;
        double lastFpsUpdateTime = System.nanoTime();

        while (context.isRunning()) {
            now = System.nanoTime();
            updatesCount = 0;

            while (now - lastUpdateTime > UPDATES_INTERVAL && updatesCount < MAX_UPDATES_PER_RENDER) {
                context.update();
                lastUpdateTime += UPDATES_INTERVAL;
                ++updatesCount;
            }

            if (now - lastRenderTime > RENDERS_INTERVAL) {
                float delta = Math.min(1.0f, (float) ((now - lastUpdateTime) / UPDATES_INTERVAL));
                context.render(delta, fps);
                lastRenderTime = now;
                ++frames;
            }

            if (now - lastFpsUpdateTime > NANOS_PER_SECOND) {
                fps = frames;
                frames = 0;
                lastFpsUpdateTime = now;
            }

            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                // consume
           }
        }
    }
}
5  Game Development / Newbie & Debugging Questions / Re: Screen tearing with BufferStrategy on: 2012-04-04 08:14:53
Here is the jar: http://dl.dropbox.com/u/1146144/reckless-skirmishers.jar

Just go to the border of the pavement floor (go to the right) and move left-right repeatedly.
Game updates every 2nd frame but with 60 FPS - 60 updates I get tearing as well so I guess it is not a problem.
 
BTW: Graphics are for test purposes (taken from Catacomb Snatch).
6  Game Development / Newbie & Debugging Questions / Re: Screen tearing with BufferStrategy on: 2012-04-04 07:30:07
@ra4king: I have tearing on both Windows and Linux. Whole application is in JFrame - maybe that is an issue? Should I swap to applet?
7  Game Development / Newbie & Debugging Questions / Screen tearing with BufferStrategy on: 2012-04-03 23:22:33
I am using BufferStrategy with 2 buffers (tried with 3 as well) but I cannot rid of screen tearing in my application (no vsync?).
I draw everything on one BufferedImage and then draw it on the graphics received from BufferStrategy.

Any ideas how to solve this problem?

This is my render method:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
    public void render() {
        do {
            do {
                Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
                g.setColor(Color.BLACK);
                g.fillRect(0, 0, getWidth(), getHeight());
                g.clipRect(0, 0, getWidth(), getHeight());
                g.scale(scale, scale);
                g.drawImage(screen.getImage(), 0, 0, screen.getWidth(), screen.getHeight(), null);
                g.dispose();
            } while (bufferStrategy.contentsRestored());
        } while (bufferStrategy.contentsLost());
        bufferStrategy.show();
    }
8  Game Development / Newbie & Debugging Questions / Re: Research for 2D Java Game on: 2012-04-03 17:48:59
Try IntelliJ Idea instead of Eclipse.
9  Game Development / Performance Tuning / Re: FPS issue - alt-tab boosts performance on: 2012-04-03 13:15:29
Graphics card: nVidia Corporation GF108 [Quadro NVS 4200M]

Before alt-tab I have like 100% core usage on one core, after switching off and back again all cores are under 40% (I have lots of stuff running so it is ok, I guess Smiley).
10  Game Development / Performance Tuning / FPS issue - alt-tab boosts performance on: 2012-04-03 12:32:18
I have a weird issue (on Linux, haven't tested it on Windows yet) with FPS.
When I start my application I get about 40 FPS. But when I switch to another application (alt-tab) and back again I get performance boost up to 120 FPS.

Any ideas what have I done wrong?
11  Java Game APIs & Engines / Java 2D / Re: Java2D drawing speed (need help with performance). on: 2012-04-02 13:55:43
@65K: Now I have 230 FPS (With rendering on screen). So it is a great improvement!

Thanks a lot guys!
12  Java Game APIs & Engines / Java 2D / Re: Java2D drawing speed (need help with performance). on: 2012-04-02 13:38:57
Huh, without rendering on the screen (only blitting on bitmap) I get like 250-260 FPS. I will test my code at Windows machine when I get back home.
Also I have uploaded sources to GitHub: https://github.com/tomaszwojcik/java2d-graphics-engine
13  Java Game APIs & Engines / Java 2D / Re: Java2D drawing speed (need help with performance). on: 2012-04-02 11:42:49
@gimbal: yes, 2700 on Windows, 60-70 on Linux. Here's my log:
1  
2  
3  
4  
5  
6  
7  
8  
X11FillRect
X11FillRect
sun.java2d.loops.FillRect::FillRect(AnyColor, SrcNoEa, AnyInt)
sun.java2d.loops.FillRect::FillRect(AnyColor, SrcNoEa, AnyInt)
sun.java2d.loops.FillRect::FillRect(AnyColor, SrcNoEa, AnyInt)
sun.java2d.loops.FillRect::FillRect(AnyColor, SrcNoEa, AnyInt)
sun.java2d.loops.Blit::Blit(IntRgb, SrcNoEa, IntRgb)
X11FillRect

Followed by lots of (I guess this is invoked in the loop):
1  
sun.java2d.loops.TransformHelper::TransformHelper(IntArgb, SrcNoEa, IntArgbPre)


Also I have tried -Dsun.java2d.opengl=true, but got performance loss.

@nsigma:
I swapped the code to use BufferedImage but only lost performance. In my code bitmaps are arrays of int, so drawing is in fact done on arrays and I blit my screen (Screen class) every render on the physical screen. Can that be a bottle neck?

@cylab:
CPU usage: core1: 60-80%, core2: 40-50%, cores3-8 are under 5%
What should I do with VSync? Is there a way to turn it off (would I get screen flickering then)?
14  Java Game APIs & Engines / Java 2D / [SOLVED] Java2D drawing speed (need help with performance). on: 2012-04-02 10:07:13
Since it is my first post I would like to say hello everybody Smiley

The goal of my program is to test how many FPS I can get while consnstantly drawing on the screen.
I would really appreciate if you can point any bottlenecks or suggest improvements.

The problem is that on one machine I can get around 2700 FPS, on the other with similiar hardware setup (but different OS) I get like 60-70.

If you want to compile those codes just remember to set image paths.

First my interface and implementation of FPS counting.
1  
2  
3  
4  
public interface FPS {
    int getFPSFrames();
    void setFPSFrames(int value);
}

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public class FPSTask extends TimerTask {
    private FPS fps;

    public FPSTask(FPS fps) {
        this.fps = fps;
    }

    public void run() {
        System.out.println("Last: " + System.currentTimeMillis() + " frames: " + fps.getFPSFrames());
        fps.setFPSFrames(0);
    }
}

Bitmap with Screen based on the Catacomb Snatch sources.
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  
77  
public class Bitmap {
    protected int w, h;
    protected int pixels[];

    public Bitmap(int w, int h) {
        this(w, h, true);
    }

    protected Bitmap(int w, int h, boolean createPixels) {
        this.w = w;
        this.h = h;
        if (createPixels) {
            pixels = new int[w * h];
        }
    }

    public void blit(Bitmap bitmap, int x, int y) {
        Rectangle blitArea = new Rectangle(x, y, x + bitmap.w, y + bitmap.h);
        adjustBlitArea(blitArea);
        int blitWidth = blitArea.x2 - blitArea.x1;
        for (int yy = blitArea.y1; yy < blitArea.y2; yy++) {
            int targetPixel = yy * w + blitArea.x1;
            int sourcePixel = (yy - y) * bitmap.w + (blitArea.x1 - x);
            targetPixel -= sourcePixel;
            for (int xx = sourcePixel; xx < sourcePixel + blitWidth; xx++) {
                int col = bitmap.pixels[xx];
                int alpha = (col >> 24) & 0xff;
                if (alpha == 0xff) {
                    pixels[targetPixel + xx] = col;
                } else {
                    int bgCol = pixels[targetPixel + xx];
                    pixels[targetPixel + xx] = blendColors(bgCol, col);
                }
            }
        }
    }

    private Rectangle adjustBlitArea(Rectangle blitArea) {
        if (blitArea.x1 < 0) blitArea.x1 = 0;
        if (blitArea.y1 < 0) blitArea.y1 = 0;
        if (blitArea.x2 > w) blitArea.x2 = w;
        if (blitArea.y2 > h) blitArea.y2 = h;
        return blitArea;
    }

    public int getWidth() {
        return w;
    }

    public int getHeight() {
        return h;
    }

    public static Bitmap load(String filename) {
        try {
            BufferedImage bi = ImageIO.read(new File(filename));
            int biWidth = bi.getWidth();
            int biHeight = bi.getHeight();
            Bitmap bitmap = new Bitmap(biWidth, biHeight);
            bi.getRGB(0, 0, biWidth, biHeight, bitmap.pixels, 0, biWidth);
            return bitmap;
        } catch (IOException e) {
            return null;
        }
    }

    public static int blendColors(int backgroundColor, int colorToBlend) {
        RGB bgRGB = new RGB(backgroundColor);
        RGB pixelRGB = new RGB(colorToBlend);
        int bgAlpha = 256 - pixelRGB.alpha;
        int r = ((pixelRGB.r * pixelRGB.alpha + bgRGB.r * bgAlpha) >> 8) & 0xff0000;
        int g = ((pixelRGB.g * pixelRGB.alpha + bgRGB.g * bgAlpha) >> 8) & 0xff00;
        int b = ((pixelRGB.b * pixelRGB.alpha + bgRGB.b * bgAlpha) >> 8) & 0xff;
        return 0xff000000 | r | g | b;
    }

}

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public class Screen extends Bitmap {
    protected final BufferedImage image;

    public Screen(int w, int h) {
        super(w, h, false);
        image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
    }

    public Image getImage() {
        return image;
    }

}


Helper classes used in Bitmap.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public class Rectangle extends java.awt.Rectangle {
    public int x1, y1, x2, y2;

    public Rectangle(int x1, int y1, int x2, int y2) {
        super(x1, y1, x2 - x1, y2 - y1);
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
    }

}

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
public class RGB {
    public int r, g, b;
    public int alpha;

    public RGB(int color) {
        r = color & 0xff0000;
        g = color & 0xff00;
        b = color & 0xff;
        alpha = color & 0xff000000;
    }
}


And finally whole program (based on ra4king's game skeleton):
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  
public class TestJFrame extends JFrame implements FPS, Runnable {
    public static final int MAX_OBJECTS = 500;

    private int frames;
    private Screen screen;

    private Bitmap background;
    private Bitmap object;

    private Canvas canvas;

    public TestJFrame() {
        setSize(640, 480);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
        setIgnoreRepaint(true);
        screen = new Screen(getWidth(), getHeight());
        canvas = new Canvas();
        canvas.setSize(getWidth(), getHeight());
        add(canvas);
        pack();
        background = Bitmap.load("your_path_to_any_background.png"); //TODO set path, my background was 640x480.
       object = Bitmap.load("your_path_to_any_object"); //TODO set path, my object was 32x32.
   }

    public static void main(String[] args) {
        TestJFrame testJFrame = new TestJFrame();
        Thread t = new Thread(testJFrame);
        t.start();
    }

    public void run() {
        Timer timer = new Timer();
        FPSTask fpsTask = new FPSTask(this);
        timer.schedule(fpsTask, 0, 1000);
        canvas.createBufferStrategy(2);
        canvas.setIgnoreRepaint(true);
        BufferStrategy bs = canvas.getBufferStrategy();
        while (true) {
            do {
                do {
                    Graphics2D g = (Graphics2D) bs.getDrawGraphics();
                    render(g);
                    g.dispose();
                } while (bs.contentsRestored());
                bs.show();
            } while (bs.contentsLost());
            frames++;
        }
    }

    private void render(Graphics g) {
        g.setColor(Color.black);
        draw(MAX_OBJECTS);
        g.drawImage(screen.getImage(), 0, 0, getWidth(), getHeight(), null);
    }

    private void draw(int objects) {
        screen.blit(background, 0, 0);
        for (int i = 0; i < MAX_OBJECTS; i++) {
            screen.blit(object, (int) (Math.random() * 640), (int) (Math.random() * 480));
        }
    }

    public int getFPSFrames() {
        return frames;
    }

    public void setFPSFrames(int value) {
        frames = value;
    }
}
Pages: [1]
 

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 (43 views)
2014-09-23 14:38:19

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

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

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

moogie (37 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!