This repository has been archived by the owner on May 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.c
165 lines (140 loc) · 5.98 KB
/
render.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
#include "render.h"
const SDL_Color GRID_COLOR = {.r = 255, .g = 255 , .b = 255};
const SDL_Color X_COLOR = {.r = 255, .g = 0, .b = 255};
const SDL_Color O_COLOR = {.r = 0, .g = 0, .b = 255};
const SDL_Color WIN_COLOR = { .r = 0, .g = 255, .b = 0};
const SDL_Color LOSE_COLOR = { .r = 255 , .g = 0, .b = 0};
const SDL_Color TIE_COLOR = { .r = 100, .g = 100, .b = 100};
void render_grid(SDL_Renderer * renderer, const SDL_Color *color){
SDL_SetRenderDrawColor(renderer, color->r, color->g, color->b, 255);
int i;
for (i = 1; i < 9; i++){
SDL_RenderDrawLine(renderer, CELL_WIDTH * i, 0, i * CELL_WIDTH, SCREEN_HEIGHT);
}
for (i = 1; i < 9; i++){
SDL_RenderDrawLine(renderer, 0, CELL_HEIGHT * i, SCREEN_WIDTH, i * CELL_HEIGHT);
}
SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255);
for (i = 1; i < 3; i++){
SDL_RenderDrawLine(renderer, 3 * CELL_WIDTH * i, 0, 3 * CELL_WIDTH * i, SCREEN_HEIGHT);
}
for (i = 1; i < 3; i++){
SDL_RenderDrawLine(renderer,0, 3 * CELL_HEIGHT * i, SCREEN_WIDTH, 3 * CELL_HEIGHT * i);
}
}
void render_x(SDL_Renderer * renderer, int box, int row, int column, const SDL_Color * color){
float half_box_side = fmin(CELL_WIDTH, CELL_HEIGHT) * 0.25;
float center_x = CELL_WIDTH * 0.5 + 3 * (box % 3) * CELL_WIDTH + column * CELL_WIDTH;
float center_y = CELL_HEIGHT * 0.5 + 3 * (box / 3 ) * CELL_HEIGHT + row* CELL_HEIGHT;
SDL_SetRenderDrawColor(renderer, color -> r, color -> g, color -> b, 255);
SDL_RenderDrawLine(renderer, center_x - half_box_side, center_y - half_box_side, center_x + half_box_side, center_y + half_box_side);
SDL_RenderDrawLine(renderer, center_x + half_box_side, center_y - half_box_side, center_x - half_box_side, center_y + half_box_side);
}
void draw_circle(SDL_Renderer * renderer, int centreX, int centreY, int radius, const SDL_Color *color)
{
const int diameter = (radius * 2);
int x = (radius - 1);
int y = 0;
int tx = 1;
int ty = 1;
int error = (tx - diameter);
SDL_SetRenderDrawColor(renderer, color->r, color->g, color->b, 255);
while (x >= y)
{
SDL_RenderDrawPoint(renderer, centreX + x, centreY - y);
SDL_RenderDrawPoint(renderer, centreX + x, centreY + y);
SDL_RenderDrawPoint(renderer, centreX - x, centreY - y);
SDL_RenderDrawPoint(renderer, centreX - x, centreY + y);
SDL_RenderDrawPoint(renderer, centreX + y, centreY - x);
SDL_RenderDrawPoint(renderer, centreX + y, centreY + x);
SDL_RenderDrawPoint(renderer, centreX - y, centreY - x);
SDL_RenderDrawPoint(renderer, centreX - y, centreY + x);
if (error <= 0)
{
++y;
error += ty;
ty += 2;
}
if (error > 0)
{
--x;
tx += 2;
error += (tx - diameter);
}
}
}
void render_o(SDL_Renderer * renderer, int box, int row, int column, const SDL_Color * color){
float half_box_side = fmin(CELL_WIDTH, CELL_HEIGHT) * 0.35;
float center_x = CELL_WIDTH * 0.5 + 3 * (box % 3) * CELL_WIDTH + column * CELL_WIDTH;
float center_y = CELL_HEIGHT * 0.5 + 3 * (box / 3 ) * CELL_HEIGHT + row* CELL_HEIGHT;
draw_circle(renderer, center_x, center_y, half_box_side, color);
}
void render_board(SDL_Renderer * renderer, const char board[9][9], const SDL_Color *player_x_color, const SDL_Color *player_o_color){
int i;
int j;
for (i = 0; i < 9; i++){
for (j = 0; j < 9; j++){
switch (board[i][j]){
case PLAYER_X:
render_x(renderer, i, j / 3, j % 3,player_x_color);
break;
case PLAYER_O:
render_o(renderer, i, j / 3, j % 3,player_o_color);
}
}
}
}
void render_running_state(SDL_Renderer * renderer, struct game_t *game){
render_grid(renderer, &GRID_COLOR);
render_board(renderer, game-> board, &X_COLOR, &O_COLOR);
}
void render_win(SDL_Renderer * renderer, struct game_t * game){
// SDL sucks
}
void render_lose(SDL_Renderer * renderer, struct game_t * game){
int width1 =SCREEN_WIDTH /4 * 1.15;
SDL_Rect firstrect = {width1, SCREEN_HEIGHT / 4, SCREEN_WIDTH/10, SCREEN_HEIGHT /3 * 2};
SDL_SetRenderDrawColor(renderer, 255, 0,0,255);
SDL_Rect SecondRect = {width1, SCREEN_HEIGHT / 12 * 11, SCREEN_WIDTH/2, SCREEN_HEIGHT/10};
SDL_RenderFillRect(renderer, &firstrect);
SDL_RenderFillRect(renderer, &SecondRect);
}
void render_game_over(SDL_Renderer * renderer, struct game_t * game, const SDL_Color * color){
render_grid(renderer, color);
render_board(renderer, game -> board, color, color );
if (game -> state == LOST){
render_lose(renderer, game);
}
// switch (game -> state){
// case WON:
// SDL_Surface * imgwon = SDL_LoadBMP("winscreen.bmp");
// SDL_Texture * textwon = SDL_CreateTextureFromSurface(renderer, imgwon);
// SDL_RenderCopy(renderer, textwon, NULL, NULL);
//// break;
// case LOST:
// SDL_Surface * imglost = SDL_LoadBMP("LoseScreen.bmp");
// SDL_Texture * textlost = SDL_CreateTextureFromSurface(renderer, imglost);
// SDL_RenderCopy(renderer, textlost, NULL, NULL);
// case TIE:
// SDL_RenderCopy(renderer, texttie, NULL, NULL);
// SDL_Surface * imgtie = SDL_LoadBMP("tiescreen.bmp");
// SDL_Texture * texttie = SDL_CreateTextureFromSurface(renderer, imgtie);
// break;
// }
}
void rendering_game(SDL_Renderer * renderer, struct game_t *game){
switch (game->state){
case RUNNING_STATE:
render_running_state(renderer,game);
break;
case WON:
render_game_over(renderer, game, &WIN_COLOR);
break;
case LOST:
render_game_over(renderer, game, &LOSE_COLOR);
break;
case TIE:
render_game_over(renderer, game, &TIE_COLOR);
break;
}
}