Vydělávej až 160.000 Kč měsíčně! Akreditované rekvalifikační kurzy s garancí práce od 0 Kč. Více informací.
Hledáme nové posily do ITnetwork týmu. Podívej se na volné pozice a přidej se do nejagilnější firmy na trhu - Více informací.

Satik

Console demo

c-plus-plus

#include <windows.h>

//using namespace std;
// Autor: Satik, na kód radši moc nekoukat, docela prasárna ;)

HANDLE hout;

const int width=32;
const int height=16;
const int pcount = 128;
int beepType = 0;

struct Particle
{
	int x;
	int y;
	int vx;
	int vy;
	int life;
};

struct BufferItem
{
	char c;
	char color1;
	char color2;
};

BufferItem * buffer;

Particle particles[pcount];

int getFreqAsm(int tone)
{
	int tmp;
	const int dvanact=12;
	const int dve=2;
	const int frq=440;
	tone=(tone-49);

	__asm 
	{
		finit;
		fild [tone];
		fidiv [dvanact];
		fild [dve];
		fyl2x;
		fist tmp;
		fild tmp;
		fsub;
		f2xm1;
		fld1;
		fadd;
		fild tmp;
		fxch;
		fscale;
		fimul [frq];
		fistp [tmp];
	}

	return tmp;
}

DWORD WINAPI beep(LPVOID)
{
	for (int i=0; i<120; i++)
	{
		int f;
		if (beepType==1)
			f = 37+(((i*13+17) & 19) + 19)%12;
		else
			f = 20+i;

		Beep(getFreqAsm(f),beepType==1?125:200);
	}
	return 0;
}

int myrand(int max)
{
	static int seed1=0xf107a4e3;
	static int seed2=0xabcdef12;
	seed1*=7;
	seed1+=13;
	seed2*=5;
	seed2+=23;

	int ret = ((seed1^seed2) % max) & 0x00ffffff;

	if (ret>max)
	{
		ret+=16777215;
		ret %= max;
	}

	return ret;
}

int abs(int x)
{
	if (x>0) return x;
	else return -x;
}

int sqrt(int a)
{
	if (a==0) return 0;

	int x=a;
	for (int i=0; i<8; i++)
	{
		x=(x+a/x)/2;
	}
	
	return x;
}

void putChar(char * c)
{
	DWORD dwRet;
	WriteConsole(hout, c, 1, &dwRet, 0);
}

void setcolor(int textcol,int backcol)
{
	unsigned short wAttributes= ((unsigned)backcol<<4)|(unsigned)textcol;
	SetConsoleTextAttribute(hout, wAttributes);
}

void SetColor(int index, byte r, byte g, byte b)
{
	CONSOLE_SCREEN_BUFFER_INFOEX info;
    info.cbSize = sizeof(info);
    GetConsoleScreenBufferInfoEx(hout, &info);
	info.srWindow.Right=width+16;//80;
	info.srWindow.Bottom=height+8;//25;
    info.ColorTable[index] = RGB(r, g, b);  
    SetConsoleScreenBufferInfoEx(hout, &info);
}

void * __cdecl operator new(unsigned int bytes)
{
  return HeapAlloc(GetProcessHeap(), 0, bytes);
}
 
void __cdecl operator delete(void *ptr)
{
  if (ptr) HeapFree(GetProcessHeap(), 0, ptr);
}

void drawBuffer()
{
	COORD c2;

	BufferItem previous = buffer[0];
	setcolor(buffer[0].color1,buffer[0].color2);

	for (int y=0; y<height; y++)
	{
		c2.X=0;
		c2.Y=y;
		SetConsoleCursorPosition(hout, c2);
		for (int x=0; x<width; x++)
		{
			if (previous.color1!=buffer[x+y*width].color1 || previous.color2!=buffer[x+y*width].color2)
			{
				setcolor(buffer[x+y*width].color1,buffer[x+y*width].color2);
				previous = buffer[x+y*width];
			}

			putChar(&buffer[x+y*width].c);
		}
	}
}

void clearBuffer()
{
	for (int y=0; y<height; y++)
		for (int x=0; x<width; x++)
		{
			buffer[x+y*width].c=' ';
			buffer[x+y*width].color1=0;
			buffer[x+y*width].color2=0;
		}
}

void drawOhnostroj(int frame)
{
	clearBuffer();

	// vytvori ohnostroj
	if (frame%32==0)
	{
		for (int i=0; i<pcount; i++)
		{
			particles[i].x=width/2*1000;
			particles[i].y=(height/3+1)*1000;
			particles[i].vy=(myrand(21)-10)*90;
			particles[i].vx=(myrand(21)-10)*90;
			if (sqrt(particles[i].vy*particles[i].vy+particles[i].vx*particles[i].vx/2)>900)
			{
				i--;
			}
			else
				particles[i].life=128+myrand(128);
		}

		// nastavi barvy
		int rmax=128+myrand(2)*127;
		int gmax=128+myrand(2)*127;
		int bmax=128+myrand(2)*127;

		for (int i=0; i<13; i++)
		{
			SetColor(i, rmax/12*i,gmax/12*i,bmax/12*i); // ohno
		}
	}

	// ohnostroj
	for (int i=0; i<pcount; i++)
	{
		// fyzika
		particles[i].x=particles[i].x+particles[i].vx;
		particles[i].y=particles[i].y+particles[i].vy;
		particles[i].vy=particles[i].vy+10;
		// vykreslit
		int x=particles[i].x/1000;
		int y=particles[i].y/1000;

		if (particles[i].life>0)
			if (x>=0 && x<width && y>=0 && y<height)
			{
				buffer[x+y*width].c='*';
				buffer[x+y*width].color1=particles[i].life/22;
				buffer[x+y*width].color2=0;
			}
			
		particles[i].life-=8;
	}

	drawBuffer();
}

struct Portal
{
	int distance;
	char character;
	char color1;
	char color2;
};

void drawPortal(Portal portal, int distance)
{
	int dist=portal.distance-distance;
	dist;
	if (dist<-5) return;

	int coord=0;
	const int sto = 100;
	const int jedna = 1;
	const int mpet = -5;
	const int e=272;

	int tmp=0;

	//coord = (int)(pow(2.72f,-(float)dist/5)*100);
	__asm 
	{
		finit;
		fild [dist];
		fidiv [mpet];
		fild [e];
		fidiv [sto];
		fyl2x;
		fist tmp;
		fild tmp;
		fsub;
		f2xm1;
		fld1;
		fadd;
		fild tmp;
		fxch;
		fscale;
		fimul [sto];
		fistp [coord];
	}

	if (coord<1) return;
	if (coord>100) coord = 100;

	int x2 = width/2;
	int y2 = height/2;
	
	int sizeX = x2*coord/100;
	int sizeY = y2*coord/100;

	 // fill
	for (int y=y2-sizeY; y<y2+sizeY; y++)
		for (int x=x2-sizeX; x<x2+sizeX; x++)
		{
			buffer[x+y*width].c=portal.character;
			buffer[x+y*width].color1=portal.color1;
			buffer[x+y*width].color2=portal.color2;
		}
}

void prulet()
{
	HANDLE hThread = CreateThread(NULL, 0, beep, NULL, 0, NULL);

	const int portalcount = 100;

	Portal portals[portalcount];

	bool up=true;

	for (int i=1; i<=10; i++)
	SetColor(i, i*25,i*25,i*25);

	for (int i=0; i<portalcount; i++)
	{
		portals[i].character=(char)219;
		portals[i].color1 = 1;
		
		portals[i].color2 = 0;
		portals[i].distance = 2;
		if (i>0)
		{
			if (up)
				portals[i].color1 = portals[i-1].color1+1;
			else
				portals[i].color1 = portals[i-1].color1-1;
			
			if (portals[i].color1==10) up=false;
			if (portals[i].color1==1) up=true;

			portals[i].distance = portals[i-1].distance+2;
		}
	}

	for (int i=0; i<100; i++)
	{
		clearBuffer();
		for (int j=0; j<portalcount; j++)
			drawPortal(portals[j], i);
		drawBuffer();
		
		for (int j=1; j<=10; j++)
		{
			int r=255;
			int g=20+i*2;
			int b=250-i*2;
			SetColor(j, r*j*25/250,g*j*25/250,b*j*25/250);
		}

		Sleep(50);
	}

	TerminateThread(hThread, 0);
	CloseHandle(hThread);
}

void main()
{
	buffer = new BufferItem[width*height];

	hout = GetStdHandle(STD_OUTPUT_HANDLE);

	prulet();
	beepType=1;

	Sleep(300);

	HANDLE hThread = CreateThread(NULL, 0, beep, NULL, 0, NULL);

	for (int i=0; i<8*32; i++)
		{
			drawOhnostroj(i);
			Sleep(20);
		}

	CloseHandle(hThread);

	delete buffer;
}

Neformátovaný

Přidáno: 15.2.2013
Expirace: Neuvedeno

Aktivity