AaronCameron.net
Not Left, nor right. Just correct.
Not a Member? - Login or Create an Account
Wednesday the 23rd of May 2012 @ 04:30pm
Front Page Journal Projects Your Profile About
[]

LibN2L-4 Library Code Reference

Classes
Compounds
Files
Members
Method Index
Full Reference

cOpenGLDisplay.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002 Nova-2 Library (libN2L, or simply n2l) Game development C++ Library
00003 Copyright (C) 2003  Aaron Cameron
00004 
00005 This library is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU Lesser General Public
00007 License as published by the Free Software Foundation; either
00008 version 2.1 of the License, or (at your option) any later version.
00009 
00010 This library is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 Lesser General Public License for more details.
00014 
00015 You should have received a copy of the GNU Lesser General Public
00016 License along with this library; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00018 
00019 A copy of the GNU Lesser General Public License has been provided with
00020 this library in the file 'COPYING'.
00021 
00022 Contact information for the author of this library has been provided
00023 with this library in the file 'AUTHOR'.
00024 ************************************************************************/
00025 #include "cOpenGLDisplay.h"
00026 
00027 #include "GL/gl.h"
00028 
00029 #include "cSurface.h"
00030 
00031 namespace n2l
00032 {
00033     /**************************************************************************/
00034     cOpenGLDisplay::cOpenGLDisplay( const tUint iWidth, const tUint iHeight,
00035         const tUbyte iBpp, const tBool iDoubleBuffered, 
00036         const tBool iResizeable, const tBool iFullScreen,
00037         const cSurface *iIcon  )
00038     {
00039         if (displayExists())
00040             throw cException( "cOpenGLDisplay::cOpenGLDisplay",
00041                 "Only one surface at a time is supported destory any "
00042                 "existing displays before proceeding." );
00043     
00044         // Build the pixel format and surface properties
00045         cSurfaceProperties newProperties;
00046 
00047         // Bits per pixel
00048         cPixelFormat::tBitCount bpp = iBpp;
00049         if (0==bpp)
00050             bpp = SDL_GetVideoInfo()->vfmt->BitsPerPixel;
00051 
00052         newProperties.setFlags( cSurfaceProperties::SurfaceFlag_OpenGL );
00053         
00054         if (iDoubleBuffered)
00055             newProperties.addFlags(
00056                 cSurfaceProperties::SurfaceFlag_DoubleBuffered );
00057                     
00058         if (iResizeable)
00059             newProperties.addFlags(
00060                 cSurfaceProperties::SurfaceFlag_Resizable );
00061 
00062         if (iFullScreen)
00063             newProperties.addFlags(
00064                 cSurfaceProperties::SurfaceFlag_FullScreen );
00065 
00066 
00067         newProperties.setSize( cSurfaceProperties::tSize(iWidth,iHeight) );
00068 
00069         SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, bpp );
00070         if (iDoubleBuffered) SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
00071 
00072         if (iIcon)
00073             SDL_WM_SetIcon( iIcon->rawSurface(), 0 );
00074 
00079         SDL_Surface *newSurface = SDL_SetVideoMode(
00080             newProperties.size().getX(), newProperties.size().getY(),
00081             bpp, newProperties.flags() );
00082 
00083         if (0==newSurface && bpp==32) {
00084             bpp = 24;
00085             SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, bpp );
00086             newSurface = SDL_SetVideoMode(
00087                 newProperties.size().getX(), newProperties.size().getY(),
00088                 bpp, newProperties.flags() );
00089         }
00090 
00091         if (0==newSurface)
00092             throw cSDLException( "cOpenGLDisplay::cOpenGLDisplay",
00093                 "Failed to create the requested surface" );
00094         glViewport(0,0,iWidth,iHeight);
00095         registerDisplayCreated();
00096         setRawSurface( newSurface,false );  // false, take no ownership
00097         setFormat( *(rawSurface()->format) );
00098         setProperties( *rawSurface() );
00099     }
00100 
00101     /**************************************************************************/
00102     cOpenGLDisplay::cOpenGLDisplay( const cPixelFormat &iFormat,
00103         const cSurfaceProperties &iProperties )
00104     {
00105         if (!surfaceIsSupported(iFormat,iProperties))
00106             throw cException( "cFramebufferDisplay::cFramebufferDisplay",
00107                 "Provided format/properties are not supported" );
00108 
00109 
00110 throw cException(   "cOpenGLDisplay::cOpenGLDisplay",
00111                     "Stub call" );  
00112 
00113 //      setFormat( *(rawSurface()->format) );
00114 //      setProperties( *rawSurface() );
00115     }
00116 
00117     /**************************************************************************/
00118     cOpenGLDisplay::~cOpenGLDisplay()
00119     {
00120     }
00121 
00122     /**************************************************************************/
00123     void cOpenGLDisplay::resizeTo( const tVector2u & iNewSize )
00124     {
00125         SDL_Surface * newSurface = SDL_SetVideoMode( iNewSize.x(),
00126             iNewSize.y(), format().bitsPerPixel(), properties().flags() );
00127 
00128         if (0==newSurface)
00129             throw cSDLException( "cOpenGLDisplay::resizeTo",
00130                 "Failed to resize to requested dimensions" );
00131         glViewport(0,0,iNewSize.x(),iNewSize.y());
00132         setRawSurface( newSurface,false );  // false, take no ownership
00133         setFormat( *(rawSurface()->format) );
00134         setProperties( *rawSurface() );
00135         registerDisplayCreated();
00136     }
00137 
00138     /**************************************************************************/
00139     void cOpenGLDisplay::resetViewport()
00140     {
00141         const tVector2u DisplaySize = properties().size();
00142         glViewport(0,0,DisplaySize.x(), DisplaySize.y());
00143     }
00144 
00145     /**************************************************************************/
00146     void cOpenGLDisplay::viewport( const tVector2f &iNewPos,
00147         const tVector2f &iNewSize )
00148     {
00149         tUint x,y,xs,ys;
00150         const tVector2u DisplaySize = properties().size();
00151         x = tUint(iNewPos.x()*DisplaySize.x());
00152         y = tUint(iNewPos.y()*DisplaySize.y());
00153         xs = tUint(iNewSize.x()*DisplaySize.x());
00154         ys = tUint(iNewSize.y()*DisplaySize.y());
00155         glViewport(x,y,xs,ys);
00156     }
00157 
00158     /**************************************************************************/
00159     void cOpenGLDisplay::blit( const tVector2s &iDestPos,
00160         const cSurfaceInterface &iSurface, const tRectangle2u &iSrcRect )
00161     {
00162         if (empty())
00163             throw cBadDataUseException( "cOpenGLDisplay::blit",
00164                 "Blitting to an empty display" );
00165 
00166         if (iSurface.empty())
00167             throw cBadDataUseException( "cOpenGLDisplay::blit",
00168                 "Blitting from an empty surface" );
00169 
00170         throw cException( "cOpenGLDisplay::blit",
00171             "Support temporarily removed." );
00172 
00173 /*      GLenum error;
00174         
00175         glMatrixMode( GL_PROJECTION );
00176         glPushMatrix();
00177         glLoadIdentity();
00178 
00179         glOrtho(0,properties().size().getX(),
00180         properties().size().getY(),0,
00181         0,1);
00182 
00183         glMatrixMode( GL_MODELVIEW );
00184         glPushMatrix();
00185         glLoadIdentity();
00186 
00187         while ( (error=glGetError()) != GL_NO_ERROR )
00188             throw cOpenGLException( "cOpenGLDisplay::blit",
00189                                     "Preparing Matrix:", error );
00190         // We need to disable this if it wasn't enabled
00191         // before, honestly, I don't know how to find out
00192         glEnable(GL_TEXTURE_2D);
00193         cGLTexture tempTex( iSurface );
00194         // There's a good chance that the surface we're blitting isn't 
00195         // n^2 x m^2, which means the texture we just made is bigger
00196         // than the original.  Because of that, we have to find out what
00197         // fraction of the texture contains the ACTUAL surface data,
00198         // otherwise the blitted image will look squashed and have a big
00199         // empty space below and to the right of it.
00200         tFloat xMax = tFloat(iSurface.properties().size().x())/tempTex.size().x();
00201         tFloat yMax = tFloat(iSurface.properties().size().y())/tempTex.size().y();
00202 
00203 
00204         tempTex.bind();
00205         while ( (error=glGetError()) != GL_NO_ERROR )
00206             throw cOpenGLException( "cGLTexture::cGLTexture",
00207                                     "Preparing Texture:", error );
00208         glBegin( GL_QUADS );
00209         
00210             glTexCoord2f(0.0f,0.0f);
00211             glVertex3f( GLfloat(iDestPos.getX()),
00212                         GLfloat(iDestPos.getY()),
00213                         0.0f );
00214 
00215             glTexCoord2f(xMax,0.0f);
00216             glVertex3f( GLfloat(iDestPos.getX()+iSurface.properties().size().getX()),
00217                         GLfloat(iDestPos.getY()),
00218                         0.0f );
00219 
00220             glTexCoord2f(xMax,yMax);
00221             glVertex3f( GLfloat(iDestPos.getX()+iSurface.properties().size().getX()),
00222                         GLfloat(iDestPos.getY()+iSurface.properties().size().getY()),
00223                         0.0f );
00224 
00225             glTexCoord2f(0.0f,yMax);
00226             glVertex3f( GLfloat(iDestPos.getX()),
00227                         GLfloat(iDestPos.getY()+iSurface.properties().size().getY()),
00228                         0.0f );
00229         glEnd();
00230         while ( (error=glGetError()) != GL_NO_ERROR )
00231             throw cOpenGLException( "cOpenGLDisplay::blit",
00232                                     "Drawing:", error );
00233         glPopMatrix();
00234         while ( (error=glGetError()) != GL_NO_ERROR )
00235             throw cOpenGLException( "cOpenGLDisplay::blit",
00236                                     "Restoring the model matrix:", error );
00237         glMatrixMode( GL_PROJECTION );
00238         glPopMatrix();
00239         while ( (error=glGetError()) != GL_NO_ERROR )
00240             throw cOpenGLException( "cOpenGLDisplay::blit",
00241                                     "Restoring the projection matrix:", error );
00242 */
00243     }
00244 
00245     /**************************************************************************/
00246     const tBool cOpenGLDisplay::supportsRWBuffer() const
00247     {
00248         // For now.
00249         return false;
00250     }
00251 
00252     /**************************************************************************/
00253     void *const cOpenGLDisplay::aquireRWBuffer()
00254     {
00255         // For now
00256         throw cUnsupportedMethodException( "cOpenGLDisplay::aquireRWBuffer",
00257             "RW Buffers not supported" );
00258     }
00259 
00260     /**************************************************************************/
00261     void cOpenGLDisplay::releaseRWBuffer() const
00262     {
00263         // For now
00264         throw cUnsupportedMethodException( "cOpenGLDisplay::releaseRWBuffer",
00265             "RW Buffers not supported" );
00266     }
00267 
00268     /**************************************************************************/
00269     void cOpenGLDisplay::flip() const
00270     {
00271         SDL_GL_SwapBuffers();
00272     }
00273 
00274     /**************************************************************************/
00275     const tString &cOpenGLDisplay::glDriver() const
00276     {
00277         static tString info;
00278         info = "GL Driver Info(\n\tGL_RENDERER:    ";
00279         info += ((char *)glGetString(GL_RENDERER));
00280         info += "\n\tGL_VERSION:     ";
00281         info += ((char *)glGetString(GL_VERSION));
00282         info += "\n\tGL_VENDOR:      ";
00283         info += ((char *)glGetString(GL_VENDOR));
00284         info += "\n)";
00285         return info;
00286     }
00287 
00288     /**************************************************************************/
00289     const tString &cOpenGLDisplay::glExtensions() const
00290     {
00291         static tString info;
00292         info = ((char *)glGetString(GL_EXTENSIONS));
00293         return info;
00294     }
00295 
00296     /**************************************************************************/
00297     const tString & cOpenGLDisplay::glCardInfo() const
00298     {
00299         static tString info;
00300         info = ((char *)glGetString(GL_EXTENSIONS));
00301         return info;
00302     }
00303 
00304 } // namespace
©2012 Aaron Cameron