How should I handle different res. and resizing?

Started by
4 comments, last by Helo7777 11 years, 2 months ago

So I here is the initialization of a game:


//initialize Direct3D
		this->p_d3d = Direct3DCreate9(D3D_SDK_VERSION);
		if (this->p_d3d == NULL) {
			return 0;
		}
	
		//get system desktop color depth
		D3DDISPLAYMODE dm;
		this->p_d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &dm);
	
		//set configuration options for Direct3D
		D3DPRESENT_PARAMETERS d3dpp;
		ZeroMemory(&d3dpp, sizeof(d3dpp));
		d3dpp.Windowed = (!fullscreen);
		d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
		d3dpp.EnableAutoDepthStencil = TRUE;
		d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
		d3dpp.PresentationInterval   = D3DPRESENT_INTERVAL_IMMEDIATE;
		d3dpp.BackBufferFormat = dm.Format;
		d3dpp.BackBufferCount = 1;
		d3dpp.BackBufferWidth = width;
		d3dpp.BackBufferHeight = height;
		d3dpp.hDeviceWindow = p_windowHandle;
	
		//create Direct3D device
		this->p_d3d->CreateDevice(
			D3DADAPTER_DEFAULT,
			D3DDEVTYPE_HAL,
			this->p_windowHandle,
			D3DCREATE_HARDWARE_VERTEXPROCESSING,
			&d3dpp,
			&this->p_device);
	
		if (this->p_device == NULL) return 0;
	
		//clear the backbuffer to black
		this->ClearScene(D3DCOLOR_XRGB(0,0,0));
	
		//create pointer to the back buffer
		this->p_device->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &this->p_backbuffer);
	
	    //use ambient lighting and z-buffering
		this->p_device->SetRenderState(D3DRS_ZENABLE, TRUE);
		this->p_device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
        this->p_device->SetRenderState(D3DRS_LIGHTING, true);
		this->SetAmbient(this->p_ambientColor);
	
		//initialize 2D renderer
		HRESULT result = D3DXCreateSprite(this->p_device, &this->p_sprite_handler);

I put the code here just to show that there is no viewport creation,just in case.

My problem is,that when I resize the window,the images get distorted.I tried to think about a solution but I can't find one.

Can someone make a suggestion?

Advertisement

I have not done really any Direct3D API programming before (mostly XNA) but id assume it be around the fact that you need to change the backbuffer width/height when the form gets re-sized. This means you would need make your game handle resizing in the first place, this could be the resizing of render targets etc... etc...

Personally I would only allow the user to resize (change the resolution of the game) in the menu of the game where I wont have to change much where as I would if the main game was running.

When the window gets resized (or full-screened) you have to recreate the backbuffers.

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

that's not the problem...i know from the past that the images will still be stretched...

They are stretched because the back buffers’ sizes do not match that of the window. Recreate them when the window is resized, with the correct dimensions.

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

When windows sends your app a message such as WM_SIZE you should recreate the back buffer. After it's been resized the back buffer will not be stretched. You can use the following to get the new size from the message pump:


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message) 
    {
    case WM_SIZE:
    {
        int screenWidth = LOWORD(lParam);
        int screenHeight = HIWORD(lParam);

        // do backbuffer resizing here

        break;
    }
    //... handle any other msgs
    }
}

This topic is closed to new replies.

Advertisement