Beispiel #1
0
        public static string AsHint(this TextureScalingQuality quality)
        {
            switch (quality)
            {
            case TextureScalingQuality.Nearest: return("nearest");

            case TextureScalingQuality.Linear: return("linear");

            case TextureScalingQuality.Anisotropic: return("best");

            default: return("");
            }
        }
Beispiel #2
0
        /// <summary>
        /// Creates a static texture from an existing surface.
        /// </summary>
        /// <param name="surface">Surface to create texture from</param>
        /// <param name="quality">Filtering quality when texture is scaled</param>
        /// <remarks>
        /// The surface is not modified or disposed by this function.
        /// <see cref="TextureAccess" /> is static.
        /// The pixel format of the created texture may be different from the
        /// pixel format of the surface.
        /// </remarks>
        public Texture CreateTextureFromSurface(Surface surface,
                                                TextureScalingQuality quality = TextureScalingQuality.Nearest)
        {
            Try(() =>
                SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, quality.AsHint()),
                "SDL_SetHint");
            var texture = SDL_CreateTextureFromSurface(Handle, surface.Handle);

            if (texture == IntPtr.Zero)
            {
                throw new ImaginiException($"Could not create texture: {SDL_GetError()}");
            }
            return(new Texture(texture, this));
        }
 public GraphicsTextRenderer(Graphics graphics, SpriteFont font,
                             TextureScalingQuality quality = TextureScalingQuality.Linear)
 {
     Font      = font;
     _graphics = graphics;
     foreach (var page in font.Pages)
     {
         var texture = TextureFactory.FromImage(_graphics, quality,
                                                page.Texture);
         texture.BlendMode = BlendMode.AlphaBlend;
         page.Texture.Dispose(); // dispose and unset the original image
         page.Texture = null;
         _pages.Add(texture);
     }
 }
Beispiel #4
0
        /// <summary>
        /// Creates a texture with the specified dimensions, format and access
        /// type.
        /// </summary>
        /// <param name="w">Width in pixels</param>
        /// <param name="h">Height in pixels</param>
        /// <param name="quality">Filtering quality when texture is scaled</param>
        /// <param name="format">Pixel format</param>
        /// <param name="access">Texture access type</param>
        public Texture CreateTexture(int w, int h,
                                     TextureScalingQuality quality = TextureScalingQuality.Nearest,
                                     PixelFormat format            = PixelFormat.Format_ARGB8888,
                                     TextureAccess access          = TextureAccess.Static)
        {
            Try(() =>
                SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, quality.AsHint()),
                "SDL_SetHint");
            var texture = SDL_CreateTexture(Handle, (uint)format, (int)access, w, h);

            if (texture == IntPtr.Zero)
            {
                throw new ImaginiException($"Could not create texture: {SDL_GetError()}");
            }
            return(new Texture(texture, this));
        }
Beispiel #5
0
        /// <summary>
        /// Creates a RGBA8888 surface from the specified Image object.
        /// </summary>
        public static Texture FromImage <TPixel>(Graphics graphics,
                                                 TextureScalingQuality quality, Image <TPixel> image)
            where TPixel : struct, IPixel <TPixel>
        {
            var cloned = image.CloneAs <Rgba32>();
            var pixels = cloned.GetPixelSpan();
            var bytes  = ArrayPool.Rent(pixels.Length * 4);

            for (int i = 0; i < pixels.Length; i++)
            {
                var pixel = pixels[i];
                bytes[i * 4]     = pixel.A;
                bytes[i * 4 + 1] = pixel.B;
                bytes[i * 4 + 2] = pixel.G;
                bytes[i * 4 + 3] = pixel.R;
            }
            var texture = graphics.CreateTexture(cloned.Width, cloned.Height,
                                                 quality, PixelFormat.Format_RGBA8888);

            texture.SetPixels(ref bytes);
            ArrayPool.Return(bytes);
            return(texture);
        }
Beispiel #6
0
 /// <summary>
 /// Creates a RGBA8888 surface by loading a file from the specified stream.
 /// </summary>
 public static Texture FromStream(Graphics graphics, Stream stream,
                                  TextureScalingQuality quality,
                                  IImageDecoder decoder) =>
 FromImage(graphics, quality, Image.Load(stream, decoder));
Beispiel #7
0
 /// <summary>
 /// Creates a RGBA8888 surface by loading a file from the specified path.
 /// </summary>
 public static Texture FromFile(Graphics graphics, TextureScalingQuality quality,
                                string path) =>
 FromImage(graphics, quality, Image.Load(path));
Beispiel #8
0
 /// <summary>
 /// Creates a text renderer for the specified font.
 /// </summary>
 public static ITextRenderer CreateTextRenderer(
     this Graphics graphics, SpriteFont font,
     TextureScalingQuality quality = TextureScalingQuality.Linear) =>
 new GraphicsTextRenderer(graphics, font, quality);