SDL窗口是透明的

时间:2018-09-06 14:32:42

标签: c sdl graphical-programming

我正在学习SDL,并尝试设置一个红色背景的窗口。 遵循了code,但是得到的结果与预期的结果不同。

    using (SKMemoryStream sourceStream = new SKMemoryStream(imageData))
    {
        using (SKCodec codec = SKCodec.Create(sourceStream))
        {
            sourceStream.Seek(0);

            using (SKImage image = SKImage.FromEncodedData(SKData.Create(sourceStream)))
            {
                int newHeight = image.Height;
                int newWidth = image.Width;

                if (maxHeight > 0 && newHeight > maxHeight)
                {
                    double scale = (double)maxHeight / newHeight;
                    newHeight = maxHeight;
                    newWidth = (int)Math.Floor(newWidth * scale);
                }

                if (maxWidth > 0 && newWidth > maxWidth)
                {
                    double scale = (double)maxWidth / newWidth;
                    newWidth = maxWidth;
                    newHeight = (int)Math.Floor(newHeight * scale);
                }

                var info = codec.Info.ColorSpace.IsSrgb ? new SKImageInfo(newWidth, newHeight) : new SKImageInfo(newWidth, newHeight, SKImageInfo.PlatformColorType, SKAlphaType.Premul, SKColorSpace.CreateSrgb());
                using (SKSurface surface = SKSurface.Create(info))
                {
                    using (SKPaint paint = new SKPaint())
                    {
                        // High quality without antialiasing
                        paint.IsAntialias = true;
                        paint.FilterQuality = SKFilterQuality.High;

                        // Draw the bitmap to fill the surface
                        surface.Canvas.Clear(SKColors.White);
                        var rect = new SKRect(0, 0, newWidth, newHeight);
                        surface.Canvas.DrawImage(image, rect, paint);
                        surface.Canvas.Flush();

                        using (SKImage newImage = surface.Snapshot())
                        {
                            using (SKData newImageData = newImage.Encode(convertToJpeg ? SKEncodedImageFormat.Jpeg : (codec.EncodedFormat == SKEncodedImageFormat.Gif ? SKEncodedImageFormat.Png : codec.EncodedFormat), 
                                codec.EncodedFormat == SKEncodedImageFormat.Gif || codec.EncodedFormat == SKEncodedImageFormat.Png || !downsampleJpeg ? 100 : 85))
                            {
                                return newImageData.ToArray();
                            }
                        }
                    }
                }
            }
        }
    }

a Transparent window

1 个答案:

答案 0 :(得分:1)

通过SDL_WaitEvent() / SDL_PollEvent() / SDL_PumpEvents()循环而不是大的'ole主线程阻塞SDL_Delay()来处理OS事件队列。