Exemple #1
0
wxDisplayImpl *wxDisplayFactoryDirectDraw::CreateDisplay(unsigned n)
{
    wxCHECK_MSG( n < m_displays.size(), NULL, wxT("invalid display index") );

    wxDisplayInfoDirectDraw *
        info = static_cast<wxDisplayInfoDirectDraw *>(m_displays[n]);

    if ( !info->m_pDD2 )
    {
        IDirectDraw *pDD;
        GUID guid(info->m_guid);
        HRESULT hr = (*m_pfnDirectDrawCreate)(&guid, &pDD, NULL);

        if ( FAILED(hr) || !pDD )
        {
            // what to do??
            wxLogApiError(wxT("DirectDrawCreate"), hr);
            return NULL;
        }

        // we got IDirectDraw, but we need IDirectDraw2
        hr = pDD->QueryInterface(wxIID_IDirectDraw2, (void **)&info->m_pDD2);
        pDD->Release();

        if ( FAILED(hr) || !info->m_pDD2 )
        {
            wxLogApiError(wxT("IDirectDraw::QueryInterface(IDD2)"), hr);
            return NULL;
        }

        // NB: m_pDD2 will now be only destroyed when m_displays is destroyed
        //     which is ok as we don't want to recreate DD objects all the time
    }
    //else: DirectDraw object corresponding to our display already exists

    return new wxDisplayImplDirectDraw(n, *info, info->m_pDD2);
}
Exemple #2
0
int main(int argc, char *argv[])
{
    /*! blitz window handle */
    HWND hWnd = 0;

    /*! blitz window client rectangle */
    RECT clientRect = { 0 };

    /*! directdraw interfaces */
    IDirectDraw         *pDD = NULL;
    IDirectDraw7        *pDD7 = NULL;

    /*! return code */
    HRESULT hRet;

    /*! advertise */
    {
        printf("autoblitz\n");
        printf("\n");
    }

    /*! ask user for blitz hwnd (found using spyxx, for example) */
    {
        printf("hwnd : ");
        scanf("%X", &hWnd);
    }

    /*! retrieve blitz window rectangle */
    GetWindowRect(hWnd, &windowRect);

    /*! retrieve blitz window client rectangle */
    GetClientRect(hWnd, &clientRect);

    /*! calculate blitz window width/height */
    windowWidth = windowRect.right - windowRect.left;
    windowHeight = windowRect.bottom - windowRect.top;

    /*! allocate rgb data */
    rgb32 = (uint32*)calloc(windowWidth*windowHeight*4, 1);

    /*! initialize capture */
    {
        rp_debug_trace("DirectDrawCreate\n");

        hRet = DirectDrawCreate(NULL, &pDD, NULL);

        if(FAILED(hRet)) { goto cleanup; }

        rp_debug_trace("pDD->QueryInterface\n");

        hRet = pDD->QueryInterface(IID_IDirectDraw7, (void**)&pDD7);

        if(FAILED(hRet)) { goto cleanup; }

        rp_debug_trace("pDD7->SetCooperativeLevel\n");

        hRet = pDD7->SetCooperativeLevel(NULL, DDSCL_NORMAL);

        if(FAILED(hRet)) { goto cleanup; }

        rp_debug_trace("pDD7->CreateSurface\n");

        /*! initialize surfaces */
        {
            DDSURFACEDESC2 ddsd;

            ZeroMemory(&ddsd, sizeof(ddsd));

            ddsd.dwSize = sizeof(ddsd);
            ddsd.dwFlags = DDSD_CAPS;
            ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;

            hRet = pDD7->CreateSurface(&ddsd, &pDDSurface7, NULL);

            if(FAILED(hRet)) { goto cleanup; }

            rp_debug_trace("pDD7->CreateSurface (Sys)\n");

            ZeroMemory(&ddsd, sizeof(DDSURFACEDESC2));
            ZeroMemory(&ddsd.ddpfPixelFormat, sizeof(DDPIXELFORMAT));

            ddsd.dwSize = sizeof(ddsd);
            ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_LPSURFACE | DDSD_PITCH | DDSD_PIXELFORMAT | DDSD_CAPS;
            ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
            ddsd.dwWidth = windowWidth;
            ddsd.dwHeight = windowHeight;
            ddsd.lPitch = windowWidth*4;
            ddsd.lpSurface = rgb32;
            ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
            ddsd.ddpfPixelFormat.dwFlags= DDPF_RGB;
            ddsd.ddpfPixelFormat.dwRGBBitCount = (DWORD)4*8;
            ddsd.ddpfPixelFormat.dwRBitMask = 0x00FF0000;
            ddsd.ddpfPixelFormat.dwGBitMask = 0x0000FF00;
            ddsd.ddpfPixelFormat.dwBBitMask = 0x000000FF;

            hRet = pDD7->CreateSurface(&ddsd, &pDDSysSurface7, NULL);

            if(FAILED(hRet)) { goto cleanup; }
        }

        rp_debug_trace("Init success!\n");

        /*! press button to start the game */
        click_window(hWnd, 229, 196);

        while(true)
        {
            /*! wait for user to tell us when to continue on to the next move */
            //int ret = MessageBox(0, L"Press OK to take the next move (wait for animation to stop)", L"autoblitz [blog.caustik.com]", MB_OKCANCEL);

            /*! if cancel was pressed, we're done */
            //if(ret == IDCANCEL) { break; }

            /*! if mouse cursor is within the flash window, wait */
            {
                POINT pt;

                GetCursorPos(&pt);

                if( (pt.x > windowRect.left) && (pt.x < windowRect.right)
                 && (pt.y > windowRect.top)  && (pt.y < windowRect.bottom) )
                {
                    printf("(cursor is inside window..waiting)\n");
                    rp_thread::sleep(100);
                    continue;
                }
            }

            /*! current grid */
            blockColor gameBoard[8][8];

            /*! capture the board */
            capture_board(gameBoard);

            /*! display gameboard */
            {
                printf("\n");
                printf("- current game board -\n");
                printf("\n");

                display_board(gameBoard);
            }

            /*! check for end of game */
            {
                int x;

                for(x=0;x<8;x++)
                {
                    if(gameBoard[7][x] != BC_BLUE) { break; }

                    /*! if last row was entirely blue, click the "play again?" button */
                    if(x == 7)
                    {
                        int clickX = baseX + eachW*1 + halfW;
                        int clickY = baseY + eachH*6 + halfH;

                        click_window(hWnd, clickX, clickY);
                    }
                }
            }

            /*! analyze gameBoard and make the best move */
            {
                int aX=0, aY=0, bX=0, bY=0;

                /*! analyze gameBoard */
                analyze_board(gameBoard, aX, aY, bX, bY);

                /*! click first piece */
                {
                    int clickX = baseX + eachW*aX + halfW;
                    int clickY = baseY + eachH*aY + halfH;

                    click_window(hWnd, clickX, clickY);
                }

                rp_thread::sleep(150);

                /*! click second piece */
                {
                    int clickX = baseX + eachW*bX + halfW;
                    int clickY = baseY + eachH*bY + halfH;

                    click_window(hWnd, clickX, clickY);
                }
            }
#if 0
            /*! wait for animation to start */
            rp_thread::sleep(250);

            /*! wait for board to stabilize */
            while(true)
            {
                /*! current grid */
                blockColor tmpBoard[8][8];

                /*! wait a little */
                rp_thread::sleep(150);

                /*! capture current game board */
                capture_board(tmpBoard);

                /*! if board has not changed, stop waiting */
                if(memcmp(tmpBoard, gameBoard, sizeof(tmpBoard)) == 0) { break; }

                /*! remember this board for next loop */
                memcpy(gameBoard, tmpBoard, sizeof(tmpBoard));
            }
#endif
        }
    }

cleanup:

    /*! cleanup rgb pixels */
    if(rgb32 != 0) { free(rgb32); }

    /*! cleanup system service */
    if(pDDSysSurface7 != 0) { pDDSysSurface7->Release(); }

    /*! cleanup primary surface */
    if(pDDSurface7 != 0) { pDDSurface7->Release(); }

    /*! cleanup directdraw7 */
    if(pDD7 != 0) { pDD7->Release(); }

    /*! cleanup directdraw */
    if(pDD != 0) { pDD->Release(); }

    return 0;
}