jag

tetoris_c

public

Small block game in C

Star0
Watch0
b19f80d9ec2d77c692ba1048f889faff339edaaa
1
ssh://git@git.jaglekon.de:30/jag/tetoris_c.git
source/draw.cC195 lines3.6 KB
1
#include "main.h"
2
3
#define RED    ((color){255, 0, 0})
4
#define GREEN  ((color){0, 255, 0})
5
#define BLUE   ((color){0, 0, 255})
6
#define YELLOW ((color){255, 255, 0})
7
#define MAGENTA ((color){255, 0, 255})
8
#define TEAL ((color){0, 255, 255})
9
#define PURPLE ((color){127, 0, 255})
10
#define WHITE ((color){255, 255, 255})
11
12
#define DARK 0.6
13
14
15
void draw_color (tetromino current_square, double dark) {
16
 
17
  color col = {};
18
19
  switch (current_square) {
20
21
    case I_PIECE:
22
      col = RED;
23
      break;
24
    case J_PIECE:
25
      col = GREEN;
26
      break;
27
    case L_PIECE:
28
      col = BLUE;
29
      break;
30
    case O_PIECE:
31
      col = YELLOW;
32
      break;
33
    case S_PIECE:
34
      col = MAGENTA;
35
      break;
36
    case T_PIECE:
37
      col = TEAL;
38
      break;
39
    case Z_PIECE:
40
      col = PURPLE;
41
      break;
42
    default:
43
      break;
44
  } 
45
46
  if(dark > 0) {
47
48
49
    col.r *= dark;
50
    col.g *= dark;
51
    col.b *= dark;
52
  }
53
54
  printf("\033[38;2;%i;%i;%im", col.r, col.g, col.b);
55
}
56
57
58
void draw_board(piece current_piece, piece next_piece, bool *blink, bool is_piece) {
59
60
  int8_t i, j;
61
62
63
  printf("\033[H");
64
  printf("┏");
65
  for (i = 0; i < FIELD_WIDTH; i++)
66
    printf("━━");
67
  printf("┓\n");
68
69
70
  for (i = 0; i < FIELD_HEIGHT; i++) {
71
    
72
    printf("┃");
73
74
    for (j = 0; j < FIELD_WIDTH; j++) {
75
76
      double dark = DARK;
77
      
78
      if ( blink != NULL && blink[i]) {
79
  
80
        color col = WHITE;
81
82
        printf("\033[38;2;%i;%i;%im", col.r, col.g, col.b);
83
      } else {
84
85
86
        tetromino current_square = playfield[i][j + BORDER_WIDTH];
87
88
89
        if (is_piece) {
90
91
          uint8_t k, l;
92
          
93
          for (k = 0; k < current_piece.size; k++) {
94
            for (l = 0; l < current_piece.size; l++) {
95
96
              coord points = find_direction(current_piece, l, k);
97
98
              if (
99
                i == current_piece.y + points.y && 
100
                j == current_piece.x + points.x - BORDER_WIDTH && 
101
                current_piece.piece[k][l]) 
102
              {
103
                current_square = current_piece.name;
104
                dark = 0;
105
              }
106
            }
107
          }
108
        } 
109
110
        if (current_square <= EMPTY || current_square > Z_PIECE) {
111
          printf("· ");
112
          continue;
113
        }
114
115
        draw_color(current_square, dark);
116
      }
117
        
118
      printf("██\033[0m");
119
    }
120
    printf("┃\n");
121
  }
122
123
  printf("┗");
124
  for (i = 0; i < FIELD_WIDTH; i++)
125
    printf("━━");
126
  printf("┛\n");
127
128
129
  /* next piece preview */
130
131
  printf("\033[H");
132
  printf("\033[%iC", FIELD_WIDTH * 2 + 1);
133
  printf("┱──────────┐\n");
134
135
  for (i = 4; i > 0; i--) {
136
    printf("\033[%iC", FIELD_WIDTH * 2 + 1);
137
    printf("┃");
138
139
    draw_color(next_piece.name, 0);
140
141
    for (j = 4; j >= 0; j--) {
142
    
143
      int8_t y = next_piece.size - i;
144
      int8_t x = next_piece.size - j;
145
        
146
147
      if (
148
        y < next_piece.size && x < next_piece.size &&
149
        y >= 0 && x >= 0 &&
150
        next_piece.piece[y][x]
151
      ) {
152
        printf("██");
153
        continue;
154
      }
155
       
156
      printf("  ");
157
    }
158
159
    printf("\033[0m│\n");
160
  }
161
162
  printf("\033[%iC", FIELD_WIDTH * 2 + 1);
163
  printf("┠──────────┘\n");
164
165
  printf("\033[%iC", FIELD_WIDTH * 2 + 2);
166
  printf("LEVEL: %u\n", level);
167
168
}
169
170
void draw_pause () {
171
  
172
  uint8_t i;
173
174
  printf("\033[H");
175
  printf("\033[%iB", 1 + (FIELD_HEIGHT >> 2));
176
177
  for (i = 0; i < 3; i++) {
178
179
    printf("\033[%iC", 1 + (((FIELD_WIDTH * 2  - 7) >> 1)));
180
181
    switch(i) {
182
      case 0:
183
        printf("╭─────╮");
184
        break;
185
      case 1:
186
        printf("│PAUSE│");
187
        break;
188
      case 2:
189
        printf("╰─────╯");
190
        break;
191
    }
192
193
    printf("\n");
194
  } 
195
}