summaryrefslogtreecommitdiff
path: root/tennis.c
blob: 7147073cfa8881761ec56e219a19f701736187e6 (plain)
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
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>

#include "./constants.h"
#include "./structs.h"
#include "./game_time.h"
#include "./sound.h"
#include "./color.h"

extern App app;

game_object ball;
game_object paddle1;
game_object paddle2;

int player1_score;
int player2_score;

float speed_up_factor;
float delta_time;

float serve_timer;
float collision_timer;
float winner_timer;

void init_pong();
void init_ball();
void init_paddles();

void update();
void update_ball(float dt);
void do_ball_collision(game_object* paddle);
void update_paddles(float dt);
void do_serve(void);
void do_goal(int* score_to_update);
void do_winner(void);

void render();
void draw_center_line();
void draw_score();

void init_pong() {
	app.input = 0;
	player1_score = 0;
	player2_score = 0;
	init_ball();
	init_paddles();
	init_sounds();
	app.update = update;
	app.render = render;
}

void init_ball() {
	const game_object BALL_TEMPLATE = {
		{
			BALL_INIT_X,
			BALL_INIT_Y,
			BALL_RADIUS,
			BALL_RADIUS
		},
		0,
		0,
		0
	};
	ball = BALL_TEMPLATE;
	speed_up_factor = -1.05;
}

void init_paddles() {
	const game_object PADDLE_TEMPLATE = {
		{
			0,
			PADDLE_Y,
			PADDLE_WIDTH,
			PADDLE_HEIGHT
		},
		0,
		0,
		0
	};
	paddle1 = paddle2 = PADDLE_TEMPLATE;
	paddle1.rect.x = PADDLE1_X;
	paddle2.rect.x = PADDLE2_X;
}

void do_ball_collision(game_object* paddle) {
	if(ball.timer >= 1) {
		ball.dx *= speed_up_factor;
		ball.dy = (WINDOW_HEIGHT * 0.015) * -((paddle->rect.y + paddle->rect.h / 2) - ball.rect.y);
		play_sound(0, -1);
		ball.timer = 0;
	}
}

void update_ball(float dt) {
	// serve ball if not moving
	if( ball.dx == 0 ) { do_serve(); }
	// check for collision with paddles
	if(SDL_HasIntersection(&ball.rect, &paddle1.rect)) { do_ball_collision(&paddle1); }
	if(SDL_HasIntersection(&ball.rect, &paddle2.rect)) { do_ball_collision(&paddle2); }
	// check for collision with top and bottom of screen
	if(ball.rect.y + ball.rect.h >= WINDOW_HEIGHT || ball.rect.y <= 0) { ball.dy *= -1; }
	// check for exit off right or left of screen
	if(ball.rect.x > WINDOW_WIDTH) 		{ do_goal(&player1_score); }
	if(ball.rect.x + ball.rect.w < 0) 	{ do_goal(&player2_score); }
	// update ball's position
	ball.rect.x += ball.dx * dt;
	ball.rect.y += ball.dy * dt;
}

void update_paddles(float dt) {
	// reset paddles' movement vectors, adjust according to input
	paddle1.dy = 0;
	paddle2.dy = 0;

	if(app.input & PADDLE_1_UP && paddle1.rect.y >= 0) 		{ paddle1.dy = -PADDLE_SPEED; }
	if(app.input & PADDLE_1_DOWN &&
		paddle1.rect.y + paddle1.rect.h <= WINDOW_HEIGHT) 	{ paddle1.dy = PADDLE_SPEED; }
	if(app.input & PADDLE_2_UP && paddle2.rect.y >= 0) 		{ paddle2.dy = -PADDLE_SPEED; }
	if(app.input & PADDLE_2_DOWN &&
		paddle2.rect.y + paddle2.rect.h <= WINDOW_HEIGHT) 	{ paddle2.dy = PADDLE_SPEED; }

	// update paddles' positions
	paddle1.rect.y += paddle1.dy * dt;
	paddle2.rect.y += paddle2.dy * dt;

	ball.timer += dt;
}

void do_goal(int* score_to_update) {
	++*score_to_update;
	init_ball();
	init_paddles();
	if(*score_to_update >= 5) { do_winner(); }
}

void do_winner() {
	init_pong();
	
	if(player1_score > player2_score) {
		SDL_Log("PLAYER 1 WINS!!!");
	}
	else {
		SDL_Log("PLAYER 2 WINS!!!");
	}
}

void do_serve(void) {
	if(ball.timer >= 3) {
		if (SDL_GetTicks() % 2 == 0) {
			ball.dx = BALL_INIT_SPEED;
		}
		else {
			ball.dx = -BALL_INIT_SPEED;
		}
	}
}

void update() {
	delay();
	delta_time = get_delta_time();

	update_ball(delta_time);
	update_paddles(delta_time);
}

void draw_center_line(void) {
	for(int i = -CENTER_LINE_HEIGHT / 2; i < WINDOW_HEIGHT; i += CENTER_LINE_HEIGHT * 2) {
		SDL_Rect center_line_rect = {
			WINDOW_WIDTH / 2 - CENTER_LINE_WIDTH / 2,
			i,
			CENTER_LINE_WIDTH,
			CENTER_LINE_HEIGHT
		};
		SDL_RenderFillRect(app.renderer, &center_line_rect);
	}
}

void draw_score(void) {
	for(int i = 0; i != player1_score; ++i) {
		SDL_Rect score_tally_rect = {
			PLAYER1_SCORE_TALLY_X + i * WINDOW_WIDTH / 16,
			SCORE_TALLY_Y,
			SCORE_TALLY_WIDTH,
			SCORE_TALLY_HEIGHT
		};
		SDL_RenderFillRect(app.renderer, &score_tally_rect);
	}
	for(int i = 0; i != player2_score; ++i) {
		SDL_Rect score_tally_rect = {
			PLAYER2_SCORE_TALLY_X - i * WINDOW_WIDTH / 16,
			SCORE_TALLY_Y,
			SCORE_TALLY_WIDTH,
			SCORE_TALLY_HEIGHT
		};
		SDL_RenderFillRect(app.renderer, &score_tally_rect);
	}
}

void draw_message(void) {
	SDL_Texture* font = IMG_LoadTexture(app.renderer, "./gfx/90.png");
	SDL_RenderCopy(app.renderer, font, NULL, NULL);
}


void render(void) {
	// fill screen
	SDL_SetRenderDrawColor(app.renderer, COLOR_BG.r, COLOR_BG.g, COLOR_BG.b, COLOR_BG.a);
	SDL_RenderClear(app.renderer);
	
	// draw ball, paddles
	SDL_SetRenderDrawColor(app.renderer, COLOR_FG.r, COLOR_FG.g, COLOR_FG.b, COLOR_FG.a);
	SDL_RenderFillRect(app.renderer, &ball.rect);
	SDL_RenderFillRect(app.renderer, &paddle1.rect);
	SDL_RenderFillRect(app.renderer, &paddle2.rect);

	// draw center line, dividing the play field
	draw_center_line();

	// draw score tally
	draw_score();

	// draw serve, goal, or winner message
	// draw_message();

	// present rendered scene
	SDL_RenderPresent(app.renderer);
}