-
Notifications
You must be signed in to change notification settings - Fork 2
/
ECAD.asm
1790 lines (1571 loc) · 36.1 KB
/
ECAD.asm
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
; ECAD by Timur "XProger" Gagiev
; xproger@list.ru
; tips: 1 tab = 10 spaces
; defines
USE_GAMMA_CORRECTION = 1
;USE_LAYER_OPACITY = 1
USE_DEBUG = 1
MODE_BENCHMARK = 1
WINDOWS = 1
include 'win32a.inc'
;include 'macros.inc'
if defined WINDOWS ; Windows
if defined USE_DEBUG
format PE CONSOLE 4.0
else
format PE GUI 4.0
end if
entry START
section '.' import data readable writeable executable
library kernel32,'KERNEL32.DLL', user32,'USER32.DLL', gdi32,'GDI32.DLL',msvcrt,'msvcrt.dll'
if defined USE_DEBUG
import msvcrt, printf, 'printf'
end if
include 'api\kernel32.inc'
include 'api\user32.inc'
include 'api\gdi32.inc'
else ; KolibriOS
format binary as "kex"
use32
org 0
db 'MENUET01'
dd 1, START, I_END, IM_END+2048, IM_END+2048, 0, 0
struct FILE_TIME
sec db ?
min db ?
hour db ?
res db ?
ends
struct FILE_DATE
day db ?
month db ?
year dw ?
ends
struct FILE_REQ
arg1 dd ?
arg2 dd ?
arg3 dd ?
arg4 dd ?
arg5 dd ?
arg6 db ?
arg7 dd ?
ends
struct FILE_INFO
attr dd ?
ntype db ?
res1 db ?
res2 db ?
res3 db ?
ctime FILE_TIME
cdate FILE_DATE
tread FILE_TIME
dread FILE_DATE
twrite FILE_TIME
dwrite FILE_DATE
filesize dd ?
filename db ?
ends
struct BLIT_INFO
dstx dd ?
dsty dd ?
dstw dd ?
dsth dd ?
srcx dd ?
srcy dd ?
srcw dd ?
srch dd ?
bitmap dd ?
stride dd ?
ends
end if
struct VEC2
x dd ?
y dd ?
ends
struct VEC4
x dd ?
y dd ?
z dd ?
w dd ?
ends
struct BOARD_TRACK
pos1 VEC2
pos2 VEC2
w dd ?
flags dd ?
sexp dd ?
pexp dd ?
ends
struct BOARD_PAD
pos VEC2
size VEC2
hole VEC2
sexp dd ?
pexp dd ?
flags dd ?
res1 dd ?
res2 dd ?
res3 dd ?
ends
struct BOARD_LAYER_INFO
index dd ?
color dd ?
tCount dd ?
pCount dd ?
ends
struct BOARD_LAYER
info BOARD_LAYER_INFO
tracks dd ?
pads dd ?
ends
struct VIEW
trans VEC4
scale VEC4
ends
struct BOARD
view VIEW
count dd ?
layers dd ?
ends
struct CANVAS
width dd ? ; width (aligned on 4)
height dd ? ; height
size dd ? ; width * height * 4
bytes dd ? ; memory (aligned on 16 bytes) udata + offset 0-15
ends
struct MOUSE
state dd ?
pos VEC2
last VEC2
origin VEC2
ends
; SSE global variable aliases
xcam_min equ xmm4
xcam_max equ xmm5
xcam_trans equ xmm6
xcam_scale equ xmm7
; mouse states
MOUSE_STATE_NONE equ 0
MOUSE_STATE_MOVE equ 1
MOUSE_STATE_ZOOM equ 2
; color constants
COLOR_BACKGROUND equ 0x00000000
COLOR_SMT_HOLE equ 0x00009190
COLOR_THT_HOLE equ 0x00816200
; pad flags
PAD_FLAG_THT equ 1 shl 15
; menu commands
MC_FILE_NEW equ 00
MC_FILE_OPEN equ 01
MC_FILE_SAVE equ 02
MC_FILE_SAVE_AS equ 03
MC_FILE_EXIT equ 04
MC_EDIT_UNDO equ 05
MC_EDIT_REDO equ 06
MC_EDIT_CUT equ 07
MC_EDIT_COPY equ 08
MC_EDIT_PASTE equ 09
MC_EDIT_DELETE equ 10
MC_EDIT_SELECT_ALL equ 11
MC_VIEW_FLIP equ 12
MC_TOOLS_TRACK equ 13
MC_TOOLS_PAD equ 14
MC_TOOLS_REGION equ 15
MC_TOOLS_COMPONENT equ 16
MC_TOOLS_MEASURE_LINEAR equ 17
MC_TOOLS_MEASURE_ANGULAR equ 18
ACCEL_COUNT equ 19
; ==== System =================================================================
; align allocated memory
; in: eax = memory ptr (size + 32)
; out: eax = aligned memory address
System.malign:
mov ecx, eax
add ecx, 15
and ecx, not 15
mov [ecx], eax
mov eax, ecx
add eax, 16
ret
; allocate 16-byte aligned memory block
; in: ecx = size
; out: eax = memory ptr
System.malloc:
push ecx
add ecx, 32
if defined WINDOWS
push esi
push edi
push ebx
push edx
invoke HeapAlloc,[sys_heap],0,ecx
pop edx
pop ebx
pop edi
pop esi
else
mcall 68, 12, ecx
end if
call System.malign
pop ecx
ret
; reallocate memory block
; in: eax = memory ptr
; ecx = new size
; out: eax = memory ptr
System.realloc:
add ecx, 32
sub eax, 16
if defined WINDOWS
invoke HeapReAlloc,[sys_heap],0,[eax],ecx
else
mov edx, [eax]
mcall 68, 20, ecx, edx
end if
call System.malign
ret
; free memory block
; in: eax = memory ptr
System.free:
sub eax, 16
if defined WINDOWS
invoke HeapFree,[sys_heap],0,[eax]
else
mov ecx, [eax]
mcall 68, 13, ecx
end if
ret
if defined USE_GAMMA_CORRECTION
System.initGammaLUT:
mov ecx, 255
;movss xmm1, [SSE_1f]
;movss xmm2, [SSE_255f]
movss xmm1, [SSE_MATH + 4]
movss xmm2, [SSE_MATH + 12]
divss xmm1, xmm2
@@: cvtsi2ss xmm0, ecx
mulss xmm0, xmm1 ; /= 255.0f
movss xmm3, xmm0
movss xmm4, xmm0
mulss xmm3, xmm3 ; sqr
sqrtss xmm4, xmm4 ; sqrt
mulss xmm3, xmm2 ; *= 255.0f
mulss xmm4, xmm2
cvttss2si eax, xmm3 ; convert to int
cvttss2si ebx, xmm4
mov [LUT_gamma2color + ecx], al
mov [LUT_color2gamma + ecx], bl
dec ecx
jnl @b
ret
end if
System.memcpy_SSE:
; TODO: System.memcpy ebx -> edi, sizeof.BOARD_LAYER_INFO
push eax
push ecx
push edi
push esi
if 1 = 0
shr ecx, 4 ; count /= 16
@@: movaps xmm0, [esi]
movaps [edi], xmm0
add esi, 16
add edi, 16
else
shr ecx, 2 ; count /= 16
@@: mov eax, [esi]
mov [edi], eax
add esi, 4
add edi, 4
end if
dec ecx
jnz @b
pop esi
pop edi
pop ecx
pop eax
ret
; fill memory by 32-bit value
; in: eax - value
; ebx - data pointer
; ecx - data size
System.memset32_sse:
shr ecx, 6 ; div data size by 16 bytes
movd xmm0, eax
shufps xmm0, xmm0, 0
@@: movaps [ebx + 00h], xmm0
movaps [ebx + 10h], xmm0
movaps [ebx + 20h], xmm0
movaps [ebx + 30h], xmm0
add ebx, 40h
dec ecx
jnz @b
ret
System.time:
if defined WINDOWS
invoke GetTickCount
else
mcall 26, 9
imul eax, 10
end if
ret
; ==== Window =================================================================
START:
call Window.Init
mov eax, filename
call Board.Load
@@: if defined WINDOWS
if defined MODE_BENCHMARK
invoke PeekMessage,msg,0,0,0,PM_REMOVE
test eax, eax
jnz .event
call Window.Repaint
jmp @b
else
invoke GetMessage,msg,0,0,0
test eax, eax
jz .exit
jmp .event
jmp @b
end if
.event: invoke TranslateAccelerator,[hwnd],[haccel],msg
test eax, eax
jnz @b
invoke TranslateMessage,msg
invoke DispatchMessage,msg
mov eax, [msg.message]
cmp eax, WM_QUIT
je .exit
jmp @b
else
.redraw: call Window.Repaint
.loop: if defined MODE_BENCHMARK
mcall 11 ; check for event
test eax, eax
jz .redraw
else
mcall 10
end if
dec eax
jz .redraw
dec eax
jz .key
dec eax
jz .button
cmp eax, 3
je .mouse
jmp .loop
.key: mcall 2
jmp .loop
.button: mcall 17
cmp ah, 1
jne .loop
jmp .exit
.mouse: call Window.Mouse.Update
;mcall 63, 1, 49
.vzoom: mcall 37, 7 ; wheel-delta
movsx eax, ax
test eax, eax
jz .vmove
movq mm0, [mouse.pos]
movq [mouse.origin], mm0
sal eax, 4
call Board.View.Zoom ; zoom
.vmove: mcall 37, 2 ; check buttons
cmp eax, 1
jne .loop
call Board.View.Move
jmp .loop
end if
.exit: mov eax, [canvas.bytes]
call System.free
if defined WINDOWS
invoke ExitProcess,0
else
mcall -1
end if
if defined WINDOWS
proc WndProc hwnd,wmsg,wparam,lparam
mov ecx, [wmsg]
cmp ecx, WM_PAINT
je .paint
cmp ecx, WM_MOUSEMOVE
je .mouse.move
cmp ecx, WM_LBUTTONDOWN
je .mouse.button.down.L
cmp ecx, WM_RBUTTONDOWN
je .mouse.button.down.R
cmp ecx, WM_LBUTTONUP
je .mouse.button.up
cmp ecx, WM_RBUTTONUP
je .mouse.button.up
cmp ecx, WM_MOUSEWHEEL
je .mouse.wheel
cmp ecx, WM_SIZE
je .size
cmp ecx, WM_GETMINMAXINFO
je .minmax
cmp ecx, WM_COMMAND
je .command
cmp ecx, WM_DESTROY
je .destroy
invoke DefWindowProc,[hwnd],[wmsg],[wparam],[lparam]
ret
.paint:
if 1 = 0
movaps xmm0, [board.view.scale]
rcpps xmm0, xmm0
cvttps2pi mm0, xmm0
movq [v0], mm0
mov eax, [v0.x]
cvttps2pi mm0, [board.view.trans]
movq [v0], mm0
if defined USE_DEBUG
cinvoke printf,d_view,[v0.x],[v0.y],eax
end if
end if
locals
ps PAINTSTRUCT
endl
call Window.Render
lea esi, [ps]
invoke BeginPaint,[hwnd],esi
invoke SetDIBitsToDevice,[ps.hdc],0,0,[canvas.width],[canvas.height],0,0,0,[canvas.height],[canvas.bytes],bmi,0
invoke EndPaint,[hwnd],esi
if defined USE_DEBUG
inc [frame]
call System.time
cmp eax, [frame_time]
jl .return0
add eax, 1000
mov [frame_time], eax
cinvoke printf,d_fps,[frame]
mov [frame], 0
end if
jmp .return0
.mouse.move:
call Window.Mouse.Update
mov ecx, [mouse.state]
test ecx, ecx ; MOUSE_STATE_NONE
jz .return0
cmp ecx, MOUSE_STATE_ZOOM
je @f
; move
call Board.View.Move
jmp .return0
; zoom
; xmm0 = delta = pos - last
@@: mov eax, [mouse.pos.y]
sub eax, [mouse.last.y]
call Board.View.Zoom
jmp .return0
.mouse.button.down.R:
call Window.Mouse.Update
mov [mouse.state], MOUSE_STATE_ZOOM
movq mm0, [mouse.pos]
movq [mouse.origin], mm0 ; mouse origin (for zoom) = mouse pos
jmp .mouse.button.down
.mouse.button.down.L:
call Window.Mouse.Update
mov [mouse.state], MOUSE_STATE_MOVE
.mouse.button.down:
invoke SetCapture,[hwnd]
jmp .return0
.mouse.button.up:
; TODO: check button ----------------------------------------------------
mov [mouse.state], MOUSE_STATE_NONE
invoke ReleaseCapture
jmp .return0
.mouse.wheel:
mov ecx, [mouse.state]
test ecx, ecx
jnz .return0 ; not MOUSE_STATE_NONE
call Window.Mouse.Update
movq mm0, [mouse.pos]
movq [mouse.origin], mm0 ; mouse origin (for zoom) = mouse pos
movsx eax, word [wparam + 2]
cdq
mov ecx, 120
idiv ecx
sal eax, 4 ; 1 wheel step = 16 zoom value
neg eax
call Board.View.Zoom
jmp .return0
.size:
; get width & height of client area
movzx eax, word [lparam]
movzx ebx, word [lparam + 2]
test ebx, ebx
jz .return0
add eax, 3 ; align width by 4 canvas.bytes (16 bytes for SSE)
and eax, not 3
call Window.Resize
invoke DrawMenuBar,[hwnd]
jmp .return0
.minmax:
virtual at eax
.mm MINMAXINFO
end virtual
mov eax, [lparam]
mov [.mm.ptMinTrackSize.x], 320
mov [.mm.ptMinTrackSize.y], 240
jmp .return0
.command:
movzx ecx, word [wparam]
if defined USE_DEBUG
pushad
cinvoke printf,d_count,ecx
popad
end if
cmp ecx, MC_FILE_EXIT
jne @f
jmp .destroy
@@:
jmp .return0
.destroy:
invoke PostQuitMessage,0
.return0:
xor eax, eax
ret
endp
end if
Window.Init:
; init memory heap
if defined WINDOWS
invoke GetProcessHeap
mov [sys_heap], eax
else
mcall 68, 11
end if
mov ecx, 16
call System.malloc
mov [canvas.bytes], eax
if defined USE_GAMMA_CORRECTION
call System.initGammaLUT
end if
mov [board.count], 0
if defined WINDOWS
; create window
call Window.InitMenu
invoke CreateWindowEx,0,WND_CLASS,WND_TITLE,WS_OVERLAPPEDWINDOW,0,0,1024,600,0,[hmenu],0,0
mov [hwnd], eax
invoke SetWindowLong,[hwnd],GWL_WNDPROC,WndProc
else
mcall 40, EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE ; events filter
mcall 12, 1
mcall 0, <0,1024>, <0,600>, 0x73000000, , WND_TITLE
mcall 12, 2
end if
mov [mouse.state], MOUSE_STATE_NONE
movaps xcam_trans, dqword [cam_trans]
movlhps xcam_trans, xcam_trans
movss xcam_scale, [cam_scale]
rcpss xcam_scale, xcam_scale
shufps xcam_scale, xcam_scale, 0
xorps xcam_scale, dqword [SSE_SIGN_MASK]
movaps dqword [board.view.trans], xcam_trans
movaps dqword [board.view.scale], xcam_scale
if defined USE_DEBUG
call System.time
mov [frame_time], eax
mov [frame], 0
end if
if defined WINDOWS
invoke ShowWindow,[hwnd],SW_SHOWDEFAULT
end if
ret
Window.InitMenu:
invoke CreateMenu
mov [hmenu], eax
; File
invoke CreatePopupMenu
mov [v0], eax
invoke AppendMenu,[v0],MF_STRING+MF_GRAYED,MC_FILE_NEW,m_file_new
invoke AppendMenu,[v0],MF_STRING,MC_FILE_OPEN,m_file_open
invoke AppendMenu,[v0],MF_STRING+MF_GRAYED,MC_FILE_SAVE,m_file_save
invoke AppendMenu,[v0],MF_STRING+MF_GRAYED,MC_FILE_SAVE_AS,m_file_save_as
invoke AppendMenu,[v0],MF_SEPARATOR,0,0
invoke AppendMenu,[v0],MF_STRING,MC_FILE_EXIT,m_file_exit
invoke AppendMenu,[hmenu],MF_POPUP,[v0],m_file
; Edit
invoke CreatePopupMenu
mov [v0], eax
invoke AppendMenu,[v0],MF_STRING+MF_GRAYED,MC_EDIT_UNDO,m_edit_undo
invoke AppendMenu,[v0],MF_STRING+MF_GRAYED,MC_EDIT_REDO,m_edit_redo
invoke AppendMenu,[v0],MF_SEPARATOR,0,0
invoke AppendMenu,[v0],MF_STRING+MF_GRAYED,MC_EDIT_CUT,m_edit_cut
invoke AppendMenu,[v0],MF_STRING+MF_GRAYED,MC_EDIT_COPY,m_edit_copy
invoke AppendMenu,[v0],MF_STRING+MF_GRAYED,MC_EDIT_PASTE,m_edit_paste
invoke AppendMenu,[v0],MF_STRING+MF_GRAYED,MC_EDIT_DELETE,m_edit_delete
invoke AppendMenu,[v0],MF_STRING+MF_GRAYED,MC_EDIT_SELECT_ALL,m_edit_select_all
invoke AppendMenu,[hmenu],MF_POPUP,[v0],m_edit
; View
invoke CreatePopupMenu
mov [v0], eax
invoke AppendMenu,[v0],MF_STRING+MF_GRAYED,MC_VIEW_FLIP,m_view_flip
invoke AppendMenu,[hmenu],MF_POPUP,[v0],m_view
; Place
invoke CreatePopupMenu
mov [v0], eax
invoke AppendMenu,[v0],MF_STRING+MF_GRAYED,MC_TOOLS_TRACK,m_tools_track
invoke AppendMenu,[v0],MF_STRING+MF_GRAYED,MC_TOOLS_PAD,m_tools_pad
invoke AppendMenu,[v0],MF_STRING+MF_GRAYED,MC_TOOLS_REGION,m_tools_region
invoke AppendMenu,[v0],MF_STRING+MF_GRAYED,MC_TOOLS_COMPONENT,m_tools_component
invoke AppendMenu,[v0],MF_SEPARATOR,0,0
invoke CreatePopupMenu
mov [v0.y], eax
invoke AppendMenu,[v0.y],MF_STRING+MF_GRAYED,MC_TOOLS_MEASURE_LINEAR,m_tools_measure_linear
invoke AppendMenu,[v0.y],MF_STRING+MF_GRAYED,MC_TOOLS_MEASURE_ANGULAR,m_tools_measure_angular
invoke AppendMenu,[v0],MF_POPUP,[v0.y],m_tools_measure
invoke AppendMenu,[hmenu],MF_POPUP,[v0],m_tools
xor ecx, ecx
mov esi, HOTKEYS
mov edi, esp
@@: movzx ax, byte [esi + ecx*2 + 0] ; modifiers
movzx dx, byte [esi + ecx*2 + 1] ; key
or ax, FVIRTKEY
push cx ; MC_* menu command id
push dx ; key
push ax ; modifier
inc ecx
cmp ecx, ACCEL_COUNT
jne @b
.done: mov [v0.x], esp
invoke CreateAcceleratorTable,[v0],ACCEL_COUNT
mov [haccel], eax
mov esp, edi
ret
Window.Resize:
mov [canvas.width], eax
mov [canvas.height], ebx
if defined WINDOWS
; fill bmi (for SetDIBitsToDevice)
mov [bmi.biSize], sizeof.BITMAPINFOHEADER
mov [bmi.biWidth], eax
neg ebx
mov [bmi.biHeight], ebx
neg ebx
mov [bmi.biPlanes], 1
mov [bmi.biBitCount], 32
mov [bmi.biCompression], BI_RGB
else
mov ecx, [proc_info.client_box.left]
mov edx, [proc_info.client_box.top]
mov [blit_info.dstx], ecx
mov [blit_info.dsty], edx
mov ecx, [proc_info.client_box.width]
mov [blit_info.dstw], ecx
mov [blit_info.dsth], ebx
mov [blit_info.srcx], 0
mov [blit_info.srcy], 0
mov [blit_info.srcw], ecx
mov [blit_info.srch], ebx
mov ecx, [canvas.bytes]
mov [blit_info.bitmap], ecx
mov ecx, eax
shl ecx, 2
mov [blit_info.stride], ecx
end if
; get unaligned memory size for canvas.bytes
mul ebx
shl eax, 2 ; *4 (32bpp)
mov [canvas.size], eax
; reallocate memory for canvas.bytes buffer
mov ecx, eax
mov eax, [canvas.bytes]
call System.realloc
mov [canvas.bytes], eax
call Window.Repaint
ret
Window.Repaint:
if defined WINDOWS
invoke InvalidateRect,[hwnd],0,0
else
mcall 9, proc_info, -1 ; get application state
mov eax, [proc_info.client_box.width]
mov ebx, [proc_info.client_box.height]
test ebx, ebx ; height = 0
jz .done
add eax, 3 ; align width by 4 canvas.bytes (16 bytes for SSE)
and eax, not 3
cmp eax, [canvas.width]
jne .resize
cmp ebx, [canvas.height]
jne .resize
jmp .render
.resize: call Window.Resize ; TODO: don't call after moving
.render: call Window.Render
mcall 12, 1
mcall 0,,, 0x73000000
mcall 73, 0, blit_info
if defined USE_DEBUG
; update fps info
mcall 47, (6 shl 16), [fps], <4, 4>, 0xffff00
inc [frame]
call System.time
cmp eax, [frame_time]
jl .present
add eax, 1000
mov [frame_time], eax
mov eax, [frame]
mov [fps], eax
mov [frame], 0
end if
.present: mcall 12, 2
end if
.done: ret
Window.Render:
; setup view to SSE registers
movaps xcam_trans, dqword [board.view.trans]
movaps xcam_scale, dqword [board.view.scale]
; setup min/max registers
xorps xcam_min, xcam_min ; xcam_min = [0, 0, 0, 0]
movq xcam_max, qword [canvas.width] ; xcam_max = [width, height, width, height]
movlhps xcam_max, xcam_max
cvtdq2ps xcam_max, xcam_max
movss xmm0, [SSE_MATH]
shufps xmm0, xmm0, 0
subps xcam_max, xmm0
;subps xcam_max, dqword [SSE_005f] ; xcam_max -= 1.0f
; prepare for alpha blending
pxor mm6, mm6 ; zero mm5
mov eax, 256
movd mm7, eax
pshufw mm7, mm7, 0 ; mm4 = [256, 256, 256, 256]
; clear background
mov eax, COLOR_BACKGROUND
mov ebx, [canvas.bytes]
mov ecx, [canvas.size]
call System.memset32_sse
; render board
call Board.Render.Layers
if defined USE_GAMMA_CORRECTION
call Window.ApplyGamma
end if
emms
ret
if defined USE_GAMMA_CORRECTION
Window.ApplyGamma:
mov esi, [canvas.bytes]
mov ecx, [canvas.size]
mov edi, LUT_color2gamma
@@: movzx eax, byte [esi+0]
movzx ebx, byte [esi+1]
movzx edx, byte [esi+2]
mov al, [edi+eax]
mov bl, [edi+ebx]
mov dl, [edi+edx]
mov [esi+0], al
mov [esi+1], bl
mov [esi+2], dl
add esi, 4
sub ecx, 4
jnz @b
ret
end if
Window.Mouse.Update:
movq mm0, [mouse.pos]
movq [mouse.last], mm0
lea esi, [mouse.pos]
if defined WINDOWS
invoke GetCursorPos,esi
invoke ScreenToClient,[hwnd],esi
; invert y
;mov eax, [canvas.height]
;dec eax
;sub eax, [esi + 4]
;mov [esi + 4], eax
else
mcall 37, 1 ; relative mouse pos
movsx ebx, ax
shr eax, 16
movsx ecx, ax
mov [esi + 0], ecx ; mouse.pos.x
mov [esi + 4], ebx ; mouse.pos.y
end if
ret
; ==== Canvas =================================================================
; mca = color * alpha
; mia = 256 - alpha
macro CANVAS_SET_ALPHA col, mca, mia {
movd mca, col ; mca = [0, ARGB2]
movq mm0, mca ; mm0 = mca
punpcklbw mca, mm6 ; mca = [0, R2, G2, B2]
movq mia, mm7 ; mia = [256, 256, 256, 256]
psrld mm0, 24 ; mm0 = [0, 0, 0, A2]
pshufw mm0, mm0, 0 ; mm0 = [A2, A2, A2, A2]
pmullw mca, mm0 ; mca = [0, R2*A2, G2*A2, B2*A2]
psubw mia, mm0 ; mia = [256 - A2, ...]
}
macro CANVAS_SET_PIXEL dst_ptr, color {
mov [dst_ptr], color
}
; Result := (A*alpha + B*(256-alpha))/256
macro CANVAS_SET_PIXELA dst_ptr, mca, mia {
movd mm0, [dst_ptr] ; mm0 = [0, ARGB1]
punpcklbw mm0, mm6 ; mm0 = [0, R1, G1, B1]
pmullw mm0, mia ; mm0 = [0, R1, G1, B1] * (256 - A2)
paddusw mm0, mca ; mm0 = 0 R1*X+Rb*Y | Ga*X+Gb*y Ba*X+Bb*Y
psrlw mm0, 8 ; mm0 = 0 0 0 Rc | 0 Gc 0 Bc
packuswb mm0, mm0 ; mm0 = 0 0 0 0 | 0 Rc Gc Bc
movd [dst_ptr], mm0
}
macro PUT_ALPHA_PIXEL color {
CANVAS_SET_ALPHA color,mm1,mm2
CANVAS_SET_PIXELA edi,mm1,mm2
}
macro CANVAS_GAMMA_TO_COLOR color {
if defined USE_GAMMA_CORRECTION
mov ebx, LUT_gamma2color
mov eax, color
xlatb
ror eax, 8
xlatb
ror eax, 8
xlatb
ror eax, 16
mov color, eax
end if
and color, 0x00ffffff
}
macro CANVAS_TRANSFORM xreg {
; transform by camera
addps xreg, xcam_trans ; translate
mulps xreg, xcam_scale ; scale
}
macro CANVAS_CLAMP xreg {
; clamp coords by viewport
maxps xreg, xcam_min
minps xreg, xcam_max
}
; in: xmm0 - position
Canvas.Dot:
movaps xmm1, xmm0
CANVAS_TRANSFORM xmm1
CANVAS_CLAMP xmm1
cvttps2dq xmm1, xmm1
movaps [v0], xmm1
; edi = data pointer
mov edi, [v0.y] ; y
imul edi, [canvas.width] ; *= canvas.width
add edi, [v0.x] ; += x
shl edi, 2 ; *= 4
add edi, [canvas.bytes] ; += data ptr
mov [edi], esi
ret
; draw 1px horizontal line with alpha blending
; in: esi = color
; edi = dest ptr
; ecx = width
Canvas.HLineAlpha:
CANVAS_SET_ALPHA esi,mm1,mm2
test ecx, ecx
js .done
@@: CANVAS_SET_PIXELA edi,mm1,mm2
add edi, 4 ; add offset to next pixel
dec ecx
jns @b
.done: ret
Canvas.VLineAlpha:
CANVAS_SET_ALPHA esi,mm1,mm2
test ecx, ecx
js .done
@@: CANVAS_SET_PIXELA edi,mm1,mm2
add edi, edx ; add offset to next pixel
dec ecx
jns @b
.done: ret
; in: xmm0 = min.xy, max.xy
; esi = color
Canvas.Fill:
CANVAS_GAMMA_TO_COLOR esi
CANVAS_TRANSFORM xmm0
movss xmm2, xcam_scale
shufps xmm2, xmm2, 0
mulps xmm1, xmm2
addps xmm0, xmm1
CANVAS_CLAMP xmm0
cvttps2dq xmm2, xmm0 ; xmm2 = (int)xmm0
movaps [v0], xmm2
; culling
cmp [v0.z], 0 ; if imax.x < 0