Added text rendering using SDL_TTF, level counter, game over message, and if statement to stop the game updating once the game is over
This commit is contained in:
parent
8082423e0c
commit
daa48a0e6d
@ -70,6 +70,7 @@ double heronsFormula(coord a, coord b, coord c) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int loadTextures(GLuint *texture, const char *imageLoc);
|
int loadTextures(GLuint *texture, const char *imageLoc);
|
||||||
|
void surfaceToTexture(GLuint *texture, SDL_Surface *surface);
|
||||||
|
|
||||||
char vertexWithinQuad(coord point, coord *quad);
|
char vertexWithinQuad(coord point, coord *quad);
|
||||||
|
|
||||||
@ -77,6 +78,34 @@ void updatePaddle(Uint32 tickrate, paddle *thePaddle, char moveX, int winWidth,
|
|||||||
void updateBall(Uint32 tickrate, ball *theBall, paddle *thePaddle, int winWidth, int winHeight, brick *bricks, int brickCount, int *playerLives);
|
void updateBall(Uint32 tickrate, ball *theBall, paddle *thePaddle, int winWidth, int winHeight, brick *bricks, int brickCount, int *playerLives);
|
||||||
void updateScore(int *playerScore, brick *bricks, int brickCount);
|
void updateScore(int *playerScore, brick *bricks, int brickCount);
|
||||||
|
|
||||||
|
void drawText(const char *text, TTF_Font *font, SDL_Color txtColour, coord loc) {
|
||||||
|
SDL_Surface *text_surface = TTF_RenderUTF8_Blended(font, text, txtColour);
|
||||||
|
if (text_surface==NULL) {
|
||||||
|
fprintf(stderr, "Error generating text");
|
||||||
|
} else {
|
||||||
|
GLuint testTxt;
|
||||||
|
surfaceToTexture(&testTxt, text_surface);
|
||||||
|
|
||||||
|
glBindTexture(GL_TEXTURE_2D, testTxt);
|
||||||
|
|
||||||
|
GLint matrixmode = 0;
|
||||||
|
glGetIntegerv(GL_MATRIX_MODE, &matrixmode);
|
||||||
|
glPushMatrix();
|
||||||
|
glTranslated(loc.x-(text_surface->w/2), loc.y, 0.0);
|
||||||
|
|
||||||
|
glBegin(GL_QUADS);
|
||||||
|
glTexCoord2f(0, 0); glVertex3d(0.0, 0.0, 0.0);
|
||||||
|
glTexCoord2f(0, 1); glVertex3d(0.0, -1*text_surface->h, 0.0);
|
||||||
|
glTexCoord2f(1, 1); glVertex3d(text_surface->w, -1*text_surface->h, 0.0);
|
||||||
|
glTexCoord2f(1, 0); glVertex3d(text_surface->w, 0.0, 0.0);
|
||||||
|
glEnd();
|
||||||
|
|
||||||
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
|
||||||
|
glPopMatrix();
|
||||||
|
glMatrixMode(matrixmode);
|
||||||
|
}
|
||||||
|
}
|
||||||
void drawHearts(GLuint heartFullTex, GLuint heartEmptyTex, int playerLives, int winWidth, int winHeight);
|
void drawHearts(GLuint heartFullTex, GLuint heartEmptyTex, int playerLives, int winWidth, int winHeight);
|
||||||
void drawPaddle(paddle *thePaddle, int winWidth, int winHeight, colour *c);
|
void drawPaddle(paddle *thePaddle, int winWidth, int winHeight, colour *c);
|
||||||
void drawBall(ball *theBall, colour *c);
|
void drawBall(ball *theBall, colour *c);
|
||||||
@ -228,8 +257,11 @@ int main(void) {
|
|||||||
.x = (winWidth/2)-(thePaddle.width/2)
|
.x = (winWidth/2)-(thePaddle.width/2)
|
||||||
};
|
};
|
||||||
|
|
||||||
int playerScore = 0;
|
char txtL[4] = "000";
|
||||||
|
char txtR[4] = "000";
|
||||||
|
|
||||||
|
int playerScore = 0;
|
||||||
|
int level = 1;
|
||||||
int playerLives = 3;
|
int playerLives = 3;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -238,7 +270,7 @@ int main(void) {
|
|||||||
* ----------------
|
* ----------------
|
||||||
*/
|
*/
|
||||||
|
|
||||||
TTF_Font* font_main = TTF_OpenFont("assets/font_main.ttf", 24);
|
TTF_Font* font_main = TTF_OpenFont("assets/font_main.ttf", 56);
|
||||||
if (font_main == NULL) {
|
if (font_main == NULL) {
|
||||||
printf("[FAILED] Unable to load font\n");
|
printf("[FAILED] Unable to load font\n");
|
||||||
return 5;
|
return 5;
|
||||||
@ -263,6 +295,7 @@ int main(void) {
|
|||||||
loadTextures(&heartFullTex, "assets/heartFull.png") > 1) {
|
loadTextures(&heartFullTex, "assets/heartFull.png") > 1) {
|
||||||
return 6;
|
return 6;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* ----------------
|
* ----------------
|
||||||
* Let's go!
|
* Let's go!
|
||||||
@ -327,11 +360,12 @@ int main(void) {
|
|||||||
* ----------------
|
* ----------------
|
||||||
*/
|
*/
|
||||||
|
|
||||||
updatePaddle(tickrate, &thePaddle, moveX, winWidth, winHeight);
|
if (playerLives > 0) {
|
||||||
updateBall(tickrate, &theBall, &thePaddle, winWidth, winHeight, &bricks, brickCount, &playerLives);
|
sprintf(txtL, "%03d", playerScore);
|
||||||
updateScore(&playerScore, &bricks, brickCount);
|
sprintf(txtR, "%03d", level);
|
||||||
if (playerLives < 1) {
|
updatePaddle(tickrate, &thePaddle, moveX, winWidth, winHeight);
|
||||||
go = 0;
|
updateBall(tickrate, &theBall, &thePaddle, winWidth, winHeight, &bricks, brickCount, &playerLives);
|
||||||
|
updateScore(&playerScore, &bricks, brickCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (step % (subSteps+1) == 0) {
|
if (step % (subSteps+1) == 0) {
|
||||||
@ -363,6 +397,12 @@ int main(void) {
|
|||||||
bgcol1.b = 0.6078; /*155*/ bgcol2.b = 0.4431; /*113*/
|
bgcol1.b = 0.6078; /*155*/ bgcol2.b = 0.4431; /*113*/
|
||||||
bgcol1.a = 1.0000; /*255*/ bgcol2.a = 1.0000; /*255*/
|
bgcol1.a = 1.0000; /*255*/ bgcol2.a = 1.0000; /*255*/
|
||||||
|
|
||||||
|
if (playerLives==0) {
|
||||||
|
drawText("Game Over", font_main, textColour, (coord){ .x=0, .y=-128});
|
||||||
|
}
|
||||||
|
|
||||||
|
drawText(txtL, font_main, textColour, (coord){ .x=-128, .y=(winHeight/2)-32 });
|
||||||
|
drawText(txtR, font_main, textColour, (coord){ .x=128, .y=(winHeight/2)-32 });
|
||||||
drawHearts(heartFullTex, heartEmptyTex, playerLives, winWidth, winHeight);
|
drawHearts(heartFullTex, heartEmptyTex, playerLives, winWidth, winHeight);
|
||||||
drawPaddle(&thePaddle, winWidth, winHeight, &paddleCol);
|
drawPaddle(&thePaddle, winWidth, winHeight, &paddleCol);
|
||||||
drawBall(&theBall, &paddleCol);
|
drawBall(&theBall, &paddleCol);
|
||||||
@ -425,6 +465,23 @@ int loadTextures(GLuint *texture, const char *imageLoc) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void surfaceToTexture(GLuint *texture, SDL_Surface *surface) {
|
||||||
|
*texture = 0;
|
||||||
|
|
||||||
|
int Mode = GL_RGB;
|
||||||
|
|
||||||
|
glGenTextures(1, texture);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, *texture);
|
||||||
|
|
||||||
|
if (surface->format->BytesPerPixel == 4) {
|
||||||
|
Mode = GL_RGBA;
|
||||||
|
}
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0, Mode, surface->w, surface->h, 0, Mode, GL_UNSIGNED_BYTE, surface->pixels);
|
||||||
|
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
}
|
||||||
|
|
||||||
char vertexWithinQuad(coord point, coord *quad) {
|
char vertexWithinQuad(coord point, coord *quad) {
|
||||||
double quadArea = heronsFormula(quad[0], quad[1], quad[2]) + heronsFormula(quad[1], quad[2], quad[3]);
|
double quadArea = heronsFormula(quad[0], quad[1], quad[2]) + heronsFormula(quad[1], quad[2], quad[3]);
|
||||||
double testArea = 0;
|
double testArea = 0;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user