00001
00002
00003
00004
00005 #include "ps2gl/dlist.h"
00006 #include "ps2gl/glcontext.h"
00007 #include "ps2gl/immgmanager.h"
00008
00009
00010
00011
00012
00013 CDList::CDList()
00014 : VertexBuf(NULL), NormalBuf(NULL), TexCoordBuf(NULL), ColorBuf(NULL),
00015 NumRenderPackets(0)
00016 {
00017 FlushCache(0);
00018 FirstCmdBlock = CurCmdBlock = new CDListCmdBlock;
00019 }
00020
00021 CDList::~CDList()
00022 {
00023 if ( VertexBuf ) delete VertexBuf;
00024 if ( NormalBuf ) delete NormalBuf;
00025 if ( TexCoordBuf ) delete TexCoordBuf;
00026
00027
00028 delete FirstCmdBlock;
00029 for ( int i = 0; i < NumRenderPackets; i++ )
00030 delete RenderPackets[i];
00031 }
00032
00033 void
00034 CDList::Begin()
00035 {
00036 }
00037
00038 CDmaPacket&
00039 CDList::GetVertexBuf()
00040 {
00041 if ( VertexBuf == NULL )
00042 VertexBuf = new CDmaPacket( kBufferMaxQwordLength, DMAC::Channels::vif1,
00043 Core::MemMappings::UncachedAccl );
00044 return *VertexBuf;
00045 }
00046
00047 CDmaPacket&
00048 CDList::GetNormalBuf()
00049 {
00050 if ( NormalBuf == NULL )
00051 NormalBuf = new CDmaPacket( kBufferMaxQwordLength, DMAC::Channels::vif1,
00052 Core::MemMappings::UncachedAccl );
00053 return *NormalBuf;
00054 }
00055
00056 CDmaPacket&
00057 CDList::GetTexCoordBuf()
00058 {
00059 if ( TexCoordBuf == NULL )
00060 TexCoordBuf = new CDmaPacket( kBufferMaxQwordLength, DMAC::Channels::vif1,
00061 Core::MemMappings::UncachedAccl );
00062 return *TexCoordBuf;
00063 }
00064
00065 CDmaPacket&
00066 CDList::GetColorBuf()
00067 {
00068 if ( ColorBuf == NULL )
00069 ColorBuf = new CDmaPacket( kBufferMaxQwordLength, DMAC::Channels::vif1,
00070 Core::MemMappings::UncachedAccl );
00071 return *ColorBuf;
00072 }
00073
00074
00075
00076
00077
00078 void
00079 CDListManager::FrameHasEnded()
00080 {
00081 CurBuffer = 1 - CurBuffer;
00082 for ( int i = 0; i < NumListsToBeFreed[CurBuffer]; i++ )
00083 delete ListsToBeFreed[CurBuffer][i];
00084 NumListsToBeFreed[CurBuffer] = 0;
00085 }
00086
00087 bool
00088 CDListManager::ListsAreFree( int firstListID, int numLists )
00089 {
00090 bool listsAreFree = true;
00091 for ( int i = 0; i < numLists; i++ )
00092 if ( Lists[firstListID + i] != NULL )
00093 listsAreFree = false;
00094 return listsAreFree;
00095 }
00096
00097 unsigned int
00098 CDListManager::GenLists( int numLists )
00099 {
00100
00101 int firstID = NextFreeListID;
00102
00103 mErrorIf( ! ListsAreFree(NextFreeListID, numLists),
00104 "Uh-oh.. time to write some real display list management code." );
00105
00106 NextFreeListID += numLists;
00107
00108
00109 if ( NextFreeListID == kMaxListID - 1 )
00110 NextFreeListID = 1;
00111
00112 mErrorIf( NextFreeListID >= kMaxListID, "Ran out of lists.. time to rewrite this code." );
00113
00114 return firstID;
00115 }
00116
00117 void
00118 CDListManager::DeleteLists( unsigned int firstListID, int numLists )
00119 {
00120 for ( int i = 0; i < numLists; i++ ) {
00121 if ( Lists[firstListID + i] ) {
00122
00123 AddListToBeFreed( Lists[firstListID + i] );
00124 Lists[firstListID + i] = NULL;
00125 }
00126 }
00127 }
00128
00129 void
00130 CDListManager::NewList( unsigned int listID, GLenum mode )
00131 {
00132 OpenListID = listID;
00133 OpenList = new CDList;
00134 OpenList->Begin();
00135 }
00136
00137 void
00138 CDListManager::EndList()
00139 {
00140 mErrorIf( OpenListID == 0, "There is no list to end!" );
00141
00142 if ( Lists[OpenListID] ) AddListToBeFreed( Lists[OpenListID] );
00143 Lists[OpenListID] = OpenList;
00144 Lists[OpenListID]->End();
00145 OpenListID = 0;
00146 OpenList = NULL;
00147 }
00148
00149 class CCallListCmd : public CDListCmd {
00150 unsigned int ListID;
00151 public:
00152 CCallListCmd( unsigned listID ) : ListID(listID) {}
00153 CDListCmd* Play() {
00154 pGLContext->GetDListManager().CallList( ListID );
00155 return CDListCmd::GetNextCmd( this );
00156 }
00157 };
00158
00159 void
00160 CDListManager::CallList( unsigned int listID )
00161 {
00162 if ( OpenListID != 0 ) {
00163
00164 *OpenList += CCallListCmd( listID );
00165 }
00166 else {
00167 mErrorIf( Lists[listID] == NULL, "List does not exist." );
00168 pGLContext->GetImmGeomManager().Flush();
00169 Lists[listID]->Play();
00170 }
00171 }
00172
00173
00174
00175
00176
00177
00178 GLuint glGenLists( GLsizei range )
00179 {
00180 return pGLContext->GetDListManager().GenLists( range );
00181 }
00182
00183 void glDeleteLists( GLuint list, GLsizei range )
00184 {
00185 pGLContext->GetDListManager().DeleteLists( list, range );
00186 }
00187
00188 void glNewList( GLuint listID, GLenum mode )
00189 {
00190 pGLContext->BeginDListDef( listID, mode );
00191 }
00192
00193 void glEndList( void )
00194 {
00195 pGLContext->EndDListDef();
00196 }
00197
00198 void glCallList( GLuint listID )
00199 {
00200 pGLContext->GetDListManager().CallList( listID );
00201 }