CGRAPH changes

xiaoxiao2021-03-06  18

depressed. After a few days a year, it seems that there is no feeling. The lively time is not long enough, it is very unhappy. Tomorrow is 2.14 Valentine's Day. Um ... I don't care what I do. It is a person at home tomorrow night, find something entertainment? no. It's a few ... WAR3, Coding, Film, TV, Music ... etc. Looking at the frame gradually developed, it feels very cool ~~ This time, what should I always make something? Come. Code on the code. If this blog has a keyword, it is fine. . .

#include "stdafx.h" #include "./graph.h"

CGRAPH :: CGRAPH (Void): m_winheight (0), M_WINWIDTH (0), M_ColorDepth (0) {}

CGraph :: ~ CGraph (void) {} ​​// initialize DirectDrawbool CGraph :: InitDDraw (int nWidth, int nHeight, int nColorDepth, bool bFullScreen) {if (DirectDrawCreate (NULL, & lpDD, NULL) = DD_OK!) Return false; / / Set CooperativeLevel DWORD dwFlags; if (bFullScreen) {dwFlags = DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_ALLOWREBOOT; this-> bIsWindowMode = false;} else {dwFlags = DDSCL_NORMAL; this-> bIsWindowMode = true;} if (lpDD-> SetCooperativeLevel (hWnd, ! dwFlags) = DD_OK) return false; // Set DisplayMode m_WinHeight = nHeight; m_WinWidth = nWidth; m_ColorDepth = nColorDepth; if (bFullScreen) if (lpDD-> SetDisplayMode (nWidth, nHeight, nColorDepth) = DD_OK) return false;! / / Set Window Style SetWindowStyle (); // Init Primary and back Buffers DDSURFACEDESC ddsd; HRESULT ddrval; if (bFullScreen) {// Fullscreen mode ZeroMemory (& ddsd, sizeof (ddsd)); ddsd.dwSize = sizeof (ddsd); ddsd .dwflags = DDSD_CAPS | DDSD_BACKBufferCount; DDSD.DDSCAPS.DWCAPS = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX;. Ddsd.dwBackBufferCount = 1; // Create Primary surface ddrval = lpDD-> CreateSurface (& ddsd, & lpDDSPrimary, NULL); if (! Ddrval = DD_OK) return false; // Create BackBuffer surface ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER; if (! lpDDSPrimary-> GetAttachedSurface (& ddsd.ddsCaps, & lpDDSBack) = DD_OK) return false;} else {// Window mode ddsd.dwSize = sizeof (ddsd); ddsd.dwFlags = DDSD_CAPS; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; // Create Primary Surface. IF (LPDD-> CreateSurface (& DDSD, & LPDDSPRIMARY, NULL)! = DD_OK) {Return False;

} // Create BackBuffer surface ZeroMemory (& ddsd, sizeof (ddsd)); ddsd.dwSize = sizeof (ddsd); ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH; ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY; ddsd.dwWidth = m_WinWidth Ddsd.dwheight = m_winheight; if (LPDD-> CreateSurface (& DDSD, & LPDDSBACK, NULL)! = DD_OK) {Return False;} // Set key color setColorKey (LPDDSBACK, RGB (255, 0));} / / Establish a crop CreateClipper (); return true;} // Release DirectDraw object void cgraph :: freeddraw (void) {MessageBox (hwnd, "limited ...", "release", mb_ok); if (LPDD! = Null ) {_RELEASE (LPCLIPPER); _RELEASE (LPDDSPRIMARY); _RELEASE (LPDDSPRIMARY); _RELEASE (LPDD);}} // Setting window style with the current display mode match Bool CGRAPH :: SetWindowStyle (void) {if (this-> biswindowmode) {SetWindowLong (hWnd, GWL_EXSTYLE, WS_EX_OVERLAPPEDWINDOW); SetWindowLong (hWnd, GWL_STYLE, WS_OVERLAPPEDWINDOW & ~ WS_MAXIMIZEBOX); SetWindowPos (hWnd, HWND_NOTOPMOST, 0,0, m_WinWidth, m_WinHeight, SWP_SHOWWINDOW | SWP_NOMOVE);} else {SetWindowLong (hWn d, GWL_EXSTYLE, WS_EX_TOPMOST); SetWindowLong (hWnd, GWL_STYLE, WS_POPUP); SetWindowPos (hWnd, HWND_TOPMOST, 0,0, GetSystemMetrics (SM_CXSCREEN), GetSystemMetrics (SM_CYSCREEN), SWP_SHOWWINDOW);} return true;} // build clipper bool CGRAPH :: CreateClipper (void) {if (LPDD-> CreateClipper (0, & lpclipper, null)! = Dd_ok) {#ifdef _debug mytrace.write ("cgraph :: createclipper () CreateClipper error! / N"); #ENDIF Return False;} f (lpddsprimary-> setclipper (lpclipper)! = dd_ok) {#ifdef _debug mytrace.write ("cgraph :: createclipper () setclipper error! / n"); #ndif returse;} return true;

} // set the color key HRESULT CGraph :: SetColorKey (IDirectDrawSurface * pdds, COLORREF rgb) {DDCOLORKEY ddck; ddck.dwColorSpaceLowValue = ColorMatch (pdds, rgb); ddck.dwColorSpaceHighValue = ddck.dwColorSpaceLowValue; return pdds-> SetColorKey (DDCKEY_SRCBLT, & ddck);} // get the specified corresponding to the color palette index DWORD CGraph :: ColorMatch (IDirectDrawSurface * pdds, COLORREF rgb) {COLORREF rgbT; HDC hdc; DWORD dw = CLR_INVALID; DDSURFACEDESC ddsd; HRESULT hres; if (rgb! = CLR_INVALID && PDDS-> getDC (& HDC) == DD_OK) {RGBT = GetPixel (HDC, 0, 0); // Save the current value setpixel (HDC, 0, 0, RGB); PDDS-> ReleaseDC (HDC); } Ddsd.dwsize = sizeof (ddsd); while ((hres = pdd, 0, null) == DDERR_WASSTILLDRAWING); if (hres == dd_ok) {dw = * (dword *) DDSD .lpsurface; // Translate into DWORD DW & = (1 << Ddsd.ddpfpixelformat.dwrgbbitcount) -1; // Mask it to bpp pdds-> unlock (null);} if (RGB! = CLR_ Invalid && PDDS-> Getdc (& HDC) == DD_OK) {setpixel (HDC, 0, 0, RGBT); // Restore the original value PDDS-> releasedc (hdc);} Return DW;} // Create a remote screen Surface bmp picture and loaded into the page IDirectDrawSurface * CGraph :: LoadBitmap (IDirectDraw * pdd, LPCSTR szBitmap, int dx, int dy) {HBITMAP hbm; BITMAP bm; DDSURFACEDESC ddsd; IDirectDrawSurface * pdds; // first try as a resource to load hbm = ( Hbitmap) loadImage (getModuleHandle (NULL), SZBitmap, Image_bitMap, DX, DY, LR_CREATEDIBSECTION; // Resource loading failed, as a file load IF (HBM == NULL) HBM =

(Hbitmap) loading (NULL, SZBITMAP, Image_bitMap, DX, DY, LR_LOADFROMFILE | LR_CREATEDIBSECTION); if (hbm == null) return null; // Get image size getObject (HBM, SIZEOF (BM), & BM); // Created Surface ZeroMemory (& ddsd, sizeof (ddsd)); ddsd.dwSize = sizeof (ddsd); ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH; ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY; ddsd.dwWidth = bm.bmWidth; ddsd. Dwheight = bm.bmheight; if (PDD-> CreateSurface (& DDSD, & PDDS, NULL)! = DD_OK) Return Null; // Create Surface Failed // Load Picture Copybitmap (PDDS, HBM, 0, 0, 0, 0); DeleteObject (hbm); return pdds;} // Copy the bitmap to the target hbm SurfaceHRESULT CGraph :: CopyBitmap (IDirectDrawSurface * pdds, HBITMAP hbm, int x, int y, int dx, int dy) {HDC hdcImage; HDC hdc; Bitmap BM; DDSurfaceDesc DDSD; HRESULT HR; IF (HBM == NULL || PDDS == NULL) RETURN E_FAIL; PDDS-> restore (); // Create a with current screen Associated memory device context hdcImage = CreateCompatibleDC (NULL); if OutputDebugString ( "createcompatible dc failed / n") (hdcImage!); SelectObject (hdcImage, hbm); // get bmp size GetObject (hbm, sizeof (bm), & bm ); DX = DX == 0? Bm.bmwidth: DX; // As long as the parameter specifies the size, use the specified size, otherwise the default size DY = DY == 0? Bm.bmHeight: Dy; // Get the page Size ddsd.dwsize = sizeof (ddsd); ddsd.dwflags = ddsd_height | DDSD_WIDTH; PDDS-> GetSurfaceDesc (& DDSD); if ((HR = PDDS-> Getdc (& HDC)) == DD_OK) {// Zoom blt stretchblt HDC, 0, 0, DDSD.dwwidth, ddsd.dwheight, HDCImage, X, Y, DX, DY, SRCCPY;

PDDS-> ReleaseDC (HDC);} deletedc (HDCIMAGE); Return HR;} / / Load a Bitmap to the page (Different with LoadBitmap, this function does not create a Surface) HRESULT CGRAPH :: ReloadBitmap (iDirectdrawsurface * PDDS, LPCSTR szBitmap) {HBITMAP hbm; HRESULT hr; // see comments LoadBitmap function, quite close hbm = (HBITMAP) LoadImage (GetModuleHandle (NULL), szBitmap, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);. if (hbm == NULL) hbm = (Hbitmap) loading (null, szbitmap, image_bitmap, 0, 0, lr_loadfromfile | lr_createdibsection); if (hbm == null) {char str [128]; Sprintf (STR, "Handle Is Null [% s] / n" , szbitmap; outputdebugstring (STR); Return E_FAIL;} HR = CopyBitmap (PDDS, HBM, 0, 0, 0, 0); if (HR! = DD_OK) {OutputdebugString ("DDCopyBitmap Failed / N");} deleteObject (HBM); RETURN HR;} // Function Test Function VOID CGRAPH :: Test (void) {}

It's roughly like this, there are several functions in the middle to copy from Microsoft's ddutil.cpp, just less DD two characters.

A function of reading the tones from the Bitmap file, because I plan to do the Game is at least 24bit colors, with a palette.

16bit colors should be judged that 555, 565 is very troublesome. Needless, then 65536 colors are too small, it is not conducive to the beauty of the art ...

转载请注明原文地址:https://www.9cbs.com/read-58599.html

New Post(0)