Add application.init(), simplify SimpleImage sample

This commit is contained in:
Joshua Granick
2014-07-09 16:32:55 -07:00
parent 10a2a74cd8
commit 0faa375f72
4 changed files with 63 additions and 131 deletions

View File

@@ -18,6 +18,7 @@ class Application extends Module {
public static var onUpdate = new Event<Int->Void> (); public static var onUpdate = new Event<Int->Void> ();
private static var __eventInfo = new UpdateEventInfo (); private static var __eventInfo = new UpdateEventInfo ();
private static var __initialized:Bool;
private static var __instance:Application; private static var __instance:Application;
private static var __registered:Bool; 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 onKeyDown (keyCode:Int, modifier:Int):Void {}
public function onKeyUp (keyCode:Int, modifier:Int):Void {} public function onKeyUp (keyCode:Int, modifier:Int):Void {}
public function onMouseDown (x:Float, y:Float, button:Int):Void {} public function onMouseDown (x:Float, y:Float, button:Int):Void {}

View File

@@ -121,6 +121,13 @@ class Renderer {
var context = window.currentRenderer.context; var context = window.currentRenderer.context;
if (!Application.__initialized) {
Application.__initialized = true;
Application.__instance.init (context);
}
Application.__instance.render (context); Application.__instance.render (context);
onRender.dispatch (context); onRender.dispatch (context);

View File

@@ -3,30 +3,20 @@ package;
import lime.app.Application; import lime.app.Application;
import lime.geom.Matrix4; import lime.geom.Matrix4;
import lime.graphics.Image; import lime.graphics.*;
import lime.graphics.GLBuffer;
import lime.graphics.GLProgram;
import lime.graphics.GLRenderContext;
import lime.graphics.GLTexture;
import lime.graphics.RenderContext;
import lime.utils.Float32Array; import lime.utils.Float32Array;
import lime.utils.GLUtils;
import lime.Assets; import lime.Assets;
#if html5
@:access(lime.graphics.GL)
#end
class Main extends Application { class Main extends Application {
private var initialized:Bool; private var buffer:GLBuffer;
private var program:GLProgram;
private var shaderProgram:GLProgram;
private var texCoordBuffer:GLBuffer;
private var texture:GLTexture; private var texture:GLTexture;
private var vertexBuffer:GLBuffer; private var textureAttribute:Int;
private var vertexAttribute:Int;
public function new () { 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) { 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): case DOM (element):
#if js #if js
@@ -49,11 +46,17 @@ class Main extends Application {
element.appendChild (image); element.appendChild (image);
#end #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): case OPENGL (gl):
// Initialize shaders var vertexSource =
var vertexShaderSource =
"attribute vec4 aPosition; "attribute vec4 aPosition;
attribute vec2 aTexCoord; attribute vec2 aTexCoord;
@@ -68,17 +71,7 @@ class Main extends Application {
}"; }";
var vertexShader = gl.createShader (gl.VERTEX_SHADER); var fragmentSource =
gl.shaderSource (vertexShader, vertexShaderSource);
gl.compileShader (vertexShader);
if (gl.getShaderParameter (vertexShader, gl.COMPILE_STATUS) == 0) {
throw "Error compiling vertex shader";
}
var fragmentShaderSource =
#if !desktop #if !desktop
"precision mediump float;" + "precision mediump float;" +
@@ -91,61 +84,36 @@ class Main extends Application {
gl_FragColor = texture2D (uImage0, vTexCoord); gl_FragColor = texture2D (uImage0, vTexCoord);
}"; }";
var fragmentShader = gl.createShader (gl.FRAGMENT_SHADER); program = GLUtils.createProgram (vertexSource, fragmentSource);
gl.shaderSource (fragmentShader, fragmentShaderSource); gl.useProgram (program);
gl.compileShader (fragmentShader);
if (gl.getShaderParameter (fragmentShader, gl.COMPILE_STATUS) == 0) { vertexAttribute = gl.getAttribLocation (program, "aPosition");
textureAttribute = gl.getAttribLocation (program, "aTexCoord");
var matrixUniform = gl.getUniformLocation (program, "uMatrix");
var imageUniform = gl.getUniformLocation (program, "uImage0");
throw "Error compiling fragment shader"; gl.enableVertexAttribArray (vertexAttribute);
gl.enableVertexAttribArray (textureAttribute);
} gl.uniform1i (imageUniform, 0);
gl.uniformMatrix4fv (matrixUniform, false, Matrix4.createOrtho (0, window.width, window.height, 0, -1000, 1000));
shaderProgram = gl.createProgram ();
gl.attachShader (shaderProgram, vertexShader);
gl.attachShader (shaderProgram, fragmentShader);
gl.linkProgram (shaderProgram);
if (gl.getProgramParameter (shaderProgram, gl.LINK_STATUS) == 0) {
throw "Unable to initialize the shader program.";
}
// Create buffers
var image = Assets.getImage ("assets/lime.png"); var image = Assets.getImage ("assets/lime.png");
var vertices = [ var data = [
image.width, image.height, 0, image.width, image.height, 0, 1, 1,
0, image.height, 0, 0, image.height, 0, 0, 1,
image.width, 0, 0, image.width, 0, 0, 1, 0,
0, 0, 0 0, 0, 0, 0, 0
]; ];
vertexBuffer = gl.createBuffer (); buffer = gl.createBuffer ();
gl.bindBuffer (gl.ARRAY_BUFFER, vertexBuffer); gl.bindBuffer (gl.ARRAY_BUFFER, buffer);
gl.bufferData (gl.ARRAY_BUFFER, new Float32Array (cast vertices), gl.STATIC_DRAW); gl.bufferData (gl.ARRAY_BUFFER, new Float32Array (cast data), gl.STATIC_DRAW);
gl.bindBuffer (gl.ARRAY_BUFFER, null); 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 (); texture = gl.createTexture ();
gl.bindTexture (gl.TEXTURE_2D, texture); gl.bindTexture (gl.TEXTURE_2D, texture);
gl.texParameteri (gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); 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 { public override function render (context:RenderContext):Void {
if (!initialized) {
initialize (context);
}
switch (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): case OPENGL (gl):
var r = ((config.background >> 16) & 0xFF) / 0xFF; var r = ((config.background >> 16) & 0xFF) / 0xFF;
@@ -205,17 +148,6 @@ class Main extends Application {
gl.clearColor (r, g, b, a); gl.clearColor (r, g, b, a);
gl.clear (gl.COLOR_BUFFER_BIT); 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.activeTexture (gl.TEXTURE0);
gl.bindTexture (gl.TEXTURE_2D, texture); gl.bindTexture (gl.TEXTURE_2D, texture);
@@ -223,27 +155,12 @@ class Main extends Application {
gl.enable (gl.TEXTURE_2D); gl.enable (gl.TEXTURE_2D);
#end #end
gl.bindBuffer (gl.ARRAY_BUFFER, vertexBuffer); gl.bindBuffer (gl.ARRAY_BUFFER, buffer);
gl.vertexAttribPointer (vertexAttribute, 3, gl.FLOAT, false, 0, 0); gl.vertexAttribPointer (vertexAttribute, 3, gl.FLOAT, false, 5 * Float32Array.BYTES_PER_ELEMENT, 0);
gl.bindBuffer (gl.ARRAY_BUFFER, texCoordBuffer); gl.vertexAttribPointer (textureAttribute, 2, gl.FLOAT, false, 5 * Float32Array.BYTES_PER_ELEMENT, 3 * Float32Array.BYTES_PER_ELEMENT);
gl.vertexAttribPointer (texCoordAttribute, 2, gl.FLOAT, false, 0, 0);
gl.uniformMatrix4fv (matrixUniform, false, matrix);
gl.uniform1i (imageUniform, 0);
gl.drawArrays (gl.TRIANGLE_STRIP, 0, 4); 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: default:
} }

View File

@@ -113,7 +113,7 @@ class DefaultAssetLibrary extends AssetLibrary {
public override function exists (id:String, type:String):Bool { 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); var assetType = this.type.get (id);
if (assetType != null) { if (assetType != null) {
@@ -341,7 +341,7 @@ class DefaultAssetLibrary extends AssetLibrary {
public override function isLocal (id:String, type:String):Bool { public override function isLocal (id:String, type:String):Bool {
var requestedType = cast (type, AssetType); var requestedType = type != null ? cast (type, AssetType) : null;
#if flash #if flash
@@ -360,7 +360,7 @@ class DefaultAssetLibrary extends AssetLibrary {
public override function list (type:String):Array<String> { public override function list (type:String):Array<String> {
var requestedType = cast (type, AssetType); var requestedType = type != null ? cast (type, AssetType) : null;
var items = []; var items = [];
for (id in this.type.keys ()) { for (id in this.type.keys ()) {