// ====================================================================================== // Texture drawing // ====================================================================================== private static void DrawTextureSetup(IntPtr textureHandle, Color?color, TextureBlendMode blendMode, TextureScaleMode scaleMode) { Color finalColor = color ?? Color.White; SDL.SDL_SetTextureColorMod(textureHandle, finalColor.R, finalColor.G, finalColor.B); SDL.SDL_SetTextureAlphaMod(textureHandle, finalColor.A); SDL.SDL_SetTextureBlendMode(textureHandle, (SDL.SDL_BlendMode)blendMode); SDL.SDL_SetTextureScaleMode(textureHandle, (SDL.SDL_ScaleMode)scaleMode); }
/// <summary> /// Draws a texture. /// </summary> /// <param name="texture">The texture to draw.</param> /// <param name="position">The position where the texture will be drawn.</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="size">The destination size of the texture. If unspecified the original texture size will be used.</param> /// <param name="rotation">The amount the texture will be rotated clockwise (in degrees). If unspecified the texture will not be rotated.</param> /// <param name="pivot">The offset from position to the pivot that the texture will be rotated about. If unspecified the center of the destination bounds will be used.</param> /// <param name="mirror">The mirroring to apply to the texture. If unspecified the texture will not be mirrored.</param> /// <param name="source">The source bounds of the texture to draw. If unspecified the entire texture will be drawn.</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 DrawTexture(Texture texture, Vector2 position, Color?color = null, Vector2?size = null, float rotation = 0, Vector2?pivot = null, TextureMirror mirror = TextureMirror.None, Bounds2?source = null, TextureBlendMode blendMode = TextureBlendMode.Normal, TextureScaleMode scaleMode = TextureScaleMode.Linear) { DrawTextureSetup(texture.Handle, color, blendMode, scaleMode); SDL.SDL_Rect src; SDL.SDL_Rect dest; if (source.HasValue) { // Use the specified source coordinates: src.x = (int)source.Value.Position.X; src.y = (int)source.Value.Position.Y; src.w = (int)source.Value.Size.X; src.h = (int)source.Value.Size.Y; dest.x = (int)position.X; dest.y = (int)position.Y; dest.w = src.w; dest.h = src.h; } else { // Use the full texture as the source: src.x = 0; src.y = 0; src.w = texture.Width; src.h = texture.Height; dest.x = (int)position.X; dest.y = (int)position.Y; dest.w = texture.Width; dest.h = texture.Height; } // Apply the size override, if specified: if (size.HasValue) { dest.w = (int)size.Value.X; dest.h = (int)size.Value.Y; } // Apply the pivot override, if specified: SDL.SDL_Point center; if (pivot.HasValue) { center.x = (int)pivot.Value.X; center.y = (int)pivot.Value.Y; } else { center.x = dest.w / 2; center.y = dest.h / 2; } SDL.SDL_RenderCopyEx(Renderer, texture.Handle, ref src, ref dest, rotation, ref center, (SDL.SDL_RendererFlip)mirror); }
/// <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)); }