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 00032 #if OGRE_PLATFORM == PLATFORM_WIN32 00033 # include <windows.h> 00034 # include <wingdi.h> 00035 # include "gl.h" 00036 # define GL_GLEXT_PROTOTYPES 00037 # include "glprocs.h" 00038 # include <GL/glu.h> 00039 #elif OGRE_PLATFORM == PLATFORM_LINUX 00040 # include <GL/gl.h> 00041 # include <GL/glu.h> 00042 #elif OGRE_PLATFORM == PLATFORM_APPLE 00043 # include <OpenGL/gl.h> 00044 # define GL_EXT_texture_env_combine 1 00045 # include <OpenGL/glext.h> 00046 # include <OpenGL/glu.h> 00047 #endif 00048 00049 namespace Ogre { 00050 00051 SDLWindow::SDLWindow() : 00052 mScreen(NULL), mActive(false), mClosed(false) 00053 { 00054 } 00055 00056 SDLWindow::~SDLWindow() 00057 { 00058 if (mScreen != NULL) 00059 SDL_FreeSurface(mScreen); 00060 00061 } 00062 00063 void SDLWindow::create(const String& name, unsigned int width, unsigned int height, unsigned int colourDepth, 00064 bool fullScreen, int left, int top, bool depthBuffer, 00065 void* miscParam, ...) 00066 { 00067 fprintf(stderr, "SDLWindow::create\n"); 00068 SDL_Surface* screen; 00069 int flags = SDL_OPENGL | SDL_HWPALETTE; 00070 00071 SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); 00072 // request good stencil size if 32-bit colour 00073 if (colourDepth == 32 && depthBuffer) 00074 { 00075 SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 8); 00076 } 00077 00078 if (fullScreen) 00079 flags |= SDL_FULLSCREEN; 00080 00081 fprintf(stderr, "Create window\n"); 00082 screen = SDL_SetVideoMode(width, height, colourDepth, flags); 00083 if (!screen) 00084 { 00085 fprintf(stderr, "Could not make screen: %s.\n", SDL_GetError()); 00086 exit(1); 00087 } 00088 fprintf(stderr, "screen is valid\n"); 00089 mScreen = screen; 00090 00091 mName = name; 00092 00093 mWidth = width; 00094 mHeight = height; 00095 00096 mActive = true; 00097 00098 if (!fullScreen) 00099 SDL_WM_SetCaption(name.c_str(), 0); 00100 00101 } 00102 00103 void SDLWindow::destroy(void) 00104 { 00105 SDL_FreeSurface(mScreen); 00106 mScreen = NULL; 00107 mActive = false; 00108 00109 Root::getSingleton().getRenderSystem()->detachRenderTarget( this->getName() ); 00110 } 00111 00112 bool SDLWindow::isActive() const 00113 { 00114 return mActive; 00115 } 00116 00117 bool SDLWindow::isClosed() const 00118 { 00119 return mClosed; 00120 } 00121 00122 void SDLWindow::reposition(int left, int top) 00123 { 00124 // XXX FIXME 00125 } 00126 00127 void SDLWindow::resize(unsigned int width, unsigned int height) 00128 { 00129 for (ViewportList::iterator it = mViewportList.begin(); 00130 it != mViewportList.end(); ++it) 00131 { 00132 (*it).second->_updateDimensions(); 00133 } 00134 } 00135 00136 void SDLWindow::swapBuffers(bool waitForVSync) 00137 { 00138 SDL_GL_SwapBuffers(); 00139 // XXX More? 00140 } 00141 00142 void SDLWindow::outputText(int x, int y, const String& text) 00143 { 00144 //XXX FIXME 00145 } 00146 void SDLWindow::writeContentsToFile(const String& filename) 00147 { 00148 ImageCodec::ImageData imgData; 00149 imgData.width = mWidth; 00150 imgData.height = mHeight; 00151 imgData.format = PF_R8G8B8; 00152 00153 // Allocate buffer 00154 uchar* pBuffer = new uchar[mWidth * mHeight * 3]; 00155 00156 // Read pixels 00157 // I love GL: it does all the locking & colour conversion for us 00158 glReadPixels(0,0, mWidth-1, mHeight-1, GL_RGB, GL_UNSIGNED_BYTE, pBuffer); 00159 00160 // Wrap buffer in a chunk 00161 DataChunk chunk(pBuffer, mWidth * mHeight * 3); 00162 00163 // Need to flip the read data over in Y though 00164 Image img; 00165 img.loadRawData(chunk, mWidth, mHeight, PF_R8G8B8 ); 00166 img.flipAroundX(); 00167 00168 DataChunk chunkFlipped(img.getData(), chunk.getSize()); 00169 00170 // Get codec 00171 size_t pos = filename.find_last_of("."); 00172 String extension; 00173 if( pos == String::npos ) 00174 Except( 00175 Exception::ERR_INVALIDPARAMS, 00176 "Unable to determine image type for '" + filename + "' - invalid extension.", 00177 "SDLWindow::writeContentsToFile" ); 00178 00179 while( pos != filename.length() - 1 ) 00180 extension += filename[++pos]; 00181 00182 // Get the codec 00183 Codec * pCodec = Codec::getCodec(extension); 00184 00185 // Write out 00186 pCodec->codeToFile(chunkFlipped, filename, &imgData); 00187 00188 delete [] pBuffer; 00189 00190 00191 } 00192 }
Copyright © 2002-2003 by The OGRE Team
Last modified Fri May 14 23:22:46 2004