Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,122 @@
/**
* Loosely emulates the "digital rain" effect from The Matrix.
*
* @author Dan Ruscoe <dan@ruscoe.org>
*/
#include <unistd.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <ncurses.h>
/* Time between row updates in microseconds.
Controls the speed of the digital rain effect.
*/
#define ROW_DELAY 40000
/**
* Gets a random integer within a given range.
*
* @param int min The low-end of the range.
* @param int max The high-end of the range.
*
* @return int The random integer.
*/
int get_rand_in_range(int min, int max)
{
return (rand() % ((max + 1) - min) + min);
}
int main(void)
{
/* Basic seed for random numbers. */
srand(time(NULL));
/* Characters to randomly appear in the rain sequence. */
char chars[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
int total_chars = sizeof(chars);
/* Set up ncurses screen and colors. */
initscr();
noecho();
curs_set(FALSE);
start_color();
init_pair(1, COLOR_GREEN, COLOR_BLACK);
attron(COLOR_PAIR(1));
int max_x = 0, max_y = 0;
getmaxyx(stdscr, max_y, max_x);
/* Create arrays of columns based on screen width. */
/* Array containing the current row of each column. */
int columns_row[max_x];
/* Array containing the active status of each column.
A column draws characters on a row when active.
*/
int columns_active[max_x];
int i;
/* Set top row as current row for all columns. */
for (i = 0; i < max_x; i++)
{
columns_row[i] = -1;
columns_active[i] = 0;
}
while (1)
{
for (i = 0; i < max_x; i++)
{
if (columns_row[i] == -1)
{
/* If a column is at the top row, pick a
random starting row and active status.
*/
columns_row[i] = get_rand_in_range(0, max_y);
columns_active[i] = get_rand_in_range(0, 1);
}
}
/* Loop through columns and draw characters on rows. */
for (i = 0; i < max_x; i++)
{
if (columns_active[i] == 1)
{
/* Draw a random character at this column's current row. */
int char_index = get_rand_in_range(0, total_chars);
mvprintw(columns_row[i], i, "%c", chars[char_index]);
}
else
{
/* Draw an empty character if the column is inactive. */
mvprintw(columns_row[i], i, " ");
}
columns_row[i]++;
/* When a column reaches the bottom row, reset to top. */
if (columns_row[i] >= max_y)
{
columns_row[i] = -1;
}
/* Randomly alternate the column's active status. */
if (get_rand_in_range(0, 1000) == 0)
{
columns_active[i] = (columns_active[i] == 0) ? 1 : 0;
}
}
usleep(ROW_DELAY);
refresh();
}
endwin();
return 0;
}

View file

@ -0,0 +1,185 @@
/*******************************************************************************
*
* Digital ASCII rain - the single thread variant.
* 2012 (C) by Author, 2020 GPL licensed for RossetaCode
*
*******************************************************************************/
#include <assert.h> /* assertions */
#include <conio.h> /* console operations */
#include <locale.h> /* l10n and i18n */
#include <string.h> /* operations on strings and memory blocks */
#include <process.h> /* Microsoft Windows API for threads etc. */
#include <stdio.h> /* standard i/o library */
#include <stdlib.h> /* standard library with rand() function */
#include <time.h> /* time(NULL) used as srand(time(NULL)) */
#include <windows.h> /* just the Microsoft Windows main header for C */
/*
* Global variables - you could use local variables instead...
* but then e.g. the memory for the buffer variable would be unnecessarily
* allocated and released manifold, which would lead to a worse performance.
*/
HANDLE hStdOut; /* the handle to console "out" */
CONSOLE_SCREEN_BUFFER_INFO csbi; /* numbers of rows, columns etc. */
PCHAR_INFO buffer = NULL; /* an enough big buffer */
COORD bufferSize, bufferCoord; /* the size of buffer etc. */
/*
* A structure that holds data that is distinct for each column.
*/
struct Data
{
DWORD armed; /* time in ms to synchronize */
int delay; /* armed = current_time + delay */
BOOL show; /* TRUE - draw, FALSE - erase */
COORD ncp; /* position for drawing new characters */
SMALL_RECT s, d; /* source/destination regions for copy */
};
/*
* A function to generate random numbers from the range (a, b).
* NOTE: POSIX rand () is not thread safe,
* but we use secure rand() from MS runtime libraries.
*/
int rnd(int a, int b)
{
return a + rand() % (b + 1 - a);
}
BOOL randomShow(void)
{
return rnd(0, 99) < 65;
}
int randomDelay(void)
{
return rnd(0, 150) + rnd(0, 150) + rnd(0, 150);
}
BOOL randomRegenerate(void)
{
return rnd(0, 99) < 2;
}
void column_init(struct Data* data, int k)
{
data->armed = 0;
data->show = randomShow();
data->delay = randomDelay();
data->s.Left = k;
data->s.Top = 0;
data->s.Bottom = bufferSize.Y - 2;
data->s.Right = k;
data->d.Left = data->s.Left;
data->d.Top = data->s.Top + 1;
data->d.Right = data->s.Right;
data->d.Bottom = data->s.Bottom + 1;
data->ncp.X = k;
data->ncp.Y = 0;
}
void column_run(struct Data* data)
{
char c;
WORD a;
DWORD result;
/*
* Shift down a column.
*/
ReadConsoleOutput (hStdOut, buffer, bufferSize, bufferCoord, &data->s);
WriteConsoleOutput(hStdOut, buffer, bufferSize, bufferCoord, &data->d);
/*
* If show == TRUE then generate a new character.
* If show == FALSE write the space to erase.
*/
if(data->show)
{
c = (rnd(1,100) <= 15) ? ' ' : rnd( 'a', 'z' );
a = FOREGROUND_GREEN | ((rnd(1,100) > 10) ? 0 : FOREGROUND_INTENSITY);
}
else
{
c = ' ';
a = FOREGROUND_GREEN;
}
WriteConsoleOutputCharacter(hStdOut, &c, 1, data->ncp, &result);
WriteConsoleOutputAttribute(hStdOut, &a, 1, data->ncp, &result);
/*
* Randomly regenerate the delay and the visibility state of the column.
*/
if(randomRegenerate()) data->show = randomShow();
if(randomRegenerate()) data->delay = randomDelay();
data->armed = GetTickCount() + data->delay;
}
int main(int argc, char* argv[])
{
int j;
struct Data* table;
srand((unsigned int)time(NULL));
hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTitle("Digital ASCII rain");
/*
* An attempt to run in the full-screen mode.
*/
if(argc == 1)
{
COORD coord;
CONSOLE_CURSOR_INFO cci;
cci.bVisible = FALSE;
cci.dwSize = 0;
SetConsoleDisplayMode(hStdOut, CONSOLE_FULLSCREEN_MODE, &coord);
SetConsoleCursorInfo(hStdOut, &cci);
}
GetConsoleScreenBufferInfo(hStdOut, &csbi );
//SetConsoleTextAttribute(hStdOut, FOREGROUND_GREEN);
bufferSize.X = 1;
bufferSize.Y = csbi.dwSize.Y - 1;
bufferCoord.X = 0;
bufferCoord.Y = 0;
buffer = (PCHAR_INFO)calloc(bufferSize.Y, sizeof(CHAR_INFO));
assert(buffer != NULL);
table = (struct Data*)calloc(csbi.dwSize.X, sizeof(struct Data));
assert(table != NULL);
for(j = 0; j < csbi.dwSize.X; j++) column_init(&table[j], j);
/*
* Main loop. Sleep(1) significally decreases the CPU load.
*/
while(!_kbhit())
{
DWORD t = GetTickCount();
for(j = 0; j < csbi.dwSize.X; j++)
if(table[j].armed < t) column_run(&table[j]);
Sleep(1);
}
free(table);
free(buffer);
return 0;
}

View file

@ -0,0 +1,204 @@
/*******************************************************************************
*
* Digital ASCII rain - multithreaded.
* 2012 (C) by Author, 2020 GPL licensed for RossetaCode
*
*******************************************************************************/
#include <assert.h> /* assertions */
#include <conio.h> /* console operations */
#include <locale.h> /* l10n and i18n */
#include <string.h> /* operations on strings and memory blocks */
#include <process.h> /* Microsoft Windows API for threads etc. */
#include <stdio.h> /* standard i/o library */
#include <stdlib.h> /* standard library with rand() function */
#include <time.h> /* time(NULL) used as srand(time(NULL)) */
#include <windows.h> /* just the Microsoft Windows main header for C */
//#define SYNCHRONIZED_INIT
/*
* Global variables, common for all threads.
* The handle eventThreadInitialized is to synchronize threads initializations.
*/
#ifdef SYNCHRONIZED_INIT
HANDLE eventThreadInitialized;
#endif
BOOL loop = TRUE; /* FALSE - przerwać działanie */
HANDLE hStdOut; /* uchwyt do konsoli "out" */
CONSOLE_SCREEN_BUFFER_INFO csbi; /* liczby wierszy i kolumn itd.*/
struct InitThreadData
{
int column;
unsigned seed;
};
/*
* A function to generate random numbers from the range (a, b).
* NOTE: POSIX rand () is not thread safe,
* but we use secure rand() from MS runtime libraries.
*/
int rnd(int a, int b)
{
return a + rand() % (b + 1 - a);
}
BOOL randomShow(void)
{
return rnd(0, 99) < 65;
}
int randomDelay(void)
{
return rnd(0,150) + rnd(0,150) + rnd(0,150);
}
BOOL randomRegenerate(void)
{
return rnd(0,99) < 2;
}
/*
* Update a single column.
*/
void one_column(void *arg)
{
BOOL show;
int k;
char c;
WORD a;
int delay;
PCHAR_INFO buffer;
COORD bufferSize, bufferCoord, newCharPosition;
SMALL_RECT s, d;
DWORD result;
/*
* Retrieve the column number, initialize the (pseudo)random numbers
* generator with a "private" seed, check assertions.
*/
k = ((struct InitThreadData*)arg)->column;
srand(((struct InitThreadData*)arg)->seed);
assert(csbi.dwSize.X != 0 && csbi.dwSize.Y != 0);
assert(0 <= k && k <= csbi.dwSize.X);
show = randomShow();
delay = randomDelay();
bufferSize.X = 1;
bufferSize.Y = csbi.dwSize.Y - 1;
bufferCoord.X = 0;
bufferCoord.Y = 0;
buffer = (PCHAR_INFO)malloc(bufferSize.Y * sizeof(CHAR_INFO));
assert( buffer != NULL );
s.Left = k;
s.Top = 0;
s.Bottom = bufferSize.Y - 2;
s.Right = k;
d.Left = s.Left;
d.Top = s.Top + 1;
d.Right = s.Right;
d.Bottom = s.Bottom + 1;
newCharPosition.X = k;
newCharPosition.Y = 0;
#ifdef SYNCHRONIZED_INIT
SetEvent(eventThreadInitialized);
#endif
/*
* Main loop inside this one thread.
*/
while(loop)
{
ReadConsoleOutput ( hStdOut, buffer, bufferSize, bufferCoord, &s);
WriteConsoleOutput( hStdOut, buffer, bufferSize, bufferCoord, &d);
if(show)
{
c = (rnd(1,100) <= 15) ? ' ' : rnd( 'a', 'z' );
a = FOREGROUND_GREEN | ((rnd(1,100) > 1) ? 0 : FOREGROUND_INTENSITY);
}
else
{
c = ' ';
a = FOREGROUND_GREEN;
}
WriteConsoleOutputCharacter( hStdOut, &c, 1, newCharPosition, &result );
WriteConsoleOutputAttribute( hStdOut, &a, 1, newCharPosition, &result );
if(randomRegenerate()) show = randomShow();
if(randomRegenerate()) delay = randomDelay();
Sleep( delay );
}
free(buffer);
}
int main(int argc, char* argv[])
{
int j;
struct InitThreadData *init = NULL;
srand((time_t)time(NULL));
hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTitle("MATRIX");
if(argc == 1)
{
COORD coord;
CONSOLE_CURSOR_INFO cci;
cci.bVisible = FALSE;
cci.dwSize = 0;
SetConsoleDisplayMode(hStdOut,CONSOLE_FULLSCREEN_MODE,&coord);
SetConsoleCursorInfo(hStdOut,&cci);
}
GetConsoleScreenBufferInfo( hStdOut, &csbi );
//SetConsoleTextAttribute(hStdOut,FOREGROUND_GREEN);
#ifdef SYNCHRONIZED_INIT
eventThreadInitialized = CreateEvent(NULL,FALSE,FALSE,NULL);
assert( eventThreadInitialized != NULL );
#endif
init = (struct InitThreadData*)
malloc(csbi.dwSize.X * sizeof(struct InitThreadData));
assert( init != NULL );
for(j = 0; j < csbi.dwSize.X; j++)
{
init[j].column = j;
init[j].seed = rand();
_beginthread(one_column, 0, (void*)&init[j]);
#ifdef SYNCHRONIZED_INIT
WaitForSingleObject(eventThreadInitialized,INFINITE);
#endif
}
getchar();
free(init);
#ifdef SYNCHRONIZED_INIT
CloseHandle(eventThreadInitialized);
#endif SYNCHRONIZED_INIT
return 0;
}

View file

@ -0,0 +1,196 @@
/*******************************************************************************
*
* Digital ASCII rain - multithreaded.
* 2012 (C) by Author, 2020 GPL licensed for RossetaCode
*
*******************************************************************************/
#include <assert.h> /* assertions */
#include <conio.h> /* console operations */
#include <locale.h> /* l10n and i18n */
#include <string.h> /* operations on strings and memory blocks */
#include <process.h> /* Microsoft Windows API for threads etc. */
#include <stdio.h> /* standard i/o library */
#include <stdlib.h> /* standard library with rand() function */
#include <time.h> /* time(NULL) used as srand(time(NULL)) */
#include <windows.h> /* just the Microsoft Windows main header for C */
/*
* Global variables, shared by fibers.
*/
BOOL loop = TRUE;
HANDLE hStdOut;
CONSOLE_SCREEN_BUFFER_INFO csbi;
LPVOID mainFiber = NULL;
struct WorkingFiber
{
int column;
DWORD callAfter;
unsigned seed;
LPVOID fiber;
} *workingFibersTable = NULL;
/*
* A function to generate random numbers from the range (a, b).
* NOTE: POSIX rand () is not thread safe,
* but we use secure rand() from MS runtime libraries.
*/
int rnd(int a, int b)
{
return a + rand() % (b + 1 - a);
}
BOOL randomShow(void)
{
return rnd(0, 99) < 65;
}
int randomDelay(void)
{
return rnd(0,150) + rnd(0,150) + rnd(0,150);
}
BOOL randomRegenerate(void)
{
return rnd(0,99) < 2;
}
VOID CALLBACK one_column(void *arg)
{
struct WorkingFiber *ptr;
BOOL show;
int k;
char c;
WORD a;
int delay;
PCHAR_INFO buffer;
COORD bufferSize, bufferCoord, newCharPosition;
SMALL_RECT s, d;
DWORD result;
ptr = (struct WorkingFiber*)arg;
k = ptr->column;
ptr->callAfter = 0;
srand(ptr->seed);
assert(csbi.dwSize.X != 0 && csbi.dwSize.Y != 0);
assert(0 <= k && k <= csbi.dwSize.X);
show = randomShow();
delay = randomDelay();
bufferSize.X = 1;
bufferSize.Y = csbi.dwSize.Y - 1;
bufferCoord.X = 0;
bufferCoord.Y = 0;
buffer = (PCHAR_INFO)malloc(bufferSize.Y * sizeof(CHAR_INFO));
assert( buffer != NULL );
s.Left = k;
s.Top = 0;
s.Bottom = bufferSize.Y - 2;
s.Right = k;
d.Left = s.Left;
d.Top = s.Top + 1;
d.Right = s.Right;
d.Bottom = s.Bottom + 1;
newCharPosition.X = k;
newCharPosition.Y = 0;
while(loop)
{
ReadConsoleOutput ( hStdOut, buffer, bufferSize, bufferCoord, &s);
WriteConsoleOutput( hStdOut, buffer, bufferSize, bufferCoord, &d);
if(show)
{
c = (rnd(1,100) <= 15) ? ' ' : rnd( 'a', 'z' );
a = FOREGROUND_GREEN | ((rnd(1,100) > 1) ? 0 : FOREGROUND_INTENSITY);
}
else
{
c = ' ';
a = FOREGROUND_GREEN;
}
WriteConsoleOutputCharacter( hStdOut, &c, 1, newCharPosition, &result );
WriteConsoleOutputAttribute( hStdOut, &a, 1, newCharPosition, &result );
if(randomRegenerate()) show = randomShow();
if(randomRegenerate()) delay = randomDelay();
ptr->callAfter = GetTickCount() + delay;
SwitchToFiber(mainFiber);
}
free(buffer);
}
int main(int argc, char* argv[])
{
int j;
srand((unsigned int)time(NULL));
hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTitle("MATRIX - FIBERS");
if(argc == 1)
{
COORD coord;
CONSOLE_CURSOR_INFO cci;
cci.bVisible = FALSE;
cci.dwSize = 0;
SetConsoleDisplayMode(hStdOut,CONSOLE_FULLSCREEN_MODE,&coord);
SetConsoleCursorInfo(hStdOut,&cci);
}
GetConsoleScreenBufferInfo( hStdOut, &csbi );
//SetConsoleTextAttribute(hStdOut,FOREGROUND_GREEN);
mainFiber = ConvertThreadToFiber(NULL);
assert( mainFiber != NULL );
workingFibersTable = (struct WorkingFiber*)
calloc(csbi.dwSize.X, sizeof(struct WorkingFiber));
assert( workingFibersTable != NULL );
for(j = 0; j < csbi.dwSize.X; j++)
{
workingFibersTable[j].column = j;
workingFibersTable[j].callAfter = 0;
workingFibersTable[j].seed = rand();
workingFibersTable[j].fiber = CreateFiber( 0, one_column, &workingFibersTable[j] );
}
loop = TRUE;
while(!_kbhit())
{
DWORD t = GetTickCount();
for(j = 0; j < csbi.dwSize.X; j++)
if(workingFibersTable[j].callAfter < t)
SwitchToFiber( workingFibersTable[j].fiber );
Sleep(1);
}
loop = FALSE;
for(j = 0; j < csbi.dwSize.X; j++)
{
SwitchToFiber( workingFibersTable[j].fiber );
DeleteFiber(workingFibersTable[j].fiber);
}
free(workingFibersTable);
return 0;
}