From daa48a0e6d0dc8d155e9360343844c81119b8f29 Mon Sep 17 00:00:00 2001 From: Joe Adams Date: Thu, 11 Jan 2018 14:14:58 +0000 Subject: [PATCH] Added text rendering using SDL_TTF, level counter, game over message, and if statement to stop the game updating once the game is over --- breakout/breakout.c | 71 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 64 insertions(+), 7 deletions(-) diff --git a/breakout/breakout.c b/breakout/breakout.c index 964adfb..c67dd42 100644 --- a/breakout/breakout.c +++ b/breakout/breakout.c @@ -70,6 +70,7 @@ double heronsFormula(coord a, coord b, coord c) { } int loadTextures(GLuint *texture, const char *imageLoc); +void surfaceToTexture(GLuint *texture, SDL_Surface *surface); 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 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 drawPaddle(paddle *thePaddle, int winWidth, int winHeight, colour *c); void drawBall(ball *theBall, colour *c); @@ -228,8 +257,11 @@ int main(void) { .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; /* @@ -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) { printf("[FAILED] Unable to load font\n"); return 5; @@ -263,6 +295,7 @@ int main(void) { loadTextures(&heartFullTex, "assets/heartFull.png") > 1) { return 6; } + /* * ---------------- * Let's go! @@ -327,11 +360,12 @@ int main(void) { * ---------------- */ - updatePaddle(tickrate, &thePaddle, moveX, winWidth, winHeight); - updateBall(tickrate, &theBall, &thePaddle, winWidth, winHeight, &bricks, brickCount, &playerLives); - updateScore(&playerScore, &bricks, brickCount); - if (playerLives < 1) { - go = 0; + if (playerLives > 0) { + sprintf(txtL, "%03d", playerScore); + sprintf(txtR, "%03d", level); + updatePaddle(tickrate, &thePaddle, moveX, winWidth, winHeight); + updateBall(tickrate, &theBall, &thePaddle, winWidth, winHeight, &bricks, brickCount, &playerLives); + updateScore(&playerScore, &bricks, brickCount); } if (step % (subSteps+1) == 0) { @@ -363,6 +397,12 @@ int main(void) { bgcol1.b = 0.6078; /*155*/ bgcol2.b = 0.4431; /*113*/ 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); drawPaddle(&thePaddle, winWidth, winHeight, &paddleCol); drawBall(&theBall, &paddleCol); @@ -425,6 +465,23 @@ int loadTextures(GLuint *texture, const char *imageLoc) { 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) { double quadArea = heronsFormula(quad[0], quad[1], quad[2]) + heronsFormula(quad[1], quad[2], quad[3]); double testArea = 0;