diff options
| author | 2019-03-06 20:59:45 -0500 | |
|---|---|---|
| committer | 2019-03-06 20:59:45 -0500 | |
| commit | c2d0d1b15cb19940c9ef065ae21dc1fd78f73772 (patch) | |
| tree | ec515f0777b110ab1dc950f3cee881cc3ce697a9 /frontend | |
| parent | 60f886b5359f792fd89b5847fb72923f6574e1bf (diff) | |
| download | cetris-c2d0d1b15cb19940c9ef065ae21dc1fd78f73772.tar.gz cetris-c2d0d1b15cb19940c9ef065ae21dc1fd78f73772.tar.bz2 cetris-c2d0d1b15cb19940c9ef065ae21dc1fd78f73772.zip | |
change a lot of stuff and flesh out sdl ui
Diffstat (limited to 'frontend')
| -rw-r--r-- | frontend/ncurses/ncurses_ui.c | 42 | ||||
| -rw-r--r-- | frontend/sdl/.border.jpeg-autosave.kra | bin | 0 -> 71027 bytes | |||
| -rw-r--r-- | frontend/sdl/sdl_ui.c | 99 |
3 files changed, 119 insertions, 22 deletions
diff --git a/frontend/ncurses/ncurses_ui.c b/frontend/ncurses/ncurses_ui.c index d3d0e36..b1407f6 100644 --- a/frontend/ncurses/ncurses_ui.c +++ b/frontend/ncurses/ncurses_ui.c @@ -58,6 +58,8 @@ #define X_OFFSET 8
#define Y_OFFSET 0
+struct cetris_game game;
+
void curses_init() {
setlocale(LC_CTYPE, "");
initscr();
@@ -78,29 +80,29 @@ void curses_init() { clear();
}
-void draw_board(struct cetris_game* g) {
+void draw_board() {
mvaddstr(0, 0, PLAY_FIELD_STR);
for (int x = 0; x < BOARD_X; x++) {
for (int y = BOARD_VISABLE; y < BOARD_Y; y++) {
- if (g->board[x][y].occupied) {
- attron(COLOR_PAIR(g->board[x][y].c));
- if (g->board[x][y].remove_tick > 0) {
- if (g->tick % 2 == 0) {
+ if (game.board[x][y].occupied) {
+ attron(COLOR_PAIR(game.board[x][y].c));
+ if (game.board[x][y].remove_tick > 0) {
+ if (game.tick % 2 == 0) {
mvaddstr((y - BOARD_VISABLE) + 1, x * 2 + X_OFFSET, BLOCK);
}
} else {
mvaddstr((y - BOARD_VISABLE) + 1, x * 2 + X_OFFSET, BLOCK);
}
- attroff(COLOR_PAIR(g->board[x][y].c));
+ attroff(COLOR_PAIR(game.board[x][y].c));
}
}
- int index = g->current_index;
- attron(COLOR_PAIR(g->piece_queue[index].c));
+ int index = game.current_index;
+ attron(COLOR_PAIR(game.piece_queue[index].c));
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
- if (g->piece_queue[index].mat[y][x]) {
- if (g->piece_queue[index].t == I) {
+ if (game.piece_queue[index].mat[y][x]) {
+ if (game.piece_queue[index].t == I) {
mvaddstr(6 + y, (x * 2) + 36, BLOCK);
} else {
mvaddstr(6 + y, (x * 2) + 37, BLOCK);
@@ -108,19 +110,24 @@ void draw_board(struct cetris_game* g) { }
}
}
- attroff(COLOR_PAIR(g->piece_queue[index].c));
+ attroff(COLOR_PAIR(game.piece_queue[index].c));
attron(A_BOLD);
char score[50];
- sprintf(score, "%li", g->score);
+ sprintf(score, "%li", game.score);
mvaddstr(1, (39 + X_OFFSET) - strlen(score), score);
char level[20];
- sprintf(level, "%i", g->level);
+ sprintf(level, "%i", game.level);
mvaddstr(3, 37, "Level");
mvaddstr(4, 40 - strlen(level), level);
+ if (game.game_over) {
+ mvaddstr(10, 6 + X_OFFSET, "GAME OVER");
+ mvaddstr(11, 5 + X_OFFSET, "r to restart");
+ }
+
attroff(A_BOLD);
}
}
@@ -128,7 +135,6 @@ void draw_board(struct cetris_game* g) { int main(void) {
curses_init();
- struct cetris_game game;
init_game(&game);
int c;
@@ -145,9 +151,15 @@ int main(void) { case KEY_UP:
rotate_clockwise(&game); break;
case ' ':
- move_hard_drop(&game);
+ move_hard_drop(&game); break;
+ case 'r':
+ if (game.game_over) {
+ init_game(&game);
+ }
+ break;
}
update_game_tick(&game);
+ erase();
draw_board(&game);
refresh();
}
diff --git a/frontend/sdl/.border.jpeg-autosave.kra b/frontend/sdl/.border.jpeg-autosave.kra Binary files differnew file mode 100644 index 0000000..69f596a --- /dev/null +++ b/frontend/sdl/.border.jpeg-autosave.kra diff --git a/frontend/sdl/sdl_ui.c b/frontend/sdl/sdl_ui.c index 012255a..f862244 100644 --- a/frontend/sdl/sdl_ui.c +++ b/frontend/sdl/sdl_ui.c @@ -1,15 +1,19 @@ #include <SDL2/SDL.h> +#include <SDL2/SDL_ttf.h> #include "cetris.h" -#define W 360 -#define H 640 +#define W 430 +#define H 550 +#define BOARD_OFFSET_X 25 +#define BOARD_OFFSET_Y 20 struct cetris_game g; SDL_Renderer* render; SDL_Texture* block; SDL_Event e; +TTF_Font* font; typedef struct { int r; @@ -31,22 +35,103 @@ sdl_color colors[8] = { void setup() { SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO); SDL_Window* win = SDL_CreateWindow("Cetris", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, W, H, SDL_WINDOW_SHOWN); - render = SDL_CreateRenderer(win, -1, SDL_RENDERER_PRESENTVSYNC); + render = SDL_CreateRenderer(win, -1, SDL_RENDERER_PRESENTVSYNC|SDL_RENDERER_ACCELERATED); + SDL_RenderSetLogicalSize(render, W, H); if (!render) exit(fprintf(stderr, "[Error] Could not create SDL renderer\n")); SDL_Surface* s = SDL_LoadBMP("block.bmp"); block = SDL_CreateTextureFromSurface(render, s); SDL_FreeSurface(s); + + TTF_Init(); + font = TTF_OpenFont("LiberationMono-Regular.ttf", 18); +} + +void draw_text(char* string, int x, int y) { + int w, h; + char msg[80]; + snprintf(msg, 80, string); + TTF_SizeText(font, msg, &w, &h); + SDL_Surface *msgsurf = TTF_RenderText_Blended(font, msg, (SDL_Color){255, 255, 255, 255}); + SDL_Texture *msgtex = SDL_CreateTextureFromSurface(render, msgsurf); + SDL_Rect fromrec = {0, 0, msgsurf->w, msgsurf->h}; + SDL_Rect torec = {x, y, msgsurf->w, msgsurf->h}; + SDL_RenderCopy(render, msgtex, &fromrec, &torec); + SDL_DestroyTexture(msgtex); + SDL_FreeSurface(msgsurf); } void draw_stuff() { + SDL_SetRenderDrawColor(render, 100, 100, 112, 255); SDL_RenderClear(render); + SDL_Rect b = {BOARD_OFFSET_X, BOARD_OFFSET_Y, 250, 500}; + SDL_SetRenderDrawColor(render, 120, 120, 132, 255); + SDL_RenderFillRect(render, &b); + SDL_RenderDrawRect(render, &b); + + + SDL_Rect next = {294, 20, 125, 125}; + SDL_RenderFillRect(render, &next); + SDL_RenderDrawRect(render, &next); + int index = g.current_index; + sdl_color nc = colors[g.piece_queue[index].c]; + SDL_SetTextureColorMod(block, nc.r, nc.g, nc.b); + SDL_Rect p = {294, 20, 25, 25}; + for (int x = 0; x < 4; x++) { + for (int y = 0; y < 4; y++) { + if (g.piece_queue[index].mat[y][x]) { + p.x = 319 + (x * 25); + p.y = 30 + (y * 25); + if (g.piece_queue[index].t == I) { + p.x = 306 + (x * 25); + } + if (g.piece_queue[index].t == O) { + p.x = 306 + (x * 25); + } + SDL_RenderCopy(render, block, NULL, &p); + } + } + } + + SDL_SetRenderDrawColor(render, 76, 70, 72, 255); + + int y_off = 140; + + SDL_Rect s = {290, 20 + y_off, 135, 42}; + SDL_RenderFillRect(render, &s); + SDL_RenderDrawRect(render, &s); + draw_text("SCORE", 295, 23 + y_off); + char* score = malloc(sizeof(char) * 25); + sprintf(score, "%li", g.score); + draw_text(score, 294 + strlen(score), 40 + y_off); + free(score); + + SDL_Rect l = {290, 70 + y_off, 135, 42}; + SDL_RenderFillRect(render, &l); + SDL_RenderDrawRect(render, &l); + draw_text("LEVEL", 295, 73 + y_off); + char* level = malloc(sizeof(char) * 25); + sprintf(level, "%i", g.level); + draw_text(level, 294 + strlen(level), 90 + y_off); + free(level); + + SDL_Rect l1 = {290, 120 + y_off, 135, 42}; + SDL_RenderFillRect(render, &l1); + SDL_RenderDrawRect(render, &l1); + draw_text("LINES", 295, 123 + y_off); + char* lines = malloc(sizeof(char) * 25); + sprintf(lines, "%i", g.lines); + draw_text(lines, 294 + strlen(lines), 140 + y_off); + free(lines); + SDL_SetRenderDrawColor(render, 255, 255, 255, 124); for (int x = 0; x < BOARD_X + 1; x++) { - SDL_RenderDrawLine(render, 1 + (x * 25), 1, 1 + (x * 25), 500); + int rx = BOARD_OFFSET_X + 1 + (x * 25); + SDL_RenderDrawLine(render, rx, BOARD_OFFSET_Y + 1, rx, BOARD_OFFSET_Y + 500); } for (int y = 0; y < BOARD_Y - BOARD_VISABLE + 1; y++) { - SDL_RenderDrawLine(render, 1, 1 + (y * 25), 250, 1 + (y * 25)); + int ry = BOARD_OFFSET_Y + (y * 25); + SDL_RenderDrawLine(render, BOARD_OFFSET_X + 1, ry, BOARD_OFFSET_X + 250, ry); } SDL_SetRenderDrawColor(render, 0, 0, 0, 255); @@ -57,8 +142,8 @@ void draw_stuff() { if (g.board[x][y].occupied) { c = colors[g.board[x][y].c]; SDL_SetTextureColorMod(block, c.r, c.g, c.b); - r.x = x * 25; - r.y = (y - BOARD_VISABLE) * 25; + r.x = BOARD_OFFSET_X + x * 25; + r.y = BOARD_OFFSET_Y + (y - BOARD_VISABLE) * 25; SDL_RenderCopy(render, block, NULL, &r); } } |