-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.asm
More file actions
1007 lines (838 loc) · 23.3 KB
/
gui.asm
File metadata and controls
1007 lines (838 loc) · 23.3 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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
proc drawField uses ecx edi ebx edx ebp esi
mov eax, [FieldSize]
mul [FieldSize]
mov ecx, eax
mov edi, [FieldAddr]
mov ebx, [XFieldOffset] ; X coords offset
mov ebp, [YFieldOffset] ; Y coords offset
; mov esi, [FoodAddr] ; will store current food addr (needed to get food amount quickly)
; mov edx, [AgentsAddr] ; will store current agent addr
.GoThoughFieldCells:
mov eax, EMPTY_COLOR ; storing there color
test dword[edi], FIELD_AGENT_STATE
jz @F
mov eax, dword[edi]
and eax, FIELD_SAFE_MASK
mov esi, [AgentsAddr]
mul [AgentRecSize]
add esi, eax
movzx eax, word[esi + AGENT_ENERGY_OFFSET]
stdcall CalcAgentColor, eax
add edx, [AgentRecSize]
jmp .stopColorSelection
@@:
test dword[edi], FIELD_FOOD_STATE
jz .stopColorSelection
mov eax, dword[edi]
and eax, FIELD_SAFE_MASK
mov esi, [FoodAddr]
mul [FoodRecSize]
add esi, eax
movzx eax, word[esi + FOOD_AMOUNT_OFFSET]
stdcall CalcFoodColor, eax
.stopColorSelection:
stdcall DrawRect, [ScreenBufAddr], ebx, ebp, [CellSizePX], [CellSizePX], eax
add ebx, [CellSizePX]
mov eax, [FieldZoneWidth]
sub eax, [XFieldOffset]
cmp ebx, eax
jb @F
.NextLine:
add ebp, [CellSizePX]
mov ebx, [XFieldOffset]
@@:
add edi, FIELD_CELL_SIZE
dec ecx
cmp ecx, 0
ja .GoThoughFieldCells
invoke SetDIBitsToDevice, [hBufDC], [FieldXInOffset], [FieldYInOffset], [FieldZoneWidth], [FieldZoneHeight], 0, 0, 0, [FieldZoneHeight], [ScreenBufAddr], bmi, 0
ret
endp
proc ShowHints
local rect RECT
push [lf.lfHeight]
mov [lf.lfHeight], HINT_FONT_SIZE
invoke CreateFontIndirect, lf
invoke SelectObject, [hBufDC], eax
mov [rect.left], TEXT_MARGIN_LEFT
mov eax, [FieldXInOffset]
mov [rect.right], eax
mov eax, [ScreenHeight]
sub eax, TEXT_MARGIN_TOP
sub eax, HINT_FONT_SIZE * 3
mov [rect.bottom], eax
sub eax, HINT_FONT_SIZE * 5
mov [rect.top], eax
lea eax, [rect]
invoke DrawText, [hBufDC], Hint1, -1, eax, DT_LEFT
cmp [InitedHint], 1
je @F
invoke BitBlt, [hMainDc], 0, 0, [ScreenWidth], [ScreenHeight], [hBufDC], 0, 0, SRCCOPY
invoke Sleep, 200
@@:
add [rect.bottom], HINT_FONT_SIZE * 2
add [rect.top], HINT_FONT_SIZE * 2
lea eax, [rect]
invoke DrawText, [hBufDC], Hint2, -1, eax, DT_LEFT
cmp [InitedHint], 1
je @F
invoke BitBlt, [hMainDc], 0, 0, [ScreenWidth], [ScreenHeight], [hBufDC], 0, 0, SRCCOPY
invoke Sleep, 200
@@:
add [rect.bottom], HINT_FONT_SIZE * 2
add [rect.top], HINT_FONT_SIZE * 2
lea eax, [rect]
invoke DrawText, [hBufDC], Hint3, -1, eax, DT_LEFT
cmp [InitedHint], 1
je @F
invoke BitBlt, [hMainDc], 0, 0, [ScreenWidth], [ScreenHeight], [hBufDC], 0, 0, SRCCOPY
invoke Sleep, 200
@@:
add [rect.bottom], HINT_FONT_SIZE * 2
add [rect.top], HINT_FONT_SIZE * 2
lea eax, [rect]
invoke DrawText, [hBufDC], Hint4, -1, eax, DT_LEFT
invoke BitBlt, [hMainDc], 0, 0, [ScreenWidth], [ScreenHeight], [hBufDC], 0, 0, SRCCOPY
pop [lf.lfHeight]
invoke CreateFontIndirect, lf
invoke SelectObject, [hBufDC], eax
mov [InitedHint], 1
ret
endp
proc PrintStats
local rect RECT
; tact number
stdcall IntToStr, [TotalTacts], numStr ; Assuming tactNStrStartI is the starting index for the number in tactNStr
mov [rect.left], TEXT_MARGIN_LEFT
mov eax, [maxTextWidth]
add [rect.left], eax
mov eax, [FieldXInOffset]
mov [rect.right], eax
mov [rect.top], TEXT_MARGIN_TOP
mov [rect.bottom], TEXT_FONT_SIZE * 2
lea eax, [rect]
invoke FillRect, [hBufDC], eax, [bkgBrush]
lea eax, [rect]
invoke DrawText, [hBufDC], numStr, -1, eax, DT_LEFT
stdcall IntToStr, [AgentsSize], numStr ; Assuming tactNStrStartI is the starting index for the number in tactNStr
mov [rect.left], TEXT_MARGIN_LEFT
mov eax, [maxTextWidth]
add [rect.left], eax
mov eax, [FieldXInOffset]
mov [rect.right], eax
mov [rect.top], TEXT_FONT_SIZE * 2 + TEXT_MARGIN_TOP
mov [rect.bottom], TEXT_FONT_SIZE * 2 + TEXT_MARGIN_TOP + TEXT_FONT_SIZE * 2
lea eax, [rect]
invoke FillRect, [hBufDC], eax, [bkgBrush]
lea eax, [rect]
invoke DrawText, [hBufDC], numStr, -1, eax, DT_LEFT
stdcall IntToStr, [FoodSize], numStr ; Assuming tactNStrStartI is the starting index for the number in tactNStr
mov [rect.left], TEXT_MARGIN_LEFT
mov eax, [maxTextWidth]
add [rect.left], eax
mov eax, [FieldXInOffset]
mov [rect.right], eax
mov [rect.top], TEXT_FONT_SIZE * 2 + TEXT_MARGIN_TOP + TEXT_FONT_SIZE * 2
mov [rect.bottom], TEXT_FONT_SIZE * 2 + TEXT_MARGIN_TOP + TEXT_FONT_SIZE * 2 + TEXT_FONT_SIZE * 2
lea eax, [rect]
invoke FillRect, [hBufDC], eax, [bkgBrush]
lea eax, [rect]
invoke DrawText, [hBufDC], numStr, -1, eax, DT_LEFT
ret
endp
proc BufMoveAgent uses ecx edi edx ebx, src, dest, energy
stdcall CalcAgentColor, [energy]
mov ecx, eax
stdcall bufUpdateCellColor, [src], EMPTY_COLOR
stdcall bufUpdateCellColor, [dest], ecx
ret
endp
proc BufCloneCell uses ecx, src, dest, energy
stdcall CalcAgentColor, [energy]
mov ecx, eax
stdcall bufUpdateCellColor, [src], ecx
stdcall bufUpdateCellColor, [dest], ecx
ret
endp
proc bufClearCell uses ecx edi edx ebx, src
stdcall bufUpdateCellColor, [src], EMPTY_COLOR
ret
endp
proc bufUpdateCellColor uses ecx edi edx ebx, src, color
local screenX dd ?
local screenY dd ?
mov eax, [src]
xor edx, edx
div [FieldSize]
mov [screenX], edx
mov [screenY], eax
mov eax, [screenY]
mul [CellSizePX]
add eax, [YFieldOffset]
mov [screenY], eax
mov eax, [screenX]
mul [CellSizePX]
add eax, [XFieldOffset]
mov ebx, [color]
stdcall DrawRect, [ScreenBufAddr], eax, [screenY], [CellSizePX], [CellSizePX], ebx
ret
endp
proc calcCellSize
; assuming that height is less then width
mov eax, [FieldZoneHeight]
sub eax, 2
xor edx, edx
div [FieldSize]
cmp eax, 0
je .LessThen1PX
mov [CellSizePX], eax
jmp .Finished
.LessThen1PX:
mov [CellSizePX], 1
mov eax, [FieldZoneHeight]
mov [FieldSize], eax
; NEEDS TO BE DONE
.Finished:
ret
endp
proc calcLeftTextOffset
local sizee SIZE
local rect RECT
; calculating offsets
lea eax, [sizee]
invoke GetTextExtentPoint32, [hBufDC], tactNStr, tactNStrLen, eax
mov eax, [sizee.cx]
mov [maxTextWidth], eax
lea eax, [sizee]
invoke GetTextExtentPoint32, [hBufDC], agentsNStr, agentsNStrLen, eax
mov eax, [sizee.cx]
cmp eax, [maxTextWidth]
jb @F
mov [maxTextWidth], eax
@@:
lea eax, [sizee]
invoke GetTextExtentPoint32, [hBufDC], foodNStr, foodNStrLen, eax
mov eax, [sizee.cx]
cmp eax, [maxTextWidth]
jb @F
mov [maxTextWidth], eax
@@:
mov [rect.left], TEXT_MARGIN_LEFT * 2
mov eax, [FieldXInOffset]
mov [rect.right], eax
mov [rect.top], TEXT_MARGIN_TOP
mov [rect.bottom], TEXT_FONT_SIZE * 2
lea eax, [rect]
invoke FillRect, [hBufDC], eax, [bkgBrush]
lea eax, [rect]
invoke DrawText, [hBufDC], tactNStr, tactNStrLen, eax, DT_LEFT
mov [rect.top], TEXT_FONT_SIZE * 2 + TEXT_MARGIN_TOP
mov [rect.bottom], TEXT_FONT_SIZE * 2 + TEXT_MARGIN_TOP + TEXT_FONT_SIZE * 2
lea eax, [rect]
invoke FillRect, [hBufDC], eax, [bkgBrush]
lea eax, [rect]
invoke DrawText, [hBufDC], agentsNStr, agentsNStrLen, eax, DT_LEFT
mov [rect.top], TEXT_FONT_SIZE * 2 + TEXT_MARGIN_TOP + TEXT_FONT_SIZE * 2
mov [rect.bottom], TEXT_FONT_SIZE * 2 + TEXT_MARGIN_TOP + TEXT_FONT_SIZE * 2 + TEXT_FONT_SIZE * 2
lea eax, [rect]
invoke FillRect, [hBufDC], eax, [bkgBrush]
lea eax, [rect]
invoke DrawText, [hBufDC], foodNStr, foodNStrLen, eax, DT_LEFT
ret
endp
proc calcFieldOffsets
mov ebx, [FieldZoneHeight]
; getting size of field in pixels
mov eax, [FieldSize]
mul [CellSizePX]
; calculating left space on screen in Y axis
sub ebx, eax
shr ebx, 1
dec eax
mov [YFieldOffset], ebx
; same for X-axis
mov ebx, [FieldZoneWidth]
sub ebx, eax
shr ebx, 1
mov [XFieldOffset], ebx
ret
endp
proc CalcAgentColor uses edx ebx ecx, energy
mov eax, [energy]
cmp eax, [AgentMinEnergyToClone]
jb @F
mov eax, 0xFF ; color
jmp .stop
; if energy if more then max value, putting max brightness
; such case mb after feeding, but before cloning (it's 2 tacts, but redrawing is each tact)
@@:
mov ecx, 0xFF
mul ecx
xor edx, edx
mov ecx, [AgentMinEnergyToClone]
div ecx
; setting min brightness (so agent will be at least visible)
cmp eax, 0x20
jg .stop
mov eax, 0x20
.stop:
shl eax, 16
ret
endp
proc CalcFoodColor uses edx ebx ecx, amount
mov eax, [amount]
cmp eax, [FoodMaxValue]
jb @F
mov eax, 0xFF ; color
jmp .stop
@@:
xor edx, edx
mov ecx, 0xFF
mul ecx
xor edx, edx
mov ecx, [FoodMaxValue]
div ecx
cmp eax, 0x20
jg .stop
mov eax, 0x20
.stop:
ret
endp
; x, y - in pixels
proc DrawRect uses eax ebx edx ecx edi, buffer, x, y, height, width, color
mov ecx, [height]
mov eax, [FieldZoneWidth]
shl eax, 2
mul [y]
mov edi, [buffer]
add edi, eax
mov eax, [x]
shl eax, 2
add edi, eax
mov edx, eax ; backed up
mov eax, [color]
rectangleLoop:
push ecx
mov ecx, [width]
rep stosd
mov ebx, [width]
shl ebx, 2
sub edi, ebx
mov ebx, [FieldZoneWidth]
shl ebx, 2
add edi, ebx
pop ecx
loop rectangleLoop
ret
endp
proc drawBkg
local rect RECT
mov edi, [ScreenBufAddr]
mov eax, [FieldZoneWidth]
mul [FieldZoneHeight]
mov ecx, eax
mov eax, GAME_BKG_COLOR
rep stosd
lea eax, [rect]
invoke GetClientRect, [hwnd], eax
lea eax, [rect]
invoke FillRect, [hBufDC], eax, [bkgBrush]
ret
endp
proc GUIBasicInit
local sizee SIZE
local rect RECT
; getting screen X size and Y
invoke GetSystemMetrics, SM_CXSCREEN
mov [ScreenWidth], eax
mov [FieldXInOffset], eax
invoke GetSystemMetrics, SM_CYSCREEN
mov [ScreenHeight], eax
mov [FieldZoneHeight], eax
mov [FieldZoneWidth], eax
mov [FieldYInOffset], 0
sub [FieldXInOffset], eax
shr [FieldXInOffset], 1
invoke GetModuleHandle, 0
mov [wc.hInstance], eax
invoke LoadIcon, 0, IDI_APPLICATION
mov [wc.hIcon], eax
invoke LoadCursor, 0, IDC_CROSS
mov [wc.hCursor], eax
invoke RegisterClass, wc
test eax, eax
jz error
invoke CreateWindowEx, 0, _class, 0, WS_VISIBLE + WS_POPUP, 0, 0, [ScreenWidth], [ScreenHeight], NULL, NULL, [wc.hInstance], NULL
mov [hwnd], eax
invoke GetDC, [hwnd]
mov [hMainDc], eax
invoke CreateCompatibleDC, [hMainDc]
mov [hBufDC], eax
invoke CreateCompatibleBitmap, [hMainDc], [ScreenWidth], [ScreenHeight]
invoke SelectObject, [hBufDC], eax
mov [bmi.biSize], sizeof.BITMAPINFOHEADER
mov eax, [FieldZoneWidth]
mov [bmi.biWidth], eax
mov eax, [FieldZoneHeight]
mov [bmi.biHeight], eax
mov [bmi.biPlanes], 1
mov [bmi.biBitCount], 32
mov [bmi.biCompression], BI_RGB
; setup font size
mov [lf.lfHeight], TEXT_FONT_SIZE
invoke CreateFontIndirect, lf
invoke SelectObject, [hBufDC], eax
; create brush for text bkg (to clear old text)
invoke CreateSolidBrush, GAME_BKG_COLOR
mov [bkgBrush], eax
invoke CreateSolidBrush, 0
mov [blackBrush], eax
; cursor is like semaphore - twice hidden, twice should be shown
cmp [isCursorShown], 1
jne @F
invoke ShowCursor, FALSE
mov [isCursorShown], 0
@@:
jmp @F
error:
invoke MessageBox, NULL, _error, NULL, MB_ICONERROR + MB_OK
@@:
ret
endp
proc ProcessWindowMsgs
invoke PeekMessage, msg, NULL, 0, 0, 1
; cmp eax, 1
; je @F
cmp eax, 0
jne @F
jmp .finish
@@:
invoke TranslateMessage, msg
invoke DispatchMessage, msg
.finish:
ret
endp
proc WriteMsg uses edi esi ebx, Msg
local rect RECT
push [lf.lfHeight]
mov [lf.lfHeight], TEXT_CHAT_FONT_SIZE
invoke CreateFontIndirect, lf
invoke SelectObject, [hBufDC], eax
; initing them in the start, cause they are constant
mov eax, [FieldXInOffset]
add eax, [FieldZoneWidth]
mov [rect.left], eax
add [rect.left], TEXT_MARGIN_LEFT / 2
mov eax, [ScreenWidth]
mov [rect.right], eax
inc [ConsoleBufCurrSave]
mov eax, [ConsoleBufSavesN]
cmp dword[ConsoleBufCurrSave], eax
jae .needRewrite
; saving text to corresponding slot (so history will work (in future:)))
mov ecx, ConsoleBufSize + 1
mov ebx, [ConsoleBufCurrSave]
mov edi, [ConsoleBufSaves + ebx * 4]
mov esi, [Msg]
rep movsb
mov eax, TEXT_FONT_SIZE * 2
mul [ConsoleBufCurrSave]
cmp [ConsoleBufCurrSave], 0
jne @F
mov eax, TEXT_MARGIN_TOP
@@:
mov [rect.top], eax
mov [rect.bottom], eax
add [rect.bottom], TEXT_FONT_SIZE * 2
lea eax, [rect]
invoke FillRect, [hBufDC], eax, [bkgBrush]
mov esi, [ConsoleBufCurrSave]
mov eax, [IsCommandValid]
mov [ConsoleBufIsCorrect + esi * 4], eax
cmp [ConsoleBufIsCorrect + esi * 4], 1
je @F
invoke SetTextColor, [hBufDC], INVALID_COMMAND_COLOR
lea eax, [rect]
invoke DrawText, [hBufDC], [Msg], -1, eax, DT_LEFT
invoke SetTextColor, [hBufDC], 0
jmp .stop
@@:
lea eax, [rect]
invoke DrawText, [hBufDC], [Msg], -1, eax, DT_LEFT
jmp .stop
.needRewrite:
; backing up first cell (moving it into last one)
mov edx, [ConsoleBufSaves]
push edx
; starting shifting text in ConsoleBufSaves array
mov ecx, [ConsoleBufSavesN]
dec ecx
mov ebx, 1
mov [rect.top], TEXT_MARGIN_TOP
mov [rect.bottom], TEXT_MARGIN_TOP + TEXT_FONT_SIZE * 2
.shiftText:
push ecx
mov edx, [ConsoleBufSaves + ebx * 4]
mov [ConsoleBufSaves + (ebx - 1) * 4], edx
mov edx, [ConsoleBufIsCorrect + ebx * 4]
mov [ConsoleBufIsCorrect + (ebx - 1) * 4], edx
lea eax, [rect]
invoke FillRect, [hBufDC], eax, [bkgBrush]
cmp [ConsoleBufIsCorrect + (ebx - 1) * 4], 1
je @F
invoke SetTextColor, [hBufDC], INVALID_COMMAND_COLOR
lea eax, [rect]
invoke DrawText, [hBufDC], [ConsoleBufSaves + (ebx - 1) * 4], -1, eax, DT_LEFT
invoke SetTextColor, [hBufDC], 0
jmp .continue
@@:
lea eax, [rect]
invoke DrawText, [hBufDC], [ConsoleBufSaves + (ebx - 1) * 4], -1, eax, DT_LEFT
.continue:
add [rect.top], TEXT_FONT_SIZE * 2
add [rect.bottom], TEXT_FONT_SIZE * 2
pop ecx
inc ebx
dec ecx
cmp ecx, 0
jne .shiftText
mov ebx, [ConsoleBufSavesN]
mov eax, [IsCommandValid]
mov [ConsoleBufIsCorrect + (ebx - 1) * 4], eax
pop edx
mov [ConsoleBufSaves + (ebx - 1) * 4], edx
; saving text to corresponding slot (so history will work (in future:)))
mov ecx, ConsoleBufSize + 1
mov edi, [ConsoleBufSaves + (ebx - 1) * 4]
mov esi, [Msg]
rep movsb
lea eax, [rect]
invoke FillRect, [hBufDC], eax, [bkgBrush]
cmp [ConsoleBufIsCorrect + (ebx - 1) * 4], 1
je @F
invoke SetTextColor, [hBufDC], INVALID_COMMAND_COLOR
lea eax, [rect]
invoke DrawText, [hBufDC], [ConsoleBufSaves + (ebx - 1) * 4], -1, eax, DT_LEFT
invoke SetTextColor, [hBufDC], 0
jmp .stop
@@:
lea eax, [rect]
invoke DrawText, [hBufDC], [ConsoleBufSaves + (ebx - 1) * 4], -1, eax, DT_LEFT
.stop:
invoke BitBlt, [hMainDc], 0, 0, [ScreenWidth], [ScreenHeight], [hBufDC], 0, 0, SRCCOPY
pop [lf.lfHeight]
invoke CreateFontIndirect, lf
invoke SelectObject, [hBufDC], eax
ret
endp
proc DrawCursor uses edi eax
local rect RECT
mov eax, [FieldXInOffset]
add eax, [FieldZoneWidth]
mov [rect.left], eax
add [rect.left], TEXT_MARGIN_LEFT / 2
mov eax, [ScreenWidth]
mov [rect.right], eax
mov eax, [ScreenHeight]
mov [rect.bottom], eax
sub eax, TEXT_FONT_SIZE * 2
mov [rect.top], eax
lea eax, [rect]
invoke FillRect, [hBufDC], eax, [bkgBrush]
lea eax, [rect]
invoke DrawText, [hBufDC], ConsoleActiveText, -1, eax, DT_LEFT
invoke BitBlt, [hMainDc], 0, 0, [ScreenWidth], [ScreenHeight], [hBufDC], 0, 0, SRCCOPY
ret
endp
proc RedrawCommand uses edi eax
local rect RECT
mov edi, ConsoleInpBuf
add edi, [ConsoleCharsN]
mov byte[edi], 0
mov eax, [FieldXInOffset]
mov [rect.left], eax
mov eax, [FieldZoneWidth]
add [rect.left], eax
add [rect.left], TEXT_MARGIN_LEFT / 2
mov eax, [ScreenWidth]
mov [rect.right], eax
mov eax, [ScreenHeight]
mov [rect.bottom], eax
sub eax, TEXT_FONT_SIZE * 2
mov [rect.top], eax
lea eax, [rect]
invoke FillRect, [hBufDC], eax, [bkgBrush]
lea eax, [rect]
invoke DrawText, [hBufDC], ConsoleInpBuf, -1, eax, DT_LEFT
invoke BitBlt, [hMainDc], 0, 0, [ScreenWidth], [ScreenHeight], [hBufDC], 0, 0, SRCCOPY
.stop:
ret
endp
proc WindowProc uses ebx esi edi, hwnd, wmsg, wparam, lparam
cmp [wmsg], WM_DESTROY
je .wmdestroy
cmp [wmsg], WM_KEYDOWN
je .checkInpMode
cmp [isDrawingActive], 1
jne @F
cmp [wmsg], WM_LBUTTONUP
je .MouseClickHandle
; cmp [wmsg], WM_SETCURSOR
; je .wmSetCursor
cmp [wmsg], WM_MOUSEMOVE
jne @F
test [wparam], MK_LBUTTON
jnz .MouseClickHandle ; left button is pressed
@@:
invoke DefWindowProc, [hwnd], [wmsg], [wparam], [lparam]
jmp .full_skip
.checkInpMode:
cmp [ConsoleInputMode], 1
jne .keyDown
mov eax, [lparam]
shr eax, 31
jc .full_skip
.keyDown:
cmp [HelpIsActive], 1
jne @F
; q
cmp [wparam], 0x51
jne .full_skip
stdcall ShowHints
stdcall calcLeftTextOffset
stdcall PrintStats
invoke SetDIBitsToDevice, [hBufDC], [FieldXInOffset], [FieldYInOffset], [FieldZoneWidth], [FieldZoneHeight], 0, 0, 0, [FieldZoneHeight], [ScreenBufAddr], bmi, 0
invoke BitBlt, [hMainDc], 0, 0, [ScreenWidth], [ScreenHeight], [hBufDC], 0, 0, SRCCOPY
mov [HelpIsActive], 0
jmp .full_skip
@@:
cmp [wparam], VK_TAB
jne @F
cmp [ConsoleInputMode], 1
jne .switchTO1
mov [ConsoleInputMode], 0
mov [ConsoleCharsN], 0
mov [ConsoleHistoryCurrI], -1
stdcall RedrawCommand
jmp .finish
.switchTO1:
mov [ConsoleInputMode], 1
stdcall DrawCursor
jmp .full_skip
@@:
cmp [wparam], VK_ESCAPE
je .wmdestroy
cmp [ConsoleInputMode], 1
je .handleConsoleInp
.handleWindowInp:
cmp [wparam], VK_SPACE
jne .coninueAnalisis
cmp [isDrawingActive], 1
je .finish
cmp [PauseGame], 0
je @F
mov [PauseGame], 0
jmp .finish
@@:
mov [PauseGame], 1
jmp .full_skip
.coninueAnalisis:
; 'n' key
cmp [wparam], 0x4E
jne .finish
mov [PutOnPauseNextTact], 1
mov [PauseGame], 0
jmp .finish
; @@:
; ; 's' key - save field
; cmp [wparam], 0x53
; jne @F
; stdcall saveField, fname1
; @@:
; ; 'd' key - save configuration
; cmp [wparam], 0x44
; jne @F
; stdcall saveSettings, fname2
; @@:
; ; 'l' key - load configuration
; cmp [wparam], 0x4C
; jne @F
; stdcall loadSettings, fname2
; @@:
; ; 'k' key - load field
; cmp [wparam], 0x4B
; jne .finish
; stdcall loadField, fname1
.handleConsoleInp:
cmp [wparam], VK_UP
jne @F
; skip if reached up limit
mov eax, [ConsoleBufCurrSave]
cmp [ConsoleHistoryCurrI], eax
jge .full_skip
inc [ConsoleHistoryCurrI]
stdcall GetCommandFromHistory
stdcall RedrawCommand
jmp .full_skip
@@:
cmp [wparam], VK_DOWN
jne @F
mov eax, [ConsoleBufCurrSave]
cmp [ConsoleHistoryCurrI], -1
jle .full_skip
dec [ConsoleHistoryCurrI]
stdcall GetCommandFromHistory
stdcall RedrawCommand
cmp [ConsoleCharsN], 0
ja .full_skip
stdcall DrawCursor
invoke BitBlt, [hMainDc], 0, 0, [ScreenWidth], [ScreenHeight], [hBufDC], 0, 0, SRCCOPY
jmp .full_skip
@@:
@@:
; enter
cmp [wparam], VK_RETURN
jne @F
mov [ConsoleHistoryCurrI], -1
cmp [ConsoleCharsN], 0
mov [ConsoleInputMode], 0
je .finish
mov edi, ConsoleInpBuf
add edi, [ConsoleCharsN]
mov byte[edi], 0
stdcall ProcessCommand
stdcall WriteMsg, ConsoleInpBuf
mov [ConsoleCharsN], 0
stdcall RedrawCommand
jmp .full_skip
@@:
cmp [wparam], VK_BACK
jne @F
mov [ConsoleHistoryCurrI], -1
cmp [ConsoleCharsN], 0
jg .GreaterThenZero
inc [ConsoleCharsN]
.GreaterThenZero:
dec [ConsoleCharsN]
cmp [ConsoleCharsN], 0
jne .finish
stdcall DrawCursor
jmp .full_skip
@@:
cmp [ConsoleCharsN], ConsoleBufSize
jae .finish ; buffer is full, not procesing new char
mov edi, ConsoleInpBuf
add edi, [ConsoleCharsN]
; space
cmp [wparam], VK_SPACE
jne @F
mov byte[edi], ' '
inc [ConsoleCharsN]
jmp .finish
@@:
; A
cmp [wparam], 0x41
jb .notALetter
; Z
cmp [wparam], 0x5A
ja .notALetter
mov [ConsoleHistoryCurrI], -1
mov eax, [wparam]
sub eax, 0x41
add eax, 'a'
mov byte[edi], al
inc [ConsoleCharsN]
jmp .finish
.notALetter:
; 0
cmp [wparam], 0x30
jb .notADigit
; 9
cmp [wparam], 0x39
ja .notADigit
mov [ConsoleHistoryCurrI], -1
mov eax, [wparam]
sub eax, 0x30
add eax, '0'
mov byte[edi], al
inc [ConsoleCharsN]
jmp .finish
.notADigit:
jmp .finish
.wmdestroy:
invoke PostQuitMessage, 0
xor eax, eax
invoke ExitProcess, 0
.MouseClickHandle:
local fieldX dd ?
local fieldY dd ?
local cellX dd ?
local cellY dd ?
cmp [HelpIsActive], 1
je .full_skip
; getting Y, converting to field coords
mov eax, [lparam]
shr eax, 16
sub eax, [YFieldOffset]
sub eax, [FieldYInOffset]
cmp eax, 0
jl .finish
cmp eax, [FieldSizePx]
jae .finish
add eax, [YFieldOffset]
add eax, [YFieldOffset]
; inversing y-axis direction
mov ebx, [FieldZoneHeight]
dec ebx
sub ebx, eax
mov [fieldY], ebx
; doing same for x coords
mov eax, [lparam]
and eax, 0x0000FFFF
sub eax, [XFieldOffset]
sub eax, [FieldXInOffset]
cmp eax, 0
jl .finish
cmp eax, [FieldSizePx]
jae .finish
mov [fieldX], eax
xor edx, edx
mov eax, [fieldX]
div [CellSizePX]
mov [cellX], eax
mov eax, [fieldY]
xor edx, edx
div [CellSizePX]
mov [cellY], eax
stdcall clearFieldCell, [cellX], [cellY]
cmp [isDrawingAgent], 1
jne @F
stdcall AddAgent, [cellX], [cellY]
stdcall PrintStats
invoke SetDIBitsToDevice, [hBufDC], [FieldXInOffset], [FieldYInOffset], [FieldZoneWidth], [FieldZoneHeight], 0, 0, 0, [FieldZoneHeight], [ScreenBufAddr], bmi, 0
invoke BitBlt, [hMainDc], 0, 0, [ScreenWidth], [ScreenHeight], [hBufDC], 0, 0, SRCCOPY
jmp .full_skip
stdcall AddFood, [cellX], [cellY]
@@:
cmp [isDrawingClear], 1
jne @F
stdcall clearCellColor, [cellX], [cellY]
stdcall PrintStats
invoke SetDIBitsToDevice, [hBufDC], [FieldXInOffset], [FieldYInOffset], [FieldZoneWidth], [FieldZoneHeight], 0, 0, 0, [FieldZoneHeight], [ScreenBufAddr], bmi, 0
invoke BitBlt, [hMainDc], 0, 0, [ScreenWidth], [ScreenHeight], [hBufDC], 0, 0, SRCCOPY
jmp .full_skip
@@:
stdcall AddFood, [cellX], [cellY]
stdcall PrintStats
invoke SetDIBitsToDevice, [hBufDC], [FieldXInOffset], [FieldYInOffset], [FieldZoneWidth], [FieldZoneHeight], 0, 0, 0, [FieldZoneHeight], [ScreenBufAddr], bmi, 0
invoke BitBlt, [hMainDc], 0, 0, [ScreenWidth], [ScreenHeight], [hBufDC], 0, 0, SRCCOPY
jmp .full_skip
.finish:
cmp [ConsoleInputMode], 1
jne @F