//
// Subclass procedure for the OVEN pushbutton window instance.
// The purpose of having this subclass is to do all "OVEN" related
// messages separately here. In particular, it processes these messages:
// - WM_MOUSEMOVE
// - WM_CHAR
// - WM_TIMER
//
LRESULT APIENTRY NewButtonWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
RECT rect;
POINT pt;
//HWND parent;
switch (uMsg) {
// The WM_MOUSEMOVE message is posted whenever the mouse is moved.
// If no window is captured, this message is posted to the window where cursor is;
// Otherwise, it is posted to the capture window.
//
// The default wndproc must NOT check for MouseMovement.
// First entry into this code will be when cursor is over button.
//
// Use PtInRect() to check if the cursor is placed over the button window area.
// If so, turn OVEN on.
// Use PtInRect() to check if the cursor is placed outside the button window area.
// If so, turn OVEN off.
//
// When the mouse is over OVEN button, capture window with SetCapture(),
// so subsequent mousemove to outside of OVEN button will still be processed here.
//
case WM_MOUSEMOVE:
GetClientRect(hwnd, &rect); // get rect of button window
pt.x = LOWORD(lParam); // is this client co-ordinate the button, or parent window?
pt.y = HIWORD(lParam);
// fill start here
if(PtInRect(&rect, pt)) {
SetToasted(hwnd, 1);
SetCapture(hwnd);
}else
{
SetToasted(hwnd, 0);
ReleaseCapture();
}
// fill end here
return 0;
// The WM_CHAR message is generated by the OS when a keyboard key is pressed,
// and the system calls TranslateMessage(). By default this message is
// sent to the window where keyboard was pressed, or the to keyboard
// focus window (which we have set to be the OVEN button using SetFocus()).
// That's why we are processing this message here, in the OVEN button subclass proc.
case WM_CHAR:
// fill start here
switch(wParam) {
case 'b':
SetToasted(hwnd, 1);
SetTimer(hwnd, 1, 5000, NULL);
Sleep(5000);
}
// fill end here
return 0;
case WM_TIMER: // this message is generated when timer timesout.
SetToasted(hwnd, 0); // set OVEN OFF
return 0;
}
// return to original wndproc and process all other messages there.
return CallWindowProc((WNDPROC)GetClassLong(GetParent(hwnd),0), hwnd, uMsg, wParam, lParam);
}
//.........这里部分代码省略.........
RedrawWindow(gameEngine->getHWnd(), NULL, NULL,
RDW_INVALIDATE | RDW_UPDATENOW);
Sleep(500);
//
// See if we need additional cards.
//
while (true) {
std::vector<int>* vals = dealerHand->GetValues();
bool done = false;
for (int i = 0; i < vals->size(); i++) {
// Dealer must stop taking cards
// if he has a value of 17 or higher.
if (vals->at(i) >= 17) {
done = true;
break;
}
}
if (done) {
break;
}
PlaySound(L"sound-flipcard.wav", NULL, SND_FILENAME | SND_ASYNC);
dealerHand->dealCard(false);
RedrawWindow(gameEngine->getHWnd(), NULL, NULL,
RDW_INVALIDATE | RDW_UPDATENOW);
Sleep(500);
}
//
// Determine winner. Update balance amounts.
//
// table->setState(TABLE_STATE_READY);
std::vector<int>* dealerValues = dealerHand->GetValues();
std::vector<int>* playerValues = playerHand->GetValues();
int dealerFinal = 0;
int playerFinal = 0;
for (int i = 0; i < dealerValues->size(); i++) {
if (dealerValues->at(i) > dealerFinal &&
dealerValues->at(i) < 22) {
dealerFinal = dealerValues->at(i);
}
}
for (int i = 0; i < playerValues->size(); i++) {
if (playerValues->at(i) > playerFinal &&
playerValues->at(i) < 22) {
playerFinal = playerValues->at(i);
}
}
table->setState(TABLE_STATE_FINISHED);
// If values are same, this is a push.
if (dealerFinal == playerFinal) {
updateTextarea(hStaticTableMiddleMessage, "Push");
// Return player's bet money.
int bet = playerHand->getBetAmount();
user->setBalance(user->getBalance() + bet);
} else if (dealerFinal < playerFinal) {
// Player wins, return bet and winning.
updateTextarea(hStaticTableMiddleMessage, "Player Wins!");
int bet = playerHand->getBetAmount();
user->setBalance(user->getBalance() + (bet*2) );
}
else if (dealerFinal > playerFinal) {
// No need to update cash. Has already been deducted
// at time of bet. Update the middle message area.
updateTextarea(hStaticTableMiddleMessage, "Dealer Wins.");
}
RedrawWindow(gameEngine->getHWnd(), NULL, NULL,
RDW_INVALIDATE | RDW_UPDATENOW);
}
return CallWindowProc(oldStandButtonProc, hwnd, msg, wp, lp);
}
/*------------------------------------------------------------------------
Procedure: SubClassEdit ID:1
Purpose: Handles messages to the editbox
Input:
Output:
Errors:
--------------------------------------------------------------------------
Edit History:
14 Sept 2003 - Chris Watford [email protected]
- Setup handler for up and down arrows
15 Sept 2003 - Chris Watford [email protected]
- Setup framework for history on up arrow
- Saves lines you move off of in the edit buffer
16 Sept 2003 - Chris Watford [email protected]
- Proper handling of newline message finished
- Fixed ENTER on middle of interior line, moves cursor to the end
and sends the line
- Setup the copying and destroying of the old buffer
- Included buffer rewrite
17 Sept 2003 - Chris Watford [email protected]
- Added C-p/C-n support
- Changed UpArrow to C-UpArrow so as to not confuse users
18 Sept 2003 - Chris Watford [email protected]
- Added Left and Right arrow line saving
- Added backspace and delete line saving and removing
- Fixed history scrolling
21 Sept 2003 - Chris Watford [email protected]
- Fixed pasting errors associated with lines being out of bounds
for the buffer
- Added error handling, possibly able to handle it diff down the
line
- Removed C-Up/C-Dn for history scrolling, buggy at best on my
machine
------------------------------------------------------------------------*/
static LRESULT CALLBACK SubClassEdit(HWND hwnd, UINT msg, WPARAM mp1, LPARAM mp2)
{
LRESULT r;
int postit=0,nl;
if (msg == WM_CHAR && mp1 == '\r') {
if (!busy) {
r = GetCurLineIndex(hwnd);
nl = GetNumberOfLines(hwnd);
// if we're not the last line
if (r != nl-1)
{
// update or add us, we might not have any lines in the edit buffer
editbuffer_updateoraddline(CurrentEditBuffer, r-LastPromptPosition.line, GetLastLine(hwnd));
// scroll to the end, add CrLf then post the newline message
GotoEOF();
AddStringToControl("\r\n");
PostMessage(GetParent(hwnd),WM_NEWLINE,0,0);
return 0;
}
CallWindowProc(lpEProc,hwnd,WM_KEYDOWN,VK_END,1);
CallWindowProc(lpEProc,hwnd,WM_KEYUP,VK_END,1);
postit = 1;
}
}
else if (msg == WM_CHAR && mp1 == (char)0x08) {
int lineindex = SendMessage(hwnd, EM_LINEINDEX, LastPromptPosition.line, 0) + 2;
int curline = SendMessage(hwnd,EM_LINEFROMCHAR,(WPARAM)-1,0);
int nextline = 0;
int curpoint = 0;
SendMessage(hwnd, EM_GETSEL, (WPARAM)&curpoint, (LPARAM)NULL);
nextline = SendMessage(hwnd,EM_LINEFROMCHAR,(WPARAM)(curpoint - 1),0);
if(curpoint <= lineindex)
{
return 0;
} else if(nextline != curline) {
// delete the line we're on
// grab the index
curline -= LastPromptPosition.line;
// kill it
editbuffer_removeline(CurrentEditBuffer, curline);
}
}
else if (msg == WM_KEYDOWN && mp1 == VK_F1) {
DoHelp(hwnd);
}
else if ((msg == WM_KEYDOWN || msg == WM_KEYUP) && mp1 == VK_UP) {
int curline = GetCurLineIndex(hwnd);
/*if((msg == WM_KEYDOWN) && (GetKeyState(VK_CONTROL) && 0x8000))
{ // go forward once in history
NextHistoryEntry();
return 0;
} else */
if((curline > LastPromptPosition.line) && (curline <= (LastPromptPosition.line + CurrentEditBuffer->LineCount)))
{
// update current line
//.........这里部分代码省略.........
//.........这里部分代码省略.........
if (msg == WM_LBUTTONDOWN) {
TCHAR buff[64];
GetWindowText(hTextboxBetAmount, buff, 20);
int bet = _ttoi(buff);
/*
for (int i = 0; buff[i] != NULL; i++) {
if (! std::isdigit(buff[i]) ) {
MessageBox(
GameEngine::getInstance()->getHWnd(),
L"Invalid bet amount.",
L"Error",
NULL);
return 0;
}
}
*/
// int bet = atoi(buff);
Table* table = GameEngine::getInstance()->getTable();
User* user = GameEngine::getInstance()->getUser();
// Can player bet this much?
if (bet > user->getBalance()) {
MessageBox(
GameEngine::getInstance()->getHWnd(),
L"Insufficient funds available to place this bet.",
L"Error",
NULL);
return 0;
}
if (bet == 0) {
MessageBox(
GameEngine::getInstance()->getHWnd(),
L"Must bet more than $0.",
L"Error",
NULL);
return 0;
}
Hand* dealerHand = new Hand();
Hand* playerHand = new Hand();
playerHand->setBetAmount(bet);
user->setBalance(user->getBalance() - bet);
dealerHand->dealCard(true);
dealerHand->dealCard(false);
playerHand->dealCard(false);
playerHand->dealCard(false);
table->setDealerHand(dealerHand);
table->setPlayerHand(playerHand);
GameEngine::getInstance()->setState(GameEngine::STATE_PLAYING);
PlaySound(L"sound-chips.wav", NULL, SND_FILENAME | SND_ASYNC);
//
// Check if we have blackjack, if so player
// wins right away. Otherwise move to substate
// "playing"
//
if (playerHand->isBlackjack()) {
table->setState(TABLE_STATE_FINISHED);
updateTextarea(hStaticTableMiddleMessage, "Blackjack! Player Wins.");
// Play YAY sound
PlaySound(L"sound-yay.wav", NULL, SND_FILENAME | SND_ASYNC);
// Update user balance.
user->setBalance(user->getBalance() + (bet*2));
} else {
table->setState(TABLE_STATE_PLAYING);
}
//
// Force redraw of window, which should now render the new
// card data.
//
// https://msdn.microsoft.com/en-us/library/dd162911%28VS.85%29.aspx
// http://stackoverflow.com/questions/2325894/difference-between-invalidaterect-and-redrawwindow
//
RedrawWindow(GameEngine::getInstance()->getHWnd(), NULL, NULL,
RDW_INVALIDATE | RDW_UPDATENOW);
return 0;
}
return CallWindowProc(oldDealButtonProc, hwnd, msg, wp, lp);
}
请发表评论