Public Group

#### Archived

This topic is now archived and is closed to further replies.

# 1 texture, many tiles?

This topic is 5658 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I''m working along with a book (Sams Teach Yourself Game Programming with DirectX in 21 Days... Hey, it works) to create a topdown 2d style rpg, much like the old Ultima (so the book claims), and the Final Fantasy games. That means it''s using tiles, which brings me to my question. The way the book creates the ''world view'' is by having one image with 64 tiles, load it as a surface, and display the right tile according to the map. That works fine for me, except it''s not very ncie to have trees, chests, people, ... with black background rather than the ''real'' background. Now the player''s a seperate image file, and a seperate drawing sequence, so adapting transparancy there was easy. However, trees, people, and so on is not that easy. They don''t have their own image, for like the background tiles they''re packed into one file (the book said it was more efficient, so it''s good practice to do that to the foreground tiles as well). So, in short, here''s what i''m having problems with: displaying (for example) the rectangle 0,160,40,200 (with a 40x40 tile, top left bottom right) from say a 40x320 image. Here''s some excerpts of code relevant to the problem:
// load image file

hResult = D3DXCreateTextureFromFileEx(m_pD3DDevice, "Graphics\\ftiles.bmp", D3DX_DEFAULT, D3DX_DEFAULT,
1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0xFF000000, NULL, NULL, &m_pFTileSurface);
if(FAILED(hResult)){
return hResult;
}

// place texture function, places a tile from the texture onto the backbuffer... Well, it should anyway

m_Direct3D.SetVertices(DstCol,DstRow);

m_Direct3D.GetDevice()->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
m_Direct3D.GetDevice()->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
m_Direct3D.GetDevice()->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
m_Direct3D.GetDevice()->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
m_Direct3D.GetDevice()->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
m_Direct3D.GetDevice()->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
m_Direct3D.GetDevice()->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
m_Direct3D.GetDevice()->SetStreamSource(0, m_Direct3D.GetVertexBuffer(),sizeof(CUSTOMVERTEX));
m_Direct3D.GetDevice()->SetTexture(0, m_Direct3D.GetFTileSurface());

if(SUCCEEDED(m_Direct3D.GetDevice()->BeginScene())){
m_Direct3D.GetDevice()->DrawPrimitive(D3DPT_TRIANGLELIST,0,2);
m_Direct3D.GetDevice()->EndScene();
}

by the way, is it a waste or not to do all that stuff each texture that needs to be placed? Thanks

##### Share on other sites
I haven''t worked very much with DirectX, but are you actually loading the tile from disk everytime it needs to be drawn? You should defainately not do that. Instead, load all the tiles into an array (for example) at startup.

For crying out loud, she has fishes coming out of her head on either side. How can you find this hot?!

##### Share on other sites
sorry, should have been more specific.
The first part of code is run once at runtime.
The second part is done each time a foreground tile needs to be drawn.

I realize an array (or a vector) would work just fine, but the book said one surface was more efficient than 64 surfaces, following that train of thought one texture would be more efficient as well.

##### Share on other sites
One texture is more efficient, since you don''t have to do any texture-changes. But most of that stuff can be removed and ran once, like all the renderstates for example.
Anyway, do get the transparency working, use a bitmap-format that supports alpha, and not BMP. Use tga, or png or something instead, and just use the alpha-channel for the transparency.

##### Share on other sites
oh... I''m not familiar with that use, but since you wrote ''just do...'' in a beginners forum, i''m assuming it automatically loads supported formats.

I realize you said ''not BMP'', but photoshop supports 32bit bitmaps. Would that work?

##### Share on other sites
no, .bmp files do not support alpha values. if i was you i would use .png''s (which i do anyways because they take up less space than bitmaps)

doh, nuts. Mmmm... donuts
My website

##### Share on other sites
Well, it''s good to know png''s are supported. I did create a gif and see if it worked (i know gif transparency from use with websites), and i got an error trying to load the file, probably because it was using a color palette and i was loading it into an X8R8G8B8 surface. No biggie, 256 colors is not my thing anyway.

I did try the png example, worked great (it is a lot smaller... hope the quality wasn''t reduced), and it works great except for one thing: no transparency. Now the way i did it was remove black from the layer (... in blending options in PhotoShop 7). The only help the PS7 help files gave me didn''t work (couldn''t find their button or their menu... optimized for web use i think, but i don''t want a palletized image). The weird thing with my image is that it''s showing up white (the transparency), where in the file it''s actually black. When viewing the files in "Windows Picture and Fax Viewer" it does show as transparent as it does in PS7.

I''ve noticed an error (i think):
hResult = m_pD3DDevice->CreateImageSurface(320, 40, D3DFMT_X8R8G8B8, &m_pFTileSurface);
mainly: D3DFMT_X8R8G8B8
if i use D3DFMT_A8R8G8B8, however, they don''t show up at all!

(tried google, no luck on photoshop help)

##### Share on other sites
The quality of the png image will remain. It''s a losless (is this spelled right?) compression.

Look at all the pretty colours!

##### Share on other sites
Well, if the transparency is showing up correctly in Picture and Fax Viewer, it''s not a problem with your image; it''s a problem with your program.

Did you set up the alpha blending states correctly? I think the format *should* be A8R8G8B8 - or R8G8B8A8, if it''s available - and the thing about them not showing up when you''ve got that on is a separate problem.

Superpig
- saving pigs from untimely fates, and when he''s not doing that, runs The Binary Refinery.

##### Share on other sites
okay, i replaced every X8R8G8B8 with an A8R8G8B8; result: DirectX Error: The required display mode is not available on this system.
... the required display mode is not available on this system? It''s a geforce 4 ti 4600 (right right, radeon rules, whatever).

Okay fine, mode not supported. That still doesn''t explain why a completely black foreground tile overwrites the background with a completely white tile, and why a tree with black background simply removes the background, showing the gray from the Clear call as background (with the tree in there showing perfectly).
Who knows, D3D does some weird stuff, best not look for explanations on what went wrong, best to just fix what did go wrong.

now, aren''t i correct to assume you can display a certain area from a texture onto a certain area from the backbuffer using a RECT struct or something? No stretching, no shrinking, nothing, just cut and paste, basically. (basically... ya right )

Thanks

If only I knew now what I knew not tomorrow...

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633709
• Total Posts
3013478
×