Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

RageDisplay.h

Go to the documentation of this file.
00001 /* 00002 * RageDisplay: Methods common to all RageDisplays 00003 */ 00004 00005 #ifndef RAGEDISPLAY_H 00006 #define RAGEDISPLAY_H 00007 00008 #include "RageTypes.h" 00009 #include "ModelTypes.h" 00010 00011 const int REFRESH_DEFAULT = 0; 00012 struct RageSurface; 00013 const int MAX_TEXTURE_UNITS = 2; 00014 00015 // RageCompiledGeometry holds vertex data in a format that is most efficient 00016 // for the graphics API. 00017 class RageCompiledGeometry 00018 { 00019 public: 00020 virtual ~RageCompiledGeometry() 00021 { 00022 m_bNeedsNormals = false; 00023 } 00024 00025 void Set( const vector<msMesh> &vMeshes, bool bNeedsNormals ) 00026 { 00027 m_bNeedsNormals = bNeedsNormals; 00028 00029 size_t totalVerts = 0; 00030 size_t totalTriangles = 0; 00031 00032 m_vMeshInfo.resize( vMeshes.size() ); 00033 for( unsigned i=0; i<vMeshes.size(); i++ ) 00034 { 00035 const msMesh& mesh = vMeshes[i]; 00036 const vector<RageModelVertex> &Vertices = mesh.Vertices; 00037 const vector<msTriangle> &Triangles = mesh.Triangles; 00038 00039 MeshInfo& meshInfo = m_vMeshInfo[i]; 00040 00041 meshInfo.iVertexStart = totalVerts; 00042 meshInfo.iVertexCount = Vertices.size(); 00043 meshInfo.iTriangleStart = totalTriangles; 00044 meshInfo.iTriangleCount = Triangles.size(); 00045 00046 totalVerts += Vertices.size(); 00047 totalTriangles += Triangles.size(); 00048 } 00049 00050 this->Allocate( vMeshes ); 00051 00052 Change( vMeshes ); 00053 } 00054 00055 virtual void Allocate( const vector<msMesh> &vMeshes ) = 0; // allocate space 00056 virtual void Change( const vector<msMesh> &vMeshes ) = 0; // new data must be the same size as was passed to Set() 00057 virtual void Draw( int iMeshIndex ) const = 0; 00058 00059 protected: 00060 size_t GetTotalVertices() { if( m_vMeshInfo.empty() ) return 0; return m_vMeshInfo.back().iVertexStart + m_vMeshInfo.back().iVertexCount; } 00061 size_t GetTotalTriangles() { if( m_vMeshInfo.empty() ) return 0; return m_vMeshInfo.back().iTriangleStart + m_vMeshInfo.back().iTriangleCount; } 00062 00063 struct MeshInfo 00064 { 00065 int iVertexStart; 00066 int iVertexCount; 00067 int iTriangleStart; 00068 int iTriangleCount; 00069 }; 00070 vector<MeshInfo> m_vMeshInfo; 00071 bool m_bNeedsNormals; 00072 }; 00073 00074 class RageDisplay 00075 { 00076 friend class RageTexture; 00077 00078 public: 00079 00080 struct PixelFormatDesc { 00081 int bpp; 00082 unsigned int masks[4]; 00083 }; 00084 00085 enum PixelFormat { 00086 FMT_RGBA8, 00087 FMT_RGBA4, 00088 FMT_RGB5A1, 00089 FMT_RGB5, 00090 FMT_RGB8, 00091 FMT_PAL, 00092 /* The above formats differ between OpenGL and D3D. These are provided as 00093 * alternatives for OpenGL that match some format in D3D. Don't use them 00094 * directly; they'll be matched automatically by FindPixelFormat. */ 00095 FMT_BGR8, 00096 FMT_A1BGR5, 00097 NUM_PIX_FORMATS 00098 }; 00099 00100 static CString PixelFormatToString( PixelFormat pixfmt ); 00101 virtual const PixelFormatDesc *GetPixelFormatDesc(PixelFormat pf) const = 0; 00102 00103 struct VideoModeParams 00104 { 00105 // Initialize with a constructor so to guarantee all paramters 00106 // are filled (in case new params are added). 00107 VideoModeParams( 00108 bool windowed_, 00109 int width_, 00110 int height_, 00111 int bpp_, 00112 int rate_, 00113 bool vsync_, 00114 bool interlaced_, 00115 bool bSmoothLines_, 00116 bool bTrilinearFiltering_, 00117 bool bAnisotropicFiltering_, 00118 CString sWindowTitle_, 00119 CString sIconFile_, 00120 bool PAL_, 00121 float fDisplayAspectRatio_ 00122 ) 00123 { 00124 windowed = windowed_; 00125 width = width_; 00126 height = height_; 00127 bpp = bpp_; 00128 rate = rate_; 00129 vsync = vsync_; 00130 interlaced = interlaced_; 00131 bSmoothLines = bSmoothLines_; 00132 bTrilinearFiltering = bTrilinearFiltering_; 00133 bAnisotropicFiltering = bAnisotropicFiltering_; 00134 sWindowTitle = sWindowTitle_; 00135 sIconFile = sIconFile_; 00136 PAL = PAL_; 00137 fDisplayAspectRatio = fDisplayAspectRatio_; 00138 } 00139 VideoModeParams() {} 00140 00141 bool windowed; 00142 int width; 00143 int height; 00144 int bpp; 00145 int rate; 00146 bool vsync; 00147 bool bSmoothLines; 00148 bool bTrilinearFiltering; 00149 bool bAnisotropicFiltering; 00150 bool interlaced; 00151 bool PAL; 00152 float fDisplayAspectRatio; 00153 CString sWindowTitle; 00154 CString sIconFile; 00155 }; 00156 00157 /* This is needed or the overridden classes' dtors will not be called. */ 00158 virtual ~RageDisplay() { } 00159 00160 virtual void Update(float fDeltaTime) { } 00161 00162 // Don't override this. Override TryVideoMode() instead. 00163 // This will set the video mode to be as close as possible to params. 00164 // Return true if device was re-created and we need to reload textures. 00165 CString SetVideoMode( VideoModeParams p, bool &bNeedReloadTextures ); 00166 00167 /* Call this when the resolution has been changed externally: */ 00168 virtual void ResolutionChanged() { } 00169 00170 virtual bool BeginFrame() = 0; 00171 virtual void EndFrame() = 0; 00172 virtual VideoModeParams GetVideoModeParams() const = 0; 00173 bool IsWindowed() const { return this->GetVideoModeParams().windowed; } 00174 00175 virtual void SetBlendMode( BlendMode mode ) = 0; 00176 00177 virtual bool SupportsTextureFormat( PixelFormat pixfmt, bool realtime=false ) = 0; 00178 00179 /* return 0 if failed or internal texture resource handle 00180 * (unsigned in OpenGL, texture pointer in D3D) */ 00181 virtual unsigned CreateTexture( 00182 PixelFormat pixfmt, // format of img and of texture in video mem 00183 RageSurface* img, // must be in pixfmt 00184 bool bGenerateMipMaps 00185 ) = 0; 00186 virtual void UpdateTexture( 00187 unsigned uTexHandle, 00188 RageSurface* img, 00189 int xoffset, int yoffset, int width, int height 00190 ) = 0; 00191 virtual void DeleteTexture( unsigned uTexHandle ) = 0; 00192 virtual void ClearAllTextures() = 0; 00193 virtual int GetNumTextureUnits() = 0; 00194 virtual void SetTexture( int iTextureUnitIndex, RageTexture* pTexture ) = 0; 00195 virtual void SetTextureModeModulate() = 0; 00196 virtual void SetTextureModeGlow() = 0; 00197 virtual void SetTextureModeAdd() = 0; 00198 virtual void SetTextureWrapping( bool b ) = 0; 00199 virtual int GetMaxTextureSize() const = 0; 00200 virtual void SetTextureFiltering( bool b ) = 0; 00201 00202 virtual bool IsZTestEnabled() const = 0; 00203 virtual bool IsZWriteEnabled() const = 0; 00204 virtual void SetZWrite( bool b ) = 0; 00205 virtual void SetZTestMode( ZTestMode mode ) = 0; 00206 virtual void ClearZBuffer() = 0; 00207 00208 virtual void SetCullMode( CullMode mode ) = 0; 00209 00210 virtual void SetAlphaTest( bool b ) = 0; 00211 00212 virtual void SetMaterial( 00213 const RageColor &emissive, 00214 const RageColor &ambient, 00215 const RageColor &diffuse, 00216 const RageColor &specular, 00217 float shininess 00218 ) = 0; 00219 00220 virtual void SetLighting( bool b ) = 0; 00221 virtual void SetLightOff( int index ) = 0; 00222 virtual void SetLightDirectional( 00223 int index, 00224 const RageColor &ambient, 00225 const RageColor &diffuse, 00226 const RageColor &specular, 00227 const RageVector3 &dir ) = 0; 00228 00229 virtual void SetSphereEnvironmentMapping( bool b ) = 0; 00230 00231 virtual RageCompiledGeometry* CreateCompiledGeometry() = 0; 00232 virtual void DeleteCompiledGeometry( RageCompiledGeometry* p ) = 0; 00233 00234 void DrawQuads( const RageSpriteVertex v[], int iNumVerts ); 00235 void DrawQuadStrip( const RageSpriteVertex v[], int iNumVerts ); 00236 void DrawFan( const RageSpriteVertex v[], int iNumVerts ); 00237 void DrawStrip( const RageSpriteVertex v[], int iNumVerts ); 00238 void DrawTriangles( const RageSpriteVertex v[], int iNumVerts ); 00239 void DrawCompiledGeometry( const RageCompiledGeometry *p, int iMeshIndex, const vector<msMesh> &vMeshes ); 00240 void DrawLineStrip( const RageSpriteVertex v[], int iNumVerts, float LineWidth ); 00241 void DrawCircle( const RageSpriteVertex &v, float radius ); 00242 00243 void DrawQuad( const RageSpriteVertex v[] ) { DrawQuads(v,4); } /* alias. upper-left, upper-right, lower-left, lower-right */ 00244 00245 // hacks for cell-shaded models 00246 virtual void SetPolygonMode( PolygonMode pm ) {} 00247 virtual void SetLineWidth( float fWidth ) {} 00248 00249 enum GraphicsFileFormat 00250 { 00251 SAVE_LOSSLESS, // bmp 00252 SAVE_LOSSY_LOW_QUAL, // jpg 00253 SAVE_LOSSY_HIGH_QUAL // jpg 00254 }; 00255 bool SaveScreenshot( CString sPath, GraphicsFileFormat format ); 00256 00257 virtual CString GetTextureDiagnostics( unsigned id ) const { return ""; } 00258 virtual RageSurface* CreateScreenshot() = 0; // allocates a surface. Caller must delete it. 00259 00260 protected: 00261 virtual void DrawQuadsInternal( const RageSpriteVertex v[], int iNumVerts ) = 0; 00262 virtual void DrawQuadStripInternal( const RageSpriteVertex v[], int iNumVerts ) = 0; 00263 virtual void DrawFanInternal( const RageSpriteVertex v[], int iNumVerts ) = 0; 00264 virtual void DrawStripInternal( const RageSpriteVertex v[], int iNumVerts ) = 0; 00265 virtual void DrawTrianglesInternal( const RageSpriteVertex v[], int iNumVerts ) = 0; 00266 virtual void DrawCompiledGeometryInternal( const RageCompiledGeometry *p, int iMeshIndex ) = 0; 00267 virtual void DrawLineStripInternal( const RageSpriteVertex v[], int iNumVerts, float LineWidth ); 00268 virtual void DrawCircleInternal( const RageSpriteVertex &v, float radius ); 00269 00270 // Return "" if mode change was successful, an error message otherwise. 00271 // bNewDeviceOut is set true if a new device was created and textures 00272 // need to be reloaded. 00273 virtual CString TryVideoMode( VideoModeParams params, bool &bNewDeviceOut ) = 0; 00274 00275 virtual void SetViewport(int shift_left, int shift_down) = 0; 00276 00277 void DrawPolyLine(const RageSpriteVertex &p1, const RageSpriteVertex &p2, float LineWidth ); 00278 00279 00280 // Stuff in RageDisplay.cpp 00281 void SetDefaultRenderStates(); 00282 00283 public: 00284 /* Statistics */ 00285 int GetFPS() const; 00286 int GetVPF() const; 00287 int GetCumFPS() const; /* average FPS since last reset */ 00288 virtual void ResetStats(); 00289 virtual void ProcessStatsOnFlip(); 00290 virtual CString GetStats() const; 00291 void StatsAddVerts( int iNumVertsRendered ); 00292 00293 /* World matrix stack functions. */ 00294 void PushMatrix(); 00295 void PopMatrix(); 00296 void Translate( float x, float y, float z ); 00297 void TranslateWorld( float x, float y, float z ); 00298 void Scale( float x, float y, float z ); 00299 void RotateX( float deg ); 00300 void RotateY( float deg ); 00301 void RotateZ( float deg ); 00302 void MultMatrix( const RageMatrix &f ) { this->PostMultMatrix(f); } /* alias */ 00303 void PostMultMatrix( const RageMatrix &f ); 00304 void PreMultMatrix( const RageMatrix &f ); 00305 void LoadIdentity(); 00306 00307 /* Texture matrix functions */ 00308 void TexturePushMatrix(); 00309 void TexturePopMatrix(); 00310 void TextureTranslate( float x, float y, float z ); 00311 00312 /* Projection and View matrix stack functions. */ 00313 void CameraPushMatrix(); 00314 void CameraPopMatrix(); 00315 void LoadMenuPerspective( float fovDegrees, float fVanishPointX, float fVanishPointY ); 00316 void LoadLookAt( float fov, const RageVector3 &Eye, const RageVector3 &At, const RageVector3 &Up ); 00317 00318 /* Centering matrix */ 00319 void ChangeCentering( int trans_x, int trans_y, int add_width, int add_height ); 00320 00321 RageSurface *CreateSurfaceFromPixfmt( PixelFormat pixfmt, void *pixels, int width, int height, int pitch ); 00322 PixelFormat FindPixelFormat( int bpp, int Rmask, int Gmask, int Bmask, int Amask, bool realtime=false ); 00323 00324 protected: 00325 RageMatrix GetPerspectiveMatrix(float fovy, float aspect, float zNear, float zFar); 00326 00327 // Different for D3D and OpenGL. Not sure why they're not compatible. -Chris 00328 virtual RageMatrix GetOrthoMatrix( float l, float r, float b, float t, float zn, float zf ); 00329 virtual RageMatrix GetFrustumMatrix( float l, float r, float b, float t, float zn, float zf ); 00330 00331 // 00332 // Matrix that adjusts position and scale of image on the screen 00333 // 00334 RageMatrix m_Centering; 00335 00336 // Called by the RageDisplay derivitives 00337 const RageMatrix* GetCentering() { return &m_Centering; } 00338 const RageMatrix* GetProjectionTop(); 00339 const RageMatrix* GetViewTop(); 00340 const RageMatrix* GetWorldTop(); 00341 const RageMatrix* GetTextureTop(); 00342 }; 00343 00344 00345 extern RageDisplay* DISPLAY; // global and accessable from anywhere in our program 00346 00347 #endif 00348 /* 00349 * Copyright (c) 2001-2004 Chris Danford, Glenn Maynard 00350 * All rights reserved. 00351 * 00352 * Permission is hereby granted, free of charge, to any person obtaining a 00353 * copy of this software and associated documentation files (the 00354 * "Software"), to deal in the Software without restriction, including 00355 * without limitation the rights to use, copy, modify, merge, publish, 00356 * distribute, and/or sell copies of the Software, and to permit persons to 00357 * whom the Software is furnished to do so, provided that the above 00358 * copyright notice(s) and this permission notice appear in all copies of 00359 * the Software and that both the above copyright notice(s) and this 00360 * permission notice appear in supporting documentation. 00361 * 00362 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00363 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00364 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF 00365 * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS 00366 * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT 00367 * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS 00368 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR 00369 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 00370 * PERFORMANCE OF THIS SOFTWARE. 00371 */

Generated on Thu Jan 27 20:57:27 2005 for StepMania by doxygen 1.3.7