TrailFrag.cpp
上传用户:henghua
上传日期:2007-11-14
资源大小:7655k
文件大小:3k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. #include "TrailFrag.h"
  2. TrailFrag::TrailFrag(LPDIRECT3DDEVICE9 device, int life, int texnum)
  3. {
  4. this->device = device;
  5. this->life = life;
  6. this->texnum = texnum;
  7. this->textures = new LPDIRECT3DTEXTURE9[texnum];
  8. this->head = new Particle;
  9. this->head->next = NULL;
  10. this->trail = head;
  11. }
  12. TrailFrag::~TrailFrag()
  13. {
  14. }
  15. bool TrailFrag::Init()
  16. {
  17. SNOWVERTEX vertices[] = 
  18. {
  19. {-1 , -1, 0, 0, 0}, 
  20. {-1, 1, 0, 0, 1}, 
  21. {1, 1, 0, 1, 1},
  22. {1, -1, 0, 1, 0}
  23. };
  24. if(FAILED(device->CreateVertexBuffer(4 * sizeof(SNOWVERTEX), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &(this->v_frag), NULL)))
  25. {
  26. return false;
  27. }
  28. void *pVertices;
  29. if(FAILED(v_frag->Lock(0, sizeof(vertices), (void**)&pVertices, 0)))
  30. return false;
  31. memcpy(pVertices, vertices, sizeof(vertices));
  32. v_frag->Unlock();
  33. this->AddTexture("textures\particle.tga", 0);
  34. return true;
  35. }
  36. bool TrailFrag::AddTexture(char * path, int t)
  37. {
  38. if(FAILED(D3DXCreateTextureFromFile(device, path, &(textures[t]))))
  39. {
  40. MessageBox(NULL, "ERROR WHILE LOADING frag TEXTURES", "OceanFlame", MB_OK);
  41. return false;
  42. }
  43. return true;
  44. }
  45. void TrailFrag::Update()
  46. {
  47. Particle *p = this->head;
  48. while(p->next)
  49. {
  50. p = p->next;
  51. p->frames -= 1;
  52. if(p->frames < 0)
  53. {
  54. head->next = p->next;
  55. delete p;
  56. p = head->next;
  57. }
  58. p->pos.x+=0.5;
  59. p->pos.y+= 0.5;
  60. p->pos.z+=0.5;
  61. }
  62. }
  63. void TrailFrag::Render()
  64. {
  65. D3DXMATRIX matYaw, matPitch, matTrans;
  66. D3DXMATRIXA16 matWorld, tmp;
  67. Particle *p = this->head;
  68. device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
  69. device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
  70. device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
  71. device->GetTransform(D3DTS_WORLD, &matWorld);
  72. tmp = matWorld;
  73. device->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
  74. device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  75. device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
  76. device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
  77. device->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );   
  78. device->SetRenderState( D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA); 
  79. device->SetRenderState( D3DRS_LIGHTING, false);
  80. device->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE);
  81. while(p->next)
  82. {
  83. p = p->next;
  84. D3DXMatrixRotationY(&matTrans, p->roty);
  85. D3DXMatrixTranslation(&matTrans, p->pos.x, p->pos.y, p->pos.z);
  86. // D3DXMatrixRotationZ(&matTrans, p->rotz * p->frames);
  87. device->SetTransform( D3DTS_WORLD,  &matTrans);
  88. device->SetTexture( 0, this->textures[0]);//int(float(texnum) * float(p->frames) / float(this->life))] ); 
  89. device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
  90. this->renderFrag();
  91. device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
  92. }
  93. device->SetTransform(D3DTS_WORLD, &tmp);
  94. }
  95. void TrailFrag::renderFrag()
  96. {
  97. device->SetStreamSource(0, v_frag, 0, sizeof(SNOWVERTEX));
  98. device->SetFVF(D3DFVF_SNOWVERTEX);
  99. device->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
  100. }
  101. void TrailFrag::newFrag(D3DXVECTOR3 pos, D3DXVECTOR3 vel, float roty, float rotz)
  102. {
  103. trail->next = new Particle;
  104. trail = trail->next;
  105. trail->pos = pos;
  106. trail->vel = vel;
  107. trail->frames = this->life;
  108. trail->roty = roty;
  109. trail->rotz = rotz;
  110. trail->next = NULL;
  111. }
  112. void TrailFrag::newFrag(float px, float py, float pz, float vx, float vy, float vz, float roty, float rotz)
  113. {
  114. this->newFrag(D3DXVECTOR3(px, py, pz), D3DXVECTOR3(vx, vy, vz), roty, rotz);
  115. }