<commit_before>part of gamedev_helpers;

class WebGlCanvasCleaningSystem extends VoidEntitySystem {
  RenderingContext2 gl;

  WebGlCanvasCleaningSystem(this.gl);

  @override
  void initialize() {
    gl.clearColor(0.0, 0.0, 0.0, 1.0);
  }

  @override
  void processSystem() {
    gl.clear(RenderingContext2.COLOR_BUFFER_BIT |
        RenderingContext2.DEPTH_BUFFER_BIT);
  }
}

abstract class WebGlRenderingMixin {
  static const int fsize = Float32List.bytesPerElement;

  RenderingContext2 gl;
  Program program;
  ShaderSource shaderSource;
  Buffer elementBuffer;
  Buffer indexBuffer;
  Map<String, Buffer> buffers = <String, Buffer>{};
  bool success = true;

  void initProgram() {
    final vShader =
        _createShader(RenderingContext.VERTEX_SHADER, shaderSource.vShader);
    final fShader =
        _createShader(RenderingContext.FRAGMENT_SHADER, shaderSource.fShader);

    _createProgram(vShader, fShader);
  }

  void _createProgram(Shader vShader, Shader fShader) {
    program = gl.createProgram();
    gl
      ..attachShader(program, vShader)
      ..attachShader(program, fShader)
      ..linkProgram(program);
    final linkSuccess =
        gl.getProgramParameter(program, RenderingContext2.LINK_STATUS);
    if (!linkSuccess) {
      print(
          '$runtimeType - Error linking program: ${gl.getProgramInfoLog(program)}');
      success = false;
    }
  }

  Shader _createShader(int type, String source) {
    final shader = gl.createShader(type);
    gl
      ..shaderSource(shader, source)
      ..compileShader(shader);
    final compileSuccess =
        gl.getShaderParameter(shader, RenderingContext2.COMPILE_STATUS);
    if (!compileSuccess) {
      print(
          '$runtimeType - Error compiling shader: ${gl.getShaderInfoLog(shader)}');
      success = false;
    }
    return shader;
  }

  void buffer(String attribute, Float32List items, int itemSize,
      {int usage: DYNAMIC_DRAW}) {
    var buffer = buffers[attribute];
    if (null == buffer) {
      buffer = gl.createBuffer();
      buffers[attribute] = buffer;
    }
    final attribLocation = gl.getAttribLocation(program, attribute);
    gl
      ..bindBuffer(RenderingContext.ARRAY_BUFFER, buffer)
      ..bufferData(RenderingContext.ARRAY_BUFFER, items, usage)
      ..vertexAttribPointer(
          attribLocation, itemSize, RenderingContext2.FLOAT, false, 0, 0)
      ..enableVertexAttribArray(attribLocation);
  }

  void bufferElements(
      List<Attrib> attributes, Float32List items, List<int> indices) {
    if (null == elementBuffer) {
      elementBuffer = gl.createBuffer();
      indexBuffer = gl.createBuffer();
    }
    gl
      ..bindBuffer(RenderingContext.ARRAY_BUFFER, elementBuffer)
      ..bufferData(RenderingContext2.ARRAY_BUFFER, items,
          RenderingContext2.DYNAMIC_DRAW);
    int offset = 0;
    int elementsPerItem = 0;
    for (Attrib attribute in attributes) {
      elementsPerItem += attribute.size;
    }
    for (Attrib attribute in attributes) {
      final attribLocation = gl.getAttribLocation(program, attribute.name);
      gl
        ..vertexAttribPointer(
            attribLocation,
            attribute.size,
            RenderingContext2.FLOAT,
            false,
            fsize * elementsPerItem,
            fsize * offset)
        ..enableVertexAttribArray(attribLocation);
      offset += attribute.size;
    }
    gl
      ..bindBuffer(RenderingContext.ELEMENT_ARRAY_BUFFER, indexBuffer)
      ..bufferData(RenderingContext.ELEMENT_ARRAY_BUFFER, indices,
          RenderingContext2.DYNAMIC_DRAW);
  }

  String get vShaderFile;
  String get fShaderFile;
  String get libName => null;
}

class Attrib {
  final String name;
  final int size;
  const Attrib(this.name, this.size);
}

abstract class WebGlRenderingSystem extends EntitySystem
    with WebGlRenderingMixin {
  int maxLength = 0;

  WebGlRenderingSystem(RenderingContext2 gl, Aspect aspect) : super(aspect) {
    this.gl = gl;
  }

  @override
  void initialize() {
    initProgram();
  }

  @override
  void processEntities(Iterable<Entity> entities) {
    final length = entities.length;
    if (length > 0) {
      gl.useProgram(program);<commit_msg>better typing of bufferElements and helper methods to draw points/triangles<commit_after>part of gamedev_helpers;

class WebGlCanvasCleaningSystem extends VoidEntitySystem {
  RenderingContext2 gl;

  WebGlCanvasCleaningSystem(this.gl);

  @override
  void initialize() {
    gl.clearColor(0.0, 0.0, 0.0, 1.0);
  }

  @override
  void processSystem() {
    gl.clear(RenderingContext2.COLOR_BUFFER_BIT |
        RenderingContext2.DEPTH_BUFFER_BIT);
  }
}

abstract class WebGlRenderingMixin {
  static const int fsize = Float32List.bytesPerElement;

  RenderingContext2 gl;
  Program program;
  ShaderSource shaderSource;
  Buffer elementBuffer;
  Buffer indexBuffer;
  Map<String, Buffer> buffers = <String, Buffer>{};
  bool success = true;

  void initProgram() {
    final vShader =
        _createShader(RenderingContext.VERTEX_SHADER, shaderSource.vShader);
    final fShader =
        _createShader(RenderingContext.FRAGMENT_SHADER, shaderSource.fShader);

    _createProgram(vShader, fShader);
  }

  void _createProgram(Shader vShader, Shader fShader) {
    program = gl.createProgram();
    gl
      ..attachShader(program, vShader)
      ..attachShader(program, fShader)
      ..linkProgram(program);
    final linkSuccess =
        gl.getProgramParameter(program, RenderingContext2.LINK_STATUS);
    if (!linkSuccess) {
      print(
          '$runtimeType - Error linking program: ${gl.getProgramInfoLog(program)}');
      success = false;
    }
  }

  Shader _createShader(int type, String source) {
    final shader = gl.createShader(type);
    gl
      ..shaderSource(shader, source)
      ..compileShader(shader);
    final compileSuccess =
        gl.getShaderParameter(shader, RenderingContext2.COMPILE_STATUS);
    if (!compileSuccess) {
      print(
          '$runtimeType - Error compiling shader: ${gl.getShaderInfoLog(shader)}');
      success = false;
    }
    return shader;
  }

  void buffer(String attribute, Float32List items, int itemSize,
      {int usage: DYNAMIC_DRAW}) {
    var buffer = buffers[attribute];
    if (null == buffer) {
      buffer = gl.createBuffer();
      buffers[attribute] = buffer;
    }
    final attribLocation = gl.getAttribLocation(program, attribute);
    gl
      ..bindBuffer(RenderingContext.ARRAY_BUFFER, buffer)
      ..bufferData(RenderingContext.ARRAY_BUFFER, items, usage)
      ..vertexAttribPointer(
          attribLocation, itemSize, RenderingContext2.FLOAT, false, 0, 0)
      ..enableVertexAttribArray(attribLocation);
  }

  void bufferElements(
      List<Attrib> attributes, Float32List items, Uint16List indices) {
    if (null == elementBuffer) {
      elementBuffer = gl.createBuffer();
      indexBuffer = gl.createBuffer();
    }
    gl
      ..bindBuffer(RenderingContext.ARRAY_BUFFER, elementBuffer)
      ..bufferData(RenderingContext2.ARRAY_BUFFER, items,
          RenderingContext2.DYNAMIC_DRAW);
    int offset = 0;
    int elementsPerItem = 0;
    for (Attrib attribute in attributes) {
      elementsPerItem += attribute.size;
    }
    for (Attrib attribute in attributes) {
      final attribLocation = gl.getAttribLocation(program, attribute.name);
      gl
        ..vertexAttribPointer(
            attribLocation,
            attribute.size,
            RenderingContext2.FLOAT,
            false,
            fsize * elementsPerItem,
            fsize * offset)
        ..enableVertexAttribArray(attribLocation);
      offset += attribute.size;
    }
    gl
      ..bindBuffer(RenderingContext.ELEMENT_ARRAY_BUFFER, indexBuffer)
      ..bufferData(RenderingContext.ELEMENT_ARRAY_BUFFER, indices,
          RenderingContext2.DYNAMIC_DRAW);
  }

  void drawTriangles(
      List<Attrib> attributes, Float32List items, Uint16List indices) {
    bufferElements(attributes, items, indices);
    gl.drawElements(RenderingContext2.TRIANGLES, indices.length,
        RenderingContext2.UNSIGNED_SHORT, 0);
  }

  void drawPoints(
      List<Attrib> attributes, Float32List items, Uint16List indices) {
    bufferElements(attributes, items, indices);
    gl.drawElements(RenderingContext2.POINTS, indices.length,
        RenderingContext2.UNSIGNED_SHORT, 0);
  }

  String get vShaderFile;
  String get fShaderFile;
  String get libName => null;
}

class Attrib {
  final String name;
  final int size;
  const Attrib(this.name, this.size);
}

abstract class WebGlRenderingSystem extends EntitySystem
    with WebGlRenderingMixin {
  int maxLength = 0;

  WebGlRenderingSystem(RenderingContext2 gl, Aspect aspect) : super(aspect) {
    this.gl = gl;
  }

  @override
  void initialize() {
    initProgram();
  }

  @override
  void processEntities(Iterable<Entity> entities) {
    final length = entities.length;
    if (length > 0) {
      gl.useProgram(program);
{'fraction_non_alphanumeric': 0.09524342473419138, 'fraction_numerical': 0.008953553441522105, 'mean_word_length': 3.213107024988213, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7244382', 'n_tokens_mistral': 2681, 'n_tokens_neox': 2641, 'n_words': 574}