/** * 获得当前游戏屏幕截图 * * @param x * @param y * @param width * @param height * @return */ public static synchronized LImage toScreenCaptureImage(int x, int y, int width, int height) { if (GLEx.gl10 == null) { return null; } if (width < 0) { width = 1; } if (height < 0) { height = 1; } if (x < 0 || x > width) { x = 0; } if (y < 0 || y > height) { y = 0; } int hashCode = 1; hashCode = LSystem.unite(hashCode, x); hashCode = LSystem.unite(hashCode, y); hashCode = LSystem.unite(hashCode, width); hashCode = LSystem.unite(hashCode, height); if (screenCache.size() > LSystem.DEFAULT_MAX_CACHE_SIZE / 10) { for (ScreenCache sc : screenCache.values()) { sc.dispose(); sc = null; } screenCache.clear(); } ScreenCache cache = screenCache.get(hashCode); if (cache == null) { cache = new ScreenCache(x, y, width, height); } else { cache.reset(); } screenCache.put(hashCode, cache); cache.commit(); for (; cache.commit == null; ) { try { Thread.yield(); } catch (Exception e) { } } return cache.commit; }
public synchronized void commit() { if (runnable != null) { return; } runnable = new Runnable() { @Override public void run() { GLEx.gl10.glReadPixels( x, (int) (LSystem.screenRect.height * LSystem.scaleHeight) - y - height, width, height, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, buffer); for (int i = 0, ny = 0; i < height; i++, ny++) { for (int j = 0; j < width; j++) { final int pix = buffer.get(i * width + j); final int pb = (pix >> 16) & 0xff; final int pr = (pix << 16) & 0x00ff0000; final int pixel = (pix & 0xff00ff00) | pr | pb; drawPixels[(height - ny - 1) * width + j] = pixel; } } if (image == null) { image = new LImage(width, height, true); } else if (image != null && image.isClose()) { image.dispose(); image = null; image = new LImage(width, height, true); } image.setPixels(drawPixels, width, height); if (image.getWidth() != trueWidth || image.getHeight() != trueHeight) { LImage tmp = image.scaledInstance(trueWidth, trueHeight); if (image != null) { image.dispose(); image = null; } commit = tmp; } else { commit = image; } } }; LSystem.callScreenRunnable(runnable); }