From d1a4e0cfea70ddd3659eb08fa7e33e54a673d869 Mon Sep 17 00:00:00 2001 From: Joshua Granick Date: Thu, 15 Feb 2018 11:53:01 -0800 Subject: [PATCH] Implement renderer.readPixels when not using SDL_Renderer --- lime/_backend/native/NativeRenderer.hx | 65 ++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 10 deletions(-) diff --git a/lime/_backend/native/NativeRenderer.hx b/lime/_backend/native/NativeRenderer.hx index b7e8087c4..b16a0fe71 100644 --- a/lime/_backend/native/NativeRenderer.hx +++ b/lime/_backend/native/NativeRenderer.hx @@ -135,20 +135,65 @@ class NativeRenderer { var imageBuffer:ImageBuffer = null; - #if (!macro && lime_cffi) - #if !cs - imageBuffer = NativeCFFI.lime_renderer_read_pixels (handle, rect, new ImageBuffer (new UInt8Array (Bytes.alloc (0)))); - #else - var data:Dynamic = NativeCFFI.lime_renderer_read_pixels (handle, rect, null); - if (data != null) { - imageBuffer = new ImageBuffer (new UInt8Array (@:privateAccess new Bytes (data.data.length, data.data.b)), data.width, data.height, data.bitsPerPixel); + switch (parent.context) { + + case OPENGL (gl): + + var windowWidth = parent.window.__width * parent.window.__scale; + var windowHeight = parent.window.__height * parent.window.__scale; + + var x = Std.int (rect.x); + var y = Std.int ((windowHeight - rect.y) - rect.height); + var width = Std.int (rect.width); + var height = Std.int (rect.height); + + var buffer = Bytes.alloc (width * height * 4); + + gl.readPixels (x, y, width, height, gl.RGBA, gl.UNSIGNED_BYTE, buffer); + + var rowLength = width * 4; + var srcPosition = (height - 1) * rowLength; + var destPosition = 0; + + var temp = Bytes.alloc (rowLength); + var rows = Std.int (height / 2); + + while (rows-- > 0) { + + temp.blit (0, buffer, destPosition, rowLength); + buffer.blit (destPosition, buffer, srcPosition, rowLength); + buffer.blit (srcPosition, temp, 0, rowLength); + + destPosition += rowLength; + srcPosition -= rowLength; + + } + + imageBuffer = new ImageBuffer (new UInt8Array (buffer), width, height, 32, RGBA32); + + default: + + #if (!macro && lime_cffi) + #if !cs + imageBuffer = NativeCFFI.lime_renderer_read_pixels (handle, rect, new ImageBuffer (new UInt8Array (Bytes.alloc (0)))); + #else + var data:Dynamic = NativeCFFI.lime_renderer_read_pixels (handle, rect, null); + if (data != null) { + imageBuffer = new ImageBuffer (new UInt8Array (@:privateAccess new Bytes (data.data.length, data.data.b)), data.width, data.height, data.bitsPerPixel); + } + #end + #end + + if (imageBuffer != null) { + + imageBuffer.format = RGBA32; + + } + } - #end - #end if (imageBuffer != null) { - imageBuffer.format = RGBA32; return new Image (imageBuffer); }