• Advertisement
Sign in to follow this  

Strange Problem with Sprites

This topic is 3006 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi again, I'm having a problem with the game I'm working on. The window is 1280x1024, and the background image is the same size, but when I draw the image to the window (using DirectX and LPD3DXSPRITE), about half of the image fills the entire screen. I can't understand why this is happening, because the image and the window are the same size. This only happens on other computers than the one I'm developing it on. With my computer, everything fits properly. The only things I can think of for that is my resolution is 1920x1080 and I have a different graphics card than the other computers. I've spent hours trying to fix this problem, but I can't figure it out. Here is the code: game.cpp
#include "Main.h"

Game::Game()
{
	mWindowHandle = NULL;
	mInstance = NULL;
	mD3d = NULL;
	mDevice = NULL;
	mMenuFont = NULL;
	mInGameFont = NULL;
	mSprite = NULL;
	mTitleFont = NULL;
	mInfoFont = NULL;
	mScreenWidth = 1280;
	mScreenHeight = 1024;
	mFullscreen = false;
	D3DXMatrixIdentity(&mViewMat);

	mAspectRatio = getAspectRatio();

	mGameState = LoadMainMenu;
}

bool Game::initD3D()
{
	mD3d = Direct3DCreate9(D3D_SDK_VERSION);
	if (!mD3d)
	{
		MessageBox(NULL, L"Error creating mD3d", L"Direct3DCreate9 Failed", MB_OK);
		return false;
	}

	D3DFORMAT backBufferFormat;
	D3DDISPLAYMODE mode;
	D3DCAPS9 caps;

	if (FAILED(mD3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &mode)))
	{
		MessageBox(mWindowHandle, L"GetAdapterDisplayMode Failed", L"GetAdapterDisplayMode Failed", MB_OK);
		return false;
	}

	backBufferFormat = mode.Format;

	if (FAILED(mD3d->CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, mode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, backBufferFormat)))
	{
		MessageBox(mWindowHandle, L"CheckDeviceFormat Failed", L"CheckDeviceFormat Failed", MB_OK);
		return false;
	}

	if (FAILED(mD3d->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps)))
	{
		MessageBox(mWindowHandle, L"GetDeviceCaps Failed", L"GetDeviceCaps Failed", MB_OK);
		return false;
	}

	ZeroMemory(&mD3dpp, sizeof(mD3dpp));

	mD3dpp.Windowed = !mFullscreen;
	mD3dpp.BackBufferCount = 1;
	mD3dpp.BackBufferWidth = mScreenWidth;
	mD3dpp.BackBufferHeight = mScreenHeight;
	mD3dpp.BackBufferFormat = backBufferFormat;
	mD3dpp.hDeviceWindow = mWindowHandle;
	mD3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	mD3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
	mD3dpp.EnableAutoDepthStencil = true;
	mD3dpp.AutoDepthStencilFormat = D3DFMT_D24X8;

	UINT adapter = D3DADAPTER_DEFAULT;

	std::string devType = "";

	if (FAILED(mD3d->CreateDevice(adapter, D3DDEVTYPE_HAL, mWindowHandle, D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &mD3dpp, &mDevice))) // pure device
	{
		if (FAILED(mD3d->CreateDevice(adapter, D3DDEVTYPE_HAL, mWindowHandle, D3DCREATE_HARDWARE_VERTEXPROCESSING, &mD3dpp, &mDevice))) // hardware device
		{
			if (FAILED(mD3d->CreateDevice(adapter, D3DDEVTYPE_HAL, mWindowHandle, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &mD3dpp, &mDevice))) // software vertex processing, hardware device
			{
				if (FAILED(mD3d->CreateDevice(adapter, D3DDEVTYPE_REF, mWindowHandle, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &mD3dpp, &mDevice))) // reference rasterizer
				{
					return false;
				}
				else
				{
					devType = "Reference Rasterizer";
				}
			}
			else
			{
				devType = "Software Vertex Processing - Hardware";
			}
		}
		else
		{
			devType = "Hardware";
		}
	}
	else
	{
		devType = "Pure";
	}

	ofstream outFile("Info.txt");

	outFile << "Width: " << mScreenWidth << "\tHeight: " << mScreenHeight << endl;
	outFile << devType << " device" << endl;

	outFile.close();

	if (FAILED(D3DXCreateSprite(mDevice, &mSprite)))
	{
		MessageBox(mWindowHandle, L"Error creating sprite", L"D3DXCreateSprite() Failed", MB_OK);
		return false;
	}

	avi.Open(mDevice, L"./Assets/Video/test.avi");

	return true;
}

void Game::advanceFrame()
{
	if (FAILED(mDevice->TestCooperativeLevel()))
	{
		MessageBox(mWindowHandle, L"TestCooperativeLevel() Failed", L"TestCooperativeLevel() Failed", MB_OK);
		return;
	}

	mDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

	mDevice->BeginScene();

	GetCursorPos(&mMousePos);
	ScreenToClient(mWindowHandle, &mMousePos);

	if (mGameState == DrawMainMenu)
	{
		drawMainMenu();
	}
	else if (mGameState == ViewHowToPlay)
	{
		drawHowToPlay();
	}
	else if (mGameState == ViewCredits)
	{
		drawCredits();
	}
	else if (mGameState == ViewLinks)
	{
		drawLinks();
	}
	else if (mGameState == LoadGame)
	{
		drawLoadingScreen();
		loadGame();
	}
	else if (mGameState == PlayIntro)
	{
		playIntro();
	}
	else if (mGameState == Playing)
	{
		play();
	}

	mTimer.Tick();

	drawGameInfo();

	mDevice->EndScene();

	mDevice->Present(NULL, NULL, NULL, NULL);
}

void Game::loadMainMenu()
{
	if (FAILED(D3DXCreateFont(mDevice, 46, 0, FW_NORMAL, 1, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FW_BOLD, L"Impact", &mMenuFont)))
	{
		MessageBox(mWindowHandle, L"Error creating menu font", L"D3DXCreateFont() Failed", MB_OK);
		return;
	}

	if (FAILED(D3DXCreateFont(mDevice, 60, 0, FW_NORMAL, 1, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FW_BOLD, L"Impact", &mTitleFont)))
	{
		MessageBox(mWindowHandle, L"Error creating title font", L"D3DXCreateFont() Failed", MB_OK);
		return;
	}

	if (FAILED(D3DXCreateFont(mDevice, 24, 0, FW_NORMAL, 1, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FW_BOLD, L"Impact", &mInfoFont)))
	{
		MessageBox(mWindowHandle, L"Error creating info font", L"D3DXCreateFont() Failed", MB_OK);
		return;
	}

	mUI[TITLE_SCREEN].create(mDevice, L"./Assets/Textures/Title Screen.bmp", 1280.0f, 1024.0f);
	mUI[TITLE_SCREEN].setFrameWidth(1280);
	mUI[TITLE_SCREEN].setFrameHeight(1024);

	for (int i = 0; i < 3; i++)
	{
		mTitleScreenFire.create(mDevice, L"./Assets/Textures/Fire Sheet.dds", 1848, 168);
		mTitleScreenFire.createAnimatedSprite(true);
		mTitleScreenFire.setNumberOfFrames(10);
		mTitleScreenFire.setFrameWidth(168);
		mTitleScreenFire.setFrameHeight(168);
	}

	mTitleScreenFire[0].scale(0.5f, 0.5f);
	D3DXVECTOR3 firePos(500.0f, 325.0f, 0.0f);
	mTitleScreenFire[0].setPos(firePos);

	firePos.x = 568.0f;
	firePos.y = 433.0f;
	mTitleScreenFire[1].setPos(firePos);
	mTitleScreenFire[1].setFrame(2);

	firePos.x = 1054.0f;
	firePos.y = 375.0f;
	mTitleScreenFire[2].setPos(firePos);
	mTitleScreenFire[2].setFrame(5);

	initAudio();

	mGameState = DrawMainMenu;
}

void Game::loadGame()
{
	if (FAILED(D3DXCreateFont(mDevice, 20, 0, FW_NORMAL, 1, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FW_BOLD, L"Times New Roman", &mInGameFont)))
	{
		MessageBox(mWindowHandle, L"Error creating in-game font", L"D3DXCreateFont() Failed", MB_OK);
		return;
	}

	D3DXVECTOR3 backgroundPos(0.0f, 0.0f, 0.0f);

	for (int i = 0; i < 6; i++)
	{
		mBackground.create(mDevice, L"./Assets/Textures/Background.bmp", 2000.0f, 2000.0f);
		mBackground.setFrameWidth(2000);
		mBackground.setFrameHeight(2000);
		mBackground.setPos(backgroundPos);
		backgroundPos.x += 2000.0f;
		if (i == 2)
		{
			backgroundPos.x = 0.0f;
			backgroundPos.y -= 2000.0f;
		}
	}

	D3DXVECTOR3 tentPos(0.0f, 0.0f, 0.0f);
	for (int i = 0; i < 8; i++)
	{
		mTents.create(mDevice, L"./Assets/Textures/Tent.dds");
		mTents.setFrameWidth(270);
		mTents.setFrameHeight(255);
		mTents.setPos(tentPos);
	}

	for (int i = 0; i < 8; i++)
	{
		mHuts.create(mDevice, L"./Assets/Textures/Hut.dds", 153.0f, 185.0f);
		mHuts.setFrameWidth(153);
		mHuts.setFrameHeight(185);
	}

	D3DXVECTOR3 hutPos(732.0f, 708.0f, 0.0f);
	mHuts[0].setPos(hutPos);
	hutPos = D3DXVECTOR3(422.0f, 1005.0f, 0.0f);
	mHuts[1].setPos(hutPos);
	hutPos = D3DXVECTOR3(165.0f, 1372.0f, 0.0f);
	mHuts[2].setPos(hutPos);
	hutPos = D3DXVECTOR3(296.0f, 1758.0f, 0.0f);
	mHuts[3].setPos(hutPos);
	hutPos = D3DXVECTOR3(684.0f, 1503.0f, 0.0f);
	mHuts[4].setPos(hutPos);
	hutPos = D3DXVECTOR3(920.0f, 1091.0f, 0.0f);
	mHuts[5].setPos(hutPos);
	hutPos = D3DXVECTOR3(1205.0f, 1370.0f, 0.0f);
	mHuts[6].setPos(hutPos);
	hutPos = D3DXVECTOR3(1148.0f, 1744.0f, 0.0f);
	mHuts[7].setPos(hutPos);

	mGameState = PlayIntro;
}


Sprite.cpp:
#include "Main.h"

Sprite::Sprite()
{
	mIsStatic = false;
	mPos = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	mTexture = NULL;
	mWidth = 0.0f;
	mHeight = 0.0f;
	mFrameWidth = 0;
	mFrameHeight = 0;
	mNumFrames = 1;
	mCurrentFrame = 0.0f;
	mAnimated = false;
	mScaleX = 1.0f;
	mScaleY = 1.0f;
}

void Sprite::create(LPDIRECT3DDEVICE9 device, LPCTSTR file, float width, float height)
{
	mWidth = width;
	mHeight = height;

	if (FAILED(D3DXCreateTextureFromFileEx(device, file, (UINT)width, (UINT)height, D3DX_DEFAULT, NULL, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_DEFAULT,
		D3DX_DEFAULT, D3DCOLOR_XRGB(255, 0, 255), NULL, NULL, &mTexture)))
	{
		MessageBox(NULL, L"Error creating Sprite texture", L"Sprite Texture Creation Failed", MB_OK);
		return;
	}
}

void Sprite::draw(LPD3DXSPRITE dxSprite, const D3DXMATRIX& view)
{
	RECT src = {(int)mCurrentFrame * mFrameWidth, 0, (int)mCurrentFrame * mFrameWidth + mFrameWidth, mFrameHeight};

	D3DXMATRIX scaling, translation, combined;
	D3DXMatrixScaling(&scaling, mScaleX, mScaleY, 0.0f);
	D3DXMatrixTranslation(&translation, mPos.x, mPos.y, mPos.z);

	if (view)
		combined = view * scaling * translation;
	else
		combined = scaling * translation;

	D3DXVECTOR3 center(0.0f, 0.0f, 0.0f);

	dxSprite->SetTransform(&combined);
	dxSprite->Begin(D3DXSPRITE_ALPHABLEND);
	dxSprite->Draw(mTexture, &src, ¢er, NULL, D3DCOLOR_ARGB(255, 255, 255, 255));
	dxSprite->End();
}

void Sprite::update(float dt, float update)
{
	if (mAnimated)
	{
		mCurrentFrame += update * dt;
		if ((int)mCurrentFrame > mNumFrames)
			mCurrentFrame = 0.0f;
	}
}

void Sprite::scale(float x, float y)
{
	mScaleX = x;
	mScaleY = y;
}


Also, some of my other sprites are not drawing fully. I set the frame width and height to the width and height of the image so that the whole image would be drawn, but only about 2/3 is being drawn. The rest is cut off. I have to set the width and height to a much higher number to draw the whole image. Example: the tent image is 200x165, but in the code I have to set it to 270x255 to draw the whole image. I can't post any screenshots right now because the computers I was testing it on are at school, but I'll post them tomorrow. Tell me if I need to post more code or if I need to clarify the problem. Any help would be appreciated. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Have you tried making your textures square, and making the sizes a power of two? Older hardware, such as one might find at a school for instance, will frequently have trouble otherwise.

Share this post


Link to post
Share on other sites
Thanks for the reply.

No, I haven't tried square textures yet, but I definitely will as soon as I can.

Also, to expand a little on the hardware, my computer has an ATI Radeon HD 4850 graphics card, and the the school computers use onboard video and the Intel Q35 Chipset (or something similar).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement