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, RenderTarget
{
////////////////////////////////////////////////////////////
///
/// 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 ContextSettings(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 ContextSettings(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, ContextSettings 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 ContextSettings(24, 8, 0))
{
}
////////////////////////////////////////////////////////////
///
/// Create the window from an existing control
///
/// Platform-specific handle of the control
/// Creation parameters
////////////////////////////////////////////////////////////
public RenderWindow(IntPtr handle, ContextSettings 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 ContextSettings 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);}
}
////////////////////////////////////////////////////////////
///
/// 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 sfRenderWindow_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;
sfRenderWindow_ConvertCoords(This, x, y, out point.X, out point.Y, view.This);
return point;
}
////////////////////////////////////////////////////////////
///
/// 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);
}
////////////////////////////////////////////////////////////
///
/// 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);
}
////////////////////////////////////////////////////////////
///
/// 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()
{
sfRenderWindow_Flush(This);
}
////////////////////////////////////////////////////////////
///
/// 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);
}
////////////////////////////////////////////////////////////
///
/// 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 override bool WaitEvent(out Event eventToFill)
{
return sfRenderWindow_WaitEvent(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, ContextSettings Params);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern IntPtr sfRenderWindow_CreateFromHandle(IntPtr Handle, ContextSettings 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 bool sfRenderWindow_WaitEvent(IntPtr This, out Event Evt);
[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 ContextSettings 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"), SuppressUnmanagedCodeSecurity]
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 bool sfRenderWindow_Flush(IntPtr This);
[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 IntRect sfRenderWindow_GetViewport(IntPtr This, IntPtr TargetView);
[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_DrawPostFX(IntPtr This, IntPtr PostFx);
#endregion
}
}
}