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 RenderWindow : SFML.Window.Window
{
////////////////////////////////////////////////////////////
///
/// Create the window with default style and creation settings
///
/// Video mode to use
/// Title of the window
////////////////////////////////////////////////////////////
public RenderWindow(VideoMode mode, string title) :
this(mode, title, Styles.Resize | Styles.Close, new WindowSettings(24, 8, 0))
{
}
////////////////////////////////////////////////////////////
///
/// Create the window with default creation settings
///
/// Video mode to use
/// Title of the window
/// Window style (Resize | Close by default)
////////////////////////////////////////////////////////////
public RenderWindow(VideoMode mode, string title, Styles style) :
this(mode, title, style, new WindowSettings(24, 8, 0))
{
}
////////////////////////////////////////////////////////////
///
/// Create the window
///
/// Video mode to use
/// Title of the window
/// Window style (Resize | Close by default)
/// Creation parameters
////////////////////////////////////////////////////////////
public RenderWindow(VideoMode mode, string title, Styles style, WindowSettings settings) :
base(sfRenderWindow_Create(mode, title, style, settings), 0)
{
Initialize();
}
////////////////////////////////////////////////////////////
///
/// Create the window from an existing control with default creation settings
///
/// Platform-specific handle of the control
////////////////////////////////////////////////////////////
public RenderWindow(IntPtr handle) :
this(handle, new WindowSettings(24, 8, 0))
{
}
////////////////////////////////////////////////////////////
///
/// Create the window from an existing control
///
/// Platform-specific handle of the control
/// Creation parameters
////////////////////////////////////////////////////////////
public RenderWindow(IntPtr handle, WindowSettings settings) :
base(sfRenderWindow_CreateFromHandle(handle, settings), 0)
{
Initialize();
}
////////////////////////////////////////////////////////////
///
/// 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 override bool IsOpened()
{
return sfRenderWindow_IsOpened(This);
}
////////////////////////////////////////////////////////////
///
/// Close (destroy) the window.
/// The Window instance remains valid and you can call
/// Create to recreate the window
///
////////////////////////////////////////////////////////////
public override void Close()
{
sfRenderWindow_Close(This);
}
////////////////////////////////////////////////////////////
///
/// Display the window on screen
///
////////////////////////////////////////////////////////////
public override void Display()
{
sfRenderWindow_Display(This);
}
////////////////////////////////////////////////////////////
///
/// Width of the rendering region of the window
///
////////////////////////////////////////////////////////////
public override uint Width
{
get {return sfRenderWindow_GetWidth(This);}
}
////////////////////////////////////////////////////////////
///
/// Height of the rendering region of the window
///
////////////////////////////////////////////////////////////
public override uint Height
{
get {return sfRenderWindow_GetHeight(This);}
}
////////////////////////////////////////////////////////////
///
/// Creation settings of the window
///
////////////////////////////////////////////////////////////
public override WindowSettings Settings
{
get {return sfRenderWindow_GetSettings(This);}
}
////////////////////////////////////////////////////////////
///
/// Enable / disable vertical synchronization
///
/// True to enable v-sync, false to deactivate
////////////////////////////////////////////////////////////
public override void UseVerticalSync(bool enable)
{
sfRenderWindow_UseVerticalSync(This, enable);
}
////////////////////////////////////////////////////////////
///
/// Show or hide the mouse cursor
///
/// True to show, false to hide
////////////////////////////////////////////////////////////
public override void ShowMouseCursor(bool show)
{
sfRenderWindow_ShowMouseCursor(This, show);
}
////////////////////////////////////////////////////////////
///
/// Change the position of the mouse cursor
///
/// Left coordinate of the cursor, relative to the window
/// Top coordinate of the cursor, relative to the window
////////////////////////////////////////////////////////////
public override void SetCursorPosition(uint x, uint y)
{
sfRenderWindow_SetCursorPosition(This, x, y);
}
////////////////////////////////////////////////////////////
///
/// Change the position of the window on screen.
/// Only works for top-level windows
///
/// Left position
/// Top position
////////////////////////////////////////////////////////////
public override void SetPosition(int x, int y)
{
sfRenderWindow_SetPosition(This, x, y);
}
////////////////////////////////////////////////////////////
///
/// Change the size of the rendering region of the window
///
/// New width
/// New height
////////////////////////////////////////////////////////////
public override void SetSize(uint width, uint height)
{
sfRenderWindow_SetSize(This, width, height);
}
////////////////////////////////////////////////////////////
///
/// Show or hide the window
///
/// True to show, false to hide
////////////////////////////////////////////////////////////
public override void Show(bool show)
{
sfRenderWindow_Show(This, show);
}
////////////////////////////////////////////////////////////
///
/// Enable or disable automatic key-repeat.
/// Automatic key-repeat is enabled by default
///
/// True to enable, false to disable
////////////////////////////////////////////////////////////
public override void EnableKeyRepeat(bool enable)
{
sfRenderWindow_EnableKeyRepeat(This, enable);
}
////////////////////////////////////////////////////////////
///
/// 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 override void SetIcon(uint width, uint height, byte[] pixels)
{
unsafe
{
fixed (byte* PixelsPtr = pixels)
{
sfRenderWindow_SetIcon(This, width, height, PixelsPtr);
}
}
}
////////////////////////////////////////////////////////////
///
/// Activate of deactivate the window as the current target
/// for rendering
///
/// True to activate, false to deactivate (true by default)
/// True if operation was successful, false otherwise
////////////////////////////////////////////////////////////
public override bool SetActive(bool active)
{
return sfRenderWindow_SetActive(This, active);
}
////////////////////////////////////////////////////////////
///
/// Limit the framerate to a maximum fixed frequency
///
/// Framerate limit, in frames per seconds (use 0 to disable limit)
////////////////////////////////////////////////////////////
public override void SetFramerateLimit(uint limit)
{
sfRenderWindow_SetFramerateLimit(This, limit);
}
////////////////////////////////////////////////////////////
///
/// Get time elapsed since last frame
///
/// Time elapsed, in seconds
////////////////////////////////////////////////////////////
public override float GetFrameTime()
{
return sfRenderWindow_GetFrameTime(This);
}
////////////////////////////////////////////////////////////
///
/// Change the joystick threshold, ie. the value below which
/// no move event will be generated
///
/// New threshold, in range [0, 100]
////////////////////////////////////////////////////////////
public override void SetJoystickThreshold(float threshold)
{
sfRenderWindow_SetJoystickThreshold(This, threshold);
}
////////////////////////////////////////////////////////////
///
/// Default view of the window
///
////////////////////////////////////////////////////////////
public View DefaultView
{
get {return myDefaultView;}
}
////////////////////////////////////////////////////////////
///
/// Current view active in the window
///
////////////////////////////////////////////////////////////
public View CurrentView
{
get {return myCurrentView;}
set {myCurrentView = value; sfRenderWindow_SetView(This, value.This);}
}
////////////////////////////////////////////////////////////
///
/// Convert a point in window coordinates into view coordinates
/// using the current view of the window
///
/// X coordinate of the point to convert, relative to the window
/// Y coordinate of the point to convert, relative to the window
/// Converted point
////////////////////////////////////////////////////////////
public Vector2 ConvertCoords(uint windowX, uint windowY)
{
return ConvertCoords(windowX, windowY, myCurrentView);
}
////////////////////////////////////////////////////////////
///
/// Convert a point in window coordinates into view coordinates
///
/// X coordinate of the point to convert, relative to the window
/// Y coordinate of the point to convert, relative to the window
/// Target view to convert the point to
/// Converted point
////////////////////////////////////////////////////////////
public Vector2 ConvertCoords(uint windowX, uint windowY, View targetView)
{
Vector2 Point;
sfRenderWindow_ConvertCoords(This, windowX, windowY, out Point.X, out Point.Y, targetView.This);
return Point;
}
////////////////////////////////////////////////////////////
///
/// Tell SFML to preserve external OpenGL states, at the expense of
/// more CPU charge. Use this function if you don't want SFML
/// to mess up your own OpenGL states (if any).
/// Don't enable state preservation if not needed, as it will allow
/// SFML to do internal optimizations and improve performances.
/// This parameter is false by default
///
/// True to preserve OpenGL states, false to let SFML optimize
////////////////////////////////////////////////////////////
public void PreserveOpenGLStates(bool preserve)
{
sfRenderWindow_PreserveOpenGLStates(This, preserve);
}
////////////////////////////////////////////////////////////
///
/// Clear the entire window with black color
///
////////////////////////////////////////////////////////////
public void Clear()
{
sfRenderWindow_Clear(This, Color.Black);
}
////////////////////////////////////////////////////////////
///
/// Clear the entire window with a single color
///
/// Color to use to clear the window
////////////////////////////////////////////////////////////
public void Clear(Color color)
{
sfRenderWindow_Clear(This, color);
}
////////////////////////////////////////////////////////////
///
/// Save the content of the window to an image
///
/// Image instance containing the contents of the screen
////////////////////////////////////////////////////////////
public Image Capture()
{
return new Image(sfRenderWindow_Capture(This));
}
////////////////////////////////////////////////////////////
///
/// Draw something into the window
///
/// Object to draw
////////////////////////////////////////////////////////////
public void Draw(Drawable objectToDraw)
{
objectToDraw.Render(this);
}
////////////////////////////////////////////////////////////
///
/// Apply a post-fx to the window
///
/// PostFx to apply
////////////////////////////////////////////////////////////
public void Draw(PostFx postFx)
{
sfRenderWindow_DrawPostFX(This, postFx != null ? postFx.This : IntPtr.Zero);
}
////////////////////////////////////////////////////////////
///
/// Internal function to get the next event
///
/// Variable to fill with the raw pointer to the event structure
/// True if there was an event, false otherwise
////////////////////////////////////////////////////////////
protected override bool GetEvent(out Event eventToFill)
{
return sfRenderWindow_GetEvent(This, out eventToFill);
}
////////////////////////////////////////////////////////////
///
/// Handle the destruction of the object
///
/// Is the GC disposing the object, or is it an explicit call ?
////////////////////////////////////////////////////////////
protected override void Destroy(bool disposing)
{
sfRenderWindow_Destroy(This);
if (disposing)
myDefaultView.Dispose();
}
////////////////////////////////////////////////////////////
///
/// Do common initializations
///
////////////////////////////////////////////////////////////
private void Initialize()
{
myInput = new Input(sfRenderWindow_GetInput(This));
myDefaultView = new View(sfRenderWindow_GetDefaultView(This));
myCurrentView = myDefaultView;
GC.SuppressFinalize(myDefaultView);
}
private View myCurrentView = null;
private View myDefaultView = null;
#region Imports
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern IntPtr sfRenderWindow_Create(VideoMode Mode, string Title, Styles Style, WindowSettings Params);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern IntPtr sfRenderWindow_CreateFromHandle(IntPtr Handle, WindowSettings Params);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern void sfRenderWindow_Destroy(IntPtr This);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern IntPtr sfRenderWindow_GetInput(IntPtr This);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern bool sfRenderWindow_IsOpened(IntPtr This);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern void sfRenderWindow_Close(IntPtr This);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern bool sfRenderWindow_GetEvent(IntPtr This, out Event Evt);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern IntPtr sfRenderWindow_Capture(IntPtr This);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern void sfRenderWindow_Clear(IntPtr This, Color ClearColor);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern void sfRenderWindow_Display(IntPtr This);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern uint sfRenderWindow_GetWidth(IntPtr This);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern uint sfRenderWindow_GetHeight(IntPtr This);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern WindowSettings sfRenderWindow_GetSettings(IntPtr This);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern void sfRenderWindow_UseVerticalSync(IntPtr This, bool Enable);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern void sfRenderWindow_ShowMouseCursor(IntPtr This, bool Show);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern void sfRenderWindow_SetCursorPosition(IntPtr This, uint X, uint Y);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern void sfRenderWindow_SetPosition(IntPtr This, int X, int Y);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern void sfRenderWindow_SetSize(IntPtr This, uint Width, uint Height);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern void sfRenderWindow_Show(IntPtr This, bool Show);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern void sfRenderWindow_EnableKeyRepeat(IntPtr This, bool Enable);
[DllImport("csfml-graphics")]
unsafe static extern void sfRenderWindow_SetIcon(IntPtr This, uint Width, uint Height, byte* Pixels);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern bool sfRenderWindow_SetActive(IntPtr This, bool Active);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern void sfRenderWindow_SetFramerateLimit(IntPtr This, uint Limit);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern float sfRenderWindow_GetFrameTime(IntPtr This);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern void sfRenderWindow_SetJoystickThreshold(IntPtr This, float Threshold);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern void sfRenderWindow_SetView(IntPtr This, IntPtr View);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern IntPtr sfRenderWindow_GetView(IntPtr This);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern IntPtr sfRenderWindow_GetDefaultView(IntPtr This);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern void sfRenderWindow_ConvertCoords(IntPtr This, uint WindowX, uint WindowY, out float ViewX, out float ViewY, IntPtr TargetView);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern void sfRenderWindow_PreserveOpenGLStates(IntPtr This, bool Preserve);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern void sfRenderWindow_DrawPostFX(IntPtr This, IntPtr PostFx);
#endregion
}
}
}