-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain_Window.cpp
More file actions
439 lines (407 loc) · 11.5 KB
/
Main_Window.cpp
File metadata and controls
439 lines (407 loc) · 11.5 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#include "Main_Window.h"
#include "Game.h"
static SDL_Color WHITE = { 255,255,255,255 };//白色
static SDL_Color BLACK = { 0,0,0,255 };//黑色
static SDL_Color RED = { 255,0,0,255 };//红色
static SDL_Color BLUE = { 0,0,255,255 };//蓝色
static SDL_Color LITTLE_BLACK = { 200,200,200,255 };//灰色
int Main_Window::GameIsEnd(int flag_Player_)
{
game->gameIsRun = false;
flag_Player = flag_Player_;
flagWindow = GameoverWindow;
SDL_DestroyWindow(window);
CreatNewWindow("Snake", 300, 300, 200, 200);
game->DestroyPlay();
return 0;
}
int Main_Window::CreatNewWindow(const char* title, int x, int y, int w, int h)
{
if (window) {
SDL_DestroyWindow(window);
}
//创建窗口
window = SDL_CreateWindow(title, x, y, w, h, SDL_WINDOW_SHOWN);
//检查
if (!window) {
SDL_Log("%s", SDL_GetError());
return Window_error;
}
//获取窗口对应的贴图器
windowSurface = SDL_GetWindowSurface(window);
//创建着色器
renderer = SDL_CreateRenderer(window, -1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
//检查
if (!renderer) {
SDL_Log("%s", SDL_GetError());
return Renderer_error;
}
return All_true;
}
int Main_Window::CreatWindow(Game* game_, class Player* red_player_,
Player* blue_player_)
{
game = game_;
input = game->GetInput();
red_player = red_player_;
blue_player = blue_player_;
mouse_window = new Mouse_Window(input, this);
//初始化TTF文字库
if (TTF_Init() == -1) {
SDL_Log("%s", SDL_GetError());
return TTF_Init_error;
}
//加载TTF文字
font = TTF_OpenFont(fontText, fontSize);
if (!font) {
SDL_Log("%s", SDL_GetError());
return TTF_Open_error;
}
return 0;
}
int Main_Window::Shutdown()
{
TTF_CloseFont(font);
TTF_Quit();
SDL_DestroyWindow(window);
return 0;
}
int Main_Window::Player_Window(const wchar_t* text_, SDL_Rect rect_)
{
Window_Msg* temp = new Window_Msg();
temp->text = text_;
temp->rect = rect_;
temp->liveTime = 0;
window_msg.push_back(temp);
return 0;
}
int Main_Window::Updata()
{
if (!window) {
CreatNewWindow("Snake", 100, 100, 500, 500);
flagWindow = MainWindow;
}
//循环背景色
SDL_SetRenderDrawColor(renderer, 240, 240, 240, 255);
SDL_RenderClear(renderer);
DrawLineColor(0, 0, 700, 0, BLACK);
//绘制一条小蛇
mouse_window->DrawSnake();
switch (flagWindow)
{
case MainWindow://首界面
Draw_MainWindow();
break;
case FrightWindow://战斗界面
Draw_FrightWindow();
break;
case GameoverWindow://游戏结束界面
Draw_GameOverWindow();
break;
case SkillWindow://技能选择界面
Draw_SkillWindow();
break;
}
//绘制缓冲区
SDL_RenderPresent(renderer);
return 0;
}
int Main_Window::Draw_FrightWindow()
{
flagWindow = FrightWindow;
//黑色
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
//绘制战斗窗口
int i = 0;
while (i <= 700) {
//Y
DrawLine(i, 0, i, 700);
//X
DrawLine(0, i, 700, i);
i += 100;
}
DrawLine(350, 700, 350, 800);
int jidi_h = 50;//基地矩形边长
vector<Point*>* jidi_point;
vector<Point*>* move_point;
//绘制玩家路线
move_point = red_player->Getmove_point();//获取红方移动数组
jidi_point = red_player->Getjidi_point();//获取红方基地数组
//绘制红方基地
if (jidi_point->at(0)->x > 0) {
FillRect(jidi_point->at(0)->x, jidi_point->at(0)->y, jidi_h, jidi_h, &RED);
}
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);//设置着色器颜色为红色
int now = 1;
int jidi_index = 1;//基地索引
//遍历移动数组并绘制
for (int before = 0; now < (*move_point).size(); now++) {
//特殊标识检测
if ((*move_point).at(now)->x == 0) {
FillRect((*jidi_point).at(jidi_index)->x, (*jidi_point).at(jidi_index)->y, jidi_h, jidi_h, &RED);
}
else if ((*move_point).at(before)->x == 0) {
DrawLine(jidi_point->at(jidi_index)->x + 25, jidi_point->at(jidi_index)->y,
(*move_point).at(now)->x, (*move_point).at(now)->y);
jidi_index++;//为下一次绘制矩形做准备
}
else {
DrawLine((*move_point).at(before)->x, (*move_point).at(before)->y,
(*move_point).at(now)->x, (*move_point).at(now)->y);
}
before++;
}
move_point = blue_player->Getmove_point();//获取蓝方移动数组
jidi_point = blue_player->Getjidi_point();//获取蓝方基地数组
//绘制蓝方基地
if (jidi_point->at(0)->x > 0) {
FillRect(jidi_point->at(0)->x, jidi_point->at(0)->y, jidi_h, jidi_h, &BLUE);
}
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);//设置着色器颜色为蓝色
now = 1;
jidi_index = 1;//基地索引
//遍历移动数组并绘制
for (int before = 0; now < (*move_point).size(); now++) {
//特殊标识检测
if ((*move_point).at(now)->x == 0) {
FillRect((*jidi_point).at(jidi_index)->x, (*jidi_point).at(jidi_index)->y, jidi_h, jidi_h, &RED);
}
else if ((*move_point).at(before)->x == 0) {
DrawLine(jidi_point->at(jidi_index)->x + 25, jidi_point->at(jidi_index)->y,
(*move_point).at(now)->x, (*move_point).at(now)->y);
jidi_index++;//为下一次绘制矩形做准备
}
else {
DrawLine((*move_point).at(before)->x, (*move_point).at(before)->y,
(*move_point).at(now)->x, (*move_point).at(now)->y);
}
before++;
}
//绘制玩家预选框
Point temp;
temp = red_player->Getyuxuan_point();//获取红方预选框坐标
FillRect(temp.x, temp.y, 20, 20, &RED);
temp = blue_player->Getyuxuan_point();//获取蓝方预选框坐标
FillRect(temp.x, temp.y, 20, 20, &BLUE);
//绘制玩家提示窗口
int size = window_msg.size();
if (size > 0)
{
for (int i = 0; i < size; i++)
{
if (window_msg[i]->liveTime > 100) {
//每个提示框循环绘制99次 绘制完成后将其删除
window_msg.erase(
window_msg.begin() + i, window_msg.begin() + i + 1);
break;//跳出循环
}
FillRect(window_msg[i]->rect.x, window_msg[i]->rect.y,
window_msg[i]->rect.w, window_msg[i]->rect.h, &LITTLE_BLACK);
DrawRect(window_msg[i]->rect.x, window_msg[i]->rect.y,
window_msg[i]->rect.w, window_msg[i]->rect.h, &BLACK);
DrawTTF(window_msg[i]->text, BLACK, window_msg[i]->rect);
window_msg[i]->liveTime++;
}
}
return 0;
}
int Main_Window::Draw_MainWindow()
{
flagWindow = MainWindow;
//DrawRect(200, 100, 100, 50, &BLACK);
DrawTTF(L"开始游戏", BLACK, { 200,100,100,40 });
DrawLine(180, 150, 320, 150);
//DrawRect(200, 200, 100, 50, &BLACK);
DrawTTF(L"技能介绍", BLACK, { 200,200,100,40 });
DrawLine(180, 250, 320, 250);
//获取鼠标坐标
Point* nowPoint = input->GetNowPoint();
//判断鼠标是否进入“开始游戏”按钮区
if (nowPoint->x < 300 && nowPoint->x > 200
&& nowPoint->y > 100 && nowPoint->y < 150)
{
//移动到键上
FillRect(150, 120, 20, 20, &RED);
//按下键
if (input->GetMouseState(SDL_BUTTON_LEFT) == Key_Down)
{
flagWindow = SkillWindow;
game->CreatePlay();//创建玩家
CreatNewWindow("Player1 chiose skill", 100, 100, 300, 300);
}
}
else if (nowPoint->x < 300 && nowPoint->x > 200
&& nowPoint->y > 200 && nowPoint->y < 250)
{
//移动到键上
FillRect(150, 220, 20, 20, &RED);
//按下键
if (input->GetMouseState(SDL_BUTTON_LEFT) == Key_Down)
{
flagWindow = FrightWindow;
CreatNewWindow("Snake", 100, 100, 700, 800);
game->CreatePlay();
}
}
return 0;
}
int Main_Window::Draw_GameOverWindow()
{
SDL_Rect rect = { 50,30,100,50 };
flagWindow = GameoverWindow;
if (flag_Player == Red_Player) {
DrawTTF(L"蓝方胜利", BLUE, rect);
}
else {
DrawTTF(L"红方胜利", RED, rect);
}
DrawTTF(L"左键回到主页", BLACK, { 20,100,160,50 });
if (input->GetMouseState(SDL_BUTTON_LEFT) == Key_Down) {
CreatNewWindow("Snake", 100, 100, 500, 500);
flagWindow = MainWindow;
}
return 0;
}
int Main_Window::Draw_SkillWindow()
{
flagWindow = SkillWindow;
//选中正方形的边
int h = 20;
SDL_Rect name1 = { 100,20,90,40 };//技能一矩形
SDL_Rect name2 = { 100,90,90,40 };//技能二矩形
DrawLine(name1.x, name1.y + name1.h + 10, name1.x + name1.w, name1.y + name1.h + 10);
DrawLine(name2.x, name2.y + name2.h + 10, name2.x + name2.w, name2.y + name2.h + 10);
const wchar_t* skill_name = game->Getskill_name(index);
if (skill_name) { DrawTTF(skill_name, BLACK, name1); }
skill_name = game->Getskill_name(index + 1);
if (skill_name) { DrawTTF(skill_name, BLACK, name2); }
SDL_Rect text1 = { 0,250,fontSize * 3,40 };//文本一矩形
SDL_Rect text2 = { 300 - fontSize * 3,250,fontSize * 3,40 };//文本二矩形
DrawLine(text1.x, text1.y + text1.h + 10, text1.x + text1.w, text1.y + text1.h + 10);
DrawLine(text2.x, text2.y + text2.h + 10, text2.x + text2.w, text2.y + text2.h + 10);
DrawTTF(L"上一页", BLACK, text1);
DrawTTF(L"下一页", BLACK, text2);
//获取鼠标坐标
Point* nowPoint = input->GetNowPoint();
//判断鼠标是否进入按钮区
//第一个技能区
if (nowPoint->x > name1.x && nowPoint->x < name1.x + name1.w
&& nowPoint->y > name1.y && nowPoint->y < name1.y + name1.h + 10)
{
//移动到键上
FillRect(name1.x - h * 2, name1.y + 10, h, h, &RED);
//按下键
if (input->GetMouseState(SDL_BUTTON_LEFT) == Key_Down)
{
if (red_player->Getskill_flag() == -1) {
//修改玩家一的技能选择
red_player->ChangeSkill(index);
//轮到玩家二选择技能
flagWindow = SkillWindow;
index = 1;
CreatNewWindow("Player2 chiose skill", 100, 100, 300, 300);
}
else {
blue_player->ChangeSkill(index);
flagWindow = FrightWindow;
CreatNewWindow("Snake Firght", 100, 100, 700, 800);
}
}
}
//第二个技能区
else if (nowPoint->x > name2.x && nowPoint->x < name2.x + name2.w
&& nowPoint->y > name2.y && nowPoint->y < name2.y + name2.h + 10)
{
//移动到键上
FillRect(name2.x - h * 2, name2.y + 10, h, h, &RED);
//按下键
if (input->GetMouseState(SDL_BUTTON_LEFT) == Key_Down)
{
if (red_player->Getskill_flag() == -1) {
//修改玩家一的技能选择
red_player->ChangeSkill(index + 1);
//轮到玩家二选择技能
index = 1;//重置索引
CreatNewWindow("Player2 chiose skill", 100, 100, 300, 300);
}
else {
blue_player->ChangeSkill(index + 1);
flagWindow = FrightWindow;
CreatNewWindow("Snake Firght", 100, 100, 700, 800);
}
}
}
//上一页
else if (nowPoint->x > text1.x && nowPoint->x < text1.x + text1.w
&& nowPoint->y > text1.y && nowPoint->y < text1.y + text1.h + 10)
{
//移动到键上
FillRect(text1.x + text1.w + h, text1.y + 10, h, h, &RED);
//按下键
if (input->GetMouseState(SDL_BUTTON_LEFT) == Key_Down)
{
if (index > 1) { index -= 2; }
}
}
//下一页
else if (nowPoint->x > text2.x && nowPoint->x < text2.x + text2.w
&& nowPoint->y > text2.y && nowPoint->y < text2.y + text2.h + 10)
{
//移动到键上
FillRect(text2.x - h * 2, text2.y + 10, h, h, &RED);
//按下键
if (input->GetMouseState(SDL_BUTTON_LEFT) == Key_Down)
{
if (index + 2 <= game->Getskill_sum()) { index += 2; }
}
}
return 0;
}
void Main_Window::DrawLine(int x1, int y1, int x2, int y2)
{
//绘制线
SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
}
void Main_Window::DrawLineColor(int x1, int y1, int x2, int y2, SDL_Color color)
{
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
DrawLine(x1, y1, x2, y2);
}
void Main_Window::DrawRect(int x, int y, int w, int h, SDL_Color* color)
{
//设置矩形信息
rect.x = x;
rect.y = y;
rect.w = w;
rect.h = h;
//设置填充颜色
SDL_SetRenderDrawColor(renderer, color->r, color->g, color->b, color->a);
//绘制矩形
SDL_RenderDrawRect(renderer, &rect);
}
void Main_Window::FillRect(int x, int y, int w, int h, SDL_Color* color)
{
//设置矩形信息
rect.x = x;
rect.y = y;
rect.w = w;
rect.h = h;
//设置填充颜色
SDL_SetRenderDrawColor(renderer, color->r, color->g, color->b, color->a);
//绘制矩形
SDL_RenderFillRect(renderer, &rect);
}
void Main_Window::DrawTTF(const wchar_t* text, SDL_Color color, SDL_Rect rect)
{
//将wchar_t转化为Uint16
Uint16* inText = (Uint16*)text;
//将字体信息转化为贴图器信息
SDL_Surface* fontSurface = TTF_RenderUNICODE_Solid(font, inText, color);
//将贴图器信息转化为纹理信息
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, fontSurface);
//释放贴图器内存
SDL_FreeSurface(fontSurface);
//将纹理信息绘制到窗口
SDL_RenderCopy(renderer, texture, 0, &rect);
}