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

📄 ddraw01.cpp

📁 《Visual C 6.0高级编程技术——DirectX篇》程序源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// DDraw01.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"

// Inserted code begin
#define INITGUID
#include <windows.h>
#include <windowsx.h>
//#include <afxwin.h>
#include <ddraw.h>
#include <stdarg.h>
#include <dinput.h>
// Inserted code end

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];								// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];								// The title bar text

// Inserted code begin
//#define NAME "DDExample1"
//#define TITLE "Direct Draw Example 1"
#define TIMER_ID        1
#define TIMER_RATE      500

TCHAR szMsgFlip[MAX_LOADSTRING];
TCHAR szMsgFront[MAX_LOADSTRING];
TCHAR szMsgBack[MAX_LOADSTRING];

LPDIRECTDRAW            lpDD;           // DirectDraw object
LPDIRECTDRAWSURFACE     lpDDSPrimary;   // DirectDraw primary surface
LPDIRECTDRAWSURFACE     lpDDSBack;      // DirectDraw back surface
BOOL                    bActive;        // is application active?

//char szMsgFlip[] = "Page Flipping Test: Press F12 to exit";
// char szMsgFront[] = "Front buffer (F12 to quit)";
// char szMsgBack[] = "Back buffer (F12 to quit)";

static void DestroyDirectDrawObjects( void );
IDirectDrawSurface * DDLoadBitmap(IDirectDraw *pdd, LPCSTR szBitmap, int dx, int dy);
HRESULT              DDAttachBitmap(IDirectDrawSurface *pdds, LPCSTR szBitmap);
HRESULT              DDCopyBitmap(IDirectDrawSurface *pdds, HBITMAP hbm, int x, int y, int dx, int dy);

typedef HRESULT (WINAPI *DIRECTDRAWCREATE)( GUID *, LPDIRECTDRAW *, IUnknown * );
typedef HRESULT (WINAPI *DIRECTINPUTCREATE)( HINSTANCE, DWORD, LPDIRECTINPUT *, IUnknown * );

void GetDirectXVersion(LPDWORD pdwDXVersion, LPDWORD pdwDXPlatform);
// Inserted code end

// Foward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_WNDCLASS_NAME, szWindowClass, MAX_LOADSTRING);
	LoadString(hInstance, IDS_MSGFLIP, szMsgFlip, MAX_LOADSTRING);
	LoadString(hInstance, IDS_MSGFRONT, szMsgFront, MAX_LOADSTRING);
	LoadString(hInstance, IDS_MSGBACK, szMsgBack, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_DDRAW01);

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage is only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
  WNDCLASS wc;
  /*
  * set up and register window class
  */
  wc.style = CS_HREDRAW | CS_VREDRAW;
  wc.lpfnWndProc = WndProc;
  wc.cbClsExtra = 0;
  wc.cbWndExtra = 0;
  wc.hInstance = hInstance;
  wc.hIcon = LoadIcon( hInstance, IDI_APPLICATION );
  wc.hCursor = LoadCursor( NULL, IDC_ARROW );
  wc.hbrBackground = NULL;
  wc.lpszMenuName = szWindowClass;
  wc.lpszClassName = szWindowClass;
  return RegisterClass( &wc );
/* 	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX); 

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, (LPCTSTR)IDI_DD1);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= (LPCSTR)IDC_DD1;
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

	return RegisterClassEx(&wcex);
*/
}

//
//   FUNCTION: InitInstance(HANDLE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
/*   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
*/
  HWND          hwnd;
  DDSURFACEDESC ddsd;
  DDSCAPS       ddscaps;
  HRESULT       ddrval;
  HDC           hdc;
  char          buf[256];
  WORD         *dest;
  int x, y;

  hInst = hInstance; // Store instance handle in our global variable

  /*
   * create a window
   */
  hwnd = CreateWindowEx(
		WS_EX_TOPMOST,
		szWindowClass,
		szTitle,
		WS_POPUP,
		0, 0,
		GetSystemMetrics( SM_CXSCREEN ),
		GetSystemMetrics( SM_CYSCREEN ),
		NULL,
		NULL,
		hInstance,
		NULL );

  if( !hwnd )
  {
		return FALSE;
  }

  ShowWindow( hwnd, nCmdShow );
  UpdateWindow( hwnd );

// Inserted code begin
//  LPDWORD pdwDXVersion;
//  LPDWORD pdwDXPlatform;
//  GetDirectXVersion( pdwDXVersion, pdwDXPlatform);
// Inserted code end

  /*
   * create the main DirectDraw object
   */
  ddrval = DirectDrawCreate( NULL, &lpDD, NULL );
  if( ddrval == DD_OK )
  {
		// Get exclusive mode
		ddrval = lpDD->SetCooperativeLevel( hwnd,	DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN );
		if( ddrval == DD_OK )
		{
	    ddrval = lpDD->SetDisplayMode( 640, 480, 8 );
	    if( ddrval == DD_OK )
	    {
				// Create the primary surface with 1 back buffer
				ddsd.dwSize = sizeof( ddsd );
				ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
				ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX;
				ddsd.dwBackBufferCount = 1;
				ddrval = lpDD->CreateSurface( &ddsd, &lpDDSPrimary, NULL );
				if( ddrval == DD_OK )
				{
				  // Get a pointer to the back buffer
					ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
					ddrval = lpDDSPrimary->GetAttachedSurface( &ddscaps, &lpDDSBack );
			    if( ddrval == DD_OK )
			    {
/*
            // First, clear the primary surface to white using Lock and Unlock
            // to directly access surface memory

            // Lock the primary surface
            if FAILED( lpDDSPrimary->Lock( NULL, &ddsd, DDLOCK_WAIT , NULL ) )
	          {
              DestroyDirectDrawObjects();
              OutputDebugString( "Couldn't lock the primary surface.\n" );
              DestroyWindow( hwnd );
              return FALSE;
            }

            // Clear it to white
            // We are assuming: 1) we are palettized, so each
            // pixel is represented by a "byte" and 2) since we are not setting
            // a new palette, entry 255 should be white.
            dest = (WORD *)ddsd.lpSurface;
            for ( y = 0; y < ( int )ddsd.dwHeight; y++ ) 
	          {
              for ( x = 0; x < (( int )ddsd.dwWidth ); x++ ) 
		          {
                dest[x] = 0xFF00;
              }
              // lPitch contains the pitch in *bytes*
              dest += ddsd.lPitch / 2;
            }

            // Unlock the primary surface
            if FAILED( lpDDSPrimary->Unlock( NULL ) )
	          {
              DestroyDirectDrawObjects();
              OutputDebugString( "Couldn't unlock the primary surface.\n" );
              DestroyWindow( hwnd );
              return FALSE;
            }
*/
            // draw some text.
						if( lpDDSPrimary->GetDC( &hdc ) == DD_OK )
						{
							SetBkColor( hdc, RGB( 0, 0, 255 ) );
							SetTextColor( hdc, RGB( 255, 255, 0 ) );
							TextOut( hdc, 0, 0, szMsgFront, lstrlen( szMsgFront ) );
							lpDDSPrimary->ReleaseDC( hdc );
						}
						if( lpDDSBack->GetDC( &hdc ) == DD_OK )
						{
							SetBkColor( hdc, RGB( 0, 0, 255 ) );
							SetTextColor( hdc, RGB( 255, 255, 0 ) );
							TextOut( hdc, 0, 0, szMsgBack, lstrlen( szMsgBack ) );
							lpDDSBack->ReleaseDC( hdc );
						}

            // load a bitmap into the primary surface.
            ddrval = DDAttachBitmap(lpDDSPrimary, "Lake.bmp");
            // load a bitmap into the back buffer.
            ddrval = DDAttachBitmap(lpDDSBack, "Lake.bmp");

            // Create a timer to flip the pages
						if( SetTimer( hwnd, TIMER_ID, TIMER_RATE, NULL ) )
						{
							return TRUE;
						}
					}
				}
	    }
		}
  }

  wsprintf( buf, "Direct Draw Init Failed (%08lx)\n", ddrval );
  MessageBox( hwnd, buf, "ERROR", MB_OK );
  DestroyDirectDrawObjects();
  DestroyWindow( hwnd );
  return FALSE;
}

//
//  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)
{
// Inserted code begin
  RECT rc;
//  CSize theSize;
  SIZE size;
  static BYTE phase = 0;
// Inserted code end
	int wmId, wmEvent;
	PAINTSTRUCT ps;
//  CDC dc;
	HDC hdc;
//	TCHAR szHello[MAX_LOADSTRING];
//	LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

	switch( message ) 
	{
// Inserted code begin
  case WM_ACTIVATEAPP:
		bActive = wParam;
		break;

  case WM_CREATE:
		break;

  case WM_SETCURSOR:
		SetCursor( NULL );
		return TRUE;

  case WM_TIMER:
	// Flip surfaces
		if( bActive )
		{
			if( lpDDSBack->GetDC( &hdc ) == DD_OK )
			{
				SetBkColor( hdc, RGB( 0, 0, 255 ) );
				SetTextColor( hdc, RGB( 255, 255, 0 ) );
				if( phase )
				{
					TextOut( hdc, 0, 0, szMsgFront, lstrlen( szMsgFront ) );
					phase = 0;
				}
				else
				{
					TextOut( hdc, 0, 0, szMsgBack, lstrlen( szMsgBack ) );
					phase = 1;
				}
				lpDDSBack->ReleaseDC( hdc );
			}

      while( 1 )
			{
				HRESULT ddrval;
				ddrval = lpDDSPrimary->Flip( NULL, 0 );
				if( ddrval == DD_OK )
				{
					break;
				}
				if( ddrval == DDERR_SURFACELOST )
				{
					ddrval = lpDDSPrimary->Restore();
					if( ddrval != DD_OK )
					{
						break;
					}
				}
				if( ddrval != DDERR_WASSTILLDRAWING )
				{
					break;
				}
			}
		}
		break;
 
  case WM_KEYDOWN:
		switch( wParam )
		{
		case VK_ESCAPE:
		case VK_F12:
	    PostMessage( hWnd, WM_CLOSE, 0, 0 );
	    break;
		}
		break;
// Inserted code end
		case WM_COMMAND:
			wmId    = LOWORD(wParam); 
			wmEvent = HIWORD(wParam); 
			// Parse the menu selections:
			switch (wmId)
			{
				case IDM_ABOUT:
				   DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
				   break;
				case IDM_EXIT:
				   DestroyWindow(hWnd);
				   break;
				default:
				   return DefWindowProc(hWnd, message, wParam, lParam);
			}
			break;
		case WM_PAINT:
//			hdc = BeginPaint(hWnd, &ps);
			// TODO: Add any drawing code here...

⌨️ 快捷键说明

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