2009-01-29 00:18:34 +08:00
|
|
|
/*
|
2010-01-07 04:37:29 +08:00
|
|
|
* DSFML - SFML Library wrapper for the D programming language.
|
|
|
|
* Copyright (C) 2008 Julien Dagorn (sirjulio13@gmail.com)
|
|
|
|
* Copyright (C) 2010 Andreas Hollandt
|
2009-01-29 00:18:34 +08:00
|
|
|
*
|
2010-01-07 04:37:29 +08:00
|
|
|
* This software is provided 'as-is', without any express or
|
|
|
|
* implied warranty. In no event will the authors be held
|
|
|
|
* liable for any damages arising from the use of this software.
|
2009-01-29 00:18:34 +08:00
|
|
|
*
|
2010-01-07 04:37:29 +08:00
|
|
|
* Permission is granted to anyone to use this software for any purpose,
|
|
|
|
* including commercial applications, and to alter it and redistribute
|
|
|
|
* it freely, subject to the following restrictions:
|
2009-01-29 00:18:34 +08:00
|
|
|
*
|
2010-01-07 04:37:29 +08:00
|
|
|
* 1. The origin of this software must not be misrepresented;
|
|
|
|
* you must not claim that you wrote the original software.
|
|
|
|
* If you use this software in a product, an acknowledgment
|
|
|
|
* in the product documentation would be appreciated but
|
|
|
|
* is not required.
|
2009-01-29 00:18:34 +08:00
|
|
|
*
|
2010-01-07 04:37:29 +08:00
|
|
|
* 2. Altered source versions must be plainly marked as such,
|
|
|
|
* and must not be misrepresented as being the original software.
|
2009-01-29 00:18:34 +08:00
|
|
|
*
|
2010-01-07 04:37:29 +08:00
|
|
|
* 3. This notice may not be removed or altered from any
|
|
|
|
* source distribution.
|
2009-01-29 00:18:34 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
module dsfml.graphics.renderwindow;
|
|
|
|
|
2010-01-08 06:00:45 +08:00
|
|
|
import dsfml.graphics.color,
|
2010-01-07 04:25:45 +08:00
|
|
|
dsfml.graphics.rect,
|
|
|
|
dsfml.graphics.shader,
|
|
|
|
dsfml.graphics.view,
|
2010-01-21 05:46:32 +08:00
|
|
|
dsfml.graphics.idrawable,
|
|
|
|
dsfml.graphics.irendertarget;
|
2010-01-07 04:25:45 +08:00
|
|
|
|
|
|
|
import dsfml.window.event,
|
|
|
|
dsfml.window.input,
|
|
|
|
dsfml.window.videomode,
|
|
|
|
dsfml.window.window,
|
|
|
|
dsfml.window.windowhandle;
|
|
|
|
|
|
|
|
import dsfml.system.common,
|
|
|
|
dsfml.system.stringutil,
|
2010-07-24 08:30:30 +08:00
|
|
|
dsfml.system.vector;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
/**
|
2010-03-16 10:04:52 +08:00
|
|
|
* Simple wrapper for Window that allows easy 2D rendering.
|
|
|
|
*/
|
2010-01-21 05:46:32 +08:00
|
|
|
class RenderWindow : Window, IRenderTarget
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
2010-01-07 04:37:29 +08:00
|
|
|
private:
|
2010-01-07 04:25:45 +08:00
|
|
|
View m_view = null;
|
|
|
|
View m_defaultView = null;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
2010-03-16 10:04:52 +08:00
|
|
|
* Construct the window
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* mode = Video mode to use
|
|
|
|
* title = Title of the window
|
|
|
|
* windowStyle = Window style (Resize | Close by default)
|
|
|
|
* settings = Context settings (default is default ContextSettings values)
|
|
|
|
*/
|
|
|
|
this(VideoMode mode, string title, Style windowStyle = Style.Default, ContextSettings settings = ContextSettings())
|
2010-01-07 04:37:29 +08:00
|
|
|
{
|
|
|
|
super(sfRenderWindow_Create(mode, toStringz(title), windowStyle, &settings));
|
|
|
|
m_input = new Input(sfRenderWindow_GetInput(m_ptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-03-16 10:04:52 +08:00
|
|
|
* Construct the window from an existing control
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* handle = Platform-specific handle of the control
|
|
|
|
* settings = Context settings (default is default ContextSettings values)
|
|
|
|
*/
|
2010-01-07 04:37:29 +08:00
|
|
|
this(WindowHandle handle, ContextSettings settings = ContextSettings())
|
|
|
|
{
|
|
|
|
super(sfRenderWindow_CreateFromHandle(handle, &settings));
|
|
|
|
m_input = new Input(sfRenderWindow_GetInput(m_ptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
override void dispose()
|
|
|
|
{
|
|
|
|
sfRenderWindow_Destroy(m_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-03-16 10:04:52 +08:00
|
|
|
* Create (or recreate) the window
|
|
|
|
*
|
|
|
|
* Input created with getInput will become invalid.
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* mode = Video mode to use
|
|
|
|
* title = Title of the window
|
|
|
|
* windowStyle = Window style (Resize | Close by default)
|
|
|
|
* settings = Context settings (default is default ContextSettings values)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
override void create(VideoMode mode, string title, Style windowStyle = Style.Default, ContextSettings settings = ContextSettings())
|
2010-01-07 04:37:29 +08:00
|
|
|
{
|
|
|
|
if (m_ptr !is null)
|
|
|
|
dispose();
|
|
|
|
|
|
|
|
m_ptr = sfRenderWindow_Create(mode, toStringz(title), windowStyle, &settings);
|
|
|
|
m_input = new Input(sfRenderWindow_GetInput(m_ptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-03-16 10:04:52 +08:00
|
|
|
* Create (or recreate) the window from an existing control
|
|
|
|
*
|
|
|
|
* Input created with getInput become invalid.
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* handle = Platform-specific handle of the control
|
|
|
|
* settings = Context settings (default is default ContextSettings values)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
override void create(WindowHandle handle, ContextSettings settings = ContextSettings())
|
2010-01-07 04:37:29 +08:00
|
|
|
{
|
|
|
|
if (m_ptr !is null)
|
|
|
|
dispose();
|
|
|
|
|
|
|
|
m_ptr = sfRenderWindow_CreateFromHandle(handle, &settings);
|
|
|
|
m_input = new Input(sfRenderWindow_GetInput(m_ptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-03-16 10:04:52 +08:00
|
|
|
* Draw a sprite, shape or text on the window with a shader
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* drawable = IDrawable to draw
|
|
|
|
* shader = Shader to use
|
|
|
|
*/
|
2010-01-07 04:25:45 +08:00
|
|
|
void draw(IDrawable drawable, Shader shader)
|
|
|
|
{
|
|
|
|
drawable.renderWithShader(this, shader);
|
|
|
|
}
|
2010-01-07 04:37:29 +08:00
|
|
|
|
|
|
|
/**
|
2010-03-16 10:04:52 +08:00
|
|
|
* Draw a sprite, shape or text
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* drawable = IDrawable to draw
|
|
|
|
*/
|
2010-01-07 04:37:29 +08:00
|
|
|
void draw(IDrawable drawable)
|
|
|
|
{
|
|
|
|
drawable.render(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-03-16 10:04:52 +08:00
|
|
|
* Clear the screen with the given color.
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* col = Fill color
|
|
|
|
*/
|
2010-01-07 04:37:29 +08:00
|
|
|
void clear(Color col = Color.BLACK)
|
|
|
|
{
|
|
|
|
sfRenderWindow_Clear(m_ptr, col);
|
|
|
|
}
|
|
|
|
|
2010-03-16 10:04:52 +08:00
|
|
|
|
|
|
|
|
2010-01-07 04:37:29 +08:00
|
|
|
/**
|
2010-03-16 10:04:52 +08:00
|
|
|
* Convert a point in window coordinates into view coordinates
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* windowX = X coordinate of the point to convert, relative to the window
|
|
|
|
* windowY = Y coordinate of the point to convert, relative to the window
|
|
|
|
* targetView = Target view to convert the point to (pass NULL to use the current view)
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* Converted point
|
|
|
|
*/
|
|
|
|
Vector2f convertCoords(uint windowX, uint windowY, View targetView = null)
|
|
|
|
{
|
|
|
|
Vector2f vec;
|
|
|
|
sfRenderWindow_ConvertCoords(m_ptr, windowX, windowY, &vec.x, &vec.y, targetView is null ? null : targetView.nativePointer);
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the current OpenGL render states and matrices
|
|
|
|
*/
|
|
|
|
void saveGLStates()
|
|
|
|
{
|
|
|
|
sfRenderWindow_SaveGLStates(m_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restore the previously saved OpenGL render states and matrices
|
|
|
|
*/
|
|
|
|
void restoreGLStates()
|
|
|
|
{
|
|
|
|
sfRenderWindow_RestoreGLStates(m_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
@property
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Change the current active view.
|
|
|
|
* The current view is defined with the initial size of the window
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* newView = Pointer to the new view (pass getDefaultView to set the default view)
|
|
|
|
*/
|
|
|
|
void view(View newView)
|
2010-01-07 04:37:29 +08:00
|
|
|
{
|
|
|
|
if (m_view !is null)
|
|
|
|
{
|
|
|
|
m_view.setHandled(false);
|
|
|
|
}
|
|
|
|
|
2010-03-16 07:52:54 +08:00
|
|
|
sfRenderWindow_SetView(m_ptr, newView.nativePointer);
|
2010-01-07 04:37:29 +08:00
|
|
|
|
|
|
|
m_view = newView;
|
|
|
|
m_view.setHandled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-03-16 10:04:52 +08:00
|
|
|
* Get the current view rectangle
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* current view rectangle, in global coordinates
|
|
|
|
*/
|
|
|
|
View view()
|
2010-01-07 04:37:29 +08:00
|
|
|
{
|
|
|
|
if (m_view is null)
|
|
|
|
{
|
2010-03-16 10:04:52 +08:00
|
|
|
SFMLClass cView = sfRenderWindow_GetView(m_ptr);
|
2010-01-07 04:37:29 +08:00
|
|
|
m_view = new View(cView, true);
|
|
|
|
}
|
|
|
|
return m_view;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-03-16 10:04:52 +08:00
|
|
|
* Get the default view
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* default view
|
|
|
|
*/
|
|
|
|
View defaultView()
|
2010-01-07 04:37:29 +08:00
|
|
|
{
|
|
|
|
if (m_defaultView is null)
|
|
|
|
{
|
2010-03-16 10:04:52 +08:00
|
|
|
SFMLClass cView = sfRenderWindow_GetDefaultView(m_ptr);
|
2010-01-07 04:37:29 +08:00
|
|
|
m_defaultView = new View(cView, true);
|
|
|
|
}
|
|
|
|
return m_defaultView;
|
|
|
|
}
|
2010-03-16 10:04:52 +08:00
|
|
|
|
2010-01-21 05:46:32 +08:00
|
|
|
/**
|
2010-03-16 10:04:52 +08:00
|
|
|
* Return the width of the rendering region of a renderwindow
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* Width in pixels
|
2010-01-21 05:46:32 +08:00
|
|
|
*/
|
2010-03-16 10:04:52 +08:00
|
|
|
override uint width()
|
2010-01-21 05:46:32 +08:00
|
|
|
{
|
|
|
|
return sfRenderWindow_GetWidth(m_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-03-16 10:04:52 +08:00
|
|
|
* Return the height of the rendering region of a renderwindow
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* Height in pixels
|
|
|
|
*/
|
|
|
|
override uint height()
|
2010-01-08 02:07:22 +08:00
|
|
|
{
|
2010-01-21 05:46:32 +08:00
|
|
|
return sfRenderWindow_GetHeight(m_ptr);
|
|
|
|
}
|
|
|
|
|
2010-03-16 10:04:52 +08:00
|
|
|
/**
|
|
|
|
* Get the viewport of a view applied to this target
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* view = Target view
|
|
|
|
* Returns:
|
|
|
|
* Viewport rectangle, expressed in pixels in the current target
|
|
|
|
*/
|
|
|
|
IntRect viewport(View v = null) // TODO: is there a need to accept other Views than the currently assigned one?
|
2010-01-21 05:46:32 +08:00
|
|
|
{
|
2010-03-16 10:04:52 +08:00
|
|
|
return sfRenderWindow_GetViewport(m_ptr, v is null ? m_view.nativePointer : v.nativePointer);
|
2010-01-08 02:07:22 +08:00
|
|
|
}
|
2010-03-16 10:04:52 +08:00
|
|
|
}
|
2010-01-08 06:00:45 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
static extern(C)
|
|
|
|
{
|
2010-03-16 10:04:52 +08:00
|
|
|
SFMLClass function(VideoMode, cchar*, Style, ContextSettings*)sfRenderWindow_Create;
|
|
|
|
SFMLClass function(WindowHandle, ContextSettings*) sfRenderWindow_CreateFromHandle;
|
|
|
|
void function(SFMLClass) sfRenderWindow_Destroy;
|
|
|
|
SFMLClass function(SFMLClass) sfRenderWindow_GetInput;
|
|
|
|
bool function(SFMLClass) sfRenderWindow_IsOpened;
|
|
|
|
uint function(SFMLClass) sfRenderWindow_GetWidth;
|
|
|
|
uint function(SFMLClass) sfRenderWindow_GetHeight;
|
2010-01-08 06:00:45 +08:00
|
|
|
|
|
|
|
/*
|
2010-03-16 10:04:52 +08:00
|
|
|
void function(SFMLClass, SFMLClass) sfRenderWindow_DrawSprite;
|
|
|
|
void function(SFMLClass, SFMLClass) sfRenderWindow_DrawShape;
|
|
|
|
void function(SFMLClass, SFMLClass) sfRenderWindow_DrawText;
|
2010-01-08 06:00:45 +08:00
|
|
|
|
2010-03-16 10:04:52 +08:00
|
|
|
void function(SFMLClass, SFMLClass, SFMLClass) sfRenderWindow_DrawSpriteWithShader;
|
|
|
|
void function(SFMLClass, SFMLClass, SFMLClass) sfRenderWindow_DrawShapeWithShader;
|
|
|
|
void function(SFMLClass, SFMLClass, SFMLClass) sfRenderWindow_DrawTextWithShader;
|
2010-01-08 06:00:45 +08:00
|
|
|
*/
|
|
|
|
|
2010-03-16 10:04:52 +08:00
|
|
|
SFMLClass function(SFMLClass) sfRenderWindow_Capture;
|
|
|
|
void function(SFMLClass, Color) sfRenderWindow_Clear;
|
|
|
|
void function(SFMLClass, SFMLClass) sfRenderWindow_SetView;
|
|
|
|
SFMLClass function(SFMLClass) sfRenderWindow_GetView;
|
|
|
|
SFMLClass function(SFMLClass) sfRenderWindow_GetDefaultView;
|
|
|
|
void function(SFMLClass, uint, uint, float*, float*, SFMLClass) sfRenderWindow_ConvertCoords;
|
2010-01-08 06:00:45 +08:00
|
|
|
|
|
|
|
// DSFML2
|
2010-03-16 10:04:52 +08:00
|
|
|
void function(SFMLClass) sfRenderWindow_SaveGLStates;
|
|
|
|
void function(SFMLClass) sfRenderWindow_RestoreGLStates;
|
|
|
|
IntRect function(SFMLClass, SFMLClass) sfRenderWindow_GetViewport;
|
2010-01-08 06:00:45 +08:00
|
|
|
}
|
|
|
|
|
2010-01-21 05:46:32 +08:00
|
|
|
mixin(loadFromSharedLib2("csfml-graphics", "sfRenderWindow", "Create", "CreateFromHandle",
|
|
|
|
"Destroy", "GetInput", "Clear", "SetView", "GetView", "GetDefaultView", "ConvertCoords",
|
|
|
|
"GetWidth", "GetHeight",
|
|
|
|
// DSFML2
|
|
|
|
"SaveGLStates", "RestoreGLStates", "GetViewport"));
|
2010-01-08 06:00:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
static ~this()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2010-01-07 04:25:45 +08:00
|
|
|
}
|