Implemented a scoring system, added SDL_ttf addon in order to render text. Attempted rendering some test text (surface->GL texture->drawing quad), but was unsuccessful. May or may not remove in favour of spritemaps in the future

This commit is contained in:
Joe Adams 2018-01-11 09:29:28 +00:00
parent 589b989b36
commit a155ce320f

View File

@ -1,6 +1,7 @@
#ifdef _WIN32 #ifdef _WIN32
#include <Windows.h> #include <Windows.h>
#include <SDL.h> #include <SDL.h>
#include <SDL_ttf.h>
#else #else
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#endif #endif
@ -68,6 +69,7 @@ char vertexWithinQuad(coord point, coord *quad);
void updatePaddle(Uint32 tickrate, paddle *thePaddle, char moveX, int winWidth, int winHeight); 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 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 drawPaddle(paddle *thePaddle, int winWidth, int winHeight, colour *c);
void drawBall(ball *theBall, colour *c); void drawBall(ball *theBall, colour *c);
@ -84,6 +86,8 @@ int main(void) {
* 1: SDL init fail * 1: SDL init fail
* 2: Error creating Window * 2: Error creating Window
* 3: Error creating context * 3: Error creating context
* 4: SDL_TTF init fail
* 5: Error loading font(s)
* *
*/ */
@ -96,6 +100,10 @@ int main(void) {
return 1; return 1;
} }
if (TTF_Init() < 0) {
perror("[FAILED] Initialising SDL_TTF: ");
}
int go; //Var for loop control int go; //Var for loop control
int step = 0; //Var for step counter int step = 0; //Var for step counter
@ -108,11 +116,12 @@ int main(void) {
// Window vars // Window vars
int winPosX = 100; int winPosY = 100; int winPosX = 100; int winPosY = 100;
int winWidth = 480; int winHeight = 720; int winWidth = 480; int winHeight = 720;
char windowTitle[128] = "Breakout";
Uint32 timer; Uint32 timer;
SDL_Window *window1 = SDL_CreateWindow( SDL_Window *window1 = SDL_CreateWindow(
"Main Window", windowTitle,
winPosX, winPosY, winPosX, winPosY,
winWidth, winHeight, winWidth, winHeight,
SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL 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 glEnable(GL_NORMALIZE); // Vectors normalized after transforming to keep units consistent for lighting
glShadeModel(GL_SMOOTH); // GL_FLAT for flat shading instead glShadeModel(GL_SMOOTH); // GL_FLAT for flat shading instead
glEnable(GL_DEPTH_TEST); // Enables depth comparisons 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); glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
@ -206,11 +218,31 @@ int main(void) {
.x = (winWidth/2)-(thePaddle.width/2) .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; go=1;
@ -272,6 +304,7 @@ int main(void) {
updatePaddle(tickrate, &thePaddle, moveX, winWidth, winHeight); updatePaddle(tickrate, &thePaddle, moveX, winWidth, winHeight);
updateBall(tickrate, &theBall, &thePaddle, winWidth, winHeight, &bricks, brickCount); updateBall(tickrate, &theBall, &thePaddle, winWidth, winHeight, &bricks, brickCount);
updateScore(&playerScore, &bricks, brickCount);
if (step % (subSteps+1) == 0) { if (step % (subSteps+1) == 0) {
//step = 0; //step = 0;
@ -283,6 +316,9 @@ int main(void) {
SDL_GL_MakeCurrent(window1, context1); SDL_GL_MakeCurrent(window1, context1);
sprintf(windowTitle, "Breakout: Score %d", playerScore);
SDL_SetWindowTitle(window1, windowTitle);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 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; 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) { void drawBg(int winWidth, int winHeight, colour *c1, colour *c2) {
GLint matrixmode = 0; GLint matrixmode = 0;
glGetIntegerv(GL_MATRIX_MODE, &matrixmode); glGetIntegerv(GL_MATRIX_MODE, &matrixmode);