Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Drawing text with DirectDraw

Drawing text with DirectDraw

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsc++game-devtutorialquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hanan888
    wrote on last edited by
    #1

    I'm developing an animated game. It's a Win32 app, written in C++. For various reasons, I'm porting it to use DirectDraw interfaces. I can't find a simple way to draw text. I saw examples of people blitting images of text, for example to draw "Hey" they draw image of 'H' then image of 'e'... I might draw with Windows GDI but that AFAIK this is drawing to 'windows'/'controls'/HWND , which will make the code some crazy mess. Where's the easy way ?

    J 1 Reply Last reply
    0
    • H Hanan888

      I'm developing an animated game. It's a Win32 app, written in C++. For various reasons, I'm porting it to use DirectDraw interfaces. I can't find a simple way to draw text. I saw examples of people blitting images of text, for example to draw "Hey" they draw image of 'H' then image of 'e'... I might draw with Windows GDI but that AFAIK this is drawing to 'windows'/'controls'/HWND , which will make the code some crazy mess. Where's the easy way ?

      J Offline
      J Offline
      James R Twine
      wrote on last edited by
      #2

      I believe you are either going to have to use letter-bitmaps as you mention above, or resort to using GUI to draw on the DirectDraw surface.  I did this a long time ago, and I am not even sure if recent versions of DirectX still support the ability to have GDI/GDI+ draw on a DD surface...    Creating a function that takes a source bitmap (of letters/numbers/symbols), a string, and a origin is not really that hard, and may work faster than you think.  It might be faster than going back and forth between DX/GDI in the long run...    Peace!

      -=- James
      Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
      Remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
      See DeleteFXPFiles

      H 1 Reply Last reply
      0
      • J James R Twine

        I believe you are either going to have to use letter-bitmaps as you mention above, or resort to using GUI to draw on the DirectDraw surface.  I did this a long time ago, and I am not even sure if recent versions of DirectX still support the ability to have GDI/GDI+ draw on a DD surface...    Creating a function that takes a source bitmap (of letters/numbers/symbols), a string, and a origin is not really that hard, and may work faster than you think.  It might be faster than going back and forth between DX/GDI in the long run...    Peace!

        -=- James
        Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
        Remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
        See DeleteFXPFiles

        H Offline
        H Offline
        Hanan888
        wrote on last edited by
        #3

        Thanks.

        H 1 Reply Last reply
        0
        • H Hanan888

          Thanks.

          H Offline
          H Offline
          Hanan888
          wrote on last edited by
          #4

          I managed to mix the DirectDraw and Windows GDI like this:

          HDC hdc = NULL;
          HRESULT result = m\_lpDDSBack->GetDC(&hdc);  //m\_lpDDSBack is my backbuffer surface
              wchar\_t text\[256\] = "thank god";
              int x = 100;
              int y = 200;
          
              size\_t length = wcslen(text);
          CPTDDUtils::MyDrawText(hdc, x, y, text, length);
          
          m\_lpDDSBack->ReleaseDC(hdc);
          

          void CPTDDUtils::MyDrawText(HDC hdc, int x, int y, LPCTSTR lpString, int stringSize)
          {
          /*HFONT*/ HGDIOBJ hFont, hOldFont;

          // Retrieve a handle to the variable stock font. 
          hFont = GetStockObject(ANSI\_VAR\_FONT); 
          
          // Select the variable stock font into the specified device context.
          if (hOldFont = SelectObject(hdc, hFont)) 
          { 
          	TextOut(hdc, x, y,lpString , stringSize); 
          
          	// Restore the original font.	
          	SelectObject(hdc, hOldFont); 
          }
          

          }

          I do this after I drew the bitmaps, and before I 'flip' . So this draws the text but in white rectangle. I don't want the white rectangle, I want this rectangle to be transparent. Also, I rather use some normal font. Also, if this code is 'messed up', please let me know. :)

          J S 2 Replies Last reply
          0
          • H Hanan888

            I managed to mix the DirectDraw and Windows GDI like this:

            HDC hdc = NULL;
            HRESULT result = m\_lpDDSBack->GetDC(&hdc);  //m\_lpDDSBack is my backbuffer surface
                wchar\_t text\[256\] = "thank god";
                int x = 100;
                int y = 200;
            
                size\_t length = wcslen(text);
            CPTDDUtils::MyDrawText(hdc, x, y, text, length);
            
            m\_lpDDSBack->ReleaseDC(hdc);
            

            void CPTDDUtils::MyDrawText(HDC hdc, int x, int y, LPCTSTR lpString, int stringSize)
            {
            /*HFONT*/ HGDIOBJ hFont, hOldFont;

            // Retrieve a handle to the variable stock font. 
            hFont = GetStockObject(ANSI\_VAR\_FONT); 
            
            // Select the variable stock font into the specified device context.
            if (hOldFont = SelectObject(hdc, hFont)) 
            { 
            	TextOut(hdc, x, y,lpString , stringSize); 
            
            	// Restore the original font.	
            	SelectObject(hdc, hOldFont); 
            }
            

            }

            I do this after I drew the bitmaps, and before I 'flip' . So this draws the text but in white rectangle. I don't want the white rectangle, I want this rectangle to be transparent. Also, I rather use some normal font. Also, if this code is 'messed up', please let me know. :)

            J Offline
            J Offline
            James R Twine
            wrote on last edited by
            #5

            Look up the SetBkMode(...) function for setting up Transparency.  Also look up the ExtTextOut(...) function, as I bet you will be moving to it soon! :)    You can specify your own font to use by using the CreateFont(...) and CreateFontIndirect(...) functions.  Better to create the font once and reuse it.    If you want something like a multicolor font, you will need to draw the bitmaps for it yourself.    Peace!

            -=- James
            Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
            Remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
            See DeleteFXPFiles

            H 1 Reply Last reply
            0
            • J James R Twine

              Look up the SetBkMode(...) function for setting up Transparency.  Also look up the ExtTextOut(...) function, as I bet you will be moving to it soon! :)    You can specify your own font to use by using the CreateFont(...) and CreateFontIndirect(...) functions.  Better to create the font once and reuse it.    If you want something like a multicolor font, you will need to draw the bitmaps for it yourself.    Peace!

              -=- James
              Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
              Remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
              See DeleteFXPFiles

              H Offline
              H Offline
              Hanan888
              wrote on last edited by
              #6

              Thanks a lot. :-D

              1 Reply Last reply
              0
              • H Hanan888

                I managed to mix the DirectDraw and Windows GDI like this:

                HDC hdc = NULL;
                HRESULT result = m\_lpDDSBack->GetDC(&hdc);  //m\_lpDDSBack is my backbuffer surface
                    wchar\_t text\[256\] = "thank god";
                    int x = 100;
                    int y = 200;
                
                    size\_t length = wcslen(text);
                CPTDDUtils::MyDrawText(hdc, x, y, text, length);
                
                m\_lpDDSBack->ReleaseDC(hdc);
                

                void CPTDDUtils::MyDrawText(HDC hdc, int x, int y, LPCTSTR lpString, int stringSize)
                {
                /*HFONT*/ HGDIOBJ hFont, hOldFont;

                // Retrieve a handle to the variable stock font. 
                hFont = GetStockObject(ANSI\_VAR\_FONT); 
                
                // Select the variable stock font into the specified device context.
                if (hOldFont = SelectObject(hdc, hFont)) 
                { 
                	TextOut(hdc, x, y,lpString , stringSize); 
                
                	// Restore the original font.	
                	SelectObject(hdc, hOldFont); 
                }
                

                }

                I do this after I drew the bitmaps, and before I 'flip' . So this draws the text but in white rectangle. I don't want the white rectangle, I want this rectangle to be transparent. Also, I rather use some normal font. Also, if this code is 'messed up', please let me know. :)

                S Offline
                S Offline
                Stephen Hewitt
                wrote on last edited by
                #7

                Hanan888 wrote:

                wchar_t wchar_t text[256] = "thank god";

                Why the 256? Use this instead:

                wchar_t text[] = "thank god";

                Why waste 492 bytes?

                Hanan888 wrote:

                size_t length = wcslen(text);

                If the above change is made this runtime call call be changed to a compile time calculation as follows:

                size_t length = sizeof(text)/sizeof(text[0]) - 1;

                Steve

                H 1 Reply Last reply
                0
                • S Stephen Hewitt

                  Hanan888 wrote:

                  wchar_t wchar_t text[256] = "thank god";

                  Why the 256? Use this instead:

                  wchar_t text[] = "thank god";

                  Why waste 492 bytes?

                  Hanan888 wrote:

                  size_t length = wcslen(text);

                  If the above change is made this runtime call call be changed to a compile time calculation as follows:

                  size_t length = sizeof(text)/sizeof(text[0]) - 1;

                  Steve

                  H Offline
                  H Offline
                  Hanan888
                  wrote on last edited by
                  #8

                  Thanks a lot

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups