⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ddvideo.cpp

📁 英特尔&#174 线程构建模块(英特尔&#174 TBB)是一个屡获殊荣的 C++ 运行时库
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*    Copyright 2005-2007 Intel Corporation.  All Rights Reserved.    This file is part of Threading Building Blocks.    Threading Building Blocks is free software; you can redistribute it    and/or modify it under the terms of the GNU General Public License    version 2 as published by the Free Software Foundation.    Threading Building Blocks is distributed in the hope that it will be    useful, but WITHOUT ANY WARRANTY; without even the implied warranty    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    GNU General Public License for more details.    You should have received a copy of the GNU General Public License    along with Threading Building Blocks; if not, write to the Free Software    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA    As a special exception, you may use this file as part of a free software    library without restriction.  Specifically, if other files instantiate    templates or use macros or inline functions from this file, or you compile    this file and link it with other files to produce an executable, this    file does not by itself cause the resulting executable to be covered by    the GNU General Public License.  This exception does not however    invalidate any other reasons why the executable file might be covered by    the GNU General Public License.*/// common Windows parts#include "winvideo.h"// and another headers#include <cassert>#include <stdio.h>#include <ddraw.h>#pragma comment(lib, "ddraw.lib")#pragma comment(lib, "dxguid.lib")LPDIRECTDRAW7               g_pDD = NULL;        // DirectDraw objectLPDIRECTDRAWSURFACE7        g_pDDSPrimary = NULL;// DirectDraw primary surfaceLPDIRECTDRAWSURFACE7        g_pDDSBack = NULL;   // DirectDraw back surfaceLPDIRECTDRAWSURFACE7        g_pDDSOverlay = NULL;// DirectDraw overlay surfaceLPDIRECTDRAWCLIPPER         g_pClipper = NULL;   // DirectDraw clipping structDDOVERLAYFX                 g_OverlayFX;         // DirectDraw overlay effects structDDCAPS                      g_DDCaps;            // DirectDraw hardware capabilities structDWORD                       g_OverlayFlags = 0;  // DirectDraw overlay flags variableDWORD                       g_dwXRatio,                            g_dwYRatio;          // The ratios between the src and dst rectsRECT                        g_rcSrc = {0, 0, 0, 0},                            g_rcDst = {0, 0, 0, 0};HANDLE                      g_hVSync;// check for new DX SDK (8 & 9)#ifdef DDSCAPS_PRIMARYSURFACELEFT#include <dxerr8.h>#pragma comment(lib, "dxerr8.lib")#else// old SDK (7)#include <d3dx.h>#pragma comment(lib, "d3dx.lib")#endif//! Create a dialog box and tell the user what went wrongbool DisplayError(LPSTR lpstrErr, HRESULT hres){    static bool InError = false;    int retval = 0;    if (!InError)    {        InError = true;#ifdef DDSCAPS_PRIMARYSURFACELEFT        const char *message = hres?DXGetErrorString8A(hres):0;#else        char message[256]; if(hres) D3DXGetErrorString(hres, 256, message);#endif        retval = MessageBoxA(g_hAppWnd, lpstrErr, hres?message:"Error!", MB_OK|MB_ICONERROR);        InError = false;    }    return false;}//! Releases the overlay surfacevoid DestroyOverlay(){    if (g_pClipper)        g_pClipper->Release();    if (g_pDDSOverlay) {        g_pImg = 0; LPDIRECTDRAWSURFACE7 pDDSOverlay(g_pDDSOverlay);        g_pDDSOverlay = NULL; Sleep(0); pDDSOverlay->Release(); // be sure nobody uses old value    }}//! Releases the primary surfacevoid DestroyPrimary(){    if (g_pDDSPrimary)    {        g_pDDSPrimary->Release();        g_pDDSPrimary = NULL;    }}//! Releases core DirectDraw objectsvoid DestroyDDraw(){    DestroyPrimary();    // Release the DDraw object    if (g_pDD) {        LPDIRECTDRAW7 pDD(g_pDD); // be sure nobody uses old value        g_pDD = NULL; Sleep(1); pDD->Release();    }}//! Checks and corrects all boundries for alignment and stretchingvoid CheckBoundries(void){    // Make sure the coordinates fulfill the stretching requirements.  Often    // the hardware will require a certain ammount of stretching to do    // overlays. This stretch factor is held in dwMinOverlayStretch as the    // stretch factor multiplied by 1000 (to keep an accuracy of 3 decimal places).    if ((g_DDCaps.dwCaps & DDCAPS_OVERLAYSTRETCH) && (g_DDCaps.dwMinOverlayStretch)        && (g_dwXRatio < g_DDCaps.dwMinOverlayStretch))    {        g_rcDst.right = 2 * GetSystemMetrics(SM_CXSIZEFRAME) + g_rcDst.left + (g_sizex                                 * (g_DDCaps.dwMinOverlayStretch + 1)) / 1000;        SetWindowTextA(g_hAppWnd, "Window is too small!");    }    else if ((g_DDCaps.dwCaps & DDCAPS_OVERLAYSTRETCH) && (g_DDCaps.dwMaxOverlayStretch)        && (g_dwXRatio > g_DDCaps.dwMaxOverlayStretch))    {        g_rcDst.right = 2 * GetSystemMetrics(SM_CXSIZEFRAME) + g_rcDst.left + (g_sizey                               * (g_DDCaps.dwMaxOverlayStretch + 999)) / 1000;        SetWindowTextA(g_hAppWnd, "Window is too large!");    }    else if(!g_video->calc_fps) SetWindowText(g_hAppWnd, g_video->title);    // Recalculate the ratio's for the upcoming calculations    g_dwXRatio = (g_rcDst.right - g_rcDst.left) * 1000 / (g_rcSrc.right - g_rcSrc.left);    g_dwYRatio = (g_rcDst.bottom - g_rcDst.top) * 1000 / (g_rcSrc.bottom - g_rcSrc.top);    // Check to make sure we're within the screen's boundries, if not then fix    // the problem by adjusting the source rectangle which we draw from.    if (g_rcDst.left < 0)    {        g_rcSrc.left = -g_rcDst.left * 1000 / g_dwXRatio;        g_rcDst.left = 0;    }    if (g_rcDst.right > GetSystemMetrics(SM_CXSCREEN))    {        g_rcSrc.right = g_sizex - ((g_rcDst.right - GetSystemMetrics(SM_CXSCREEN)) * 1000 / g_dwXRatio);        g_rcDst.right = GetSystemMetrics(SM_CXSCREEN);    }    if (g_rcDst.bottom > GetSystemMetrics(SM_CYSCREEN))    {        g_rcSrc.bottom = g_sizey - ((g_rcDst.bottom - GetSystemMetrics(SM_CYSCREEN)) * 1000 / g_dwYRatio);        g_rcDst.bottom = GetSystemMetrics(SM_CYSCREEN);    }    // I don't know how useful this is... but just in case someone can do it - here's the check.    if (g_rcDst.top < 0)    {        g_rcSrc.top = -g_rcDst.top * 1000 / g_dwYRatio;        g_rcDst.top = 0;    }    // Make sure the coordinates fulfill the alignment requirements    // these expressions (x & -y) just do alignment by dropping low order bits...    // so to round up, we add first, then truncate.    if ((g_DDCaps.dwCaps & DDCAPS_ALIGNBOUNDARYSRC) && g_DDCaps.dwAlignBoundarySrc)        g_rcSrc.left = (g_rcSrc.left + g_DDCaps.dwAlignBoundarySrc / 2) & -(signed)            (g_DDCaps.dwAlignBoundarySrc);    if ((g_DDCaps.dwCaps & DDCAPS_ALIGNSIZESRC) && g_DDCaps.dwAlignSizeSrc)        g_rcSrc.right = g_rcSrc.left + (g_rcSrc.right - g_rcSrc.left + g_DDCaps.dwAlignSizeSrc                                   / 2) & -(signed) (g_DDCaps.dwAlignSizeSrc);    if ((g_DDCaps.dwCaps & DDCAPS_ALIGNBOUNDARYDEST) && g_DDCaps.dwAlignBoundaryDest)        g_rcDst.left = (g_rcDst.left + g_DDCaps.dwAlignBoundaryDest / 2) & -(signed)            (g_DDCaps.dwAlignBoundaryDest);    if ((g_DDCaps.dwCaps & DDCAPS_ALIGNSIZEDEST) && g_DDCaps.dwAlignSizeDest)        g_rcDst.right = g_rcDst.left + (g_rcDst.right - g_rcDst.left) & -(signed) (g_DDCaps.dwAlignSizeDest);}//! Get translated by system color valueDWORD DDColorMatch(IDirectDrawSurface7 * pdds, COLORREF rgb){    COLORREF       rgbT;    HDC            hdc;    DWORD          dw = CLR_INVALID;    DDSURFACEDESC2 ddsd;    HRESULT        hres;    //  Use GDI SetPixel to color match for us    if (rgb != CLR_INVALID && pdds->GetDC(&hdc) == DD_OK) {        rgbT = GetPixel(hdc, 0, 0);     // Save current pixel value        SetPixel(hdc, 0, 0, rgb);       // Set our value        pdds->ReleaseDC(hdc);    }    // Now lock the surface so we can read back the converted color    ddsd.dwSize = sizeof(ddsd);    while ((hres = pdds->Lock(NULL, &ddsd, 0, NULL)) == DDERR_WASSTILLDRAWING)        Sleep(0);    if (hres == DD_OK) {        dw = *(DWORD *) ddsd.lpSurface;                 // Get DWORD        if (ddsd.ddpfPixelFormat.dwRGBBitCount < 32)            dw &= (1 << ddsd.ddpfPixelFormat.dwRGBBitCount) - 1;  // Mask it to bpp        pdds->Unlock(NULL);    }    else return DisplayError("Can't lock primary surface", hres);    //  Now put the color that was there back.    if (rgb != CLR_INVALID && pdds->GetDC(&hdc) == DD_OK) {        SetPixel(hdc, 0, 0, rgbT);        pdds->ReleaseDC(hdc);    }    return dw;}//! Load the bitmap and copy it to the overlay surfacebool DrawOverlay(){    HRESULT        hRet;       // This is where we put return values from DirectDraw.    DDSURFACEDESC2 surfDesc;    // Setup structure    memset(&surfDesc, 0, sizeof(surfDesc)); surfDesc.dwSize = sizeof(surfDesc);    hRet = g_pDDSOverlay->Lock(NULL, &surfDesc, DDLOCK_SURFACEMEMORYPTR | DDLOCK_NOSYSLOCK | DDLOCK_WRITEONLY, NULL);    if (hRet != DD_OK ||  surfDesc.lpSurface == NULL)        return DisplayError("Can't lock overlay surface", hRet);    else {        g_pImg = (unsigned int *)surfDesc.lpSurface;        //g_pDDSOverlay->Unlock(NULL); is not needed?    }    // Setup effects structure    memset(&g_OverlayFX, 0, sizeof(g_OverlayFX)); g_OverlayFX.dwSize = sizeof(g_OverlayFX);    // Setup overlay flags.    g_OverlayFlags = DDOVER_SHOW;    // Check for destination color keying capability    if ((g_DDCaps.dwCKeyCaps & DDCKEYCAPS_DESTOVERLAY) && ((g_DDCaps.dwCaps & DDCAPS_OVERLAYCANTCLIP) || (g_DDCaps.dwCKeyCaps & DDCKEYCAPS_NOCOSTOVERLAY) ))    {        // If so, we'll use it to clip the bitmap when other windows go on top        // of us. Just for the record - this color range for color keying (the        // high/low values) are not heavily supported right now, so for almost        // all cards, just use the same color for both.        g_OverlayFX.dckDestColorkey.dwColorSpaceLowValue =        g_OverlayFX.dckDestColorkey.dwColorSpaceHighValue = DDColorMatch(g_pDDSPrimary, RGBKEY);        g_OverlayFlags |= DDOVER_DDFX | DDOVER_KEYDESTOVERRIDE;    } else {        // If not, we'll setup a clipper for the window.  This will fix the        // problem on a few video cards - but the ones that don't shouldn't care.        hRet = g_pDD->CreateClipper(0, &g_pClipper, NULL);        if (hRet != DD_OK)            return DisplayError("Can't create clipper", hRet);        hRet = g_pClipper->SetHWnd(0, g_hAppWnd);        if (hRet != DD_OK)            return DisplayError("Can't attach clipper", hRet);        hRet = g_pDDSPrimary->SetClipper(g_pClipper);        if (hRet != DD_OK)            return DisplayError("Can't set clipper", hRet);    }    return true;}//! Init the primary surfacebool DDPrimaryInit(){    HRESULT        hRet;    DDSURFACEDESC2 ddsd;  // A surface description structure    // Create the primary surface.  The primary surface is the full screen -    // since we're a windowed app - we'll just write to the portion of the    // screen within our window.    memset(&ddsd, 0, sizeof(ddsd)); // Set all fields of struct to 0 and set .dwSize to    ddsd.dwSize = sizeof(ddsd);     // Sizeof the variable - these two steps required for most DDraw structs    ddsd.dwFlags = DDSD_CAPS;       // Set flags for variables we're using...    ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;  // Set the variables we said we would in dwFlags    hRet = g_pDD->CreateSurface(&ddsd, &g_pDDSPrimary, NULL);    if (hRet != DD_OK)        return DisplayError("Can't create primary surface", hRet);    return true;}//! Init DirectDraw Stuffbool DDInit(){    HRESULT hRet;    g_rcSrc.right = g_sizex;    g_rcSrc.bottom = g_sizey;    hRet = DirectDrawCreateEx(NULL, (VOID**)&g_pDD, IID_IDirectDraw7, NULL);    if (hRet != DD_OK)        return DisplayError("Can't create DirectDraw7 instance", hRet);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -