]> git.bzium.org - embe/choinka.git/blob - choinka.c
b10f073e25bcfa9df85a303995af0708041d4b9c
[embe/choinka.git] / choinka.c
1 #include <ncurses.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <term.h>
5 #include <time.h>
6
7 typedef struct {
8   int attr;
9   int kolor;
10 } atrybut;
11
12 atrybut const bombki[] = {
13   {A_BOLD, COLOR_RED},
14   {A_BOLD, COLOR_BLUE},
15   {A_BOLD, COLOR_YELLOW},
16   {A_BOLD, COLOR_MAGENTA},
17   {A_BOLD, COLOR_CYAN},
18   {A_BOLD, COLOR_WHITE},
19 };
20 int const ile_b = sizeof bombki / sizeof *bombki;
21
22 atrybut const choinka = {0, COLOR_GREEN};
23 atrybut const podstawa = {0, COLOR_YELLOW};
24 atrybut const podpis = {A_BOLD, COLOR_RED};
25 atrybut const gwiazdka = {A_BOLD, COLOR_YELLOW};
26
27 void inicjuj_rng()
28 {
29   unsigned ziarno = 0;
30   FILE* f = fopen("/dev/urandom", "rb");
31   if (f != NULL) {
32     unsigned nowe_ziarno;
33     if (fread(&nowe_ziarno, sizeof nowe_ziarno, 1, f) == 1) {
34       ziarno = nowe_ziarno;
35     }
36     fclose(f);
37   }
38   if (ziarno == 0) {
39     ziarno = time(NULL);
40   }
41   srand(ziarno);
42 }
43
44 char const rysunek[] =
45 "           *\n"
46 "          /.\\\n"
47 "         /..'\\\n"
48 "         /'.'\\\n"
49 "        /.''.'\\\n"
50 "        /.'.'.\\\n"
51 "       /'.''.'.\\\n"
52 "  jgs  ^^^[_]^^^\n"
53 ;
54
55 void ustaw(atrybut const* attr)
56 {
57   static atrybut ostatni = {-1, -1};
58   if (ostatni.attr == attr->attr && ostatni.kolor == attr->kolor) return;
59   ostatni = *attr;
60   if (attr->attr & A_BOLD) {
61     putp(enter_bold_mode);
62   } else {
63     putp(exit_attribute_mode);
64   }
65   putp(tparm(set_a_foreground, attr->kolor));
66 }
67
68 int main()
69 {
70   inicjuj_rng();
71   setupterm(NULL, 1, NULL);
72   atrybut const* kolor = &choinka;
73   for (int i = 0; rysunek[i] != '\0'; i++) {
74     char c = rysunek[i];
75     if (c == '[') kolor = &podstawa;
76     atrybut const* akt = kolor;
77     if (c >= 'a' && c <= 'z') akt = &podpis;
78     if (c == '.') akt = &bombki[rand() % ile_b];
79     if (c == '*') akt = &gwiazdka;
80     if (c == ']') kolor = &choinka;
81     ustaw(akt);
82     putchar(c);
83   }
84   putp(exit_attribute_mode);
85   return 0;
86 }