views:

109

answers:

1

i have to event that should insert values in two vectors but when event get called for the first time it inserts the values successfully but when it get called again it will insert the values in the first vector but not in the second vector than when another event get called the second vector's values will be deleted.
here is the full code

#define MAX_LOADSTRING 100

RECT *rect;
const UINT_PTR EVERYTHING_ID=0x1;
const UINT_PTR LBUTTONDOWN_ID=0x3;
const UINT_PTR TDENEMIE1_ID=0x4;
const UINT_PTR TAENEMIE1_ID=0x5;
int conno=2;
int side=0;
int cEnemie1=0;
int dEnemie1=1;
int aEnemie1=0;
int sEnemie1=1;
bool e1=true;
time_t now;
time_t tEnemie1;

vector <POINT> vRegularShots;
vector <POINT> vS1Enemie1;
vector <POINT> vS2Enemie1;
VOID Paint(HDC hdc, HWND hWnd)
{
    hdc=GetDC(hWnd);
    HDC memDC=CreateCompatibleDC(hdc);
    HBITMAP hMemMap=CreateCompatibleBitmap(hdc, 225, 350);
    HBITMAP hOldMap=(HBITMAP)SelectObject(memDC, hMemMap);
    Graphics draw(memDC);

    // Drawing
    Image bg(L"bg.jpg");
    draw.DrawImage(&bg, 0, 0);

    // Regular shots
    Image shot(L"RegularShots.png");
    long s=vRegularShots.size();

    // Draw shots
    for(long index=0; index < (long)vRegularShots.size(); ++index) 
    { 
        draw.DrawImage(&shot, vRegularShots[index].x, vRegularShots[index].y);
    }

    // Update the shots
    for(long index=0; index < (long)vRegularShots.size(); ++index) 
    { 
        vRegularShots[index].y--;
    }

    // Create Enemies
    if(dEnemie1==0)
    {
        if(cEnemie1<2)
        {
            if(aEnemie1==0)
            {
                SetTimer(hWnd, TAENEMIE1_ID, 550, NULL);
            }
            aEnemie1=1;
            cEnemie1++;
        }
        else
        {
            KillTimer(hWnd, TDENEMIE1_ID);
        }
        dEnemie1=1;
    }

    // Draw enemies
    for(long index=0; index < (long)vS1Enemie1.size(); ++index) 
    {
        Image iEnemie1(L"Enemie1.png");
        draw.DrawImage(&iEnemie1, vS1Enemie1[index].x, vS1Enemie1[index].y);
    }
    for(long index=0; index < (long)vS2Enemie1.size(); ++index) 
    {
        Image iEnemie1(L"Enemie1.png");
        draw.DrawImage(&iEnemie1, vS2Enemie1[index].x, vS2Enemie1[index].y);
    }

    // Update enemies
    for(long index=0; index < (long)vS1Enemie1.size(); index++) 
    {
        vS1Enemie1[index].x++;
        vS1Enemie1[index].y++;
    }
    for(long index=0; index < (long)vS2Enemie1.size(); index++) 
    {
        vS2Enemie1[index].x--;
        vS2Enemie1[index].y++;
    }

    // Delete enemies
    for(long index=0; index < (long)vS1Enemie1.size(); index++) 
    {
        if(vS1Enemie1[index].x>225)
        {
            vS1Enemie1.erase(vS1Enemie1.begin()+index);
        }
    }
    for(long index=0; index < (long)vS2Enemie1.size(); index++) 
    {
        if(vS2Enemie1[index].x>225)
        {
            vS2Enemie1.erase(vS2Enemie1.begin()+index);
        }
    }

    BitBlt(hdc, 0, 0, 225, 350, memDC, 0, 0, SRCCOPY);
    ReleaseDC(hWnd, hdc);
    SelectObject(memDC, hOldMap);
    DeleteObject(hMemMap);
    DeleteDC(memDC);
}

VOID CheckDead()
{
    for(long index=0; index < (long)vRegularShots.size(); ++index) 
    { 
         vRegularShots[index].x, vRegularShots[index].y;
    }
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;
    POINT pt;
    POINT pts;

    switch (message)
    {
    case WM_CREATE:
        SetTimer(hWnd, EVERYTHING_ID, 1, NULL);
        break;
    case WM_COMMAND:
        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        // Parse the menu selections:
        switch (wmId)
        {
        case IDM_ABOUT:
            DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
            break;
        case IDM_EXIT:
            DestroyWindow(hWnd);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        break;
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        Paint(hdc, hWnd);
        EndPaint(hWnd, &ps);
        break;
    case WM_LBUTTONDOWN:
        GetCursorPos(&pt);
        ScreenToClient(hWnd, &pt);
        if(conno==1)
        {
            conno++;
        }
        else
        {
            pt.x+=18;
            conno--;
        }
        vRegularShots.push_back(pt);
        SetTimer(hWnd, LBUTTONDOWN_ID, 350, NULL); 
        InvalidateRect(hWnd, rect, false);
        break;
    case WM_LBUTTONUP:
        KillTimer(hWnd, LBUTTONDOWN_ID);
        break;
    case WM_TIMER:
        switch(wParam)
        {
        case EVERYTHING_ID:
            if(e1==true)
            {
                now=time(NULL);
                tEnemie1=now+1;
                e1=false;
            }
            now=time(NULL);
            if(now==tEnemie1)
            {
                SetTimer(hWnd, TDENEMIE1_ID, 550, NULL);
            }
            InvalidateRect(hWnd, rect, false);
            break;
        case LBUTTONDOWN_ID:
            GetCursorPos(&pt);
            ScreenToClient(hWnd, &pt);
            if(conno==1)
            {
                conno++;
            }
            else
            {
                pt.x+=18;
                conno--;
            }
            vRegularShots.push_back(pt);
            break;
        case TDENEMIE1_ID:
            pt.y=5;
            pt.x=-26;
            vS1Enemie1.push_back(pt);
            pt.y=52;
            pt.x=251;
            vS2Enemie1.push_back(pt);
            dEnemie1=0;
            InvalidateRect(hWnd, rect, false);
            break;
        case TAENEMIE1_ID:
            InvalidateRect(hWnd, rect, false);
            break;
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

i set a break point at dEnemie1=0 in TAENEMIE1_ID.
anyidea?

A: 

For one thing, these two loops have issues:

// Delete enemies
for(long index=0; index < (long)vS1Enemie1.size(); index++) 
{
    if(vS1Enemie1[index].x>225)
    {
        vS1Enemie1.erase(vS1Enemie1.begin()+index);
    }
}
for(long index=0; index < (long)vS2Enemie1.size(); index++) 
{
    if(vS2Enemie1[index].x>225)
    {
        vS2Enemie1.erase(vS2Enemie1.begin()+index);
    }
}

One issue with each is that when you erase the POINT at index index, then all POINTs at indices index + 1, index + 2, etc. are shifted to the left by one. So, when you increment index, you skip a POINT. The other issue is that you should be using the unsigned size_type as the type of your indexing variable rather than casting to long, which is signed.

Here is corrected code:

// Delete enemies
vector <POINT>::size_type index;
for(index=0; index < vS1Enemie1.size(); ) 
{
    if(vS1Enemie1[index].x>225)
    {
        vS1Enemie1.erase(vS1Enemie1.begin()+index);
    }
    else
        ++index;
}
for(index=0; index < vS2Enemie1.size(); ) 
{
    if(vS2Enemie1[index].x>225)
    {
        vS2Enemie1.erase(vS2Enemie1.begin()+index);
    }
    else
        ++index;
}

It would be even better if you used iterators:

// Delete enemies
vector <POINT>::iterator it;
for(it=vS1Enemie1.begin(); it != vS1Enemie1.end(); ) 
{
    if(it->x > 225)
    {
        it = vS1Enemie1.erase(it);
    }
    else
        ++it;
}
for(it=vS2Enemie1.begin(); it != vS2Enemie1.end(); ) 
{
    if(it->x > 225)
    {
        it = vS2Enemie1.erase(it);
    }
    else
        ++it;
}
Daniel Trebbien
And even better if you used the erase(remove()) pattern.
Martin York