Sign in to follow this  
Slyfox

drawing 2d images

Recommended Posts

Hi, I've managed to get 2D images being drawn in my program, but at the moment they are drawn to the backbuffer. I need to draw them one the screen in away that i could make a user interface so that the images are infront of the 3D scene. below is my code for the 2dSurface class, what would i need to add/change to get the images to the front of the screen?
#include ".\2dSurface.h"

Surface::Surface(void)
{
	surface = NULL;
}

Surface::~Surface(void)	
{
	if( surface != NULL) 
	{
        surface->Release();
		surface = NULL;
	}
}

bool Surface::loadSurface(LPDIRECT3DDEVICE9 device, std::string filename)
{
	imageScale = 100; // Set the image scale to 100%

	HRESULT hResult;
	// Get the width and height of the image
	D3DXIMAGE_INFO imageInfo;
	hResult = D3DXGetImageInfoFromFile(filename.c_str(), &imageInfo);
	if FAILED(hResult)
	{
		return false;
	}
	// Record the width and height
	height = imageInfo.Height;
	width = imageInfo.Width;

	// Create the off screen surface
	hResult = device->CreateOffscreenPlainSurface(width, // Surface width
		height, // Surface height
		D3DFMT_A8R8G8B8, // Surface format  is a 32 bit format with 8 alpha bits
		D3DPOOL_DEFAULT, // Create it in the default memory pool
		&surface, // The pointer to the surface
		NULL
		);
	

	if (FAILED(hResult))
	{
		return false;
	}

	// Load the surface from a file
	hResult = D3DXLoadSurfaceFromFile(surface, // the surface just created
		NULL, // pallete entry, Null for non 256	colour formats
		NULL, // destination rect, Null for the whole surface
		filename.c_str(), // The file to load
		NULL, // Source rect, Null for the whole file
		D3DX_DEFAULT, // Filter
		0, // Colour key, colour that should be transparent
		NULL // Pointer to a D3DXIMAGE_INFO structure, for holding info about the image}
		);

	if (FAILED(hResult))
	{
		return false;
	}

	// Sets rects
	destRect.left = 0;
	destRect.top = 0;
	destRect.bottom = destRect.top + height;
	destRect.right = destRect.left + width;

	srcRect.left = 0;
	srcRect.top = 0;
	srcRect.bottom = destRect.top + height;
	srcRect.right = destRect.left + width;

	return true;
}

void Surface::render(LPDIRECT3DDEVICE9 pDevice)
{
	// Scale the destination based on current imageScale
	destRect.bottom = destRect.top + (int)(height * (imageScale/100));
	destRect.right = destRect.left + (int)(width * (imageScale/100));

	IDirect3DSurface9* backbuffer = NULL;
	pDevice->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO, &backbuffer);

	pDevice->StretchRect(surface, &srcRect, backbuffer, &destRect, D3DTEXF_NONE);
}

void Surface::setPosition(int x, int y)
{
	destRect.left = x;
	destRect.top = y;
	destRect.bottom = destRect.top + height;
	destRect.right = destRect.left + width;
}

void Surface::setSize(int percent)
{
	imageScale = percent;
}

void Surface::setSrcRect(int left, int top, int width, int height)
{
	srcRect.left = left;
	srcRect.top = top;
	srcRect.bottom = srcRect.top + height;
	srcRect.right = srcRect.left + width;
}

void Surface::setDestRect(int left, int top, int width, int height)
{
	destRect.left = left;
	destRect.top = top;
	destRect.bottom = destRect.top + height;
	destRect.right = destRect.left + width;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this