Skip to content

Instantly share code, notes, and snippets.

@jgranick

jgranick/Main.hx

Created May 10, 2017
Embed
What would you like to do?
Native Render (Simple)
package;
import lime.graphics.cairo.*;
import lime.graphics.opengl.*;
import openfl.display.Bitmap;
import openfl.display.BitmapData;
import openfl.display.NativeSprite;
import openfl.display.Sprite;
import openfl.events.NativeRenderEvent;
import openfl.Assets;
class Main extends Sprite {
private var bitmapData:BitmapData;
private var nativeSprite:NativeSprite;
public function new () {
super ();
nativeSprite = new NativeSprite ();
nativeSprite.addEventListener (NativeRenderEvent.NATIVE_RENDER, render);
addChild (nativeSprite);
nativeSprite.x = 200;
nativeSprite.rotation = 20;
}
private function render (event:NativeRenderEvent):Void {
if (bitmapData == null) {
bitmapData = Assets.getBitmapData ("assets/lime.png");
switch (nativeSprite.context) {
case DOM (div):
div.appendChild (bitmapData.image.src);
case FLASH (sprite):
var bitmap = new Bitmap (bitmapData);
bitmap.smoothing = true;
sprite.addChild (bitmap);
default:
}
}
#if !flash
switch (nativeSprite.context) {
case CAIRO (cairo):
cairo.setSourceSurface (bitmapData.getSurface (), 0, 0);
cairo.paint ();
case CANVAS (context):
context.drawImage (bitmapData.image.src, 0, 0, bitmapData.width, bitmapData.height);
case OPENGL (gl):
nativeSprite.shader.data.uImage0.input = bitmapData;
nativeSprite.shader.data.uImage0.smoothing = true;
nativeSprite.shader.data.uMatrix.value = nativeSprite.shaderMatrixArray (nativeSprite.renderTransform);
nativeSprite.bindShader ();
gl.bindBuffer (gl.ARRAY_BUFFER, bitmapData.getBuffer (gl, 1));
gl.vertexAttribPointer (nativeSprite.shader.data.aPosition.index, 3, gl.FLOAT, false, 6 * 4, 0);
gl.vertexAttribPointer (nativeSprite.shader.data.aTexCoord.index, 2, gl.FLOAT, false, 6 * 4, 3 * 4);
gl.vertexAttribPointer (nativeSprite.shader.data.aAlpha.index, 1, gl.FLOAT, false, 6 * 4, 5 * 4);
gl.drawArrays (gl.TRIANGLE_STRIP, 0, 4);
default:
}
#end
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment