Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
4.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: sheet code for particles and other sprite functions
  4. //
  5. //===========================================================================//
  6. #include "psheet.h"
  7. #include "tier1/UtlStringMap.h"
  8. #include "tier1/utlbuffer.h"
  9. #include "tier2/fileutils.h"
  10. // memdbgon must be the last include file in a .cpp file!!!
  11. #include "tier0/memdbgon.h"
  12. CSheet::CSheet( void )
  13. {
  14. memset( m_pSamples, 0, sizeof( m_pSamples ) );
  15. memset( m_bClamp, 0, sizeof( m_bClamp ) );
  16. }
  17. CSheet::CSheet( CUtlBuffer &buf )
  18. {
  19. memset( m_pSamples, 0, sizeof( m_pSamples ) );
  20. memset( m_bClamp, 0, sizeof( m_bClamp ) );
  21. memset( m_bSequenceIsCopyOfAnotherSequence, 0, sizeof( m_bSequenceIsCopyOfAnotherSequence ) );
  22. // lets read a sheet
  23. buf.ActivateByteSwappingIfBigEndian();
  24. int nVersion = buf.GetInt(); // version#
  25. int nNumCoordsPerFrame = (nVersion)?MAX_IMAGES_PER_FRAME_ON_DISK:1;
  26. int nNumSequences = buf.GetInt();
  27. while ( nNumSequences-- )
  28. {
  29. int nSequenceNumber = buf.GetInt();
  30. if ( ( nSequenceNumber < 0 ) || (nSequenceNumber >= MAX_SEQUENCES ) )
  31. {
  32. Warning("sequence number %d too high in sheet file!!!\n", nSequenceNumber);
  33. return;
  34. }
  35. m_bClamp[ nSequenceNumber ] = ( buf.GetInt() != 0 );
  36. int nFrameCount = buf.GetInt();
  37. // Save off how many frames we have for this sequence
  38. m_nNumFrames[ nSequenceNumber ] = nFrameCount;
  39. bool bSingleFrameSequence = ( nFrameCount == 1 );
  40. int nTimeSamples = bSingleFrameSequence ? 1 : SEQUENCE_SAMPLE_COUNT;
  41. m_pSamples[ nSequenceNumber ] =
  42. new SheetSequenceSample_t[ nTimeSamples ];
  43. int fTotalSequenceTime = buf.GetFloat();
  44. float InterpKnot[SEQUENCE_SAMPLE_COUNT];
  45. float InterpValue[SEQUENCE_SAMPLE_COUNT];
  46. SheetSequenceSample_t Samples[SEQUENCE_SAMPLE_COUNT];
  47. float fCurTime = 0.;
  48. for( int nFrm = 0 ; nFrm < nFrameCount; nFrm++ )
  49. {
  50. float fThisDuration = buf.GetFloat();
  51. InterpValue[ nFrm ] = nFrm;
  52. InterpKnot [ nFrm ] = SEQUENCE_SAMPLE_COUNT*( fCurTime/ fTotalSequenceTime );
  53. SheetSequenceSample_t &seq = Samples[ nFrm ];
  54. seq.m_fBlendFactor = 0.0f;
  55. for(int nImage = 0 ; nImage< nNumCoordsPerFrame; nImage++ )
  56. {
  57. SequenceSampleTextureCoords_t &s=seq.m_TextureCoordData[nImage];
  58. s.m_fLeft_U0 = buf.GetFloat();
  59. s.m_fTop_V0 = buf.GetFloat();
  60. s.m_fRight_U0 = buf.GetFloat();
  61. s.m_fBottom_V0 = buf.GetFloat();
  62. }
  63. if ( nNumCoordsPerFrame == 1 )
  64. seq.CopyFirstFrameToOthers();
  65. fCurTime += fThisDuration;
  66. m_flFrameSpan[nSequenceNumber] = fCurTime;
  67. }
  68. // now, fill in the whole table
  69. for( int nIdx = 0; nIdx < nTimeSamples; nIdx++ )
  70. {
  71. float flIdxA, flIdxB, flInterp;
  72. GetInterpolationData( InterpKnot, InterpValue, nFrameCount,
  73. SEQUENCE_SAMPLE_COUNT,
  74. nIdx,
  75. ! ( m_bClamp[nSequenceNumber] ),
  76. &flIdxA, &flIdxB, &flInterp );
  77. SheetSequenceSample_t sA = Samples[(int) flIdxA];
  78. SheetSequenceSample_t sB = Samples[(int) flIdxB];
  79. SheetSequenceSample_t &oseq = m_pSamples[nSequenceNumber][nIdx];
  80. oseq.m_fBlendFactor = flInterp;
  81. for(int nImage = 0 ; nImage< MAX_IMAGES_PER_FRAME_IN_MEMORY; nImage++ )
  82. {
  83. SequenceSampleTextureCoords_t &src0=sA.m_TextureCoordData[nImage];
  84. SequenceSampleTextureCoords_t &src1=sB.m_TextureCoordData[nImage];
  85. SequenceSampleTextureCoords_t &o=oseq.m_TextureCoordData[nImage];
  86. o.m_fLeft_U0 = src0.m_fLeft_U0;
  87. o.m_fTop_V0 = src0.m_fTop_V0;
  88. o.m_fRight_U0 = src0.m_fRight_U0;
  89. o.m_fBottom_V0 = src0.m_fBottom_V0;
  90. o.m_fLeft_U1 = src1.m_fLeft_U0;
  91. o.m_fTop_V1 = src1.m_fTop_V0;
  92. o.m_fRight_U1 = src1.m_fRight_U0;
  93. o.m_fBottom_V1 = src1.m_fBottom_V0;
  94. }
  95. }
  96. }
  97. // now, fill in all unseen sequences with copies of the first seen sequence to prevent crashes
  98. // while editing
  99. int nFirstSequence = -1;
  100. for(int i=0 ; i<MAX_SEQUENCES ; i++)
  101. {
  102. if ( m_pSamples[i] )
  103. {
  104. nFirstSequence = i;
  105. break;
  106. }
  107. }
  108. if ( nFirstSequence != -1 )
  109. {
  110. for(int i=0 ; i<MAX_SEQUENCES ; i++)
  111. {
  112. if ( m_pSamples[i] == NULL )
  113. {
  114. m_pSamples[i] = m_pSamples[nFirstSequence];
  115. m_bClamp[i]= m_bClamp[nFirstSequence];
  116. m_nNumFrames[i] = m_nNumFrames[nFirstSequence];
  117. m_bSequenceIsCopyOfAnotherSequence[i] = true;
  118. }
  119. }
  120. }
  121. }
  122. CSheet::~CSheet( void )
  123. {
  124. for( int i=0; i<NELEMS(m_pSamples); i++ )
  125. {
  126. if ( m_pSamples[i] && ( ! m_bSequenceIsCopyOfAnotherSequence[i] ) )
  127. {
  128. delete[] m_pSamples[i];
  129. }
  130. }
  131. }