Blitz 2D
A wrapper for easily using DirectX9 in 2D games (with a built-in fall back to GDI+ if DirectX9 isn't present)
Setup
- Make sure you have the DirectX 9 SDK installed
- Include Blitz2D.cpp and Blitz2D.h in your project
- Add gdiplus.lib to the list of libraries linked to
- Add #include "Blitz2D.h" to any of your .cpp files that will call the Blitz2D functions
Useage
Call B2D_Start() before you do your ShowWindow() call and the code will attempt to load the DirectX9 DLL at run time (and the first D3DX9_nn.DLL it can find), and on failing this it will fall back on using GDI+ calls to produce the same, but slower, results.
Next, call the create and load functions to set things up for your game. You can load pretty much any type of image file including .BMP and .PNGs:
- int B2D_LoadImage(char* szFilename, int iWidth, int iHeight);
- int B2D_CreateOffscreenSurface(int iWidth, int iHeight);
- int B2D_CreateFont(unsigned int Height, unsigned int Weight, BOOL Italic, char* szFacename);
- int B2D_CreateFontEx(unsigned int Height, unsigned int Width, unsigned int Weight, unsigned int MipLevels, BOOL Italic, DWORD CharSet, DWORD OutputPrecision, DWORD Quality, DWORD PitchAndFamily, char* szFacename);
Then, once your game has entered the message loop you can start drawing to a surface (either one that you have created using B2D_CreateOffscreenSurface or to the "Back Buffer"). First select which surface you want to draw to:
- BOOL B2D_StartDrawingTo(int hSurfaceHandle); - use a handle returned from B2D_CreateOffscreenSurface or the define BACK_BUFFER
You can set the attributes of the currently selected surface:
- BOOL B2D_SetPenColor(int iRed, int iGreen, int iBlue, float fOpacity);
- BOOL B2D_SetFillColor(int iRed, int iGreen, int iBlue, float fOpacity);
- BOOL B2D_SetPenAntiAlias(BOOL bAntiAlias);
- BOOL B2D_SetFont(int hFontHandle);
And then draw to the currently selected surface:
- BOOL B2D_Clear(int iRed, int iGreen, int iBlue);
- BOOL B2D_ClearRect(RECT* rect, int iRed, int iGreen, int iBlue);
- BOOL B2D_BltSurface(int hSurfaceHandle, int x, int y);
- BOOL B2D_BltSurfaceStretched(int hSurfaceHandle, RECT* pSrc, RECT* pDest);
- BOOL B2D_DrawImage(int hImageHandle, int x, int y, float fRotate, float fOpacity);
- BOOL B2D_DrawImageCenteredAt(int hImageHandle, int x, int y, float fRotate, float fOpacity);
- BOOL B2D_DrawImageFrame(int hImageHandle, int x, int y, float fRotate, float fOpacity, int iFrameX, int iFrameY, int iFrameSizeX, int iFrameSizeY);
- BOOL B2D_DrawImageFrameCenteredAt(int hImageHandle, int x, int y, float fRotate, float fOpacity, int iFrameX, int iFrameY, int iFrameSizeX, int iFrameSizeY);
- BOOL B2D_DrawLine(int iFromX, int iFromY, int iToX, int iToY, int iWidth);
- BOOL B2D_DrawBox(int iLeft, int iTop, int iRight, int iBottom, int iWidth);
- BOOL B2D_DrawBox3D(int iLeft, int iTop, int iRight, int iBottom, int iWidth);
- BOOL B2D_DrawText(char* szString, int x, int y, int iAlignment);
- BOOL B2D_DrawTextWithShadow(char* szString, int x, int y, int iAlignment, int iShadowX, int iShadowY);
- BOOL B2D_DrawTextCenteredAt(char* szString, int x, int y);
- BOOL B2D_DrawTextCenteredAtWithShadow(char* szString, int x, int y, int iShadowX, int iShadowY);
- BOOL B2D_DrawTextInRect(char* szString, RECT* pRect, int iAlignment);
When all drawing is finished you can BLT the back buffer on to the screen for the user to see:
- BOOL B2D_Present();
When your program is about to exit call this function to free up all memory:
- void B2D_Stop();
Downloads
Blitz2D.cpp
Blitz2D.h
A Visual Studio 6 project that demonstrates Blitz2D
A .exe file which demonstrates an example Blitz2D program
Help
You're free to use this code! But, please email me if you have any comments, suggestions, bug reports, bug fixes etc... on this project.
If it's popular I'll set up a forum.
For the future I'd like to add: drawing simple triangles and drawing arrows. Another future idea might be to make use of Direct2D if that's found on the user's computer.
|