📄 ddraw04.cpp
字号:
// ddraw04.cpp : Defines the entry point for the application.
//
//#define INITGUID
#include "stdafx.h"
#include "resource.h"
// Inserted code begin
#include <windowsx.h>
//#include <afxwin.h>
#include <ddraw.h>
#include <stdarg.h>
// Inserted code end
#define MAX_LOADSTRING 100
#define MAX_SPRITES 10
// 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
// char szBitmap[] = "All.bmp";
char szBitmap[] = "Sprites.bmp";
LPDIRECTDRAW lpDD; // DirectDraw object
LPDIRECTDRAWSURFACE lpDDSPrimary; // DirectDraw primary surface
LPDIRECTDRAWSURFACE lpDDSBack; // DirectDraw back surface
LPDIRECTDRAWSURFACE lpDDSOne; // Offscreen surface 1
LPDIRECTDRAWPALETTE lpDDPal; // DirectDraw palette
BOOL bActive; // is application active?
// Foward declarations of functions included in this code module:
HRESULT RestoreAllDirectDrawObjects( void );
void DestroyDirectDrawObjects( void );
//IDirectDrawSurface * DDLoadBitmap( IDirectDraw *pdd, LPCSTR szBitmap, int dx, int dy );
LPDIRECTDRAWSURFACE DDLoadBitmap( LPDIRECTDRAW pdd, LPCSTR szBitmap, int dx, int dy );
//HRESULT DDAttachBitmap( IDirectDrawSurface *pdds, LPCSTR szBitmap );
HRESULT DDAttachBitmap( LPDIRECTDRAWSURFACE pdds, LPCSTR szBitmap );
//HRESULT DDCopyBitmap( IDirectDrawSurface *pdds, HBITMAP hbm, int x, int y, int dx, int dy );
HRESULT DDCopyBitmap( LPDIRECTDRAWSURFACE pdds, HBITMAP hbm, int x, int y, int dx, int dy );
//IDirectDrawPalette * DDLoadPalette( IDirectDraw *pdd, LPCSTR szBitmap );
LPDIRECTDRAWPALETTE DDLoadPalette( LPDIRECTDRAW pdd, LPCSTR szBitmap );
//HRESULT DDSetColorKey( IDirectDrawSurface *pdds, COLORREF rgb );
HRESULT DDSetColorKey( LPDIRECTDRAWSURFACE pdds, COLORREF rgb );
//DWORD DDConvertRGBToPysicalColor( IDirectDrawSurface *pdds, COLORREF rgb );
DWORD DDConvertRGBToPysicalColor( LPDIRECTDRAWSURFACE pdds, COLORREF rgb );
void UpdateFrame( void );
// 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_DDRAW04, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_DDRAW04);
// Main message loop:
// Modified code begin
while( TRUE )
{
if( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) )
{
if( !GetMessage( &msg, NULL, 0, 0 ) )
return msg.wParam;
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else if( bActive )
{
UpdateFrame();
}
else
{
// make sure we go to sleep if we have nothing else to do
WaitMessage();
}
}
/*
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
*/
// Modified code end
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 );
}
//
// 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;
DDSURFACEDESC ddsd;
DDSCAPS ddscaps;
HRESULT ddrval;
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 );
// create the main DirectDraw object
ddrval = DirectDrawCreate( NULL, &lpDD, NULL );
if( ddrval != DD_OK )
{
DestroyDirectDrawObjects();
MessageBox( hwnd, "DirectDraw DirectDrawCreate FAILED", szTitle, MB_OK );
DestroyWindow( hwnd );
return FALSE;
}
// Get exclusive mode
ddrval = lpDD->SetCooperativeLevel( hwnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN );
if( ddrval != DD_OK )
{
DestroyDirectDrawObjects();
MessageBox( hwnd, "DirectDraw SetCooperativeLevel FAILED", szTitle, MB_OK );
DestroyWindow( hwnd );
return FALSE;
}
// Set the video mode to 640x480x8
ddrval = lpDD->SetDisplayMode( 640, 480, 8 );
if( ddrval != DD_OK )
{
DestroyDirectDrawObjects();
MessageBox( hwnd, "DirectDraw SetDisplayMode FAILED", szTitle, MB_OK );
DestroyWindow( hwnd );
return FALSE;
}
// 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 )
{
DestroyDirectDrawObjects();
MessageBox( hwnd, "DirectDraw CreateSurface FAILED", szTitle, MB_OK );
DestroyWindow( hwnd );
return FALSE;
}
ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
ddrval = lpDDSPrimary->GetAttachedSurface( &ddscaps, &lpDDSBack );
if( ddrval != DD_OK )
{
DestroyDirectDrawObjects();
MessageBox( hwnd, "DirectDraw GetAttachedSurface FAILED", szTitle, MB_OK );
DestroyWindow( hwnd );
return FALSE;
}
// create and set the palette
lpDDPal = DDLoadPalette( lpDD, szBitmap );
if( lpDDPal )
lpDDSPrimary->SetPalette( lpDDPal );
// Create the offscreen surface, by loading our bitmap.
lpDDSOne = DDLoadBitmap( lpDD, szBitmap, 0, 0 );
if( lpDDSOne == NULL )
{
DestroyDirectDrawObjects();
MessageBox( hwnd, "DirectDraw DDLoadBitmap FAILED", szTitle, MB_OK );
DestroyWindow( hwnd );
return FALSE;
}
// Set the color key for this bitmap (black)
DDSetColorKey( lpDDSOne, RGB( 0, 0, 0 ) );
return TRUE;
}
//
// 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)
{
int wmId, wmEvent;
/*
PAINTSTRUCT ps;
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;
// Inserted code end
case WM_KEYDOWN:
switch( wParam )
{
case VK_ESCAPE:
case VK_F12:
PostMessage( hWnd, WM_CLOSE, 0, 0 );
break;
}
break;
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:
// BeginPaint(hWnd, &ps);
// hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
// RECT rt;
// GetClientRect(hWnd, &rt);
// DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
// EndPaint(hWnd, &ps);
// break;
case WM_DESTROY:
// Inserted code begin
DestroyDirectDrawObjects();
// Inserted code end
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
// Inserted code begin
// DestroyDirectDrawObjects
// finished with all objects we use; release them
void DestroyDirectDrawObjects( void )
{
if( lpDD != NULL )
{
if( lpDDSPrimary != NULL )
{
lpDDSPrimary->Release();
lpDDSPrimary = NULL;
}
lpDD->Release();
lpDD = NULL;
}
} /* DestroyDirectDrawObjects */
LPDIRECTDRAWSURFACE DDLoadBitmap( LPDIRECTDRAW pdd, LPCSTR szBitmap, int dx, int dy )
//IDirectDrawSurface * DDLoadBitmap(IDirectDraw *pdd, LPCSTR szBitmap, int dx, int dy)
{
HBITMAP hbm;
BITMAP bm;
DDSURFACEDESC ddsd;
LPDIRECTDRAWSURFACE pdds;
// IDirectDrawSurface *pdds;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -