Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ DirectX resize
Picture
(Left: original image, right: ingame)
I got a problem with my sprites in my 2d game. I am new to directx9 and I want to make a 2d game with a tile map to learn how to make such games. The tiles work, they are all displayed in 48x48 as they used to be. But the "player" with the dummy icon (the ball) is resized to 58x58. The real size is 48x48. I did not change anything between the tiles and the player sprite. So I am asking you guys, how can I fix that? Also is this code alright (since I am new to directx9 but I still want it to be efficient)
#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
// include the Direct3D Library file
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
 // globals
HINSTANCE hInstance;								// The application window.
HWND hMainWnd;								// The application window.
LPDIRECT3D9       g_pDirect3D = NULL;
LPDIRECT3DDEVICE9 g_pDirect3D_Device = NULL;
D3DXVECTOR3 upos;
ID3DXSprite*		g_pSprite;				// The sprite instance
IDirect3DTexture9*	g_grass;				// Instance to hold the texture.
IDirect3DTexture9*	g_player;				// Instance to hold the texture.
IDirect3DTexture9*	g_sand;				// Instance to hold the texture.

int map[16][22] = {
                  {1,1,1,1,1,1,1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,1},
                  {1,1,1,1,1,1,1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,1},
                  {1,1,1,1,1,1,1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,1},
                  {1,1,1,1,1,1,1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,1},
                  {1,1,1,1,1,1,1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,1},
                  {1,1,1,1,1,1,1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,1},
                  {1,1,1,1,1,1,1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,1},
                  {1,1,1,1,1,1,1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,1},
                  {1,1,1,1,1,1,1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,1},
                  {1,1,1,1,1,1,1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,1},
                  {1,1,1,1,1,1,1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,1},
                  {1,1,1,1,1,1,1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,1},
                  {1,1,1,1,1,1,1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,1},
                  {1,1,1,1,1,1,1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,1},
                  {1,1,1,1,1,1,1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,1},
                  {1,1,1,1,1,1,1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,1}
					};
#define WINDOW_WIDTH	1024					// Prefered width of the app
#define WINDOW_HEIGHT	768					// Prefered height of the app

HRESULT Render();
HRESULT InitDirect3d();
HRESULT InitSprite();
HRESULT InitWindow();
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpCmdLine,int nShow)
{
		// TODO: Place code here.
	InitDirect3d();
	InitSprite();
 
	MSG msg;
	// Main message loop:
    ZeroMemory( &msg, sizeof(msg) );
    while( msg.message!=WM_QUIT )
    {
        if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
        {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
        else
            Render();
    }
 
	return msg.wParam;
 
	return 0;
}
HRESULT InitWindow()
{
	WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WndProc, 0L, 0L, 
                  GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
                  "TheWorld", NULL };
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    RegisterClassEx( &wc );
    hMainWnd = CreateWindow( "TheWorld", 
							  "TheWorld", 
                              WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX|WS_CLIPSIBLINGS|WS_VISIBLE, 
							  CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, WINDOW_HEIGHT,
                              GetDesktopWindow(), 
							  NULL, 
							  wc.hInstance, 
							  NULL );
 
	if(hMainWnd == NULL)
	{
		return(E_FAIL);
	}
 
	ShowWindow(hMainWnd, SW_SHOW);
	UpdateWindow(hMainWnd);
 
	return(S_OK);
 
}
HRESULT InitDirect3d()
{
	do
	{
		if(FAILED(InitWindow()))
		{
			break;
		}
		g_pDirect3D = Direct3DCreate9(D3D_SDK_VERSION); //erstellt das Fenster
		D3DPRESENT_PARAMETERS PresentParams;
		memset(&PresentParams, 0, sizeof(D3DPRESENT_PARAMETERS));
		PresentParams.Windowed = TRUE;
		PresentParams.SwapEffect = D3DSWAPEFFECT_DISCARD; //effizienteste methode
		PresentParams.hDeviceWindow = hMainWnd;    // set the window to be used by Direct3D
		PresentParams.BackBufferFormat = D3DFMT_X8R8G8B8;    // set the back buffer format to 32-bit
		PresentParams.BackBufferWidth = WINDOW_WIDTH;    // set the width of the buffer
		PresentParams.BackBufferHeight = WINDOW_HEIGHT;    // set the height of the buffer
		g_pDirect3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hMainWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING, &PresentParams,&g_pDirect3D_Device); //erzeuge das Fenster
		return S_OK;
	} while(false);
 
	return E_FAIL;
}
HRESULT InitSprite()
{
	HRESULT hr;
        hr = D3DXCreateTextureFromFile(g_pDirect3D_Device, "grass2.png",&g_grass);
        hr = D3DXCreateTextureFromFile(g_pDirect3D_Device, "sand.png",&g_sand);
        hr = D3DXCreateTextureFromFile(g_pDirect3D_Device, "player.png",&g_player);
	upos.x = 10;
	upos.y = 10;
 
	hr = D3DXCreateSprite(g_pDirect3D_Device,&g_pSprite); 
	return hr;
}
HRESULT Render()
{
 
	
    g_pDirect3D_Device->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );
 
	g_pDirect3D_Device->BeginScene();
	{
		g_pSprite->Begin(D3DXSPRITE_ALPHABLEND);
	for(int i=0;i<16;i++){
	for(int j=0;j<22;j++){
		 if(map[i][j] == 1){
			 g_pSprite->Draw(g_grass,NULL,NULL,&D3DXVECTOR3( (float)j*48, (float)i*48, 0 ),0xFFFFFFFF);
		 }
		 if(map[i][j] == 2){
			 g_pSprite->Draw(g_sand,NULL,NULL,&D3DXVECTOR3( (float)j*48, (float)i*48, 0 ),0xFFFFFFFF);
		 }
	}
	}
			g_pSprite->Draw(g_player,NULL,NULL,&D3DXVECTOR3( (float)0, (float)0, 0 ),0xFFFFFFFF);
			
		g_pSprite->End();
	}
	g_pDirect3D_Device->EndScene();
	g_pDirect3D_Device->Present( NULL, NULL, NULL, NULL );
	return S_OK;
}
 
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message) 
	{
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}
 
Posted 18-Sep-12 7:26am
brownii358

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

		g_pDirect3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &g_d3ddm);
		D3DPRESENT_PARAMETERS PresentParams;
		ZeroMemory( &PresentParams, sizeof(PresentParams) );
		PresentParams.Windowed = TRUE;
		PresentParams.SwapEffect = D3DSWAPEFFECT_DISCARD; //effizienteste methode
		PresentParams.hDeviceWindow = hMainWnd;    // set the window to be used by Direct3D
		PresentParams.BackBufferFormat = g_d3ddm.Format;    // set the back buffer format to 32-bit
		PresentParams.BackBufferWidth = WINDOW_WIDTH;    // set the width of the buffer
		PresentParams.BackBufferHeight = WINDOW_HEIGHT;    // set the height of the buffer
		PresentParams.BackBufferCount = 1;
 
hr =D3DXCreateTextureFromFileEx(  g_pDirect3D_Device,"../Sprites/player.png",48, 48,0,D3DPOOL_DEFAULT,D3DFMT_UNKNOWN,D3DPOOL_DEFAULT,D3DX_DEFAULT,D3DX_DEFAULT,D3DCOLOR_RGBA(255,255,255, 0),NULL, NULL,&g_player);
Fixed it for me.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 George Jonsson 175
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 19 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100