CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2011
    Posts
    4

    Question DirectX9 FullScreen Issues

    I'm new to Windows programming and DirectX, but I feel I'm moving along at a good pace, but I seem to have hit a wall. I just learned about fullscreen mode with DirectX, and I successfully programmed a working program that just drew a rectangle the size of the screen that constantly changed colors. Now I am trying to make a program that draws a rectangle of random size, randomly on the screen, and then have it bounce around the screen without going off (while changing its color). This program works 100% in windowed mode, but in fullscreen mode I get an error when the Direct3D Device is trying to be created... here is the code (for fullscreen mode, only thing i change between fullscreen and windowed mode is the d3dpp.windowed = true/false);

    here is the code...

    Code:
    //Bouncing Screen Project
    #include<windows.h>
    #include<time.h>
    #include<d3d9.h>
    #include<d3dx9.h>
    #include<string>
    using namespace std;
    
    #pragma comment(lib, "d3dx9.lib")
    #pragma comment(lib, "d3d9.lib")
    
    #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code)) & 0x8000) ? 1 : 0)
    
    bool gameover = false;
    bool goingup = true, goingright = true;
    const int SCREENH = 640;
    const int SCREENW = 480;
    
    RECT rect;
    
    //Direct 3D Objects
    LPDIRECT3D9 d3d = NULL;
    LPDIRECT3DDEVICE9 d3ddev = NULL;
    LPDIRECT3DSURFACE9 backbuffer;
    LPDIRECT3DSURFACE9 surface;
    
    
    bool Game_Init(HWND window)
    {
    	d3d = Direct3DCreate9(D3D_SDK_VERSION);
    	if(d3d == 0)
    	{
    		MessageBox(window, "Error Initilizing Direct3D", "Error", MB_OK);
    		return false;
    	}
    
    	D3DPRESENT_PARAMETERS d3dpp;
    	ZeroMemory(&d3dpp, sizeof(d3dpp));
    	d3dpp.Windowed = false;
    	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    	d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
    	d3dpp.BackBufferCount = 1;
    	d3dpp.BackBufferWidth = SCREENW;
    	d3dpp.BackBufferHeight = SCREENH;
    	d3dpp.hDeviceWindow = window;
    
    	d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, window,  D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);
    
    	if(d3ddev == NULL) //<------------- Is equal to true when run in fullscreen mode
    	{
    		MessageBox(window, "Error creating Direct3D Device", "Error", MB_OK);
    		return false;
    	}
    
    	srand(time(NULL));
    
    	d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    
    	d3ddev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
    
    	int boxh = rand() % 100;
    	int boxw = rand() % 100;
    	boxh++;
    	boxw++;
    
    	HRESULT result = d3ddev->CreateOffscreenPlainSurface(boxw, boxh,
    														 D3DFMT_X8R8G8B8,
    														 D3DPOOL_DEFAULT,
    														 &surface,
    														 NULL);
    
    	rect.top = rand() % SCREENH;
    	rect.bottom = rect.top + boxh;
    	while(rect.bottom > SCREENH)
    	{
    		rect.bottom--;
    		rect.top--;
    	}
    	rect.left = rand() % SCREENW;
    	rect.right = rect.left + boxw;
    	while(rect.right > SCREENW)
    	{
    		rect.right--;
    		rect.left--;
    	}
    
    	if(!SUCCEEDED(result))
    		return false;
    
    	return true;
    }
    
    void Game_Run(HWND hWnd)
    {
    	if(!d3ddev)
    		return;
    
    	d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    
    	if(d3ddev->BeginScene())
    	{
    		int r = rand() % 255, g = rand() % 255, b = rand() % 255;
    		d3ddev->ColorFill(surface, NULL, D3DCOLOR_XRGB(r, g, b));
    
    		d3ddev->StretchRect(surface, NULL, backbuffer, &rect, D3DTEXF_NONE);
    
    		d3ddev->EndScene();
    	}
    
    	d3ddev->Present(NULL, NULL, NULL, NULL);
    
    	if(goingup)
    	{
    		if((rect.top - 1) < 0)
    		{
    			goingup = false;
    			rect.top++;
    			rect.bottom++;
    		}
    		else
    		{
    			rect.top--;
    			rect.bottom--;
    		}
    	}
    	else if(!goingup)
    	{
    		if((rect.bottom + 1) > SCREENH)
    		{
    			goingup = true;
    			rect.top--;
    			rect.bottom--;
    		}
    		else
    		{
    			rect.top++;
    			rect.bottom++;
    		}
    	}
    	if(goingright)
    	{
    		if((rect.right + 1) > SCREENW)
    		{
    			goingright = false;
    			rect.right--;
    			rect.left--;
    		}
    		else
    		{
    			rect.right++;
    			rect.left++;
    		}
    	}
    	else if(!goingright)
    	{
    		if((rect.left - 1) < 0)
    		{
    			goingright = true;
    			rect.right++;
    			rect.left++;
    		}
    		else
    		{
    			rect.right--;
    			rect.left--;
    		}
    	}
    
    	if(KEY_DOWN(VK_ESCAPE)
    		PostMessage(hWnd, WM_DESTROY, 0, 0);
    }
    
    void Game_End(HWND hWnd)
    {
    	if(surface)
    		surface->Release();
    	if(d3ddev)
    		d3ddev->Release();
    	if(d3d)
    		d3d->Release();
    }
    
    LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	switch(msg)
    	{
    	case WM_DESTROY:
    		gameover = true;
    		PostQuitMessage(0);
    		return 0;
    	}
    
    	return DefWindowProc(hWnd, msg, wParam, lParam);
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
    	WNDCLASSEX wc;
    	wc.cbSize = sizeof(WNDCLASSEX);
    	wc.style = CS_HREDRAW | CS_VREDRAW;
    	wc.cbClsExtra = 0;
    	wc.cbWndExtra = 0;
    	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    	wc.hIcon = NULL;
    	wc.hIconSm = NULL;
    	wc.hInstance = hInstance;
    	wc.lpfnWndProc = WinProc;
    	wc.lpszClassName = "Bouncy";
    	wc.lpszMenuName = NULL;
    	RegisterClassEx(&wc);
    
    	HWND window = CreateWindow("Bouncy", "Bouncy", WS_EX_TOPMOST | WS_VISIBLE | WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT, SCREENW, SCREENH, NULL, NULL, hInstance, NULL);
    
    	if(window == 0)
    		return 0;
    
    	ShowWindow(window, nCmdShow);
    	UpdateWindow(window);
    
    	MessageBox(window, "About to load Direct3D...", "BreakPoint", MB_OK);
    
    	if(!Game_Init(window))
    		return 0;
    
    	MSG message;
    
    	while(!gameover)
    	{
    		if(PeekMessage(&message, NULL, 0, 0, PM_REMOVE))
    		{
    			TranslateMessage(&message);
    			DispatchMessage(&message);
    		}
    
    		Game_Run(window);
    	}
    
    	Game_End(window);
    
    	return message.wParam;
    }

  2. #2
    Join Date
    Apr 1999
    Posts
    3,585

    Re: DirectX9 FullScreen Issues

    Try running with the DirectX debugging applet. It should point out any errors you may have.
    Gort...Klaatu, Barada Nikto!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured