Sign in to follow this  

How do I dispaly text in direct draw

This topic is 4763 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
#define WINDOW_CLASS_NAME "WINCLASS1"

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

// FUNCTIONS //////////////////////////////////////////////
LRESULT CALLBACK WindowProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
// this is the main message handler of the system
PAINTSTRUCT ps; // used in WM_PAINT
HDC hdc; // handle to a device context

// what is the message
switch(msg)
{
case WM_CREATE:
{
// do initialization stuff here
// return success
return(0);
} break;


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

// return success
return(0);
} break;

case WM_DESTROY:
{

// kill the application, this sends a WM_QUIT message
PostQuitMessage(0);

// return success
return(0);
} break;

default: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 |
CS_HREDRAW | CS_VREDRAW;
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))
return(0);

// create the window
if (!(hwnd = CreateWindowEx(NULL, // extended style
WINDOW_CLASS_NAME, // class
"GDI Text Printing Demo", // title
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
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
return(0);

// 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
while(TRUE)
{
// 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)
break;

// translate any accelerator keys
TranslateMessage(&msg);

// send the message to the window proc
DispatchMessage(&msg);
} // 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!"));

Sleep(10);

} // end while

// release the dc
ReleaseDC(hwnd,hdc);

// return to Windows like this
return(msg.wParam);

} // 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;
}

return;
}







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
Here's a function that i have used for a long time when i was still using DirectDraw. It actually uses the feature in DirectDraw the lets you access the windows GDI. The parameters in the function should be obvious to understand. As for the definitions of Win32 functions inside the function, look them up in MSDN. Since it uses GDI (although wrapped inside this function), it is not the fastest way of rendering text into DirectDraw screen, however, it guarantees you an easy way of printing text.
I hope it helps you, as it's been very useful for me.

int DrawTextGDI( LPDIRECTDRAWSURFACE7 lpDDS,
int x, int y,
unsigned char red,
unsigned char green,
unsigned char blue,
unsigned char alpha,
HFONT hFont,
int BkMode,
char *text)
{
// this function draws the sent text on the sent surface
// using color index as the color in the palette
HDC hdc; // the working dc
HFONT hFontOld;

// get the dc from surface
if ( FAILED(lpDDS->GetDC(&hdc)) )
return 0;

// set the colors for the text up
COLORREF TxtColorOld = SetTextColor(hdc,RGB(red,green,blue));

// set background mode to transparent so black isn't copied
int BkModeOld = SetBkMode(hdc, BkMode);

// set the font if it's available
if ( hFont ) hFontOld = (HFONT)SelectObject(hdc,hFont);

// draw the text a
TextOut(hdc,x,y,text,(int)strlen(text));

// return back the HDC to its original settings
// do this to avoid memory leak
SetTextColor(hdc,TxtColorOld);
SetBkMode(hdc, BkModeOld);

// if there's font, please delete it
if ( hFont ) SelectObject(hdc,hFontOld);

// release the dc
lpDDS->ReleaseDC(hdc);

// return success
return 1;

} // end Draw_Text_GDI



Edited by Coder: Added source tags

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

Share this post


Link to post
Share on other sites
Quote:
Original post by kingpinzs
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.


I guess my solution is a little confusing. In my example, I'm assuming that the font bitmap's ASCII values are laid out (00 -> 127) from left to right all on one row. So 'A' would be 65 characters in on the left.

If your bitmap is laid out differently, you just have to change the equations that affect iLetterMinX and iLetterMinY.

Share this post


Link to post
Share on other sites

This topic is 4763 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this