diff --git a/breakout/breakout.c b/breakout/breakout.c index 6564eff..b08a927 100644 --- a/breakout/breakout.c +++ b/breakout/breakout.c @@ -1,6 +1,7 @@ #ifdef _WIN32 #include #include +#include #else #include #endif @@ -68,6 +69,7 @@ char vertexWithinQuad(coord point, coord *quad); void updatePaddle(Uint32 tickrate, paddle *thePaddle, char moveX, int winWidth, int winHeight); void updateBall(Uint32 tickrate, ball *theBall, paddle *thePaddle, int winWidth, int winHeight, brick *bricks, int brickCount); +void updateScore(int *playerScore, brick *bricks, int brickCount); void drawPaddle(paddle *thePaddle, int winWidth, int winHeight, colour *c); void drawBall(ball *theBall, colour *c); @@ -84,6 +86,8 @@ int main(void) { * 1: SDL init fail * 2: Error creating Window * 3: Error creating context + * 4: SDL_TTF init fail + * 5: Error loading font(s) * */ @@ -96,6 +100,10 @@ int main(void) { return 1; } + if (TTF_Init() < 0) { + perror("[FAILED] Initialising SDL_TTF: "); + } + int go; //Var for loop control int step = 0; //Var for step counter @@ -108,11 +116,12 @@ int main(void) { // Window vars int winPosX = 100; int winPosY = 100; int winWidth = 480; int winHeight = 720; + char windowTitle[128] = "Breakout"; Uint32 timer; SDL_Window *window1 = SDL_CreateWindow( - "Main Window", + windowTitle, winPosX, winPosY, winWidth, winHeight, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL @@ -152,6 +161,9 @@ int main(void) { glEnable(GL_NORMALIZE); // Vectors normalized after transforming to keep units consistent for lighting glShadeModel(GL_SMOOTH); // GL_FLAT for flat shading instead glEnable(GL_DEPTH_TEST); // Enables depth comparisons + glEnable(GL_TEXTURE_2D); // Enables use of textures + + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); @@ -206,11 +218,31 @@ int main(void) { .x = (winWidth/2)-(thePaddle.width/2) }; + int playerScore = 0; + /* - * ---------------- - * Let's go! - * ---------------- - */ + * ---------------- + * Font stuff + * ---------------- + */ + + TTF_Font* font_main = TTF_OpenFont("font_main.ttf", 24); + if (font_main == NULL) { + printf("[FAILED] Unable to load font\n"); + return 5; + } + SDL_Color textColour = { + .a = 255, + .r = 255, + .g = 255, + .b = 255 + }; + + /* + * ---------------- + * Let's go! + * ---------------- + */ go=1; @@ -272,6 +304,7 @@ int main(void) { updatePaddle(tickrate, &thePaddle, moveX, winWidth, winHeight); updateBall(tickrate, &theBall, &thePaddle, winWidth, winHeight, &bricks, brickCount); + updateScore(&playerScore, &bricks, brickCount); if (step % (subSteps+1) == 0) { //step = 0; @@ -283,6 +316,9 @@ int main(void) { SDL_GL_MakeCurrent(window1, context1); + sprintf(windowTitle, "Breakout: Score %d", playerScore); + SDL_SetWindowTitle(window1, windowTitle); + glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); @@ -454,6 +490,28 @@ void updateBall(Uint32 tickrate, ball *theBall, paddle *thePaddle, int winWidth, theBall->y += theBall->vY * 0.1 * tickrate; } +void updateScore(int *playerScore, brick *bricks, int brickCount) { + *playerScore = 0; + for (int i = 0; i < brickCount; i++) { + if (bricks[i].destroyed == 1) { + switch (bricks[i].brickType) { + case 1: + *playerScore += 1; + break; + case 2: + *playerScore += 3; + break; + case 3: + *playerScore += 5; + break; + case 4: + *playerScore += 7; + break; + } + } + } +} + void drawBg(int winWidth, int winHeight, colour *c1, colour *c2) { GLint matrixmode = 0; glGetIntegerv(GL_MATRIX_MODE, &matrixmode);