texturedPolygonMt function

bool texturedPolygonMt(
  1. Pointer<SdlRenderer> renderer,
  2. Pointer<Int16> vx,
  3. Pointer<Int16> vy,
  4. int n,
  5. Pointer<SdlSurface> texture,
  6. int textureDx,
  7. int textureDy,
)

Implementation

bool texturedPolygonMt(
    Pointer<SdlRenderer> renderer,
    Pointer<Int16> vx,
    Pointer<Int16> vy,
    int n,
    Pointer<SdlSurface> texture,
    int textureDx,
    int textureDy) {
  bool result = true;
  int i;
  int y, xa, xb;
  int minx, maxx, miny, maxy;
  int x1, y1;
  int x2, y2;
  int ind1, ind2;
  var gfxPrimitivesPolyInts = <int>[];
  Pointer<SdlTexture> textureAsTexture;

  /*
	* Sanity check number of edges
	*/
  if (n < 3) {
    return false;
  }

  /*
	* Map polygon cache
	*/

  /*
	* Allocate temp array, only grow array
	*/

  /*
	* Check temp array
	*/

  /*
	* Update cache variables
	*/

  /*
	* Check temp array again
	*/

  /*
	* Determine X,Y minima,maxima
	*/
  miny = vy[0];
  maxy = vy[0];
  minx = vx[0];
  maxx = vx[0];
  for (i = 1; (i < n); i++) {
    if (vy[i] < miny) {
      miny = vy[i];
    } else if (vy[i] > maxy) {
      maxy = vy[i];
    }
    if (vx[i] < minx) {
      minx = vx[i];
    } else if (vx[i] > maxx) {
      maxx = vx[i];
    }
  }

  /* Create texture for drawing */
  textureAsTexture = sdlCreateTextureFromSurface(renderer, texture);
  if (textureAsTexture == nullptr) {
    return false;
  }
  sdlSetTextureBlendMode(textureAsTexture, SDL_BLENDMODE_BLEND);

  /*
	* Draw, scanning y
	*/
  result = true;
  for (y = miny; (y <= maxy); y++) {
    gfxPrimitivesPolyInts.clear();
    for (i = 0; (i < n); i++) {
      if (i == 0) {
        ind1 = n - 1;
        ind2 = 0;
      } else {
        ind1 = i - 1;
        ind2 = i;
      }
      y1 = vy[ind1];
      y2 = vy[ind2];
      if (y1 < y2) {
        x1 = vx[ind1];
        x2 = vx[ind2];
      } else if (y1 > y2) {
        y2 = vy[ind1];
        y1 = vy[ind2];
        x2 = vx[ind1];
        x1 = vx[ind2];
      } else {
        continue;
      }
      if (((y >= y1) && (y < y2)) || ((y == maxy) && (y > y1) && (y <= y2))) {
        gfxPrimitivesPolyInts
            .add(((65536 * (y - y1)) ~/ (y2 - y1)) * (x2 - x1) + (65536 * x1));
      }
    }
    gfxPrimitivesPolyInts.sort((a, b) => b - a);

    for (i = 0; i < gfxPrimitivesPolyInts.length; i += 2) {
      xa = gfxPrimitivesPolyInts[i] + 1;
      xa = (xa >> 16) + ((xa & 32768) >> 15);
      xb = gfxPrimitivesPolyInts[i + 1] - 1;
      xb = (xb >> 16) + ((xb & 32768) >> 15);
      if (result) {
        result = _hLineTextured(
            renderer,
            xa.toDouble(),
            xb.toDouble(),
            y.toDouble(),
            textureAsTexture,
            texture.ref.w,
            texture.ref.h,
            textureDx,
            textureDy);
      }
    }
  }

  sdlRenderPresent(renderer);
  sdlDestroyTexture(textureAsTexture);

  return (result);
}