Skip to content

Commit

Permalink
Update for 0.15
Browse files Browse the repository at this point in the history
  • Loading branch information
aforemny committed May 14, 2015
1 parent ed143a1 commit 212807b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 31 deletions.
56 changes: 32 additions & 24 deletions src/Native/WebGL.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,41 @@ Elm.Native.WebGL.make = function(elm) {
}

var createNode = Elm.Native.Graphics.Element.make(elm).createNode;
var newElement = Elm.Graphics.Element.make(elm).newElement;
var newElement = Elm.Native.Graphics.Element.make(elm).newElement;

var List = Elm.Native.List.make(elm);
var Utils = Elm.Native.Utils.make(elm);
var List = Elm.List.make(elm);
var Utils = Elm.Native.Utils.make(elm);
var Signal = Elm.Signal.make(elm);
var Tuple2 = Utils.Tuple2;
var Task = Elm.Native.Task.make(elm);

function unsafeCoerceGLSL(src) {
return { src : src };
}

function loadTex(source) {

var response = Signal.constant(elm.Http.values.Waiting);

var img = new Image();

img.onload = function() {
var success = elm.Http.values.Success({img:img});
elm.notify(response.id, success);
}

img.onerror = function(e) {
var failure = A2(elm.Http.values.Failure,0,"Failed");
elm.notify(response.id, failure);
}

img.src = source;

return response;
function toTexture(req) {
var img = new Image();
img.src = URL.createObjectURL(req.response);
return {img:img};
};

function loadTexture(url) {
return Task.asyncFunction(function(callback) {
var req = new XMLHttpRequest;
req.addEventListener('error', function() {
return callback(Task.fail({ ctor: 'NetworkError' }));
});
req.addEventListener('timeout', function() {
return callback(Task.fail({ ctor: 'Timeout' }));
});
req.addEventListener('load', function() {
return callback(Task.succeed(toTexture(req)));
});
req.open('get', url, true);
req.timeout = 30;
req.responseType = 'blob';
req.send();
});
}

function entity(vert, frag, buffer, uniforms) {
Expand Down Expand Up @@ -209,7 +213,10 @@ Elm.Native.WebGL.make = function(elm) {
}

var numIndices = 3 * List.length(bufferElems);
var indices = List.toArray(List.range(0, numIndices - 1));
var indices = [];
for (var i = 0; i < numIndices; i += 1) {
indices.push(i);
}
LOG("Created index buffer");
var indexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
Expand All @@ -231,6 +238,7 @@ Elm.Native.WebGL.make = function(elm) {

gl.viewport(0, 0, model.w, model.h);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

LOG("Drawing");

function drawEntity(entity) {
Expand Down Expand Up @@ -444,7 +452,7 @@ Elm.Native.WebGL.make = function(elm) {

return elm.Native.WebGL.values = {
unsafeCoerceGLSL:unsafeCoerceGLSL,
loadTex:loadTex,
loadTexture:loadTexture,
entity:F4(entity),
webgl:F2(webgl)
};
Expand Down
13 changes: 6 additions & 7 deletions src/WebGL.elm
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,16 @@ unsafeShader =

type Texture = Texture

type Error =
NetworkError
| Timeout

{-| Loads a texture from the given url. PNG and JPEG are known to work, but
other formats have not been as well-tested yet.
-}
loadTexture : String -> Task RawError Texture
loadTexture url =
let request = { verb = "GET", headers = [], url = url, body = empty }
in send defaultSettings request `andThen` decodeTexture

decodeTexture : Response -> Task RawError Texture
decodeTexture = Native.WebGL.decodeTexture
loadTexture : String -> Task Error Texture
loadTexture url =
Native.WebGL.loadTexture url

type Entity = Entity

Expand Down

0 comments on commit 212807b

Please sign in to comment.