• Advertisement
Sign in to follow this  

[DirectX] Image problems, gets auto resized?

This topic is 3392 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

Hello everyone! Sadly I've come across another problem which I thought I had solved before, but apparently it wasn't completely fixed, or at least what I found wasn't the answer. ; ; As shown on the image below; http://i41.tinypic.com/39b0o.png I have two images. One is 272x88, and my project's resolution is 272x272. Now, this image is a regular PNG, and when I draw it to 0,184(it should cover the whole bottom of the screen) it appears very wack, as if it was resized. (right side of the picture above) So I picked the original picture, and resized it to 50% smaller, so now I also have the same pic but with 136,44 dimensions. When I draw that pic to the same position, it's width is almost filling up the screen, while it's only supposed to be as big as 50%(width) of the screen! I'm not sure what's going wrong but perhaps I'm doing something wrong? (Pretty sure of this one..) ; And here's how I create my main screen:
int CoreServices::init(HINSTANCE hInstance, int nCmdShow, LPCTSTR windowCName, LPCTSTR appTitle, int wXpos, int wYpos, int wWidth, int wHeight)
{
	
    //Clear out the window class for use
    ZeroMemory(&wc, sizeof(WNDCLASSEX));

    //Fill in the struct with the needed information
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = (WNDPROC)WindowProc;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wc.lpszClassName = windowCName;

    //Register the window class
    RegisterClassEx(&wc);
	
	RECT rc;
	rc.top = 0;
	rc.left = 0;
	rc.right = wWidth;//250;
	rc.bottom = wHeight;//250;
	AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE); // Or whatever your window style is
	int nWindowWidth = rc.right - rc.left;
	int nWindowHeight = rc.bottom - rc.top;

    //Create the window and use the result as the handle
    hWnd = CreateWindowEx(NULL,
                          windowCName,
                          appTitle,
                          WS_OVERLAPPEDWINDOW,
                          /*wXpos, wYpos, wWidth, wHeight,*/
						  0, 0, nWindowWidth, nWindowHeight,
                          NULL, NULL, hInstance, NULL);

	//Display the window on the screen
    ShowWindow(hWnd, nCmdShow);

	return 0;
}




And here's how I init, draw and handle the whole Direct3D stuff (graphicshandler.cpp):
void GraphicsHandler::initD3D(HWND hWnd)
{
    d3d = Direct3DCreate9(D3D_SDK_VERSION);		//Create the Direct3D interface

    D3DPRESENT_PARAMETERS d3dpp;				//Create a struct to hold various device information

    ZeroMemory(&d3dpp, sizeof(d3dpp));			//Clear out the struct for use
    d3dpp.Windowed = TRUE;						//Program windowed, not fullscreen
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;	//Discard old frames
    d3dpp.hDeviceWindow = hWnd;					//Set the window to be used by Direct3D

    //Create a device class using this information and information from the d3dpp stuct
    d3d->CreateDevice(D3DADAPTER_DEFAULT,
                      D3DDEVTYPE_HAL,
                      hWnd,
                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                      &d3dpp,
                      &d3ddev);

	 D3DXCreateSprite(d3ddev, &d3dspt);			//Create the Direct3D Sprite object


	 //Initiate font 
	 D3DXCreateFont(d3ddev,
                   30,
                   0,
                   FW_NORMAL,
                   1,
                   true,
                   DEFAULT_CHARSET,
                   OUT_DEFAULT_PRECIS,
                   DEFAULT_QUALITY,
                   DEFAULT_PITCH | FF_DONTCARE,
                   L"Arial",
                   &dxfont);
    return;
}

void GraphicsHandler::clear_screen(void)
{
	//Lets clear the window First
	d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0);
}

void GraphicsHandler::begin_drawscene(void)
{
    d3ddev->BeginScene();						// Begins the 3D scene

    return;
}

void GraphicsHandler::begin_imagedrawing(void)
{
	d3dspt->Begin(NULL);						// Begin the sprite drawing

	return;
}

void GraphicsHandler::end_imagedrawing(void)
{
	d3dspt->End();								// End the sprite drawing
}


void GraphicsHandler::end_drawscene(/*void*/ int posx, int posy)
{
    d3ddev->EndScene();							// End the 3D scene

	/*RECT cliprect;
	cliprect.top = (posx * 16);
	cliprect.left = (posy * 16);
	cliprect.right = 256;
	cliprect.bottom = 160;

	RECT screenrect;
	screenrect.top = 0;
	screenrect.left = 0;
	screenrect.right = 256;
	screenrect.bottom = 160;*/
	

    d3ddev->Present(NULL, NULL, NULL, NULL);	// Refreshes/Shows the screen
	return;
}



void GraphicsHandler::cleanD3D(void)
{
	// Release all Direct3D related things
    d3ddev->Release();
    d3d->Release();

    return;
}


int GraphicsHandler::loadSprite(int buffer, LPCTSTR path, int width, int height)
{
	--buffer;
	if(buffer != images.size()) //Check whether the given buffer is valid or not
	{
		MessageBox(NULL, L"GraphicsHandler :: Wrong buffer ID given!", L"GraphicsHandler :: Fail", MB_OK);
		return -1;
	}
	imageData data;

	//MessageBox(NULL, path, L"Opening file from path:", MB_OK);

	//D3DXCreateTextureFromFile(d3ddev, path, &data.sprite);

	D3DXCreateTextureFromFileEx(d3ddev, //Our D3D Device
                                  path,    //Filename of our texture
                                  D3DX_DEFAULT, //Width:D3DX_DEFAULT = Take from file 
                                  D3DX_DEFAULT, //Height:D3DX_DEFAULT = Take from file
                                  1,            //MipLevels
                                  0,            //Usage, Is this to be used as a Render Target? 0 == No
                                  D3DFMT_A8R8G8B8, //32-bit with Alpha, everything should support this
                                  D3DPOOL_MANAGED,//Pool, let D3D Manage our memory
                                  D3DX_DEFAULT, //Filter:Default filtering
                                  D3DX_DEFAULT, //MipFilter, used for filtering mipmaps
                                  0,            //Disable ColourKey
                                  NULL,         //SourceInfo, returns extra info if we want it (we don't)
                                  NULL,         //Palette:We're not using one
                                  &data.sprite);  // Our texture goes here.



	data.rect.top = 0;
	data.rect.left = 0;
	data.rect.right = width;
	data.rect.bottom = height;

	if(data.sprite == 0)
	{
		MessageBox(NULL, L"GraphicsHandler :: Sprite is empty!", L"GraphicsHandler :: Load Fail", MB_OK);
		return -1;
	}
	images.push_back(data);

	return 0;
}


int GraphicsHandler::drawSprite(int buffer, int xpos, int ypos)
{
	--buffer;
	if(buffer >= images.size()) //Check whether the given buffer is valid or not
	{
		MessageBox(NULL, L"GraphicsHandler :: Wrong buffer ID given!", L"GraphicsHandler :: Fail", MB_OK);
		return -1;
	}
	imageData &data = images[buffer];

    // Draw the sprite
    D3DXVECTOR3 center(0.0f, 0.0f, 0.0f);		// Center at the upper-left corner
	D3DXVECTOR3 position(xpos, ypos, 0.0f);		// Position the image to the given paramenters
	



	//pseudo
	d3ddev->SetRenderState( D3DRS_ALPHABLENDENABLE, true );
	d3ddev->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
	d3ddev->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );

	d3ddev->SetTextureStageState( 0, D3DTSS_ALPHAARG1,D3DTA_TEXTURE );
	//pseudo




    d3dspt->Draw(data.sprite, /*&data.rect*/NULL, &center, &position, D3DCOLOR_XRGB(255, 255, 255)); // Draw
    
	return 0;
}

Edit: Thought I may as well include the graphicshandler.h as well;
#ifndef GRAPHICSHANDLER_H
#define GRAPHICSHANDLER_H

#include "globals.h"
#include "core.h"

class GraphicsHandler
{
	private:
		struct imageData
		{
			LPDIRECT3DTEXTURE9 sprite; //Using vector for storing images, never failed me
			RECT rect;
		};
		std::vector<imageData> images;

	public:
		void initD3D(HWND hWnd);
		void clear_screen(void);
		void begin_drawscene(void);
		void begin_imagedrawing(void);
		void end_imagedrawing(void);
		void end_drawscene(/*void*/ int posx, int posy);
		void cleanD3D(void);

		int loadSprite(int buffer, LPCTSTR path, int width, int height);
		int drawSprite(int buffer, int xpos, int ypos);
		int rotateSprite(int buffer, float rotate, float xpos, float ypos, float xcenter, float ycenter);
};
#endif


Thanks in advance! ~J P.S. If you're reading this, CandleJack and Steve00000, I didn't reply back in the topic (http://www.gamedev.net/community/forums/topic.asp?topic_id=520014) but fixed the problem, in a way/kinda. But still want to thank you for the help! (Didn't wanna bump a topic which was on page 3 just to say thanks, tends to tick some people off sometimes, so.. Thank you anyway! ^^ )

Share this post


Link to post
Share on other sites
Advertisement
Change the width/height in your load texture call to D3DX_DEFAULT_NONPOW2.

Pretty much all cards will load a non-pow2 texture, but many cards have restrictions on the use of non-pow2 textures (look at D3DPTEXTURECAPS_NONPOW2CONDITIONAL in the D3DCAPS9 page in the help).

Share this post


Link to post
Share on other sites
Hey!

Wow thanks, this worked like a charm!
I was warned not to use the NONPOW2 but I guess it's good for some purpose?

I will read on this further for sure.

Thank you very much! :)

Share this post


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

  • Advertisement