aboutsummaryrefslogtreecommitdiff
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
parentce65a7e1f682fa31934c05623c41ea25d9b71ce7 (diff)
download2048-c435852cc6147a0af23ef12cc0a07a195ac60756.tar.gz
2048-c435852cc6147a0af23ef12cc0a07a195ac60756.tar.bz2
2048-c435852cc6147a0af23ef12cc0a07a195ac60756.zip
Added score
-rw-r--r--include/game.hpp2
-rw-r--r--include/graphics.hpp8
-rw-r--r--src/game.cpp11
-rw-r--r--src/graphics.cpp69
-rw-r--r--src/main.cpp5
5 files changed, 75 insertions, 20 deletions
diff --git a/include/game.hpp b/include/game.hpp
index 550b47a..62a024c 100644
--- a/include/game.hpp
+++ b/include/game.hpp
@@ -12,6 +12,7 @@ class Game
Game(int s);
~Game();
int getSize();
+ int getScore();
int at(int x, int y);
void move(Direction direction);
void spawn();
@@ -20,6 +21,7 @@ class Game
private:
int size;
int **grid;
+ int score;
void mergeRow(std::vector<int> &row);
int **gridCopy();
bool gridEqual(int **other);
diff --git a/include/graphics.hpp b/include/graphics.hpp
index ccebdae..ebb596b 100644
--- a/include/graphics.hpp
+++ b/include/graphics.hpp
@@ -19,8 +19,6 @@ class Graphics
~Graphics();
void update();
- void drawGame();
- void drawCell(int x, int y);
bool isRunning();
private:
@@ -28,16 +26,22 @@ class Graphics
std::string title;
int width;
int height;
+ int gridSize;
Game *game;
SDL_Renderer *renderer;
SDL_Window *window;
TTF_Font *font;
+ SDL_Texture *scoreText;
std::vector< std::pair<int, SDL_Texture*> > numberTexBuf;
std::map< int, SDL_Color > palette;
+ void drawGame();
+ void drawCell(int x, int y);
+ void drawScore();
void handleEvent();
SDL_Texture *addNumberTex(int n);
SDL_Texture *getNumberTex(int n);
+ SDL_Texture *newTextTex(std::string s, SDL_Color c);
void error();
};
diff --git a/src/game.cpp b/src/game.cpp
index 2ef3c15..b505f9f 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -6,9 +6,14 @@ Game::Game(int s)
size = s;
grid = new int*[size];
for (int i = 0; i < size; i++)
+ {
grid[i] = new int[size];
+ for (int j = 0; j < size; j++)
+ grid[i][j] = 0;
+ }
spawn();
spawn();
+ score = 0;
}
Game::~Game()
@@ -21,6 +26,11 @@ int Game::getSize()
return size;
}
+int Game::getScore()
+{
+ return score;
+}
+
int Game::at(int x, int y)
{
return grid[y][x];
@@ -145,6 +155,7 @@ void Game::mergeRow(std::vector<int> &row)
if (row[curr + 1] == row[curr])
{
row[curr] *= 2;
+ score += row[curr];
row[curr + 1] = 0;
for (size_t i = curr + 1; i < row.size() - 1; i++)
row[i] = row[i + 1];
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;
diff --git a/src/main.cpp b/src/main.cpp
index 2209368..7d4d0fc 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -4,7 +4,7 @@
#include "game.hpp"
#define WINDOW_TITLE "2048"
-#define WINDOW_WIDTH 480
+#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
#define GAME_GRID_SIZE 6
@@ -13,11 +13,8 @@ int main()
srand(time(NULL));
Game game = Game(GAME_GRID_SIZE);
Graphics *graphics = new Graphics(&game, WINDOW_TITLE, WINDOW_WIDTH, WINDOW_HEIGHT);
-
while (graphics->isRunning())
graphics->update();
-
delete graphics;
-
return 0;
}