How do I dispaly text in direct draw

Started by
11 comments, last by GroZZleR 19 years, 4 months ago
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.
Advertisement
does any body know how to do this?
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?
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.
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.
-Mike
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.
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 windowHINSTANCE 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 systemPAINTSTRUCT		ps;		// used in WM_PAINTHDC				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 createHWND	   hwnd;	 // generic window handleMSG		   msg;		 // generic message// first fill in the window class stucturewinclass.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 globalhinstance_app = hinstance;// register the window classif (!RegisterClassEx(&winclass))	return(0);// create the windowif (!(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 parmsreturn(0);// save main window handlemain_window_handle = hwnd;// get the dc and hold itHDC hdc = GetDC(hwnd);// enter main event loop, but this time we use PeekMessage()// instead of GetMessage() to retrieve messageswhile(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 dcReleaseDC(hwnd,hdc);// return to Windows like thisreturn(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]
does any one have a way to write text with only using directdraw?
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]
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.

This topic is closed to new replies.

Advertisement