Using MFC to create a BlackJack game platform
- double buffer drawing strategy in MFC to prevent controls or numbers from blinking (see OnPaint ())
- usage of CButtonST (Image Button with features of transparent background and no border)
- OpenCV perspective tranform to show slanted cards (see WarpPespective ())
- dynamically show number changes (see code when win or lose)
- dealing cards animation
- Download Visual Studio 2017 or newer versions
- Check on the option of "x86 and x64 version of C++ MFC"
- Install
- Open BlackJack.vcxproj
- Upgrade if it is required
- Open this project's property page
- Choose the SDK version you have in "General-Windows SDK Version"
- Choose the right toolset you have in "General-Platform Toolset" (for me, it is Visual Studio 2017 (v141))
- Go to "C/C++ - General", and type in "Additional Include Directories" for your own OpenCV include headers (e.g. C:\OpenCV3.1\opencv\build\include or C:\OpenCV4.0\opencv\build\include)
- Type in "Library Directories" for your own OpenCV's library path (the directory where your opencv_worldXX.lib locates)
- Go to "Linker-Input", and type in library name (e.g. C:\OpenCV3.1\build2017_worldx64\lib\opencv_world310d_vs2017_x64.lib)
- Make sure that your opencv_worldXX.dll and is in the same directory as .exe of this project (after building)
With Perspective Tranform, we can make the animation of deal more fluent and sophistocated.
If we want to build a full action of flipping cards, we need to divide this action into several steps.
For instance, now I break flipping cards into 20 actions with 20 different perspective matrix:
In this way, showing all cards continually can simulate the action of flipping
code to output images like this:
Point2f vecLT2RT = PLAYER_CARD1_RT - PLAYER_CARD1_LT;
Point2f vecLB2RB = PLAYER_CARD1_RB - PLAYER_CARD1_LB;
for (int i = 1; i < 20; i++)
{
Mat matShow = m_matShow.clone ();
m_ptsPlayerOne[0] = PLAYER_CARD1_LT + vecLT2RT / 40 * i;//LT
m_ptsPlayerOne[1] = PLAYER_CARD1_RT - vecLT2RT / 40 * i;//RT
m_ptsPlayerOne[2] = PLAYER_CARD1_LB + vecLT2RT / 40 * i;//LB
m_ptsPlayerOne[3] = PLAYER_CARD1_RB - vecLT2RT / 40 * i;//RB
Mat matAction = getPerspectiveTransform (m_ptsTableTop, m_ptsPlayerOne);
warpPerspective (m_matHoleCard, matShow, matAction, m_matTableTop.size (), 1, BORDER_TRANSPARENT);
string s = format ("C:\\users\\user\\Downloads\\%d.jpg", i);
imwrite (s, matShow (rect));
}