



; ========================================================================================

align 16
export Window_Startup:

  sub rsp, 78h

  mov [rsp+60h], rsi
  mov [rsp+68h], rdi
  mov [rsp+70h], rbx

; -----  Enter  -----


; ----------------------------
; clear WNDCLASSEXA stucture
; ----------------------------
  lea edi, win                                    ; win = address of the structure
  mov ecx, 10                                     ; ecx: loop limit = # of bytes / 8
  xor eax, eax                                    ; eax: fillvalue = 0
  dec ecx                                         ; ecx: loop counter - 1

; loop
: mov [edi+ecx*8], rax                            ; use indexed*8 addressing
  dec ecx
  jns <                                           ; until loop counter = loop limit

; --------------------------------------
;     fill the WNDCLASSEXA structure
; --------------------------------------
  mov D[win.cbSize], sizeof(WNDCLASSEXA)          ; (50h) structure size in bytes

  mov rax, [hInstance]                            ; get program's instance handle
  mov	[win.hInstance], rax                        ; store handle in the window structure
    
  lea ebx, ClassName
  lea eax, WndProc
  mov [win.lpszClassName], ebx                    ; ptr to window class name
  mov [win.lpfnWndProc], eax                      ; ptr to window procedure


; win.hbrBackground = CreateSolidBrush(RGB(color));
  mov ecx, 0 ;AlmostBlack                            ; p1:  COLORREF crColor (brush color)
  mov [GUISkinColor], ecx                         ; save a copy for gui updates

; convert the color to Windows: bgr = rgb
  bswap ecx                   ; flip the alpha channel into bits 0-7
  shr ecx, 8                  ; get rid of bits 0-7

  call CreateSolidBrush       ; returns: handle of a logical brush for LoadCursor
    test rax, rax
    jz >> wcError
  mov [win.hbrBackground], rax                    ; store the handle

; ----------------------------
;     load the hand cursor
; ----------------------------
  xor ecx, ecx                                    ; p1: NULL
  mov edx, IDC_HAND                               ; p2: const for the hand
  call LoadCursorA
    test rax, rax
    jz >> wcError

  mov [win.hCursor], rax                          ; = hand cursor

; ----------------------------
;     register the window
; ----------------------------
  lea ecx, win                ; p1: &win
  call RegisterClassExA       ; register the window
    test eax, eax
    jz >> wcError

; --------------------------------------
;   create a centered 1920x1080 window
; --------------------------------------
  xor ecx, ecx                ; p1: DWORD dwExStyle (0)
  lea edx, ClassName          ; p2: LPCTSTR lpClassName
  lea r8d, WindowTitle        ; p3: LPCTSTR lpWindowName
  mov r9d, WS_POPUP+WS_VISIBLE ; p4: DWORD dwStyle
    
  mov [rsp+20h], ecx          ; p5: int x (x=0)
  mov [rsp+28h], ecx          ; p6: int y (y=0)

  mov eax, [monitor_w]
  mov ebx, [monitor_h]
  mov [rsp+30h], eax          ; p7: int nWidth (monitor width)
  mov [rsp+38h], ebx          ; p8: int nHeight (monitor height)

  mov [rsp+40h], rcx          ; p9: HWND hWndParent (0 = top level)
  mov [rsp+48h], rcx          ; p10: HMENU hMenu (0 = no menu)

  mov rax, [hInstance]
  mov [rsp+50h], rax          ; p11: HINSTANCE hInstance

  mov [rsp+58h], rcx          ; p12: LPVOID lpParam  (0)

; ------------------
; create the window
; ------------------
  call CreateWindowExA
    test rax, rax
    jz >> wcError

  mov [hWindow], rax     ; store the window handle

; ---------------
; hide the cursor
; ---------------
  mov ecx, 0
  call ShowCursor


; ----- REPORT SUCCESS -----

  mov eax, TRUE     ; 1 signals success


; ----- EXIT -----

; if eax = 0, then an error occurred
wcError:
  mov rsi, [rsp+60h]
  mov rdi, [rsp+68h]
  mov rbx, [rsp+70h]
  add rsp, 78h

  ret

; ========================================================================================
