tuitris

Modern block stacking game in the terminal
Log | Files | Refs

draw.c (1295B)


      1 #include <ncurses.h>
      2 
      3 #include "board.h"
      4 #include "piece.h"
      5 
      6 extern const bool piece_occupancy[PIECE_CNT][ROTATION_CNT][9];
      7 extern const bool I_occupancy[ROTATION_CNT][16];
      8 
      9 void draw_square(int y, int x) {
     10 	mvprintw(y, x, "  ");
     11 }
     12 
     13 void
     14 draw_piece(int y, int x, int piece, int rot) {
     15 	attron(COLOR_PAIR(piece));
     16 	if (piece != I) {
     17 		for (int i = 0; i < 9; i++) {
     18 				if (piece_occupancy[piece][rot][i]) {
     19 					draw_square(y+i/3, x+2*(i%3));
     20 				}
     21 		}
     22 	} else {
     23 		for (int i = 0; i < 16; i++) {
     24 				if (I_occupancy[rot][i]) {
     25 					draw_square(y+i/4, x+2*(i%4));
     26 				}
     27 		}
     28 	}
     29 	attroff(COLOR_PAIR(piece));
     30 }
     31 
     32 void
     33 draw_box(int x1, int y1, int x2, int y2) {
     34 	attron(COLOR_PAIR(8));
     35 	for (int x = x1; x <= x2; x++) {
     36 		mvaddch(y1, x, ' ');
     37 		mvaddch(y2, x, ' ');
     38 	}
     39 	for (int y = y1; y <= y2; y++) {
     40 		mvaddch(y, x1-1, ' ');
     41 		mvaddch(y, x1,   ' ');
     42 		mvaddch(y, x2+1, ' ');
     43 		mvaddch(y, x2,   ' ');
     44 	}
     45 	attroff(COLOR_PAIR(8));
     46 }
     47 
     48 void
     49 draw_board(Board *b) {
     50 	int i,j;
     51 	for (i = 0; i < 21; i++) {
     52 		for (j = 0; j < 10; j++) {
     53 			attron(COLOR_PAIR(1));
     54 			draw_square(LINES/2 + 10 - i, COLS/2 - 10 + j*2);
     55 			attroff(COLOR_PAIR(1));
     56 		}
     57 	}
     58 }
     59 
     60 void
     61 draw_queue(Board *b) {
     62 	int i = b->qi+1;
     63 	for (int _ = 0; _ < 6; _++) {
     64 		draw_piece(LINES/2 - 10 + 3*_, COLS/2 + 16, b->q[i++], NORTH);
     65 		i %= 7;
     66 	}
     67 }