示例#1
0
//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
    // Create the D3D object.
    if( NULL == ( g_pD3D = Direct3DCreate8( D3D_SDK_VERSION ) ) )
        return E_FAIL;

    // Get the current desktop display mode, so we can set up a back
    // buffer of the same format
    D3DDISPLAYMODE d3ddm;
    if( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) )
        return E_FAIL;

    // Set up the structure used to create the D3DDevice
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory( &d3dpp, sizeof(d3dpp) );
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = d3ddm.Format;

    // Create the D3DDevice
    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                      &d3dpp, &g_pd3dDevice ) ) )
    {
        return E_FAIL;
    }

    // Turn off culling, so we see the front and back of the triangle
    g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );

    // Turn off D3D lighting, since we are providing our own vertex colors
    g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );

    return S_OK;
}
示例#2
0
BOOL Init_DX( HWND hWnd )
{
	D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory ( &d3dpp, sizeof ( d3dpp ) );
    d3dpp.Windowed   = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_FLIP;
    d3dpp.BackBufferWidth	= SCREENWIDTH;
	d3dpp.BackBufferHeight	= SCREENHEIGHT;
	d3dpp.BackBufferFormat	= D3DFMT_R5G6B5;
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
	
	lpD3D = Direct3DCreate8 ( D3D_SDK_VERSION ) ;
   

	if( FAILED( lpD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                     D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                     &d3dpp, &lpDevice ) ) )
	{
		
		d3dpp.BackBufferFormat = D3DFMT_X1R5G5B5;

		if ( FAILED ( lpD3D->CreateDevice ( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                         D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                         &d3dpp, &lpDevice ) ) )
										 return false;

	}

	D3DCAPS8 DevCaps;
	ZeroMemory( &DevCaps, sizeof ( D3DCAPS8 ) );
	lpDevice->GetDeviceCaps ( &DevCaps );


	return TRUE;
}
示例#3
0
void InitialiseD3D()
{

	g_pD3D = Direct3DCreate8(D3D_SDK_VERSION);

	D3DPRESENT_PARAMETERS d3dpp; 
	
	ZeroMemory(&d3dpp, sizeof(d3dpp));
	
	d3dpp.BackBufferWidth = 640;
	d3dpp.BackBufferHeight = 480;
	d3dpp.BackBufferFormat = D3DFMT_LIN_X8R8G8B8;
	d3dpp.BackBufferCount = 1;
	d3dpp.EnableAutoDepthStencil = TRUE;
	d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	g_pD3D->CreateDevice(0, D3DDEVTYPE_HAL, NULL, 
							D3DCREATE_HARDWARE_VERTEXPROCESSING, 
							&d3dpp, &g_pD3DDevice);

	g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
	g_pD3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
	g_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);

	g_pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_SELECTARG1);
	g_pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG1,D3DTA_DIFFUSE);

	g_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORKEYOP, D3DTCOLORKEYOP_KILL);
	g_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORKEYCOLOR, D3DCOLOR_XRGB( 150, 200, 250));

	g_pD3DDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
	g_pD3DDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
	g_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE);


	g_pD3DDevice->SetTextureStageState(0,D3DTSS_MAGFILTER, D3DTEXF_ANISOTROPIC);
	g_pD3DDevice->SetTextureStageState(0,D3DTSS_MINFILTER, D3DTEXF_ANISOTROPIC);

	g_pD3DDevice->SetTextureStageState(0, D3DTSS_MAXANISOTROPY, 1 );

	g_pD3DDevice->SetTextureStageState(0, D3DTSS_MIPMAPLODBIAS, *((LPDWORD)(&g_fMipMapLodBias)));
	g_pD3DDevice->SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_LINEAR);
}
示例#4
0
//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
    // Create the D3D object, which is needed to create the D3DDevice.
    if( NULL == ( g_pD3D = Direct3DCreate8( D3D_SDK_VERSION ) ) )
        return E_FAIL;

    // Get the current desktop display mode
    D3DDISPLAYMODE d3ddm;
    if( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) )
        return E_FAIL;

    // Set up the structure used to create the D3DDevice. Most parameters are
    // zeroed out. We set Windowed to TRUE, since we want to do D3D in a
    // window, and then set the SwapEffect to "discard", which is the most
    // efficient method of presenting the back buffer to the display.  And 
    // we request a back buffer format that matches the current desktop display 
    // format.
    D3DPRESENT_PARAMETERS d3dpp; 
    ZeroMemory( &d3dpp, sizeof(d3dpp) );
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = d3ddm.Format;

    // Create the Direct3D device. Here we are using the default adapter (most
    // systems only have one, unless they have multiple graphics hardware cards
    // installed) and requesting the HAL (which is saying we want the hardware
    // device rather than a software one). Software vertex processing is 
    // specified since we know it will work on all cards. On cards that support 
    // hardware vertex processing, though, we would see a big performance gain 
    // by specifying hardware vertex processing.
    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                      &d3dpp, &g_pd3dDevice ) ) )
    {
        return E_FAIL;
    }

    // Device state would normally be set here

    return S_OK;
}
示例#5
0
//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
    // Create the D3D object.
    if( NULL == ( g_pD3D = Direct3DCreate8( D3D_SDK_VERSION ) ) )
        return E_FAIL;

    // Get the current desktop display mode, so we can set up a back
    // buffer of the same format
    D3DDISPLAYMODE d3ddm;
    if( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) )
        return E_FAIL;

    // Set up the structure used to create the D3DDevice. Since we are now
    // using more complex geometry, we will create a device with a zbuffer.
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory( &d3dpp, sizeof(d3dpp) );
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = d3ddm.Format;
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

    // Create the D3DDevice
    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                      &d3dpp, &g_pd3dDevice ) ) )
    {
        return E_FAIL;
    }

    // Turn off culling
    g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );

    // Turn off D3D lighting
    g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );

    // Turn on the zbuffer
    g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );

    return S_OK;
}
示例#6
0
//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT Renderer::InitD3D()
{
    // Create the D3D object.
    if( nullptr == ( g_pD3D = Direct3DCreate8( D3D_SDK_VERSION ) ) )
        return E_FAIL;

    // Set up the structure used to create the D3DDevice.
    D3DPRESENT_PARAMETERS d3dpp; 
    ZeroMemory( &d3dpp, sizeof(d3dpp) );
    d3dpp.BackBufferWidth        = 640;
    d3dpp.BackBufferHeight       = 480;
    d3dpp.BackBufferFormat       = D3DFMT_X8R8G8B8;
    d3dpp.BackBufferCount        = 1;
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
    d3dpp.SwapEffect             = D3DSWAPEFFECT_DISCARD;

    // Create the Direct3D device.
    if( FAILED( g_pD3D->CreateDevice( 0, D3DDEVTYPE_HAL, nullptr,
                                      D3DCREATE_HARDWARE_VERTEXPROCESSING,
                                      &d3dpp, &g_pd3dDevice ) ) )
        return E_FAIL;

    // Turn off culling
    g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );

    // Turn off D3D lighting
    g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );

    // Turn on the zbuffer
    g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );

    g_pd3dDevice->SetRenderState( D3DRS_ALPHATESTENABLE, TRUE );
    g_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );
    //g_pd3dDevice->SetRenderState( D3DRS_SRCBLEND,  D3DBLEND_SRCALPHA );
    //g_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
    return S_OK;
}
RString SetD3DParams( bool &bNewDeviceOut )
{
	if( g_pd3dDevice == NULL ) // device is not yet created. We need to create it
	{
		bNewDeviceOut = true;
		HRESULT hr = g_pd3d->CreateDevice(
			D3DADAPTER_DEFAULT, 
			D3DDEVTYPE_HAL, 
			GraphicsWindow::GetHwnd(),
			D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED,
			&g_d3dpp, 
			&g_pd3dDevice );
		if( FAILED(hr) )
		{
			// Likely D3D_ERR_INVALIDCALL.  The driver probably doesn't support this video mode.
			return ssprintf( "CreateDevice failed: '%s'", GetErrorString(hr).c_str() );
		}
	}
	else
	{
		bNewDeviceOut = false;
		//LOG->Warn( "Resetting D3D device" );
		HRESULT hr = g_pd3dDevice->Reset( &g_d3dpp );
		if( FAILED(hr) )
		{
			// Likely D3D_ERR_INVALIDCALL.  The driver probably doesn't support this video mode.
			return ssprintf("g_pd3dDevice->Reset failed: '%s'", GetErrorString(hr).c_str() );
		}
	}

	g_pd3dDevice->SetRenderState( D3DRS_NORMALIZENORMALS, TRUE );	

	// Palettes were lost by Reset(), so mark them unloaded.
	g_TexResourceToPaletteIndex.clear();

	return RString();
}
示例#8
0
CString SetD3DParams( bool &bNewDeviceOut )
{
	if( g_pd3dDevice == NULL )		// device is not yet created.  We need to create it
	{
		bNewDeviceOut = true;
		HRESULT hr = g_pd3d->CreateDevice(
			D3DADAPTER_DEFAULT, 
			D3DDEVTYPE_HAL, 
#if !defined(XBOX)
			GraphicsWindow::GetHwnd(),
			D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED,
#else
			NULL,
			D3DCREATE_HARDWARE_VERTEXPROCESSING,
#endif
			&g_d3dpp, 
			&g_pd3dDevice );
		if( FAILED(hr) )
		{
			// Likely D3D_ERR_INVALIDCALL.  The driver probably doesn't support this video mode.
			return ssprintf( "CreateDevice failed: '%s'", GetErrorString(hr).c_str() );
		}
	}
	else
	{
		bNewDeviceOut = false;
		HRESULT hr = g_pd3dDevice->Reset( &g_d3dpp );
		if( FAILED(hr) )
		{
			// Likely D3D_ERR_INVALIDCALL.  The driver probably doesn't support this video mode.
			return ssprintf("g_pd3dDevice->Reset failed: '%s'", GetErrorString(hr).c_str() );
		}
	}

	return "";
}
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.
	HWND hwnd;
	MSG msg;
	char appname[6];
	strcpy(appname,"test1");
	WNDCLASS wc;
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = NULL;
	wc.hCursor = LoadCursor(NULL,IDC_ARROW);
	wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName = NULL;
	wc.lpszClassName = appname;
	RegisterClass(&wc);
	hwnd = CreateWindowEx(0/*WS_EX_TOPMOST*/,appname,NULL,WS_VISIBLE | WS_BORDER | WS_SYSMENU,0,0,800,600,NULL,NULL,hInstance,NULL);
	ShowWindow(hwnd,SW_SHOW);
	SetFocus(hwnd);


	if(NULL == (pd3d = Direct3DCreate8( D3D_SDK_VERSION ))) 
	{
		MessageBox(hwnd,"Failed to create D3D object",ERROR,MB_ICONERROR | MB_OK | MB_SYSTEMMODAL);
		//PostQuitMessage(0);
		return false;
	}

	D3DPRESENT_PARAMETERS d3dpp; 
	D3DDISPLAYMODE d3ddm;
    /*d3ddm.Width = 800;
	d3ddm.Height = 600;
	d3ddm.RefreshRate = 0;
	d3ddm.Format = D3DFMT_A8R8G8B8;
	*/pd3d->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm );
	ZeroMemory( &d3dpp, sizeof(d3dpp) );
	/*d3dpp.BackBufferWidth = 800;
	d3dpp.BackBufferHeight = 600;
	d3dpp.BackBufferCount = 1;
	d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
	d3dpp.Windowed   = FALSE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = d3ddm.Format;
    d3dpp.hDeviceWindow = NULL;
	d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER ;
	d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; 
	d3dpp.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_ONE;
	d3dpp.EnableAutoDepthStencil = TRUE;
	d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
	*/d3dpp.Windowed   = TRUE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = d3ddm.Format;
	d3dpp.EnableAutoDepthStencil = TRUE;
	d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
	if(FAILED(pd3d->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
                                  D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                  &d3dpp, &d3ddevice )))
	{
		MessageBox(hwnd,"Failed to Initialize D3D object",ERROR,MB_ICONERROR | MB_OK | MB_SYSTEMMODAL);
		//PostQuitMessage(0);
		return false;
	}


  setup();
   


	SetTimer(hwnd,1,20,NULL);
	//ShowCursor(false);
	while(!done)
	{
		while(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		//if(msg.message == WM_QUIT) break;
		StartFrame();
		Render();
		EndFrame();
	}
	KillTimer(hwnd,1);
	shutdown();
	if( d3ddevice != NULL)
        d3ddevice->Release();
    if( pd3d != NULL)
        pd3d->Release();
	//ShowCursor(true);

	return 0;
}
示例#10
0
int main(int argc, char* argv[])
{
  int NoProtect = 0;
  AllowLinear = true;
  double MaxMSE = 4.0;

	CmdLineArgs args;

	if (args.size() == 1)
	{
		Usage();
		return 1;
	}

	const char* InputDir = NULL;
	const char* OutputFilename = "Textures.xpr";

	for (unsigned int i = 1; i < args.size(); ++i)
	{
		if (!stricmp(args[i], "-help") || !stricmp(args[i], "-h") || !stricmp(args[i], "-?"))
		{
			Usage();
			return 1;
		}
		else if (!stricmp(args[i], "-input") || !stricmp(args[i], "-i"))
		{
			InputDir = args[++i];
		}
		else if (!stricmp(args[i], "-output") || !stricmp(args[i], "-o"))
		{
			OutputFilename = args[++i];
		}
    else if (!stricmp(args[i], "-noprotect") || !stricmp(args[i], "-p"))
    {
      NoProtect = 1;
    }
    else if (!stricmp(args[i], "-onlyswizzled") || !stricmp(args[i], "-s"))
    {
      AllowLinear = false;
    }
    else if (!stricmp(args[i], "-quality") || !stricmp(args[i], "-q"))
		{
			++i;
			if (!stricmp(args[i], "min"))
			{
				MaxMSE = DBL_MAX;
			}
			else if (!stricmp(args[i], "low"))
			{
				MaxMSE = 20.0;
			}
			else if (!stricmp(args[i], "normal"))
			{
				MaxMSE = 4.0;
			}
			else if (!stricmp(args[i], "high"))
			{
				MaxMSE = 1.5;
			}
			else if (!stricmp(args[i], "max"))
			{
				MaxMSE = 0.0;
			}
			else
			{
				printf("Unrecognised quality setting: %s\n", args[i]);
			}
		}
		else
		{
			printf("Unrecognised command line flag: %s\n", args[i]);
		}
	}

	// Initialize DirectDraw
	pD3D = Direct3DCreate8(D3D_SDK_VERSION);
	if (pD3D == NULL)
	{
		puts("Cannot init D3D");
		return 1;
	}

	HRESULT hr;
	D3DDISPLAYMODE dispMode;
	D3DPRESENT_PARAMETERS presentParams;

	pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &dispMode);

	ZeroMemory(&presentParams, sizeof(presentParams));
	presentParams.Windowed = TRUE;
	presentParams.hDeviceWindow = GetConsoleWindow();
	presentParams.SwapEffect = D3DSWAPEFFECT_COPY;
	presentParams.BackBufferWidth = 8;
	presentParams.BackBufferHeight = 8;
	presentParams.BackBufferFormat = dispMode.Format;

	hr = pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, NULL, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParams, &pD3DDevice);
	if (FAILED(hr))
	{
		printf("Cannot init D3D device: %08x\n", hr);
		pD3D->Release();
		return 1;
	}

	char HomeDir[MAX_PATH];
	GetCurrentDirectory(MAX_PATH, HomeDir);

	XPRFile.OutputBuf = (char*)VirtualAlloc(0, 64 * 1024 * 1024, MEM_RESERVE, PAGE_NOACCESS);
	if (!XPRFile.OutputBuf)
	{
		printf("Memory allocation failure: %08x\n", GetLastError());
		pD3DDevice->Release();
		pD3D->Release();
		return 1;
	}

	Bundler.StartBundle();

	// Scan the input directory (or current dir if false) for media files
	ConvertDirectory(InputDir, NULL, MaxMSE);

	VirtualFree(XPRFile.OutputBuf, 0, MEM_RELEASE);

	pD3DDevice->Release();
	pD3D->Release();

	SetCurrentDirectory(HomeDir);
	DWORD attr = GetFileAttributes(OutputFilename);
	if (attr != -1 && (attr & FILE_ATTRIBUTE_DIRECTORY))
	{
		SetCurrentDirectory(OutputFilename);
		OutputFilename = "Textures.xpr";
	}

	printf("\nWriting bundle: %s", OutputFilename);
  int BundleSize = Bundler.WriteBundle(OutputFilename, NoProtect);
	if (BundleSize == -1)
	{
		printf("\nERROR: %08x\n", GetLastError());
		return 1;
	}

	printf("\nUncompressed texture size: %6dkB\nCompressed texture size: %8dkB\nBundle size:             %8dkB\n\nWasted Pixels: %u/%u (%5.2f%%)\n",
		(UncompressedSize + 1023) / 1024, (((CompressedSize + 1023) / 1024) + 3) & ~3, (BundleSize + 1023) / 1024,
		TotalDstPixels - TotalSrcPixels, TotalDstPixels, 100.f * (float)(TotalDstPixels - TotalSrcPixels) / (float)TotalDstPixels);

	return 0;
}
示例#11
0
//*******
// this function initializes Direct3D... 
bool init3D(HWND hWnd)
{
   D3DDISPLAYMODE d3ddm;   // Direct3D Display Mode..
   D3DPRESENT_PARAMETERS d3dpp;   // d3d present parameters..details on how it should present
         // a scene.. such as swap effect.. size.. windowed or full screen.. 
   HFONT fnt;

   // create D3D8.. if error (like that ever happens..) return false..
   if (NULL == (lpD3D8 = Direct3DCreate8(D3D_SDK_VERSION)))
      return false;
   //  get display adapter mode.. if error return false..
   if (FAILED(lpD3D8->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
      return false;

   // NOTE:  it's possible that this example will not work as is
   //        in windowed mode due to display format and depth buffer format
   ZeroMemory(&d3dpp, sizeof(d3dpp));            // blank the memory for good measure..
   //   d3dpp.Windowed = true;                   // use this for window mode..
   d3dpp.Windowed = false;                       // use this for full screen... it's that easy!
   d3dpp.BackBufferWidth = 1024;                 // rather useless for windowed version
   d3dpp.BackBufferHeight = 768;                 // ditto..but left it in anyway for full screen
   d3dpp.SwapEffect = D3DSWAPEFFECT_FLIP;        // flip using a back buffer...easy performance
   d3dpp.BackBufferFormat = D3DFMT_R5G6B5;       // just set back buffer format from display mode
   d3dpp.EnableAutoDepthStencil = true;
   d3dpp.AutoDepthStencilFormat =  D3DFMT_D16;   // 16 bit depth buffer..

   // Create the D3DDevice
   if (FAILED(lpD3D8->CreateDevice(D3DADAPTER_DEFAULT,   // which display adapter..
             D3DDEVTYPE_HAL,   
             hWnd, D3DCREATE_MIXED_VERTEXPROCESSING,     // mixed, software, or hardware..
                  &d3dpp, &lpD3DDevice8)))               // sends stuff.. and to receive
   {  // if it fails to create with HAL, then try (usually succeeds) to create
      // with ref (software) using a much lower screen res
      d3dpp.BackBufferWidth = 320;
      d3dpp.BackBufferHeight = 240;
      d3dpp.Windowed = false;
      if (FAILED(lpD3D8->CreateDevice(D3DADAPTER_DEFAULT,
                D3DDEVTYPE_REF,
                hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                &d3dpp, &lpD3DDevice8)))
      {
         return false;     
      }
      else
      {  // create smaller font for software... lower res..
         fnt = CreateFont(20, 10, 2, 0, 500, 0, 0, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS,
               CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_MODERN, 0);
         // disable dithering for software..
         lpD3DDevice8->SetRenderState( D3DRS_DITHERENABLE, false);
      }
   }
   else
   {  // create larger font for hardware.. higher res
      fnt = CreateFont(50, 22, 2, 0, 500, 0, 0, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS,
            CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_MODERN, 0);
      // enable dithering for hardware... 
      lpD3DDevice8->SetRenderState( D3DRS_DITHERENABLE, true);
   }

   lpD3DDevice8->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
   lpD3DDevice8->SetRenderState( D3DRS_ZENABLE, false);
   
   lpD3DDevice8->SetRenderState( D3DRS_LIGHTING, false );

   // create a D3DXFont from a windows font created above...
   // for use in drawing text with D3D
   D3DXCreateFont(lpD3DDevice8, fnt, &lpD3DXFont);

   lpD3DDevice8->SetRenderState(D3DRS_ALPHABLENDENABLE , true);
   lpD3DDevice8->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
   lpD3DDevice8->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_SRCALPHA);

   return true;
}
示例#12
0
int D3DM_InitDevices( HWND hwnd, int w, int h, int full, HMENU menu )
{
	char	buf[256];
	int		i,j;
		LPDIRECTDRAW7 pDD = NULL;	
		HRESULT hr;
		if( FAILED( hr = DirectDrawCreateEx( NULL, (VOID**)&pDD, IID_IDirectDraw7, NULL ) ) )
			return DDENUMRET_CANCEL;

		D3DCapsStruct.m_ddCaps.dwSize = sizeof(DDCAPS);
		pDD->GetCaps( &D3DCapsStruct.m_ddCaps, NULL );
		if(pDD) {
			pDD->Release();
			pDD = NULL;
		}
	ZeroMemory( d3dTexture, sizeof(D3DD_TEXTURE)*D3DD_MAX_TEXTURE_AMOUNT );
	ZeroMemory( d3dText, sizeof(D3DD_TEXT)*D3DD_MAX_TEXT_AMOUNT );
	ZeroMemory( d3dDisp, sizeof(D3DD_DISP)*D3DD_MAX_DISP_AMOUNT );


	
	pD3D = Direct3DCreate8(D3D_SDK_VERSION);
	if(pD3D == NULL){
		MessageBox(NULL,"Direct3Dオブジェクトの生成に失敗しました。[DirectX8.1が入っていない?]","致命的なエラー", MB_OK | MB_ICONSTOP);
		return FALSE;
	}

	
	if( FAILED(pD3D->GetDeviceCaps( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &D3DCapsStruct.m_d3dCaps)) ){
		MessageBox(NULL,"デバイス能力の取得に失敗しました","致命的なエラー", MB_OK | MB_ICONSTOP);
		return FALSE;
	}
	
	EnumAdapters(D3DADAPTER_DEFAULT);

	
	if( (int)D3DCapsStruct.m_d3dCaps.MaxTextureWidth < D3DD_TEXTURE_CONTROL_SIZE ){
		DebugBox( NULL, "このビデオカードは、幅 %d pixel 以上のサイズのテクスチャを生成できません。[%s]", D3DD_TEXTURE_CONTROL_SIZE);
		return FALSE;
	}else if( (int)D3DCapsStruct.m_d3dCaps.MaxTextureHeight < D3DD_TEXTURE_CONTROL_SIZE ){
		DebugBox( NULL, "このビデオカードは、高さ %d pixel 以上のサイズのテクスチャを生成できません。[%s]", D3DD_TEXTURE_CONTROL_SIZE );
		return FALSE;
	}

	if( !(D3DCapsStruct.m_d3dCaps.ShadeCaps&D3DPSHADECAPS_ALPHAGOURAUDBLEND) ){
		MessageBox(NULL,"このビデオデバイスはグーロブレンディングに対応していません。\nゲームの画像が乱れることがあります","警告", MB_OK | MB_ICONSTOP);
	}
	if( !(D3DCapsStruct.m_d3dCaps.ShadeCaps&D3DPSHADECAPS_COLORGOURAUDRGB) ){
		MessageBox(NULL,"このビデオデバイスはグーロシェーディングに対応していません。\nゲームの画像が乱れることがあります","警告", MB_OK | MB_ICONSTOP);
	}

	if( D3DCapsStruct.m_d3dCaps.TextureCaps&D3DPTEXTURECAPS_SQUAREONLY ){
		DebugBox( NULL, "このビデオカードは長方形テクスチャを生成できません。[デバッグ用ダイアログ]" );
	}

	
	if( FAILED(pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&D3DCapsStruct.m_NowDisplayMode)) ){	
		MessageBox(NULL,"ディスプレイモードの取得に失敗しました。[なにゆえ?]","致命的なエラー", MB_OK | MB_ICONSTOP);
		return FALSE;
	}
	D3DCapsStruct.m_WindowDisplayMode = D3DCapsStruct.m_NowDisplayMode;
	D3DMain.m_DrawHwnd = hwnd;
	D3DMain.m_MenuHwnd = menu;
	if( GetSystemMetrics(SM_CXFULLSCREEN)<=800 || GetSystemMetrics(SM_CYFULLSCREEN)<=600){
		D3DMain.m_FullScreenOnly = TRUE;
		D3DMain.m_WindowMode = FALSE;
	}else{
		D3DMain.m_FullScreenOnly = FALSE;
		D3DMain.m_WindowMode = !full;
	}


	ZeroMemory(&d3dppApp,sizeof(d3dppApp));
	


	WinWidth  = w;
	WinHeight = h;



	d3dppApp.SwapEffect = D3DSWAPEFFECT_COPY;

	d3dppApp.BackBufferFormat = D3DCapsStruct.m_NowDisplayMode.Format;
	d3dppApp.BackBufferCount = 1;
	d3dppApp.BackBufferWidth  = WinWidth;
	d3dppApp.BackBufferHeight = WinHeight;





	d3dppApp.Windowed = TRUE;							
	d3dppApp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;

	
	HRESULT	ret;
	ret = pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,D3DCREATE_HARDWARE_VERTEXPROCESSING,&d3dppApp,&pD3DDevice);
	if( FAILED(ret) ){	
		ret = pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dppApp,&pD3DDevice);
		if( FAILED(ret) ){	
			D3DMain.m_FullScreenOnly = TRUE;
		}
	}
	if(D3DMain.m_FullScreenOnly==TRUE){
		RELEASE_3D(pD3DDevice);

		D3DMain.m_WindowMode = FALSE;
		ZeroMemory(&d3dppApp,sizeof(d3dppApp));
		for(i=0;i<D3DCapsStruct.m_DisplayModeNum;i++){
			if( D3DCapsStruct.m_DisplayMode[i].Width  == 800 && D3DCapsStruct.m_DisplayMode[i].Height == 600 ){
				switch( D3DCapsStruct.m_DisplayMode[i].Format ){
					case D3DFMT_R5G6B5:		case D3DFMT_X1R5G5B5:
					case D3DFMT_A1R5G5B5:	case D3DFMT_A4R4G4B4:
					case D3DFMT_X4R4G4B4:
						D3DCapsStruct.m_FullModeNum=i;

						d3dppApp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
						d3dppApp.Windowed = D3DMain.m_WindowMode;		
						d3dppApp.SwapEffect = FULL_FLIP;		
						d3dppApp.BackBufferFormat = D3DCapsStruct.m_DisplayMode[i].Format;	
						d3dppApp.BackBufferCount = 1;
						d3dppApp.BackBufferWidth  = WinWidth;
						d3dppApp.BackBufferHeight = WinHeight;



						break;
				}
			}
			if(d3dppApp.SwapEffect) break;
		}
		if(!full){
			wsprintf( buf, "このビデオカードの現在のモードではゲームを実行できません。フルスクリーン化しますか?\n[%d]", D3DCapsStruct.m_DisplayMode[i].RefreshRate );
			if( MessageBox( NULL, buf, "問い合わせ", MB_YESNO )==IDNO ){
				RELEASE_3D(pD3D);
				return FALSE;
			}
		}

		ret = pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,D3DCREATE_HARDWARE_VERTEXPROCESSING,&d3dppApp,&pD3DDevice);
		if( FAILED(ret) ){	
			ret = pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dppApp,&pD3DDevice);
			if( FAILED(ret) ){	
				switch(ret){
				default:
				case D3DERR_OUTOFVIDEOMEMORY:
					DebugBox( NULL, "Direct3D が処理を行うのに十分なディスプレイ メモリがありません。" );
					RELEASE_3D(pD3D);
					return FALSE;
				case D3DERR_INVALIDCALL:
					DebugBox( NULL, "Direct3Dの初期化に失敗しました[D3DERR_INVALIDCALL]\nこのグラフィックカードは必要な機能をサポートしていないか、\nあるいはDirectX8に対応したドライバが入っていません。" );
					RELEASE_3D(pD3D);
					return FALSE;
				case D3DERR_NOTAVAILABLE:
					DebugBox( NULL, "Direct3Dの初期化に失敗しました[D3DERR_NOTAVAILABLE]\nこのグラフィックカードは必要な機能をサポートしていないか、\nあるいはDirectX8に対応したドライバが入っていません。" );
					RELEASE_3D(pD3D);
					return FALSE;
				}
			}
		}
	}else{
		if(D3DMain.m_WindowMode){	
		}else{
			RELEASE_3D(pD3DDevice);
			ZeroMemory(&d3dppApp,sizeof(d3dppApp));

	
			d3dppApp.SwapEffect = FULL_FLIP;
	
	
			d3dppApp.BackBufferCount = 1;
			d3dppApp.BackBufferWidth  = WinWidth;
			d3dppApp.BackBufferHeight = WinHeight;

		
			d3dppApp.Windowed = FALSE;							
			d3dppApp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;


			if(DrawSetting.full_16bit){
				d3dppApp.BackBufferFormat=D3DFMT_UNKNOWN;
				for(i=0;i<D3DCapsStruct.m_DisplayModeNum;i++){
					if( D3DCapsStruct.m_DisplayMode[i].Width  == 800 && D3DCapsStruct.m_DisplayMode[i].Height == 600 ){
						switch( D3DCapsStruct.m_DisplayMode[i].Format ){
							case D3DFMT_R5G6B5:		case D3DFMT_X1R5G5B5:
							case D3DFMT_A1R5G5B5:	case D3DFMT_A4R4G4B4:
							case D3DFMT_X4R4G4B4:
								D3DCapsStruct.m_FullModeNum=i;
								d3dppApp.BackBufferFormat = D3DCapsStruct.m_DisplayMode[i].Format;	
								break;
						}
					}
					if(d3dppApp.BackBufferFormat!=D3DFMT_UNKNOWN) break;
				}
			}else{
				d3dppApp.BackBufferFormat = D3DCapsStruct.m_NowDisplayMode.Format;
			}

			ret = pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,D3DCREATE_HARDWARE_VERTEXPROCESSING,&d3dppApp,&pD3DDevice);
			if( FAILED(ret) ){	
				ret = pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dppApp,&pD3DDevice);
				if( FAILED(ret) ){	
					switch(ret){
					default:
					case D3DERR_OUTOFVIDEOMEMORY:
						DebugBox( NULL, "Direct3D が処理を行うのに十分なディスプレイ メモリがありません。" );
						RELEASE_3D(pD3D);
						return FALSE;
					case D3DERR_INVALIDCALL:
						DebugBox( NULL, "Direct3Dの初期化に失敗しました[D3DERR_INVALIDCALL]\nこのグラフィックカードは必要な機能をサポートしていないか、\nあるいはDirectX8に対応したドライバが入っていません。" );
						RELEASE_3D(pD3D);
						return FALSE;
					case D3DERR_NOTAVAILABLE:
						DebugBox( NULL, "Direct3Dの初期化に失敗しました[D3DERR_NOTAVAILABLE]\nこのグラフィックカードは必要な機能をサポートしていないか、\nあるいはDirectX8に対応したドライバが入っていません。" );
						RELEASE_3D(pD3D);
						return FALSE;
					}
				}
			}
		}
	}



	if( FAILED(pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&D3DCapsStruct.m_NowDisplayMode)) ){	
		MessageBox(NULL,"ディスプレイモードの取得に失敗しました。[なにゆえ?]","致命的なエラー", MB_OK | MB_ICONSTOP);
		RELEASE_3D(pD3D);
		return FALSE;
	}
	D3DCapsStruct.m_ttCaps.m_R8G8B8		= IsTextureFormatOk( D3DFMT_R8G8B8,   D3DCapsStruct.m_NowDisplayMode.Format);
	D3DCapsStruct.m_ttCaps.m_X8R8G8B8	= IsTextureFormatOk( D3DFMT_X8R8G8B8, D3DCapsStruct.m_NowDisplayMode.Format);
	D3DCapsStruct.m_ttCaps.m_A8R8G8B8	= IsTextureFormatOk( D3DFMT_A8R8G8B8, D3DCapsStruct.m_NowDisplayMode.Format);

	D3DCapsStruct.m_ttCaps.m_R5G6B5		= IsTextureFormatOk( D3DFMT_R5G6B5,   D3DCapsStruct.m_NowDisplayMode.Format);
	D3DCapsStruct.m_ttCaps.m_X1R5G5B5	= IsTextureFormatOk( D3DFMT_X1R5G5B5, D3DCapsStruct.m_NowDisplayMode.Format);
	D3DCapsStruct.m_ttCaps.m_A1R5G5B5	= IsTextureFormatOk( D3DFMT_A1R5G5B5, D3DCapsStruct.m_NowDisplayMode.Format);

	D3DCapsStruct.m_ttCaps.m_X4R4G4B4	= IsTextureFormatOk( D3DFMT_X4R4G4B4, D3DCapsStruct.m_NowDisplayMode.Format);
	D3DCapsStruct.m_ttCaps.m_A4R4G4B4	= IsTextureFormatOk( D3DFMT_A4R4G4B4, D3DCapsStruct.m_NowDisplayMode.Format);

	D3DCapsStruct.m_ttCaps.m_A8P8		= IsTextureFormatOk( D3DFMT_A8P8,     D3DCapsStruct.m_NowDisplayMode.Format);
	D3DCapsStruct.m_ttCaps.m_P8			= IsTextureFormatOk( D3DFMT_P8,       D3DCapsStruct.m_NowDisplayMode.Format);
	D3DCapsStruct.m_ttCaps.m_A8			= IsTextureFormatOk( D3DFMT_A8,       D3DCapsStruct.m_NowDisplayMode.Format);
	
	D3DD_SetBackBuffer( WinWidth, WinHeight );
	pD3DDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_ARGB(0,0,0,0),1.0,0);

	if(0){
		char	buf2[512];

		wsprintf( buf, "実行ディスプレイモード[%s]\n", TxFmtMode[ LIM(d3dppApp.BackBufferFormat,0,D3DFMT_D3DD_MAX-1) ] );
		MBS_CPY(buf2,buf);

		MBS_CAT(buf2,"使用可能テクスチャ列挙\n");
		for(i=1;i<D3DFMT_D3DD_MAX-1;i++){
			if( TxFmtMode[i] ){
				j = IsTextureFormatOk( (D3DFORMAT)i, D3DCapsStruct.m_NowDisplayMode.Format);
				if(j){
					wsprintf(buf,"%-15s[%s]\n", TxFmtMode[i], (j==2)?"OK":"OK(NotRenderTarget)" );
					MBS_CAT(buf2,buf);
				}else{
					wsprintf(buf,"%-15s[%s]\n", TxFmtMode[i], "--Not--" );
					MBS_CAT(buf2,buf);
				}
			}
		}

		
		DebugBox(NULL,buf2);



	}

	D3DD_CreateTable();

	return TRUE;
}