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

📄 wince_ddraw.cpp

📁 这个是延伸mame的在wince平台下的游戏模拟器的代码
💻 CPP
字号:
/* PocketCultMAME - MAME Emulator for PocketPC
   (c) Copyright 2006 Manuel Castrillo Mart韓ez

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#ifdef DIRECTDRAW_BUILD

#define WIN32_LEAN_AND_MEAN		// Exclude rarely-used stuff from Windows headers
#include <windows.h>
#include <ddraw.h>

#include "vidsys.h"

extern "C" {
extern VidSysInfo VideoInfo;
}

struct _ddrawVidSysInfo
{
	VidSysInfo	*VideoInfo;
	int			i2700G_QVGAforzed;
	bool		usingBackBuffer;
};

_ddrawVidSysInfo ddrawVidSysInfo;

LPDIRECTDRAW		g_pDD = NULL;			// DirectDraw object
LPDIRECTDRAWSURFACE	g_pDDSPrimary = NULL;	// DirectDraw primary surface
LPDIRECTDRAWSURFACE	g_pDDSBack = NULL;		// DirectDraw back surface

DDCAPS ddCaps;
DDCAPS ddHelCaps;

DDSURFACEDESC ddsd;

bool SurfaceLockedStatus = FALSE;


static HRESULT PASCAL EnumFunction(LPDIRECTDRAWSURFACE pSurface, LPDDSURFACEDESC lpSurfaceDesc, LPVOID  lpContext)
{

	// from MSDN documentation about DirectDraw...

    static BOOL bCalled = FALSE;

    if (!bCalled) {		// This function only can be called one time

        *((LPDIRECTDRAWSURFACE *)lpContext) = pSurface;
        bCalled = TRUE;
        return DDENUMRET_OK;
    }
    else {

        pSurface->Release();
        return DDENUMRET_CANCEL;
    }
}



void ddrawRelease(void)
{
    if (g_pDDSBack != NULL)
    {
        g_pDDSBack->Release();
        g_pDDSBack = NULL;
    }
    if (g_pDDSPrimary != NULL)
    {
        g_pDDSPrimary->Release();
        g_pDDSPrimary = NULL;
    }
    if (g_pDD != NULL)
    {
        g_pDD->Release();
        g_pDD = NULL;
    }
}



bool ddrawInit( HWND hWnd, int force )
{

	HRESULT hRet;

	ddrawVidSysInfo.VideoInfo = &VideoInfo;

	hRet = DirectDrawCreate(NULL, &g_pDD, NULL);
	if (hRet != DD_OK)
	{
		MessageBox( hWnd, _T("Error creating DirectDraw device."), _T("DirectDraw"), MB_OK | MB_ICONSTOP );
		return FALSE;
	}

    // Get exclusive mode
    hRet = g_pDD->SetCooperativeLevel(hWnd, DDSCL_FULLSCREEN);
    if (hRet != DD_OK)
	{
        MessageBox( hWnd, _T("Error getting exclusive mode."), _T("DirectDraw"), MB_OK | MB_ICONSTOP );
		return FALSE;
	}

    g_pDD->GetCaps(&ddCaps, &ddHelCaps);

	if (!(ddCaps.ddsCaps.dwCaps & DDSCAPS_FLIP)) 
		ddrawVidSysInfo.VideoInfo->SwapMethod = SWAP_NONE;			// Overrites swapmethod

    // Create the primary surface and may be a backbuffer
    
	memset(&ddsd, 0, sizeof(ddsd));
    ddsd.dwSize = sizeof(ddsd);

	if (!(ddCaps.ddsCaps.dwCaps & DDSCAPS_BACKBUFFER) || ddrawVidSysInfo.VideoInfo->SwapMethod != SWAP_FLIP )
	{
		// Without backbuffer
		ddsd.dwFlags = DDSD_CAPS;
		ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
		ddrawVidSysInfo.usingBackBuffer = FALSE;
	}
	else
	{
		// With backbuffer (the better case)
		ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
		ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP;
		ddsd.dwBackBufferCount = 1;
		ddrawVidSysInfo.usingBackBuffer = TRUE;
	}

    hRet = g_pDD->CreateSurface(&ddsd, &g_pDDSPrimary, NULL);
    if (hRet != DD_OK)
    {
		MessageBox( hWnd, _T("Error creating primary surface."), _T("DirectDraw"), MB_OK | MB_ICONSTOP );
		return FALSE;
    }

    // Get a pointer to the back buffer
	if( ddrawVidSysInfo.usingBackBuffer )
	{
		hRet = g_pDDSPrimary->EnumAttachedSurfaces(&g_pDDSBack, EnumFunction);
		if (hRet != DD_OK)
		{
			MessageBox( hWnd, _T("Error creating back surface."), _T("DirectDraw"), MB_OK | MB_ICONSTOP );
			return FALSE;
		}
	}

	return TRUE;

}



void ddrawBeginDraw()
{

	if( SurfaceLockedStatus )
		return;

	memset(&ddsd, 0, sizeof(ddsd));
    ddsd.dwSize = sizeof(ddsd);

	ddrawVidSysInfo.VideoInfo = &VideoInfo;

	if( ddrawVidSysInfo.usingBackBuffer )
		g_pDDSBack->Lock(0, &ddsd, DDLOCK_WAITNOTBUSY , 0);
	else
		g_pDDSPrimary->Lock(0, &ddsd, DDLOCK_DISCARD , 0);

	ddrawVidSysInfo.VideoInfo->pOrigin = (unsigned short *)ddsd.lpSurface;

	SurfaceLockedStatus = TRUE;

}



void ddrawEndDraw()
{

	if( !SurfaceLockedStatus )
		return;

	if( ddrawVidSysInfo.usingBackBuffer )
		g_pDDSBack->Unlock(0);
	else
		g_pDDSPrimary->Unlock(0);

	if( ddrawVidSysInfo.VideoInfo->SwapMethod == SWAP_FLIP )
		g_pDDSPrimary->Flip(NULL, 0);

	SurfaceLockedStatus = FALSE;

}


#endif	// DIRECTDRAW_BUILD

⌨️ 快捷键说明

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