X-Git-Url: https://git.bzium.org/embe/choinka.git/blobdiff_plain/1f2928827d5fad5cc66cfd8e157f168997ab05c4..HEAD:/choinka.c diff --git a/choinka.c b/choinka.c index 9c03183..4865cff 100644 --- a/choinka.c +++ b/choinka.c @@ -1,30 +1,62 @@ -#include -#include +#include +#include #include #include +#include #include +#include + +#define A_POGRUBIONY (1 << 0) + +#define KOLOR_BRAK -1 +#define KOLOR_CZARNY 0 +#define KOLOR_CZERWONY 1 +#define KOLOR_ZIELONY 2 +#define KOLOR_BRAZOWY 3 +#define KOLOR_NIEBIESKI 4 +#define KOLOR_MAGENTA 5 +#define KOLOR_CYJAN 6 +#define KOLOR_BIALY 7 typedef struct { int attr; int kolor; } atrybut; -atrybut bombki[] = { - {A_BOLD, COLOR_RED}, - {A_BOLD, COLOR_BLUE}, - {A_BOLD, COLOR_YELLOW}, - {A_BOLD, COLOR_MAGENTA}, - {A_BOLD, COLOR_CYAN}, - {A_BOLD, COLOR_WHITE}, +static atrybut const bombki[] = { + {A_POGRUBIONY, KOLOR_CZERWONY}, + {A_POGRUBIONY, KOLOR_NIEBIESKI}, + {A_POGRUBIONY, KOLOR_BRAZOWY}, + {A_POGRUBIONY, KOLOR_MAGENTA}, + {A_POGRUBIONY, KOLOR_CYJAN}, + {A_POGRUBIONY, KOLOR_BIALY}, }; -int ile_b = sizeof bombki / sizeof *bombki; +static int const ile_b = sizeof bombki / sizeof *bombki; -atrybut choinka = {0, COLOR_GREEN}; -atrybut podstawa = {0, COLOR_YELLOW}; -atrybut podpis = {A_BOLD, COLOR_RED}; -atrybut gwiazdka = {A_BOLD, COLOR_YELLOW}; +static atrybut const spacja = {0, KOLOR_BRAK}; +static atrybut const choinka = {0, KOLOR_ZIELONY}; +static atrybut const podstawa = {0, KOLOR_BRAZOWY}; +static atrybut const podpis = {A_POGRUBIONY, KOLOR_CZERWONY}; +static atrybut const gwiazdka = {A_POGRUBIONY, KOLOR_BRAZOWY}; -char rysunek[] = +static void inicjuj_rng() +{ + bool ok = false; + unsigned ziarno; + int f = open("/dev/urandom", O_RDONLY); + if (f != -1) { + if (read(f, &ziarno, sizeof ziarno) == sizeof ziarno) { + ok = true; + } + close(f); + } + if (!ok) { + ziarno = time(NULL); + } + srand(ziarno); +} + +static char const rysunek[] = " *\n" " /.\\\n" " /..'\\\n" @@ -35,31 +67,34 @@ char rysunek[] = " jgs ^^^[_]^^^\n" ; -void ustaw(atrybut const* attr) +static void ustaw(atrybut const* attr) { static atrybut ostatni = {-1, -1}; if (ostatni.attr == attr->attr && ostatni.kolor == attr->kolor) return; ostatni = *attr; - if (attr->attr & A_BOLD) { + putp(exit_attribute_mode); + if ((attr->attr & A_POGRUBIONY) != 0) { putp(enter_bold_mode); - } else { - putp(exit_attribute_mode); } - putp(tparm(set_a_foreground, attr->kolor)); + if (attr->kolor != KOLOR_BRAK) { + putp(tparm(set_a_foreground, attr->kolor)); + } } int main() { - srand(time(NULL)); + setvbuf(stdout, NULL, _IOFBF, BUFSIZ); + inicjuj_rng(); setupterm(NULL, 1, NULL); - atrybut* kolor = &choinka; + atrybut const* kolor = &choinka; for (int i = 0; rysunek[i] != '\0'; i++) { char c = rysunek[i]; if (c == '[') kolor = &podstawa; - atrybut* akt = kolor; + atrybut const* akt = kolor; if (c >= 'a' && c <= 'z') akt = &podpis; if (c == '.') akt = &bombki[rand() % ile_b]; if (c == '*') akt = &gwiazdka; + if (c == ' ' || c == '\n') akt = &spacja; if (c == ']') kolor = &choinka; ustaw(akt); putchar(c);