Click here to Skip to main content
15,884,176 members
Articles / Desktop Programming / MFC

Drawing with DirectDraw & GDI

Rate me:
Please Sign up or sign in to vote.
4.88/5 (11 votes)
24 May 2002 300.2K   13.8K   68  
Drawing Graphics fast with DirectDraw than with GDI
// DDSurface.h: interface for the NDDSurface class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_DDSURFACE_H__D9D7EBE5_7660_4622_97D4_254D08CD7C6D__INCLUDED_)
#define AFX_DDSURFACE_H__D9D7EBE5_7660_4622_97D4_254D08CD7C6D__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <ddraw.h>
//#include "cdxfile.h"

//typedef struct
//{
//    RGBQUAD depth;		// How many bits of precision per channel
//    RGBQUAD position;	// At what bit position does the color start
//	UINT    bpp;        // Bits Per Pixel
//}RGBFORMAT;


// TYPES //////////////////////////////////////////////////////

// basic unsigned types
typedef unsigned short USHORT;
typedef unsigned short WORD;
typedef unsigned char  UCHAR;
typedef unsigned char  BYTE;

 //container structure for bitmaps .BMP file
//typedef struct BITMAP_FILE_TAG
//{
//	BITMAPFILEHEADER bitmapfileheader;  // this contains the bitmapfile header
//	BITMAPINFOHEADER bitmapinfoheader;  // this is all the info including the palette
//	PALETTEENTRY     palette[256];      // we will store the palette here
//	UCHAR            *buffer;           // this is a pointer to the data
//	
//} BITMAP_FILE, *BITMAP_FILE_PTR;

typedef struct
{
    RGBQUAD depth;		// How many bits of precision per channel
    RGBQUAD position;	// At what bit position does the color start
	UINT    bpp;        // Bits Per Pixel
} RGBFORMAT;

void DDError(HRESULT hErr, HWND hWnd);
class NDDScreen;
class NDDSurface  
{
public:
	NDDSurface();
	NDDSurface(NDDScreen *pScreen, int Width, int Height);
	~NDDSurface();

	BOOL Create(NDDScreen *pScreen, int Width, int Height);
	void SetDest(int t, int l, int b, int r);
 	void SetSrc(int t, int l, int b, int r);
	HRESULT Restore(void);
	void Fill(DWORD FillColor);

	HRESULT Lock(void);
	HRESULT UnLock(void);
	HDC		GetDC();
	HRESULT	ReleaseDC();


	virtual HRESULT Draw(NDDSurface* lpDDS);
	virtual HRESULT Draw(NDDScreen* lpDDS);
	virtual HRESULT DrawFast(int X, int Y, NDDSurface* lpDDS);
	inline  HRESULT PutPixel(int X, int Y, DWORD Col) { return (*this.*m_fpPutPixel)(X, Y, Col); }
    inline  HRESULT PutAAPixel(int X, int Y, DWORD Col, WORD ALPHA) { return (*this.*m_fpPutAAPixel)(X, Y, Col, ALPHA); }
    inline  DWORD   GetPixel(int X, int Y) { return (*this.*m_fpGetPixel)(X, Y); }
    inline  HRESULT VLine(int Y1, int Y2, int X, DWORD Col) { return (*this.*m_fpVLine)(Y1, Y2, X, Col); }
    inline  HRESULT HLine(int X1, int X2, int Y, DWORD Col) { return (*this.*m_fpHLine)(X1, X2, Y, Col); }
    inline  LPDIRECTDRAWSURFACE7 GetDDS( void ) { return m_lpDDS; }
	void*       GetSurfacePointer( void ) { return m_DDSD.lpSurface; }
    int         GetWidth( void ) { return m_PixelWidth;  }
    int         GetHeight( void ) { return m_PixelHeight; }
	DWORD       GetPitch( void ) { return m_DDSD.lPitch; }
	NDDScreen*  GetScreen(void) { return Screen; }
public:
	HRESULT AttachClipper(LPDIRECTDRAWSURFACE7 pddSurface,int num_rects,LPRECT clip_list);
	BOOL GetRGBFormat(LPDIRECTDRAWSURFACE7 Surface,RGBFORMAT *rgb,int BPP);
	BOOL Create(NDDScreen *pScreen,int Width,int Height,int BPP);
	HRESULT Blt(LPDIRECTDRAWSURFACE7 pddSurface, LPRECT pDestRect, LPRECT pSrcRect);
	HRESULT Release();
	HRESULT TextOut(int x, int y, COLORREF col, LPCTSTR pString);
	void Swap (int *a,int *b);
	HRESULT SaveAsBMP(const char * szFilename);
	HRESULT RoundedRect(int X1, int Y1, int X2, int Y2, int Radius, DWORD Col);
	HRESULT Line(int X1, int Y1, int X2, int Y2, DWORD Col);
	HRESULT AALine(int X1, int Y1, int X2, int Y2, DWORD Col);
	HRESULT Rect(int X1, int Y1, int X2, int Y2, DWORD Col);
	HRESULT FillRect(int X1, int Y1, int X2, int Y2, DWORD Col);
	HRESULT FillCircle(int X, int Y, int Radius, DWORD Col);
//	int DrawBitmapToSurface(LPDIRECTDRAWSURFACE7 pdds,BITMAP_FILE_PTR bitmap,int width,int height);
//	int Unload_Bitmap_File(BITMAP_FILE_PTR bitmap);
//	int Load_Bitmap_File(BITMAP_FILE_PTR bitmap, const char *filename);
	int Flip_Bitmap(UCHAR *image, int bytes_per_line, int height);
	HRESULT DDCopyBitmap(LPDIRECTDRAWSURFACE7 pdds, HBITMAP hbm, int x, int y, int dx, int dy);
	HRESULT DDReLoadBitmap(LPDIRECTDRAWSURFACE7 pdds, LPCSTR szBitmap);
	HRESULT LoadBitmap(const char *szFilename);
	HRESULT DrawText(LPCSTR pString, COLORREF col, LPRECT pRect);
	HRESULT SetFont(void);
	HRESULT ChangeFont(const char* FontName, int Width, int Height, int Attributes = FW_NORMAL);
	HRESULT ReLoadBitmap(const char* fileName = NULL);
	char* GetFontName(char* name);
	char* GetFileName(char* name);
	void SetClipRect(RECT *clipRect);
	BOOL ClipRect(RECT *Rect);
	BOOL ValidateBlt(NDDSurface* lpCDXS, LONG *lDestX, LONG *lDestY, RECT *srcRect);
	HRESULT DrawBlk(NDDSurface* lpCDXS, LONG lDestX, LONG lDestY, RECT* srcRect);
	HRESULT InvertColors( int x1, int y1, int x2, int y2 );
	HRESULT Circle(int X, int Y, int Radius, DWORD Col);
	BOOL GetRGBFormat( LPDIRECTDRAWSURFACE7 Surface, RGBFORMAT* rgb);
	void GetPixelFormat(RGBFORMAT* format);
	void FunctionMapper(void);
	HRESULT Flip();
	int		m_PixelWidth;
	int		m_PixelHeight;

	HDC				m_hDC;
	RECT			SrcRect;
	RECT			DestRect;
	DDSURFACEDESC2	m_DDSD;
	LPDIRECTDRAWSURFACE7 m_lpDDS;
	NDDScreen		*Screen;
private:
	RGBFORMAT   m_PixelFormat;          // Structure that holds the information about the color depths, etc.
    RECT        m_ClipRect;             // The rectangle to which drawing operations are clipped
    DWORD       m_LockCount;            // Reference count to avoid mulitple lock/Unlocks
    char        m_FileName[256];		// The name of the bitmap file loaded, used in Restore
    char        m_FontName[256];        // Current font associated to the surface
    HFONT       m_Font;					// A HFONT object describing the currently selected font
    DWORD       m_DeviceContextCount;   // Reference count to avoid mulitple GetDC/ReleaseDC


private:
	    //Function  Pointer declarations
    HRESULT (NDDSurface::* m_fpPutPixel)(int X, int Y, DWORD Col);
	HRESULT (NDDSurface::* m_fpPutAAPixel)(int X, int Y, DWORD Col, WORD ALPHA);
	DWORD   (NDDSurface::* m_fpGetPixel)(int X, int Y);
	HRESULT (NDDSurface::* m_fpVLine)(int Y1, int Y2, int X, DWORD Col);
	HRESULT (NDDSurface::* m_fpHLine)(int X1, int X2, int Y, DWORD Col);
    HRESULT (NDDSurface::* fpDrawBlk)(NDDSurface* lpCDXS, LONG lDestX, LONG lDestY, RECT srcRect);
	HRESULT (NDDSurface::* fpDrawDDBlk)(NDDSurface* lpCDXS, LONG lDestX, LONG lDestY, RECT srcRect);
	HRESULT (NDDSurface::* fpDrawTrans)(NDDSurface* lpCDXS, LONG lDestX, LONG lDestY, RECT srcRect);
	HRESULT (NDDSurface::* fpDrawDDTrans)(NDDSurface* lpCDXS, LONG lDestX, LONG lDestY, RECT srcRect);
	HRESULT (NDDSurface::* fpDrawBlkHFlip)(NDDSurface* lpCDXS, LONG lDestX, LONG lDestY, RECT srcRect);
	HRESULT (NDDSurface::* fpDrawTransHFlip)(NDDSurface* lpCDXS, LONG lDestX, LONG lDestY, RECT srcRect);
	HRESULT (NDDSurface::* fpDrawBlkVFlip)(NDDSurface* lpCDXS, LONG lDestX, LONG lDestY, RECT srcRect);
	HRESULT (NDDSurface::* fpDrawTransVFlip)(NDDSurface* lpCDXS, LONG lDestX, LONG lDestY, RECT srcRect);
	HRESULT (NDDSurface::* fpDrawBlkAlphaFast)(NDDSurface* dest, LONG lDestX, LONG lDestY, RECT srcRect);
	HRESULT (NDDSurface::* fpDrawTransAlphaFast)(NDDSurface* dest, LONG lDestX, LONG lDestY, RECT srcRect);
	HRESULT (NDDSurface::* fpDrawBlkAlpha)(NDDSurface* dest, LONG lDestX, LONG lDestY, RECT srcRect, WORD ALPHA);
	HRESULT (NDDSurface::* fpDrawTransAlpha)(NDDSurface* dest, LONG lDestX, LONG lDestY, RECT srcRect, WORD ALPHA);
	HRESULT (NDDSurface::* fpDrawBlkShadow)(NDDSurface* dest, LONG lDestX, LONG lDestY, RECT srcRect, WORD SHADOW);
	HRESULT (NDDSurface::* fpDrawTransShadow)(NDDSurface* dest, LONG lDestX, LONG lDestY, RECT srcRect, WORD SHADOW);
	HRESULT (NDDSurface::* fpDrawBlkShadowFast)(NDDSurface* dest, LONG lDestX, LONG lDestY, RECT srcRect);
	HRESULT (NDDSurface::* fpDrawTransShadowFast)(NDDSurface* dest, LONG lDestX, LONG lDestY, RECT srcRect);
	HRESULT (NDDSurface::* fpDrawTransAlphaMask)(NDDSurface* dest, LONG lDestX, LONG lDestY, RECT srcRect);
	HRESULT (NDDSurface::* fpDrawTransScaled)(NDDSurface* dest, LONG lDestX, LONG lDestY, RECT srcRect, float scaleFactor);
	HRESULT (NDDSurface::* fpDrawBlkScaled)(NDDSurface* dest, LONG lDestX, LONG lDestY, RECT srcRect, float scaleFactor);
	HRESULT (NDDSurface::* fpDrawBlkStretched)(NDDSurface* lpCDXS, LONG lDestX, LONG lDestY, RECT srcRect, LONG width, LONG height);
	HRESULT (NDDSurface::* fpDrawTransStretched)(NDDSurface* lpCDXS, LONG lDestX, LONG lDestY, RECT srcRect, LONG width, LONG height);
	HRESULT (NDDSurface::* fpFade)(NDDSurface* dest, LONG lDestX, LONG lDestY, RECT srcRect, WORD ALPHA);

    // Specific video mode function declarations
    HRESULT PutPixel8(int X, int Y, DWORD Col);
    HRESULT PutPixel16(int X, int Y, DWORD Col);
    HRESULT PutPixel24(int X, int Y, DWORD Col);
    HRESULT PutPixel32(int X, int Y, DWORD Col);

    HRESULT PutAAPixel8(int X, int Y, DWORD Col, WORD ALPHA);
    HRESULT PutAAPixel15(int X, int Y, DWORD Col, WORD ALPHA);
    HRESULT PutAAPixel16(int X, int Y, DWORD Col, WORD ALPHA);
    HRESULT PutAAPixel24(int X, int Y, DWORD Col, WORD ALPHA);
    HRESULT PutAAPixel32(int X, int Y, DWORD Col, WORD ALPHA);

    DWORD   GetPixel8(int X, int Y);
    DWORD   GetPixel16(int X, int Y);
    DWORD   GetPixel24(int X, int Y);
    DWORD   GetPixel32(int X, int Y);

    HRESULT VLine8(int Y1, int Y2, int X, DWORD Col);
    HRESULT VLine16(int Y1, int Y2, int X, DWORD Col);
    HRESULT VLine24(int Y1, int Y2, int X, DWORD Col);
    HRESULT VLine32(int Y1, int Y2, int X, DWORD Col);

    HRESULT HLine8(int Y1, int Y2, int X, DWORD Col);
    HRESULT HLine16(int Y1, int Y2, int X, DWORD Col);
    HRESULT HLine24(int Y1, int Y2, int X, DWORD Col);
    HRESULT HLine32(int Y1, int Y2, int X, DWORD Col);


};


#endif // !defined(AFX_DDSURFACE_H__D9D7EBE5_7660_4622_97D4_254D08CD7C6D__INCLUDED_)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions