-
Notifications
You must be signed in to change notification settings - Fork 0
/
pong.cpp
208 lines (178 loc) · 7.29 KB
/
pong.cpp
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
struct Paddle
{
// r32 width = 20.0f;
// r32 height = 100.0f;
// r32 originLocationHeight = (r32)(screenHeight / 2);
// r32 moveSpeed = 10.0f;
//vector2 tRight = toClipSpace2( (x/2) + (r32)(screenWidth/2), (y/2) + (r32)(screenHeight/2));
//vector2 bLeft = toClipSpace2(-(x/2) + (r32)(screenWidth/2), -(y/2) + (r32)(screenHeight/2));
vector2 dim = {10, 100};
//r32 width = (20.0f / (r32)screenWidth);
//r32 height = (100.0f / (r32)screenHeight);
r32 originLocationHeight = (r32)(screenHeight / 2);
r32 moveSpeed = 10.0f;
};
struct Ball
{
r32 sideLength = 5.0f;
vector2 locationOnScreen {};
r32 moveSpeed = 5.0f;// look at having this changed based on hit and such
};
struct GameState
{
Paddle playerPaddle;
Paddle computerPaddle;
Ball ball = {};
bool gameInprogress = false;
vector2 gameScore = {};
};
inline void
DrawPlayerPaddle(Paddle *paddle)
{
vector3 color = {0.0f,0.0f,1.0f}; // Green
// RenderRectViaPixels( \
// Vector2(startX,(paddle->originLocationHeight - (paddle->height / 2))), \
// Vector2(startX + paddle->width,(paddle->originLocationHeight + (paddle->height / 2))), \
// color);
vector2 tRight = toClipSpace2( (paddle->dim.width/2),
(paddle->dim.height/2) + paddle->originLocationHeight);
vector2 bLeft = toClipSpace2(-(paddle->dim.width/2),
-(paddle->dim.height/2) + paddle->originLocationHeight);
RenderRect(tRight, bLeft, color);
}
inline void
DrawComputerPaddle(Paddle *paddle)
{
vector3 color = {0.0f,1.0f,0.0f}; // Green
// RenderRectViaPixels( \
// Vector2(startX,(paddle->originLocationHeight - (paddle->height / 2))), \
// Vector2(startX - paddle->width,(paddle->originLocationHeight + (paddle->height / 2))), \
// color);
vector2 tRight = toClipSpace2( (paddle->dim.width/2) + (r32)(screenWidth),
(paddle->dim.height/2) + paddle->originLocationHeight);
vector2 bLeft = toClipSpace2(-(paddle->dim.width/2) + (r32)(screenWidth),
-(paddle->dim.height/2) + paddle->originLocationHeight);
RenderRect(tRight, bLeft, color);
}
inline void
DrawCenterLine()
{
r32 lineWidth = 5.0f;
vector3 color = {1.0f,0.0f,0.0f}; // Whjte
// RenderRectViaPixels(Vector2(((screenWidth / 2.0f) - 0.5f),0.0f),
// Vector2(((screenWidth / 2.0f) + 0.5f),(r32)screenHeight),
// Vector3(1.0f,1.0f,1.0f));
vector2 tRight = toClipSpace2( (lineWidth + (r32)screenWidth/2), 0);
vector2 bLeft = toClipSpace2(-(lineWidth + (r32)screenWidth/2), (r32)screenHeight);
RenderRect(tRight, bLeft, color);
}
inline void
DrawBall(Ball *ball)
{
if(!ball->locationOnScreen) {
ball->locationOnScreen.x = (r32)(screenWidth / 2.0f);
ball->locationOnScreen.y = (r32)(screenHeight / 2.0f);
}
ball->locationOnScreen = BoundToScreen(ball->locationOnScreen); // Make sure the ball is on the screen
vector3 color = {1.0f,0.0f,0.0f};
//RenderRectViaPixels(Vector2((ball->locationOnScreen.x - 4.0f),(ball->locationOnScreen.y - 4.0f)),
// Vector2((ball->locationOnScreen.x + 4.0f),(ball->locationOnScreen.y + 4.0f)),
// Vector3(1,0,0));
RenderRect(
toClipSpace2((ball->locationOnScreen.x + 4.0f), (ball->locationOnScreen.y + 4.0f)),
toClipSpace2((ball->locationOnScreen.x - 4.0f), (ball->locationOnScreen.y - 4.0f)),
color);
}
inline void
ResetBallPostition(Ball *ball)
{
ball->locationOnScreen.x = (r32)(screenWidth / 2.0f);
ball->locationOnScreen.y = (r32)(screenHeight / 2.0f);
}
inline void
gameInputHandling(GameState *state,u32 keyPressed, u8 wasDown, u8 isDown, LPARAM lParam)
{
if(keyPressed == VK_UP || keyPressed == 'W')
{
state->playerPaddle.originLocationHeight = \
BoundtoScreenY(state->playerPaddle.originLocationHeight + state->playerPaddle.moveSpeed);
if(state->playerPaddle.originLocationHeight - ((state->playerPaddle.dim.height) /2.0f) <= 0)
{ state->playerPaddle.originLocationHeight = state->playerPaddle.dim.height /2.0f;}
}
else if(keyPressed == VK_DOWN || keyPressed == 'S')
{
state->playerPaddle.originLocationHeight = \
BoundtoScreenY(state->playerPaddle.originLocationHeight - state->playerPaddle.moveSpeed);
if(state->playerPaddle.originLocationHeight + ((state->playerPaddle.dim.height) /2.0f) >= screenHeight)
{ state->playerPaddle.originLocationHeight = (screenHeight - (state->playerPaddle.dim.height) /2.0f);}
}
else if(keyPressed == ' ' && !state->gameInprogress)
{
state->gameInprogress=true;
}
}
inline void
update(GameState *state)
{
glTranslatef(0.0f, 0.0f, -2.0f);
localPersist vector2 ballMoveVector = {0,0};
//clearBackgroundToColor(Vector3(0,0,0));
//glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
DrawCenterLine();
DrawPlayerPaddle(&state->playerPaddle);
DrawComputerPaddle(&state->computerPaddle);
//TODO:Handle Ball Math
if(state->gameInprogress)
{
if(ballMoveVector == Vector2(0,0))
{
ballMoveVector.x = -state->ball.moveSpeed;
}
if((state->ball.locationOnScreen.x <= 30
&& state->ball.locationOnScreen.x >= 20)
|| (state->ball.locationOnScreen.x >= (screenWidth-30)
&& state->ball.locationOnScreen.x <= (screenWidth-20)))
{
r32 playerPaddleTop= \
state->playerPaddle.originLocationHeight + (state->playerPaddle.dim.height/2.0f);
r32 playerPaddleBottom= \
state->playerPaddle.originLocationHeight - (state->playerPaddle.dim.height/2.0f);
r32 computerPaddleTop= \
state->computerPaddle.originLocationHeight + (state->computerPaddle.dim.height/2.0f);
r32 computerPaddleBottom= \
state->computerPaddle.originLocationHeight - (state->computerPaddle.dim.height/2.0f);
if((state->ball.locationOnScreen.y <= playerPaddleTop
&& state->ball.locationOnScreen.y >= playerPaddleBottom)
|| (state->ball.locationOnScreen.y <= computerPaddleTop
&& state->ball.locationOnScreen.y >= computerPaddleBottom))
{
ballMoveVector = {-ballMoveVector.x, -ballMoveVector.y};
}
}
state->ball.locationOnScreen += ballMoveVector; // moveball
if(state->ball.locationOnScreen.x <= 0)
{
state->gameScore.y += 1;
ResetBallPostition(&state->ball);
if(state->gameScore.y == 10)
{
//TODO: Game over section (player Loses)
state->gameInprogress=false;
ballMoveVector = Vector2(0,0);
}
}
else if(state->ball.locationOnScreen.y >= screenWidth)
{
state->gameScore.x += 1;
ResetBallPostition(&state->ball);
if(state->gameScore.x == 10)
{
//TODO: Game over section (player wins)
state->gameInprogress=false;
ballMoveVector = Vector2(0,0);
}
}
}
DrawBall(&state->ball);
}