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 00073 if (fullScreen) 00074 flags |= SDL_FULLSCREEN; 00075 00076 fprintf(stderr, "Create window\n"); 00077 screen = SDL_SetVideoMode(width, height, colourDepth, flags); 00078 if (!screen) 00079 { 00080 fprintf(stderr, "Could not make screen\n"); 00081 exit(1); 00082 } 00083 fprintf(stderr, "screen is valid\n"); 00084 mScreen = screen; 00085 00086 mName = name; 00087 00088 mWidth = width; 00089 mHeight = height; 00090 00091 mActive = true; 00092 00093 if (!fullScreen) 00094 SDL_WM_SetCaption(name.c_str(), 0); 00095 00096 } 00097 00098 void SDLWindow::destroy(void) 00099 { 00100 SDL_FreeSurface(mScreen); 00101 mScreen = NULL; 00102 mActive = false; 00103 00104 Root::getSingleton().getRenderSystem()->detachRenderTarget( this->getName() ); 00105 } 00106 00107 bool SDLWindow::isActive() const 00108 { 00109 return mActive; 00110 } 00111 00112 bool SDLWindow::isClosed() const 00113 { 00114 return mClosed; 00115 } 00116 00117 void SDLWindow::reposition(int left, int top) 00118 { 00119 // XXX FIXME 00120 } 00121 00122 void SDLWindow::resize(unsigned int width, unsigned int height) 00123 { 00124 for (ViewportList::iterator it = mViewportList.begin(); 00125 it != mViewportList.end(); ++it) 00126 { 00127 (*it).second->_updateDimensions(); 00128 } 00129 } 00130 00131 void SDLWindow::swapBuffers(bool waitForVSync) 00132 { 00133 SDL_GL_SwapBuffers(); 00134 // XXX More? 00135 } 00136 00137 void SDLWindow::outputText(int x, int y, const String& text) 00138 { 00139 //XXX FIXME 00140 } 00141 void SDLWindow::writeContentsToFile(const String& filename) 00142 { 00143 ImageCodec::ImageData imgData; 00144 imgData.width = mWidth; 00145 imgData.height = mHeight; 00146 imgData.format = PF_R8G8B8; 00147 00148 // Allocate buffer 00149 uchar* pBuffer = new uchar[mWidth * mHeight * 3]; 00150 00151 // Read pixels 00152 // I love GL: it does all the locking & colour conversion for us 00153 glReadPixels(0,0, mWidth-1, mHeight-1, GL_RGB, GL_UNSIGNED_BYTE, pBuffer); 00154 00155 // Wrap buffer in a chunk 00156 DataChunk chunk(pBuffer, mWidth * mHeight * 3); 00157 00158 // Need to flip the read data over in Y though 00159 Image img; 00160 img.loadRawData(chunk, mWidth, mHeight, PF_R8G8B8 ); 00161 img.flipAroundX(); 00162 00163 DataChunk chunkFlipped(img.getData(), chunk.getSize()); 00164 00165 // Get codec 00166 size_t pos = filename.find_last_of("."); 00167 String extension; 00168 if( pos == String::npos ) 00169 Except( 00170 Exception::ERR_INVALIDPARAMS, 00171 "Unable to determine image type for '" + filename + "' - invalid extension.", 00172 "SDLWindow::writeContentsToFile" ); 00173 00174 while( pos != filename.length() - 1 ) 00175 extension += filename[++pos]; 00176 00177 // Get the codec 00178 Codec * pCodec = Codec::getCodec(extension); 00179 00180 // Write out 00181 pCodec->codeToFile(chunkFlipped, filename, &imgData); 00182 00183 delete [] pBuffer; 00184 00185 00186 } 00187 }
Copyright © 2002-2003 by The OGRE Team
Last modified Wed Jan 21 00:10:27 2004