/// <summary> /// Draws a resizable texture. /// See the documentation for an explanation of how resizable textures work. /// </summary> /// <param name="texture">The resizable texture to draw.</param> /// <param name="bounds">The bounds that the texture should be resized to.</param> /// <param name="color">The color to multiply with the colors of the texture. If unspecified the colors of the texture will be unchanged.</param> /// <param name="blendMode">The blend mode to use when drawing the texture. If unspecified the texture will be drawn using the standard alpha-based blend mode.</param> /// <param name="scaleMode">The scale mode to use when drawing the texture. If unspecified the texture will be linearly interpolated.</param> public static void DrawResizableTexture(ResizableTexture texture, Bounds2 bounds, Color?color = null, TextureBlendMode blendMode = TextureBlendMode.Normal, TextureScaleMode scaleMode = TextureScaleMode.Linear) { DrawTextureSetup(texture.Handle, color, blendMode, scaleMode); /* * 0 bxmin bxmax txmax * v v v v * +----|-------------|----+ < tymax * | 1 | 5 | 2 | * -----+-------------+----- < bymax * | | | | * | | | | * | 6 | 9 | 7 | * | | | | * | | | | * -----+-------------+----- < bymin * | 3 | 8 | 4 | * +----|-------------|----+ < 0 */ int bxmin = texture.LeftOffset; int bxmax = texture.RightOffset; int bymin = texture.TopOffset; int bymax = texture.BottomOffset; int txmax = texture.Width; int tymax = texture.Height; int px = (int)bounds.Position.X; int py = (int)bounds.Position.Y; // Don't let the overall size be so small that segment 9 has a negative size in either dimension: int sx = Math.Max((int)bounds.Size.X, txmax - bxmax + bxmin); int sy = Math.Max((int)bounds.Size.Y, tymax - bymax + bymin); // Draw each of the nine segments: DrawResizableTextureSegment(texture, 0, 0, bxmin, bymin, px, py, bxmin, bymin); DrawResizableTextureSegment(texture, bxmax, 0, txmax - bxmax, bymin, px + sx - (txmax - bxmax), py, txmax - bxmax, bymin); DrawResizableTextureSegment(texture, 0, bymax, bxmin, tymax - bymax, px, py + sy - (tymax - bymax), bxmin, tymax - bymax); DrawResizableTextureSegment(texture, bxmax, bymax, txmax - bxmax, tymax - bymax, px + sx - (txmax - bxmax), py + sy - (tymax - bymax), txmax - bxmax, tymax - bymax); DrawResizableTextureSegment(texture, bxmin, 0, bxmax - bxmin, bymin, px + bxmin, py, sx - bxmin - (txmax - bxmax), bymin); DrawResizableTextureSegment(texture, 0, bymin, bxmin, bymax - bymin, px, py + bymin, bxmin, sy - bymin - (tymax - bymax)); DrawResizableTextureSegment(texture, bxmax, bymin, txmax - bxmax, bymax - bymin, px + sx - (txmax - bxmax), py + bymin, txmax - bxmax, sy - bymin - (tymax - bymax)); DrawResizableTextureSegment(texture, bxmin, bymax, bxmax - bxmin, tymax - bymax, px + bxmin, py + sy - (tymax - bymax), sx - bxmin - (txmax - bxmax), tymax - bymax); DrawResizableTextureSegment(texture, bxmin, bymin, bxmax - bxmin, bymax - bymin, px + bxmin, py + bymin, sx - bxmin - (txmax - bxmax), sy - bymin - (tymax - bymax)); }
private static void DrawResizableTextureSegment(ResizableTexture texture, int subtextureX, int subtextureY, int subtextureW, int subtextureH, int destX, int destY, int destW, int destH) { // Don't draw invisible segments: if (subtextureW <= 0 || subtextureH <= 0) { return; } SDL.SDL_Rect src; src.x = subtextureX; src.y = subtextureY; src.w = subtextureW; src.h = subtextureH; SDL.SDL_Rect dest; dest.x = destX; dest.y = destY; dest.w = destW; dest.h = destH; SDL.SDL_RenderCopy(Renderer, texture.Handle, ref src, ref dest); }