
;########################################################################
;#   http://babekcom.chat.ru/  Copyright © Babekcom Engineering Group   #
;########################################################################

.386
.model flat,stdcall

include const.inc
include struct.inc
include macro.inc
include extrn.inc

.const
ID_ICON		= 100
ID_CURSOR	= 200
ID_BITMAP0	= 400
ID_BITMAP1	= 401
ID_BITMAP2	= 402
ID_TIMER	= 500

.data
szCopy		db 'Copyright © Babekcom Engineering Group', 0
szTitleName	db 'Babekcom KillBall alpha', 0
szClassName	db 'ASMCLASS32',0
szMenuName	db 0
szCount		db 'счёт : '
szCt		db 32 dup('0'), 0
f1		db "%lu", 0
f2		db "%lX", 0

TimerID		dd 0	; идентификатор таймера
time		dd 50	; период таймера в милисекундах
_X		dd 0	; текущие координаты
_Y		dd 0	; вывода карты бит юнита
old_X		dd 0	; координаты предидущего
old_Y		dd 0	; вывода карты бит юнита
max_X		dd 0
max_Y		dd 0
min_X		dd 0
min_Y		dd 0
direct_x	dd 0	; направление движения юнита 0-вправо, 1-влево
direct_y	dd 0	; направление движения юнита 0-вниз, 1-вверх
mouse_x		dd 0	; x координата клика мыши в окне
mouse_y		dd 0	; y координата клика мыши в окне
step		dd 8	; шаг перемещения юнита
Yes		dd 0	; 1 - если попал
count		dd 0	; значение счетчика попаданий
ln		dd 8	; длина строки счетчика

.data?
CommandLine	dd ?
hInstance	dd ?
hWnd		dd ?
hDC		dd ?
hCompatibleDC	dd ?
hMenu		dd ?
hBitmap0	dd ?
hBitmap1	dd ?
hBitmap2	dd ?
wc		WNDCLASS <?>
msg		MSGSTRUCT  <?>
PaintStruct	PAINTSTRUCT <?>
client		RECT <?>
Bitmap		BITMAP <?>

.code
start:
	call	GetModuleHandleA, NULL
	mov	hInstance,eax
	call	GetCommandLineA
	mov	CommandLine, eax
	call	WinMain,hInst,NULL,CommandLine,SW_SHOWNORMAL
	call	ExitProcess,eax
;	call	InitCommonControls

WinMain proc hInst:DWORD,hPrevInst:DWORD,CmdLine:DWORD,CmdShow:DWORD

; подготовить и зарегистрировать класс (WNDCLASS structure)

        mov [wc.clsStyle], CS_HREDRAW + CS_VREDRAW + CS_GLOBALCLASS
        mov [wc.clsLpfnWndProc], offset WndProc
        mov [wc.clsCbClsExtra],NULL
        mov [wc.clsCbWndExtra],NULL
	push [hInstance]
	pop [wc.clsHInstance]
        call LoadIconA,hInstance,ID_ICON
        mov [wc.clsHIcon], eax

        call LoadCursorA,hInstance,ID_CURSOR
        mov [wc.clsHCursor],eax

        mov [wc.clsHbrBackground], COLOR_WINDOW+1
        mov dword ptr [wc.clsLpszMenuName], offset szMenuName
        mov dword ptr [wc.clsLpszClassName], offset szClassName
        mov [wc.hIconSm],0

	call RegisterClassA, offset wc

	push 0                    ; lpParam
	push [wc.clsHInstance]    ; hInstance
	push 0                    ; menu
	push 0                    ; parent hwnd
	push 300                  ; height
	push 400                  ; width
	push CW_USEDEFAULT        ; y
	push CW_USEDEFAULT        ; x
	push WS_OVERLAPPEDWINDOW  ; Style
	push offset szTitleName   ; Title string
	push offset szClassName   ; Class name
	push 0                    ; extra style
	call CreateWindowExA
	mov [hWnd],eax            ; идентификатор окна

        call LoadMenuA,hInstance,300  ; menu ID
        mov hMenu,eax
        call SetMenu,hWnd,eax

        ; показать окно
	call ShowWindow,[hWnd],SW_SHOWNORMAL
	call UpdateWindow,[hWnd]

;######################################################################
;#                  проверка сообщений от окна                        #
;######################################################################

msg_loop:
	call    GetMessageA,offset msg,NULL,0,0
        cmp     eax, 0				; проверка на WM_QUIT
        je      end_loop

        call    TranslateMessage,offset msg
        call    DispatchMessageA,offset msg
        jmp     short msg_loop
end_loop:
        mov     eax,[msg.msWPARAM]
	ret

WinMain endp
;######################################################################
WndProc proc uses ebx edi esi, hwnd:DWORD, wmsg:DWORD,\
                              wparam:DWORD, lparam:DWORD
	.if wmsg==WM_CREATE
		call    LoadBitmapA, hInstance, 400
		mov	hBitmap0, eax
		call    LoadBitmapA, hInstance, 401
		mov	hBitmap1, eax
		call    LoadBitmapA, hInstance, 402
		mov	hBitmap2, eax
		call	GetObjectA,hBitmap0,size BITMAP,offset Bitmap
		call	GetClientRect,hwnd,offset client
		mov	eax, client.Left
		mov	_X, eax
		mov	min_X, eax
		mov	eax, client.Right
		sub	eax, Bitmap.bmWidth
		mov	max_X, eax
		mov	eax, client.Top
		mov	_Y, eax
		mov	min_Y, eax
		mov	eax, client.Bottom
		sub	eax, Bitmap.bmHeight
		mov	max_Y, eax
		call	SetTimer,hwnd,ID_TIMER,time,NULL
		mov	TimerID,eax
	.elseif wmsg==WM_SIZE
		call	GetClientRect,hwnd,offset client
		mov	eax, client.Left
		mov	min_X, eax
		mov	eax, client.Right
		sub	eax, Bitmap.bmWidth
		mov	max_X, eax
		mov	eax, client.Top
		mov	min_Y, eax
		mov	eax, client.Bottom
		sub	eax, Bitmap.bmHeight
		mov	max_Y, eax
	.elseif wmsg==WM_PAINT
		call	PaintProc
	.elseif wmsg==WM_DESTROY
		call PostQuitMessage,NULL
		.if TimerID!=0
			call KillTimer,hwnd,TimerID
		.endif
	.elseif wmsg==WM_TIMER	; расчет координат _X и _Y
		mov	eax,_X
		mov	old_X,eax
		.if direct_x==0
		  add	eax,step
		.elseif direct_x==1
		  sub	eax,step
		.endif
		.if eax>=max_X
		  mov	direct_x,1
		  mov	eax,client.Right
		  sub	eax,Bitmap.bmWidth
		.endif
		.if eax<Bitmap.bmWidth
		  mov	direct_x,0
		  mov	eax,client.Left
		.endif
		mov	_X,eax

		mov	eax,_Y
		mov	old_Y,eax
		.if direct_y==0
		  add	eax,step
		.elseif direct_y==1
		  sub	eax,step
		.endif
		.if eax>=max_Y
		  mov	direct_y,1
		  mov	eax,client.Bottom
		  sub	eax,Bitmap.bmHeight
		.endif
		.if eax<Bitmap.bmHeight
		  mov	direct_y,0
		  mov	eax,client.Top
		.endif
		mov	_Y,eax
		call	InvalidateRect,hwnd,0,FALSE
	.elseif wmsg==WM_LBUTTONDOWN	; если нажата левая кнопка мыши
		call	OnClickProc
		call	InvalidateRect,hwnd,0,FALSE
	.else
		call DefWindowProcA,hwnd,wmsg,wparam,lparam		
		ret
	.endif
	xor eax,eax
	ret
WndProc endp
;######################################################################
PaintProc proc
	call	BeginPaint,hwnd,offset PaintStruct
	mov	hDC, eax
	call	TextOutA,hDC,8,8,offset szCount,ln
	call	CreateCompatibleDC,hDC
	mov	hCompatibleDC, eax
	call	SelectObject,eax,hBitmap0
	call	BitBlt,hDC,old_X,old_Y,8,8, \
		hCompatibleDC,0,0, \
		SRCCOPY
	.if Yes==1
	  call	SelectObject,hCompatibleDC,hBitmap2
	  call	BitBlt,hDC,_X,_Y,8,8, \
		hCompatibleDC,0,0, \
		SRCCOPY
	  mov	eax,0
	  mov	Yes,eax
	  mov	direct_x,eax
	  mov	direct_y,eax
	  mov	eax,client.Top
	  mov	_Y,eax
	  mov	eax,client.Left
	  mov	_X,eax
	.endif
	call	SelectObject,hCompatibleDC,hBitmap1
	call	BitBlt,hDC,_X,_Y,8,8, \
		hCompatibleDC,0,0, \
		SRCCOPY
	call	DeleteDC,hCompatibleDC
	call	EndPaint,hwnd,offset PaintStruct
	ret

PaintProc endp
;######################################################################
OnClickProc proc
	mov	eax, lparam
	and	eax,0FFFFh
	sub	eax, 4
	mov	mouse_x,eax
	mov	eax,lparam
	shr	eax,16
	sub	eax, 4
	mov	mouse_y,eax
	mov	eax,_X
	.if	eax>=mouse_x
	  mov	eax,_Y
	  .if	eax>=mouse_y
	    add	mouse_x,8
	    add mouse_y,8
	    .if	eax<=mouse_y
	      mov	eax,_X
	      .if eax<=mouse_x
	        inc	count
		mov	Yes,1
	      .endif
	    .endif
	  .endif
	.endif
	.if count==10
	  .if Yes==1
	    add	ln,1
	  .endif
	.endif
	.if count==100
	  .if Yes==1
	    add	ln,1
	  .endif
	.endif
	call    jprintf, offset szCt, offset f1, count
	ret
OnClickProc endp
;######################################################################
jprintf proc p1:dword, p2:dword, p3:dword
        push    p3
        push    p2
        push    p1
        call    _wsprintfA
        add     esp, 12
        ret
jprintf endp

end start
