Jump to content
  • Advertisement
Sign in to follow this  

How do I dispaly text in direct draw

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do I display text in directdraw using a font.bmp? I want to be able to display my variables at run time so I know what is going on. Like a debug screen but I want it displayed in the main windows for now then I will move it to a console type windows later.

Share this post

Link to post
Share on other sites
I think you need to be a bit more specific about what the problem is. Essentially, you want to blit the rectangle on your source surface (your font) containing the letter you want to draw to the destination surface at the point where you want the letter. Repeat for each letter.
But what is holding you up?

Share this post

Link to post
Share on other sites
so in the end it is just like a sprite the you are printing to the screen. So I would just make a rutine that would let me just type in text and it would you the bmp and blt it to the screen. I did not think of it that way even though that would be the obvius way to do it. I was thinking there was a built in function in directdraw that would do it for you.

Share this post

Link to post
Share on other sites
You can also call GetDC on the surface and then use GDI functions (e.g. TextOut).

It's also common to do both, e.g. use GDI to render a system font to a scrach surface then write a mini font engine to blt the rendered characters from the scratch surface to the destination surface.

Regardless, you need to do some work. There is no IDirectDrawSurface::DrawText or suchlike.

Share this post

Link to post
Share on other sites
does any one have sample code that will help me figure out how to load the ltters from my bmp and be able to blt them to screen by just putting int the text instead of righting in the coordinets.

I know it will be like drawing my sprite but it needs to be a little fdiffernt so I can code in . ddfont(" hi", 10,0);
and it would blt hi at 10 x 0 y on the screen.

Share this post

Link to post
Share on other sites
Just use this coding, its from a text application (Win32)

// INCLUDES ///////////////////////////////////////////////
#define WIN32_LEAN_AND_MEAN // just say no to MFC

#include <windows.h> // include all the windows headers
#include <windowsx.h> // include useful macros
#include <mmsystem.h> // very important and include WINMM.LIB too!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

// DEFINES ////////////////////////////////////////////////

// defines for windows

// GLOBALS ////////////////////////////////////////////////
HWND main_window_handle = NULL; // globally track main window
HINSTANCE hinstance_app = NULL; // globally track hinstance

// FUNCTIONS //////////////////////////////////////////////
UINT msg,
WPARAM wparam,
LPARAM lparam)
// this is the main message handler of the system
HDC hdc; // handle to a device context

// what is the message
// do initialization stuff here
// return success
} break;

case WM_PAINT:
// simply validate the window
hdc = BeginPaint(hwnd,&ps);
// you would do all your painting here

// return success
} break;


// kill the application, this sends a WM_QUIT message

// return success
} break;


} // end switch

// process any messages that we didn't take care of
return (DefWindowProc(hwnd, msg, wparam, lparam));

} // end WinProc

// WINMAIN ////////////////////////////////////////////////
int WINAPI WinMain( HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)

WNDCLASSEX winclass; // this will hold the class we create
HWND hwnd; // generic window handle
MSG msg; // generic message

// first fill in the window class stucture
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_DBLCLKS | CS_OWNDC |
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;
winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

// save hinstance in global
hinstance_app = hinstance;

// register the window class
if (!RegisterClassEx(&winclass))

// create the window
if (!(hwnd = CreateWindowEx(NULL, // extended style
"GDI Text Printing Demo", // title
0,0, // initial x,y
400,400, // initial width, height
NULL, // handle to parent
NULL, // handle to menu
hinstance,// instance of this application
NULL))) // extra creation parms

// save main window handle
main_window_handle = hwnd;

// get the dc and hold it
HDC hdc = GetDC(hwnd);

// enter main event loop, but this time we use PeekMessage()
// instead of GetMessage() to retrieve messages
// test if there is a message in queue, if so get it
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
// test if this is a quit
if (msg.message == WM_QUIT)

// translate any accelerator keys

// send the message to the window proc
} // end if

// main game processing goes here

// set the foreground color to random
SetTextColor(hdc, RGB(rand()%256,rand()%256,rand()%256));

// set the background color to black
SetBkColor(hdc, RGB(0,0,0));

// finally set the transparency mode to transparent
SetBkMode(hdc, TRANSPARENT);

// draw some text at a random location
TextOut(hdc,rand()%400,rand()%400, "GDI Text Demo!", strlen("GDI Text Demo!"));


} // end while

// release the dc

// return to Windows like this

} // end WinMain


Dont forget your DirectX includes!

Edited by Coder: Added source tags

[Edited by - Coder on December 3, 2004 12:27:56 PM]

Share this post

Link to post
Share on other sites
What aspect are you struggling on?

I'm going to go ahead and assume your bitmap is stored like this (in ASCII values):
00 01 02 03 04 05 06 07 08 09 10 ... 127

So capital A would be 65 in from the left (ASCII Table).

So your DrawText routine would just be a simple loop, moving to the right on every iteration, for every letter.

void DrawText(std::string szText, int iX, int iY)
for(int iLoop = 0; iLoop < szText.size(); ++iLoop)
// Get the ASCII value of the letter.
int iLetterMinX = szText[iLoop];
int iLetterMinY = 0;
int iLetterMaxX = iLetterMinX + LetterWidth;
int iLetterMaxY = iLetterMinY + LetterHeight;

RECT rcSource = {iLetterMinX, iLetterMinY, iLetterMaxX, iLetterMaxY};
RECT rcDestination = {iX, iY, iX + LetterWidth, iY + LetterHeight};

Blit(FontBitmap, rcSource, rcDestination);

// Move the next letter to the right.
iX += LetterWidth;


This is just rough psuedo-code, but it should get you going.

[Edited by - GroZZleR on December 3, 2004 11:59:20 AM]

Share this post

Link to post
Share on other sites
in my bitmap I am just using plain text.

I just want to blt each letter that is needed.

It just goes along picks out the letters then displays them on to the screen just using directdraw.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!