File Texture.cs

File List > Render > src > Texture.cs

Go to the documentation of this file.

using System.Drawing;

namespace Qkmaxware.Rendering {

public enum TextureWrapMode {
    Clamp,
    Repeat,
}

public abstract class Texture2D {
    public TextureWrapMode WrapMode = TextureWrapMode.Clamp;

    public abstract int Height {get;}
    public abstract int Width {get;}

    public abstract Color this[int x, int y] {get;}
}

public class PixelTexture : Texture2D {
    private Color[,] pixels;

    public override int Height => pixels.GetLength(0);
    public override int Width => pixels.GetLength(1);

    public PixelTexture (Color[,] pixels) {
        this.pixels = pixels;
    }

    private int Wrap(int x, int x_min, int x_max) {
        return (((x - x_min) % (x_max - x_min)) + (x_max - x_min)) % (x_max - x_min) + x_min;
    }
    private int Clamp(int x, int x_min, int x_max) {
        return x < x_min ? x_min : (x > x_max ? x_max : x);
    }

    public override Color this [int x, int y] {
        get {
            switch (WrapMode) {
                case TextureWrapMode.Repeat:
                    x = Wrap(x, 0, this.Width - 1);
                    y = Wrap(y, 0, this.Height - 1);
                    break;
                case TextureWrapMode.Clamp:
                default:
                    x = Clamp(x, 0, this.Width - 1);
                    y = Clamp(y, 0, this.Height - 1);
                    break;
            }

            return pixels[y, x];
        }
    }
}

}