aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-03-18 20:50:58 +0100
committerCharles <sircharlesaze@gmail.com>2020-03-18 20:50:58 +0100
commit79ab0bdeedd6e7f97b4c246b4319af5eac545061 (patch)
tree841a4fafcd634285cf9a267e0e80550735a154c3
parentb228349c5d3e93e8e58e96efeb332fdb58093f0f (diff)
download2048-79ab0bdeedd6e7f97b4c246b4319af5eac545061.tar.gz
2048-79ab0bdeedd6e7f97b4c246b4319af5eac545061.tar.bz2
2048-79ab0bdeedd6e7f97b4c246b4319af5eac545061.zip
game class, display of the game grid
-rw-r--r--Makefile2
-rw-r--r--README.md1
-rw-r--r--font/noto_mono_regular.ttfbin0 -> 107848 bytes
-rw-r--r--include/game.hpp17
-rw-r--r--include/graphics.hpp10
-rw-r--r--src/game.cpp27
-rw-r--r--src/graphics.cpp61
-rw-r--r--src/main.cpp14
8 files changed, 121 insertions, 11 deletions
diff --git a/Makefile b/Makefile
index 2a6469e..0b643f2 100644
--- a/Makefile
+++ b/Makefile
@@ -9,7 +9,7 @@ OBJDIR = build
CXX = g++
CXXFLAGS = -Wall -Wextra -I$(INCDIR) $(shell sdl2-config --cflags)
-LDFLAGS = $(shell sdl2-config --libs)
+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")
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..56e5489
--- /dev/null
+++ b/README.md
@@ -0,0 +1 @@
+# 2048
diff --git a/font/noto_mono_regular.ttf b/font/noto_mono_regular.ttf
new file mode 100644
index 0000000..3560a3a
--- /dev/null
+++ b/font/noto_mono_regular.ttf
Binary files differ
diff --git a/include/game.hpp b/include/game.hpp
new file mode 100644
index 0000000..a644799
--- /dev/null
+++ b/include/game.hpp
@@ -0,0 +1,17 @@
+#ifndef GAME_HPP
+# define GAME_HPP
+
+class Game
+{
+ public:
+ Game(int s);
+ ~Game();
+ int getSize();
+ int at(int x, int y);
+
+ private:
+ int size;
+ int **grid;
+};
+
+#endif
diff --git a/include/graphics.hpp b/include/graphics.hpp
index 69ac808..0990247 100644
--- a/include/graphics.hpp
+++ b/include/graphics.hpp
@@ -2,15 +2,20 @@
# define GRAPHICS_HPP
#include <string>
+#include <iostream>
#include <SDL2/SDL.h>
+#include <SDL2/SDL_ttf.h>
+#include "game.hpp"
class Graphics
{
public:
- Graphics(std::string t, int w, int h);
+ Graphics(Game *g, std::string t, int w, int h);
~Graphics();
void update();
+ void drawGame();
+ void drawCell(int x, int y);
bool isRunning();
private:
@@ -18,10 +23,13 @@ class Graphics
std::string title;
int width;
int height;
+ Game *game;
SDL_Renderer *renderer;
SDL_Window *window;
+ TTF_Font *font;
void handleEvent();
+ void error();
};
#endif
diff --git a/src/game.cpp b/src/game.cpp
new file mode 100644
index 0000000..11a603b
--- /dev/null
+++ b/src/game.cpp
@@ -0,0 +1,27 @@
+#include "game.hpp"
+
+
+Game::Game(int s)
+{
+ size = s;
+ grid = new int*[size];
+ for (int i = 0; i < size; i++)
+ grid[i] = new int[size];
+}
+
+Game::~Game()
+{
+ for (int i = 0; i < size; i++)
+ delete []grid[i];
+ delete []grid;
+}
+
+int Game::getSize()
+{
+ return size;
+}
+
+int Game::at(int x, int y)
+{
+ return grid[y][x];
+}
diff --git a/src/graphics.cpp b/src/graphics.cpp
index 589a1a8..3fca69b 100644
--- a/src/graphics.cpp
+++ b/src/graphics.cpp
@@ -1,22 +1,30 @@
#include "graphics.hpp"
-Graphics::Graphics(std::string t, int w, int h)
+Graphics::Graphics(Game *g, std::string t, int w, int h)
{
running = true;
+ game = g;
title = t;
width = w;
height = h;
if (SDL_Init(SDL_INIT_VIDEO) < 0)
- return;
+ error();
if ((window = SDL_CreateWindow(title.c_str(), 0, 0, width, height, 0)) == NULL)
- return;
+ error();
if ((renderer = SDL_CreateRenderer(window, -1, 0)) == NULL)
- return;
+ error();
+ if (TTF_Init() < 0)
+ error();
+ if ((font = TTF_OpenFont("./font/noto_mono_regular.ttf", 16)) == NULL)
+ error();
+ std::cout << font << std::endl;
}
Graphics::~Graphics()
{
+ TTF_CloseFont(font);
+ TTF_Quit();
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
@@ -24,9 +32,48 @@ Graphics::~Graphics()
void Graphics::update()
{
+ SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);
handleEvent();
+ drawGame();
SDL_RenderPresent(renderer);
+ SDL_Delay(3);
+}
+
+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);
+}
+
+void Graphics::drawCell(int x, int y)
+{
+ SDL_Rect r;
+ SDL_Color c;
+ SDL_Surface *text;
+ SDL_Texture *tex;
+
+ 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);
+ 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);
+ 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);
}
bool Graphics::isRunning()
@@ -48,3 +95,9 @@ void Graphics::handleEvent()
}
}
+
+void Graphics::error()
+{
+ std::cout << SDL_GetError() << std::endl;
+ exit(1);
+}
diff --git a/src/main.cpp b/src/main.cpp
index c4ef817..7fe08bf 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,17 +1,21 @@
#include <iostream>
-#include <SDL2/SDL.h>
#include "graphics.hpp"
+#include "game.hpp"
#define WINDOW_TITLE "2048"
-#define WINDOW_WIDTH 640
+#define WINDOW_WIDTH 480
#define WINDOW_HEIGHT 480
+#define GAME_GRID_SIZE 4
int main()
{
- Graphics g = Graphics(WINDOW_TITLE, WINDOW_WIDTH, WINDOW_HEIGHT);
+ Game game = Game(GAME_GRID_SIZE);
+ Graphics *graphics = new Graphics(&game, WINDOW_TITLE, WINDOW_WIDTH, WINDOW_HEIGHT);
- while (g.isRunning())
- g.update();
+ while (graphics->isRunning())
+ graphics->update();
+
+ delete graphics;
return 0;
}