From ce65a7e1f682fa31934c05623c41ea25d9b71ce7 Mon Sep 17 00:00:00 2001 From: Charles Date: Thu, 19 Mar 2020 11:28:30 +0100 Subject: Added colors --- src/graphics.cpp | 72 ++++++++++++++++++++++++++++++++++++++++---------------- src/main.cpp | 2 +- 2 files changed, 53 insertions(+), 21 deletions(-) (limited to 'src') 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() diff --git a/src/main.cpp b/src/main.cpp index b0c98a0..2209368 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,7 +6,7 @@ #define WINDOW_TITLE "2048" #define WINDOW_WIDTH 480 #define WINDOW_HEIGHT 480 -#define GAME_GRID_SIZE 4 +#define GAME_GRID_SIZE 6 int main() { -- cgit