Ветеран
Сообщения: 1180
Благодарности: 279
|
Профиль
|
Отправить PM
| Цитировать
кому понравится - дорабатывайте, только не забудьте меня добрым именем в about помянуть 
если не хватает какие библиотечек - пишите, дошлю.
Код: 
#define UNICODE
#define _UNICODE
#include <windows.h>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;
//---------------------------------------------------------------------------
class sampler_t
{
public:
sampler_t(const double& dt);
void set_dt(const double& dt) {_dt=dt;}
double get();
private:
double _t, _dt, _a, _b, _c, _d;
double _p[4];
double _get();
};
inline double sampler_t::_get()
{
return double(rand())/RAND_MAX;
}
sampler_t::sampler_t(const double& dt) :
_t(), _dt(dt), _a(), _b(), _c(), _d()
{
_p[0] = _get();
_p[1] = _get();
_p[2] = _get();
_p[3] = _get();
}
double sampler_t::get()
{
_t += _dt;
if (_t>=1.)
{
_t -= 1.;
_p[0] = _p[1];
_p[1] = _p[2];
_p[2] = _p[3];
_p[3] = _get();
_a = -0.5*_p[0] + 1.5*_p[1] - 1.5*_p[2] + 0.5*_p[3];
_b = 1.0*_p[0] - 2.5*_p[1] + 2.0*_p[2] - 0.5*_p[3];
_c = -0.5*_p[0] + 0.5*_p[2];
_d = _p[1];
}
double t2=_t*_t, t3=t2*_t;
return min(1., max(0., _a*t3 + _b*t2 + _c*_t + _d));
}
//---------------------------------------------------------------------------
class job_t
{
public:
~job_t();
job_t();
void operator()();
private:
vector<unsigned char> _bm_data;
BITMAPINFO* _bmi;
unsigned char* _bits;
sampler_t _sampler_main, _sampler_freq;
unsigned _bar, _arrow_color;
const wchar_t* _atom;
HWND _window;
HFONT _font;
void _fill_bits(unsigned char* first, unsigned char* last);
void render_all();
void cpu_step();
static long __stdcall _wnd_proc(HWND hwnd, unsigned code, unsigned wparam, long lparam);
};
job_t job;
job_t::~job_t()
{
DeleteObject(_font);
DestroyWindow(_window);
}
job_t::job_t() :
_bm_data(800*600 + 256*4 + sizeof(BITMAPINFOHEADER)),
_bmi (reinterpret_cast<BITMAPINFO*>(&_bm_data[0])),
_bits (&_bm_data[256*4 + sizeof(BITMAPINFOHEADER)]),
_sampler_main(.102134941),
_sampler_freq(.1034941),
_bar(),
_arrow_color(0x00ffff),
_font(CreateFont(32, 16, 0, 0, FW_BOLD, 0, 0, 0, 0,
OUT_RASTER_PRECIS, 0, 0, FIXED_PITCH, 0))
{
_bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
_bmi->bmiHeader.biWidth = 800;
_bmi->bmiHeader.biHeight = 600;
_bmi->bmiHeader.biPlanes = 1;
_bmi->bmiHeader.biBitCount = 8;
WNDCLASS wcla = {0, _wnd_proc, 0, 4, GetModuleHandle(0),
LoadIcon(0, IDI_APPLICATION), 0,
/*HBRUSH(COLOR_WINDOW + 1)*/0, 0, L"Window"};
_atom = reinterpret_cast<const wchar_t*>(RegisterClass(&wcla));
_window = CreateWindowEx(WS_EX_TOPMOST, _atom, L"GDI+ Window", WS_POPUP|WS_VISIBLE|WS_MAXIMIZE,
10, 10, 800, 600, 0, 0, 0, 0);
SetTimer(_window, 101, 1000/25, 0);
SetTimer(_window, 102, 973, 0);
SetTimer(_window, 103, 333, 0);
ShowCursor(false);
}
void job_t::operator()()
{
MSG cmsg;
// while(PeekMessage(&cmsg, _window, 0x200, 0x20a, PM_REMOVE)) {}
// while(PeekMessage(&cmsg, _window, 0x100, 0x107, PM_REMOVE)) {}
while(GetMessage(&cmsg, 0, 0, 0)>0)
{
DispatchMessage(&cmsg);
}
}
void job_t::render_all()
{
_fill_bits(_bits, _bits + _bmi->bmiHeader.biWidth*_bmi->bmiHeader.biHeight);
long frame_pos = 10 + 50./(_sampler_main.get() + 1.);
HDC dc = GetDC(_window);
SetBkMode(dc, TRANSPARENT);
SelectObject(dc, _font);
RECT rect;
GetClientRect(_window, &rect);
StretchDIBits(dc, 0, 0, rect.right, rect.bottom,
0, 0, _bmi->bmiHeader.biWidth, _bmi->bmiHeader.biHeight,
_bits, _bmi, DIB_RGB_COLORS, SRCCOPY);
// SetDIBitsToDevice(dc, 0, 0, _bmi->bmiHeader.biWidth, _bmi->bmiHeader.biHeight,
// 0, 0, 0, _bmi->bmiHeader.biHeight, _bits, _bmi, 0);
static wchar_t text[] = L"Searching [012345678901234567890123456789]";
wchar_t* caret = text + 12 + (_bar);
fill(text + 11, caret, '|');
fill(caret, text + 11 + 30, '.');
SetTextColor(dc, 0x00ff00);
TextOut(dc, 80, frame_pos, text, 12 + 30);
static wchar_t arrows[] = L">>>";
SetTextColor(dc, _arrow_color);
TextOut(dc, 10, frame_pos, arrows, 3);
ReleaseDC(_window, dc);
}
void job_t::cpu_step()
{
if (28<_bar) _bar=0;
_sampler_main.set_dt(0.02*pow(1.12234, ++_bar));
//_sampler_main.set_dt(0.00342143897*_sampler_freq.get());
}
void job_t::_fill_bits(unsigned char* first, unsigned char* last)
{
{
double gain = 0.93 + 0.06*_sampler_freq.get();
unsigned* colors = reinterpret_cast<unsigned*>(&_bm_data[sizeof(BITMAPINFOHEADER)]);
for (unsigned n=0; n<256; ++n)
{
colors[n] = 0x10101*unsigned(255.*pow(gain,n));
}
}
for (; first!=last; ++first)
{
first[0] = _sampler_main.get()*255;
}
}
long __stdcall job_t::_wnd_proc(HWND hwnd, unsigned code, unsigned wparam, long lparam)
{
static long __mouse_lparam = -1;
if (code==WM_PAINT)
{
ValidateRect(hwnd, 0);
}
else if (code==WM_TIMER)
{
switch (wparam)
{
case 101 : job.render_all(); break;
case 102 : job.cpu_step(); break;
case 103 : job._arrow_color^=0xffffff; break;
}
}
else if (WM_MOUSEFIRST <= code && code <= WM_MOUSELAST)
{
if (__mouse_lparam!=-1 && __mouse_lparam!=lparam)
{
PostQuitMessage(0);
}
__mouse_lparam = lparam;
}
else if (code==WM_CLOSE || (WM_KEYFIRST <= code && code <= WM_KEYLAST))
{
PostQuitMessage(0);
}
else
{
return DefWindowProc(hwnd, code, wparam, lparam);
}
return 0;
}
int __stdcall WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
job();
}
catch(exception& e)
{
MessageBoxA(0, e.what(), 0, MB_OK|MB_ICONERROR);
}
return 0;
}
//---------------------------------------------------------------------------
|