-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics.c
171 lines (133 loc) · 4.03 KB
/
graphics.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
/* ____________________________________________________
|_| == Graphic Code == |_|
| You can find here all the source code of the |
| graphical part of Cruxxade-2600. |
| |
|_ <=========> _|
|_|________________________________________________|_|
\ ,__,
\ (oo)____
(__) )\
||--|| *
*/
#include "graphics.h"
//Even more Vicious Evil Globals Section
extern SDL_Rect hexaT[3];
extern SDL_Rect hexaBlack;
extern SDL_Rect Token[2];
extern SDL_Rect winText[3];
extern SDL_Rect closed;
void BlitWinner(SDL_Surface *sprite, SDL_Surface *screen, int *score){
SDL_Rect pos;
if(score[0]>score[1]){
pos.x=400-(winText[0].w/2);
pos.y=300-(winText[0].h/2);
SDL_BlitSurface(sprite, &winText[0], screen, &pos);
}
else if(score[0]==score[1]){
pos.x=400-(winText[2].w/2);
pos.y=300-(winText[2].h/2);
SDL_BlitSurface(sprite, &winText[2], screen, &pos);
}
else{
pos.x=400-(winText[1].w/2);
pos.y=300-(winText[1].h/2);
SDL_BlitSurface(sprite, &winText[1], screen, &pos);
}
}
vect GetOrigineHex(int size){
vect pos;
pos.x = 0.5*(800-hexaT[0].w);
pos.y = 0.5*(600-(size)*hexaT[0].h);
return pos;
}
void DrawOneHex(T_board board, SDL_Surface *screen, SDL_Surface *sprite, vect hex, int color){
SDL_Rect Pos, tokenPos;
vect initPos = GetOrigineHex(board.size);
initPos.x = 0.5*(800-hexaT[0].w);
initPos.y = 0.5*(600- (board.size)*hexaT[0].h);
Pos.x = (4.0/6.0)*hexaT[0].w*(hex.x-hex.y) + initPos.x;
Pos.y = 0.5*hexaT[0].h*(hex.x+hex.y) + initPos.y;
SDL_BlitSurface(sprite, &hexaT[color], screen, &Pos);
switch(board.grid[hex.x][hex.y].val){
case PLAYER_1:
tokenPos = (SDL_Rect){Pos.x+16,Pos.y+10,0,0};
SDL_BlitSurface(sprite, Token, screen, &tokenPos);
break;
case PLAYER_2:
tokenPos = (SDL_Rect){Pos.x+11,Pos.y+7,0,0};
SDL_BlitSurface(sprite, Token+1, screen, &tokenPos);
break;
case CLOSED:
tokenPos = (SDL_Rect){Pos.x+18, Pos.y+15};
SDL_BlitSurface(sprite, &closed, screen, &tokenPos);
break;
default :
break;
}
}
void BlitGameboard(T_board board, SDL_Surface* screen, SDL_Surface* sprite){
int i, j;
for(i=0;i<board.size;i++){
for(j=0;j<board.size;j++){
DrawOneHex(board, screen, sprite , (vect){i,j}, 0);
}
}
}
void EraseBoard(int size, SDL_Surface* screen, SDL_Surface* sprite){
SDL_Rect Pos;
vect initPos = GetOrigineHex(size);
int i, j;
initPos.x = 0.5*(800-hexaT[0].w);
initPos.y = 0.5*(600- size*hexaT[0].h);
for(i=0;i<size;i++){
for(j=0;j<size;j++){
Pos.x = (4.0/6.0)*hexaBlack.w*(i-j) + initPos.x;
Pos.y = 0.5*hexaBlack.h*(i+j) + initPos.y;
SDL_BlitSurface(sprite, &hexaBlack, screen, &Pos);
}
}
}
vect GetHexaCoor(T_board board, vect cursor){
vect initPos = GetOrigineHex(board.size), res;
double posX, posY, posReCentX, posReCentY, coefCurv, p;
int k=0;
initPos.x=initPos.x+(hexaT[0].w/2);
initPos.y=initPos.y;
posX = (cursor.x-initPos.x) / ((4.0/6.0)*hexaT[0].w);
posY = (cursor.y-initPos.y) / (0.5*hexaT[0].h);
k= (int)posX%2 + (int)posY%2;
coefCurv = 4.0/2.0;
p = 2.0/3.0;
posReCentX = posX - (int)posX;
posReCentY = posY - (int)posY;
if(posX>0){
switch(k){
case 1:
if(posReCentY < ((-coefCurv)*posReCentX+p))
posX=(int)posX+1;
break;
case 0:
case 2:
if(posReCentY < ((coefCurv*posReCentX)-p))
posX=(int)posX+1;
break;
}
}
else if(posX<0){
switch(k){
case 1:
if(posReCentY > (coefCurv*posReCentX+p))
posX=(int)posX-1;
break;
case 0:
case 2:
if(posReCentY < ((-coefCurv)*posReCentX-p))
posX=(int)posX-1;
break;
}
}
res.x = (int) (0.5*posX + 0.5*posY);
res.y = (int) (-0.5*posX + 0.5*posY);
return res;
}