Jump to content
  • Advertisement
Sign in to follow this  
Vostromus

Picture Comming Out Weird....

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

I am just starting to learn the basics of DX9 and I can use a little help... I have a .bmp file that is 64x128 pixels... I managed to chop it up into 8 32x32 blocks and display them accordingly to the screen using a 2D character array. I decided to add another tile to the map and resized the image to 96x128. I have modified my loops to consider the extra column. Now when I try to display my tiles they are cut short. Here is the code that cuts up and displays image:
 // clear the window to black
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

    d3ddev->BeginScene();    // begins the 3D scene

    d3dspt->Begin(D3DXSPRITE_ALPHABLEND);    // // begin sprite drawing with transparency

   
         //tile map
	char maparoo[5][10] ={
	{0,1,3,4,6,7,9,10,0,2},
	{0,1,3,4,6,7,9,10,0,2},
	{0,1,3,4,6,7,9,10,0,2},
	{0,1,3,4,6,7,9,10,0,2},
	{0,1,3,4,6,7,9,10,0,2}
	};

        RECT part[12]; //hold my tiles

	int y1;
	int y2;
	
	int x1;
	int x2;

	x1 = 0;
	y1 = 0;
	
	x2 = 32;
	y2 = 32;
	
	int frames = 0;

	for (int numRows = 0;numRows < 4;numRows++)
	{			
		for (int numCols = 0;numCols < 3;numCols++)
		{
			SetRect(&part[frames],x1,y1,x2,y2);
			x1 += 32;
			x2 += 32;
			frames +=1;
			
		}
		
		x1 = 0;
		x2 = 32;
		y1 +=32;
		y2 +=32;
		
	}
	
    D3DXVECTOR3 center(0.0f, 0.0f, 0.0f);    // center at the upper-left corner
     D3DXVECTOR3 position(50.0f, 50.0f, 0.0f); // position at 50, 50 with no depth   
	
	 for (int setBlox = 0;setBlox < 5; setBlox++)
	 {
	
		for (int setBlox2 = 0;setBlox2 < 10; setBlox2++)
		{
			char bloxNum = maparoo[setBlox][setBlox2];
			d3dspt->Draw(sprite, &part[bloxNum], ¢er, &position, D3DCOLOR_ARGB(255, 255, 255, 255));
			position.x += 50;
		}
		position.x = 50;
		position.y += 32;
	 }
	//d3dspt->Draw(sprite, &part[2], ¢er, &position, D3DCOLOR_ARGB(255, 255, 255, 255));
			
    d3dspt->End();    // end sprite drawing

    d3ddev->EndScene();    // ends the 3D scene

    d3ddev->Present(NULL, NULL, NULL, NULL);

Any help or resources to get this figured out would be greatly appriciated!!!

Share this post


Link to post
Share on other sites
Advertisement
Have you accounted for the fact that, for example, the tile on the left side of the second row will now be in array slot [3] instead of [2], since there are now 3 tiles per row?

That said, we can simplify the code dramatically:


d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
d3ddev->BeginScene(); // begins the 3D scene
d3dspt->Begin(D3DXSPRITE_ALPHABLEND); // // begin sprite drawing with transparency

char map[5][10] = { // No need to get cutesy.
{0,1,3,4,6,7,9,10,0,2},
{0,1,3,4,6,7,9,10,0,2},
{0,1,3,4,6,7,9,10,0,2},
{0,1,3,4,6,7,9,10,0,2},
{0,1,3,4,6,7,9,10,0,2}
};

// Use named constants.
int numRows = 4;
int numColumns = 3;
RECT part[numRows * numColumns];
// Instead of maintaining several variables that move "in step", use arithmetic
// expressions. Here, I've gotten rid of x1/y1/x2/y2 by multiplying within the
// loop.
// Note that '&x[y]' can be written more simply as 'x + y'. But we can take that
// a step further by tracking a pointer to the current RECT, instead of an
// index:

RECT* current = part;

// Don't call a counter variable numSomething. The counter is going to change
// each time; but the number of rows/columns doesn't.
// Notice how, with the way the constants are used here, if you expand the tile
// set again, you won't need to change the code here - just the num constants.
for (int row = 0; row < numRows; ++row) {
for (int column = 0; column < numColumns; ++column) {
SetRect(current++, column * 32, row * 32, (column + 1) * 32, (row + 1) * 32);
// Yep, really that simple. If you want (and I would definitely
// not fault you for it) you could separate out the increment
// of 'current'.
}
}

// Why were you spacing the blocks out 50 pixels horizontally when they are
// only 32 pixels wide?
D3DXVECTOR3 center(0.0f, 0.0f, 0.0f);

// Again, we can calculate the 'position' directly in the loop.
// Again, a name like 'setBlox' is misleading and hard to interpret here.
// We can reuse the 'row' and 'column' names just fine:
for (int row = 0; row < 5; ++row) {
for (int column = 0; column < 10; ++column) {
char blockType = map[row][column];
D3DXVECTOR3 position((column + 1) * 32.0f, (row + 1) * 32.0f, 0.0f);
d3dspt->Draw(sprite, part + blockType, &center, &position, D3DCOLOR_ARGB(255, 255, 255, 255));
}
}

d3dspt->End(); // end sprite drawing
d3ddev->EndScene(); // ends the 3D scene
d3ddev->Present(NULL, NULL, NULL, NULL);

Share this post


Link to post
Share on other sites
Zahlman

Thank you for your constructive critism. I will use them to help me wrap this into a class eventually.

I was able to follow and understand your simplification but the results were the same.

I had taken into consideration the shift of the tile numbers with the addition of another column.

The reason the blocks were being set 50 pixel apart was because I was trying to get a better since of what was going on.

I have also checked my image and my tiles are lined up correctly on there but when I run my/your code the left side of every tile is off by 10-15 pixels to the left.

Any other suggestions?

Share this post


Link to post
Share on other sites
I went and added another column to my image. I also updated my const values...Now my tiles are displaying correctly.

Can anyone help me understand why this is happening?

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!