one star instead of many...

Started by
1 comment, last by TransformedBG 12 years, 2 months ago
Im just trying to make the old flying through space screen saver in direct X and could use a little help.

oh the star is just a basic struct:
struct STAR
{
float x; // Star X Posistion
float y; // Star Y Posistion
float z; // Star Z Position
float velocity; // Star Velocity
int direction;
DWORD color; // Star Color
bool hyperspace; //boost?
};


global variables
//Stars
IDirect3DVertexBuffer9 * star_VB;
const int TOTAL_STARS = 250;
STAR stars[TOTAL_STARS]; //star vertex
float star_velocity = 0.1f;
const int Far_Depth = 100;
const int Near_Depth = 20;


//how i create my starbool createStars(IDirect3DDevice9* device, const int Height, const int Width)
{
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(&proj,D3DX_PI * 0.25f , (float)Width/(float)Height, 0.01f, 1000.0f);//45, 1.0f, 0.1f, 500.0f));
device->SetTransform(D3DTS_PROJECTION,&proj);
time_t now = time(0);
srand((unsigned int )&now);
D3DXMATRIX V;
D3DXMatrixIdentity(&V);
device->SetTransform(D3DTS_WORLD,&V);
device->SetTransform(D3DTS_VIEW, &V);
for(int i=0; i < TOTAL_STARS;i++)
{
stars.z -= star_velocity;
stars.x = 30.0f - (rand()%60);
stars.y = 30.0f - (rand()%60);

stars.z = Near_Depth + (float)(rand()%(Far_Depth - Near_Depth));
stars.color = d3d::WHITE;
}
return true;
}


which i call in my winMain:
// WinMain
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd)
{
main_instance = hinstance;
if(!d3d::InitD3D(hinstance, Width, Height, true, D3DDEVTYPE_HAL, &Device))
{
::MessageBox(0, "InitD3D() - FAILED", 0, 0);
return 0;
}

if(!Setup())
{
::MessageBox(0, "Setup() - FAILED", 0, 0);
return 0;
}
bool test = true;
test = createStars(Device,Height,Width);

if(test == false)
{
::MessageBox(0, "createStars() - FAILED",0,0);
return 0;
}
d3d::EnterMsgLoop(Display);
Cleanup();
Device->Release();
return 0;
}


and i call the function render() in my display
bool render(IDirect3DDevice9* device)
{
if(!device)return false;
moveStars(device);

device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, d3d::BLACK, 1.0f,0);
device->BeginScene();
renderStars(device);

device->EndScene();
return true;
}


which calls movestars:
void moveStars(IDirect3DDevice9* device)
{
for(int i = 0; i < TOTAL_STARS; i++)
{
//move the star
stars.z -= star_velocity;

if(stars.z <= Near_Depth) stars.z = Far_Depth;
if(stars.direction == 2)
{
stars.x += 0.1f;
}
}
}


then renders them:
void renderStars(IDirect3DDevice9* device)
{
device->SetRenderState(D3DRS_LIGHTING,FALSE);
device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
device->SetTextureStageState(0, D3DTSS_COLORARG1,D3DTA_DIFFUSE);
device->CreateVertexBuffer(TOTAL_STARS*sizeof(stars),D3DUSAGE_WRITEONLY, CUSTOMFVF,D3DPOOL_MANAGED,&star_VB,NULL);

VOID* temp_pointer_vb;
star_VB->Lock(0,TOTAL_STARS*sizeof(stars),&temp_pointer_vb,0);
memcpy(temp_pointer_vb,stars,TOTAL_STARS);
star_VB->Unlock();
device->SetStreamSource(0,star_VB,0,sizeof(stars));
device->SetFVF(CUSTOMFVF);
device->DrawPrimitive(D3DPT_POINTLIST,0,TOTAL_STARS);
star_VB->Release();
}


Now problem im having is im only rendering 1 star somewhere on the screen over and over again... so the move function is obviously working... Problem is that it should be rendering 250 stars..

Just wondering if someone might have some thoughts on this..
Advertisement

memcpy(temp_pointer_vb,stars,TOTAL_STARS);

device->SetStreamSource(0,star_VB,0,sizeof(stars));


your memcpy has incorrect size : the last parameter should be TOTAL_STARS* sizeof(STAR)
SetStreamSource : the last parameter (vertex stride) should be sizeof(STAR) ie. size of a single vertex

Cheers!
You sir are my hero!

This topic is closed to new replies.

Advertisement