Jump to content

  • Log In with Google      Sign In   
  • Create Account

one star instead of many...


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 TransformedBG   Members   -  Reputation: 122

Like
0Likes
Like

Posted 08 February 2012 - 08:10 PM

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 star
bool 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[i].z -= star_velocity;
  stars[i].x = 30.0f - (rand()%60);
  stars[i].y = 30.0f - (rand()%60);

  stars[i].z = Near_Depth + (float)(rand()%(Far_Depth - Near_Depth));
  stars[i].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[i].z -= star_velocity;

  if(stars[i].z <= Near_Depth) stars[i].z = Far_Depth;
  if(stars[i].direction == 2)
  {
   stars[i].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..

Sponsor:

#2 kauna   Crossbones+   -  Reputation: 2508

Like
1Likes
Like

Posted 09 February 2012 - 05:19 AM

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!

#3 TransformedBG   Members   -  Reputation: 122

Like
0Likes
Like

Posted 09 February 2012 - 08:17 AM

You sir are my hero!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS