Download the project files here, you’ll notice there are two projects this time, first is the font demo, the other is the bitmap demo. Open up the font project file for starters. This program is exactly the same as the last line plotting demo for the most part, the difference lies in the global’s (concepts the same, they just refer to a font and not a pen), and the initialisation/cleanup routines. Compile it and run it, just so you can see what it does.
Yeah the fonts ugly, but it was the first one i came across and you can replace it with whatever you like, assuming you’ve read the init/cleanup comments:
bool Prog_Init()
{
RECT rcClient;
GetClientRect(hWndMain,&rcClient);
AddFontResource("Ass.ttf");
hfntNew=CreateFont(-44,0,0,0,0,0,0,0,0,0,0,0,0,"Astigma");
HDC hdc=GetDC(hWndMain);
hfntOld=(HFONT)SelectObject(hdc,hfntNew);
SetBkMode(hdc,TRANSPARENT);
SetTextColor(hdc,RGB(0,0,0));
DrawText(hdc,"OSI SI TEH OWN",-1,&rcClient,DT_CENTER | DT_VCENTER | DT_SINGLELINE);
ReleaseDC(hWndMain,hdc);
return(true);//return success
}
The first two lines are important as the RECT function is prevalent in direct x too, in a window the area you can draw on is known as the ‘client area’ obviously the size of this could be different every time, the first two lines just get the size of the client area. The AddFontResource function, as its name suggests loads a specified font into the system font table, once you load it you can create it within your app, which i do on the next line with the CreateFont function:
HFONT CreateFont(
int nHeight,
int nWidth,
int nEscapement,
int nOrientation,
int fnWeight,
DWORD fdwItalic,
DWORD fdwUnderline,
DWORD fdwStrikeout,
DWORD fdwCharSet,
DWORD fdwOutputPrecision,
DWORD fdwClipPrecision,
DWORD fdwQuality,
DWORD fdwPitchAndFamily,
LPCTstr lpszFace // font name (not filename)
);
Lot of parameters, you’ll notice ive set most of them off so feel free to experiment. The rest is pretty much explained by the comments, although the DrawText function could use some explaining:
int DrawText(
HDC hDC, //handle to the DC
LPCTSTR lpString //text to output
int nCount, //number of characters to display
LPRECT lpRect, //formatting dimensions
UINT uFormat //additional options
)
uFormat values are: DT_BOTTOM, DT_CENTER, DT_LEFT, DT_NOCLIP, DT_RIGHT, DT_SINGLELINE, DT_TOP and DT_VCENTER, once again experiment with the flags. The cleanup routine is pretty much a rewind version of the initialisation.
When you understand that. Onto the bitmap demo, fire up the project file and read through it, unlike the font app this program has a left mouse button handler that could use some explaining
case WM_LBUTTONDOWN:
{
HDC hdc=GetDC(hWndMain);
BitBlt(hdc,LOWORD(lParam)-BITMAPWIDTH/2,HIWORD(lParam)- BITMAPHEIGHT/2,BITMAPWIDTH,BITMAPHEIGHT,hdcMem,0,0,SRCCOPY)
ReleaseDC(hWndMain,hdc);
return(0);
}
you know the first and last statements, the new bugger is the one in the middle, BitBlt moves the bitmap from its dc to the window dc:
BOOL BitBlt
(
HDC hdcDest, //handle to destination (window) DC
int nXDest, //x cord of upper left corner
int nYDest, //y cord of upper left corner
int nWidth, //width of destination bitmap
int nHeight, //height of destination bitmap
HDC hdcSrc, //handle to dc that contains the bitmap
int nXSrc, //x cord of source upper left corner
int nYSrc, //y cord of upper left corner
DWORD dwRop //which raster operation to perform
);
Basically it defines how the bitmap gets coped, here ive used SRCCOPY which would overwrite the current contents of the DC, if anything was there, other raster op flags include SRCAND (copies one bmp over the other using AND), SRCPAINT (uses OR to combine the two) and SRCINVERT (uses XOR to combine and as its name suggests, inverts the image). The function that needs explaining in the initialisation routine is LoadImage:
HANDLE LoadImage(
HINSTANCE hinst, //handle to an instance
LPCTSTR lpszName, //name of the image (with extension)
UINT uType //type of image
int cxDesired, //width you want
int cyDesired, //height you want
UINT fuLoad //load flags
);
Once again the cleanup is self explanatory.
Direct x next time (albeit completely unimpressive), promise, make sure you have the sdk.
This article was originally written by Pigsbig78 |