Jump to content
  • Advertisement
Sign in to follow this  

[DirectX] Image problems, gets auto resized?

This topic is 3570 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
	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,
                          /*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

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

	 //Initiate font 
                   DEFAULT_PITCH | FF_DONTCARE,

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


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


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

void GraphicsHandler::cleanD3D(void)
	// Release all Direct3D related things


int GraphicsHandler::loadSprite(int buffer, LPCTSTR path, int width, int height)
	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;

	return 0;

int GraphicsHandler::drawSprite(int buffer, int xpos, int ypos)
	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

	d3ddev->SetRenderState( D3DRS_ALPHABLENDENABLE, true );
	d3ddev->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );

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

    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;

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

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

		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);

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
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

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

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!