FIRE.CPP 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "engine.h"
  5. #include "fire.h"
  6. #include "typedefs.h"
  7. #include "misc.h"
  8. #include "names.h"
  9. #include "error.h"
  10. #include "debug4g.h"
  11. #include "globals.h"
  12. #include "resource.h"
  13. #include "tile.h"
  14. #include <memcheck.h>
  15. /***********************************************************************
  16. * Defines
  17. **********************************************************************/
  18. #define kSize 128
  19. #define kSeedLines 3
  20. #define kPorchLines 4
  21. #define kSeedBuffers 16
  22. /***********************************************************************
  23. * Global variables
  24. **********************************************************************/
  25. int gDamping = 7; // use 7 for 64x64, 4 for 128x128
  26. /***********************************************************************
  27. * Local variables
  28. **********************************************************************/
  29. extern "C" BYTE CoolTable[];
  30. BYTE CoolTable[1024]; // contains the new heat value per frame per cell
  31. BYTE FrameBuffer[kSize * (kSize + kPorchLines + kSeedLines)];
  32. BYTE SeedBuffer[kSeedBuffers][kSize];
  33. int fireSize = kSize;
  34. BYTE *gCLU;
  35. /***********************************************************************
  36. * External declarations
  37. **********************************************************************/
  38. extern "C" void cdecl CellularFrame(void * buffer, int width, int height);
  39. /***********************************************************************
  40. * UpdateTile
  41. **********************************************************************/
  42. void UpdateTile( BYTE *pCLU, BYTE *pTile );
  43. #pragma aux UpdateTile = \
  44. "mov esi,OFFSET FrameBuffer" \
  45. "xor eax,eax" \
  46. "mov edx,[fireSize]" \
  47. "rowLoop:" \
  48. "push edi" \
  49. "mov ecx,[fireSize]" \
  50. "colLoop:" \
  51. "mov al,[esi]" \
  52. "inc esi" \
  53. "mov al,[ebx+eax]" \
  54. "mov [edi],al" \
  55. "add edi,[fireSize]" \
  56. "dec ecx" \
  57. "jnz colLoop" \
  58. "pop edi" \
  59. "inc edi" \
  60. "dec edx" \
  61. "jnz rowLoop" \
  62. parm [ EBX ] [ EDI ] \
  63. modify [ EAX EBX ECX EDX ESI EDI ];
  64. /***********************************************************************
  65. * InitSeedBuffers
  66. **********************************************************************/
  67. void InitSeedBuffers(void)
  68. {
  69. int i, j;
  70. BYTE c;
  71. for ( i = 0; i < kSeedBuffers; i++ )
  72. {
  73. for ( j = 0; j < fireSize; j += 2 )
  74. {
  75. c = (BYTE)rand();
  76. SeedBuffer[i][j + 0] = c;
  77. SeedBuffer[i][j + 1] = c;
  78. }
  79. }
  80. }
  81. /***********************************************************************
  82. * BuildCoolTable
  83. **********************************************************************/
  84. void BuildCoolTable( void )
  85. {
  86. int i;
  87. for (i = 0; i < 1024; i++)
  88. CoolTable[i] = (BYTE)ClipLow((i - gDamping) / 4, 0);
  89. }
  90. /***********************************************************************
  91. * FireInit
  92. **********************************************************************/
  93. void FireInit( void )
  94. {
  95. memset(FrameBuffer, 0, sizeof(FrameBuffer));
  96. BuildCoolTable();
  97. InitSeedBuffers();
  98. RESHANDLE hCLU = gSysRes.Lookup("RFIRE", ".CLU");
  99. if (hCLU == NULL)
  100. ThrowError("RFIRE.CLU not found", ES_ERROR);
  101. gCLU = (BYTE *)gSysRes.Lock(hCLU);
  102. // run a few frame to start the fire
  103. for (int i = 0; i < 50; i++)
  104. FireProcess();
  105. }
  106. /***********************************************************************
  107. * FireProcess
  108. **********************************************************************/
  109. #define kSeedLoc (gFireSize * (gFireSize + 2))
  110. void FireProcess( void )
  111. {
  112. int i;
  113. int nSeed = RandShort() & (kSeedBuffers - 1); // we need a secondary random number generator for view stuff!
  114. for (i = 0; i < kSeedLines; i++)
  115. memcpy(&FrameBuffer[(kSize + kPorchLines + i) * kSize], SeedBuffer[nSeed], kSize);
  116. CellularFrame(FrameBuffer, kSize, kSize + kPorchLines);
  117. UpdateTile(gCLU, tileLoadTile(kPicFire));
  118. }