Tried than to store the values of the sprite vector(called gun) in an array of vectors (called ammo), and i can actually have more bullets on the screen at same time,but it's still not a good way to manage the sprite, since the uses are limited and if i try to reutilize the sprite after the 30th shoot (i gave a fixed ammount of space to the array for testing purposes) every bullet on the screen disappears and reset the bullet position. Now to my code:
This is my message handler:
flag it's a boolean variable.
key it's a boolean array.
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_INPUT:
{
UINT buffersize;
GetRawInputData((HRAWINPUT)lParam,RID_INPUT,NULL,&buffersize,sizeof(RAWINPUTHEADER));
BYTE * buffer=new BYTE[buffersize];
GetRawInputData((HRAWINPUT)lParam,RID_INPUT,(LPVOID)buffer,&buffersize,sizeof(RAWINPUTHEADER));
RAWINPUT* raw=(RAWINPUT*)buffer;
if(raw->header.dwType==RIM_TYPEKEYBOARD)
{
USHORT keycode=raw->data.keyboard.VKey;
bool flag=raw->data.keyboard.Flags & RI_KEY_BREAK;
if(flag==0)
{
if(keycode==VK_RIGHT)
key[0]=TRUE;
if(keycode==VK_LEFT)
key[1]=TRUE;
if(keycode==VK_SPACE)
key[2]=FALSE;
}
if(flag==1)
{
if(keycode==VK_RIGHT)
key[0]=FALSE;
if(keycode==VK_LEFT)
key[1]=FALSE;
if(keycode==VK_SPACE)
key[2]=TRUE;
}
}
}
break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
return 0;
}
This function checks the values of the keys (pressed or released).
It also gives the bullet starting position.
void controls()
{
if(key[0]){
ship.pos.x+=7.0f;
}
if(key[1]){
ship.pos.x-=7.0f;
}
if(key[2]){
if(count>29)
{
count=0;
gun.x=ship.pos.x;
gun.y=ship.pos.y;
ammo[count]=gun;
}
else
{
gun.x=ship.pos.x;
gun.y=ship.pos.y;
ammo[count]=gun;
ship.fire=TRUE;
key[2]=FALSE;
}
count++;
}
}
and than my draw function.
void draw()
{
device->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);
if(SUCCEEDED(device->BeginScene()))
{
spri->Begin(D3DXSPRITE_ALPHABLEND);
D3DXMATRIX mat;
D3DXMatrixIdentity(&mat);
spri->SetTransform(&mat);
spri->Draw(texship,NULL,NULL,&ship.pos,0xFFFFFFFF);
if(ship.fire==TRUE)
{
for(int i=0;i<count;i++)
{
spri->Draw(texgun,NULL,NULL,&ammo,0xFFFFFFFF);
ammo.y-=5.0f;
}
}
spri->End();
device->EndScene();
device->Present(NULL,NULL,NULL,NULL);
}
}
What i need to know is:
what's the best way to manage a sprite in this case (multiple copy of the same sprite) ?
Does the sprite has to be regulated by a timer to accomplish my goal?
I saw some solution for XNA: they create new object each time the draw function is called and add them to a list, but that's still a weird way IMO since my code would actually have an infinite amount of bullets at player's disposal.
Thanks for the help in advance.