1 | /* |
---|
2 | ----------------------------------------------------------------------------- |
---|
3 | This source file is part of OGRE |
---|
4 | (Object-oriented Graphics Rendering Engine) |
---|
5 | For the latest info, see http://www.ogre3d.org/ |
---|
6 | |
---|
7 | Copyright (c) 2000-2006 Torus Knot Software Ltd |
---|
8 | Also see acknowledgements in Readme.html |
---|
9 | |
---|
10 | This program is free software; you can redistribute it and/or modify it under |
---|
11 | the terms of the GNU Lesser General Public License as published by the Free Software |
---|
12 | Foundation; either version 2 of the License, or (at your option) any later |
---|
13 | version. |
---|
14 | |
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT |
---|
16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU Lesser General Public License along with |
---|
20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
---|
21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
---|
22 | http://www.gnu.org/copyleft/lesser.txt. |
---|
23 | |
---|
24 | You may alternatively use this source under the terms of a specific version of |
---|
25 | the OGRE Unrestricted License provided you have obtained such a license from |
---|
26 | Torus Knot Software Ltd. |
---|
27 | ----------------------------------------------------------------------------- |
---|
28 | */ |
---|
29 | |
---|
30 | #include "OgreSDLWindow.h" |
---|
31 | #include "OgreRoot.h" |
---|
32 | #include "OgreRenderSystem.h" |
---|
33 | #include "OgreImageCodec.h" |
---|
34 | #include "OgreException.h" |
---|
35 | #include "OgreLogManager.h" |
---|
36 | #include "OgreStringConverter.h" |
---|
37 | |
---|
38 | #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 |
---|
39 | # include <windows.h> |
---|
40 | # include <wingdi.h> |
---|
41 | # include <GL/gl.h> |
---|
42 | # define GL_GLEXT_PROTOTYPES |
---|
43 | # include "glprocs.h" |
---|
44 | # include <GL/glu.h> |
---|
45 | #elif OGRE_PLATFORM == OGRE_PLATFORM_LINUX |
---|
46 | # include <GL/gl.h> |
---|
47 | # include <GL/glu.h> |
---|
48 | #elif OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
49 | # include <OpenGL/gl.h> |
---|
50 | # define GL_EXT_texture_env_combine 1 |
---|
51 | # include <OpenGL/glext.h> |
---|
52 | # include <OpenGL/glu.h> |
---|
53 | #endif |
---|
54 | |
---|
55 | namespace Ogre { |
---|
56 | |
---|
57 | SDLWindow::SDLWindow() : |
---|
58 | mScreen(NULL), mActive(false), mClosed(false) |
---|
59 | { |
---|
60 | } |
---|
61 | |
---|
62 | SDLWindow::~SDLWindow() |
---|
63 | { |
---|
64 | // according to http://www.libsdl.org/cgi/docwiki.cgi/SDL_5fSetVideoMode |
---|
65 | // never free the surface returned from SDL_SetVideoMode |
---|
66 | /*if (mScreen != NULL) |
---|
67 | SDL_FreeSurface(mScreen);*/ |
---|
68 | |
---|
69 | } |
---|
70 | |
---|
71 | void SDLWindow::create(const String& name, unsigned int width, unsigned int height, |
---|
72 | bool fullScreen, const NameValuePairList *miscParams) |
---|
73 | { |
---|
74 | int colourDepth = 32; |
---|
75 | String title = name; |
---|
76 | if(miscParams) |
---|
77 | { |
---|
78 | // Parse miscellenous parameters |
---|
79 | NameValuePairList::const_iterator opt; |
---|
80 | // Bit depth |
---|
81 | opt = miscParams->find("colourDepth"); |
---|
82 | if(opt != miscParams->end()) //check for FSAA parameter, if not ignore it... |
---|
83 | colourDepth = StringConverter::parseUnsignedInt(opt->second); |
---|
84 | // Full screen antialiasing |
---|
85 | opt = miscParams->find("FSAA"); |
---|
86 | if(opt != miscParams->end()) //check for FSAA parameter, if not ignore it... |
---|
87 | { |
---|
88 | size_t fsaa_x_samples = StringConverter::parseUnsignedInt(opt->second); |
---|
89 | if(fsaa_x_samples>1) { |
---|
90 | // If FSAA is enabled in the parameters, enable the MULTISAMPLEBUFFERS |
---|
91 | // and set the number of samples before the render window is created. |
---|
92 | SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS,1); |
---|
93 | SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES,fsaa_x_samples); |
---|
94 | } |
---|
95 | } |
---|
96 | // Window title |
---|
97 | opt = miscParams->find("title"); |
---|
98 | if(opt != miscParams->end()) //check for FSAA parameter, if not ignore it... |
---|
99 | title = opt->second; |
---|
100 | } |
---|
101 | |
---|
102 | LogManager::getSingleton().logMessage("SDLWindow::create", LML_TRIVIAL); |
---|
103 | SDL_Surface* screen; |
---|
104 | int flags = SDL_OPENGL | SDL_HWPALETTE | SDL_RESIZABLE; |
---|
105 | |
---|
106 | SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); |
---|
107 | // request good stencil size if 32-bit colour |
---|
108 | if (colourDepth == 32) |
---|
109 | { |
---|
110 | SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 8); |
---|
111 | } |
---|
112 | |
---|
113 | if (fullScreen) |
---|
114 | flags |= SDL_FULLSCREEN; |
---|
115 | |
---|
116 | LogManager::getSingleton().logMessage("Create window", LML_TRIVIAL); |
---|
117 | screen = SDL_SetVideoMode(width, height, colourDepth, flags); |
---|
118 | if (!screen) |
---|
119 | { |
---|
120 | LogManager::getSingleton().logMessage(LML_CRITICAL, |
---|
121 | String("Could not make screen: ") + SDL_GetError()); |
---|
122 | exit(1); |
---|
123 | } |
---|
124 | LogManager::getSingleton().logMessage("screen is valid", LML_TRIVIAL); |
---|
125 | mScreen = screen; |
---|
126 | |
---|
127 | mName = name; |
---|
128 | |
---|
129 | mWidth = width; |
---|
130 | mHeight = height; |
---|
131 | |
---|
132 | mActive = true; |
---|
133 | |
---|
134 | if (!fullScreen) |
---|
135 | SDL_WM_SetCaption(title.c_str(), 0); |
---|
136 | |
---|
137 | glXGetVideoSyncSGI = (int (*)(unsigned int *))SDL_GL_GetProcAddress("glXGetVideoSyncSGI"); |
---|
138 | glXWaitVideoSyncSGI = (int (*)(int, int, unsigned int *))SDL_GL_GetProcAddress("glXWaitVideoSyncSGI"); |
---|
139 | } |
---|
140 | |
---|
141 | void SDLWindow::destroy(void) |
---|
142 | { |
---|
143 | // according to http://www.libsdl.org/cgi/docwiki.cgi/SDL_5fSetVideoMode |
---|
144 | // never free the surface returned from SDL_SetVideoMode |
---|
145 | //SDL_FreeSurface(mScreen); |
---|
146 | mScreen = NULL; |
---|
147 | mActive = false; |
---|
148 | |
---|
149 | Root::getSingleton().getRenderSystem()->detachRenderTarget( this->getName() ); |
---|
150 | } |
---|
151 | |
---|
152 | bool SDLWindow::isActive() const |
---|
153 | { |
---|
154 | return mActive; |
---|
155 | } |
---|
156 | |
---|
157 | bool SDLWindow::isClosed() const |
---|
158 | { |
---|
159 | return mClosed; |
---|
160 | } |
---|
161 | |
---|
162 | void SDLWindow::reposition(int left, int top) |
---|
163 | { |
---|
164 | // XXX FIXME |
---|
165 | } |
---|
166 | |
---|
167 | void SDLWindow::resize(unsigned int width, unsigned int height) |
---|
168 | { |
---|
169 | SDL_Surface* screen; |
---|
170 | int flags = SDL_OPENGL | SDL_HWPALETTE | SDL_RESIZABLE; |
---|
171 | |
---|
172 | LogManager::getSingleton().logMessage("Updating window", LML_TRIVIAL); |
---|
173 | screen = SDL_SetVideoMode(width, height, mScreen->format->BitsPerPixel, flags); |
---|
174 | if (!screen) |
---|
175 | { |
---|
176 | LogManager::getSingleton().logMessage(LML_CRITICAL, |
---|
177 | String("Could not make screen: ") + SDL_GetError()); |
---|
178 | exit(1); |
---|
179 | } |
---|
180 | LogManager::getSingleton().logMessage("screen is valid", LML_TRIVIAL); |
---|
181 | mScreen = screen; |
---|
182 | |
---|
183 | |
---|
184 | mWidth = width; |
---|
185 | mHeight = height; |
---|
186 | |
---|
187 | for (ViewportList::iterator it = mViewportList.begin(); |
---|
188 | it != mViewportList.end(); ++it) |
---|
189 | { |
---|
190 | (*it).second->_updateDimensions(); |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | void SDLWindow::swapBuffers(bool waitForVSync) |
---|
195 | { |
---|
196 | if ( waitForVSync && glXGetVideoSyncSGI && glXWaitVideoSyncSGI ) |
---|
197 | { |
---|
198 | unsigned int retraceCount; |
---|
199 | glXGetVideoSyncSGI( &retraceCount ); |
---|
200 | glXWaitVideoSyncSGI( 2, ( retraceCount + 1 ) & 1, &retraceCount); |
---|
201 | } |
---|
202 | |
---|
203 | SDL_GL_SwapBuffers(); |
---|
204 | // XXX More? |
---|
205 | } |
---|
206 | |
---|
207 | void SDLWindow::writeContentsToFile(const String& filename) |
---|
208 | { |
---|
209 | ImageCodec::ImageData* imgData = new ImageCodec::ImageData; |
---|
210 | imgData->width = mWidth; |
---|
211 | imgData->height = mHeight; |
---|
212 | imgData->format = PF_BYTE_RGB; |
---|
213 | |
---|
214 | // Allocate buffer |
---|
215 | uchar* pBuffer = new uchar[mWidth * mHeight * 3]; |
---|
216 | |
---|
217 | // Read pixels |
---|
218 | // I love GL: it does all the locking & colour conversion for us |
---|
219 | glReadPixels(0,0, mWidth-1, mHeight-1, GL_RGB, GL_UNSIGNED_BYTE, pBuffer); |
---|
220 | |
---|
221 | // Wrap buffer in a memory stream |
---|
222 | DataStreamPtr stream(new MemoryDataStream(pBuffer, mWidth * mHeight * 3, false)); |
---|
223 | |
---|
224 | // Need to flip the read data over in Y though |
---|
225 | Image img; |
---|
226 | img.loadRawData(stream, mWidth, mHeight, imgData->format ); |
---|
227 | img.flipAroundX(); |
---|
228 | |
---|
229 | MemoryDataStreamPtr streamFlipped(new MemoryDataStream(img.getData(), stream->size(), false)); |
---|
230 | |
---|
231 | // Get codec |
---|
232 | size_t pos = filename.find_last_of("."); |
---|
233 | String extension; |
---|
234 | if( pos == String::npos ) |
---|
235 | OGRE_EXCEPT( |
---|
236 | Exception::ERR_INVALIDPARAMS, |
---|
237 | "Unable to determine image type for '" + filename + "' - invalid extension.", |
---|
238 | "SDLWindow::writeContentsToFile" ); |
---|
239 | |
---|
240 | while( pos != filename.length() - 1 ) |
---|
241 | extension += filename[++pos]; |
---|
242 | |
---|
243 | // Get the codec |
---|
244 | Codec * pCodec = Codec::getCodec(extension); |
---|
245 | |
---|
246 | // Write out |
---|
247 | Codec::CodecDataPtr codecDataPtr(imgData); |
---|
248 | pCodec->codeToFile(streamFlipped, filename, codecDataPtr); |
---|
249 | |
---|
250 | delete [] pBuffer; |
---|
251 | |
---|
252 | |
---|
253 | } |
---|
254 | } |
---|