diff --git a/src/SFML.Audio/Music.cs b/src/SFML.Audio/Music.cs index 3636f7c2..1772d8c1 100644 --- a/src/SFML.Audio/Music.cs +++ b/src/SFML.Audio/Music.cs @@ -15,7 +15,13 @@ public class Music : ObjectBase { //////////////////////////////////////////////////////////// /// - /// Constructs a music from an audio file + /// Constructs a music from a file + /// + /// This constructor doesn't start playing the music (call + /// to do so). + /// Here is a complete list of all the supported audio formats: + /// ogg, wav, flac, mp3, aiff, au, raw, paf, svx, nist, voc, ircam, + /// w64, mat4, mat5 pvf, htk, sds, avr, sd2, caf, wve, mpc2k, rf64. /// /// Path of the music file to open //////////////////////////////////////////////////////////// @@ -31,6 +37,12 @@ public class Music : ObjectBase //////////////////////////////////////////////////////////// /// /// Constructs a music from a custom stream + /// + /// This constructor doesn't start playing the music (call + /// to do so). + /// Here is a complete list of all the supported audio formats: + /// ogg, wav, flac, mp3, aiff, au, raw, paf, svx, nist, voc, ircam, + /// w64, mat4, mat5 pvf, htk, sds, avr, sd2, caf, wve, mpc2k, rf64. /// /// Source stream to read from //////////////////////////////////////////////////////////// @@ -49,6 +61,12 @@ public class Music : ObjectBase //////////////////////////////////////////////////////////// /// /// Constructs a music from an audio file in memory + /// + /// This constructor doesn't start playing the music (call + /// to do so). + /// Here is a complete list of all the supported audio formats: + /// ogg, wav, flac, mp3, aiff, au, raw, paf, svx, nist, voc, ircam, + /// w64, mat4, mat5 pvf, htk, sds, avr, sd2, caf, wve, mpc2k, rf64. /// /// Byte array containing the file contents /// diff --git a/src/SFML.Audio/SoundBuffer.cs b/src/SFML.Audio/SoundBuffer.cs index 4e743172..c7c7097a 100644 --- a/src/SFML.Audio/SoundBuffer.cs +++ b/src/SFML.Audio/SoundBuffer.cs @@ -18,7 +18,7 @@ public class SoundBuffer : ObjectBase /// Construct a sound buffer from a file /// /// Here is a complete list of all the supported audio formats: - /// ogg, wav, flac, aiff, au, raw, paf, svx, nist, voc, ircam, + /// ogg, wav, flac, mp3, aiff, au, raw, paf, svx, nist, voc, ircam, /// w64, mat4, mat5 pvf, htk, sds, avr, sd2, caf, wve, mpc2k, rf64. /// /// Path of the sound file to load @@ -38,7 +38,7 @@ public class SoundBuffer : ObjectBase /// Construct a sound buffer from a custom stream. /// /// Here is a complete list of all the supported audio formats: - /// ogg, wav, flac, aiff, au, raw, paf, svx, nist, voc, ircam, + /// ogg, wav, flac, mp3, aiff, au, raw, paf, svx, nist, voc, ircam, /// w64, mat4, mat5 pvf, htk, sds, avr, sd2, caf, wve, mpc2k, rf64. /// /// Source stream to read from @@ -63,7 +63,7 @@ public class SoundBuffer : ObjectBase /// Construct a sound buffer from a file in memory. /// /// Here is a complete list of all the supported audio formats: - /// ogg, wav, flac, aiff, au, raw, paf, svx, nist, voc, ircam, + /// ogg, wav, flac, mp3, aiff, au, raw, paf, svx, nist, voc, ircam, /// w64, mat4, mat5 pvf, htk, sds, avr, sd2, caf, wve, mpc2k, rf64. /// /// Byte array containing the file contents @@ -129,7 +129,7 @@ public class SoundBuffer : ObjectBase /// Save the sound buffer to an audio file. /// /// Here is a complete list of all the supported audio formats: - /// ogg, wav, flac, aiff, au, raw, paf, svx, nist, voc, ircam, + /// ogg, wav, flac, mp3, aiff, au, raw, paf, svx, nist, voc, ircam, /// w64, mat4, mat5 pvf, htk, sds, avr, sd2, caf, wve, mpc2k, rf64. /// /// Path of the sound file to write diff --git a/src/SFML.Graphics/BlendMode.cs b/src/SFML.Graphics/BlendMode.cs index d606b993..651569f1 100644 --- a/src/SFML.Graphics/BlendMode.cs +++ b/src/SFML.Graphics/BlendMode.cs @@ -63,7 +63,13 @@ public enum Equation Subtract, /// Pixel = Dst * DstFactor - Src * SrcFactor - ReverseSubtract + ReverseSubtract, + + /// Pixel = min(Dst, Src) + Min, + + /// Pixel = max(Dst, Src) + Max } /// Blend source and dest according to dest alpha @@ -77,10 +83,15 @@ public enum Equation /// Multiply source and dest public static readonly BlendMode Multiply = new BlendMode(Factor.DstColor, Factor.Zero); + /// Take minimum between source and dest + public static readonly BlendMode Min = new BlendMode(Factor.One, Factor.One, Equation.Min); + + /// Take maximum between source and dest + public static readonly BlendMode Max = new BlendMode(Factor.One, Factor.One, Equation.Max); + /// Overwrite dest with source public static readonly BlendMode None = new BlendMode(Factor.One, Factor.Zero); - //////////////////////////////////////////////////////////// /// /// Construct the blend mode given the factors and equation diff --git a/src/SFML.Graphics/Font.cs b/src/SFML.Graphics/Font.cs index 36112348..d07cdb28 100644 --- a/src/SFML.Graphics/Font.cs +++ b/src/SFML.Graphics/Font.cs @@ -81,15 +81,28 @@ public Font(Stream stream) : base(IntPtr.Zero) //////////////////////////////////////////////////////////// /// - /// Get the kerning offset between two glyphs + /// Get the kerning value corresponding to a given pair of + /// characters in a font /// /// Unicode code point of the first character /// Unicode code point of the second character - /// Character size + /// Character size, in pixels /// Kerning offset, in pixels //////////////////////////////////////////////////////////// public float GetKerning(uint first, uint second, uint characterSize) => sfFont_getKerning(CPointer, first, second, characterSize); + //////////////////////////////////////////////////////////// + /// + /// Get the bold kerning value corresponding to a given pair + /// of characters in a font + /// + /// Unicode code point of the first character + /// Unicode code point of the second character + /// Character size, in pixels + /// Kerning offset, in pixels + //////////////////////////////////////////////////////////// + public float GetBoldKerning(uint first, uint second, uint characterSize) => sfFont_getBoldKerning(CPointer, first, second, characterSize); + //////////////////////////////////////////////////////////// /// /// Get spacing between two consecutive lines @@ -130,6 +143,28 @@ public Texture GetTexture(uint characterSize) return myTextures[characterSize]; } + //////////////////////////////////////////////////////////// + /// + /// Enable or disable the smooth filter + /// + /// When the filter is activated, the font appears smoother + /// so that pixels are less noticeable. However if you want + /// the font to look exactly the same as its source file, + /// you should disable it. + /// The smooth filter is enabled by default. + /// + /// True to enable smoothing, false to disable it + //////////////////////////////////////////////////////////// + public void SetSmooth(bool smooth) => sfFont_setSmooth(CPointer, smooth); + + //////////////////////////////////////////////////////////// + /// + /// Tell whether the smooth filter is enabled or disabled + /// + /// True if smoothing is enabled, false if it is disabled + //////////////////////////////////////////////////////////// + public bool IsSmooth() => sfFont_isSmooth(CPointer); + //////////////////////////////////////////////////////////// /// /// Get the font information @@ -244,6 +279,9 @@ internal struct InfoMarshalData [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern float sfFont_getKerning(IntPtr CPointer, uint first, uint second, uint characterSize); + [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern float sfFont_getBoldKerning(IntPtr CPointer, uint first, uint second, uint characterSize); + [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern float sfFont_getLineSpacing(IntPtr CPointer, uint characterSize); @@ -256,6 +294,12 @@ internal struct InfoMarshalData [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern IntPtr sfFont_getTexture(IntPtr CPointer, uint characterSize); + [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern void sfFont_setSmooth(IntPtr CPointer, bool smooth); + + [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern bool sfFont_isSmooth(IntPtr CPointer); + [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern InfoMarshalData sfFont_getInfo(IntPtr CPointer); #endregion diff --git a/src/SFML.Graphics/Image.cs b/src/SFML.Graphics/Image.cs index 155bf6a9..3d8c672e 100644 --- a/src/SFML.Graphics/Image.cs +++ b/src/SFML.Graphics/Image.cs @@ -185,6 +185,30 @@ public Image(string filename) : base(sfImage_createFromFile(filename)) //////////////////////////////////////////////////////////// public bool SaveToFile(string filename) => sfImage_saveToFile(CPointer, filename); + //////////////////////////////////////////////////////////// + /// + /// Save the image to a buffer in memory + /// + /// The format of the image must be specified. + /// The supported image formats are bmp, png, tga and jpg. + /// This function fails if the image is empty, or if + /// the format was invalid. + /// + /// Byte array filled with encoded data + /// Encoding format to use + /// True if saving was successful + //////////////////////////////////////////////////////////// + public bool SaveToMemory(out byte[] output, string format) + { + using (SFML.System.Buffer buffer = new SFML.System.Buffer()) + { + var success = sfImage_saveToMemory(CPointer, buffer.CPointer, format); + + output = success ? buffer.GetData() : Array.Empty(); + return success; + } + } + //////////////////////////////////////////////////////////// /// /// Create a transparency mask from a specified colorkey @@ -376,6 +400,9 @@ protected override void Destroy(bool disposing) [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern bool sfImage_saveToFile(IntPtr CPointer, string Filename); + [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern bool sfImage_saveToMemory(IntPtr CPointer, IntPtr bufferOutput, string format); + [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern void sfImage_createMaskFromColor(IntPtr CPointer, Color Col, byte Alpha); diff --git a/src/SFML.Graphics/Rect.cs b/src/SFML.Graphics/Rect.cs index ae508b3e..4eff6ffe 100644 --- a/src/SFML.Graphics/Rect.cs +++ b/src/SFML.Graphics/Rect.cs @@ -122,6 +122,22 @@ public bool Intersects(IntRect rect, out IntRect overlap) } } + //////////////////////////////////////////////////////////// + /// + /// Get the position of the rectangle's top-left corner + /// + /// Position of rectangle + //////////////////////////////////////////////////////////// + public Vector2i Position => new Vector2i(Left, Top); + + //////////////////////////////////////////////////////////// + /// + /// Get the size of the rectangle + /// + /// Size of rectangle + //////////////////////////////////////////////////////////// + public Vector2i Size => new Vector2i(Width, Height); + //////////////////////////////////////////////////////////// /// /// Provide a string describing the object @@ -337,6 +353,22 @@ public bool Intersects(FloatRect rect, out FloatRect overlap) } } + //////////////////////////////////////////////////////////// + /// + /// Get the position of the rectangle's top-left corner + /// + /// Position of rectangle + //////////////////////////////////////////////////////////// + public Vector2f Position => new Vector2f(Left, Top); + + //////////////////////////////////////////////////////////// + /// + /// Get the size of the rectangle + /// + /// Size of rectangle + //////////////////////////////////////////////////////////// + public Vector2f Size => new Vector2f(Width, Height); + //////////////////////////////////////////////////////////// /// /// Provide a string describing the object diff --git a/src/SFML.Graphics/RenderTexture.cs b/src/SFML.Graphics/RenderTexture.cs index 5dac187a..77ae3a1c 100644 --- a/src/SFML.Graphics/RenderTexture.cs +++ b/src/SFML.Graphics/RenderTexture.cs @@ -101,6 +101,16 @@ public Vector2u Size get { return sfRenderTexture_getSize(CPointer); } } + //////////////////////////////////////////////////////////// + /// + /// Tell if the render texture will use sRGB encoding when drawing on it + /// + //////////////////////////////////////////////////////////// + public bool IsSrgb + { + get { return sfRenderTexture_isSrgb(CPointer); } + } + //////////////////////////////////////////////////////////// /// /// Default view of the render texture @@ -540,6 +550,9 @@ protected override void Destroy(bool disposing) [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern Vector2u sfRenderTexture_getSize(IntPtr CPointer); + [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern bool sfRenderTexture_isSrgb(IntPtr CPointer); + [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern bool sfRenderTexture_setActive(IntPtr CPointer, bool Active); diff --git a/src/SFML.Graphics/RenderWindow.cs b/src/SFML.Graphics/RenderWindow.cs index af772148..3ee0bbf1 100644 --- a/src/SFML.Graphics/RenderWindow.cs +++ b/src/SFML.Graphics/RenderWindow.cs @@ -146,6 +146,16 @@ public override Vector2u Size set { sfRenderWindow_setSize(CPointer, value); } } + //////////////////////////////////////////////////////////// + /// + /// Tell if the render window will use sRGB encoding when drawing on it + /// + //////////////////////////////////////////////////////////// + public bool IsSrgb + { + get { return sfRenderWindow_isSrgb(CPointer); } + } + //////////////////////////////////////////////////////////// /// /// Change the title of the window @@ -830,6 +840,9 @@ private void Initialize() [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern Vector2u sfRenderWindow_getSize(IntPtr CPointer); + [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern bool sfRenderWindow_isSrgb(IntPtr CPointer); + [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern void sfRenderWindow_setSize(IntPtr CPointer, Vector2u size); diff --git a/src/SFML.Graphics/Texture.cs b/src/SFML.Graphics/Texture.cs index 9c47eca0..acd42d2b 100644 --- a/src/SFML.Graphics/Texture.cs +++ b/src/SFML.Graphics/Texture.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Runtime.InteropServices; +using System.Runtime.InteropServices.ComTypes; using System.Security; using SFML.System; using SFML.Window; @@ -10,10 +11,33 @@ namespace SFML.Graphics //////////////////////////////////////////////////////////// /// /// Image living on the graphics card that can be used for drawing + /// + /// When providing texture data from an image file or memory, it can + /// either be stored in a linear color space or an sRGB color space. + /// Most digital images account for gamma correction already, so they + /// would need to be "uncorrected" back to linear color space before + /// being processed by the hardware. The hardware can automatically + /// convert it from the sRGB color space to a linear color space when + /// it gets sampled. When the rendered image gets output to the final + /// framebuffer, it gets converted back to sRGB. + /// + /// This load option is only useful in conjunction with an sRGB capable + /// framebuffer. This can be requested during window creation. /// //////////////////////////////////////////////////////////// public class Texture : ObjectBase { + /// + /// Types of texture coordinates that can be used for rendering. + /// + public enum TextureCoordinateType + { + /// Texture coordinates in range [0 .. 1]. + Normalized, + /// Texture coordinates in range [0 .. size]. + Pixels + } + //////////////////////////////////////////////////////////// /// /// Construct the texture @@ -39,10 +63,11 @@ public class Texture : ObjectBase /// Construct the texture from a file /// /// Path of the image file to load + /// True to convert the texture source from sRGB, false otherwise /// //////////////////////////////////////////////////////////// - public Texture(string filename) : - this(filename, new IntRect(0, 0, 0, 0)) + public Texture(string filename, bool srgb = false) : + this(filename, new IntRect(0, 0, 0, 0), srgb) { } @@ -52,11 +77,21 @@ public class Texture : ObjectBase /// /// Path of the image file to load /// Area of the image to load + /// True to convert the texture source from sRGB, false otherwise /// //////////////////////////////////////////////////////////// - public Texture(string filename, IntRect area) : - base(sfTexture_createFromFile(filename, ref area)) + public Texture(string filename, IntRect area, bool srgb = false) : + base(IntPtr.Zero) { + if (srgb) + { + CPointer = sfTexture_createSrgbFromFile(filename, ref area); + } + else + { + CPointer = sfTexture_createFromFile(filename, ref area); + } + if (CPointer == IntPtr.Zero) { throw new LoadingFailedException("texture", filename); @@ -68,10 +103,11 @@ public class Texture : ObjectBase /// Construct the texture from a file in a stream /// /// Stream containing the file contents + /// True to convert the texture source from sRGB, false otherwise /// //////////////////////////////////////////////////////////// - public Texture(Stream stream) : - this(stream, new IntRect(0, 0, 0, 0)) + public Texture(Stream stream, bool srgb = false) : + this(stream, new IntRect(0, 0, 0, 0), srgb) { } @@ -81,14 +117,22 @@ public class Texture : ObjectBase /// /// Stream containing the file contents /// Area of the image to load + /// True to convert the texture source from sRGB, false otherwise /// //////////////////////////////////////////////////////////// - public Texture(Stream stream, IntRect area) : + public Texture(Stream stream, IntRect area, bool srgb = false) : base(IntPtr.Zero) { using (StreamAdaptor adaptor = new StreamAdaptor(stream)) { - CPointer = sfTexture_createFromStream(adaptor.InputStreamPtr, ref area); + if (srgb) + { + CPointer = sfTexture_createSrgbFromStream(adaptor.InputStreamPtr, ref area); + } + else + { + CPointer = sfTexture_createFromStream(adaptor.InputStreamPtr, ref area); + } } if (CPointer == IntPtr.Zero) @@ -102,10 +146,11 @@ public class Texture : ObjectBase /// Construct the texture from an image /// /// Image to load to the texture + /// True to convert the texture source from sRGB, false otherwise /// //////////////////////////////////////////////////////////// - public Texture(Image image) : - this(image, new IntRect(0, 0, 0, 0)) + public Texture(Image image, bool srgb = false) : + this(image, new IntRect(0, 0, 0, 0), srgb) { } @@ -115,11 +160,21 @@ public class Texture : ObjectBase /// /// Image to load to the texture /// Area of the image to load + /// True to convert the texture source from sRGB, false otherwise /// //////////////////////////////////////////////////////////// - public Texture(Image image, IntRect area) : - base(sfTexture_createFromImage(image.CPointer, ref area)) + public Texture(Image image, IntRect area, bool srgb = false) : + base(IntPtr.Zero) { + if (srgb) + { + CPointer = sfTexture_createSrgbFromImage(image.CPointer, ref area); + } + else + { + CPointer = sfTexture_createFromImage(image.CPointer, ref area); + } + if (CPointer == IntPtr.Zero) { throw new LoadingFailedException("texture"); @@ -131,16 +186,25 @@ public class Texture : ObjectBase /// Construct the texture from a file in memory /// /// Byte array containing the file contents + /// True to convert the texture source from sRGB, false otherwise /// //////////////////////////////////////////////////////////// - public Texture(byte[] bytes) : + public Texture(byte[] bytes, bool srgb = false) : base(IntPtr.Zero) { GCHandle pin = GCHandle.Alloc(bytes, GCHandleType.Pinned); try { IntRect rect = new IntRect(0, 0, 0, 0); - CPointer = sfTexture_createFromMemory(pin.AddrOfPinnedObject(), Convert.ToUInt64(bytes.Length), ref rect); + + if (srgb) + { + CPointer = sfTexture_createSrgbFromMemory(pin.AddrOfPinnedObject(), Convert.ToUInt64(bytes.Length), ref rect); + } + else + { + CPointer = sfTexture_createFromMemory(pin.AddrOfPinnedObject(), Convert.ToUInt64(bytes.Length), ref rect); + } } finally { @@ -385,7 +449,6 @@ public bool Smooth public bool Srgb { get { return sfTexture_isSrgb(CPointer); } - set { sfTexture_setSrgb(CPointer, value); } } //////////////////////////////////////////////////////////// @@ -414,10 +477,11 @@ public Vector2u Size /// Bind a texture for rendering /// /// Shader to bind (can be null to use no texture) + /// Type of texture coordinates to use //////////////////////////////////////////////////////////// - public static void Bind(Texture texture) + public static void Bind(Texture texture, TextureCoordinateType type) { - sfTexture_bind(texture != null ? texture.CPointer : IntPtr.Zero); + sfTexture_bind(texture != null ? texture.CPointer : IntPtr.Zero, type); } //////////////////////////////////////////////////////////// @@ -489,15 +553,27 @@ protected override void Destroy(bool disposing) [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern IntPtr sfTexture_createFromFile(string filename, ref IntRect area); + [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern IntPtr sfTexture_createSrgbFromFile(string filename, ref IntRect area); + [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern IntPtr sfTexture_createFromStream(IntPtr stream, ref IntRect area); + [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern IntPtr sfTexture_createSrgbFromStream(IntPtr stream, ref IntRect area); + [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern IntPtr sfTexture_createFromImage(IntPtr image, ref IntRect area); + [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern IntPtr sfTexture_createSrgbFromImage(IntPtr image, ref IntRect area); + [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern IntPtr sfTexture_createFromMemory(IntPtr data, ulong size, ref IntRect area); + [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern IntPtr sfTexture_createSrgbFromMemory(IntPtr data, ulong size, ref IntRect area); + [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern IntPtr sfTexture_copy(IntPtr texture); @@ -526,7 +602,7 @@ protected override void Destroy(bool disposing) private static extern void sfTexture_updateFromRenderWindow(IntPtr texture, IntPtr renderWindow, uint x, uint y); [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] - private static extern void sfTexture_bind(IntPtr texture); + private static extern void sfTexture_bind(IntPtr texture, TextureCoordinateType type); [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern void sfTexture_setSmooth(IntPtr texture, bool smooth); @@ -534,9 +610,6 @@ protected override void Destroy(bool disposing) [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern bool sfTexture_isSmooth(IntPtr texture); - [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] - private static extern void sfTexture_setSrgb(IntPtr texture, bool sRgb); - [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern bool sfTexture_isSrgb(IntPtr texture); diff --git a/src/SFML.Graphics/VertexBuffer.cs b/src/SFML.Graphics/VertexBuffer.cs index b6cb2d08..f7d9a0b4 100644 --- a/src/SFML.Graphics/VertexBuffer.cs +++ b/src/SFML.Graphics/VertexBuffer.cs @@ -315,6 +315,29 @@ public void Draw(RenderTarget target, RenderStates states) } } + //////////////////////////////////////////////////////////// + /// + /// Draw the vertex buffer to a render target + /// + /// Render target to draw to + /// Index of the first vertex to render + /// Number of vertices to render + /// Current render states + //////////////////////////////////////////////////////////// + public void Draw(RenderTarget target, uint firstVertex, uint vertexCount, RenderStates states) + { + RenderStates.MarshalData marshaledStates = states.Marshal(); + + if (target is RenderWindow) + { + sfRenderWindow_drawVertexBufferRange(( (RenderWindow)target ).CPointer, CPointer, firstVertex, vertexCount, ref marshaledStates); + } + else if (target is RenderTexture) + { + sfRenderTexture_drawVertexBufferRange(( (RenderTexture)target ).CPointer, CPointer, firstVertex, vertexCount, ref marshaledStates); + } + } + #region Imports [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern IntPtr sfVertexBuffer_create(uint vertexCount, PrimitiveType type, UsageSpecifier usage); @@ -361,8 +384,14 @@ public void Draw(RenderTarget target, RenderStates states) [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern void sfRenderWindow_drawVertexBuffer(IntPtr CPointer, IntPtr VertexArray, ref RenderStates.MarshalData states); + [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern void sfRenderWindow_drawVertexBufferRange(IntPtr CPointer, IntPtr VertexBuffer, uint firstVertex, uint vertexCount, ref RenderStates.MarshalData states); + [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern void sfRenderTexture_drawVertexBuffer(IntPtr CPointer, IntPtr VertexBuffer, ref RenderStates.MarshalData states); + + [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern void sfRenderTexture_drawVertexBufferRange(IntPtr CPointer, IntPtr VertexBuffer, uint firstVertex, uint vertexCount, ref RenderStates.MarshalData states); #endregion } } diff --git a/src/SFML.System/Buffer.cs b/src/SFML.System/Buffer.cs new file mode 100644 index 00000000..2a4b309c --- /dev/null +++ b/src/SFML.System/Buffer.cs @@ -0,0 +1,87 @@ +using System; +using System.Runtime.InteropServices; +using System.Security; + +namespace SFML.System +{ + //////////////////////////////////////////////////////////// + /// + /// Internal helper class for CSFML's sfBuffer + /// + //////////////////////////////////////////////////////////// + public class Buffer : ObjectBase + { + //////////////////////////////////////////////////////////// + /// + /// Construct the buffer + /// + /// + //////////////////////////////////////////////////////////// + public Buffer() : + base(sfBuffer_create()) + { + if (CPointer == IntPtr.Zero) + { + throw new LoadingFailedException("buffer"); + } + } + + //////////////////////////////////////////////////////////// + /// + /// Get a copy of the buffer data + /// + /// A byte array containing the buffer data + //////////////////////////////////////////////////////////// + public byte[] GetData() + { + var size = sfBuffer_getSize(CPointer); + var ptr = sfBuffer_getData(CPointer); + + if (ptr == IntPtr.Zero) + { + return Array.Empty(); + } + + var data = new byte[size]; + Marshal.Copy(ptr, data, 0, (int)size); + + return data; + } + + //////////////////////////////////////////////////////////// + /// + /// Internal constructor + /// + /// Pointer to the object in C library + //////////////////////////////////////////////////////////// + internal Buffer(IntPtr cPointer) : + base(cPointer) + { + } + + //////////////////////////////////////////////////////////// + /// + /// Handle the destruction of the object + /// + /// Is the GC disposing the object, or is it an explicit call? + //////////////////////////////////////////////////////////// + protected override void Destroy(bool disposing) + { + sfBuffer_destroy(CPointer); + } + + #region Imports + [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern IntPtr sfBuffer_create(); + + [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern void sfBuffer_destroy(IntPtr buffer); + + [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern uint sfBuffer_getSize(IntPtr buffer); + + [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern IntPtr sfBuffer_getData(IntPtr buffer); + #endregion + } +} diff --git a/src/SFML.Window/Context.cs b/src/SFML.Window/Context.cs index 0b6c4c5f..5d8ef5b5 100644 --- a/src/SFML.Window/Context.cs +++ b/src/SFML.Window/Context.cs @@ -33,18 +33,42 @@ public Context() sfContext_destroy(myThis); } + //////////////////////////////////////////////////////////// + /// + /// Check whether a given OpenGL extension is available. + /// + /// Name of the extension to check for + /// True if available, false if unavailable + //////////////////////////////////////////////////////////// + public bool IsExtensionAvailable(string name) + { + return sfContext_isExtensionAvailable(myThis, name); + } + //////////////////////////////////////////////////////////// /// /// Activate or deactivate the context /// /// True to activate, false to deactivate - /// true on success, false on failure + /// True on success, false on failure //////////////////////////////////////////////////////////// public bool SetActive(bool active) { return sfContext_setActive(myThis, active); } + //////////////////////////////////////////////////////////// + /// + /// Get the address of an OpenGL function. + /// + /// Name of the function to get the address of + /// Address of the OpenGL function, on failure + //////////////////////////////////////////////////////////// + public IntPtr GetFunction(string name) + { + return sfContext_getFunction(myThis, name); + } + //////////////////////////////////////////////////////////// /// /// Get the settings of the context. @@ -95,9 +119,15 @@ public override string ToString() [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern void sfContext_destroy(IntPtr View); + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern bool sfContext_isExtensionAvailable(IntPtr View, string name); + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern bool sfContext_setActive(IntPtr View, bool Active); + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern IntPtr sfContext_getFunction(IntPtr View, string name); + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern ContextSettings sfContext_getSettings(IntPtr View); #endregion diff --git a/src/SFML.Window/Cursor.cs b/src/SFML.Window/Cursor.cs index 6d3a17ea..3168a6f1 100644 --- a/src/SFML.Window/Cursor.cs +++ b/src/SFML.Window/Cursor.cs @@ -84,6 +84,62 @@ public enum CursorType /// SizeBottomLeftTopRight, /// + /// Left arrow cursor on Linux, same as SizeHorizontal on other platforms + /// Windows: Yes + /// Mac OS: Yes + /// Linux: Yes + /// + SizeLeft, + /// + /// Right arrow cursor on Linux, same as SizeHorizontal on other platforms + /// Windows: Yes + /// Mac OS: Yes + /// Linux: Yes + /// + SizeRight, + /// + /// Up arrow cursor on Linux, same as SizeVertical on other platforms + /// Windows: Yes + /// Mac OS: Yes + /// Linux: Yes + /// + SizeTop, + /// + /// Down arrow cursor on Linux, same as SizeVertical on other platforms + /// Windows: Yes + /// Mac OS: Yes + /// Linux: Yes + /// + SizeBottom, + /// + /// Top-left arrow cursor on Linux, same as SizeTopLeftBottomRight on other platforms + /// Windows: Yes + /// Mac OS: Yes + /// Linux: Yes + /// + SizeTopLeft, + /// + /// Bottom-right arrow cursor on Linux, same as SizeTopLeftBottomRight on other platforms + /// Windows: Yes + /// Mac OS: Yes + /// Linux: Yes + /// + SizeBottomRight, + /// + /// Bottom-left arrow cursor on Linux, same as SizeBottomLeftTopRight on other platforms + /// Windows: Yes + /// Mac OS: Yes + /// Linux: Yes + /// + SizeBottomLeft, + /// + /// Top-right arrow cursor on Linux, same as SizeBottomLeftTopRight on other platforms + /// Windows: Yes + /// Mac OS: Yes + /// Linux: Yes + /// + SizeTopRight, + /// /// Combination of SizeHorizontal and SizeVertical /// Windows: Yes /// Mac OS: No diff --git a/src/SFML.Window/Keyboard.cs b/src/SFML.Window/Keyboard.cs index ebf37666..f17d67a5 100644 --- a/src/SFML.Window/Keyboard.cs +++ b/src/SFML.Window/Keyboard.cs @@ -15,6 +15,10 @@ public static class Keyboard //////////////////////////////////////////////////////////// /// /// Key codes + /// + /// The enumerators refer to the "localized" key; i.e. depending + /// on the layout set by the operating system, a key can be mapped + /// to `Y` or `Z`. /// //////////////////////////////////////////////////////////// public enum Key @@ -101,7 +105,7 @@ public enum Key LShift, /// The left Alt key LAlt, - /// The left OS specific key: window (Windows and Linux), apple (MacOS X), ... + /// The left OS specific key: window (Windows and Linux), apple (macOS), ... LSystem, /// The right Control key RControl, @@ -109,7 +113,7 @@ public enum Key RShift, /// The right Alt key RAlt, - /// The right OS specific key: window (Windows and Linux), apple (MacOS X), ... + /// The right OS specific key: window (Windows and Linux), apple (macOS), ... RSystem, /// The Menu key Menu, @@ -124,13 +128,13 @@ public enum Key /// The . key Period, /// The ' key - Quote, + Apostrophe, /// The / key Slash, /// The \ key Backslash, /// The ~ key - Tilde, + Grave, /// The = key Equal, /// The - key @@ -228,6 +232,9 @@ public enum Key KeyCount, // Keep last // Deprecated backwards compatible stuff + /// DEPRECATED: Use Grave + [Obsolete("Replace with Grave")] + Tilde = Grave, /// DEPRECATED: Use Hyphen [Obsolete("Replace with Hyphen")] Dash = Hyphen, @@ -242,9 +249,328 @@ public enum Key BackSlash = Backslash, /// DEPRECATED: Use Semicolon [Obsolete("Replace with Semicolon")] - SemiColon = Semicolon + SemiColon = Semicolon, + /// DEPRECATED: Use Apostrophe + [Obsolete("Replace with Apostrophe")] + Quote = Apostrophe }; + //////////////////////////////////////////////////////////// + /// + /// Scancodes + /// + /// The enumerators are bound to a physical key and do not depend on + /// the keyboard layout used by the operating system. Usually, the AT-101 + /// keyboard can be used as reference for the physical position of the keys. + /// + //////////////////////////////////////////////////////////// + public enum Scancode + { + /// Represents any scancode not present in this enum + Unknown = -1, + /// Keyboard a and A key + A = 0, + /// Keyboard b and B key + B, + /// Keyboard c and C key + C, + /// Keyboard d and D key + D, + /// Keyboard e and E key + E, + /// Keyboard f and F key + F, + /// Keyboard g and G key + G, + /// Keyboard h and H key + H, + /// Keyboard i and I key + I, + /// Keyboard j and J key + J, + /// Keyboard k and K key + K, + /// Keyboard l and L key + L, + /// Keyboard m and M key + M, + /// Keyboard n and N key + N, + /// Keyboard o and O key + O, + /// Keyboard p and P key + P, + /// Keyboard q and Q key + Q, + /// Keyboard r and R key + R, + /// Keyboard s and S key + S, + /// Keyboard t and T key + T, + /// Keyboard u and U key + U, + /// Keyboard v and V key + V, + /// Keyboard w and W key + W, + /// Keyboard x and X key + X, + /// Keyboard y and Y key + Y, + /// Keyboard z and Z key + Z, + /// Keyboard 1 and ! key + Num1, + /// Keyboard 2 and @ key + Num2, + /// Keyboard 3 and # key + Num3, + /// Keyboard 4 and $ key + Num4, + /// Keyboard 5 and % key + Num5, + /// Keyboard 6 and ^ key + Num6, + /// Keyboard 7 and & key + Num7, + /// Keyboard 8 and * key + Num8, + /// Keyboard 9 and ) key + Num9, + /// Keyboard 0 and ) key + Num0, + /// Keyboard Enter/Return key + Enter, + /// Keyboard Escape key + Escape, + /// Keyboard Backspace key + Backspace, + /// Keyboard Tab key + Tab, + /// Keyboard Space key + Space, + /// Keyboard - and _ key + Hyphen, + /// Keyboard = and + + Equal, + /// Keyboard [ and { key + LBracket, + /// Keyboard ] and } key + RBracket, + // For US keyboards mapped to key 29 (Microsoft Keyboard Scan Code Specification) + // For Non-US keyboards mapped to key 42 (Microsoft Keyboard Scan Code Specification) + // Typical language mappings: Belg:£µ` FrCa:<>} Dan:*' Dutch:`´ Fren:µ* Ger:'# Ital:§ù LatAm:[}` Nor:*@ Span:ç} Swed:*' Swiss:$£} UK:~# Brazil:}] + /// Keyboard \ and | key OR various keys for Non-US keyboards + Backslash, + /// Keyboard ; and : key + Semicolon, + /// Keyboard ' and " key + Apostrophe, + /// Keyboard ` and ~ key + Grave, + /// Keyboard , and < key + Comma, + /// Keyboard . and > key + Period, + /// Keyboard / and ? key + Slash, + /// Keyboard F1 key + F1, + /// Keyboard F2 key + F2, + /// Keyboard F3 key + F3, + /// Keyboard F5 key + F4, + /// Keyboard F4 key + F5, + /// Keyboard F6 key + F6, + /// Keyboard F7 key + F7, + /// Keyboard F8 key + F8, + /// Keyboard F9 key + F9, + /// Keyboard F10 key + F10, + /// Keyboard F11 key + F11, + /// Keyboard F12 key + F12, + /// Keyboard F13 key + F13, + /// Keyboard F14 key + F14, + /// Keyboard F15 key + F15, + /// Keyboard F16 key + F16, + /// Keyboard F17 key + F17, + /// Keyboard F18 key + F18, + /// Keyboard F19 key + F19, + /// Keyboard F20 key + F20, + /// Keyboard F21 key + F21, + /// Keyboard F22 key + F22, + /// Keyboard F23 key + F23, + /// Keyboard F24 key + F24, + /// Keyboard Caps %Lock key + CapsLock, + /// Keyboard Print Screen key + PrintScreen, + /// Keyboard Scroll %Lock key + ScrollLock, + /// Keyboard Pause key + Pause, + /// Keyboard Insert key + Insert, + /// Keyboard Home key + Home, + /// Keyboard Page Up key + PageUp, + /// Keyboard Delete Forward key + Delete, + /// Keyboard End key + End, + /// Keyboard Page Down key + PageDown, + /// Keyboard Right Arrow key + Right, + /// Keyboard Left Arrow key + Left, + /// Keyboard Down Arrow key + Down, + /// Keyboard Up Arrow key + Up, + /// Keypad Num %Lock and Clear key + NumLock, + /// Keypad / key + NumpadDivide, + /// Keypad * key + NumpadMultiply, + /// Keypad - key + NumpadMinus, + /// Keypad + key + NumpadPlus, + /// keypad = key + NumpadEqual, + /// Keypad Enter/Return key + NumpadEnter, + /// Keypad . and Delete key + NumpadDecimal, + /// Keypad 1 and End key + Numpad1, + /// Keypad 2 and Down Arrow key + Numpad2, + /// Keypad 3 and Page Down key + Numpad3, + /// Keypad 4 and Left Arrow key + Numpad4, + /// Keypad 5 key + Numpad5, + /// Keypad 6 and Right Arrow key + Numpad6, + /// Keypad 7 and Home key + Numpad7, + /// Keypad 8 and Up Arrow key + Numpad8, + /// Keypad 9 and Page Up key + Numpad9, + /// Keypad 0 and Insert key + Numpad0, + // For US keyboards doesn't exist + // For Non-US keyboards mapped to key 45 (Microsoft Keyboard Scan Code Specification) + // Typical language mappings: Belg:<\> FrCa:«°» Dan:<\> Dutch:]|[ Fren:<> Ger:<|> Ital:<> LatAm:<> Nor:<> Span:<> Swed:<|> Swiss:<\> UK:\| Brazil: \|. + /// Keyboard Non-US \ and | key + NonUsBackslash, + /// Keyboard Application key + Application, + /// Keyboard Execute key + Execute, + /// Keyboard Mode Change key + ModeChange, + /// Keyboard Help key + Help, + /// Keyboard Menu key + Menu, + /// Keyboard Select key + Select, + /// Keyboard Redo key + Redo, + /// Keyboard Undo key + Undo, + /// Keyboard Cut key + Cut, + /// Keyboard Copy key + Copy, + /// Keyboard Paste key + Paste, + /// Keyboard Volume Mute key + VolumeMute, + /// Keyboard Volume Up key + VolumeUp, + /// Keyboard Volume Down key + VolumeDown, + /// Keyboard Media Play Pause key + MediaPlayPause, + /// Keyboard Media Stop key + MediaStop, + /// Keyboard Media Next Track key + MediaNextTrack, + /// Keyboard Media Previous Track key + MediaPreviousTrack, + /// Keyboard Left Control key + LControl, + /// Keyboard Left Shift key + LShift, + /// Keyboard Left Alt key + LAlt, + /// Keyboard Left System key + LSystem, + /// Keyboard Right Control key + RControl, + /// Keyboard Right Shift key + RShift, + /// Keyboard Right Alt key + RAlt, + /// Keyboard Right System key + RSystem, + /// Keyboard Back key + Back, + /// Keyboard Forward key + Forward, + /// Keyboard Refresh key + Refresh, + /// Keyboard Stop key + Stop, + /// Keyboard Search key + Search, + /// Keyboard Favorites key + Favorites, + /// Keyboard Home Page key + HomePage, + /// Keyboard Launch Application 1 key + LaunchApplication1, + /// Keyboard Launch Application 2 key + LaunchApplication2, + /// Keyboard Launch Mail key + LaunchMail, + /// Keyboard Launch Media Select key + LaunchMediaSelect, + + /// Keep last -- the total number of scancodes + ScancodeCount + } + //////////////////////////////////////////////////////////// /// /// Check if a key is pressed @@ -259,7 +585,55 @@ public static bool IsKeyPressed(Key key) //////////////////////////////////////////////////////////// /// - /// Enable/Disable visibility of the virtual keyboard + /// Check if a key is pressed + /// + /// Scancode to check + /// True if the physical key is pressed, false otherwise + //////////////////////////////////////////////////////////// + public static bool IsScancodePressed(Scancode code) + { + return sfKeyboard_isScancodePressed(code); + } + + //////////////////////////////////////////////////////////// + /// + /// Localize a physical key to a logical one + /// + /// Scancode to localize + /// + /// The key corresponding to the scancode under the current + /// keyboard layout used by the operating system, or + /// when the scancode cannot be mapped + /// to a . + /// + //////////////////////////////////////////////////////////// + public static Key Localize(Scancode code) + { + return sfKeyboard_localize(code); + } + + //////////////////////////////////////////////////////////// + /// + /// Identify the physical key corresponding to a logical one + /// + /// Key to "delocalize" + /// + /// The scancode corresponding to the key under the current + /// keyboard layout used by the operating system, or + /// when the key cannot be mapped + /// to a . + /// + //////////////////////////////////////////////////////////// + public static Scancode Delocalize(Key key) + { + return sfKeyboard_delocalize(key); + } + + // TODO Implement GetDescription + + //////////////////////////////////////////////////////////// + /// + /// Show or hide the virtual keyboard. /// /// Applicable only on Android and iOS /// Whether to make the virtual keyboard visible (true) or not (false) @@ -273,6 +647,17 @@ public static void SetVirtualKeyboardVisible(bool visible) [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern bool sfKeyboard_isKeyPressed(Key Key); + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern bool sfKeyboard_isScancodePressed(Scancode code); + + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern Key sfKeyboard_localize(Scancode code); + + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern Scancode sfKeyboard_delocalize(Key key); + + // TODO Import sfKeyboard_getDescription + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern void sfKeyboard_setVirtualKeyboardVisible(bool visible); #endregion diff --git a/src/SFML.Window/Vulkan.cs b/src/SFML.Window/Vulkan.cs new file mode 100644 index 00000000..bc72574f --- /dev/null +++ b/src/SFML.Window/Vulkan.cs @@ -0,0 +1,49 @@ +using System; +using System.Runtime.InteropServices; +using System.Security; +using SFML.System; + +namespace SFML.Window +{ + /// Vulkan helper functions + public static class Vulkan + { + //////////////////////////////////////////////////////////// + /// + /// Tell whether or not the system supports Vulkan + /// + /// This function should always be called before using + /// the Vulkan features. If it returns false, then + /// any attempt to use Vulkan will fail. + /// + /// If only compute is required, set + /// to false to skip checking for the extensions necessary + /// for graphics rendering. + /// + /// True to skip checking for graphics extensions, false otherwise + /// True if Vulkan is supported, false otherwise + //////////////////////////////////////////////////////////// + public static bool IsAvailable(bool requireGraphics = true) => sfVulkan_isAvailable(requireGraphics); + + //////////////////////////////////////////////////////////// + /// + /// Get the address of a Vulkan function + /// + /// Name of the function to get the address of + /// Address of the Vulkan function, on failure + //////////////////////////////////////////////////////////// + public static IntPtr GetFunction(string name) => sfVulkan_getFunction(name); + + // TODO: Implement GetGraphicsRequiredInstanceExtensions + + #region Imports + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern bool sfVulkan_isAvailable(bool requireGraphics); + + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern IntPtr sfVulkan_getFunction(string name); + + // TODO: Import sfVulkan_getGraphicsRequiredInstanceExtensions + #endregion + } +} \ No newline at end of file diff --git a/src/SFML.Window/Window.cs b/src/SFML.Window/Window.cs index 6480fd47..d1f5f376 100644 --- a/src/SFML.Window/Window.cs +++ b/src/SFML.Window/Window.cs @@ -8,38 +8,10 @@ namespace SFML.Window { //////////////////////////////////////////////////////////// /// - /// Enumeration of window creation styles + /// Window that serves as a target for OpenGL rendering /// //////////////////////////////////////////////////////////// - [Flags] - public enum Styles - { - /// No border / title bar (this flag and all others are mutually exclusive) - None = 0, - - /// Title bar + fixed border - Titlebar = 1 << 0, - - /// Titlebar + resizable border + maximize button - Resize = 1 << 1, - - /// Titlebar + close button - Close = 1 << 2, - - /// Fullscreen mode (this flag and all others are mutually exclusive)) - Fullscreen = 1 << 3, - - /// Default window style (titlebar + resize + close) - Default = Titlebar | Resize | Close - } - - //////////////////////////////////////////////////////////// - /// - /// Window is a rendering window ; it can create a new window - /// or connect to an existing one - /// - //////////////////////////////////////////////////////////// - public class Window : ObjectBase + public class Window : WindowBase { //////////////////////////////////////////////////////////// /// @@ -121,7 +93,7 @@ public class Window : ObjectBase /// /// True if the window is opened //////////////////////////////////////////////////////////// - public virtual bool IsOpen + public override bool IsOpen { get { return sfWindow_isOpen(CPointer); } } @@ -133,7 +105,7 @@ public virtual bool IsOpen /// Create to recreate the window /// //////////////////////////////////////////////////////////// - public virtual void Close() + public override void Close() { sfWindow_close(CPointer); } @@ -163,7 +135,7 @@ public virtual ContextSettings Settings /// Position of the window /// //////////////////////////////////////////////////////////// - public virtual Vector2i Position + public override Vector2i Position { get { return sfWindow_getPosition(CPointer); } set { sfWindow_setPosition(CPointer, value); } @@ -174,7 +146,7 @@ public virtual Vector2i Position /// Size of the rendering region of the window /// //////////////////////////////////////////////////////////// - public virtual Vector2u Size + public override Vector2u Size { get { return sfWindow_getSize(CPointer); } set { sfWindow_setSize(CPointer, value); } @@ -186,7 +158,7 @@ public virtual Vector2u Size /// /// New title //////////////////////////////////////////////////////////// - public virtual void SetTitle(string title) + public override void SetTitle(string title) { // Copy the title to a null-terminated UTF-32 byte array byte[] titleAsUtf32 = Encoding.UTF32.GetBytes(title + '\0'); @@ -208,7 +180,7 @@ public virtual void SetTitle(string title) /// Icon's height, in pixels /// Array of pixels, format must be RGBA 32 bits //////////////////////////////////////////////////////////// - public virtual void SetIcon(uint width, uint height, byte[] pixels) + public override void SetIcon(uint width, uint height, byte[] pixels) { unsafe { @@ -225,7 +197,7 @@ public virtual void SetIcon(uint width, uint height, byte[] pixels) /// /// True to show the window, false to hide it //////////////////////////////////////////////////////////// - public virtual void SetVisible(bool visible) + public override void SetVisible(bool visible) { sfWindow_setVisible(CPointer, visible); } @@ -236,7 +208,7 @@ public virtual void SetVisible(bool visible) /// /// True to show, false to hide //////////////////////////////////////////////////////////// - public virtual void SetMouseCursorVisible(bool show) + public override void SetMouseCursorVisible(bool show) { sfWindow_setMouseCursorVisible(CPointer, show); } @@ -256,7 +228,7 @@ public virtual void SetMouseCursorVisible(bool show) /// cursor). /// //////////////////////////////////////////////////////////// - public virtual void SetMouseCursorGrabbed(bool grabbed) + public override void SetMouseCursorGrabbed(bool grabbed) { sfWindow_setMouseCursorGrabbed(CPointer, grabbed); } @@ -267,7 +239,7 @@ public virtual void SetMouseCursorGrabbed(bool grabbed) /// /// Native system cursor type to display //////////////////////////////////////////////////////////// - public virtual void SetMouseCursor(Cursor cursor) + public override void SetMouseCursor(Cursor cursor) { sfWindow_setMouseCursor(CPointer, cursor.CPointer); } @@ -290,7 +262,7 @@ public virtual void SetVerticalSyncEnabled(bool enable) /// /// True to enable, false to disable //////////////////////////////////////////////////////////// - public virtual void SetKeyRepeatEnabled(bool enable) + public override void SetKeyRepeatEnabled(bool enable) { sfWindow_setKeyRepeatEnabled(CPointer, enable); } @@ -338,7 +310,7 @@ public virtual void SetFramerateLimit(uint limit) /// /// New threshold, in range [0, 100] //////////////////////////////////////////////////////////// - public virtual void SetJoystickThreshold(float threshold) + public override void SetJoystickThreshold(float threshold) { sfWindow_setJoystickThreshold(CPointer, threshold); } @@ -348,47 +320,18 @@ public virtual void SetJoystickThreshold(float threshold) /// OS-specific handle of the window /// //////////////////////////////////////////////////////////// - public virtual IntPtr SystemHandle + public override IntPtr SystemHandle { get { return sfWindow_getSystemHandle(CPointer); } } - //////////////////////////////////////////////////////////// - /// - /// Wait for a new event and dispatch it to the corresponding - /// event handler - /// - //////////////////////////////////////////////////////////// - public void WaitAndDispatchEvents() - { - Event e; - if (WaitEvent(out e)) - { - CallEventHandler(e); - } - } - - //////////////////////////////////////////////////////////// - /// - /// Call the event handlers for each pending event - /// - //////////////////////////////////////////////////////////// - public void DispatchEvents() - { - Event e; - while (PollEvent(out e)) - { - CallEventHandler(e); - } - } - //////////////////////////////////////////////////////////// /// /// Request the current window to be made the active /// foreground window /// //////////////////////////////////////////////////////////// - public virtual void RequestFocus() + public override void RequestFocus() { sfWindow_requestFocus(CPointer); } @@ -399,7 +342,7 @@ public virtual void RequestFocus() /// /// True if the window has focus, false otherwise //////////////////////////////////////////////////////////// - public virtual bool HasFocus() + public override bool HasFocus() { return sfWindow_hasFocus(CPointer); } @@ -426,7 +369,7 @@ public override string ToString() /// Internal hack :) //////////////////////////////////////////////////////////// protected Window(IntPtr cPointer, int dummy) : - base(cPointer) + base(cPointer, 0) { // TODO : find a cleaner way of separating this constructor from Window(IntPtr handle) } @@ -438,7 +381,7 @@ public override string ToString() /// Variable to fill with the raw pointer to the event structure /// True if there was an event, false otherwise //////////////////////////////////////////////////////////// - protected virtual bool PollEvent(out Event eventToFill) + protected override bool PollEvent(out Event eventToFill) { return sfWindow_pollEvent(CPointer, out eventToFill); } @@ -450,51 +393,11 @@ protected virtual bool PollEvent(out Event eventToFill) /// Variable to fill with the raw pointer to the event structure /// False if any error occured //////////////////////////////////////////////////////////// - protected virtual bool WaitEvent(out Event eventToFill) + protected override bool WaitEvent(out Event eventToFill) { return sfWindow_waitEvent(CPointer, out eventToFill); } - //////////////////////////////////////////////////////////// - /// - /// Internal function to get the mouse position relative to the window. - /// This function is protected because it is called by another class of - /// another module, it is not meant to be called by users. - /// - /// Relative mouse position - //////////////////////////////////////////////////////////// - protected internal virtual Vector2i InternalGetMousePosition() - { - return sfMouse_getPosition(CPointer); - } - - //////////////////////////////////////////////////////////// - /// - /// Internal function to set the mouse position relative to the window. - /// This function is protected because it is called by another class of - /// another module, it is not meant to be called by users. - /// - /// Relative mouse position - //////////////////////////////////////////////////////////// - protected internal virtual void InternalSetMousePosition(Vector2i position) - { - sfMouse_setPosition(position, CPointer); - } - - //////////////////////////////////////////////////////////// - /// - /// Internal function to get the touch position relative to the window. - /// This function is protected because it is called by another class of - /// another module, it is not meant to be called by users. - /// - /// Finger index - /// Relative touch position - //////////////////////////////////////////////////////////// - protected internal virtual Vector2i InternalGetTouchPosition(uint Finger) - { - return sfTouch_getPosition(Finger, CPointer); - } - //////////////////////////////////////////////////////////// /// /// Handle the destruction of the object @@ -506,279 +409,6 @@ protected override void Destroy(bool disposing) sfWindow_destroy(CPointer); } - //////////////////////////////////////////////////////////// - /// - /// Call the event handler for the given event - /// - /// Event to dispatch - //////////////////////////////////////////////////////////// - private void CallEventHandler(Event e) - { - switch (e.Type) - { - case EventType.Closed: - if (Closed != null) - { - Closed(this, EventArgs.Empty); - } - - break; - - case EventType.GainedFocus: - if (GainedFocus != null) - { - GainedFocus(this, EventArgs.Empty); - } - - break; - - case EventType.JoystickButtonPressed: - if (JoystickButtonPressed != null) - { - JoystickButtonPressed(this, new JoystickButtonEventArgs(e.JoystickButton)); - } - - break; - - case EventType.JoystickButtonReleased: - if (JoystickButtonReleased != null) - { - JoystickButtonReleased(this, new JoystickButtonEventArgs(e.JoystickButton)); - } - - break; - - case EventType.JoystickMoved: - if (JoystickMoved != null) - { - JoystickMoved(this, new JoystickMoveEventArgs(e.JoystickMove)); - } - - break; - - case EventType.JoystickConnected: - if (JoystickConnected != null) - { - JoystickConnected(this, new JoystickConnectEventArgs(e.JoystickConnect)); - } - - break; - - case EventType.JoystickDisconnected: - if (JoystickDisconnected != null) - { - JoystickDisconnected(this, new JoystickConnectEventArgs(e.JoystickConnect)); - } - - break; - - case EventType.KeyPressed: - if (KeyPressed != null) - { - KeyPressed(this, new KeyEventArgs(e.Key)); - } - - break; - - case EventType.KeyReleased: - if (KeyReleased != null) - { - KeyReleased(this, new KeyEventArgs(e.Key)); - } - - break; - - case EventType.LostFocus: - if (LostFocus != null) - { - LostFocus(this, EventArgs.Empty); - } - - break; - - case EventType.MouseButtonPressed: - if (MouseButtonPressed != null) - { - MouseButtonPressed(this, new MouseButtonEventArgs(e.MouseButton)); - } - - break; - - case EventType.MouseButtonReleased: - if (MouseButtonReleased != null) - { - MouseButtonReleased(this, new MouseButtonEventArgs(e.MouseButton)); - } - - break; - - case EventType.MouseEntered: - if (MouseEntered != null) - { - MouseEntered(this, EventArgs.Empty); - } - - break; - - case EventType.MouseLeft: - if (MouseLeft != null) - { - MouseLeft(this, EventArgs.Empty); - } - - break; - - case EventType.MouseMoved: - if (MouseMoved != null) - { - MouseMoved(this, new MouseMoveEventArgs(e.MouseMove)); - } - - break; - -// Disable CS0618 (Obselete Warning). This Event will be removed in SFML.NET 3.0, but should remain supported until then. -#pragma warning disable CS0618 - case EventType.MouseWheelMoved: - if (MouseWheelMoved != null) - { - MouseWheelMoved(this, new MouseWheelEventArgs(e.MouseWheel)); - } - - break; -// restore CS0618 -#pragma warning restore CS0618 - - case EventType.MouseWheelScrolled: - if (MouseWheelScrolled != null) - { - MouseWheelScrolled(this, new MouseWheelScrollEventArgs(e.MouseWheelScroll)); - } - - break; - - case EventType.Resized: - if (Resized != null) - { - Resized(this, new SizeEventArgs(e.Size)); - } - - break; - - case EventType.TextEntered: - if (TextEntered != null) - { - TextEntered(this, new TextEventArgs(e.Text)); - } - - break; - - case EventType.TouchBegan: - if (TouchBegan != null) - { - TouchBegan(this, new TouchEventArgs(e.Touch)); - } - - break; - - case EventType.TouchMoved: - if (TouchMoved != null) - { - TouchMoved(this, new TouchEventArgs(e.Touch)); - } - - break; - - case EventType.TouchEnded: - if (TouchEnded != null) - { - TouchEnded(this, new TouchEventArgs(e.Touch)); - } - - break; - - case EventType.SensorChanged: - if (SensorChanged != null) - { - SensorChanged(this, new SensorEventArgs(e.Sensor)); - } - - break; - - default: - break; - } - } - - /// Event handler for the Closed event - public event EventHandler Closed = null; - - /// Event handler for the Resized event - public event EventHandler Resized = null; - - /// Event handler for the LostFocus event - public event EventHandler LostFocus = null; - - /// Event handler for the GainedFocus event - public event EventHandler GainedFocus = null; - - /// Event handler for the TextEntered event - public event EventHandler TextEntered = null; - - /// Event handler for the KeyPressed event - public event EventHandler KeyPressed = null; - - /// Event handler for the KeyReleased event - public event EventHandler KeyReleased = null; - - /// Event handler for the MouseWheelMoved event - [Obsolete("Use MouseWheelScrolled")] - public event EventHandler MouseWheelMoved = null; - - /// Event handler for the MouseWheelScrolled event - public event EventHandler MouseWheelScrolled = null; - - /// Event handler for the MouseButtonPressed event - public event EventHandler MouseButtonPressed = null; - - /// Event handler for the MouseButtonReleased event - public event EventHandler MouseButtonReleased = null; - - /// Event handler for the MouseMoved event - public event EventHandler MouseMoved = null; - - /// Event handler for the MouseEntered event - public event EventHandler MouseEntered = null; - - /// Event handler for the MouseLeft event - public event EventHandler MouseLeft = null; - - /// Event handler for the JoystickButtonPressed event - public event EventHandler JoystickButtonPressed = null; - - /// Event handler for the JoystickButtonReleased event - public event EventHandler JoystickButtonReleased = null; - - /// Event handler for the JoystickMoved event - public event EventHandler JoystickMoved = null; - - /// Event handler for the JoystickConnected event - public event EventHandler JoystickConnected = null; - - /// Event handler for the JoystickDisconnected event - public event EventHandler JoystickDisconnected = null; - - /// Event handler for the TouchBegan event - public event EventHandler TouchBegan = null; - - /// Event handler for the TouchMoved event - public event EventHandler TouchMoved = null; - - /// Event handler for the TouchEnded event - public event EventHandler TouchEnded = null; - - /// Event handler for the SensorChanged event - public event EventHandler SensorChanged = null; - #region Imports [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern IntPtr sfWindow_create(VideoMode Mode, string Title, Styles Style, ref ContextSettings Params); @@ -869,15 +499,6 @@ private void CallEventHandler(Event e) [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern bool sfWindow_hasFocus(IntPtr CPointer); - - [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] - private static extern Vector2i sfMouse_getPosition(IntPtr CPointer); - - [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] - private static extern void sfMouse_setPosition(Vector2i position, IntPtr CPointer); - - [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] - private static extern Vector2i sfTouch_getPosition(uint Finger, IntPtr RelativeTo); #endregion } } diff --git a/src/SFML.Window/WindowBase.cs b/src/SFML.Window/WindowBase.cs new file mode 100644 index 00000000..3b95f291 --- /dev/null +++ b/src/SFML.Window/WindowBase.cs @@ -0,0 +1,797 @@ +using System; +using System.Collections.Generic; +using System.Runtime; +using System.Runtime.InteropServices; +using System.Security; +using System.Text; +using SFML.System; + +namespace SFML.Window +{ + //////////////////////////////////////////////////////////// + /// + /// Enumeration of window creation styles + /// + //////////////////////////////////////////////////////////// + [Flags] + public enum Styles + { + /// No border / title bar (this flag and all others are mutually exclusive) + None = 0, + + /// Title bar + fixed border + Titlebar = 1 << 0, + + /// Titlebar + resizable border + maximize button + Resize = 1 << 1, + + /// Titlebar + close button + Close = 1 << 2, + + /// Fullscreen mode (this flag and all others are mutually exclusive)) + Fullscreen = 1 << 3, + + /// Default window style (titlebar + resize + close) + Default = Titlebar | Resize | Close + } + + + //////////////////////////////////////////////////////////// + /// + /// Window that serves as a base for other windows + /// + //////////////////////////////////////////////////////////// + public class WindowBase : ObjectBase + { + //////////////////////////////////////////////////////////// + /// + /// Create the window with default style and creation settings + /// + /// Video mode to use + /// Title of the window + //////////////////////////////////////////////////////////// + public WindowBase(VideoMode mode, string title) : + this(mode, title, Styles.Default) + { + } + + //////////////////////////////////////////////////////////// + /// + /// Create the window with default creation settings + /// + /// Video mode to use + /// Title of the window + /// Window style (Resize | Close by default) + //////////////////////////////////////////////////////////// + public WindowBase(VideoMode mode, string title, Styles style) : + base(IntPtr.Zero) + { + // Copy the title to a null-terminated UTF-32 byte array + var titleAsUtf32 = Encoding.UTF32.GetBytes(title + '\0'); + + unsafe + { + fixed (byte* titlePtr = titleAsUtf32) + { + CPointer = sfWindowBase_createUnicode(mode, (IntPtr)titlePtr, style); + } + } + } + + //////////////////////////////////////////////////////////// + /// + /// Create the window from an existing control + /// + /// Platform-specific handle of the control + //////////////////////////////////////////////////////////// + public WindowBase(IntPtr Handle) : + base(sfWindowBase_createFromHandle(Handle)) + { + } + + //////////////////////////////////////////////////////////// + /// + /// Tell whether or not the window is opened (ie. has been created). + /// Note that a hidden window (Show(false)) + /// will still return true + /// + /// True if the window is opened + //////////////////////////////////////////////////////////// + public virtual bool IsOpen + { + get { return sfWindowBase_isOpen(CPointer); } + } + + //////////////////////////////////////////////////////////// + /// + /// Close (destroy) the window. + /// The Window instance remains valid and you can call + /// Create to recreate the window + /// + //////////////////////////////////////////////////////////// + public virtual void Close() + { + sfWindowBase_close(CPointer); + } + + //////////////////////////////////////////////////////////// + /// + /// Position of the window + /// + //////////////////////////////////////////////////////////// + public virtual Vector2i Position + { + get { return sfWindowBase_getPosition(CPointer); } + set { sfWindowBase_setPosition(CPointer, value); } + } + + //////////////////////////////////////////////////////////// + /// + /// Size of the rendering region of the window + /// + //////////////////////////////////////////////////////////// + public virtual Vector2u Size + { + get { return sfWindowBase_getSize(CPointer); } + set { sfWindowBase_setSize(CPointer, value); } + } + + //////////////////////////////////////////////////////////// + /// + /// Change the title of the window + /// + /// New title + //////////////////////////////////////////////////////////// + public virtual void SetTitle(string title) + { + // Copy the title to a null-terminated UTF-32 byte array + var titleAsUtf32 = Encoding.UTF32.GetBytes(title + '\0'); + + unsafe + { + fixed (byte* titlePtr = titleAsUtf32) + { + sfWindowBase_setUnicodeTitle(CPointer, (IntPtr)titlePtr); + } + } + } + + //////////////////////////////////////////////////////////// + /// + /// Change the window's icon + /// + /// Icon's width, in pixels + /// Icon's height, in pixels + /// Array of pixels, format must be RGBA 32 bits + //////////////////////////////////////////////////////////// + public virtual void SetIcon(uint width, uint height, byte[] pixels) + { + unsafe + { + fixed (byte* PixelsPtr = pixels) + { + sfWindowBase_setIcon(CPointer, width, height, PixelsPtr); + } + } + } + + //////////////////////////////////////////////////////////// + /// + /// Show or hide the window + /// + /// True to show the window, false to hide it + //////////////////////////////////////////////////////////// + public virtual void SetVisible(bool visible) + { + sfWindowBase_setVisible(CPointer, visible); + } + + //////////////////////////////////////////////////////////// + /// + /// Show or hide the mouse cursor + /// + /// True to show, false to hide + //////////////////////////////////////////////////////////// + public virtual void SetMouseCursorVisible(bool show) + { + sfWindowBase_setMouseCursorVisible(CPointer, show); + } + + //////////////////////////////////////////////////////////// + /// + /// Grab or release the mouse cursor + /// + /// True to grab, false to release + /// + /// + /// If set, grabs the mouse cursor inside this window's client + /// area so it may no longer be moved outside its bounds. + /// Note that grabbing is only active while the window has + /// focus and calling this function for fullscreen windows + /// won't have any effect (fullscreen windows always grab the + /// cursor). + /// + //////////////////////////////////////////////////////////// + public virtual void SetMouseCursorGrabbed(bool grabbed) + { + sfWindowBase_setMouseCursorGrabbed(CPointer, grabbed); + } + + //////////////////////////////////////////////////////////// + /// + /// Set the displayed cursor to a native system cursor + /// + /// Upon window creation, the arrow cursor is used by default. + /// + /// Native system cursor type to display + //////////////////////////////////////////////////////////// + public virtual void SetMouseCursor(Cursor cursor) + { + sfWindowBase_setMouseCursor(CPointer, cursor.CPointer); + } + + //////////////////////////////////////////////////////////// + /// + /// Enable or disable automatic key-repeat. + /// + /// If key repeat is enabled, you will receive repeated + /// events while keeping a key pressed. If it is + /// disabled, you will only get a single event when the key is pressed. + /// + /// Automatic key-repeat is enabled by default + /// + /// True to enable, false to disable + //////////////////////////////////////////////////////////// + public virtual void SetKeyRepeatEnabled(bool enable) + { + sfWindowBase_setKeyRepeatEnabled(CPointer, enable); + } + + //////////////////////////////////////////////////////////// + /// + /// Change the joystick threshold, ie. the value below which + /// no move event will be generated + /// + /// New threshold, in range [0, 100] + //////////////////////////////////////////////////////////// + public virtual void SetJoystickThreshold(float threshold) + { + sfWindowBase_setJoystickThreshold(CPointer, threshold); + } + + //////////////////////////////////////////////////////////// + /// + /// OS-specific handle of the window + /// + //////////////////////////////////////////////////////////// + public virtual IntPtr SystemHandle + { + get { return sfWindowBase_getSystemHandle(CPointer); } + } + + //////////////////////////////////////////////////////////// + /// + /// Wait for a new event and dispatch it to the corresponding + /// event handler + /// + //////////////////////////////////////////////////////////// + public void WaitAndDispatchEvents() + { + Event e; + if (WaitEvent(out e)) + { + CallEventHandler(e); + } + } + + //////////////////////////////////////////////////////////// + /// + /// Call the event handlers for each pending event + /// + //////////////////////////////////////////////////////////// + public void DispatchEvents() + { + Event e; + while (PollEvent(out e)) + { + CallEventHandler(e); + } + } + + //////////////////////////////////////////////////////////// + /// + /// Request the current window to be made the active + /// foreground window + /// + //////////////////////////////////////////////////////////// + public virtual void RequestFocus() + { + sfWindowBase_requestFocus(CPointer); + } + + //////////////////////////////////////////////////////////// + /// + /// Check whether the window has the input focus + /// + /// True if the window has focus, false otherwise + //////////////////////////////////////////////////////////// + public virtual bool HasFocus() + { + return sfWindowBase_hasFocus(CPointer); + } + + //////////////////////////////////////////////////////////// + /// + /// Create a Vulkan rendering surface + /// + /// Vulkan instance + /// Created surface + /// Allocator to use + /// True if surface creation was successful, false otherwise + //////////////////////////////////////////////////////////// + public virtual bool CreateVulkanSurface(IntPtr vkInstance, out IntPtr vkSurface, IntPtr vkAllocator) + { + return sfWindowBase_createVulkanSurface(CPointer, vkInstance, out vkSurface, vkAllocator); + } + + //////////////////////////////////////////////////////////// + /// + /// Provide a string describing the object + /// + /// String description of the object + //////////////////////////////////////////////////////////// + public override string ToString() + { + return "[WindowBase]" + + " Size(" + Size + ")" + + " Position(" + Position + ")"; + } + + //////////////////////////////////////////////////////////// + /// + /// Constructor for derived classes + /// + /// Pointer to the internal object in the C API + /// Internal hack :) + //////////////////////////////////////////////////////////// + protected WindowBase(IntPtr cPointer, int dummy) : + base(cPointer) + { + // TODO : find a cleaner way of separating this constructor from WindowBase(IntPtr handle) + } + + //////////////////////////////////////////////////////////// + /// + /// Internal function to get the next event (non-blocking) + /// + /// Variable to fill with the raw pointer to the event structure + /// True if there was an event, false otherwise + //////////////////////////////////////////////////////////// + protected virtual bool PollEvent(out Event eventToFill) + { + return sfWindowBase_pollEvent(CPointer, out eventToFill); + } + + //////////////////////////////////////////////////////////// + /// + /// Internal function to get the next event (blocking) + /// + /// Variable to fill with the raw pointer to the event structure + /// False if any error occured + //////////////////////////////////////////////////////////// + protected virtual bool WaitEvent(out Event eventToFill) + { + return sfWindowBase_waitEvent(CPointer, out eventToFill); + } + + //////////////////////////////////////////////////////////// + /// + /// Internal function to get the mouse position relative to the window. + /// This function is protected because it is called by another class of + /// another module, it is not meant to be called by users. + /// + /// Relative mouse position + //////////////////////////////////////////////////////////// + protected internal virtual Vector2i InternalGetMousePosition() + { + return sfMouse_getPosition(CPointer); + } + + //////////////////////////////////////////////////////////// + /// + /// Internal function to set the mouse position relative to the window. + /// This function is protected because it is called by another class of + /// another module, it is not meant to be called by users. + /// + /// Relative mouse position + //////////////////////////////////////////////////////////// + protected internal virtual void InternalSetMousePosition(Vector2i position) + { + sfMouse_setPosition(position, CPointer); + } + + //////////////////////////////////////////////////////////// + /// + /// Internal function to get the touch position relative to the window. + /// This function is protected because it is called by another class of + /// another module, it is not meant to be called by users. + /// + /// Finger index + /// Relative touch position + //////////////////////////////////////////////////////////// + protected internal virtual Vector2i InternalGetTouchPosition(uint Finger) + { + return sfTouch_getPosition(Finger, CPointer); + } + + //////////////////////////////////////////////////////////// + /// + /// Handle the destruction of the object + /// + /// Is the GC disposing the object, or is it an explicit call ? + //////////////////////////////////////////////////////////// + protected override void Destroy(bool disposing) + { + sfWindowBase_destroy(CPointer); + } + + //////////////////////////////////////////////////////////// + /// + /// Call the event handler for the given event + /// + /// Event to dispatch + //////////////////////////////////////////////////////////// + private void CallEventHandler(Event e) + { + switch (e.Type) + { + case EventType.Closed: + if (Closed != null) + { + Closed(this, EventArgs.Empty); + } + + break; + + case EventType.GainedFocus: + if (GainedFocus != null) + { + GainedFocus(this, EventArgs.Empty); + } + + break; + + case EventType.JoystickButtonPressed: + if (JoystickButtonPressed != null) + { + JoystickButtonPressed(this, new JoystickButtonEventArgs(e.JoystickButton)); + } + + break; + + case EventType.JoystickButtonReleased: + if (JoystickButtonReleased != null) + { + JoystickButtonReleased(this, new JoystickButtonEventArgs(e.JoystickButton)); + } + + break; + + case EventType.JoystickMoved: + if (JoystickMoved != null) + { + JoystickMoved(this, new JoystickMoveEventArgs(e.JoystickMove)); + } + + break; + + case EventType.JoystickConnected: + if (JoystickConnected != null) + { + JoystickConnected(this, new JoystickConnectEventArgs(e.JoystickConnect)); + } + + break; + + case EventType.JoystickDisconnected: + if (JoystickDisconnected != null) + { + JoystickDisconnected(this, new JoystickConnectEventArgs(e.JoystickConnect)); + } + + break; + + case EventType.KeyPressed: + if (KeyPressed != null) + { + KeyPressed(this, new KeyEventArgs(e.Key)); + } + + break; + + case EventType.KeyReleased: + if (KeyReleased != null) + { + KeyReleased(this, new KeyEventArgs(e.Key)); + } + + break; + + case EventType.LostFocus: + if (LostFocus != null) + { + LostFocus(this, EventArgs.Empty); + } + + break; + + case EventType.MouseButtonPressed: + if (MouseButtonPressed != null) + { + MouseButtonPressed(this, new MouseButtonEventArgs(e.MouseButton)); + } + + break; + + case EventType.MouseButtonReleased: + if (MouseButtonReleased != null) + { + MouseButtonReleased(this, new MouseButtonEventArgs(e.MouseButton)); + } + + break; + + case EventType.MouseEntered: + if (MouseEntered != null) + { + MouseEntered(this, EventArgs.Empty); + } + + break; + + case EventType.MouseLeft: + if (MouseLeft != null) + { + MouseLeft(this, EventArgs.Empty); + } + + break; + + case EventType.MouseMoved: + if (MouseMoved != null) + { + MouseMoved(this, new MouseMoveEventArgs(e.MouseMove)); + } + + break; + + // Disable CS0618 (Obselete Warning). This Event will be removed in SFML.NET 3.0, but should remain supported until then. +#pragma warning disable CS0618 + case EventType.MouseWheelMoved: + if (MouseWheelMoved != null) + { + MouseWheelMoved(this, new MouseWheelEventArgs(e.MouseWheel)); + } + + break; + // restore CS0618 +#pragma warning restore CS0618 + + case EventType.MouseWheelScrolled: + if (MouseWheelScrolled != null) + { + MouseWheelScrolled(this, new MouseWheelScrollEventArgs(e.MouseWheelScroll)); + } + + break; + + case EventType.Resized: + if (Resized != null) + { + Resized(this, new SizeEventArgs(e.Size)); + } + + break; + + case EventType.TextEntered: + if (TextEntered != null) + { + TextEntered(this, new TextEventArgs(e.Text)); + } + + break; + + case EventType.TouchBegan: + if (TouchBegan != null) + { + TouchBegan(this, new TouchEventArgs(e.Touch)); + } + + break; + + case EventType.TouchMoved: + if (TouchMoved != null) + { + TouchMoved(this, new TouchEventArgs(e.Touch)); + } + + break; + + case EventType.TouchEnded: + if (TouchEnded != null) + { + TouchEnded(this, new TouchEventArgs(e.Touch)); + } + + break; + + case EventType.SensorChanged: + if (SensorChanged != null) + { + SensorChanged(this, new SensorEventArgs(e.Sensor)); + } + + break; + + default: + break; + } + } + + /// Event handler for the Closed event + public event EventHandler Closed = null; + + /// Event handler for the Resized event + public event EventHandler Resized = null; + + /// Event handler for the LostFocus event + public event EventHandler LostFocus = null; + + /// Event handler for the GainedFocus event + public event EventHandler GainedFocus = null; + + /// Event handler for the TextEntered event + public event EventHandler TextEntered = null; + + /// Event handler for the KeyPressed event + public event EventHandler KeyPressed = null; + + /// Event handler for the KeyReleased event + public event EventHandler KeyReleased = null; + + /// Event handler for the MouseWheelMoved event + [Obsolete("Use MouseWheelScrolled")] + public event EventHandler MouseWheelMoved = null; + + /// Event handler for the MouseWheelScrolled event + public event EventHandler MouseWheelScrolled = null; + + /// Event handler for the MouseButtonPressed event + public event EventHandler MouseButtonPressed = null; + + /// Event handler for the MouseButtonReleased event + public event EventHandler MouseButtonReleased = null; + + /// Event handler for the MouseMoved event + public event EventHandler MouseMoved = null; + + /// Event handler for the MouseEntered event + public event EventHandler MouseEntered = null; + + /// Event handler for the MouseLeft event + public event EventHandler MouseLeft = null; + + /// Event handler for the JoystickButtonPressed event + public event EventHandler JoystickButtonPressed = null; + + /// Event handler for the JoystickButtonReleased event + public event EventHandler JoystickButtonReleased = null; + + /// Event handler for the JoystickMoved event + public event EventHandler JoystickMoved = null; + + /// Event handler for the JoystickConnected event + public event EventHandler JoystickConnected = null; + + /// Event handler for the JoystickDisconnected event + public event EventHandler JoystickDisconnected = null; + + /// Event handler for the TouchBegan event + public event EventHandler TouchBegan = null; + + /// Event handler for the TouchMoved event + public event EventHandler TouchMoved = null; + + /// Event handler for the TouchEnded event + public event EventHandler TouchEnded = null; + + /// Event handler for the SensorChanged event + public event EventHandler SensorChanged = null; + + #region Imports + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern IntPtr sfWindowBase_create(VideoMode Mode, string Title, Styles Style); + + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern IntPtr sfWindowBase_createUnicode(VideoMode Mode, IntPtr Title, Styles Style); + + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern IntPtr sfWindowBase_createFromHandle(IntPtr Handle); + + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern void sfWindowBase_destroy(IntPtr CPointer); + + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern void sfWindowBase_close(IntPtr CPointer); + + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern bool sfWindowBase_isOpen(IntPtr CPointer); + + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern bool sfWindowBase_pollEvent(IntPtr CPointer, out Event Evt); + + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern bool sfWindowBase_waitEvent(IntPtr CPointer, out Event Evt); + + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern Vector2i sfWindowBase_getPosition(IntPtr CPointer); + + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern void sfWindowBase_setPosition(IntPtr CPointer, Vector2i position); + + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern Vector2u sfWindowBase_getSize(IntPtr CPointer); + + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern void sfWindowBase_setSize(IntPtr CPointer, Vector2u size); + + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern void sfWindowBase_setTitle(IntPtr CPointer, string title); + + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern void sfWindowBase_setUnicodeTitle(IntPtr CPointer, IntPtr title); + + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private unsafe static extern void sfWindowBase_setIcon(IntPtr CPointer, uint Width, uint Height, byte* Pixels); + + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern void sfWindowBase_setVisible(IntPtr CPointer, bool visible); + + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern void sfWindowBase_setMouseCursorVisible(IntPtr CPointer, bool Show); + + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern void sfWindowBase_setMouseCursorGrabbed(IntPtr CPointer, bool grabbed); + + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern void sfWindowBase_setMouseCursor(IntPtr CPointer, IntPtr cursor); + + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern void sfWindowBase_setKeyRepeatEnabled(IntPtr CPointer, bool Enable); + + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern void sfWindowBase_setJoystickThreshold(IntPtr CPointer, float Threshold); + + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern void sfWindowBase_requestFocus(IntPtr CPointer); + + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern bool sfWindowBase_hasFocus(IntPtr CPointer); + + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern IntPtr sfWindowBase_getSystemHandle(IntPtr CPointer); + + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern bool sfWindowBase_createVulkanSurface(IntPtr CPointer, IntPtr vkInstance, out IntPtr surface, IntPtr vkAllocator); + + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern Vector2i sfMouse_getPosition(IntPtr CPointer); + + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern void sfMouse_setPosition(Vector2i position, IntPtr CPointer); + + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern Vector2i sfTouch_getPosition(uint Finger, IntPtr RelativeTo); + #endregion + } +}