在DirectX9中调整了Sprite的大小 [英] Sprite is resized in DirectX9

查看:146
本文介绍了在DirectX9中调整了Sprite的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

图片
(左:原始图像,右:游戏中)
我在2D游戏中的精灵出现问题.我是Directx9的新手,我想制作一个带有图块贴图的2d游戏,以学习如何制作此类游戏.磁贴正常工作,它们全部以48x48的分辨率显示.但是带有虚拟图标(球)的玩家"已调整为58x58.实际尺寸为48x48.我没有在磁贴和播放器精灵之间进行任何更改.所以我问你们,我该如何解决?这段代码也没问题(因为我是Directx9的新手,但我仍然希望它高效)

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

推荐答案

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.


这篇关于在DirectX9中调整了Sprite的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆