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>
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
HINSTANCE hInstance; HWND hMainWnd; LPDIRECT3D9 g_pDirect3D = NULL;
LPDIRECT3DDEVICE9 g_pDirect3D_Device = NULL;
D3DXVECTOR3 upos;
ID3DXSprite* g_pSprite; IDirect3DTexture9* g_grass; IDirect3DTexture9* g_player; IDirect3DTexture9* g_sand;
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)
{
InitDirect3d();
InitSprite();
MSG msg;
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); D3DPRESENT_PARAMETERS PresentParams;
memset(&PresentParams, 0, sizeof(D3DPRESENT_PARAMETERS));
PresentParams.Windowed = TRUE;
PresentParams.SwapEffect = D3DSWAPEFFECT_DISCARD; PresentParams.hDeviceWindow = hMainWnd; PresentParams.BackBufferFormat = D3DFMT_X8R8G8B8; PresentParams.BackBufferWidth = WINDOW_WIDTH; PresentParams.BackBufferHeight = WINDOW_HEIGHT; g_pDirect3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hMainWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING, &PresentParams,&g_pDirect3D_Device); 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;
}