Browse Source

Now using SdlInit to manage subsystem initialization/destruction; added a Canvas object to handle graphics.

improved_timing
Ian Burgmyer 7 years ago
parent
commit
1b9551d736
  1. 43
      DotSDL/Graphics/Canvas.cs
  2. 15
      DotSDL/Graphics/SdlWindow.cs
  3. 5
      DotSDL/SdlInit.cs
  4. 16
      Samples/Sample.BasicPixels/Window.cs

43
DotSDL/Graphics/Canvas.cs

@ -0,0 +1,43 @@
namespace DotSDL.Graphics {
/// <summary>
/// A representation of the contents of the SDL window, with a number of
/// helper routines.
/// </summary>
public class Canvas {
/// <summary>
/// The raw pixels in the <see cref="Canvas"/>.
/// </summary>
public Color[] Pixels;
/// <summary>
/// Gets the width of the <see cref="Canvas"/>.
/// </summary>
public int Width { get; private set; }
/// <summary>
/// Gets the height of the <see cref="Canvas"/>.
/// </summary>
public int Height { get; private set; }
/// <summary>
/// Initialization a new <see cref="Canvas"/>.
/// </summary>
/// <param name="width">The width of the <see cref="Canvas"/>.</param>
/// <param name="height">The height of the <see cref="Canvas"/>.</param>
internal Canvas(int width, int height) {
SetSize(width, height);
}
/// <summary>
/// Resizes the <see cref="Canvas"/>.
/// </summary>
/// <param name="width">The new width of the <see cref="Canvas"/>.</param>
/// <param name="height">The new height of the <see cref="Canvas"/>.</param>
internal void SetSize(int width, int height) {
Width = width;
Height = height;
Pixels = new Color[Width * Height];
}
}
}

15
DotSDL/Graphics/SdlWindow.cs

@ -6,6 +6,8 @@ namespace DotSDL.Graphics {
/// Represents an SDL window.
/// </summary>
public class SdlWindow {
private readonly SdlInit sdlInit = SdlInit.Instance;
private readonly IntPtr _window;
private readonly IntPtr _renderer;
private readonly IntPtr _texture;
@ -15,7 +17,7 @@ namespace DotSDL.Graphics {
public int Width { get; }
public int Height { get; }
private Color[] _pixels;
private Canvas _canvas;
/// <summary>Indicates that the window manager should position the window.</summary>
public const int WindowPosUndefined = 0x1FFF0000;
@ -30,21 +32,22 @@ namespace DotSDL.Graphics {
}
public SdlWindow(string name, Point position, int width, int height) {
sdlInit.InitSubsystem(Init.SubsystemFlags.Video);
_window = Video.CreateWindow(name, position.X, position.Y, width, height, Video.WindowFlags.Hidden);
_renderer = Render.CreateRenderer(_window, -1, Render.RendererFlags.Accelerated);
_texture = Render.CreateTexture(_renderer, Pixels.PixelFormatArgb8888, Render.TextureAccess.Streaming, width, height);
// TODO: Support other pixel formats.
_pixels = new Color[width * height];
_canvas = new Canvas(width, height);
Width = width;
Height = height;
}
private unsafe void BaseDraw() {
OnDraw(ref _pixels); // Call the overridden Draw function.
OnDraw(ref _canvas); // Call the overridden Draw function.
fixed(void* pixelsPtr = _pixels) {
fixed(void* pixelsPtr = _canvas.Pixels) {
var ptr = (IntPtr)pixelsPtr;
Render.UpdateTexture(_texture, IntPtr.Zero, ptr, Width * 4);
}
@ -73,7 +76,7 @@ namespace DotSDL.Graphics {
/// <summary>
/// Fired every time the window is drawn to.
/// </summary>
protected virtual void OnDraw(ref Color[] pixels) {}
protected virtual void OnDraw(ref Canvas canvas) {}
/// <summary>
/// Fired before the window is shown.

5
DotSDL/SdlInit.cs

@ -10,6 +10,11 @@ namespace DotSDL {
private static readonly Lazy<SdlInit> Singleton = new Lazy<SdlInit>(() => new SdlInit());
internal static SdlInit Instance => Singleton.Value;
// Subsystem initialization is ref counted in SDL2, and there really
// isn't much point in shutting down a specific subsystem after it's
// been started. We'll use a stack to ensure that everything is taken
// down in the order that it's been brought up and that QuitSubsystem
// has been called the appropriate number of times.
private readonly Stack<Init.SubsystemFlags> _subsystems = new Stack<Init.SubsystemFlags>();
private SdlInit() {

16
Samples/Sample.BasicPixels/Window.cs

@ -87,13 +87,13 @@ namespace DotSDL.Sample.BasicPixels {
return (Width * y) + x;
}
protected override void OnDraw(ref Color[] pixels) {
protected override void OnDraw(ref Canvas canvas) {
const byte min = 128, max = 255;
const int offsetX = 96, offsetY = 80;
DrawBackground(ref pixels);
DrawBackground(ref canvas.Pixels);
// D
DrawSequence(ref pixels, RandomColor(min, max),
DrawSequence(ref canvas.Pixels, RandomColor(min, max),
new Line { Start = new Point { X = offsetX, Y = offsetY + 96 }, End = new Point { X = offsetX, Y = offsetY } },
new Line { Start = new Point { X = offsetX, Y = offsetY }, End = new Point { X = offsetX + 32, Y = offsetY } },
new Line { Start = new Point { X = offsetX + 32, Y = offsetY }, End = new Point { X = offsetX + 48, Y = offsetY + 16 } },
@ -102,7 +102,7 @@ namespace DotSDL.Sample.BasicPixels {
new Line { Start = new Point { X = offsetX + 32, Y = offsetY + 96 }, End = new Point { X = offsetX, Y = offsetY + 96 } }
);
// o
DrawSequence(ref pixels, RandomColor(min, max),
DrawSequence(ref canvas.Pixels, RandomColor(min, max),
new Line { Start = new Point { X = offsetX + 64, Y = offsetY + 88}, End = new Point { X = offsetX + 64, Y = offsetY + 56 } },
new Line { Start = new Point { X = offsetX + 64, Y = offsetY + 56}, End = new Point { X = offsetX + 72, Y = offsetY + 48 } },
new Line { Start = new Point { X = offsetX + 72, Y = offsetY + 48}, End = new Point { X = offsetX + 80, Y = offsetY + 48 } },
@ -113,13 +113,13 @@ namespace DotSDL.Sample.BasicPixels {
new Line { Start = new Point { X = offsetX + 72, Y = offsetY + 96}, End = new Point { X = offsetX + 64, Y = offsetY + 88 } }
);
// t
DrawSequence(ref pixels, RandomColor(min, max),
DrawSequence(ref canvas.Pixels, RandomColor(min, max),
new Line { Start = new Point { X = offsetX + 104 + 12, Y = offsetY + 24}, End = new Point { X = offsetX + 104 + 12, Y = offsetY + 96 } },
new Line { Start = new Point { X = offsetX + 104, Y = offsetY + 48}, End = new Point { X = offsetX + 104 + 24, Y = offsetY + 48 } }
);
// S
DrawSequence(ref pixels, RandomColor(min, max),
DrawSequence(ref canvas.Pixels, RandomColor(min, max),
new Line { Start = new Point { X = offsetX + 144 + 48, Y = offsetY }, End = new Point { X = offsetX + 144, Y = offsetY } },
new Line { Start = new Point { X = offsetX + 144, Y = offsetY }, End = new Point { X = offsetX + 144, Y = offsetY + 48 } },
new Line { Start = new Point { X = offsetX + 144, Y = offsetY + 48 }, End = new Point { X = offsetX + 144 + 48 , Y = offsetY + 48 } },
@ -127,7 +127,7 @@ namespace DotSDL.Sample.BasicPixels {
new Line { Start = new Point { X = offsetX + 144 + 48, Y = offsetY + 96 }, End = new Point { X = offsetX + 144, Y = offsetY + 96 } }
);
// D
DrawSequence(ref pixels, RandomColor(min, max),
DrawSequence(ref canvas.Pixels, RandomColor(min, max),
new Line { Start = new Point { X = offsetX + 208, Y = offsetY + 96 }, End = new Point { X = offsetX + 208, Y = offsetY } },
new Line { Start = new Point { X = offsetX + 208, Y = offsetY }, End = new Point { X = offsetX + 208 + 32, Y = offsetY } },
new Line { Start = new Point { X = offsetX + 208 + 32, Y = offsetY }, End = new Point { X = offsetX + 208 + 48, Y = offsetY + 16 } },
@ -136,7 +136,7 @@ namespace DotSDL.Sample.BasicPixels {
new Line { Start = new Point { X = offsetX + 208 + 32, Y = offsetY + 96 }, End = new Point { X = offsetX + 208, Y = offsetY + 96 } }
);
// L
DrawSequence(ref pixels, RandomColor(min, max),
DrawSequence(ref canvas.Pixels, RandomColor(min, max),
new Line { Start = new Point { X = offsetX + 272, Y = offsetY }, End = new Point { X = offsetX + 272, Y = offsetY + 96} },
new Line { Start = new Point { X = offsetX + 272, Y = offsetY + 96 }, End = new Point { X = offsetX + 272 + 48, Y = offsetY + 96} }
);

Loading…
Cancel
Save