Texture Flipping, Tiling, and Class Deletion

Started by
6 comments, last by jrmiller84 18 years ago
Alrigght, so I've got three questions. I'll start with my texture questions. I am developing a 2d scroller and I'm about 4/5 done with it but there's a problem I've been avoiding for a while (denial maybe? hah) that I need to take care of. Ok, so I have a texture sheet that I've put together that works fine but the problem is that all the textures face right. So when my character is moving left, the animation itself is running right! So I did a little research and the only thing I could find is this line of code that apparently mirrors a texture
g_pDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_MIRROR);
Problem is, I'm using DX8, not DX9 and apparently this is a DX9 method. I can't seem to find the equivelent DX8 method if there is one. I am not opposed to just making my texture sheet larger and mirroring the textures for the different directions. I don't particularly like that idea as it is not very elegant and is extremely time consuming. Next question is quick and to the point. Is there a way to tile textures on one polygon? For example, I have a texture I am using on the floor of one of my levels that I would like to tile and not stretch across the entire floor of my level, nor would I like to create a couple hundred proportional polygons to line across the bottom so that it creates a pattern. Is there a way around this, I'm guessing there must be but I can't find anything on it. Final problem and maybe a small bit more involved. I am keeping track of my objects within a linked list and looping through them to render them as well as check for collisions, etc. I have found a way to delete items off of the list using this bit of code:
	for(std::list<CCharacter>::iterator iter = Global.begin(); iter != Global.end(); ++iter) {

		if(iter->GetCharacterType() == PROJECTILE) {

			if(iter->GetPosition(X_AXIS) > winWidth || (iter->GetPosition(X_AXIS) + iter->GetDimension(WIDTH)) < 0.0f) {

				iter = Global.erase(iter);
				--iter;

			}

		}

		iter->IncrementFrameCt();
		PrimitiveInterface->SetVertexAndRender(*iter);

	}


Does the erase method of the linked list actually destroy the class as well? As you can see, that bit of code is controlling the destruction of projectiles that go off of the screen and I would hate to have a few hundred active classes after they have actually gone off of the screen. I couldn't find a definitive "Yes, this destroys the class pointed to" anywhere so I am curious. Thanks everyone!
I will forever be a student.
Advertisement
Quote:Original post by jrmiller84
Alrigght, so I've got three questions. I'll start with my texture questions. I am developing a 2d scroller and I'm about 4/5 done with it but there's a problem I've been avoiding for a while (denial maybe? hah) that I need to take care of. Ok, so I have a texture sheet that I've put together that works fine but the problem is that all the textures face right. So when my character is moving left, the animation itself is running right! So I did a little research and the only thing I could find is this line of code that apparently mirrors a texture

g_pDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_MIRROR);


Problem is, I'm using DX8, not DX9 and apparently this is a DX9 method. I can't seem to find the equivelent DX8 method if there is one. I am not opposed to just making my texture sheet larger and mirroring the textures for the different directions. I don't particularly like that idea as it is not very elegant and is extremely time consuming.


This wouldn't do what you think it would. See this.

This is what I use for flipping images before rendering:
//Note: I use DirectX 9, but this ought to work in DirectX 8 unless some big//changes were made inbetween. If so however, it should be simple to convert.D3DXMatrix finalMatrix;if(image.flipped())    {					        D3DXVECTOR3 textureCenter(0.0f, 0.0f, 0.0f);        D3DXVECTOR3 translation(static_cast<float>(image.width()), 0.0f, 0.0f);        D3DXMATRIX flippedMatrix;        D3DXMatrixRotationY(&flippedMatrix, D3DX_PI);        D3DXQUATERNION rotationQuaternion;        D3DXQuaternionRotationMatrix(&rotationQuaternion, &flippedMatrix);        D3DXMatrixTransformation(&finalMatrix, NULL, NULL, NULL, &textureCenter, &rotationQuaternion, &translation);    }


This makes "finalMatrix" the "flipping matrix" to be SetTransform()'d or matrix multiplied with the final transformation matrix.

Quote:Next question is quick and to the point. Is there a way to tile textures on one polygon? For example, I have a texture I am using on the floor of one of my levels that I would like to tile and not stretch across the entire floor of my level, nor would I like to create a couple hundred proportional polygons to line across the bottom so that it creates a pattern. Is there a way around this, I'm guessing there must be but I can't find anything on it.


I haven't deal with texture addressing myself in a while, so someone else will come along and give you a better explanation, but IIRC you set your texture coordinates in a range greater than [0.0, 1.0] and it'll repeat the texture accordingly. There should be more detailed info in the DX docs.

Quote:Final problem and maybe a small bit more involved. I am keeping track of my objects within a linked list and looping through them to render them as well as check for collisions, etc. I have found a way to delete items off of the list using this bit of code:

*** Source Snippet Removed ***
Does the erase method of the linked list actually destroy the class as well? As you can see, that bit of code is controlling the destruction of projectiles that go off of the screen and I would hate to have a few hundred active classes after they have actually gone off of the screen. I couldn't find a definitive "Yes, this destroys the class pointed to" anywhere so I am curious. Thanks everyone!


You probably mean "object", not the class. Assuming you're not storing pointers to new'd memory that you never free, yes, that destroys the object(s).
Hmm, well I haven't been using matrices yet since this is just a 2d scroller, I haven't gotten around to messing with them. Is that still relevant within a 2d world? As for the tiling, I will try that ASAP. I hadn't ever tried going past the 1.0f threshold, I thought that it would not display the texture entirely.
I will forever be a student.
In DX8, use
SetTextureStageState(0, D3DTSS_ADDRESSU, D3DTADDRESS_MIRROR);

This means that the texture is facing normally when U is between 0 and 1. When U is between 1 and 2, it reads backwards. Between 2 and 3 it's facing normally again. So, for example, if you wanted the range from 0.25 to 0.50 to be mirrored, you'd use 1.50 to 1.75 instead, or -0.25 to -0.50.

To tile a texture, set the address mode (ADDRESSU and ADDRESSV, as above) to D3DTADDRESS_WRAP. If your UVs extend from 0 to 3, you'll see 3 copys of the texture. If go from 0 to 3 in both U and V, you'll see a 3x3 grid of your texture.

As for the STL question, I've never used STL, so I'll let someone else handle that one.

edit: A better way to mirror a texture in polygons is to just change your UVs, and ignore the addressing mode change. Instead of going from 0.25 to 0.5, just go from 0.5 to 0.25... or whatever your UV coords are. Just swap the left and right U coords and the top and bottom V coords)
Does that actually work? I havent attempted to try that since it's a pretty big code change for me and I wasn't completely sure it would work that way. (reversing the UV's I mean)
I will forever be a student.
Quote:Original post by jrmiller84
Hmm, well I haven't been using matrices yet since this is just a 2d scroller, I haven't gotten around to messing with them. Is that still relevant within a 2d world? As for the tiling, I will try that ASAP. I hadn't ever tried going past the 1.0f threshold, I thought that it would not display the texture entirely.


How are you rendering your objects? You said you were using DX8 and didn't mention ID3DXSprite so I assumed you were using a screen-aligned textured quad system. If you're still using the old DirectDraw7 interface to render, then yeah, the flip-matrix code isn't going to be much help :/

EDIT:
Quote:Original post by jrmiller84
Does that actually work? I havent attempted to try that since it's a pretty big code change for me and I wasn't completely sure it would work that way. (reversing the UV's I mean)


Don't worry, it's Namethatnobodyelsetook. He knows what he's talking about! [wink] You could probably write up a quick test anyway.
Quote:Original post by jrmiller84
Does that actually work? I havent attempted to try that since it's a pretty big code change for me and I wasn't completely sure it would work that way. (reversing the UV's I mean)

Yeah, it should be fine. You're saying you want "this part the the image" at "these world coordinates". Textures can be mapped onto polygons in any direction, they don't have to follow any left to right, top to bottom rules of old 2D engines.
Quote:Original post by load_bitmap_file
How are you rendering your objects? You said you were using DX8 and didn't mention ID3DXSprite so I assumed you were using a screen-aligned textured quad system. If you're still using the old DirectDraw7 interface to render, then yeah, the flip-matrix code isn't going to be much help :/



haha, I'll definately try it then. As for the quote above. I'm not so sharp on the terminology yet but I do believe I am using textured quads. Basically I am just rendering square polygons onto the screens and texturing them w/ transparency and doing a bit of modification to the UV values of the vertices to shift focus of the textures to appear like animation. Maybe that's a pretty broad explanation, but hopefully it helps, lol. Thanks everyone!
I will forever be a student.

This topic is closed to new replies.

Advertisement