source: OGRE/trunk/ogrenew/RenderSystems/GL/src/SDL/OgreSDLWindow.cpp @ 692

Revision 692, 6.6 KB checked in by mattausch, 19 years ago (diff)

adding ogre 1.2 and dependencies

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