-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
617 lines (524 loc) · 19.9 KB
/
main.c
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/**************************************************************************************\
*** Snake 1.3
*** Desenvolvedor: Abner Cordeiro ( Hellhound )
*** 2014 - Todos os Direitos reservados.
** Arquivo: main.c
** data: 21/09/2014 - Inicio do projeto
** 22/09/2014 - continuação
** 23/09/2014 - continuação
** 24/09/2014 - termino.
** Contém o principal do jogo,carrega a biblioteca FreeLibrary,trata mensagens do sistema
** e chama funções dos outros arquivos.
** Programando mesmo após a morte.
\**************************************************************************************/
// TODO (abner#1#09/25/14): Implementar comida bonus na proxima versão.
// TODO (abner#1#09/25/14): Implementar sistema de pontos original do jogo Snake
// TODO (abner#1#09/25/14): Implementar quantos pontos o jogador ganhou ao comer a comida Bonus ou normal
#define _WIN32_WINNT 0x0500
#define WINVER 0x0500
#include <windows.h >
#include <Windowsx.h>
#include <commctrl.h>
#include <stdio.h >
#include <time.h >
#include "FreeImage.h"
#include "main.h"
#include "game.h"
#include "init.h"
#include "resource.h"
GAME_INF GameInf;
/**********************----------------------------------------
** Retorna a tecla oposta,ex: baixo = cima,dã...
*********/
int Oposicao(WPARAM d)
{
switch(d)
{
case VK_LEFT :
return DIREITO;
break;// break só pra ficar bunitim
case VK_RIGHT:
return ESQUERDO;
break;
case VK_UP :
return BAIXO;
break;
case VK_DOWN :
return CIMA;
break;
default:
return 0;
}
}
/**********************----------------------------------------
** Função que pinta uma imagem aumentada ou diminuida
*********/
void BlendStretch(HDC hDC_Dest,
HBITMAP hBitmap,
int nXOriginDest,int nYOriginDest,
int nWidthDest ,int nHeightDest,
int nXOriginSrc ,int nYOriginSrc,
int nWidthSrc ,int nHeightSrc)
{
HDC hBitmapDC = CreateCompatibleDC (hDC_Dest);
SelectObject (hBitmapDC,hBitmap);
int iAntes = SetStretchBltMode(hDC_Dest, STRETCH_HALFTONE);
StretchBlt(hDC_Dest,
nXOriginDest,nYOriginDest,
nWidthDest,nHeightDest,
hBitmapDC,
nXOriginSrc,nYOriginSrc,
nWidthSrc,nHeightSrc,
SRCCOPY);
SetStretchBltMode(hDC_Dest, iAntes);
DeleteDC (hBitmapDC);
}
/**********************----------------------------------------
** Função que pinta a imagem deixando transparente.
*********/
void BlendAlpha(HDC hDC_Dest,
HBITMAP hBitmap,
BYTE ConstantAlpha,
int xoriginDest,int yoriginDest,
int wDest,int hDest,
int xoriginSrc,int yoriginSrc,
int wSrc,int hSrc)
{
HDC hBitmapDC = CreateCompatibleDC (hDC_Dest);
SelectObject (hBitmapDC,hBitmap);
BLENDFUNCTION BlendFunc;
BlendFunc.BlendOp = AC_SRC_OVER;
BlendFunc.BlendFlags = 0;
BlendFunc.SourceConstantAlpha = ConstantAlpha;
BlendFunc.AlphaFormat = AC_SRC_ALPHA;
AlphaBlend(hDC_Dest,
xoriginDest,yoriginDest,
wDest,hDest,
hBitmapDC,
xoriginSrc,yoriginSrc,
wSrc,hSrc,
BlendFunc);
DeleteDC(hBitmapDC);
}
/**********************----------------------------------------
** Função que pinta a imagem deixando a cor crTransparent transparente.
*********/
void BlendTransparent(HDC hDC_Dest,
HBITMAP hBitmap,
int xoriginDest,int yoriginDest,
int wDest,int hDest,
int xoriginSrc,int yoriginSrc,
int wSrc,int hSrc,UINT crTransparent)
{
HDC memDC = CreateCompatibleDC ( hDC_Dest );
SelectObject(memDC,hBitmap);
TransparentBlt(hDC_Dest,
xoriginDest,
yoriginDest,
wDest,
hDest,memDC,
xoriginSrc,yoriginSrc,
wSrc,hSrc,crTransparent);
DeleteDC(memDC);
}
/**********************----------------------------------------
** Zera as variaveis do jogo,e inicializa...
*********/
void NewGame(GAME_INF *pGameInf)
{
int x = 0,y = 0;
for(x = 0; x < TOTAL_TILES_LINHAS; x++)
for(y = 0; y < TOTAL_TILES_COLUNAS; y++)
pGameInf->iMatriz1[x][y] = 0;
x = 0;
y = 10;
/**********************----------------------------------------
** Colocando a cabeça da cobra
*********/
pGameInf->iDirecaoCorrent = BAIXO;
pGameInf->bPegou = TRUE;
pGameInf->Snake[x].iLinha = 11;
pGameInf->Snake[x].iColuna = 10;
pGameInf->Snake[x].iTipoPart = CABECA_3;
pGameInf->Snake[x].iDirecao = BAIXO;
x++;
/**********************----------------------------------------
** Laço que coloca o corpo da cobra
*********/
while(x < 10 )
{
pGameInf->Snake[x].iLinha = y;
pGameInf->Snake[x].iColuna = 10;
pGameInf->Snake[x].iTipoPart = CORPO_2;
pGameInf->Snake[x].iDirecao = BAIXO;
y--;
x++;
}
/**********************----------------------------------------
** Colocando o rabo da cobra
*********/
pGameInf->Snake[x].iLinha = y;
pGameInf->Snake[x].iColuna = 10;
pGameInf->Snake[x].iTipoPart = RABO_1;
pGameInf->Snake[x].iDirecao = BAIXO;
pGameInf->iNumSegmentos = x;
pGameInf->iNumTotalPoints = 0;
pGameInf->iSnakeVelocit = 6;
pGameInf->iCountBigFood = 0;
GeraMaca(pGameInf,&pGameInf->Food.x,&pGameInf->Food.y );
}
/**********************----------------------------------------
** Verifica o que é para mostrar e mostra!.
*********/
void showgame()
{
switch(GameInf.iGameStatus)
{
case GAME_MENU:
ShowMenu(&GameInf);
break;
case GAME_RUNNING:
ShowGame(&GameInf);
break;
}
}
/**********************----------------------------------------
** É chamada quando o sistema têm algo interessante para nós...
*********/
LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch(uMsg)
{
/**********************----------------------------------------
** Ao chamar CreateWindow se sucesso ele manda a mensagem
** WM_CREATE para a proc registrada na classe criada,heheh
*********/
case WM_CREATE:
{
/**********************----------------------------------------
** Inicializa a biblioteca FreeImage,carrega imagens,converte,
** e desaloca recursos não mais utilizados...
*********/
FreeImage_Initialise(FALSE);
GameInf.hwnd = hwnd;
GetClientRect(hwnd,&GameInf.rc);
NewGame(&GameInf);
HDC hdc = GetDC(hwnd);
GameInf.hDC_Screen = CreateCompatibleDC ( hdc );
GameInf.hBitmapScreen = CreateCompatibleBitmap ( hdc, GameInf.rc.right, GameInf.rc.bottom );
SelectObject ( GameInf.hDC_Screen, GameInf.hBitmapScreen );
ReleaseDC(hwnd,hdc);
LoadResources(&GameInf);
GameInf.hFonte1 = CreateFont(17,7,0,0,FW_MEDIUM,
FALSE,FALSE,FALSE,
ANSI_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
PROOF_QUALITY,
DEFAULT_PITCH,
"Comic Sans MS");
GameInf.hFonte2 = CreateFont(18,8,0,0,FW_EXTRABOLD,
FALSE,FALSE,FALSE,
ANSI_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
PROOF_QUALITY,
DEFAULT_PITCH,
"Comic Sans MS");
SetTimer(hwnd,TIMER_GAME,2,NULL);
}
break;
/**********************----------------------------------------
** Algum botão pressionado!!!!!!
*********/
case WM_KEYDOWN:
{
if(GameInf.iGameStatus == GAME_MENU)
{
if(wParam == VK_UP ) GameInf.iMenuSelected = 0;
if(wParam == VK_DOWN) GameInf.iMenuSelected = 1;
if(wParam == VK_RETURN)
{
if(GameInf.iMenuSelected == 0) GameInf.iGameStatus = GAME_RUNNING;
else SendMessage(hwnd,WM_CLOSE,0,0);
}
}
else if(GameInf.iGameStatus != GAME_MENU)
{
/**********************----------------------------------------
** Usuario apertou botão F1 dentro do jogo ( PAUSA O JOGO )
*********/
if(wParam == VK_F1)
{
GameInf.iGameStatus = (GameInf.iGameStatus == GAME_PAUSED) ? GAME_RUNNING : GAME_PAUSED;
}
if((GameInf.iGameStatus != GAME_PAUSED) && (GameInf.bPegou == 1) )
{
/**********************----------------------------------------
** Usuario apertou botão esquerdo dentro do jogo
*********/
if(wParam == VK_LEFT && (GameInf.iDirecaoCorrent != Oposicao(wParam)) && (GameInf.iDirecaoCorrent != ESQUERDO) )
{
GameInf.Snake[0].iLinha = GameInf.Snake[1].iLinha;
GameInf.Snake[0].iColuna = GameInf.Snake[1].iColuna;
if(GameInf.iDirecaoCorrent == BAIXO)
{
GameInf.Snake[0].iColuna--;
GameInf.Snake[1].iVertice = VERTICE_1;
}
if(GameInf.iDirecaoCorrent == CIMA )
{
GameInf.Snake[0].iColuna--;
GameInf.Snake[1].iVertice = VERTICE_4;
}
GameInf.Snake[0].iDirecao = ESQUERDO;
GameInf.Snake[0].iTipoPart = CABECA_4;
GameInf.Snake[1].iTipoPart = CORPO_1;
GameInf.iDirecaoCorrent = ESQUERDO;
GameInf.bPegou = FALSE;
}
/**********************----------------------------------------
** Usuario apertou botão para cima dentro do jogo
*********/
if(wParam == VK_UP && (GameInf.iDirecaoCorrent != Oposicao(wParam))&& (GameInf.iDirecaoCorrent != CIMA))
{
GameInf.Snake[0].iLinha = GameInf.Snake[1].iLinha;
GameInf.Snake[0].iColuna = GameInf.Snake[1].iColuna;
if(GameInf.iDirecaoCorrent == DIREITO )
{
GameInf.Snake[0].iLinha--;
GameInf.Snake[1].iVertice = VERTICE_1;
}
if(GameInf.iDirecaoCorrent == ESQUERDO)
{
GameInf.Snake[0].iLinha--;
GameInf.Snake[1].iVertice = VERTICE_2;
}
GameInf.Snake[0].iDirecao = CIMA;
GameInf.Snake[0].iTipoPart = CABECA_1;
GameInf.Snake[1].iTipoPart = CORPO_2;
GameInf.iDirecaoCorrent = CIMA;
GameInf.bPegou = FALSE;
}
/**********************----------------------------------------
** Usuario apertou botão direito dentro do jogo
*********/
if(wParam == VK_RIGHT && (GameInf.iDirecaoCorrent != Oposicao(wParam))&& (GameInf.iDirecaoCorrent != DIREITO))
{
GameInf.Snake[0].iLinha = GameInf.Snake[1].iLinha;
GameInf.Snake[0].iColuna = GameInf.Snake[1].iColuna;
if(GameInf.iDirecaoCorrent == BAIXO)
{
GameInf.Snake[0].iColuna++;
GameInf.Snake[1].iVertice = VERTICE_2;
}
if(GameInf.iDirecaoCorrent == CIMA )
{
GameInf.Snake[0].iColuna++;
GameInf.Snake[1].iVertice = VERTICE_3;
}
GameInf.Snake[0].iDirecao = DIREITO;
GameInf.Snake[0].iTipoPart = CABECA_2;
GameInf.Snake[1].iTipoPart = CORPO_1;
GameInf.iDirecaoCorrent = DIREITO;
GameInf.bPegou = FALSE;
}
/**********************----------------------------------------
** Usuario apertou botão para baixo dentro do jogo
*********/
if(wParam == VK_DOWN && (GameInf.iDirecaoCorrent != Oposicao(wParam))&& (GameInf.iDirecaoCorrent != BAIXO))
{
GameInf.Snake[0].iLinha = GameInf.Snake[1].iLinha;
GameInf.Snake[0].iColuna = GameInf.Snake[1].iColuna;
if(GameInf.iDirecaoCorrent == DIREITO )
{
GameInf.Snake[1].iVertice = VERTICE_4;
GameInf.Snake[0].iLinha++;
}
if(GameInf.iDirecaoCorrent == ESQUERDO)
{
GameInf.Snake[1].iVertice = VERTICE_3;
GameInf.Snake[0].iLinha++;
}
GameInf.Snake[0].iDirecao = BAIXO;
GameInf.Snake[0].iTipoPart = CABECA_3;
GameInf.Snake[1].iTipoPart = CORPO_2;
GameInf.iDirecaoCorrent = BAIXO;
GameInf.bPegou = FALSE;
}
if(GameInf.Snake[0].iLinha >= TOTAL_TILES_LINHAS )GameInf.Snake[0].iLinha = 0;
if(GameInf.Snake[0].iColuna >= TOTAL_TILES_COLUNAS)GameInf.Snake[0].iColuna = 0;
}
}
}
break;
/**********************----------------------------------------
** Timer usado para contar o tempo para pintar,e processar as
** informações do jogo.
*********/
case WM_TIMER:
{
if (wParam == TIMER_GAME)
{
HDC hdc = GetDC(hwnd);
showgame( hwnd,GameInf.hDC_Screen );
BitBlt(hdc,0,0,GameInf.rc.right, GameInf.rc.bottom,GameInf.hDC_Screen,0, 0,SRCCOPY);
ReleaseDC(hwnd,hdc);
}
}
break;
/**********************----------------------------------------
** Solicitação de atualização da area cliente da Window.
*********/
case WM_PAINT:
{
PAINTSTRUCT ps;
BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
}
break;
/**********************----------------------------------------
** Usuario quer fechar o jogo,desaloca tudo relacionado ao jogo.
*********/
case WM_CLOSE:
{
KillTimer (hwnd, TIMER_GAME );
DeleteDC (GameInf.hDC_Screen );
DeleteObject(GameInf.hBitmapScreen);
DeleteObject(GameInf.hFonte1);
DeleteObject(GameInf.hFonte2);
free(GameInf.pRecords);
UnloadResources(&GameInf);
FreeImage_DeInitialise();
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return FALSE;
}
INT_PTR CALLBACK DialogAddRecordProc( HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
static HWND hRecord;
static HWND hName;
static GAME_INF *pGameInf;
char szBuff[MAX_PATH];
switch(uMsg)
{
case WM_INITDIALOG:
{
hRecord = GetDlgItem(hwnd,TEXT_RECORD);
hName = GetDlgItem(hwnd,EDIT_NAME );
pGameInf = (GAME_INF*)lParam;
sprintf(szBuff,"%d",pGameInf->iNumTotalPoints);
SetWindowText(hRecord,szBuff);
}
return TRUE;
break;
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case BUTTON_OK:
{
GetWindowText(hName,(pGameInf->pRecords[0]).szNome,MAX_PATH);
EndDialog(hwnd,0);
}
break;
}
}
return TRUE;
break;
case WM_CLOSE:
{
EndDialog(hwnd,0);
}
break;
}
return FALSE;
}
BOOL ShowDialogAddRecord(GAME_INF *pGameInf)
{
DialogBoxParam(pGameInf->hInst,MAKEINTRESOURCE(IDD_ADD_RECORD),pGameInf->hwnd,DialogAddRecordProc,(LPARAM)pGameInf);
//FIX
return TRUE;
}
PRECORDS LoadRecord(GAME_INF *pGameInf)
{
pGameInf->pRecords = (PRECORDS)malloc( (sizeof(RECORDS)* 6) );
FILE *pFile = fopen("snake.txt","rb+" );
if(pFile == NULL)
{
pFile = fopen("snake.txt","wb+" );
ZeroMemory(pGameInf->pRecords, (sizeof(RECORDS)* 6));
(pGameInf->pRecords[0]).iRecord = (pGameInf->pRecords[1]).iRecord = (pGameInf->pRecords[3]).iRecord = (pGameInf->pRecords[4]).iRecord = (pGameInf->pRecords[5]).iRecord = 0;
fwrite( pGameInf->pRecords, sizeof(RECORDS),6, pFile );
}
fread( pGameInf->pRecords,sizeof(RECORDS),6,pFile );
fclose(pFile);
return pGameInf->pRecords;
}
PRECORDS SaveRecord(GAME_INF *pGameInf)
{
FILE *pFile = fopen("snake.txt","rb+" );
if(pFile == NULL)
{
pFile = fopen("snake.txt","wb+" );
}
fwrite( pGameInf->pRecords, sizeof(RECORDS),6, pFile );
fclose(pFile);
return pGameInf->pRecords;
}
/**********************----------------------------------------
** Inicio...do pesadelo.
*********/
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
//PRECORDS pRecords =
//LoadRecord(&GameInf);
// salvamos isso,vai que precisa neh...
GameInf.hInst = hInstance;
// isso tava por padrão caso fosse programar GUI,mas de boa.
InitCommonControls();
WNDCLASSEX wincl;
MSG msg;
HWND hwnd;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.style = CS_DBLCLKS;
wincl.lpfnWndProc = WindowProc;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hInstance = hInstance;
wincl.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE(IDI_ICON1 ));
wincl.hCursor = LoadCursor (hInstance, MAKEINTRESOURCE(IDI_CURSOR1));
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
wincl.lpszMenuName = NULL;
wincl.lpszClassName = WINCLASS_STR;
wincl.hIconSm = LoadIcon (hInstance, MAKEINTRESOURCE(IDI_ICON1));
/**********************----------------------------------------
** Registra a Classe
*********/
if(!RegisterClassEx (&wincl)) return 0;
// inicializa a parada aleatória manolo.
srand(time(NULL));
AddFontResource("comicbd.ttf");
hwnd = CreateWindow(WINCLASS_STR,WINTITLE,
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU,
(GetSystemMetrics(SM_CXSCREEN) - WIN_WIDTH_DEBUG) / 2,
(GetSystemMetrics(SM_CYSCREEN) - WIN_HEIGHT_DEBUG) / 2,
WIN_WIDTH_DEBUG,WIN_HEIGHT_DEBUG, HWND_DESKTOP,NULL,hInstance,NULL);
ShowWindow(hwnd,nShowCmd);
/**********************----------------------------------------
** Mostra a janela criada,e espera eventos...
*********/
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage (&msg);
}
return (int)msg.wParam;
}