-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
473 lines (373 loc) · 10.4 KB
/
main.cpp
File metadata and controls
473 lines (373 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#define logow 40
#define logoh 10
#define LX 8
#define LY 16
#define SFONT "Courier New"
#define ACTORS_VELOCITY 20
#define COLOR_ACTOR 0xB0FFB0
#define COLOR_MATRIXMAXR 0x00
#define COLOR_MATRIXMAXG 0xFF
#define COLOR_MATRIXMAXB 0x00
#define COLOR_MATRIXMINR 0x00
#define COLOR_MATRIXMING 0x00
#define COLOR_MATRIXMINB 0x00
#define COLOR_LOGO 0x00FF00
#define COLOR_LOGO_EDGE 0x00FFFF
#define COLOR_LOGO_TITLE 0xB0FFB0
#define SYMBOL_0 '0'
#define SYMBOL_1 '1'
#define ERROR_TITLE "Îøèáêà"
#define ERROR_CANTREGISTERClASS "Íå óäàëîñü çàðåãèñòðèðîâàòü êëàññ îêíà"
#define ERROR_CANTCREATEWINDOW "Íå óäàëîñü ñîçäàòü îêíî"
HINSTANCE hInstance;
int w, h;
int ws, hs, xs, ys;
int Random(int from, int to){
return rand() % (to-from+1) + from;
}
class Window{
protected:
HWND hWnd;
private:
int RegisterClass(){
LOGBRUSH lb;
lb.lbColor = 0x000000;
lb.lbStyle = BS_SOLID;
lb.lbHatch = NULL;
HBRUSH brush = CreateBrushIndirect(&lb);
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WindowProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hIconSm = NULL;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = brush;
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "MySSWindow";
return RegisterClassEx(&wcex);
}
int Create(Window *pobj){
hWnd = CreateWindow("MySSWindow", "ScreenSaver", WS_POPUP, 0, 0, w, h, NULL, NULL, hInstance, pobj);
if(!hWnd) return FALSE;
ShowWindow(hWnd, SW_SHOWMAXIMIZED);
UpdateWindow(hWnd);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
PAINTSTRUCT ps;
HDC hdc;
Window *pWindow;
pWindow = (Window *)GetWindowLong(hWnd, GWL_USERDATA);
switch(uMsg){
case WM_TIMER:
pWindow->OnTimer();
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
pWindow->OnDraw(hdc);
EndPaint(hWnd, &ps);
break;
case WM_KEYDOWN:
pWindow->OnKeyUp(wParam);
break;
case WM_DESTROY:
pWindow->OnDestroy();
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}
public:
Window(){
if(!RegisterClass()){
MessageBox(0, ERROR_CANTREGISTERClASS, ERROR_TITLE, MB_ICONERROR);
ExitProcess(0);
}
}
void Init(Window *pobj){
if(!Create(pobj)){
MessageBox(0, ERROR_CANTCREATEWINDOW, ERROR_TITLE, MB_ICONERROR);
ExitProcess(0);
}
MainLoop();
}
int MainLoop(){
MSG msg;
while(GetMessage(&msg, NULL, 0, 0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
if(uMsg == WM_NCCREATE) SetWindowLong(hwnd, GWL_USERDATA, (LONG)((LPCREATESTRUCT)lParam)->lpCreateParams);
Window *pWindow;
pWindow = (Window *)GetWindowLong(hwnd, GWL_USERDATA);
return pWindow->WndProc(hwnd, uMsg, wParam, lParam);
}
virtual void OnKeyUp(WPARAM KeyCode){}
virtual void OnDraw(HDC hDC){}
virtual void OnTimer(){}
virtual void OnDestroy(){}
};
class Symbols{
private:
char *pSymbols, *oldpSymbols;
int *pColors, *oldpColors;
public:
Symbols(){
pSymbols = new char [ws*hs];
oldpSymbols = new char [ws*hs];
pColors = new int [ws*hs];
oldpColors = new int [ws*hs];
for(int i=0; i<ws; i++){
for(int j=0; j<hs; j++){
pSymbols[j*ws+i] = ' ';
pColors[j*ws+i] = (COLOR_MATRIXMAXB<<16)+(COLOR_MATRIXMAXG<<8)+COLOR_MATRIXMAXR;
oldpSymbols[j*ws+i] = '\0';
oldpColors[j*ws+i] = 0;
}
}
}
char forcegetSymbol(int x, int y){
if(x<0 || x>=ws || y<0 || y>=hs) MessageBox(0, "forcegetSymbol leak!", "err", MB_ICONERROR);
return pSymbols[y*ws+x];
}
char getSymbol(int x, int y){
if(x<0 || x>=ws || y<0 || y>=hs) MessageBox(0, "getSymbol leak!", "err", MB_ICONERROR);
if(pSymbols[y*ws+x] != oldpSymbols[y*ws+x] || pColors[y*ws+x] != oldpColors[y*ws+x]){
oldpSymbols[y*ws+x] = pSymbols[y*ws+x];
oldpColors[y*ws+x] = pColors[y*ws+x];
return pSymbols[y*ws+x];
}
return 0;
}
int getColor(int x, int y){
if(x<0 || x>=ws || y<0 || y>=hs) MessageBox(0, "getColor leak!", "err", MB_ICONERROR);
return pColors[y*ws+x];
}
void setActorTrack(int x, int y, int c){
if(x<0 || x>=ws || y<0 || y>hs || c<0 || c>1) MessageBox(0, "setActorTrack leak!", "err", MB_ICONERROR);
unsigned int rc, r, g, b;
int nn = Random(0, 1)?SYMBOL_0:SYMBOL_1;
if(c == 1){
if(y < hs){
pSymbols[y*ws+x] = nn;
pColors[y*ws+x] = COLOR_ACTOR;
}
if(y>0){
pSymbols[(y-1)*ws+x] = nn;
rc = Random(1, 255);
r = (COLOR_MATRIXMAXR-COLOR_MATRIXMINR)*rc/255+COLOR_MATRIXMINR;
g = (COLOR_MATRIXMAXG-COLOR_MATRIXMING)*rc/255+COLOR_MATRIXMING;
b = (COLOR_MATRIXMAXB-COLOR_MATRIXMINB)*rc/255+COLOR_MATRIXMINB;
pColors[(y-1)*ws+x] = (b<<16)+(g<<8)+r;
}
} else{
if(y < hs) pSymbols[y*ws+x] = ' ';
}
}
};
class Actors{
private:
struct tagActor{
int pos;
int col;
} *pActors;
public:
Actors(){
pActors = new tagActor [ws];
for(int i=0; i<ws; i++){
pActors[i].pos = -1;
pActors[i].col = -1;
}
}
int getPosition(int nActor){
if(nActor<0 || nActor>=ws) MessageBox(0, "getPosition leak!", "err", MB_ICONERROR);
return pActors[nActor].pos;
}
int getType(int nActor){
if(nActor<0 || nActor>=ws) MessageBox(0, "getType leak!", "err", MB_ICONERROR);
return pActors[nActor].col;
}
void NextStep(){
int i;
for(i=0; i<ws; i++){
if(pActors[i].pos >= 0) pActors[i].pos++;
if(pActors[i].pos > hs) pActors[i].pos = -1;
}
}
void SelectRndActor(){
int h;
do{
h = Random(0, ws-1);
if(h<0 || h>=ws) MessageBox(0, "SelectRndActor leak!", "err", MB_ICONERROR);
} while(pActors[h].pos != -1);
pActors[h].pos = 0;
pActors[h].col = (Random(1, 5) != 5);
}
};
class MatrixHY{
private:
Actors *pActors;
Symbols *pSymbols;
enum tagState{
LOGO_HIDE, LOGO_SHOW, TEXT_SHOW
} State;
public:
MatrixHY(){
State = LOGO_HIDE;
pActors = new Actors;
pSymbols = new Symbols;
}
void ReCalculate(){
if(State == TEXT_SHOW){
} else{
pActors->SelectRndActor();
for(int i=0; i<ws; i++){
if(pActors->getPosition(i) >= 0) pSymbols->setActorTrack(i, pActors->getPosition(i), pActors->getType(i));
}
pActors->NextStep();
}
}
void ToggleState(){
State = (State == LOGO_HIDE)?LOGO_SHOW:LOGO_HIDE;
}
unsigned int getColor(int x, int y){
if(State == LOGO_SHOW && x>=xs && x<xs+logow && y>=ys && y<ys+logoh){
if(y == ys || x == xs || x == xs+logow-1 || y == ys+logoh-1) return COLOR_LOGO_EDGE;
if(y == ys+1) return COLOR_LOGO_TITLE;
else return COLOR_LOGO;
}
return pSymbols->getColor(x, y);
}
char getSymbol(int x, int y){
static char buff[10][43] ={ "+--------------------------------------+\n",
"| 'Matrix has you' screensaver v. 2.00 |\n",
"| |\n",
"| + new font |\n",
"| + this 'About...' |\n",
"| + Stuff have been deleted |\n",
"| + rewrited on C++ |\n",
"| Have fun! |\n",
"| © SharpC, 2005 |\n",
"+--------------------------------------+\n"};
if(x>=xs && x<xs+logow && y>=ys && y<ys+logoh){
if(y-ys<0 || y-ys>=logoh || x-xs<0 || x-xs>=logow) MessageBox(0, "SelectRndActor leak!", "err", MB_ICONERROR);
if(State == LOGO_SHOW) return buff[y-ys][x-xs];
else return pSymbols->forcegetSymbol(x, y);
}
return pSymbols->getSymbol(x, y);
}
};
class MySSWindow: public Window{
private:
MatrixHY *pMatrix;
UINT_PTR timerId;
UINT_PTR nTimer1;
HFONT hFont;
void DrawBorder(HDC hDC){
int x1, y1, x2, y2;
x1 = xs*LX+LX/2;
y1 = ys*LY+LY/2-3;
x2 = (xs+logow)*LX-LX/2;
y2 = (ys+logoh)*LY-LY/2+3;
MoveToEx(hDC, x1, y1, NULL);
LineTo(hDC, x2, y1);
LineTo(hDC, x2, y2);
LineTo(hDC, x1, y2);
LineTo(hDC, x1, y1);
x1+=3; y1+=3; x2-=3; y2-=3;
MoveToEx(hDC, x1, y1, NULL);
LineTo(hDC, x2, y1);
LineTo(hDC, x2, y2);
LineTo(hDC, x1, y2);
LineTo(hDC, x1, y1);
}
public:
MySSWindow(){
pMatrix = new MatrixHY;
Init(this);
}
void OnCreate(){
timerId = 0;
nTimer1 = 1;
if(!timerId) timerId = SetTimer(hWnd, nTimer1, ACTORS_VELOCITY, NULL);
hFont = CreateFont(LY, LX, 0, 0, 400, 0, 0, 0, \
RUSSIAN_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, \
DEFAULT_QUALITY, DEFAULT_PITCH | FF_SCRIPT, SFONT);
char buff[128];
//wsprintf(buff, "hFont = %d", hFont);
//MessageBox(0, buff, "hFont", MB_ICONASTERISK);
//SendMessage(hWnd, WM_SETFONT, (WPARAM)hFont, TRUE);
//SelectObject(GetDC(hWnd), hFont);
}
void OnDestroy(){
KillTimer(hWnd, nTimer1);
DeleteObject(hFont);
}
void OnKeyUp(WPARAM KeyCode){
switch(KeyCode){
case 0x41:
pMatrix->ToggleState();
RECT wr;
wr.left = xs*LX;
wr.top = ys*LY;
wr.right = (xs+logow)*LX;
wr.bottom = (ys+logoh)*LY;
InvalidateRect(hWnd, &wr, 1);
break;
case VK_ESCAPE:
ExitProcess(0);
break;
}
}
void OnDraw(HDC hDC){
static int firstCall = 0;
if(!firstCall){
firstCall = 1;
OnCreate();
}
RECT r;
HPEN hPen;
SelectObject(hDC, hFont);
SetBkColor(hDC, (COLORREF)0x000000);
char b[2]=" ";
unsigned int c;
for(int i=0; i<ws; i++){
for(int j=0; j<hs; j++){
if(b[0] = pMatrix->getSymbol(i, j)){
c = pMatrix->getColor(i, j);
if(c) SetTextColor(hDC, (COLORREF)c);
r.left = i*LX; r.top = j*LY; r.right = (i+1)*LX; r.bottom = (j+1)*LY;
DrawText(hDC, b, 1, &r, 0);
}
}
}
}
void OnTimer(){
pMatrix->ReCalculate();
OnDraw(GetDC(hWnd));
}
};
int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR sCmdLine, int nCmdShow){
hInstance = hInst;
w = GetSystemMetrics(SM_CXSCREEN);
h = GetSystemMetrics(SM_CYSCREEN);
ws = w/LX;
hs = h/LY;
xs=(ws-logow)/2;
ys=(hs-logoh)/2;
srand(GetTickCount());
MySSWindow MyWindow;
ExitProcess(0);
}