aboutsummaryrefslogtreecommitdiff
path: root/src/graphics.cpp
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-03-19 12:18:52 +0100
committerCharles <sircharlesaze@gmail.com>2020-03-19 12:18:52 +0100
commitc435852cc6147a0af23ef12cc0a07a195ac60756 (patch)
tree46797e58189fca8946f752ed20c24e2ccf609f32 /src/graphics.cpp
parentce65a7e1f682fa31934c05623c41ea25d9b71ce7 (diff)
download2048-c435852cc6147a0af23ef12cc0a07a195ac60756.tar.gz
2048-c435852cc6147a0af23ef12cc0a07a195ac60756.tar.bz2
2048-c435852cc6147a0af23ef12cc0a07a195ac60756.zip
Added score
Diffstat (limited to 'src/graphics.cpp')
-rw-r--r--src/graphics.cpp69
1 files changed, 55 insertions, 14 deletions
diff --git a/src/graphics.cpp b/src/graphics.cpp
index 988fc8e..4985b5a 100644
--- a/src/graphics.cpp
+++ b/src/graphics.cpp
@@ -10,6 +10,7 @@ Graphics::Graphics(Game *g, std::string t, int w, int h)
title = t;
width = w;
height = h;
+ gridSize = std::min(width, height);
if (SDL_Init(SDL_INIT_VIDEO) < 0)
error();
@@ -21,7 +22,8 @@ Graphics::Graphics(Game *g, std::string t, int w, int h)
error();
if ((font = TTF_OpenFont("./font/noto_mono_regular.ttf", 16)) == NULL)
error();
- palette[0] = {20, 20, 20, 255};
+ scoreText = newTextTex("score", {255, 255, 255, 255});
+ palette[0] = {10, 10, 10, 255};
palette[2] = {100, 100, 100, 255};
palette[4] = {65, 65, 65, 255};
palette[8] = {40, 40, 40, 255};
@@ -54,6 +56,7 @@ void Graphics::update()
SDL_RenderClear(renderer);
handleEvent();
drawGame();
+ drawScore();
SDL_RenderPresent(renderer);
SDL_Delay(3);
}
@@ -74,24 +77,54 @@ void Graphics::drawCell(int x, int y)
if ((tex = getNumberTex(game->at(x, y))) == NULL)
tex = addNumberTex(game->at(x, y));
if (palette.find(game->at(x, y)) == palette.end())
- c = {0, 0, 0, 255};
+ c = {30, 30, 30, 255};
else
c = palette[game->at(x, y)];
- r.x = 2 + x * (width / game->getSize());
- r.y = 2 + y * (height / game->getSize());
- r.w = width / game->getSize() - 5;
- r.h = height / game->getSize() - 5;
+ r.x = 2 + x * (gridSize / game->getSize());
+ r.y = 2 + y * (gridSize / game->getSize());
+ r.w = gridSize / game->getSize() - 5;
+ r.h = gridSize / game->getSize() - 5;
SDL_SetRenderDrawColor(renderer, c.r, c.g, c.b, SDL_ALPHA_OPAQUE);
SDL_RenderFillRect(renderer, &r);
if (game->at(x, y) != 0)
{
- r.x += width / game->getSize() / 2;
- r.y += height / game->getSize() / 2;
SDL_QueryTexture(tex, NULL, NULL, &r.w, &r.h);
+ r.x += gridSize / game->getSize() / 2 - r.w / 2;
+ r.y += gridSize / game->getSize() / 2 - r.h / 2;
SDL_RenderCopy(renderer, tex, NULL, &r);
}
}
+void Graphics::drawScore()
+{
+ SDL_Rect r;
+ int dist = std::abs(width - height);
+
+ if (dist < 50)
+ return;
+ SDL_QueryTexture(scoreText, NULL, NULL, &r.w, &r.h);
+ if (width > height)
+ {
+ r.x = width - dist / 2 - r.w / 2;
+ r.y = height / 2;
+ }
+ else
+ {
+ r.x = width / 2;
+ r.y = height - dist / 2 - r.h / 2;
+ }
+ SDL_RenderCopy(renderer, scoreText, NULL, &r);
+
+ SDL_Texture *scoreNumTex = newTextTex(std::to_string(game->getScore()), {255, 255, 255, 255});
+ SDL_QueryTexture(scoreNumTex, NULL, NULL, &r.w, &r.h);
+ if (width > height)
+ r.y += 20;
+ else
+ r.x += 20;
+ SDL_RenderCopy(renderer, scoreNumTex, NULL, &r);
+ SDL_DestroyTexture(scoreNumTex);
+}
+
bool Graphics::isRunning()
{
return running;
@@ -130,15 +163,10 @@ void Graphics::handleEvent()
SDL_Texture *Graphics::addNumberTex(int n)
{
- SDL_Surface *surface;
SDL_Texture *tex;
SDL_Color c = {255, 255, 255, 255};
- if ((surface = TTF_RenderText_Solid(font, std::to_string(n).c_str(), c)) == NULL)
- error();
- if ((tex = SDL_CreateTextureFromSurface(renderer, surface)) == NULL)
- error();
+ tex = newTextTex(std::to_string(n), c);
numberTexBuf.push_back(std::make_pair(n, tex));
- SDL_FreeSurface(surface);
return tex;
}
@@ -150,6 +178,19 @@ SDL_Texture *Graphics::getNumberTex(int n)
return NULL;
}
+SDL_Texture *Graphics::newTextTex(std::string s, SDL_Color c)
+{
+ SDL_Surface *surface;
+ SDL_Texture *tex;
+
+ if ((surface = TTF_RenderText_Solid(font, s.c_str(), c)) == NULL)
+ error();
+ if ((tex = SDL_CreateTextureFromSurface(renderer, surface)) == NULL)
+ error();
+ SDL_FreeSurface(surface);
+ return (tex);
+}
+
void Graphics::error()
{
std::cout << SDL_GetError() << std::endl;