D:/Programmation/Cpp/SFML/src/SFML/Graphics/RenderWindow.cpp

00001 
00002 //
00003 // SFML - Simple and Fast Multimedia Library
00004 // Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
00005 //
00006 // This software is provided 'as-is', without any express or implied warranty.
00007 // In no event will the authors be held liable for any damages arising from the use of this software.
00008 //
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it freely,
00011 // subject to the following restrictions:
00012 //
00013 // 1. The origin of this software must not be misrepresented;
00014 //    you must not claim that you wrote the original software.
00015 //    If you use this software in a product, an acknowledgment
00016 //    in the product documentation would be appreciated but is not required.
00017 //
00018 // 2. Altered source versions must be plainly marked as such,
00019 //    and must not be misrepresented as being the original software.
00020 //
00021 // 3. This notice may not be removed or altered from any source distribution.
00022 //
00024 
00026 // Headers
00028 #include <SFML/Graphics/RenderWindow.hpp>
00029 #include <SFML/Graphics/Drawable.hpp>
00030 #include <SFML/Graphics/GraphicsDevice.hpp>
00031 #include <SFML/Graphics/Image.hpp>
00032 #include <SFML/Graphics/OpenGL.hpp>
00033 #include <iostream>
00034 
00035 
00039 sfRenderWindow::sfRenderWindow() :
00040 myBackgroundColor(sfColor::Black),
00041 myOpenGLMode     (false)
00042 {
00043 
00044 }
00045 
00046 
00050 sfRenderWindow::sfRenderWindow(sfVideoMode Mode, const std::string& Title, bool Fullscreen) :
00051 myBackgroundColor(sfColor::Black),
00052 myOpenGLMode     (false)
00053 {
00054     Create(Mode, Title, Fullscreen);
00055 }
00056 
00057 
00061 sfRenderWindow::sfRenderWindow(void* Handle) :
00062 myBackgroundColor(sfColor::Black),
00063 myOpenGLMode     (false)
00064 {
00065     Create(Handle);
00066 }
00067 
00068 
00072 sfRenderWindow::~sfRenderWindow()
00073 {
00074     // Shutdown graphics device
00075     sf_private::sfGraphicsDevice::Shutdown();
00076 }
00077 
00078 
00082 void sfRenderWindow::Create(sfVideoMode Mode, const std::string& Title, bool Fullscreen)
00083 {
00084     // Let base class do the job
00085     sfWindow::Create(Mode, Title, Fullscreen);
00086 
00087     // Initialize rendering window state
00088     Initialize();
00089 }
00090 
00091 
00095 void sfRenderWindow::Create(void* Handle)
00096 {
00097     // Let base class do the job
00098     sfWindow::Create(Handle);
00099 
00100     // Initialize rendering window state
00101     Initialize();
00102 }
00103 
00104 
00108 void sfRenderWindow::Display()
00109 {
00110     // Discard display if we are in OpenGL custom rendering mode (it would mess up the render states)
00111     if (myOpenGLMode)
00112     {
00113         std::cerr << "You cannot display a window while in OpenGL mode ; call EndOpenGL() before calling sfWindow::Display()" << std::endl;
00114         return;
00115     }
00116 
00117     // Set our window as the current target for rendering
00118     if (sfWindow::SetCurrent())
00119     {
00120         // Display backbuffer on screen
00121         sfWindow::Display();
00122 
00123         // Clear background for next frame
00124         sfGLCheck(glClearColor(myBackgroundColor.Red   / 255.f,
00125                                myBackgroundColor.Green / 255.f,
00126                                myBackgroundColor.Blue  / 255.f,
00127                                myBackgroundColor.Alpha / 255.f));
00128         sfGLCheck(glClear(GL_COLOR_BUFFER_BIT));
00129     }
00130 }
00131 
00132 
00136 void sfRenderWindow::Draw(sfDrawable& Object)
00137 {
00138     // Discard draw if we are in OpenGL custom rendering mode (it would mess up the render states)
00139     if (myOpenGLMode)
00140     {
00141         std::cerr << "You cannot use SFML rendering while in OpenGL mode ; call EndOpenGL() before drawing your SFML object" << std::endl;
00142         return;
00143     }
00144 
00145     // Set our window as the current target for rendering
00146     if (sfWindow::SetCurrent())
00147     {
00148         // Reset transformations
00149         sfGLCheck(glMatrixMode(GL_MODELVIEW));
00150         sfGLCheck(glLoadIdentity());
00151 
00152         // Let the object render itself
00153         Object.Render(*this);
00154     }
00155 }
00156 
00157 
00161 void sfRenderWindow::Capture(const std::string& Filename) const
00162 {
00163     // Get window dimensions
00164     const unsigned int Width  = GetWidth();
00165     const unsigned int Height = GetHeight();
00166 
00167     // Set our window as the current target for rendering
00168     if (sfWindow::SetCurrent())
00169     {
00170         // Get pixels from backbuffer
00171         std::vector<sfUint32> Pixels(Width * Height);
00172         glReadPixels(0, 0, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, &Pixels[0]);
00173 
00174         // Create an image from it and save it to disk
00175         sfImage Screen(Width, Height, &Pixels[0]);
00176         Screen.SaveToFile(Filename);
00177     }
00178 }
00179 
00180 
00184 void sfRenderWindow::SetBackgroundColor(const sfColor& Color)
00185 {
00186     myBackgroundColor = Color;
00187 }
00188 
00189 
00193 bool sfRenderWindow::GetEvent(sfEvent& Event)
00194 {
00195     // Forward to base class
00196     if (sfWindow::GetEvent(Event))
00197     {
00198         // Intercept resize events, because we have to adjust the viewport to new size
00199         if (Event.Type == sfEvent::Resize)
00200             sfGLCheck(glViewport(0, 0, Event.Size.Width, Event.Size.Height));
00201 
00202         return true;
00203     }
00204 
00205     return false;
00206 }
00207 
00208 
00212 void sfRenderWindow::SetView(const sfView* View)
00213 {
00214     // Discard display if we are in OpenGL custom rendering mode (it would mess up the render states)
00215     if (myOpenGLMode)
00216     {
00217         std::cerr << "You cannot change the view while in OpenGL mode ; call EndOpenGL() before setting a new SFML view" << std::endl;
00218         return;
00219     }
00220 
00221     // Set our window as the current target for rendering
00222     if (sfWindow::SetCurrent())
00223     {
00224         // If a null pointer is passed, switch to default view
00225         if (View == NULL)
00226             View = &myDefaultView;
00227 
00228         // Compute view rectangle coordinates
00229         float X = View->Left + View->Width  / 2;
00230         float Y = View->Top  + View->Height / 2;
00231         float HalfWidth  = View->Width  / (2 * View->Zoom);
00232         float HalfHeight = View->Height / (2 * View->Zoom);
00233 
00234         // Store view rectangle for optimization purpose
00235         myCurrentRect = sfFloatRect(X - HalfWidth, Y - HalfHeight, X + HalfWidth, Y + HalfHeight);
00236 
00237         // Update the projection matrix according to the new view
00238         sfGLCheck(glMatrixMode(GL_PROJECTION));
00239         sfGLCheck(glLoadIdentity());
00240         sfGLCheck(glOrtho(myCurrentRect.Left, myCurrentRect.Right, myCurrentRect.Bottom, myCurrentRect.Top, -1, 1));
00241     }
00242 }
00243 
00244 
00248 const sfFloatRect& sfRenderWindow::GetViewRect() const
00249 {
00250     return myCurrentRect;
00251 }
00252 
00253 
00257 void sfRenderWindow::BeginOpenGL()
00258 {
00259     if (!myOpenGLMode && sfWindow::SetCurrent())
00260     {
00261         // Save current projection matrix
00262         sfGLCheck(glMatrixMode(GL_PROJECTION));
00263         sfGLCheck(glPushMatrix());
00264 
00265         // Save current render states
00266         sfGLCheck(glPushAttrib(GL_ALL_ATTRIB_BITS));
00267 
00268         // Set OpenGL mode flag
00269         myOpenGLMode = true;
00270     }
00271 }
00272 
00273 
00277 void sfRenderWindow::EndOpenGL()
00278 {
00279     if (myOpenGLMode && sfWindow::SetCurrent())
00280     {
00281         // Restore render states
00282         sfGLCheck(glPopAttrib());
00283 
00284         // Restore projection matrix
00285         sfGLCheck(glMatrixMode(GL_PROJECTION));
00286         sfGLCheck(glPopMatrix());
00287 
00288         // Clear OpenGL mode flag
00289         myOpenGLMode = false;
00290     }
00291 }
00292 
00293 
00297 void sfRenderWindow::Initialize()
00298 {
00299     if (sfWindow::SetCurrent())
00300     {
00301         // Once the window is created, we have a rendering context so we can initialize the graphics device
00302         sf_private::sfGraphicsDevice::Initialize();
00303 
00304         // Set default OpenGL states
00305         sfGLCheck(glEnable(GL_ALPHA_TEST));
00306         sfGLCheck(glAlphaFunc(GL_GREATER, 0));
00307         sfGLCheck(glEnable(GL_BLEND));
00308         sfGLCheck(glEnable(GL_TEXTURE_2D));
00309         sfGLCheck(glDisable(GL_LIGHTING));
00310         sfGLCheck(glDisable(GL_DEPTH_TEST));
00311         sfGLCheck(glDisable(GL_CULL_FACE));
00312         sfGLCheck(glShadeModel(GL_SMOOTH));
00313         sfGLCheck(glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST));
00314 
00315         // Setup default view
00316         myDefaultView.Left   = 0;
00317         myDefaultView.Top    = 0;
00318         myDefaultView.Width  = static_cast<float>(GetWidth());
00319         myDefaultView.Height = static_cast<float>(GetHeight());
00320         myDefaultView.Zoom   = 1.f;
00321         SetView(NULL);
00322     }
00323 }

Generated for SFML by  doxygen 1.5.2