using System; using System.Runtime.InteropServices; using System.Collections; using System.Collections.Generic; using System.Security; using SFML.Window; namespace SFML { namespace Graphics { //////////////////////////////////////////////////////////// /// /// Simple wrapper for Window that allows easy /// 2D rendering /// //////////////////////////////////////////////////////////// public class RenderImage : ObjectBase, RenderTarget { //////////////////////////////////////////////////////////// /// /// Create the render image with the given dimensions /// /// Width of the render image /// Height of the render image //////////////////////////////////////////////////////////// public RenderImage(uint width, uint height) : this(width, height, false) { } //////////////////////////////////////////////////////////// /// /// Create the render image with the given dimensions and /// an optional depth-buffer attached /// /// Width of the render image /// Height of the render image /// Do you wxant a depth-buffer attached? //////////////////////////////////////////////////////////// public RenderImage(uint width, uint height, bool depthBuffer) : base(sfRenderImage_Create(width, height, depthBuffer)) { myDefaultView = new View(sfRenderImage_GetDefaultView(This)); myCurrentView = myDefaultView; myImage = new Image(sfRenderImage_GetImage(This)); GC.SuppressFinalize(myDefaultView); GC.SuppressFinalize(myImage); } //////////////////////////////////////////////////////////// /// /// Width of the rendering region of the image /// //////////////////////////////////////////////////////////// public uint Width { get {return sfRenderImage_GetWidth(This);} } //////////////////////////////////////////////////////////// /// /// Height of the rendering region of the image /// //////////////////////////////////////////////////////////// public uint Height { get {return sfRenderImage_GetHeight(This);} } //////////////////////////////////////////////////////////// /// /// Activate of deactivate the render image as the current target /// for rendering /// /// True to activate, false to deactivate (true by default) /// True if operation was successful, false otherwise //////////////////////////////////////////////////////////// public bool SetActive(bool active) { return sfRenderImage_SetActive(This, active); } //////////////////////////////////////////////////////////// /// /// Default view of the render image /// //////////////////////////////////////////////////////////// public View DefaultView { get {return myDefaultView;} } //////////////////////////////////////////////////////////// /// /// Current view active in the render image /// //////////////////////////////////////////////////////////// public View CurrentView { get {return myCurrentView;} set {myCurrentView = value; sfRenderImage_SetView(This, value.This);} } //////////////////////////////////////////////////////////// /// /// Get the viewport of a view applied to this target /// /// Target view /// Viewport rectangle, expressed in pixels in the current target //////////////////////////////////////////////////////////// public IntRect GetViewport(View view) { return sfRenderImage_GetViewport(This, view.This); } //////////////////////////////////////////////////////////// /// /// Convert a point in target coordinates into view coordinates /// This version uses the current view of the window /// /// X coordinate of the point to convert, relative to the target /// Y coordinate of the point to convert, relative to the target /// Converted point /// //////////////////////////////////////////////////////////// public Vector2 ConvertCoords(uint x, uint y) { return ConvertCoords(x, y, CurrentView); } //////////////////////////////////////////////////////////// /// /// Convert a point in target coordinates into view coordinates /// This version uses the given view /// /// X coordinate of the point to convert, relative to the target /// Y coordinate of the point to convert, relative to the target /// Target view to convert the point to /// Converted point /// //////////////////////////////////////////////////////////// public Vector2 ConvertCoords(uint x, uint y, View view) { Vector2 point; sfRenderImage_ConvertCoords(This, x, y, out point.X, out point.Y, view.This); return point; } //////////////////////////////////////////////////////////// /// /// Clear the entire render image with black color /// //////////////////////////////////////////////////////////// public void Clear() { sfRenderImage_Clear(This, Color.Black); } //////////////////////////////////////////////////////////// /// /// Clear the entire render image with a single color /// /// Color to use to clear the image //////////////////////////////////////////////////////////// public void Clear(Color color) { sfRenderImage_Clear(This, color); } //////////////////////////////////////////////////////////// /// /// Draw something into the render image /// /// Object to draw //////////////////////////////////////////////////////////// public void Draw(Drawable objectToDraw) { objectToDraw.Render(this); } //////////////////////////////////////////////////////////// /// /// Apply a post-fx to the render image /// /// PostFx to apply //////////////////////////////////////////////////////////// public void Draw(PostFx postFx) { sfRenderImage_DrawPostFX(This, postFx != null ? postFx.This : IntPtr.Zero); } //////////////////////////////////////////////////////////// /// /// Make sure that what has been drawn so far is rendered. /// /// Use this function if you use OpenGL rendering commands, /// and you want to make sure that things will appear on top /// of all the SFML objects that have been drawn so far. /// This is needed because SFML doesn't use immediate rendering, /// it first accumulates drawables into a queue and /// trigger the actual rendering afterwards. /// /// You don't need to call this function if you're not /// dealing with OpenGL directly. /// //////////////////////////////////////////////////////////// public void Flush() { sfRenderImage_Flush(This); } //////////////////////////////////////////////////////////// /// /// Update the contents of the target image /// //////////////////////////////////////////////////////////// public void Display() { sfRenderImage_Display(This); } //////////////////////////////////////////////////////////// /// /// Target image of the render image /// //////////////////////////////////////////////////////////// public Image Image { get {return myImage;} } //////////////////////////////////////////////////////////// /// /// Tell whether or not the system supports render images /// //////////////////////////////////////////////////////////// public static bool CanUseRenderImage { get {return sfRenderImage_CanUseRenderImage();} } //////////////////////////////////////////////////////////// /// /// Handle the destruction of the object /// /// Is the GC disposing the object, or is it an explicit call ? //////////////////////////////////////////////////////////// protected override void Destroy(bool disposing) { if (!disposing) Context.Global.SetActive(true); sfRenderImage_Destroy(This); if (disposing) { myDefaultView.Dispose(); myImage.Dispose(); } if (!disposing) Context.Global.SetActive(false); } private View myCurrentView = null; private View myDefaultView = null; private Image myImage = null; #region Imports [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern IntPtr sfRenderImage_Create(uint Width, uint Height, bool DepthBuffer); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfRenderImage_Destroy(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfRenderImage_Clear(IntPtr This, Color ClearColor); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern uint sfRenderImage_GetWidth(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern uint sfRenderImage_GetHeight(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern bool sfRenderImage_SetActive(IntPtr This, bool Active); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern bool sfRenderImage_Flush(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern bool sfRenderImage_Display(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfRenderImage_SetView(IntPtr This, IntPtr View); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern IntPtr sfRenderImage_GetView(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern IntPtr sfRenderImage_GetDefaultView(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern IntRect sfRenderImage_GetViewport(IntPtr This, IntPtr TargetView); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfRenderImage_ConvertCoords(IntPtr This, uint WindowX, uint WindowY, out float ViewX, out float ViewY, IntPtr TargetView); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfRenderImage_DrawPostFX(IntPtr This, IntPtr PostFx); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern IntPtr sfRenderImage_GetImage(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern bool sfRenderImage_CanUseRenderImage(); #endregion } } }