Jump to content
  • Advertisement
Sign in to follow this  
PuReInSaNe

Bug: Sprite is not drawn correctly

This topic is 1979 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

[attachment=13273:bug.png]

 


Bug that happens: the tiles are drawn 49x50 or 48x50 instead of 48x48. On the right side it's the actual texture which I attached, the left side is the bug I got from the code below.

 

Posted about this some time before, happend to me again somehow.
This time I made a new project and made an easy main loop so I don't have to post my whole game.

 

Please help me to fix it!

 

#ifndef _WINMAIN_H
#define _WINMAIN_H 1	

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

using namespace std;

// include the Direct3D Library file
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
#pragma comment (lib, "dinput8.lib")
#pragma comment (lib, "dxguid.lib")

#endif


HINSTANCE g_hInstance;
HWND g_hWnd;
HICON hMyIcon;
int width = 1056;
int height = 768;

LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
	g_hInstance = hInstance;

	WNDCLASSEX wc;
	wc.cbSize = sizeof(WNDCLASSEX); 

	//fill the struct with info
	wc.style		 = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc   = (WNDPROC)WinProc;
	wc.cbClsExtra	 = 0;
	wc.cbWndExtra	 = 0;
	wc.hInstance	 = hInstance;
	wc.hbrBackground = NULL;
	wc.lpszMenuName  = NULL;
	wc.lpszClassName = "winmain";
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	hMyIcon = LoadIcon(NULL, MAKEINTRESOURCE(IDI_WINLOGO));
	wc.hIconSm = hMyIcon;
	wc.hIcon = hMyIcon;

	RegisterClassEx(&wc); // calculate the size of the client area 
	HWND window;	
	RECT wr = {0, 0, width, height}; // set the size, but not the position 
	AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, TRUE); // adjust the size
	window = CreateWindowEx( 0,
	   "winmain",								 //window class
	   "test",								 //title bar
      WS_EX_TOPMOST|WS_POPUP|WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_CLIPSIBLINGS|WS_VISIBLE, 
	   100, 100,											 //x,y coordinate 
	   wr.right - wr.left, // width of the window 
	   wr.bottom - wr.top, // height of the window
	   0,											 //parent window
	   0,											 //menu
	   g_hInstance,									  //application instance
	   0);	
	//display the window


	ShowWindow(g_hWnd, SW_SHOW);
	UpdateWindow(g_hWnd);
	LPDIRECT3D9 p_d3d;
	LPDIRECT3DDEVICE9 p_device;
	p_d3d = NULL;
	p_device = NULL;
	p_d3d = Direct3DCreate9(D3D_SDK_VERSION);
	if (p_d3d == NULL) {
		return 0;
	}
	//get system desktop color depth
	D3DDISPLAYMODE dm;
	p_d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &dm);

	D3DPRESENT_PARAMETERS PresentParams;

	ZeroMemory( &PresentParams, sizeof(PresentParams) );
	PresentParams.Windowed = TRUE;
	PresentParams.SwapEffect = D3DSWAPEFFECT_DISCARD; //effizienteste methode
	PresentParams.hDeviceWindow = window;    // set the window to be used by Direct3D
	PresentParams.BackBufferFormat = D3DFMT_X8R8G8B8;    // set the back buffer format to 32-bit
	PresentParams.BackBufferWidth = width;    // set the width of the buffer
	PresentParams.BackBufferHeight = height;    // set the height of the buffer
	PresentParams.BackBufferCount = 1;

		//create Direct3D device
		p_d3d->CreateDevice(
			D3DADAPTER_DEFAULT,
			D3DDEVTYPE_HAL,
			window,
			D3DCREATE_HARDWARE_VERTEXPROCESSING,
			&PresentParams,
			&p_device);
		p_device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);

//Create sprite
		
		IDirect3DTexture9* texture;
		ID3DXSprite*		sprite;				// The sprite instance
		D3DXIMAGE_INFO info;
		//standard Windows return value
		HRESULT result;
		//create the new texture by loading a bitmap image file
		result = D3DXGetImageInfoFromFile("textures.png",&info);
		if (result != D3D_OK) 	{
		MessageBox(0, "Texture file not found", "Error!", 0);
			texture = NULL;
		}
		result = D3DXCreateTextureFromFileEx( 
			p_device, //Direct3D device object
			"textures.png",
			info.Width,            //bitmap image width
			info.Height,           //bitmap image height
			1,                     //mip-map levels (1 for no chain)
			D3DPOOL_DEFAULT,       //the type of surface (standard)
			D3DFMT_UNKNOWN,        //surface format (default)
			D3DPOOL_DEFAULT,       //memory class for the texture
			D3DX_DEFAULT,          //image filter
			D3DX_DEFAULT,          //mip filter
			D3DCOLOR_RGBA(255,255,255, 0),            //color key for transparency
			&info,                 //bitmap file info (from loaded file)
			NULL,                  //color palette
			&texture );            //destination texture

		//make sure the bitmap textre was loaded correctly
		if (result != D3D_OK) 	{
		MessageBox(0, "Texture not loaded correctly.", "Error!", 0);
			texture = NULL;
		}
		D3DXCreateSprite(p_device,&sprite);

	MSG msg;
    ZeroMemory( &msg, sizeof(msg) );
    while( msg.message!=WM_QUIT )
    {
		while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		// ------ MAP------
		p_device->BeginScene();
		sprite->Begin(D3DXSPRITE_ALPHABLEND);
		RECT rectangleg;
		rectangleg.right = 48;
		rectangleg.left = 0;
		rectangleg.bottom = 48;
		rectangleg.top = 0;
		RECT rectanglew;
		rectanglew.right = 96;
		rectanglew.left = 48;
		rectanglew.bottom = 48;
		rectanglew.top = 0;
		RECT rectangles;
		rectangles.right = 48;
		rectangles.left = 0;
		rectangles.bottom = 96;
		rectangles.top = 48;
		int y = 0;
		int x = 0;
		for(int i=0;i<16;i++){
			x = 0;
		for(int j=0;j<22;j++){
			if(j == 10||j == 11&&i != 3)
			{
				sprite->Draw(texture,&rectanglew,NULL,&D3DXVECTOR3( (float)x*48, (float)y*48, 0 ),D3DCOLOR_RGBA(255,255,255, 255)); }
			else if(j == 9&&i == 3||j == 11&&i == 3)
			{
				sprite->Draw(texture,&rectangles,NULL,&D3DXVECTOR3( (float)x*48, (float)y*48, 0 ),D3DCOLOR_RGBA(255,255,255, 255)); }
			else
			{
				sprite->Draw(texture,&rectangleg,NULL,&D3DXVECTOR3( (float)x*48, (float)y*48, 0 ),D3DCOLOR_RGBA(255,255,255, 255)); }
		x++;
		 }
		y++;
		}
		sprite->End();
		p_device->EndScene();
		p_device->Present(NULL, NULL, NULL, NULL);
		// ------ MAP Ende------
	}

   return 1;
}
LRESULT WINAPI WinProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
	switch( msg )
		{                
		 case WM_QUIT:
		 case WM_CLOSE:
		 case WM_DESTROY:
			PostQuitMessage(0);
			break; 

		}
	return DefWindowProc( hWnd, msg, wParam, lParam );
}

 

Share this post


Link to post
Share on other sites
Advertisement

Adding
 

				sprite->Draw(texture,&rectanglew,NULL,&D3DXVECTOR3( (float)(x*48)-0.5f, (float)(y*48)-0.5f, 0 ),D3DCOLOR_RGBA(255,255,255, 255)); }
			else if(j == 9&&i == 3||j == 11&&i == 3)
			{
				sprite->Draw(texture,&rectangles,NULL,&D3DXVECTOR3( (float)(x*48)-0.5f, (float)(y*48)-0.5f, 0 ),D3DCOLOR_RGBA(255,255,255, 255)); }
			else
			{
				sprite->Draw(texture,&rectangleg,NULL,&D3DXVECTOR3( (float)(x*48)-0.5f, (float)(y*48)-0.5f, 0 ),D3DCOLOR_RGBA(255,255,255, 255)); }

results in

ag4tjf3nl6y.png

 

now they have a small blurr too. blink.png

Share this post


Link to post
Share on other sites

make sure about the mapping, probably you are mapping it wrong

 

// ------ MAP------
p_device->BeginScene();
sprite->Begin(D3DXSPRITE_ALPHABLEND);
RECT rectangleg;
rectangleg.right = 48;
rectangleg.left = 0;
rectangleg.bottom = 48;
rectangleg.top = 0;
RECT rectanglew;
rectanglew.right = 96;
rectanglew.left = 48;
rectanglew.bottom = 48;
rectanglew.top = 0;
RECT rectangles;
rectangles.right = 48;
rectangles.left = 0;
rectangles.bottom = 96;
rectangles.top = 48;

Share this post


Link to post
Share on other sites

In the image you posted in original post, are you sure backbuffer was the right size? If it's smaller it could stretch everything when actually everything's fine.

Share this post


Link to post
Share on other sites
Right, the backbuffer had some problems.

I removed this:
PresentParams.BackBufferWidth = width; // set the width of the buffer
PresentParams.BackBufferHeight = height; // set the height of the buffer

Because of PresentParams.windowed = true; the BackBufferWidth and Height is set automatical.

Also I changed the width and height here:
window = CreateWindowEx( 0,"winmain",	//window class"test",	//title bar
WS_EX_TOPMOST|WS_POPUP|WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_CLIPSIBLINGS|WS_VISIBLE,100, 100,	//x,y coordinate
wr.right - wr.left, // width of the window
wr.bottom - wr.top, // height of the window
0,	//parent window
0,	//menu
g_hInstance,	//application instance
0);	//display the window

to the variables width, and height. Dunno why I made a rectangle there. Edited by PuReInSaNe

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!