From 3dc61fd55d97a407e18a3306263b7013302f4130 Mon Sep 17 00:00:00 2001 From: David Phillips Date: Wed, 3 Apr 2019 22:06:00 +1300 Subject: Add lines counter/scoring --- config.mk | 2 +- plot.c | 25 +++++++++++++++++++++++++ plot.h | 1 + tetris.c | 24 ++++++++++++++++++++++-- 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/config.mk b/config.mk index 39f6a44..5ab5f49 100644 --- a/config.mk +++ b/config.mk @@ -1,2 +1,2 @@ CFLAGS += -I/usr/include/SDL2 -Wall -Wextra -Wpedantic -pedantic-errors -LDFLAGS += -lSDL2 +LDFLAGS += -lSDL2 -lSDL2_ttf diff --git a/plot.c b/plot.c index 3cf2c0d..7a7a54e 100644 --- a/plot.c +++ b/plot.c @@ -1,4 +1,5 @@ #include +#include #include "colour.h" #include "plot.h" @@ -40,6 +41,30 @@ plot_cell(unsigned int x, unsigned int y, struct colour *c) } void +plot_text(const char *message, TTF_Font *font, SDL_Color color, int x, int y) +{ + SDL_Texture *texture = NULL; + SDL_Rect dst; + dst.x = x; + dst.y = y; + SDL_Surface *surf = TTF_RenderUTF8_Blended(font, message, color); + if (!surf) { + printf("Error in TTF_RenderUTF8_Blended\n"); + return; + } + texture = SDL_CreateTextureFromSurface(renderer, surf); + if (!texture){ + printf("Error in SDL_CreateTextureFromSurface\n"); + } + SDL_FreeSurface(surf); + + SDL_QueryTexture(texture, NULL, NULL, &dst.w, &dst.h); + SDL_RenderCopy(renderer, texture, NULL, &dst); + + SDL_DestroyTexture(texture); +} + +void plot_clear(void) { /* blank out the background with black */ diff --git a/plot.h b/plot.h index edde581..75accc8 100644 --- a/plot.h +++ b/plot.h @@ -1,5 +1,6 @@ void plot_rect(unsigned int x, unsigned int y, unsigned int width, unsigned int height, struct colour *c); void plot_cell(unsigned int x, unsigned int y, struct colour *c); +void plot_text(const char *message, TTF_Font *font, SDL_Color color, int x, int y); void plot_clear(void); int plot_init(void); void plot_update(void); diff --git a/tetris.c b/tetris.c index 4b4b8f1..bc7e0d0 100644 --- a/tetris.c +++ b/tetris.c @@ -2,12 +2,16 @@ #include #include #include +#include #include #include "colour.h" #include "tetromino.h" #include "plot.h" +#define FONT_FILE "/usr/share/fonts/TTF/arial.ttf" +#define FONT_SIZE 16 + #define VERSION "1.0" #define TETROMINO_WIDTH 4 #define TETROMINO_HEIGHT 4 @@ -163,9 +167,10 @@ rotate(struct piece *held, int direction) update_bitmap(held); } -void +int clear_rows(struct colour* (*board)[WIDTH_CELLS][HEIGHT_CELLS]) { + int rows = 0; char row; int x, y, x1, y1; for (y = 0; y < HEIGHT_CELLS; y++) { @@ -179,6 +184,8 @@ clear_rows(struct colour* (*board)[WIDTH_CELLS][HEIGHT_CELLS]) if (!row) continue; + rows++; + for (y1 = y; y1 > 0; y1--) for (x1 = 0; x1 < WIDTH_CELLS; x1++) (*board)[x1][y1] = (*board)[x1][y1-1]; @@ -186,6 +193,7 @@ clear_rows(struct colour* (*board)[WIDTH_CELLS][HEIGHT_CELLS]) for (x1 = 0; x1 < WIDTH_CELLS; x1++) (*board)[x1][y1] = &(palette[0]); } + return rows; } void @@ -197,11 +205,18 @@ main_loop() int i = 0; int x = 0; int y = 0; + int score = 0; int last_x = 0; int last_y = 1; struct piece held; new_piece(&held); + TTF_Font *font = TTF_OpenFont(FONT_FILE, FONT_SIZE); + if (!font) { + printf("TTF_OpenFont: %s\n", TTF_GetError()); + return; + } + for (y = 0; y < HEIGHT_CELLS; y++) for (x = 0; x < WIDTH_CELLS; x++) board[x][y] = &(palette[0]); @@ -213,7 +228,7 @@ main_loop() while (running) { lockout = 0; - clear_rows(&board); + score += clear_rows(&board); if (hit_side(x, y, &held, &board)) x = last_x; @@ -223,6 +238,10 @@ main_loop() draw_board(&board); draw_piece(x, y, held.colour, held.bitmap); + char score_string[16]; + snprintf(score_string, sizeof(score_string), "Score: %d", score); + score_string[sizeof(score_string) - 1] = '\0'; + plot_text(score_string, font, ((SDL_Color){255,255,255,255}), 10, 10); plot_update(); SDL_WaitEvent(&e); if (pause_mode) { @@ -289,6 +308,7 @@ int main(int argc, char **argv) { char *argument = NULL; + TTF_Init(); speed_mode = 0; pause_mode = 0; -- cgit v1.1