Sign in to follow this  
JeZ-l-Lee

PNG To Texture W/Original PNG's Width/Height?

Recommended Posts

Hi,

 

I just started to get into DirectX 2D game programming.

 

I can load a PNG image into a DirectX texture just fine,

but the resulting texture does not have the same width/height as the original PNG

when displayed on the screen?

 

Screenshot and code are below.

Any help would be appreciated, thanks!

 

JeZ+Lee

 

 

 

Here is the screen and also what the original PNG looks like side-by-side:

TC5-DirectX-PNG_To_Texture_Problem_01.pn

/*
 -----------------------------------------------------------------------------------------------------------------------------
|                                                          JeZ+Lee's                                                          |
|                                                                                                                             |
|                                                       P.C. Windows(R)                                                       |
|   ____  ___         _________          .__   .__ ___.                     ___________                 .__                   |
|   \   \/  /         \_   ___ \ _____   |  |  |__|\_ |__    ____  _______  \_   _____/  ____     ____  |__|  ____    ____ TM |
|    \     /   ______ /    \  \/ \__  \  |  |  |  | | __ \ _/ __ \ \_  __ \  |    __)_  /    \   / ___\ |  | /    \ _/ __ \   |
|    /     \  /_____/ \     \____ / __ \_|  |__|  | | \_\ \\  ___/  |  | \/  |        \|   |  \ / /_/  >|  ||   |  \\  ___/   |
|   /___/\  \          \______  /(____  /|____/|__| |___  / \___  > |__|    /_______  /|___|  / \___  / |__||___|  / \___  >  |
|         \_/                 \/      \/                \/      \/                  \/      \/ /_____/           \/      \/   |
|                                               2-Dimensional Video Game Engine                                               |
|                                                                                                                             |
|                             (C)opyright 2014, By Jesse "JeZ+Lee" Palser (JessePalser@GMail.com)                             |
| ----------------------------------------------------------------------------------------------------------------------------|
|                                                                                TM                                           |
|                                            "TetriCrisis 5 ''Firefox'' 110% A.I."                                            |
|                                                                                                                             |
|                                              (C)opyright 2014, By 16BitSoft(R)                                              |
|                                                       www.16BitSoft.com                                                     |
|                                                                                                                             |
 -----------------------------------------------------------------------------------------------------------------------------
*/

#define STRICT
#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>

HWND                    g_hWnd          = NULL;
LPDIRECT3D9             g_pD3D          = NULL;
LPDIRECT3DDEVICE9       g_pd3dDevice    = NULL;
LPDIRECT3DVERTEXBUFFER9 g_pVertexBuffer = NULL;
LPDIRECT3DTEXTURE9      g_pTexture      = NULL;

#define IDI_DIRECTX_ICON                107

#define D3DFVF_CUSTOMVERTEX ( D3DFVF_XYZ | D3DFVF_TEX1 )

struct Vertex
{
    float x, y, z;
    float tu, tv;
};

Vertex g_quadVertices[] =
{
	{-1.0f, 1.0f, 0.0f,  0.0f,0.0f },
	{ 1.0f, 1.0f, 0.0f,  1.0f,0.0f },
	{-1.0f,-1.0f, 0.0f,  0.0f,1.0f },
	{ 1.0f,-1.0f, 0.0f,  1.0f,1.0f }
};

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
				   LPSTR lpCmdLine, int nCmdShow);
LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
void loadTexture(void);
void init(void);
void shutDown(void);
void render(void);

//-------------------------------------------------------------------------------------------------------------------------------
void init( void )
{
    g_pD3D = Direct3DCreate9( D3D_SDK_VERSION );

    D3DDISPLAYMODE d3ddm;

    g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm );

    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory( &d3dpp, sizeof(d3dpp) );
	
    d3dpp.Windowed               = TRUE;
    d3dpp.SwapEffect             = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat       = d3ddm.Format;
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
    d3dpp.PresentationInterval   = D3DPRESENT_INTERVAL_IMMEDIATE;

    g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hWnd,
                          D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                          &d3dpp, &g_pd3dDevice );

	loadTexture();
	
	g_pd3dDevice->CreateVertexBuffer( 4*sizeof(Vertex), D3DUSAGE_WRITEONLY, 
                                      D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, 
                                      &g_pVertexBuffer, NULL );
    void *pVertices = NULL;

    g_pVertexBuffer->Lock( 0, sizeof(g_quadVertices), (void**)&pVertices, 0 );
    memcpy( pVertices, g_quadVertices, sizeof(g_quadVertices) );
    g_pVertexBuffer->Unlock();

    D3DXMATRIX matProj;
    D3DXMatrixPerspectiveFovLH( &matProj, D3DXToRadian( 45.0f ), 
                                800.0f / 480.0f, 0.1f, 100.0f );
    g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );

	g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
}

//-------------------------------------------------------------------------------------------------------------------------------
void loadTexture( void )
{
//	D3DXCreateTextureFromFile( g_pd3dDevice, L"TC5-Logo.png", &g_pTexture );
	D3DXCreateTextureFromFileEx(g_pd3dDevice, L"TC5-Logo.png",  D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT, 0,
		                        D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &g_pTexture);

	g_pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
}

//-------------------------------------------------------------------------------------------------------------------------------
void render( void )
{
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
                         D3DCOLOR_COLORVALUE(0.0f,0.0f,0.0f,1.0f), 1.0f, 0 );

    D3DXMATRIX matWorld;
    D3DXMatrixTranslation( &matWorld, 0.0f, 0.0f, 4.0f );
    g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );

    g_pd3dDevice->BeginScene();

    g_pd3dDevice->SetTexture( 0, g_pTexture );
    g_pd3dDevice->SetStreamSource( 0, g_pVertexBuffer, 0, sizeof(Vertex) );
	g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );

	g_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE);
	g_pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
	g_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );

	g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );

    g_pd3dDevice->EndScene();
    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}
//-------------------------------------------------------------------------------------------------------------------------------
LRESULT CALLBACK WindowProc( HWND   hWnd,  UINT   msg, WPARAM wParam, LPARAM lParam )
{
    switch( msg )
	{	
        case WM_KEYDOWN:
		{
			switch( wParam )
			{
				case VK_ESCAPE:
					PostQuitMessage(0);
					break;
			}
		}
        break;

		case WM_CLOSE:
		{
			PostQuitMessage(0);	
		}
		
        case WM_DESTROY:
		{
            PostQuitMessage(0);
		}
        break;

		default:
		{
			return DefWindowProc( hWnd, msg, wParam, lParam );
		}
		break;
	}

	return 0;
}

//-------------------------------------------------------------------------------------------------------------------------------
void shutDown( void )
{
    if( g_pTexture != NULL ) 
        g_pTexture->Release();

    if( g_pVertexBuffer != NULL ) 
        g_pVertexBuffer->Release(); 

    if( g_pd3dDevice != NULL )
        g_pd3dDevice->Release();

    if( g_pD3D != NULL )
        g_pD3D->Release();
}

//-------------------------------------------------------------------------------------------------------------------------------
int WINAPI WinMain(	HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
	WNDCLASSEX winClass; 
	MSG        uMsg;

    memset(&uMsg,0,sizeof(uMsg));
    
	winClass.lpszClassName = L"MY_WINDOWS_CLASS";
	winClass.cbSize        = sizeof(WNDCLASSEX);
	winClass.style         = CS_HREDRAW | CS_VREDRAW;
	winClass.lpfnWndProc   = WindowProc;
	winClass.hInstance     = hInstance;
	winClass.hIcon	       = LoadIcon(hInstance, (LPCTSTR)IDI_DIRECTX_ICON);
    winClass.hIconSm	   = LoadIcon(hInstance, (LPCTSTR)IDI_DIRECTX_ICON);
	winClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
	winClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	winClass.lpszMenuName  = NULL;
	winClass.cbClsExtra    = 0;
	winClass.cbWndExtra    = 0;

	if( !RegisterClassEx(&winClass) )
		return E_FAIL;

	g_hWnd = CreateWindowEx( NULL, L"MY_WINDOWS_CLASS", 
                             L"TetriCrisis 5 ''Firefox'' 110% A.I.",
						     WS_OVERLAPPEDWINDOW | WS_VISIBLE,
					         GetSystemMetrics(SM_CXSCREEN)/2-400,
							 GetSystemMetrics(SM_CYSCREEN)/2-240, 800, 480, NULL, NULL, hInstance, NULL );

	if( g_hWnd == NULL )
		return E_FAIL;

    ShowWindow( g_hWnd, nCmdShow );
    UpdateWindow( g_hWnd );

	init();

	while( uMsg.message != WM_QUIT )
	{
		if( PeekMessage( &uMsg, NULL, 0, 0, PM_REMOVE ) )
		{ 
			TranslateMessage( &uMsg );
			DispatchMessage( &uMsg );
		}
        else
		    render();
	}

	shutDown();

    UnregisterClass( L"MY_WINDOWS_CLASS", winClass.hInstance );

	return uMsg.wParam;
}

Share this post


Link to post
Share on other sites

The textures load fine. The quad you draw covers the entire screen, that's why the texture looks streched.

You need to call SetViewport() before drawing, with the same aspect ratio as the original image. Doesn't have to be same width/height - just the same aspect ratio.

Share this post


Link to post
Share on other sites

The textures load fine. The quad you draw covers the entire screen, that's why the texture looks streched.

You need to call SetViewport() before drawing, with the same aspect ratio as the original image. Doesn't have to be same width/height - just the same aspect ratio.

Hi,

 

Thank you for the reply to my question!

 

I tried to add "SetViewport()" but am not getting the desired result?

See screenshots and code below, thanks in advance for the assistance...

 

JeZ+Lee

 

 

 

TC5-DirectX-PNG_To_Texture_Problem_02.pn

//-------------------------------------------------------------------------------------------------------------------------------
void render( void )
{
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
                         D3DCOLOR_COLORVALUE(0.0f,0.0f,0.0f,1.0f), 1.0f, 0 );

	D3DSURFACE_DESC textureInfo;
	g_pTexture->GetLevelDesc(0, &textureInfo);
    D3DVIEWPORT9 ViewPort;
    ViewPort.X      = 0;
    ViewPort.Y      = 0;
    ViewPort.Width  = textureInfo.Width;
    ViewPort.Height = textureInfo.Height;
    ViewPort.MinZ   = 0.0f;
    ViewPort.MaxZ   = 1.0f;
    g_pd3dDevice->SetViewport( &ViewPort );

    D3DXMATRIX matWorld;
    D3DXMatrixTranslation( &matWorld, 0.0f, 0.0f, 4.0f );
    g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );

    g_pd3dDevice->BeginScene();

    g_pd3dDevice->SetTexture( 0, g_pTexture );
    g_pd3dDevice->SetStreamSource( 0, g_pVertexBuffer, 0, sizeof(Vertex) );
	g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );

	g_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE);
	g_pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
	g_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );

	g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );

    g_pd3dDevice->EndScene();
    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}

//-------------------------------------------------------------------------------------------------------------------------------

Share this post


Link to post
Share on other sites

use AdjustWindowRectEx

RECT rc;
SetRect(&rc, 0, 0, gw.Width, gw.Height);
DWORD grfStyle = gw.Windowed ? WS_OVERLAPPEDWINDOW&~WS_THICKFRAME&~WS_MAXIMIZEBOX : WS_EX_TOPMOST|WS_POPUP;
AdjustWindowRectEx(&rc, grfStyle, FALSE, NULL);
CreateWindowEx(grfStyle,...........................................);

Share this post


Link to post
Share on other sites

 

use AdjustWindowRectEx

RECT rc;
SetRect(&rc, 0, 0, gw.Width, gw.Height);
DWORD grfStyle = gw.Windowed ? WS_OVERLAPPEDWINDOW&~WS_THICKFRAME&~WS_MAXIMIZEBOX : WS_EX_TOPMOST|WS_POPUP;
AdjustWindowRectEx(&rc, grfStyle, FALSE, NULL);
CreateWindowEx(grfStyle,...........................................);

Hi,

 

I appreciate your reply, but I can't use the code because I don't know what "gw" is.

What is "gw" in the above code?

Thanks!

 

JeZ+Lee

Share this post


Link to post
Share on other sites

gw is just a struct that holds the stuff you put in those functions.

 

Here is a variant of what I use:

	RECT rc;
	SetRect(&rc, 0, 0, Width, Height);
	DWORD grfStyle = !Fullscreen ? WS_OVERLAPPEDWINDOW&~WS_THICKFRAME&~WS_MAXIMIZEBOX : WS_EX_TOPMOST|WS_POPUP;
	AdjustWindowRectEx(&rc, grfStyle, FALSE, NULL);

	hWnd = CreateWindowExA(NULL,
		std::string("WindowClass").c_str(),
		std::string("Title").c_str(),
		grfStyle,
		20, 20,
		!Fullscreen ? rc.right-rc.left : Width,
		!Fullscreen ? rc.bottom-rc.top : Height,
		NULL,
		NULL,
		hInstance,
		NULL);

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