-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCGameDlg.cpp
217 lines (185 loc) · 4.38 KB
/
CGameDlg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// CGameDlg.cpp: 实现文件
//
#include "pch.h"
#include "Tetris.h"
#include "CGameDlg.h"
#include "afxdialogex.h"
#include "Log.h"
#include"GameProcess.h"
#include"GameManager.h"
#include "CGlobe.h"
#include "CPauseDlg.h"
#include "CEndDlg.h"
// CGameDlg 对话框
IMPLEMENT_DYNAMIC(CGameDlg, CDialogEx)
CGameDlg::CGameDlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_GAME_DIALOG, pParent), _Game(NULL)
, _Diff(_T(""))
, _Score(_T(""))
{
}
CGameDlg::~CGameDlg()
{
}
void CGameDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_GAME_WINDOW, GameArea);
DDX_Control(pDX, IDC_NEXT_BRICK_PIC, NextArea);
DDX_Text(pDX, IDC_DIFF_TEXT, _Diff);
DDX_Text(pDX, IDC_SCORE_TEXT, _Score);
}
BEGIN_MESSAGE_MAP(CGameDlg, CDialogEx)
// ON_WM_SHOWWINDOW()
ON_WM_CTLCOLOR()
ON_WM_HELPINFO()
// ON_WM_KEYDOWN()
// ON_WM_KEYUP()
ON_WM_TIMER()
ON_WM_PAINT()
// ON_WM_KILLFOCUS()
END_MESSAGE_MAP()
// CGameDlg 消息处理程序
BOOL CGameDlg::DestroyWindow()
{
Globe.KEY_UP = FALSE;
Globe.KEY_DOWN = FALSE;
Globe.KEY_LEFT = FALSE;
Globe.KEY_RIGHT = FALSE;
Globe.KEY_PAUSE = FALSE;
GameSDL::ButtonDownSound();
KillTimer(17);
// TODO: 在此添加专用代码和/或调用基类
delete _Game;
delete _GProcess;
LOG_DEBUG("GameDlg Destroy!");
if (Globe.dlg != NULL) {
Globe.dlg->ShowWindow(SW_SHOWNORMAL);
}
return CDialogEx::DestroyWindow();
}
BOOL CGameDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
HICON m_hIcon = AfxGetApp()->LoadIconW(IDR_MAINFRAME);
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE);
GameArea.SetWindowPos(NULL, 0, 0, 300, 600, SWP_NOMOVE);
NextArea.SetWindowPos(NULL, 0, 0, 120, 120, SWP_NOMOVE);
CRect rect, rectClient;
GetWindowRect(rect);
GetClientRect(rectClient);
SetWindowPos(NULL, 0, 0, 465, rect.Height() - rectClient.Height() + 600, SWP_NOMOVE);
_Game = new GameSDL(GameArea.GetSafeHwnd(), NextArea.GetSafeHwnd());
_GProcess = new GameProcess(_Game);
_GProcess->TetrisManger.NewGame();
_GProcess->DrawNext();
SetTimer(17, 10, NULL);
_Pause = false;
_Exit = false;
// TODO: 在此添加额外的初始化
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
HBRUSH CGameDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
auto i = pWnd->GetDlgCtrlID();
if (i == IDC_GAME_WINDOW || i == IDC_NEXT_BRICK_PIC) {
_Game->MainRendererPresent();
_Game->NextRendererPresent();
}
switch (nCtlColor)
{
case CTLCOLOR_STATIC:
{
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(RGB(200, 200, 200));
HBRUSH B = CreateSolidBrush(RGB(46, 50, 58));
return (HBRUSH)B;
}
}
// TODO: 在此更改 DC 的任何特性
// TODO: 如果默认的不是所需画笔,则返回另一个画笔
return hbr;
}
BOOL CGameDlg::OnHelpInfo(HELPINFO* pHelpInfo)
{
return TRUE;
}
BOOL CGameDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: 在此添加专用代码和/或调用基类
if (pMsg->message == WM_KEYDOWN)
{
if (pMsg->wParam == VK_UP)
{
Globe.KEY_UP = TRUE;
return TRUE;
}
if (pMsg->wParam == VK_DOWN)
{
Globe.KEY_DOWN = TRUE;
return TRUE;
}
if (pMsg->wParam == VK_LEFT)
{
Globe.KEY_LEFT = TRUE;
return TRUE;
}
if (pMsg->wParam == VK_RIGHT)
{
Globe.KEY_RIGHT = TRUE;
return TRUE;
}
if (pMsg->wParam == VK_ESCAPE) {
GameSDL::ButtonDownSound();
KillTimer(17);
_Pause = true;
CPauseDlg Dlg(_GProcess->score, &_Exit);
Dlg.DoModal();
if (_Exit) {
this->OnOK();
}
_Pause = false;
SetTimer(17, 10, NULL);
return TRUE;
}
if (pMsg->wParam == VK_RETURN) {
return TRUE;
}
}
if (pMsg->message == WM_KEYUP) {
if (pMsg->wParam == VK_DOWN)
{
Globe.KEY_DOWN = FALSE;
return TRUE;
}
}
return CDialogEx::PreTranslateMessage(pMsg);
}
void CGameDlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if (nIDEvent == 17) {
_Score.Format(_T("%d"), _GProcess->score);
int i = _GProcess->score / 1000;
_Diff.Format(_T("%d"), i < 7 ? i + 1 : 7);
UpdateData(FALSE);
if (_GProcess->Around()) {
KillTimer(17);
CEndDlg dlg(_GProcess->score);
dlg.DoModal();
this->OnOK();
}
return;
}
CDialogEx::OnTimer(nIDEvent);
}
void CGameDlg::OnPaint()
{
CRect rect;
CPaintDC dc(this);
GetClientRect(rect);
dc.FillSolidRect(rect, RGB(46, 50, 58));
}