Applied tickrate to updateBall function and introduced updatePaddle function (also using tickrate) to keep timing consistent across platforms/launch scenarios - not yet tested on Linux

This commit is contained in:
Joe Adams 2017-12-13 09:03:35 +00:00
parent c2bb045d5e
commit 00a089f6f9

View File

@ -17,7 +17,8 @@ typedef struct colour {
double a; double a;
} colour; } colour;
void updateBall(double *ballX, double *ballY, double *ballVX, double *ballVY, double paddleX, int paddleWidth, int winWidth, int winHeight); void updatePaddle(Uint32 tickrate, double *paddleX, char moveX, int paddleWidth, int winWidth, int winHeight);
void updateBall(Uint32 tickrate, double *ballX, double *ballY, double *ballVX, double *ballVY, double paddleX, int paddleWidth, int winWidth, int winHeight);
void drawPaddle(double paddleX, int paddleWidth, int winWidth, int winHeight, colour *c); void drawPaddle(double paddleX, int paddleWidth, int winWidth, int winHeight, colour *c);
void drawBall(double ballX, double ballY, colour *c); void drawBall(double ballX, double ballY, colour *c);
@ -127,8 +128,8 @@ int main(void) {
double paddleX=(winWidth/2)-(paddleWidth/2); double paddleX=(winWidth/2)-(paddleWidth/2);
double ballX = 0; double ballX = 0;
double ballY = 0; double ballY = 0;
double ballVX = 0.2; double ballVX = 0.75;
double ballVY = -0.3; double ballVY = -1.5;
/* /*
* ---------------- * ----------------
@ -146,10 +147,10 @@ int main(void) {
*/ */
Uint32 tickspassed=SDL_GetTicks()-timer; Uint32 tickrate=SDL_GetTicks()-timer;
timer=SDL_GetTicks(); timer=SDL_GetTicks();
int fps=1000/(tickspassed+1); int fps=1000/(tickrate+1);
printf("Time to draw: %u, fps: %i \r", tickspassed, fps); fflush(stdout); printf("Time to draw: %u, fps: %i \r", tickrate, fps); fflush(stdout);
/* /*
* ---------------- * ----------------
@ -188,12 +189,15 @@ int main(void) {
break; break;
} }
} }
paddleX += moveX*0.1;
updateBall(&ballX, &ballY, &ballVX, &ballVY, paddleX, paddleWidth, winWidth, winHeight); /*
ballX += ballVX*0.1; * ----------------
ballY += ballVY*0.1; * Update elements
* ----------------
*/
updatePaddle(tickrate, &paddleX, moveX, paddleWidth, winWidth, winHeight);
updateBall(tickrate, &ballX, &ballY, &ballVX, &ballVY, paddleX, paddleWidth, winWidth, winHeight);
/* /*
* ---------------- * ----------------
@ -219,7 +223,7 @@ int main(void) {
bgcol1.b = 0.4431; /*113*/ bgcol2.b = 0.6078; /*155*/ bgcol1.b = 0.4431; /*113*/ bgcol2.b = 0.6078; /*155*/
bgcol1.a = 1.0000; /*255*/ bgcol2.a = 1.0000; /*255*/ bgcol1.a = 1.0000; /*255*/ bgcol2.a = 1.0000; /*255*/
drawPaddle(paddleX, paddleWidth, winWidth, winHeight, &paddleCol); drawPaddle( paddleX, paddleWidth, winWidth, winHeight, &paddleCol);
drawBall(ballX, ballY, &paddleCol); drawBall(ballX, ballY, &paddleCol);
drawBg(winWidth, winHeight, &bgcol1, &bgcol2); drawBg(winWidth, winHeight, &bgcol1, &bgcol2);
@ -245,7 +249,13 @@ int main(void) {
return 0; return 0;
} }
void updateBall(double *ballX, double *ballY, double *ballVX, double *ballVY, double paddleX, int paddleWidth, int winWidth, int winHeight) { void updatePaddle(Uint32 tickrate, double *paddleX, char moveX, int paddleWidth, int winWidth, int winHeight) {
*paddleX += moveX * 0.5 * tickrate;
if (*paddleX < 0-(paddleWidth/2)) *paddleX = 0-(paddleWidth/2);
if (*paddleX > winWidth-(paddleWidth/2)) *paddleX = winWidth-(paddleWidth/2);
}
void updateBall(Uint32 tickrate, double *ballX, double *ballY, double *ballVX, double *ballVY, double paddleX, int paddleWidth, int winWidth, int winHeight) {
double ballNX = *ballX + *ballVX; // Calculates the position of the ball on the next step double ballNX = *ballX + *ballVX; // Calculates the position of the ball on the next step
double ballNY = *ballY + *ballVY; double ballNY = *ballY + *ballVY;
double paddleRX = paddleX - (double)(winWidth / 2); double paddleRX = paddleX - (double)(winWidth / 2);
@ -267,9 +277,12 @@ void updateBall(double *ballX, double *ballY, double *ballVX, double *ballVY, do
if (ballNY < (-1.0*winHeight/2)) { // Collision with floor if (ballNY < (-1.0*winHeight/2)) { // Collision with floor
*ballX = 0; *ballX = 0;
*ballY = 0; *ballY = 0;
*ballVX = -0.1; *ballVX = -0.75;
*ballVY = -0.5; *ballVY = -1.5;
} }
*ballX += *ballVX * 0.1 * tickrate;
*ballY += *ballVY * 0.1 * tickrate;
} }
void drawBg(int winWidth, int winHeight, colour *c1, colour *c2) { void drawBg(int winWidth, int winHeight, colour *c1, colour *c2) {