Method of displaying background images

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
REAPER

Method of displaying background images

Post by REAPER »

I think that this code will be usefull to you Niko:

Code: Select all

/////////////CSurface.h/////////////////

class CSurface
{
private:

	DWORD m_Color;
	UINT m_uWidth;
	UINT m_uHeight;
	char* m_pszPathname;

public:
			
	CSurface(void);
	~CSurface(void);

	void CreateSurface(LPDIRECT3D8 pD3D, LPDIRECT3DDEVICE8 pD3D_Device, char* Pathname, UINT width, UINT height, DWORD color);

protected:
                 LPDIRECT3DSURFACE8 m_pSurface;
};

////////////CSurface.cpp//////////////

CSurface::CSurface(void) : m_pszPathname(NULL), m_Color(0), m_uWidth(0), m_uHeight(0)
{}

CSurface::~CSurface(void)
{
	SAFE_RELEASE(m_pSurface);
	SAFE_DELETE_ARRAY(m_pszPathname);
}

void CSurface::CreateSurface(LPDIRECT3D8 pD3D, LPDIRECT3DDEVICE8 pD3D_Device, char *Pathname, UINT width, UINT height, DWORD color)
{
	D3DDISPLAYMODE DisplayMode;
	D3DXIMAGE_INFO srcInfo;
	LPDIRECT3DSURFACE8 pSurface;

	HRESULT hr;

	if (!pD3D_Device) Except(999, "Invalid Direct3D device", "CSurface::CreateSurface()");
		
	m_Color = color;
	m_pszPathname = new char[strlen(Pathname) + 1];
	strcpy(m_pszPathname, Pathname);
	m_uWidth = width;
	m_uHeight = height;
		
	hr = pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &DisplayMode);
	if (FAILED(hr)) Except(999, "Failed to get adapter display mode", "CSurface::CreateSusrface()");

	if (m_uWidth == 0 || m_uHeight == 0)
	{	
		// Gets image size.
		hr = pD3D_Device->CreateImageSurface(1, 1, DisplayMode.Format, &pSurface );
		if (FAILED(hr)) Except(999, "Failed to create image surface", "CSurface::CreateSusrface()");

		hr = D3DXLoadSurfaceFromFile(pSurface, NULL, NULL, Pathname, NULL, D3DX_FILTER_NONE, 0, &srcInfo);
		
                                 if (FAILED(hr)) Except(999, "Failed to load surface from file", "CSurface::CreateSusrface()");

		hr = pSurface->Release();
		if (FAILED(hr)) Except(999, "Failed to release surface", "CSurface::CreateSusrface()");

		//Создаём поверхность
		hr = pD3D_Device->CreateImageSurface(srcInfo.Width, srcInfo.Height, DisplayMode.Format, &m_pSurface);
			
                                 if (FAILED(hr)) Except(999, "Failed to create image surface", "CSurface::CreateSusrface()");
	}
	else
	{
		//Creates surface
		hr = pD3D_Device->CreateImageSurface(m_uWidth, m_uHeight, DisplayMode.Format, &m_pSurface);
		if (FAILED(hr)) Except(999, "Failed to create image surface", "CSurface::CreateSusrface()");
	}
	// Loads surface from file
	hr = D3DXLoadSurfaceFromFile(m_pSurface, NULL, NULL, m_pszPathname, NULL, D3DX_FILTER_NONE, m_Color, NULL);
	if (FAILED(hr)) Except(999, "Failed to load surface from file", "CSurface::CreateSusrface()");
}

/////////////CBackSurface.h//////////////

class CBackSurface: public CSurface
{
	private:
	public:

		CBackSurface(void);
		~CBackSurface(void);
		void Update(LPDIRECT3DDEVICE8 pD3D_Device, LPDIRECT3DSURFACE8 pBackBuffer);
};

//////////////CBackSurface.cpp///////////

CBackSurface::CBackSurface(void) : CSurface() {}

CBackSurface::~CBackSurface(void)
{
	CSurface::~CSurface();
}

void CBackSurface::Update(LPDIRECT3DDEVICE8 pD3D_Device, LPDIRECT3DSURFACE8 pBackBuffer)
{
	HRESULT hr;

	if (!pBackBuffer) Except(999, "Invalid back buffer passed in", "CBackSurface::Update()");

	hr = pD3D_Device->CopyRects(m_pSurface, NULL, 0, pBackBuffer, NULL);
	if (FAILED(hr)) Except(999, "Failed to render back surface", "CBackSurface::Update()");
}
And how to use it

Code: Select all

CBackSurface surface;

surface.CreateSurface(D3D8Object, D3DDevice8Object(), "filename.bmp",  800, 600, D3DCOLOR_XRGB(255, 255, 255);

Then in game loop between beginScene() and endScene() we call:
surface.Update(D3DDevice8Object, BackBuffer);

//BackBuffer var is a surface:
LPDIRECT3DSURFACE8	 BackBuffer;

//To init this var we must in video driver initialization code write:
hr = m_pD3D_Device->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &BackBuffer);
I think that this method wiil be more faster than drawing large images as usual.

What you think about it?
Haddock
Posts: 15
Joined: Tue Dec 16, 2003 7:19 pm

Post by Haddock »

I have an issue which can be solved by this piece of code:
I would like to draw background images for an application which runs, let's say, at a resolution of 800x600. With the standard irrlicht method, a texture of 1024x1024 is created (512x512 in 0.5 version), and the content is scaled which leads to ugly artifacts.

So, I think I will implement this code, but I have to hack the engine :(
Perhaps there is another way of properly drawing background images ?
Post Reply