aboutsummaryrefslogtreecommitdiff
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
parent9578de486aa49d4f89548155ab462732e42267cd (diff)
download2048-ce65a7e1f682fa31934c05623c41ea25d9b71ce7.tar.gz
2048-ce65a7e1f682fa31934c05623c41ea25d9b71ce7.tar.bz2
2048-ce65a7e1f682fa31934c05623c41ea25d9b71ce7.zip
Added colors
-rw-r--r--Makefile4
-rw-r--r--include/graphics.hpp8
-rw-r--r--src/graphics.cpp72
-rw-r--r--src/main.cpp2
4 files changed, 63 insertions, 23 deletions
diff --git a/Makefile b/Makefile
index 0b643f2..5cd4949 100644
--- a/Makefile
+++ b/Makefile
@@ -12,7 +12,7 @@ CXXFLAGS = -Wall -Wextra -I$(INCDIR) $(shell sdl2-config --cflags)
LDFLAGS = $(shell sdl2-config --libs) -lSDL2_ttf
SRC = $(shell find $(SRCDIR) -type f -name "*.cpp")
-INC = $(shell find $(INCDIR) -type f -name "*.h" -name "*.hpp")
+INC = $(shell find $(INCDIR) -type f -name "*.h" -o -name "*.hpp")
OBJ = $(SRC:$(SRCDIR)/%.cpp=$(OBJDIR)/%.o)
all: prebuild $(NAME)
@@ -23,7 +23,7 @@ prebuild:
$(NAME): $(OBJ)
$(CXX) -o $@ $^ $(LDFLAGS)
-$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
+$(OBJDIR)/%.o: $(SRCDIR)/%.cpp $(INC)
$(CXX) $(CXXFLAGS) -c -o $@ $<
clean:
diff --git a/include/graphics.hpp b/include/graphics.hpp
index 44f631c..ccebdae 100644
--- a/include/graphics.hpp
+++ b/include/graphics.hpp
@@ -3,6 +3,10 @@
#include <string>
#include <iostream>
+#include <vector>
+#include <map>
+#include <utility>
+#include <utility>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include "game.hpp"
@@ -28,8 +32,12 @@ class Graphics
SDL_Renderer *renderer;
SDL_Window *window;
TTF_Font *font;
+ std::vector< std::pair<int, SDL_Texture*> > numberTexBuf;
+ std::map< int, SDL_Color > palette;
void handleEvent();
+ SDL_Texture *addNumberTex(int n);
+ SDL_Texture *getNumberTex(int n);
void error();
};
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()
{