• ### Announcements

#### Archived

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

# Text output code

## Recommended Posts

Irrelevant    175
void text(char* pstring,float x,float y,DWORD c)
{
if(pstring&&pdev)
{
pdev->SetTexture(0,pfontmap);/*
vertex* pverts = 0;
pvbuf->Lock(0,0,(void**)&pverts,0);
for(int x = 0; x < 2112; x++)
pverts[x].c = c;
pvbuf->Unlock();*/
int col = 0;
int row = 0;
int len = strlen(pstring);
int idx = 0;
int max = 0;
for(int i = 0; i < len; i++)
{
if(pstring[i] > 31 && pstring [i] < 127)
{
idx = (pstring[i] - 32) * 4;
D3DXMatrixTranslation(&viewmat,x+(float)col*5,y-(float)row*5,0.0f);
pdev->SetTransform(D3DTS_VIEW,&viewmat);
col++;
pdev->DrawPrimitive(D3DPT_TRIANGLESTRIP,idx,2);
if(col > max)
max = col;
}
else
if(pstring[i] == ''\t'')
col += 4;
else
if(pstring[i] == ''\n'')
{
col = 0;
row++;
}
}
}
else
{
report("Lost the device. Fix this already.");
func = 0;
}
}

// this is the code i use to create the vertex buffer if anyone is wondering

pdev->CreateVertexBuffer(sizeof(vertex)*2112,D3DUSAGE_DYNAMIC|D3DUSAGE_WRITEONLY,D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1,D3DPOOL_DEFAULT,&pvbuf,0)

That code works fun until the locking/unlocking gets uncommented. Why would that mess things up? There are 2112 verts in the buffer, so no overflow or anything in the loop. Can you call Lock on a vb once you''re in BeginScene? this function is called after BeginScene, and when I initialize the verts, that''s before BeginScene so that''s the only thing I can think of.

##### Share on other sites
Irrelevant    175
And in case it''s not evident from the code, I''m just trying to change the color.

##### Share on other sites
HaywireGuy    208
Usually if you want text to be drawn on a texture, and then map
it to the polygon, you don''t make each of the characters a
polygon by itself. Try drawin'' text onto your own bitmap (or
whatever) usin'' Win32 DrawText() function, and then lock the
texture and copy that bitmap onto it.

Note that however if you''re usin'' DrawText() to draw on a 32-
bit bitmap, the function is gonna reset the alpha to zero for
those areas your text is coverin'' (this will effectively make
the text area totally transparent).

Hope that helps.

##### Share on other sites
Hawkeye3    122
In my text drawing function I use:
pdev->SetRenderState(D3DRS_TEXTUREFACTOR,color)
right before drawing the text primitives
and before rendering any text I set the states:
pdev->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
pdev->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
pdev->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_TFACTOR );

Oh, and to speed things up some, you should look at putting all of your characters into one vertex buffer and rendering them in one call. (You seemed to of had the right idea in the commented out code, you just didn''t perform it correctly). This will require that you create an algorithm to create the vertex positions at runtime, but it shouldn''t be too hard. Also, you can lock / unlock buffers while drawing, just don''t do it too much, and make sure they are write only.