00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00024
00026
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
00075 sf_private::sfGraphicsDevice::Shutdown();
00076 }
00077
00078
00082 void sfRenderWindow::Create(sfVideoMode Mode, const std::string& Title, bool Fullscreen)
00083 {
00084
00085 sfWindow::Create(Mode, Title, Fullscreen);
00086
00087
00088 Initialize();
00089 }
00090
00091
00095 void sfRenderWindow::Create(void* Handle)
00096 {
00097
00098 sfWindow::Create(Handle);
00099
00100
00101 Initialize();
00102 }
00103
00104
00108 void sfRenderWindow::Display()
00109 {
00110
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
00118 if (sfWindow::SetCurrent())
00119 {
00120
00121 sfWindow::Display();
00122
00123
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
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
00146 if (sfWindow::SetCurrent())
00147 {
00148
00149 sfGLCheck(glMatrixMode(GL_MODELVIEW));
00150 sfGLCheck(glLoadIdentity());
00151
00152
00153 Object.Render(*this);
00154 }
00155 }
00156
00157
00161 void sfRenderWindow::Capture(const std::string& Filename) const
00162 {
00163
00164 const unsigned int Width = GetWidth();
00165 const unsigned int Height = GetHeight();
00166
00167
00168 if (sfWindow::SetCurrent())
00169 {
00170
00171 std::vector<sfUint32> Pixels(Width * Height);
00172 glReadPixels(0, 0, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, &Pixels[0]);
00173
00174
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
00196 if (sfWindow::GetEvent(Event))
00197 {
00198
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
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
00222 if (sfWindow::SetCurrent())
00223 {
00224
00225 if (View == NULL)
00226 View = &myDefaultView;
00227
00228
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
00235 myCurrentRect = sfFloatRect(X - HalfWidth, Y - HalfHeight, X + HalfWidth, Y + HalfHeight);
00236
00237
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
00262 sfGLCheck(glMatrixMode(GL_PROJECTION));
00263 sfGLCheck(glPushMatrix());
00264
00265
00266 sfGLCheck(glPushAttrib(GL_ALL_ATTRIB_BITS));
00267
00268
00269 myOpenGLMode = true;
00270 }
00271 }
00272
00273
00277 void sfRenderWindow::EndOpenGL()
00278 {
00279 if (myOpenGLMode && sfWindow::SetCurrent())
00280 {
00281
00282 sfGLCheck(glPopAttrib());
00283
00284
00285 sfGLCheck(glMatrixMode(GL_PROJECTION));
00286 sfGLCheck(glPopMatrix());
00287
00288
00289 myOpenGLMode = false;
00290 }
00291 }
00292
00293
00297 void sfRenderWindow::Initialize()
00298 {
00299 if (sfWindow::SetCurrent())
00300 {
00301
00302 sf_private::sfGraphicsDevice::Initialize();
00303
00304
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
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 }