运行程序报异常
First-chance exception in PaiJi.exe: 0x80000002: DataType Misalignment
图片就贴了一次后出异常,也没法跟踪到具体哪一行代码出错
程序主要代码如下
#include "stdafx.h"
#include "ddraw.h"
#include "DirectDraw.h"
LPDIRECTDRAW g_pDD;
LPDIRECTDRAWSURFACE g_pFrontBuffer;
LPDIRECTDRAWSURFACE g_pBackBuffer;
LPDIRECTDRAWSURFACE g_pBack;
LPDIRECTDRAWSURFACE g_pCar[CARCNT];
SIZE g_BackSize;
SIZE g_CarSize[CARCNT];
RECT g_rcWindow;
DWORD g_dwFillColor;
int g_CarIndex;
//´´½¨LPDIRECTDRAWSURFACE
LPDIRECTDRAWSURFACE DDCreateSurface(int width, int height)
{
DDSURFACEDESC ddsd;
LPDIRECTDRAWSURFACE pdds;
ZeroMemory( &ddsd, sizeof(ddsd) );
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
ddsd.dwWidth = width;
ddsd.dwHeight = height;
if( FAILED(g_pDD->CreateSurface( &ddsd, &pdds, NULL )) )
return NULL;
return pdds;
}
//¼ÓÔØͼƬ
LPDIRECTDRAWSURFACE DDLoadBitmap(unsigned short *szBitmap, SIZE *pSize)
{
HBITMAP hbm;
BITMAP bm;
DDSURFACEDESC ddsd;
LPDIRECTDRAWSURFACE pdds;
//
// Try to load the bitmap as a resource, if that fails, try it as a file
//
hbm = (HBITMAP) SHLoadDIBitmap(szBitmap);
if (hbm == NULL)
return NULL;
//
// Get size of the bitmap
//
GetObject(hbm, sizeof(bm), &bm);
if(pSize != NULL)
{
pSize->cx = bm.bmWidth;
pSize->cy = bm.bmHeight;
}
//
// Create a DirectDrawSurface for this bitmap
//
ZeroMemory(&ddsd, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
ddsd.dwWidth = bm.bmWidth;
ddsd.dwHeight = bm.bmHeight;
if (g_pDD->CreateSurface(&ddsd, &pdds, NULL) != DD_OK)
return NULL;
DDCopyBitmap(pdds, hbm, 0, 0, 0, 0);
DeleteObject(hbm);
return pdds;
}
//³õʼ»¯DirectDraw
BOOL DDInit(BOOL bFullScreen, HWND hWnd, RECT rcWindow)
{
int i;
HRESULT hr;
DDSURFACEDESC ddsd;
g_CarIndex = 0;
g_pDD = NULL;
g_pFrontBuffer = NULL;
g_pBackBuffer = NULL;
g_pBack = NULL;
memset(&g_BackSize, 0, sizeof(SIZE));
for(i = 0; i < CARCNT; i++)
memset(&g_CarSize, 0, sizeof(SIZE));
hr = DirectDrawCreate(NULL, &(g_pDD), NULL);
if(FAILED(hr))
return FALSE;
if(bFullScreen)
g_pDD->SetCooperativeLevel(hWnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
else
g_pDD->SetCooperativeLevel(hWnd, DDSCL_NORMAL);
if(bFullScreen)
{
if(FAILED(g_pDD->SetDisplayMode(SCREEN_W, SCREEN_H, 16)))
return FALSE;
}
ZeroMemory(&ddsd, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
if(bFullScreen)
{
ddsd.dwFlags |= DDSD_BACKBUFFERCOUNT;
ddsd.ddsCaps.dwCaps |= DDSCAPS_FLIP | DDSCAPS_COMPLEX;
ddsd.dwBackBufferCount = 1;
}
hr = g_pDD->CreateSurface(&ddsd, &(g_pFrontBuffer), NULL);
if(FAILED(hr))
return FALSE;
if(bFullScreen)
{
DDSCAPS ddscaps;
ZeroMemory(&ddscaps, sizeof(ddscaps));
ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
g_pFrontBuffer->GetAttachedSurface(&ddscaps, &(g_pBackBuffer));
}
else
g_pBackBuffer = DDCreateSurface(SCREEN_W, SCREEN_H);
if(!LoadGameBitmaps())
return FALSE;
g_dwFillColor = DDColorMatch(g_pBackBuffer, RGB(128,128,128));
if(!bFullScreen)
{
SetWindowPos(hWnd, 0, 0, 0, SCREEN_W, SCREEN_H, SWP_NOMOVE | SWP_NOZORDER);
rcWindow.right = rcWindow.left + SCREEN_W;
rcWindow.bottom = rcWindow.top + SCREEN_H;
}
return TRUE;
}
void DDClear(RECT *prc, DWORD dwFillColor)
{
DDBLTFX ddbfx;
ZeroMemory(&ddbfx, sizeof(ddbfx));
ddbfx.dwSize = sizeof(ddbfx);
ddbfx.dwFillColor = dwFillColor;
if(g_pDD != NULL)
g_pBackBuffer->Blt(prc, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbfx);
}
//»Ö¸´ËùÓеÄSurface
void RestoreSurfaces()
{
int i;
g_pBackBuffer->Restore();
g_pFrontBuffer->Restore();
g_pBack->Restore();
for(i = 0; i < CARCNT; i++)
g_pCar->Restore();
LoadGameBitmaps();
}
//½«Í¼Æ¬ÌùÔں󻺳åÇøÖÐ
void DrawPicture()
{
HRESULT hr;
DDClear(NULL, 0);
hr = g_pBackBuffer->BltFast(0, 0, g_pBack, NULL, DDBLTFAST_WAIT);
if(hr == DDERR_SURFACELOST)
RestoreSurfaces();
hr = g_pBackBuffer->BltFast(0, 0, g_pCar[g_CarIndex], NULL, DDBLTFAST_WAIT);
if(hr == DDERR_SURFACELOST)
RestoreSurfaces();
}
//ͼÏñ·×ª
void FlipScreen(BOOL bFullScreen, LPRECT rcWindow)
{
HRESULT hr;
if(bFullScreen)
hr = g_pFrontBuffer->Flip(NULL, DDFLIP_WAIT);
else
hr = g_pFrontBuffer->Blt(rcWindow, g_pBackBuffer, NULL, DDBLT_WAIT, NULL);
}
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
//使用
BOOL CPaiJiDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
CenterWindow(GetDesktopWindow()); // center to the hpc screen
// TODO: Add extra initialization here
g_rcWindow.left = 0;
g_rcWindow.top = 0;
DDInit(FULLSCREEN, m_hWnd, g_rcWindow);
SetTimer(1, 100, NULL);
return TRUE; // return TRUE unless you set the focus to a control
}
void CPaiJiDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
DrawPicture();
FlipScreen(FULLSCREEN, NULL);
// Do not call CDialog::OnPaint() for painting messages
}
void CPaiJiDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(nIDEvent == 1)
{
if(g_CarIndex >= CARCNT - 1)
g_CarIndex = 0;
else
g_CarIndex++;
}
CDialog::OnTimer(nIDEvent);
}