| | 1 | | using System.Collections.Generic; |
| | 2 | |
|
| | 3 | | using static SDL2.SDL_error; |
| | 4 | | using static SDL2.SDL_pixels; |
| | 5 | | using static SDL2.SDL_render; |
| | 6 | |
|
| | 7 | | namespace Imagini.Drawing |
| | 8 | | { |
| | 9 | | public sealed class RendererInfo |
| | 10 | | { |
| 66 | 11 | | internal int Index { get; private set; } |
| | 12 | |
|
| 1 | 13 | | private static List<RendererInfo> s_renderers = |
| 1 | 14 | | new List<RendererInfo>(); |
| | 15 | |
|
| 3 | 16 | | public string Name { get; private set; } |
| 3 | 17 | | public IReadOnlyCollection<PixelFormat> PixelFormats { get; private set; } |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Indicates whether this renderer is hardware accelerated. |
| | 21 | | /// </summary> |
| 72 | 22 | | public bool IsHardwareAccelerated { get; private set; } |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Indicates whether this is a software renderer. |
| | 26 | | /// </summary> |
| 0 | 27 | | public bool IsSoftware => !IsHardwareAccelerated; |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Indicates whether this renderer supports rendering to texture. |
| | 31 | | /// </summary> |
| 66 | 32 | | public bool SupportsRenderingToTexture { get; private set; } |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// Indicates whether this renderer supports refresh rate synchronization. |
| | 36 | | /// </summary> |
| 3 | 37 | | public bool SupportsVSync { get; private set; } |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Returns the maximum supported texture width. |
| | 41 | | /// </summary> |
| 3 | 42 | | public int MaxTextureWidth { get; private set; } |
| | 43 | | /// <summary> |
| | 44 | | /// Returns the maximum supported texture height. |
| | 45 | | /// </summary> |
| 3 | 46 | | public int MaxTextureHeight { get; private set; } |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Returns the all available renderers. |
| | 50 | | /// </summary> |
| 66 | 51 | | public static IReadOnlyCollection<RendererInfo> All => s_renderers; |
| | 52 | |
|
| 3 | 53 | | private RendererInfo(int index, SDL_RendererInfo info) |
| | 54 | | { |
| 3 | 55 | | Index = index; |
| 3 | 56 | | Name = Util.FromNullTerminated(info.name); |
| 3 | 57 | | MaxTextureWidth = info.max_texture_width; |
| 3 | 58 | | MaxTextureHeight = info.max_texture_height; |
| 3 | 59 | | var flags = info.flags; |
| 3 | 60 | | IsHardwareAccelerated = flags.HasFlag(SDL_RendererFlags.SDL_RENDERER_ACCELERATED); |
| 3 | 61 | | SupportsRenderingToTexture = flags.HasFlag(SDL_RendererFlags.SDL_RENDERER_TARGETTEXTURE); |
| 3 | 62 | | SupportsVSync = flags.HasFlag(SDL_RendererFlags.SDL_RENDERER_PRESENTVSYNC); |
| | 63 | |
|
| 3 | 64 | | var formats = new List<PixelFormat>((int)info.num_texture_formats); |
| 32 | 65 | | for (int i = 0; i < info.num_texture_formats; i++) |
| | 66 | | { |
| | 67 | | unsafe { |
| 13 | 68 | | var format = *(info.texture_formats + i); |
| 13 | 69 | | formats.Add((PixelFormat)format); |
| | 70 | | } |
| | 71 | | } |
| 3 | 72 | | PixelFormats = formats; |
| 3 | 73 | | } |
| | 74 | |
|
| | 75 | | static RendererInfo() |
| | 76 | | { |
| 1 | 77 | | Lifecycle.TryInitialize(); |
| 1 | 78 | | int numRenderers = SDL_GetNumRenderDrivers(); |
| 1 | 79 | | if (numRenderers < 0) |
| 0 | 80 | | throw new ImaginiException($"Could not obtain renderer info: {SDL_GetError()}"); |
| 8 | 81 | | for (int i = 0; i < numRenderers; i++) |
| | 82 | | { |
| 3 | 83 | | SDL_GetRenderDriverInfo(i, out SDL_RendererInfo info); |
| 3 | 84 | | s_renderers.Add(new RendererInfo(i, info)); |
| | 85 | | } |
| 1 | 86 | | } |
| | 87 | | } |
| | 88 | | } |