// test_win.cpp : Defines the entry point for the application. // #include #include LPDIRECT3D9 gpD3DHandle; /**< Direct3D Handle */ LPDIRECT3DDEVICE9 gpD3DDevice; /**< The Direct3D Adapter */ IDirect3DSurface9 *gpD3DSurface; /**< Offscreen Direct3D Surface. MPlayer renders inside it. Uses colorspace MovieSrcFmt */ void D3DInit(HWND hWnd, HDC * hDC, HGLRC * hRC); void InitD3DViewPort(int width, int height); void InitD3DState(); void ShutdownD3D(HWND hWnd, HDC hDC, HGLRC hRC); void PaintProc(HWND hWnd); // Forward declarations of functions included in this code module: LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; WNDCLASSEX wc; HWND hWnd; HDC hDC; HGLRC hRC; RECT wndRect = { 0, 0, 640, 480 }; wc.cbSize = sizeof(WNDCLASSEX); wc.style = CS_CLASSDC; wc.lpfnWndProc = (WNDPROC)WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = NULL; wc.lpszMenuName = NULL; wc.lpszClassName = L"JIMBO"; wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if(!RegisterClassEx(&wc)) return FALSE; AdjustWindowRect(&wndRect, WS_OVERLAPPEDWINDOW, false); hWnd = CreateWindow(L"JIMBO", L"Native HW Mode", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, wndRect.right - wndRect.left, wndRect.bottom - wndRect.top, NULL, NULL, wc.hInstance, NULL); if (!hWnd) return FALSE; /* enable D3D for the window */ D3DInit(hWnd, &hDC, &hRC); ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); /* Main message loop: */ while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } /* shutdown OpenGL */ ShutdownD3D(hWnd, hDC, hRC); /* destroy the window explicitly */ DestroyWindow(hWnd); return (int) msg.wParam; } // // FUNCTION: WndProc(HWND, unsigned, WORD, LONG) // // PURPOSE: Processes messages for the main window. // // WM_COMMAND - process the application menu // WM_PAINT - Paint the main window // WM_DESTROY - post a quit message and return // // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_KEYDOWN: switch(wParam) { case VK_ESCAPE: PostQuitMessage(0); break; } break; case WM_PAINT: PaintProc(hWnd); break; case WM_ERASEBKGND: // cause the redraw not to clear the area return 1; case WM_DESTROY: PostQuitMessage(0); break; case WM_SIZE: { // intercept int width = (unsigned int)lParam & 0xFFFF; int height = (unsigned int)lParam >> 16; InitD3DViewPort(width, height); InvalidateRect(hWnd, 0, FALSE); } break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } /** @brief Destroy D3D Context related to the current window. */ void D3DDestroyContext (void) { /* Let's destroy the old (if any) D3D Content */ if (gpD3DSurface != NULL) { IDirect3DSurface9_Release (gpD3DSurface); gpD3DSurface = NULL; } if (gpD3DDevice != NULL) { IDirect3DDevice9_Release (gpD3DDevice); gpD3DDevice = NULL; } } void D3DInit(HWND hWnd, HDC * hDC, HGLRC * hRC) { D3DPRESENT_PARAMETERS PresentParams; D3DDISPLAYMODE DisplayMode; /* Set to zero all global variables. */ gpD3DHandle = 0; gpD3DDevice = 0; gpD3DSurface = 0; D3DDestroyContext(); /* FIXME > Please use subopt-helper.h for this, see vo_gl.c:preinit for > an example of how to use it. */ if ((gpD3DHandle = Direct3DCreate9 (D3D_SDK_VERSION)) == NULL) return; if (FAILED (IDirect3D9_GetAdapterDisplayMode (gpD3DHandle, D3DADAPTER_DEFAULT, &DisplayMode))) return; /* Prepare Direct3D initialization parameters. */ ZeroMemory(&PresentParams, sizeof(D3DPRESENT_PARAMETERS)); PresentParams.Windowed = TRUE; PresentParams.SwapEffect = D3DSWAPEFFECT_COPY; PresentParams.Flags = D3DPRESENTFLAG_VIDEO; PresentParams.hDeviceWindow = hWnd; /* w32_common var */ PresentParams.BackBufferWidth = 0; /* Fill up window Width */ PresentParams.BackBufferHeight = 0; /* Fill up window Height */ PresentParams.MultiSampleType = D3DMULTISAMPLE_NONE; /* D3DPRESENT_INTERVAL_ONE = vsync */ PresentParams.PresentationInterval = D3DPRESENT_INTERVAL_ONE; PresentParams.BackBufferFormat = DisplayMode.Format; PresentParams.BackBufferCount = 1; PresentParams.EnableAutoDepthStencil = FALSE; /* vo_w32_window is w32_common variable. It's a handle to the window. */ if (FAILED (IDirect3D9_CreateDevice(gpD3DHandle, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, NULL, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &PresentParams, &gpD3DDevice))) return; D3DFORMAT sf = (D3DFORMAT)MAKEFOURCC('Y','V','1','2'); if (FAILED (IDirect3DDevice9_CreateOffscreenPlainSurface( gpD3DDevice, 640, 480, sf, D3DPOOL_DEFAULT, &gpD3DSurface, NULL))) return; /* Fill the Surface with black color. */ IDirect3DDevice9_ColorFill(gpD3DDevice, gpD3DSurface, NULL, D3DCOLOR_ARGB(0xFF, 0, 0, 0) ); InitD3DState(); } // Disable D3D void ShutdownD3D(HWND hWnd, HDC hDC, HGLRC hRC) { /* Destroy D3D Context inside the window. */ D3DDestroyContext(); /* Stop the whole D3D. */ if (NULL != gpD3DHandle) { IDirect3D9_Release (gpD3DHandle); } ReleaseDC( hWnd, hDC ); }