Text output code

Started by
3 comments, last by Irrelevant 20 years, 8 months ago

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.
Advertisement
And in case it''s not evident from the code, I''m just trying to change the color.
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.




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.
Thanks for the replies. Didn''t think to draw text onto a texture... good idea.
What I''ve done (was rushed, didn''t communicate effectively (sorry)) is load a bitmap as a texture. On the bitmap I''ve drawn the whole font (8 rows of 16, haven''t drawn the other 128 characters yet (and probably won''t, i''ve got what i need)) and wrote an algorithm to generate the texture coordinates for the image. Everything was working delightfully. It displayed the white text (as I had drawn it) exactly as I was expecting. When I tried to put the lock/unlock code in to change the color, it stopped appearing.
And to hawkeye, all my verts are in a buffer (write only) created at startup. I wasn''t generating new data every frame (I''m not TOTALLY newb ) But thanks for checking.
Just tried your color method hawkeye, works like a charm (and saves me from modifying 1024 verts). Thanks for the tip. Never even knew about that render state hehe.
Thanks all

This topic is closed to new replies.

Advertisement