| | 1 | | using System; |
| | 2 | | using Imagini.Drawing; |
| | 3 | | using static SDL2.SDL_error; |
| | 4 | | using static SDL2.SDL_render; |
| | 5 | | using static SDL2.SDL_video; |
| | 6 | |
|
| | 7 | | namespace Imagini |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Main class which instantiates a window, an event loop and a |
| | 11 | | /// 2D accelerated renderer. |
| | 12 | | /// </summary> |
| | 13 | | public abstract class App2D : AppBase |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// Provides access to hardware accelerated drawing functions for the app's window. |
| | 17 | | /// If software renderer is used, returns null. |
| | 18 | | /// </summary> |
| 414 | 19 | | public Graphics Graphics { get; private set; } |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Returns the window surface used for software rendering. |
| | 23 | | /// If a hardware renderer is used, returns null. |
| | 24 | | /// </summary> |
| 68 | 25 | | public Surface Surface { get; private set; } |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Gets or sets if this app uses hardware acceleration. |
| | 29 | | /// </summary> |
| | 30 | | /// <returns></returns> |
| 66 | 31 | | public bool IsHardwareAccelerated { get; private set; } |
| | 32 | |
|
| | 33 | | private readonly bool _useSurfaceApi = false; |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// Creates a new app with the specified window settings. |
| | 37 | | /// </summary> |
| | 38 | | /// <remarks> |
| | 39 | | /// If you have your own constructor, make sure to call this |
| | 40 | | /// one because it initializes the window and the event queue. |
| | 41 | | /// </remarks> |
| | 42 | | /// <param name="driver">Specifies a renderer to be used. If null, first hardware-accelerated renderer is used.< |
| | 43 | | /// <param name="useSurfaceApi">If true, initializes <see cref="Surface" /> instead of <see cref="Graphics" /></ |
| | 44 | | public App2D(WindowSettings settings = null, RendererInfo driver = null, |
| 64 | 45 | | bool useSurfaceApi = false) : base(settings) |
| | 46 | | { |
| 64 | 47 | | _useSurfaceApi = useSurfaceApi; |
| 64 | 48 | | IsHardwareAccelerated = driver?.IsHardwareAccelerated ?? true; |
| 64 | 49 | | if (!useSurfaceApi) |
| 63 | 50 | | Graphics = new Graphics(Window, driver); |
| | 51 | | else |
| | 52 | | { |
| 2 | 53 | | Resized += (s, e) => UpdateSurface(); |
| 1 | 54 | | UpdateSurface(); |
| | 55 | | } |
| 1 | 56 | | } |
| | 57 | |
|
| | 58 | | private void UpdateSurface() |
| | 59 | | { |
| 2 | 60 | | var handle = SDL_GetWindowSurface(Window.Handle); |
| 2 | 61 | | if (handle == IntPtr.Zero) |
| 0 | 62 | | throw new ImaginiException($"Could not obtain window surface: {SDL_GetError()}"); |
| 2 | 63 | | Surface = new Surface(handle); |
| 2 | 64 | | } |
| | 65 | |
|
| | 66 | | protected override void AfterDraw(TimeSpan frameTime) |
| | 67 | | { |
| 83 | 68 | | if (!_useSurfaceApi) |
| 82 | 69 | | SDL_RenderPresent(Graphics.Handle); |
| | 70 | | else |
| 1 | 71 | | SDL_UpdateWindowSurface(Window.Handle); |
| 1 | 72 | | } |
| | 73 | |
|
| | 74 | | protected override void OnDispose() |
| | 75 | | { |
| 64 | 76 | | Graphics?.Destroy(); |
| 64 | 77 | | Surface?.Dispose(); |
| 1 | 78 | | } |
| | 79 | | } |
| | 80 | |
|
| | 81 | | } |