#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __gnu_linux__
#include <unistd.h>
#include "../deveasyconsole/easyconsole.h"
#include "../deveasymath/easymath.h"
#include "../deveasythread/easythread.h"
#else
#include <time.h>
#include <windows.h>
#define thr_sleep(MS) Sleep(MS)
#define CON_COLOR_BK_WHYTE 0xF
#define CON_COLOR_BK_BLACK 0x0
#define CON_COLOR_GREEN 0x2
void con_gotorc (short r,short c)
{
HANDLE hconsole = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord = {c, r};
SetConsoleCursorPosition (hconsole,coord );
}
void con_cls()
{
HANDLE hconsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
const COORD startCoords = {0,0};
DWORD dummy;
GetConsoleScreenBufferInfo(hconsole,&csbi);
FillConsoleOutputAttribute(hconsole,0,csbi.dwSize.X * csbi.dwSize.Y,startCoords,&dummy);
FillConsoleOutputCharacter(hconsole,' ',csbi.dwSize.X * csbi.dwSize.Y,startCoords,&dummy);
con_gotorc(0,0);
}
void con_setcolor(unsigned char bk, unsigned char c)
{
HANDLE hconsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hconsole,(bk << 4) | c);
}
void mth_initrandom()
{
srand((unsigned)time(NULL));
}
int mth_random(int n)
{
return rand() % (n+1);
}
int mth_randomrange(int min,int max)
{
return min + (rand() % (max-min+1));
}
#endif
#define SCR_W 80
#define SCR_H 24
#define STATO_WRITE 0
#define STATO_LOOP 1
#define STATO_ERASE 2
#define PROB 1
#define PROBL 4
#define PROBC 2
#define MIN_SIZE 6
typedef struct _TXTMX
{
int stato;
int icol;
char b[SCR_H];
int i;
int sz;
int r;
struct _TXTMX* next;
}TXTMX;
char txt_random()
{
static char ch[] = "abcdefghijklmnopqrstuvwxyz1234567890";
return ch[mth_random((sizeof ch) - 1)];
}
TXTMX* txt_new()
{
TXTMX* txt = malloc(sizeof(TXTMX));
txt->sz = mth_randomrange(MIN_SIZE,SCR_H - 2);
txt->next = NULL;
txt->r = mth_random(SCR_H - txt->sz);
txt->i = 0;
while ((txt->icol = mth_random(SCR_W - 1)) % 2);
txt->stato = STATO_WRITE;
int i;
for (i = 0 ; i < txt->sz ; i++)
txt->b[i] = txt_random();
txt->b[txt->sz] = '\0';
return txt;
}
void txt_remove(TXTMX** h,TXTMX* r)
{
TXTMX* tmp;
if (*h == r)
{
tmp = r->next;
free(r);
*h = tmp;
return;
}
for (tmp = *h; tmp->next && tmp->next != r; tmp = tmp->next);
if (tmp->next)
{
TXTMX* tx = tmp->next;
tmp->next = tx->next;
free(tx);
}
}
void txt_add(TXTMX** h,TXTMX* n)
{
if (!*h)
{
*h = n;
return;
}
n->next = *h;
*h = n;
}
void txt_draw(TXTMX** head)
{
TXTMX* h = *head;
for ( ; h; h = h->next)
{
con_gotorc(h->r + h->i,h->icol);
if (h->stato == STATO_WRITE || h->stato == STATO_LOOP)
{
if ( h->b[h->i] == '\0' )
{
h->stato = (mth_random(PROBL)) ? STATO_LOOP : STATO_ERASE;
h->i = 0;
continue;
}
if (h->stato == STATO_WRITE)
{
con_setcolor(CON_COLOR_BK_WHYTE,CON_COLOR_GREEN);
putchar(h->b[h->i]);
con_gotorc(SCR_H-1,SCR_W-1);
fflush(stdout);
}
else if (h->stato == STATO_LOOP)
{
con_setcolor(CON_COLOR_BK_BLACK,CON_COLOR_GREEN);
if (!mth_random(PROBC))
{
putchar(txt_random());
con_gotorc(SCR_H-1,SCR_W-1);
fflush(stdout);
}
}
if ( h->i && h->stato == STATO_WRITE)
{
con_setcolor(CON_COLOR_BK_BLACK,CON_COLOR_GREEN);
con_gotorc(h->r + h->i - 1,h->icol);
putchar(h->b[h->i -1]);
con_gotorc(SCR_H-1,SCR_W-1);
fflush(stdout);
}
h->i++;
}
else if ( h->stato == STATO_ERASE)
{
if (h->b[h->i] == '\0')
{
txt_remove(head,h);
h = *head;//sarebbe meglio continuare in altro modo
if (h == NULL) return;
continue;
}
con_setcolor(CON_COLOR_BK_BLACK,CON_COLOR_GREEN);
putchar(' ');
h->i++;
con_gotorc(SCR_H-1,SCR_W-1);
fflush(stdout);
}
}
}
int main()
{
mth_initrandom();
con_cls();
TXTMX* head = NULL;
while(1234)
{
if (!mth_random(PROB))
txt_add(&head, txt_new());
if (head)
txt_draw(&head);
thr_sleep(150);
}
return 0;
}
messo probabilità ca,bio carattere cosi è piu uniforme,prima veniva il mal di testa da quanti caratteri cambiavano.
tolto di mezzo il cursore.
ora penso che manchi un pò di ottimizzazione.