tags:

views:

145

answers:

1

the point of my code is for me to press f1 and it will scan 500 pixels down and 500 pixels and put them in a array (it just takes a box that is 500 by 500 of the screen). then after that when i hit end it will click on only on the color black or... what i set it to. anyway it has been doing odd stuff and i can't find why:

#include <iostream>
#include <windows.h>

using namespace std;

COLORREF rgb[499][499];
HDC hDC = GetDC(HWND_DESKTOP);

POINT main_coner;

BYTE rVal;
BYTE gVal;
BYTE bVal;

int red;  
int green;
int blue; 

int ff = 0;

int main()
{
for(;;)
{
    if(GetAsyncKeyState(VK_F1))
    {
        cout << "started";
        int a1 = 0;
        int a2 = 0;

        GetCursorPos(&main_coner);

        int x = main_coner.x;
        int y = main_coner.y;

        for(;;)
        {
            //cout << a1 << "___" << a2 << "\n";
            rgb[a1][a2] = GetPixel(hDC, x, y);
            a1++;
            x++;

            if(x > main_coner.x + 499)
            {
                y++;
                x = main_coner.x;
                a1 = 0;
                a2++;

            }
            if(y > main_coner.y + 499)
            {
                ff = 1;
                break;
            }
        }

        cout << "done";
        break;
    }
    if(ff == 1)
      break;

}

for(;;)
{
        if(GetAsyncKeyState(VK_END))
        {
            GetCursorPos(&main_coner);

            int x = main_coner.x;
            int y = main_coner.y;

            int a1 = -1;
            int a2 = -1;

            for(;;)
            {
                x++;
                a1++;
                rVal = GetRValue(rgb[a1][a2]);
                gVal = GetGValue(rgb[a1][a2]);
                bVal = GetBValue(rgb[a1][a2]);

                red   = (int)rVal;   // get the colors into __int8
                green = (int)gVal;   // get the colors into __int8
                blue  = (int)bVal;   // get the colors into __int8

                if(red == 0 && green == 0 && blue == 0)
                {

                    SetCursorPos(main_coner.x + x, main_coner.y + y);

                    mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
                    Sleep(10);
                    mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
                    Sleep(100);

                }

                if(x > main_coner.x + 499)
                {
                    a1 = 0;
                    a2++;
                }

                if(y > main_coner.y + 499)
                {
                    Sleep(100000000000);

                    break;
                }
                if(GetAsyncKeyState(VK_CONTROL))
                {
                    Sleep(100000);
                    break;
                }

            }

        }
}

for(;;)
{


    if(GetAsyncKeyState(VK_END))
    {
        break;
    }

}

return 0;
}

anyone see what's wrong with my code :( (feel free to add tags)

+2  A: 

If you want your rgb array to have 500x500 entries (numbered [0][0] to [499][499]), you'll need to declare it as COLORREF rgb[500][500];

Also, make sure you don't try to access rgb[a1][a2] where a2 == -1

Paul Baker
an array starts at 0 does it not? so 499 is 500 and also if you look at my code when a1 and a2 are first used they get a ++ at the start of each loop so it will push them up to 0 before it is used that way i get all 500.
blood
@blood - no, it isn't. When you specify an array size of 499 elements, they will be numbered 0 through 498. You need to specify 500 elements to get 0 through 499.
Ferruccio
o cool xD thanks i don't think that will fix it but =3 still tyvmalso u are right about the a2 sorry =3
blood
@blood:The indices start at zero, and end at *size-1*, so that the array has exactly *size* elements. So for an array of 499 elements, the last index is 498. 499 is **out of bounds**.
slacker
"they get a ++ at the start of each loop" - At the start of the second top-level loop in main(), you have: `int a1 = -1; int a2 = -1; for(;;) { x++; a1++; rVal = GetRValue(rgb[a1][a2]);` Note that there's no `a2++;`
Paul Baker
ok uhh well i fixed it i was forgetting to incress Y when x was 500 =3 but this also helped
blood