Added viewport handling in sf::View

Upgraded SFML.Net project files to VS2008

git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1155 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
LaurentGom 2009-06-26 13:24:29 +00:00
parent 1bb96087ad
commit 7cc00085d8
24 changed files with 361 additions and 102 deletions

View file

@ -93,10 +93,19 @@ void RenderTarget::Draw(const Drawable& Object)
SetRenderStates();
}
// Set the window viewport and transform matrices
GLCheck(glViewport(0, 0, GetWidth(), GetHeight()));
GLCheck(glMatrixMode(GL_PROJECTION)); GLCheck(glLoadMatrixf(myCurrentView->GetMatrix().Get4x4Elements()));
GLCheck(glMatrixMode(GL_MODELVIEW)); GLCheck(glLoadIdentity());
// Setup the viewport
const FloatRect& Viewport = myCurrentView->GetViewport();
int Left = static_cast<int>(0.5f + GetWidth() * Viewport.Left);
int Top = static_cast<int>(0.5f + GetHeight() * (1.f - Viewport.Bottom));
int Width = static_cast<int>(0.5f + GetWidth() * Viewport.GetSize().x);
int Height = static_cast<int>(0.5f + GetHeight() * Viewport.GetSize().y);
GLCheck(glViewport(Left, Top, Width, Height));
// Setup the transform matrices
GLCheck(glMatrixMode(GL_PROJECTION));
GLCheck(glLoadMatrixf(myCurrentView->GetMatrix().Get4x4Elements()));
GLCheck(glMatrixMode(GL_MODELVIEW));
GLCheck(glLoadIdentity());
// Let the object draw itself
Object.Draw(*this);

View file

@ -135,22 +135,33 @@ Image RenderWindow::Capture() const
////////////////////////////////////////////////////////////
/// Convert a point in window coordinates into view coordinates
/// This version uses the current view of the window
////////////////////////////////////////////////////////////
sf::Vector2f RenderWindow::ConvertCoords(unsigned int WindowX, unsigned int WindowY, const View* TargetView) const
sf::Vector2f RenderWindow::ConvertCoords(unsigned int WindowX, unsigned int WindowY) const
{
// Use the current view if none has been passed
if (!TargetView)
TargetView = &GetView();
return ConvertCoords(WindowX, WindowY, GetView());
}
////////////////////////////////////////////////////////////
/// Convert a point in window coordinates into view coordinates
/// This version uses the given view
////////////////////////////////////////////////////////////
sf::Vector2f RenderWindow::ConvertCoords(unsigned int WindowX, unsigned int WindowY, const View& TargetView) const
{
// First, convert from viewport coordinates to homogeneous coordinates
const FloatRect& Viewport = TargetView.GetViewport();
int Left = static_cast<int>(0.5f + GetWidth() * Viewport.Left);
int Top = static_cast<int>(0.5f + GetHeight() * Viewport.Top);
int Width = static_cast<int>(0.5f + GetWidth() * Viewport.GetSize().x);
int Height = static_cast<int>(0.5f + GetHeight() * Viewport.GetSize().y);
// First, convert from viewport coordinates to homogeneous coordinates:
// --> [0, Width] to [-1, 1]
// --> [0, Height] to [1, -1]
Vector2f Coords;
Coords.x = -1.f + 2.f * WindowX / GetWidth();
Coords.y = 1.f - 2.f * WindowY / GetHeight();
Coords.x = -1.f + 2.f * (static_cast<int>(WindowX) - Left) / Width;
Coords.y = 1.f - 2.f * (static_cast<int>(WindowY) - Top) / Height;
// Then transform by the inverse of the view matrix
return TargetView->GetInverseMatrix().Transform(Coords);
return TargetView.GetInverseMatrix().Transform(Coords);
}

View file

@ -38,6 +38,7 @@ View::View() :
myCenter (),
mySize (),
myRotation (0),
myViewport (0, 0, 1, 1),
myNeedUpdate (true),
myNeedInvUpdate(true)
{
@ -52,6 +53,7 @@ View::View(const FloatRect& Rectangle) :
myCenter (),
mySize (),
myRotation (0),
myViewport (0, 0, 1, 1),
myNeedUpdate (true),
myNeedInvUpdate(true)
{
@ -66,6 +68,7 @@ View::View(const Vector2f& Center, const Vector2f& Size) :
myCenter (Center),
mySize (Size),
myRotation (0),
myViewport (0, 0, 1, 1),
myNeedUpdate (true),
myNeedInvUpdate(true)
{
@ -125,6 +128,15 @@ void View::SetRotation(float Angle)
}
////////////////////////////////////////////////////////////
/// Set the target viewport
////////////////////////////////////////////////////////////
void View::SetViewport(const FloatRect& Viewport)
{
myViewport = Viewport;
}
////////////////////////////////////////////////////////////
/// Reset the view to the given rectangle
////////////////////////////////////////////////////////////
@ -155,6 +167,24 @@ const Vector2f& View::GetSize() const
}
////////////////////////////////////////////////////////////
/// Get the current rotation
////////////////////////////////////////////////////////////
float View::GetRotation() const
{
return myRotation;
}
////////////////////////////////////////////////////////////
/// Get the target viewport
////////////////////////////////////////////////////////////
const FloatRect& View::GetViewport() const
{
return myViewport;
}
////////////////////////////////////////////////////////////
/// Move the view
////////////////////////////////////////////////////////////