//----------------------------------------------------------------------------- // Name: CreateSurface() // Desc: creates a offscreen plain surface //----------------------------------------------------------------------------- LPDIRECTDRAWSURFACE7 CreateSurface(int width, int height, SCCOLOR TransparentColor) {// this function creates an offscreen plain surface //DDSURFACEDESC2 ddsd; // working description LPDIRECTDRAWSURFACE7 lpdds; // temporary surface // set to access caps, width, and height memset(&ddsd,0,sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT; // set dimensions of the new bitmap surface ddsd.dwWidth = width; ddsd.dwHeight = height; // set surface to offscreen plain ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;//default is video memory VRAM // create the surface ddReturnVal = lpddObj->CreateSurface(&ddsd,&lpdds,NULL); if(ddReturnVal == DDERR_OUTOFVIDEOMEMORY)//out of vram { // set surface to offscreen plain system memory ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY; ddReturnVal = lpddObj->CreateSurface(&ddsd,&lpdds,NULL); } if (DDFailedCheck(ddReturnVal, "CreateSurface() failed", cpErrorBuf ))//DDERR_OUTOFVIDEOMEMORY DDSCAPS_SYSTEMMEMORY { MessageBox(main_window_handle, cpErrorBuf, "CreateSurface()", MB_ICONEXCLAMATION); return(NULL); } // set color key to TransparentColor DDCOLORKEY color_key; // used to set color key color_key.dwColorSpaceLowValue = BIULDCOLOR(TransparentColor.r,TransparentColor.g,TransparentColor.b); color_key.dwColorSpaceHighValue = BIULDCOLOR(TransparentColor.r,TransparentColor.g,TransparentColor.b); // now set the color key for source blitting ddReturnVal = lpdds->SetColorKey(DDCKEY_SRCBLT, &color_key); if (DDFailedCheck(ddReturnVal, "SetColorKey() failed", cpErrorBuf )) { MessageBox(main_window_handle, cpErrorBuf, "CreateSurface()", MB_ICONEXCLAMATION); return(NULL); } // return surface return(lpdds); } // end CreateSurface()
LPDIRECTDRAWSURFACE7 DDraw_Create_Surface(int width, int height, int mem_flags, int color_key = 0) { // this function creates an offscreen plain surface DDSURFACEDESC2 ddsd; // working description LPDIRECTDRAWSURFACE7 lpdds; // temporary surface // 清空ddsd然后再设置width, and height来创建离屏表面 memset(&ddsd,0,sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT; // set dimensions of the new bitmap surface ddsd.dwWidth = width; ddsd.dwHeight = height; // set surface to offscreen plain ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | mem_flags; // create the surface if (FAILED(lpdd->CreateSurface(&ddsd,&lpdds,NULL))) return(NULL); // 设置色彩键、也就是透明色、默认设置为黑色 // test if user wants a color key if (color_key >= 0) { // set color key to color 0 DDCOLORKEY color_key; // used to set color key color_key.dwColorSpaceLowValue = 0; color_key.dwColorSpaceHighValue = 0; // now set the color key for source blitting lpdds->SetColorKey(DDCKEY_SRCBLT, &color_key); } // end if // return surface return(lpdds); // return surface return(lpdds); } // end DDraw_Create_Surface
//----------------------------------------------------------------------------- // Name: InitDD() // Desc: Initializes Driect draw and anything that needs to be inited //----------------------------------------------------------------------------- int InitDD() {// this function is where i do all the initialization // for the game // create dd object and test for error ddReturnVal = DirectDrawCreateEx(NULL, (void **)&lpddObj, IID_IDirectDraw7, NULL); if(DDFailedCheck(ddReturnVal, "DirectDrawCreateEx() failed", cpErrorBuf )) { MessageBox(main_window_handle, cpErrorBuf, "Init()", MB_ICONEXCLAMATION); return (0); } /* //set cooperation level to windowed mode normal ddReturnVal = lpddObj->SetCooperativeLevel(main_window_handle, DDSCL_NORMAL); if (DDFailedCheck(ddReturnVal, "SetCooperativeLevel() failed", cpErrorBuf )) { MessageBox(main_window_handle, cpErrorBuf, "Init()", MB_ICONEXCLAMATION); return(0); } */ //set cooperation level to full screen ddReturnVal = lpddObj->SetCooperativeLevel(main_window_handle, DDSCL_ALLOWMODEX | DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT); if (DDFailedCheck(ddReturnVal, "SetCooperativeLevel() failed", cpErrorBuf )) { MessageBox(main_window_handle, cpErrorBuf, "Init()", MB_ICONEXCLAMATION); return(0); } // set the display mode ddReturnVal = lpddObj->SetDisplayMode(WINDOW_WIDTH,WINDOW_HEIGHT,BPP,0,0); if (DDFailedCheck(ddReturnVal, "SetDisplayMode() failed", cpErrorBuf )) { MessageBox(main_window_handle, cpErrorBuf, "Init()", MB_ICONEXCLAMATION); return(0); } // Create the primary surface first set the fields memset(&ddsd,0,sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT; //set the flags to validate both capabilites field adn the backbuffer count field ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX; //tell dd that u have a complex flippable surface ddsd.dwBackBufferCount = 1; //set the back buffer count //Createt the primary surface ddReturnVal = lpddObj->CreateSurface(&ddsd,&lpddsPrimary,NULL); if (DDFailedCheck(ddReturnVal, "SetCooperativeLevel() failed", cpErrorBuf )) { MessageBox(main_window_handle, cpErrorBuf, "Init()", MB_ICONEXCLAMATION); return(0); } //Query for the backbuffer, use the ddscaps to indicate what you're requesting ddscaps.dwCaps = DDSCAPS_BACKBUFFER; //get the surface ddReturnVal = lpddsPrimary->GetAttachedSurface(&ddscaps, &lpddsSecondary); if (DDFailedCheck(ddReturnVal, "GetAttachedSurface() failed", cpErrorBuf )) { MessageBox(main_window_handle, cpErrorBuf, "Init()", MB_ICONEXCLAMATION); return(0); } //Attach a clipper RECT cliplist[1]; cliplist[0].top = 0; cliplist[0].left = 0; cliplist[0].bottom = GetSystemMetrics(SM_CYSCREEN); cliplist[0].right = GetSystemMetrics(SM_CXSCREEN); AttachClipper(lpddsSecondary,1,cliplist); AttachClipper(lpddsPrimary,main_window_handle); //attack a color key // set color key to TransparentColor DDCOLORKEY color_key; // used to set color key if(BPP == 16) { color_key.dwColorSpaceLowValue = BIULDCOLOR(TransColor.r,TransColor.g,TransColor.b); color_key.dwColorSpaceHighValue = BIULDCOLOR(TransColor.r,TransColor.g,TransColor.b); } else if(BPP == 32) { color_key.dwColorSpaceLowValue = _ARGB24BIT(0,TransColor.r,TransColor.g,TransColor.b); color_key.dwColorSpaceHighValue = _ARGB24BIT(0,TransColor.r,TransColor.g,TransColor.b); } // now set the color key for source blitting ddReturnVal = lpddsSecondary->SetColorKey(DDCKEY_SRCBLT, &color_key); if (DDFailedCheck(ddReturnVal, "SetColorKey() failed", cpErrorBuf )) { MessageBox(main_window_handle, cpErrorBuf, "Init()", MB_ICONEXCLAMATION); return(0); } //Fill in the wndRect struct wndRect.top = 0; wndRect.left = 0; wndRect.bottom = WINDOW_HEIGHT; wndRect.right = WINDOW_WIDTH; return(1); }
//----------------------------------------------------------------------------- // Name: Init() // Desc: Initializes Driect draw and anything that needs to be inited //----------------------------------------------------------------------------- int InitDD() {// this function is where i do all the initialization // for the game // create dd object and test for error ddReturnVal = DirectDrawCreateEx(NULL, (void **)&lpddObj, IID_IDirectDraw7, NULL); if (DDFailedCheck(ddReturnVal, "DirectDrawCreateEx() failed", cpErrorBuf )) { MessageBox(main_window_handle, cpErrorBuf, "Init()", MB_ICONEXCLAMATION); return(0); } // set cooperation level to windowed mode normal ddReturnVal = lpddObj->SetCooperativeLevel(main_window_handle, DDSCL_NORMAL); if (DDFailedCheck(ddReturnVal, "SetCooperativeLevel() failed", cpErrorBuf )) { MessageBox(main_window_handle, cpErrorBuf, "Init()", MB_ICONEXCLAMATION); return(0); } /* // set the display mode ddReturnVal = lpddObj->SetDisplayMode(nWindowWidth,nWindowHeight,BPP,0,0); if (DDFailedCheck(ddReturnVal, "SetDisplayMode() failed", cpErrorBuf )) { MessageBox(main_window_handle, cpErrorBuf, "Init()", MB_ICONEXCLAMATION); return(0); } */ // Create the primary surface first set the fields memset(&ddsd,0,sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS; //set the flags to validate both capabilites field adn the backbuffer count field ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; //tell dd that u have a complex flippable surface //ddsd.dwBackBufferCount = 1; //set the back buffer count //Createt the primary surface ddReturnVal = lpddObj->CreateSurface(&ddsd,&lpddsPrimary,NULL); if (DDFailedCheck(ddReturnVal, "CreateSurface() failed", cpErrorBuf )) { MessageBox(main_window_handle, cpErrorBuf, "Init()", MB_ICONEXCLAMATION); return(0); } //Create an off-screen surface as a backBuffer SCCOLOR col={255,0,255}; lpddsSecondary = CreateSurface(WINDOW_WIDTH, WINDOW_HEIGHT, col); //Attach a clipper RECT cliplist[1]; cliplist[0].top = iWindow_y; cliplist[0].left = iWindow_x; cliplist[0].bottom = iWindow_y+nWindowHeight;//GetSystemMetrics(SM_CYSCREEN); cliplist[0].right = iWindow_x+nWindowWidth;//GetSystemMetrics(SM_CXSCREEN); AttachClipper(lpddsPrimary,main_window_handle); //attack a color key // set color key to TransparentColor DDCOLORKEY color_key; // used to set color key color_key.dwColorSpaceLowValue = _RGB16BIT565(TransColor.r,TransColor.g,TransColor.b); color_key.dwColorSpaceHighValue = _RGB16BIT565(TransColor.r,TransColor.g,TransColor.b); // now set the color key for source blitting ddReturnVal = lpddsPrimary->SetColorKey(DDCKEY_SRCBLT, &color_key); if (DDFailedCheck(ddReturnVal, "SetColorKey() failed", cpErrorBuf )) { MessageBox(main_window_handle, cpErrorBuf, "Init()", MB_ICONEXCLAMATION); return(0); } //"""""""""""""""""""""Init Sprites""""""""""""""""""""" static int array[]={0,1}, array2[]= {0,1,2,3,4,5,6,7,8,9,10,11,12,13}; Sprites[0].InitSprite(64,64,1,1,1,120); Sprites[0].SetAnimations(array,0,SPRITE_ANIM_ONCE,0); Sprites[0].LoadSpriteImage("myface1.bmp"); Sprites[0].SetState(SPRITE_STATE_ALIVE); Sprites[0].SetXPos(100); Sprites[0].SetYPos(100); Sprites[1].InitSprite(43,37,14,6,3,120); Sprites[1].SetAnimations(array2,13,SPRITE_ANIM_CONT,0); Sprites[1].LoadSpriteImage("Expl16.bmp"); Sprites[1].SetState(SPRITE_STATE_ALIVE); Sprites[1].SetXPos(200); Sprites[1].SetYPos(100); //""""""""""""""""""End Init Sprites"""""""""""""""""""" return(1); }
static HRESULT CreateWindowedDisplay(HWND hWnd, DWORD dwWidth, DWORD dwHeight) { // Set cooperative level if (FAILED(lpdd->SetCooperativeLevel(hWnd, DDSCL_NORMAL))) { return E_FAIL; } RECT rcWork; RECT rc; DWORD dwStyle; // If we are still a WS_POPUP window // We should convert to a normal app window so we look like a windows app. dwStyle = GetWindowLong(hWnd, GWL_STYLE); dwStyle &= ~WS_POPUP; dwStyle |= WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX; SetWindowLong(hWnd, GWL_STYLE, dwStyle); // Adapt window size SetRect(&rc, 0, 0, dwWidth, dwHeight); AdjustWindowRectEx(&rc, GetWindowStyle(hWnd), GetMenu(hWnd) != NULL, GetWindowExStyle(hWnd)); SetWindowPos(hWnd, NULL, 0, 0, rc.right - rc.left, rc.bottom - rc.top, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE); SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE); // Make sure our window does not hang outside of the work area SystemParametersInfo(SPI_GETWORKAREA, 0, &rcWork, 0); GetWindowRect(hWnd, &rc); if (rc.left < rcWork.left) rc.left = rcWork.left; if (rc.top < rcWork.top) rc.top = rcWork.top; SetWindowPos(hWnd, NULL, rc.left, rc.top, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE); // Create the primary surface DDSURFACEDESC2 ddsd; DDRAW_INIT_STRUCT(ddsd); ddsd.dwFlags = DDSD_CAPS; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; if (FAILED(lpdd->CreateSurface(&ddsd, &lpddsMain, NULL))) { return E_FAIL; } // Create the backbuffer surface ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CKSRCBLT; ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE; ddsd.dwWidth = dwWidth; ddsd.dwHeight = dwHeight; if (FAILED(lpdd->CreateSurface(&ddsd, &lpddsBack, NULL))) { return E_FAIL; } // Set surface color key DDCOLORKEY ddCK; ddCK.dwColorSpaceLowValue = 0; ddCK.dwColorSpaceHighValue = 0; if (lpddsBack->SetColorKey(DDCKEY_SRCBLT, &ddCK)) { return E_FAIL; } // Create clipper for the primary surface LPDIRECTDRAWCLIPPER lpClipper; if (FAILED(lpdd->CreateClipper(0, &lpClipper, NULL))) { return E_FAIL; } if (FAILED(lpClipper->SetHWnd(0, hWnd))) { lpClipper->Release(); return E_FAIL; } if (FAILED(lpddsMain->SetClipper(lpClipper))) { lpClipper->Release(); return E_FAIL; } lpClipper->Release(); lpClipper = NULL; // Update window flag SetClassLong(hWnd, GCL_HICONSM, (LONG)LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICONSM))); SendMessage(hWnd, WM_SETICON, ICON_SMALL, (LONG)LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICONSM))); UpdateWindow(hWnd); return S_OK; }
static HRESULT CreateFullScreenDisplay(HWND hWnd, DWORD dwWidth, DWORD dwHeight, DWORD dwBPP) { RECT rc; DWORD dwStyle; // Set app window's style to WS_POPUP so that it can fit for full screen mode. dwStyle = GetWindowLong(hWnd, GWL_STYLE); dwStyle &= ~(WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX); dwStyle |= WS_POPUP; SetWindowLong(hWnd, GWL_STYLE, dwStyle); // Adapt window size SetRect(&rc, 0, 0, dwWidth, dwHeight); AdjustWindowRectEx(&rc, GetWindowStyle(hWnd), GetMenu(hWnd) != NULL, GetWindowExStyle(hWnd)); // Set cooperative level if (FAILED(lpdd->SetCooperativeLevel(hWnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_ALLOWREBOOT))) { return E_FAIL; } // Set the display mode if (FAILED(lpdd->SetDisplayMode(dwWidth, dwHeight, dwBPP, 0, 0))) { return E_FAIL; } // Create primary surface (with backbuffer attached) DDSURFACEDESC2 ddsd; DDRAW_INIT_STRUCT(ddsd); ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT | DDSD_CKSRCBLT; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX | DDSCAPS_3DDEVICE; ddsd.dwBackBufferCount = 1; if (FAILED(lpdd->CreateSurface(&ddsd, &lpddsMain, NULL))) { return E_FAIL; } // Get a pointer to the back buffer DDSCAPS2 ddscaps; ZeroMemory(&ddscaps, sizeof(ddscaps)); ddscaps.dwCaps = DDSCAPS_BACKBUFFER; if (FAILED(lpddsMain->GetAttachedSurface(&ddscaps, &lpddsBack))) { return E_FAIL; } //lpddsBack->AddRef(); // Set surface color key DDCOLORKEY ddCK; ddCK.dwColorSpaceLowValue = 0; ddCK.dwColorSpaceHighValue = 0; if (lpddsBack->SetColorKey(DDCKEY_SRCBLT, &ddCK)) { return E_FAIL; } // Update window flag SetClassLong(hWnd, GCL_HICONSM, NULL); SendMessage(hWnd, WM_SETICON, ICON_SMALL, NULL); UpdateWindow(hWnd); return S_OK; }