使用期货进行异步加载 [英] Use of futures for async loading

查看:117
本文介绍了使用期货进行异步加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定学习Dart,我的项目将是一个小型的webgl游戏。

I decided to learn Dart and my project for doing so will be a small webgl game.

使用单线程与aysnc操作和未来是新的

The use of a single thread with aysnc operations and futures are new to me though and while I understand the concepts I'm finding it a little hard to know how to use them.

在我的游戏中,我想加载一个webgl GLSL程序。要创建这个程序,我首先要从文件加载一个顶点着色器和一个片段着色器。所以我写了这样似乎工作确定加载和编译着色器。

In my game I want to load a webgl GLSL program. To create this program I first have to load a vertex shader and a fragment shader from files. So I've written this which seems to work ok for loading and compiling the shaders.

我有问题是如何知道当两个着色器加载,然后我可以从两个加载的着色器创建程序对象。以相同的方式,我可以使用.then对未来从HttpRequest.getString重新运行我需要以某种方式做同样的,有一个.then运行时,片段和顶点着色器加载。

The problem I have is how do I then know when both shaders are loaded so that I can then create the "program" object from the two loaded shaders. In the same way that I can use .then on the future retruned from HttpRequest.getString I need to somehow do the same and have a .then that runs when both the fragment and the vertex shader are loaded.

我确定我在这里缺少一些重要和容易的东西,但这对我来说是新的,我正在努力学习如何使用它。 (这是好的,学习新的东西很好!)

I'm sure i'm missing something major and easy here, but this is new to me and I'm struggling a bit to know how to use it. (Which is good, it's good to learn new things!)

  //
  // Start the process of loading the shaders. 
  //
  void initShaders()
  {
    Shader vs = gl.createShader(VERTEX_SHADER);
    Shader fs = gl.createShader(FRAGMENT_SHADER);

    var vsFuture = HttpRequest.getString('basic.vert');
    var fsFuture = HttpRequest.getString('basic.frag');

    //
    // When we have the vertex shader source, compile it
    //
    vsFuture.then((src) {
      gl.shaderSource(vs, src);
      gl.compileShader(vs);
      if (!gl.getShaderParameter(vs, COMPILE_STATUS)) {
        throw new Exception(gl.getShaderInfoLog(vs));
      }
      print ('Vertex shader compiled');
    });

    //
    // When we have the fragment shader source, compile it
    //
    fsFuture.then((src) {
      gl.shaderSource(fs, src);
      gl.compileShader(fs);
      if (!gl.getShaderParameter(fs, COMPILE_STATUS)) {
        throw new Exception(gl.getShaderInfoLog(fs));
      }
      print ('Fragment shader compiled');
    });


    //
    // When both the fragment shader and the vertex shader are 
    // compiled, we need to link them. But how do we know??
    //

    ***something... to make this be called when both the vertex and
    fragment shaders are compiled...***
    {
      program = gl.createProgram();
      gl.attachShader(program, fs);
      gl.attachShader(program, ps);
      gl.linkProgram(program);

    }


推荐答案

然后返回在执行then回调之后触发的 Future 。所以你有两个未来从编译步骤。之后,您可以使用 未来。 wait 等待期货列表。 wait 函数返回在所有输入期货完成后触发的未来

All call to then returns a Future that is triggered after the then callback is executed. So you have two futures from the compile step. After that you can use Future.wait to wait for a list of futures. The wait function returns a Future that is triggered after all input futures are completed.

这样的东西(不完整,但是它是如何工作的):

Someting like this (not complete but for an idea how it works):

   var vsCompiledFuture = vsFuture.then((src) {
     // Compile
   });

   var fsCompiledFuture = fsFuture.then((src) {
     // Compile
   });

   Future.wait([vsCompiledFuture, fsCompiledFuture]).then((_) {
     // Link here...
   });

如果返回未来回调中编译的着色器, Future.wait 回调接收包含所有期货的所有结果的列表。

If you return the compiled shaders from you future callback, the Future.wait callback receives a list with all results from all futures.

这篇关于使用期货进行异步加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆