From 0faa375f725a3115a39b6a208aded98436cef293 Mon Sep 17 00:00:00 2001 From: Joshua Granick Date: Wed, 9 Jul 2014 16:32:55 -0700 Subject: [PATCH] Add application.init(), simplify SimpleImage sample --- lime/app/Application.hx | 8 ++ lime/graphics/Renderer.hx | 7 ++ samples/SimpleImage/Source/Main.hx | 173 +++++++------------------- templates/haxe/DefaultAssetLibrary.hx | 6 +- 4 files changed, 63 insertions(+), 131 deletions(-) diff --git a/lime/app/Application.hx b/lime/app/Application.hx index 4ebd8deb2..e2cd236ed 100644 --- a/lime/app/Application.hx +++ b/lime/app/Application.hx @@ -18,6 +18,7 @@ class Application extends Module { public static var onUpdate = new EventVoid> (); private static var __eventInfo = new UpdateEventInfo (); + private static var __initialized:Bool; private static var __instance:Application; private static var __registered:Bool; @@ -147,6 +148,13 @@ class Application extends Module { } + public function init (context:RenderContext):Void { + + + + } + + public function onKeyDown (keyCode:Int, modifier:Int):Void {} public function onKeyUp (keyCode:Int, modifier:Int):Void {} public function onMouseDown (x:Float, y:Float, button:Int):Void {} diff --git a/lime/graphics/Renderer.hx b/lime/graphics/Renderer.hx index 5babe56da..917089135 100644 --- a/lime/graphics/Renderer.hx +++ b/lime/graphics/Renderer.hx @@ -121,6 +121,13 @@ class Renderer { var context = window.currentRenderer.context; + if (!Application.__initialized) { + + Application.__initialized = true; + Application.__instance.init (context); + + } + Application.__instance.render (context); onRender.dispatch (context); diff --git a/samples/SimpleImage/Source/Main.hx b/samples/SimpleImage/Source/Main.hx index 2364991ed..c1ff0d674 100644 --- a/samples/SimpleImage/Source/Main.hx +++ b/samples/SimpleImage/Source/Main.hx @@ -3,30 +3,20 @@ package; import lime.app.Application; import lime.geom.Matrix4; -import lime.graphics.Image; -import lime.graphics.GLBuffer; -import lime.graphics.GLProgram; -import lime.graphics.GLRenderContext; -import lime.graphics.GLTexture; -import lime.graphics.RenderContext; +import lime.graphics.*; import lime.utils.Float32Array; +import lime.utils.GLUtils; import lime.Assets; -#if html5 -@:access(lime.graphics.GL) -#end - - class Main extends Application { - private var initialized:Bool; - - private var shaderProgram:GLProgram; - private var texCoordBuffer:GLBuffer; + private var buffer:GLBuffer; + private var program:GLProgram; private var texture:GLTexture; - private var vertexBuffer:GLBuffer; + private var textureAttribute:Int; + private var vertexAttribute:Int; public function new () { @@ -36,10 +26,17 @@ class Main extends Application { } - private function initialize (context:RenderContext):Void { + public override function init (context:RenderContext):Void { switch (context) { + case CANVAS (context): + + var image = Assets.getImage ("assets/lime.png"); + context.fillStyle = "#" + StringTools.hex (config.background, 6); + context.fillRect (0, 0, window.width, window.height); + context.drawImage (image.src, 0, 0, image.width, image.height); + case DOM (element): #if js @@ -49,11 +46,17 @@ class Main extends Application { element.appendChild (image); #end + case FLASH (sprite): + + #if flash + var image = Assets.getImage ("assets/lime.png"); + var bitmap = new flash.display.Bitmap (image.src); + sprite.addChild (bitmap); + #end + case OPENGL (gl): - // Initialize shaders - - var vertexShaderSource = + var vertexSource = "attribute vec4 aPosition; attribute vec2 aTexCoord; @@ -68,17 +71,7 @@ class Main extends Application { }"; - var vertexShader = gl.createShader (gl.VERTEX_SHADER); - gl.shaderSource (vertexShader, vertexShaderSource); - gl.compileShader (vertexShader); - - if (gl.getShaderParameter (vertexShader, gl.COMPILE_STATUS) == 0) { - - throw "Error compiling vertex shader"; - - } - - var fragmentShaderSource = + var fragmentSource = #if !desktop "precision mediump float;" + @@ -91,61 +84,36 @@ class Main extends Application { gl_FragColor = texture2D (uImage0, vTexCoord); }"; - var fragmentShader = gl.createShader (gl.FRAGMENT_SHADER); - gl.shaderSource (fragmentShader, fragmentShaderSource); - gl.compileShader (fragmentShader); + program = GLUtils.createProgram (vertexSource, fragmentSource); + gl.useProgram (program); - if (gl.getShaderParameter (fragmentShader, gl.COMPILE_STATUS) == 0) { - - throw "Error compiling fragment shader"; - - } + vertexAttribute = gl.getAttribLocation (program, "aPosition"); + textureAttribute = gl.getAttribLocation (program, "aTexCoord"); + var matrixUniform = gl.getUniformLocation (program, "uMatrix"); + var imageUniform = gl.getUniformLocation (program, "uImage0"); - shaderProgram = gl.createProgram (); - gl.attachShader (shaderProgram, vertexShader); - gl.attachShader (shaderProgram, fragmentShader); - gl.linkProgram (shaderProgram); + gl.enableVertexAttribArray (vertexAttribute); + gl.enableVertexAttribArray (textureAttribute); - if (gl.getProgramParameter (shaderProgram, gl.LINK_STATUS) == 0) { - - throw "Unable to initialize the shader program."; - - } - - // Create buffers + gl.uniform1i (imageUniform, 0); + gl.uniformMatrix4fv (matrixUniform, false, Matrix4.createOrtho (0, window.width, window.height, 0, -1000, 1000)); var image = Assets.getImage ("assets/lime.png"); - var vertices = [ + var data = [ - image.width, image.height, 0, - 0, image.height, 0, - image.width, 0, 0, - 0, 0, 0 + image.width, image.height, 0, 1, 1, + 0, image.height, 0, 0, 1, + image.width, 0, 0, 1, 0, + 0, 0, 0, 0, 0 ]; - vertexBuffer = gl.createBuffer (); - gl.bindBuffer (gl.ARRAY_BUFFER, vertexBuffer); - gl.bufferData (gl.ARRAY_BUFFER, new Float32Array (cast vertices), gl.STATIC_DRAW); + buffer = gl.createBuffer (); + gl.bindBuffer (gl.ARRAY_BUFFER, buffer); + gl.bufferData (gl.ARRAY_BUFFER, new Float32Array (cast data), gl.STATIC_DRAW); gl.bindBuffer (gl.ARRAY_BUFFER, null); - var texCoords = [ - - 1, 1, - 0, 1, - 1, 0, - 0, 0, - - ]; - - texCoordBuffer = gl.createBuffer (); - gl.bindBuffer (gl.ARRAY_BUFFER, texCoordBuffer); - gl.bufferData (gl.ARRAY_BUFFER, new Float32Array (cast texCoords), gl.STATIC_DRAW); - gl.bindBuffer (gl.ARRAY_BUFFER, null); - - // Create texture - texture = gl.createTexture (); gl.bindTexture (gl.TEXTURE_2D, texture); gl.texParameteri (gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); @@ -163,38 +131,13 @@ class Main extends Application { } - initialized = true; - } public override function render (context:RenderContext):Void { - if (!initialized) { - - initialize (context); - - } - switch (context) { - case CANVAS (context): - - var image = Assets.getImage ("assets/lime.png"); - context.fillStyle = "#" + StringTools.hex (config.background, 6); - context.fillRect (0, 0, window.width, window.height); - context.drawImage (image.src, 0, 0, image.width, image.height); - - case DOM (element): - - // still visible - - case FLASH (sprite): - - var image = Assets.getImage ("assets/lime.png"); - sprite.graphics.beginBitmapFill (image.src); - sprite.graphics.drawRect (0, 0, image.width, image.height); - case OPENGL (gl): var r = ((config.background >> 16) & 0xFF) / 0xFF; @@ -205,17 +148,6 @@ class Main extends Application { gl.clearColor (r, g, b, a); gl.clear (gl.COLOR_BUFFER_BIT); - var vertexAttribute = gl.getAttribLocation (shaderProgram, "aPosition"); - var texCoordAttribute = gl.getAttribLocation (shaderProgram, "aTexCoord"); - var matrixUniform = gl.getUniformLocation (shaderProgram, "uMatrix"); - var imageUniform = gl.getUniformLocation (shaderProgram, "uImage0"); - - var matrix = Matrix4.createOrtho (0, window.width, window.height, 0, -1000, 1000); - - gl.useProgram (shaderProgram); - gl.enableVertexAttribArray (vertexAttribute); - gl.enableVertexAttribArray (texCoordAttribute); - gl.activeTexture (gl.TEXTURE0); gl.bindTexture (gl.TEXTURE_2D, texture); @@ -223,27 +155,12 @@ class Main extends Application { gl.enable (gl.TEXTURE_2D); #end - gl.bindBuffer (gl.ARRAY_BUFFER, vertexBuffer); - gl.vertexAttribPointer (vertexAttribute, 3, gl.FLOAT, false, 0, 0); - gl.bindBuffer (gl.ARRAY_BUFFER, texCoordBuffer); - gl.vertexAttribPointer (texCoordAttribute, 2, gl.FLOAT, false, 0, 0); - - gl.uniformMatrix4fv (matrixUniform, false, matrix); - gl.uniform1i (imageUniform, 0); + gl.bindBuffer (gl.ARRAY_BUFFER, buffer); + gl.vertexAttribPointer (vertexAttribute, 3, gl.FLOAT, false, 5 * Float32Array.BYTES_PER_ELEMENT, 0); + gl.vertexAttribPointer (textureAttribute, 2, gl.FLOAT, false, 5 * Float32Array.BYTES_PER_ELEMENT, 3 * Float32Array.BYTES_PER_ELEMENT); gl.drawArrays (gl.TRIANGLE_STRIP, 0, 4); - gl.bindBuffer (gl.ARRAY_BUFFER, null); - gl.bindTexture (gl.TEXTURE_2D, null); - - #if desktop - gl.disable (gl.TEXTURE_2D); - #end - - gl.disableVertexAttribArray (vertexAttribute); - gl.disableVertexAttribArray (texCoordAttribute); - gl.useProgram (null); - default: } diff --git a/templates/haxe/DefaultAssetLibrary.hx b/templates/haxe/DefaultAssetLibrary.hx index a747a7ae5..de7a21d88 100644 --- a/templates/haxe/DefaultAssetLibrary.hx +++ b/templates/haxe/DefaultAssetLibrary.hx @@ -113,7 +113,7 @@ class DefaultAssetLibrary extends AssetLibrary { public override function exists (id:String, type:String):Bool { - var requestedType = cast (type, AssetType); + var requestedType = type != null ? cast (type, AssetType) : null; var assetType = this.type.get (id); if (assetType != null) { @@ -341,7 +341,7 @@ class DefaultAssetLibrary extends AssetLibrary { public override function isLocal (id:String, type:String):Bool { - var requestedType = cast (type, AssetType); + var requestedType = type != null ? cast (type, AssetType) : null; #if flash @@ -360,7 +360,7 @@ class DefaultAssetLibrary extends AssetLibrary { public override function list (type:String):Array { - var requestedType = cast (type, AssetType); + var requestedType = type != null ? cast (type, AssetType) : null; var items = []; for (id in this.type.keys ()) {