diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/game.cpp | 27 | ||||
| -rw-r--r-- | src/graphics.cpp | 61 | ||||
| -rw-r--r-- | src/main.cpp | 14 |
3 files changed, 93 insertions, 9 deletions
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; } |
