AutoDefine "Off"
Const GWL_STYLE=-16
Const GWL_EXSTYLE=-20
Const WS_OVERLAPPED=0
Const SWP_NOMOVE=2
Declare Import,SetWindowPos(hwnd As Int,hWndInsertAfter As Int,x As Int,y As Int,cx As Int,cy As Int,wFlags As Int),Int
Declare Import,AdjustWindowRectEx(rec As Pointer,style As Int,mflag As Int,ExStyle As Int),Int
Declare Import,GetWindowLongA(hwin As Int,nIndex As Int),Int
Declare Import,SetRect(pRect As Pointer,x1 As Int,y1 As Int,x2 As Int,y2 As Int),Int
Declare Import,PtInRect(pRect As WINRECT, x As Int, y As Int),Int
Dim wndMain As Window
Dim grid[960]As WINRECT
Dim cells[48,20] As Char
Dim x,z,row As Int
OpenWindow wndMain,0,0,0,0,@MinBox|@NoAutoDraw,0,"iLife",&wndMainProc
SetClientSize(wndMain,980,400)
Control wndMain,@Button,"Q U I T",0,0,18,18*6,@CTLBTNMulti|@CTLBTNFlat,1
Control wndMain,@Button,"A B O U T",0,18*6,18,18*6,@CTLBTNMulti|@CTLBTNFlat,2
Control wndMain,@Button,"C L E A R",0,18*12,18,18*6,@CTLBTNMulti|@CTLBTNFlat,3
Control wndMain,@Button,"R A N D",0,18*18,18,18*4+3,@CTLBTNMulti|@CTLBTNFlat,4
SetFont wndMain,"FixedSys",0,0,0,1
SetFont wndMain,"FixedSys",0,0,0,2
SetFont wndMain,"FixedSys",0,0,0,3
SetFont wndMain,"FixedSys",0,0,0,4
WaitUntil wndMain=0
End
Sub wndMainProc
	Dim i,t As Int
	Select @Message
		Case @IDCloseWindow
			CloseWindow wndMain
		Case @IDPaint
			Paint(wndMain)
		Case @IDLButtonDN
			For x = 0 To 959
				If PtInRect(grid[x],@MouseX,@MouseY)
					cells[x]=1
					Rect wndMain,grid[x].left,grid[x].top,18,18,RGB(255,0,0),RGB(0,0,255)
					BreakFor
				EndIf
			Next x
		Case @IDRButtonDN
			For x = 0 To 959
				If PtInRect(grid[x],@MouseX,@MouseY)
					cells[x]=0
					Rect wndMain,grid[x].left,grid[x].top,18,18,RGB(255,0,0),RGB(255,255,255)
					BreakFor
				EndIf
			Next x
		Case @IDControl
			Select @ControlID
				Case 1
					SendMessage wndMain,@IDCloseWindow,0,0
				Case 2
					Messagebox wndMain,"Made by Wayne Anthony\nwith code by Bevets.","About."
				Case 3
					Set(cells,null)
					Paint(wndMain)
				Case 4
					Set(cells,null,True)
					Paint(wndMain)
			EndSelect
			SetFocus wndMain
		Case @IDChar
			Select @wParam
				Case Asc("r")
				Case& Asc("R")
				Case& Asc("1")
					runGen(cells)
					Paint(wndMain)
				Case Asc("q")
				Case& Asc("Q")
					SendMessage wndMain,@IDCloseWindow,0,0
				Case 50
				Case& 51
				Case& 52
				Case& 53
				Case& 54
				Case& 55
				Case& 56
				Case& 57
					t=@wParam-48
					For i=1 to t
						runGen(cells)
					Next i
					Paint(wndMain)
			EndSelect
	EndSelect
	Return
EndSub
Sub runGen(cells[48,20] As Char)
	Dim i,ii,nAlive As Int
	Dim newCells[48,20] As Char
	For i=0 to 47
		For ii=0 to 19
			nAlive=0
			nAlive=cells[Dec(i,48),ii]+cells[Inc(i,48),ii]+cells[i,Dec(ii,20)]+cells[i,Inc(ii,20)]+_
				cells[Dec(i,48),Dec(ii,20)]+cells[Inc(i,48),Inc(ii,20)]+cells[Dec(i,48),Inc(ii,20)]+cells[Inc(i,48),Dec(ii,20)]
			Select cells[i,ii]
				Case 0
					If nAlive=3 Then newCells[i,ii]=1 Else newCells[i,ii]=0
				Case 1
					If nAlive<>2 And nAlive<>3 Then newCells[i,ii]=0 Else newCells[i,ii]=1
			EndSelect
		Next ii
	Next i
	For i=0 to 47
		For ii=0 to 19
			cells[i,ii]=newCells[i,ii]
		Next ii
	Next i
	Return
EndSub
Sub Set(cells[48,20] As Char,value As Char,Opt random=False As Int)
	Dim i,ii As Int
	For i=0 to 47
		For ii=0 to 19
			If random then value=Rand(0,1)
			cells[i,ii]=value
		Next ii
	Next i
	Return
EndSub
Sub Inc(in As Int,max As Int),Int
	Dim r As Int
	'If in=max-1 Then r=0 Else r=in+1
	_asm
		dec dword [ebp+12]
		mov eax,[ebp+12]
		cmp dword [ebp+8],eax
		jne diff
		mov dword [ebp-4],0
		jmp done
		diff:
		mov eax,[ebp+8]
		mov dword [ebp-4],eax
		inc dword [ebp-4]
		done:
	_endasm
	Return r
EndSub
Sub Dec(in As Int,max As Int),Int
	Dim r As Int
	'If in=0 Then r=max-1 Else r=in-1
	_asm
		dec dword [ebp+12]
		cmp dword [ebp+8],0
		jne nzero
		mov dword eax,[ebp+12]
		mov dword [ebp-4],eax
		jmp finished
		nzero:
		mov eax,[ebp+8]
		mov dword [ebp-4],eax
		dec dword [ebp-4]
		finished:
	_endasm
	Return r
EndSub
Sub Paint(win As Window)
	Dim cons As Int
	cons=20
	z=0
	For x = 0 To 959
		If x%48=0
			row = x/48
			z = 0
		EndIf
		grid[x].top=row*cons
		z=z+cons
		grid[x].left=z
		grid[x].right=18
		grid[x].bottom=18
		Rect win,grid[x].left,grid[x].top,grid[x].right,grid[x].bottom,RGB(255,0,0)
		If cells[x]=1 Then Rect win,grid[x].left,grid[x].top,18,18,RGB(255,0,0),RGB(0,0,255) Else Rect win,grid[x].left,grid[x].top,18,18,RGB(255,0,0),RGB(255,255,255)
		grid[x].right=grid[x].left+18
		grid[x].bottom=grid[x].top+18
	Next x
	Return
EndSub
Sub SetClientSize(win As Window,w As Int,h As Int,Opt fmenu=0 As Int),Int
	 WINRECT rec
	 Int style,exstyle,err
	 style=0:exstyle=0:err=0
	 style=GetWindowLongA(win.hwnd,GWL_STYLE)
	 If style
		style=style & not(WS_OVERLAPPED)
		exstyle=GetWindowLongA(win.hwnd,GWL_EXSTYLE)
		If style=0 | exstyle=0 Then Return 0
	 EndIf
	 err=SetRect(rec,0,0,w,h)
	 If err
		err=AdjustWindowRectEx(rec,style,fmenu,exstyle)
		If err
			err=SetWindowPos(win.hwnd,0,0,0,rec.right+Int(ABS(rec.left)),rec.bottom+Int(ABS(rec.top)),SWP_NOMOVE)
		EndIf
	 EndIf
	Return err
EndSub