dispose method

void dispose()

Decrements the reference count of this texture. If the reference count reaches zero, the texture is deleted, and any onDispose callbacks are triggered. Logs an error if the reference count is already zero or less.

Implementation

void dispose() {
  if (_refCount < 1) {
    die("Refcount of Texture cannot be less than zero.");
  }

  _refCount--;

  if (_refCount == 0) {
    _gl.deleteTexture(texture);
    // TODO: It is possible that we still have draw commands issued with this texture.
    // we should either flush the draw commands
    // or draw commands should retain the texture (maybe complex)
    // or we should delay it on frame boundaries???
    texture = null;
    for (var func in _onDispose) {
      func(this);
    }
    _onDispose.clear();
  }
}