-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrprint.c
158 lines (140 loc) · 4.87 KB
/
strprint.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "strprint.h"
#define MAX_FILENAME 16
#define PADDING 3
#define LINE_SPACING 2
#define MAX_ROWS 2
#define BUBBLE_HEIGHT MAX_ROWS*LETTER_HEIGHT + 2*PADDING + (MAX_ROWS - 1)*LINE_SPACING
int global_secs;
char printed_sentence[MSG_LEN] = "";
pthread_t sleep_timer;
void clean_text() {
int i = 0, j;
pthread_mutex_lock(&semaphore);
move_cursor_to(0, BACKGROUND_HEIGHT);
for (; i < BUBBLE_HEIGHT; i++) {
for (j = 0; j < BACKGROUND_WIDTH; j++)
printf(COLOR_SPACE(255, 255, 255));
printf(ANSI_COLOR_RESET "\n");
}
pthread_mutex_unlock(&semaphore);
}
void strprint_PIN(char *str) {
/* We assume the input is correct (_checkPin will handle that), that is, only up to 4 numbers */
const int digits = 4;
/* We tried to do posX[digits] but it only compiles in Mac, not in Linux */
const unsigned short posX[] = {99, 110, 121, 132};
const unsigned short posY = 81;
unsigned short i = 0;
unsigned short x, y;
char filename[MAX_FILENAME];
Image *image;
/* Print the string passed */
while (*str != '\0') {
sprintf(filename, "Fonts/%c.bmp", *str);
image = image_ini(filename);
if (image == NULL) {
continue;
}
image_print(image, posX[i], posY);
image_free(image);
str++;
i++;
}
/* Clear the last positions (because we handle the DEL key) */
pthread_mutex_lock(&semaphore);
for(; i < digits; i++) {
for (y = 0; y < 7; y++) {
move_cursor_to(posX[i], posY + y);
for (x = 0; x < 4; x++) {
printf(COLOR_SPACE(255, 255, 255));
}
}
}
pthread_mutex_unlock(&semaphore);
}
Status _strprint(char *str) {
char filename[MAX_FILENAME];
Image *image;
int x = PADDING;
int y = BACKGROUND_HEIGHT + PADDING;
int row = 0;
char c;
if (str == NULL)
return ERROR;
/* If we are trying to print the same sentece that it is printed, we won't */
if (strcmp(str, printed_sentence) == 0)
return OK;
strcpy(printed_sentence, str);
clean_text();
while (*str != '\0') {
if ((*str >= 'A' && *str <= 'Z') || (*str >= 'a' && *str <= 'z') || *str == '!' || *str == '?' || *str == ',' || *str == '.' || *str == ':' || *str == '\'' || (*str >= '0' && *str <= '9')) {
/* Print lowercase uppercase */
if (*str >= 'a' && *str <= 'z')
c = *str - 32;
else
c = *str;
/* As we couldn't create files named '..bmp' or ':.bmp' */
if (c == '.') {
sprintf(filename, "Fonts/dot.bmp");
} else if (c == ':') {
sprintf(filename, "Fonts/colon.bmp");
} else {
sprintf(filename, "Fonts/%c.bmp", c);
}
image = image_ini(filename);
/* Check if the letter fits in, if it doesn't continue on the next line */
if (x + image->width + PADDING > BACKGROUND_WIDTH) {
x = PADDING;
y += LETTER_HEIGHT + LINE_SPACING;
row++;
image_free(image);
/* Exit if no more rows can fit in (it is an ERROR, but we are not going to check it)
* This way the code in game.c for allowing map changes is nicer */
if (row == MAX_ROWS)
return OK;
continue;
}
if (image != NULL) {
image_print(image, x, y);
x += image->width + 1;
image_free(image);
}
} else if (*str == ' ') {
x += 4;
} else if (*str == '\n') {
x = PADDING;
y += LETTER_HEIGHT + LINE_SPACING;
row++;
/* Exit if no more rows can fit in (it is an ERROR, but we are not going to check it)
* This way the code in game.c for allowing map changes is nicer */
if (row == MAX_ROWS)
return OK;
}
str++;
}
fflush(stdout);
return OK;
}
Status strprint(char *str) {
/* Cancel the previous sleep_timer, if there's one */
pthread_cancel(sleep_timer);
return _strprint(str);
}
void *_wait(void *secs) {
sleep(*((int *)secs));
clean_text();
printed_sentence[0] = '\0';
return NULL;
}
Status strprint_time(char *str, int secs) {
/* If we don't copy secs to a global variable, it will get destroyed by the time we want to use it */
global_secs = secs;
/* If we want to print the same sentence, we won't, we'll just reset the sleep timer (strprint won't print anything) */
if (_strprint(str) == ERROR)
return ERROR;
/* Cancel the previous sleep_timer, if there's one */
pthread_cancel(sleep_timer);
/* Spawn a new thread */
pthread_create(&sleep_timer, NULL, _wait, &global_secs);
return OK;
}