Show Posts
|
|
Pages: [1]
|
|
2
|
Game Development / Newbie & Debugging Questions / Re: Research for 2D Java Game
|
on: 2012-04-04 20: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  If you write tests you will even get code coverage. And it is integrated with various SCMs like SVN/GIT.
|
|
|
|
|
4
|
Game Development / Newbie & Debugging Questions / Re: Screen tearing with BufferStrategy
|
on: 2012-04-04 11: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);
private static final int MAX_UPDATES_PER_RENDER = Integer.MAX_VALUE;
private static final double NANOS_PER_SECOND = 1000000000;
private static final double UPDATES_RATE = 30;
private static final double UPDATES_INTERVAL = NANOS_PER_SECOND / UPDATES_RATE;
private static final double FRAMES_RATE = 60;
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) { } } } } |
|
|
|
|
|
7
|
Game Development / Newbie & Debugging Questions / Screen tearing with BufferStrategy
|
on: 2012-04-04 01: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(); } |
|
|
|
|
|
10
|
Game Development / Performance Tuning / FPS issue - alt-tab boosts performance
|
on: 2012-04-03 14: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?
|
|
|
|
|
13
|
Java Game APIs & Engines / Java 2D / Re: Java2D drawing speed (need help with performance).
|
on: 2012-04-02 13: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 12:07:13
|
Since it is my first post I would like to say hello everybody  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"); object = Bitmap.load("your_path_to_any_object"); }
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; } } |
|
|
|
|
|
|
Add your game by posting it in the WIP section,
or publish it in Showcase.
The first screenshot will be displayed as a thumbnail.
|
|