Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

OgreSDLWindow.cpp

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright © 2000-2002 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 
00026 #include "OgreSDLWindow.h"
00027 #include "OgreRoot.h"
00028 #include "OgreRenderSystem.h"
00029 #include "OgreImageCodec.h"
00030 #include "OgreException.h"
00031 #include "OgreLogManager.h"
00032 
00033 #if OGRE_PLATFORM == PLATFORM_WIN32
00034 #   include <windows.h>
00035 #   include <wingdi.h>
00036 #   include <GL/gl.h>
00037 #   define GL_GLEXT_PROTOTYPES
00038 #   include "glprocs.h"
00039 #   include <GL/glu.h>
00040 #elif OGRE_PLATFORM == PLATFORM_LINUX
00041 #   include <GL/gl.h>
00042 #   include <GL/glu.h>
00043 #elif OGRE_PLATFORM == PLATFORM_APPLE
00044 #   include <OpenGL/gl.h>
00045 #   define GL_EXT_texture_env_combine 1
00046 #   include <OpenGL/glext.h>
00047 #   include <OpenGL/glu.h>
00048 #endif
00049 
00050 namespace Ogre {
00051 
00052     SDLWindow::SDLWindow() :
00053         mScreen(NULL), mActive(false), mClosed(false)
00054     {
00055     }
00056 
00057     SDLWindow::~SDLWindow()
00058     {
00059         if (mScreen != NULL)
00060             SDL_FreeSurface(mScreen);
00061 
00062     }
00063 
00064     void SDLWindow::create(const String& name, unsigned int width, unsigned int height, unsigned int colourDepth,
00065                            bool fullScreen, int left, int top, bool depthBuffer,
00066                            void* miscParam, ...)
00067     {
00068         LogManager::getSingleton().logMessage("SDLWindow::create", LML_TRIVIAL);
00069         SDL_Surface* screen;
00070         int flags = SDL_OPENGL | SDL_HWPALETTE;
00071         
00072         SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
00073         // request good stencil size if 32-bit colour
00074         if (colourDepth == 32 && depthBuffer)
00075         {
00076             SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 8);
00077         }
00078         
00079         if (fullScreen)
00080             flags |= SDL_FULLSCREEN;
00081 
00082         LogManager::getSingleton().logMessage("Create window", LML_TRIVIAL);
00083         screen = SDL_SetVideoMode(width, height, colourDepth, flags);
00084         if (!screen)
00085         {
00086             LogManager::getSingleton().logMessage(LML_CRITICAL, "Could not make screen: %s.", SDL_GetError());
00087             exit(1);
00088         }
00089         LogManager::getSingleton().logMessage("screen is valid", LML_TRIVIAL);
00090         mScreen = screen;
00091 
00092         mName = name;
00093 
00094         mWidth = width;
00095         mHeight = height;
00096 
00097         mActive = true;
00098 
00099         if (!fullScreen)
00100             SDL_WM_SetCaption(name.c_str(), 0);
00101 
00102     }
00103 
00104     void SDLWindow::destroy(void)
00105     {
00106         SDL_FreeSurface(mScreen);
00107         mScreen = NULL;
00108         mActive = false;
00109 
00110         Root::getSingleton().getRenderSystem()->detachRenderTarget( this->getName() );
00111     }
00112 
00113     bool SDLWindow::isActive() const
00114     {
00115         return mActive;
00116     }
00117 
00118     bool SDLWindow::isClosed() const
00119     {
00120         return mClosed;
00121     }
00122 
00123     void SDLWindow::reposition(int left, int top)
00124     {
00125         // XXX FIXME
00126     }
00127 
00128     void SDLWindow::resize(unsigned int width, unsigned int height)
00129     {
00130         for (ViewportList::iterator it = mViewportList.begin();
00131              it != mViewportList.end(); ++it)
00132         {
00133             (*it).second->_updateDimensions();
00134         }
00135     }
00136 
00137     void SDLWindow::swapBuffers(bool waitForVSync)
00138     {
00139         SDL_GL_SwapBuffers();
00140         // XXX More?
00141     }
00142 
00143     void SDLWindow::writeContentsToFile(const String& filename)
00144     {
00145         ImageCodec::ImageData imgData;
00146         imgData.width = mWidth;
00147         imgData.height = mHeight;
00148         imgData.format = PF_R8G8B8;
00149 
00150         // Allocate buffer 
00151         uchar* pBuffer = new uchar[mWidth * mHeight * 3];
00152 
00153         // Read pixels
00154         // I love GL: it does all the locking & colour conversion for us
00155         glReadPixels(0,0, mWidth-1, mHeight-1, GL_RGB, GL_UNSIGNED_BYTE, pBuffer);
00156 
00157         // Wrap buffer in a chunk
00158         DataChunk chunk(pBuffer, mWidth * mHeight * 3);
00159 
00160         // Need to flip the read data over in Y though
00161         Image img;
00162         img.loadRawData(chunk, mWidth, mHeight, PF_R8G8B8 );
00163         img.flipAroundX();
00164 
00165         DataChunk chunkFlipped(img.getData(), chunk.getSize());
00166 
00167         // Get codec 
00168         size_t pos = filename.find_last_of(".");
00169         String extension;
00170         if( pos == String::npos )
00171             Except(
00172             Exception::ERR_INVALIDPARAMS, 
00173             "Unable to determine image type for '" + filename + "' - invalid extension.",
00174             "SDLWindow::writeContentsToFile" );
00175 
00176         while( pos != filename.length() - 1 )
00177             extension += filename[++pos];
00178 
00179         // Get the codec
00180         Codec * pCodec = Codec::getCodec(extension);
00181 
00182         // Write out
00183         pCodec->codeToFile(chunkFlipped, filename, &imgData);
00184 
00185         delete [] pBuffer;
00186 
00187 
00188     }
00189 }

Copyright © 2002-2003 by The OGRE Team
Last modified Sun Nov 28 19:48:46 2004