Thread: Direct Draw and Text

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    60

    Direct Draw and Text

    while(TRUE){
    if(PeekMessage(&msg,NULL,0,0,PM_REMOVE){
    // Quit if necessary
    }
    else{
    if (nextFRAME<timeNOW){
    // Sprite1.move();
    // Screen.clear(RGB(0,0,0));
    // Sprite1.draw(Screen.GetSurface());
    // Screen.Flip();
    nextFRAME=timeNOW+timeBETWEENusually;
    }
    }

    The code above should demonstrate (simplified) how I usually implement a game loop for a DD7 game. (As you can see I'm still a beginner).
    The loop above works well when animating sprites.

    NOW: I have read that text output must still be done by GDI-Functions. Does this mean I have to send a WM_PAINT message to my Message-Processing CALLBACK function? But that means that the earliest point of time the text appears on the screen will be when the PeekMessage Part is processed. The problem is that the Sprite1.draw() function and the WM_PAINT message would be processed at completely different points of time, and I would have twice as much calls to the Screen.Flip() function, resulting in an annoying loss of time and performance.
    (I also heard one should not mix GDI and DirectDrawBlitting Functions in the same program).

    How would you solve the problems stated above?

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    No you don't have to send a WM_PAINT message. You have 2 basic options when using text in DDraw. You can use GDI functions or create your own text sprite class.

    I've used both in the past but now I stick with the text sprite class. It's faster and customizable. You can see an example of it here.

    Basically you have a bitmap that has the alphabet, numbers and whatever special symbols you want in a row all confined within a set rectangle (whatever size you prefer, I use 8x8). Then you parse in the string you want to display and use char arithmetic to get the position.

    IE: I use all lower case letters and I start with a and end with the number 9, so the letter be would be at position 1, c at position 2, etc.

    Code:
    //str is a member var that hold the text you want to display
    //iLen is a member var that represents str's length
    //sfText is LPDIRECTDRAWSURFACE7 member var
    //rcText is a RECT member var
    
    void CText::Blt(lpdds7 sf)
    {
        for(int i=0;i<iLen;i++)
        {
            iLtrPos=str[i]-'a';
            SetRect(&rcText,iLtrPos<<3,iClr<<3,(iLtrPos<<3)+8,(iClr<<3)+8);
            sf->BltFast(rcXY.left+(i<<3),rcXY.top,sfText,&rcText,DDBLTFAST_SRCCOLORKEY);
        }
    }
    If you're interested email or PM me and I'll send you the code for the class.


    **EDIT** sorry I just realized that I forgot to address your GDI questions. There is absolutely nothing wrong with mixing GDI and DirectDraw in an app as long as you do it sensibly. In other words, using BitBlt to draw all your sprites to a DirectDraw backbuffer each frame would be ridiculous. But on the other hand, during initialization I can't think of a quicker, easier way to load the bitmap's image onto the Sprite's surface than BitBlt. I use a 2 or 3 GDI functions during my set-up, but for all of my in game blitting I use DDraw.

    As for how to use GDI to do text with DDraw simply use TextOut. Here's an example:

    Code:
    //where sfBackbuffer is your backbuffer surface and sfPrime is 
    //your primary primary surface
    
    //in your main loop after clearing the backbuffer
    HDC hdc;
    sfBackbuffer->GetDC(&hdc);
    TextOut(hdc,100,100,"hello",5);
    sfBackbuffer->ReleaseDC(hdc);
    Last edited by jdinger; 03-14-2003 at 08:19 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Draw an hyperlink on the window
    By Niara in forum Windows Programming
    Replies: 4
    Last Post: 06-25-2007, 01:08 PM
  2. Having trouble drawing text xlib.
    By Qui in forum Linux Programming
    Replies: 1
    Last Post: 05-10-2006, 12:07 PM
  3. text on 2d tiles in DirectX?
    By Quantum1024 in forum Game Programming
    Replies: 4
    Last Post: 04-26-2006, 11:40 PM
  4. Drawing Text on the Desktop
    By dalek in forum Windows Programming
    Replies: 6
    Last Post: 04-11-2004, 05:02 AM
  5. Constantly updated editable colored text
    By _Elixia_ in forum Windows Programming
    Replies: 2
    Last Post: 06-15-2003, 04:21 PM