/*
* Copyright (C) Freespace Source Code Project. All rights reserved.
*
* All source code herein is the property of FSCP. You may not sell
* or otherwise commercially exploit the source or things you created based on the
* source.
*
* Star particle class by Sticks
*/
#include "starparticle/starparticle.h"
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
SParticle *P = 0;
int sOffset = 0;
int sTotal = 0;
int sBatch = 0;
HRESULT hr;
DWORD FtoDw(float f)
{
return *((DWORD*)&f);
}
DWORD vbSize = sizeof (SParticle);
IDirect3DVertexBuffer8 *vb;
IDirect3DTexture8 *tex = NULL;
int starNum = 0;
void sp_init(int maxStars)
{
d3d_set_initial_render_state();
lpD3DDevice->SetRenderState(D3DRS_LIGHTING, false);
lpD3DDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, true);
lpD3DDevice->SetRenderState(D3DRS_POINTSCALEENABLE, true);
lpD3DDevice->SetRenderState(D3DRS_POINTSIZE, FtoDw(2.0f));
lpD3DDevice->SetRenderState(D3DRS_POINTSIZE_MIN, FtoDw(0.2f));
// control the size of the particle relative to distance
lpD3DDevice->SetRenderState(D3DRS_POINTSCALE_A, FtoDw(1.0f));
lpD3DDevice->SetRenderState(D3DRS_POINTSCALE_B, FtoDw(0.0f));
lpD3DDevice->SetRenderState(D3DRS_POINTSCALE_C, FtoDw(0.0f));
// use alpha from texture
lpD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
lpD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
lpD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
// lpD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
// lpD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ZERO);
HRESULT hr = 0;
hr = lpD3DDevice->CreateVertexBuffer(
maxStars * sizeof(SParticle),
D3DUSAGE_DYNAMIC | D3DUSAGE_POINTS | D3DUSAGE_WRITEONLY,
D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, // D3DPOOL_MANAGED can't be used with D3DUSAGE_DYNAMIC
&vb);
hr = D3DXCreateTextureFromFile(lpD3DDevice,"star.bmp",&tex);
lpD3DDevice->SetTexture(0, tex);
lpD3DDevice->SetStreamSource(0, vb, sizeof(SParticle));
// lpD3DDevice->GetVertexShader(IDirect3DVertexShader8** oldFVF);
}
void sp_destroy()
{
//lpD3DDevice->SetRenderState(D3DRS_LIGHTING, true);
//lpD3DDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, false);
//lpD3DDevice->SetRenderState(D3DRS_POINTSCALEENABLE, false);
//lpD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
// lpD3DDevice->SetVertexShader(oldFVF);
if (sBatch > 0) {
sp_render();
sBatch = 0;
}
d3d_set_initial_render_state();
}
void sp_add(vertex *pos)
{
if (sBatch == 0) {
vb->Lock(
sOffset,
sBatch * sizeof( SParticle ),
(BYTE**)&P,
sOffset ? D3DLOCK_NOOVERWRITE : D3DLOCK_DISCARD);
}
sBatch++;
sOffset++;
if (sBatch == 200) {
sp_render();
sBatch = 0;
}
P->x = pos->sx;
P->y = pos->sy;
P->z = 0.0f;
P->rhw = 1.0f;
P->color = 0xdddddddd;
P++;
}
void sp_render()
{
vb->Unlock();
hr = lpD3DDevice->DrawPrimitive(
D3DPT_POINTLIST,
sOffset,
sBatch);
}
It's a whole new set of point sprite code