aboutsummaryrefslogtreecommitdiff
path: root/src/graphics.cpp
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-03-19 11:28:30 +0100
committerCharles <sircharlesaze@gmail.com>2020-03-19 11:28:30 +0100
commitce65a7e1f682fa31934c05623c41ea25d9b71ce7 (patch)
tree2fb658bf47c0f8802c9c53c81ce5cb5b3ef4fa2a /src/graphics.cpp
parent9578de486aa49d4f89548155ab462732e42267cd (diff)
download2048-ce65a7e1f682fa31934c05623c41ea25d9b71ce7.tar.gz
2048-ce65a7e1f682fa31934c05623c41ea25d9b71ce7.tar.bz2
2048-ce65a7e1f682fa31934c05623c41ea25d9b71ce7.zip
Added colors
Diffstat (limited to 'src/graphics.cpp')
-rw-r--r--src/graphics.cpp72
1 files changed, 52 insertions, 20 deletions
diff --git a/src/graphics.cpp b/src/graphics.cpp
index 13d05b4..988fc8e 100644
--- a/src/graphics.cpp
+++ b/src/graphics.cpp
@@ -21,11 +21,24 @@ Graphics::Graphics(Game *g, std::string t, int w, int h)
error();
if ((font = TTF_OpenFont("./font/noto_mono_regular.ttf", 16)) == NULL)
error();
- std::cout << font << std::endl;
+ palette[0] = {20, 20, 20, 255};
+ palette[2] = {100, 100, 100, 255};
+ palette[4] = {65, 65, 65, 255};
+ palette[8] = {40, 40, 40, 255};
+ palette[16] = {150, 50, 20, 255};
+ palette[32] = {160, 70, 20, 255};
+ palette[64] = {190, 80, 30, 255};
+ palette[128] = {200, 100, 40, 255};
+ palette[256] = {220, 120, 40, 255};
+ palette[512] = {230, 180, 30, 255};
+ palette[1024] = {230, 190, 20, 255};
+ palette[2048] = {230, 200, 10, 255};
}
Graphics::~Graphics()
{
+ for (size_t i = 0; i < numberTexBuf.size(); i++)
+ SDL_DestroyTexture(numberTexBuf[i].second);
TTF_CloseFont(font);
TTF_Quit();
SDL_DestroyRenderer(renderer);
@@ -47,7 +60,6 @@ void Graphics::update()
void Graphics::drawGame()
{
- SDL_SetRenderDrawColor(renderer, 50, 50, 50, SDL_ALPHA_OPAQUE);
for (int i = 0; i < game->getSize(); i++)
for (int j = 0; j < game->getSize(); j++)
drawCell(i, j);
@@ -56,29 +68,28 @@ void Graphics::drawGame()
void Graphics::drawCell(int x, int y)
{
SDL_Rect r;
- SDL_Color c;
- SDL_Surface *text;
SDL_Texture *tex;
+ SDL_Color c;
- c.r = 255;
- c.g = 255;
- c.b = 255;
- c.a = 255;
- if ((text = TTF_RenderText_Solid(font, std::to_string(game->at(x, y)).c_str(), c)) == NULL)
- error();
- if ((tex = SDL_CreateTextureFromSurface(renderer, text)) == NULL)
- error();
- SDL_FreeSurface(text);
+ 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};
+ 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.w = width / game->getSize() - 5;
+ r.h = height / game->getSize() - 5;
+ SDL_SetRenderDrawColor(renderer, c.r, c.g, c.b, SDL_ALPHA_OPAQUE);
SDL_RenderFillRect(renderer, &r);
- r.x += 20;
- r.y += 20;
- SDL_QueryTexture(tex, NULL, NULL, &r.w, &r.h);
- SDL_RenderCopy(renderer, tex, NULL, &r);
- SDL_DestroyTexture(tex);
+ 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);
+ SDL_RenderCopy(renderer, tex, NULL, &r);
+ }
}
bool Graphics::isRunning()
@@ -115,7 +126,28 @@ 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();
+ numberTexBuf.push_back(std::make_pair(n, tex));
+ SDL_FreeSurface(surface);
+ return tex;
+}
+
+SDL_Texture *Graphics::getNumberTex(int n)
+{
+ for (size_t i = 0; i < numberTexBuf.size(); i++)
+ if (numberTexBuf[i].first == n)
+ return numberTexBuf[i].second;
+ return NULL;
}
void Graphics::error()