; ---- Text window for Ultrabasic
; ---- Gabriel Fernandez
; ---- You are not allowed to use this code without permission
Createtextwindow:
  push dword 0
  call [GetModuleHandleA]

  mov [Textwindowclass+WNDCLASSEX.hInstance],eax

  PUSH dword Textwindowclass
  call [RegisterClassExA]

  push dword 0
  push dword [Textwindowclass+WNDCLASSEX.hInstance]
  push dword 0
  push dword 0
  push dword 400
  push dword 640
  push dword CW_USEDEFAULT
  push dword CW_USEDEFAULT
  push dword WS_NORESIZABLEWINDOW
  push dword Textwinname
  push dword Textwinclassname
  push dword 0
  call [CreateWindowExA]
  mov [Windowhandle],eax

  CMP eax, 0
  PUSH EAX
  mov eax, 5
  JE ErrorEndProgram
  POP EAX
  PUSH dword SW_SHOWNORMAL
  PUSH eax
  call [ShowWindow]
  PUSH dword [Windowhandle]
  call [UpdateWindow]
  RET

textwinproc:
  Mov eax, [ESP+8] ;uMsg]
  CMP eax, WM_DESTROY
  JNE avoidmsg1
    PUSH dword 0
    CALL [PostQuitMessage]
    xor eax, eax
 RET
avoidmsg1:      ; Dibujar la pantalla de texto
  CMP eax, WM_PAINT
  JNE avoidmsg2
  PUSH dword textPaintStruct
  PUSH dword [Windowhandle]
  CALL [BeginPaint]
  MOV [textdchandle], eax
  PUSH dword fixedsys
  PUSH dword DEFAULT_PITCH ;+ FF_SCRIPT
  PUSH dword DEFAULT_QUALITY
  PUSH dword CLIP_DEFAULT_PRECIS
  PUSH dword OUT_DEFAULT_PRECIS
  PUSH dword OEM_CHARSET
  PUSH dword 0
  PUSH dword 0
  PUSH dword 0
  PUSH dword FW_NORMAL
  PUSH dword 0
  PUSH dword 0
  PUSH dword 8
  PUSH dword 16
  CALL [CreateFontA]
  Push eax
  Push dword [textdchandle]
  CALL [SelectObject]
  mov [TextFont], eax
  
  CALL PutTextScreenOnTextWindow

  PUSH dword [TextFont]
  PUSH dword [textdchandle]
  CALL [SelectObject]  
  PUSH dword textPaintStruct
  PUSH dword [Windowhandle]
  CALL [EndPaint]
  RET
avoidmsg2:
    mov eax, esp
    PUSH dword [EAX+16]
    PUSH dword [EAX+12]
    PUSH dword [EAX+8]
    PUSH dword [EAX+4]
    CALL [DefWindowProcA]
 RET

Endwinprogramloop:
  PUSH dword 0
  PUSH dword 0
  PUSH dword 0
  PUSH dword MSGstruct
  CALL [GetMessageA]
  CMP EAX, 0
  JE EndProgram
  push dword MSGstruct
  call [DispatchMessageA]
  jmp Endwinprogramloop

PutTextScreenOnTextWindow:
  mov dword [curty], 0
getcharloopy:
  mov dword [curtx], 0
  mov eax, [curty]
  mov ebx, [curty]
  mov ecx, [textscreenoffset]
  mov [curtoffset], ecx
  SHL eax, 4
  SHL ebx, 6
  add eax, ebx
  shl eax, 1
  add [curtoffset], eax

getcharloopx:
  mov eax, [curtx]
  mov ecx, [curty]
  shl eax, 3
  shl ecx, 4
  mov [writex], eax
  mov [writey], ecx

  mov eax, 0
  mov ebx, [curtx]
  shl ebx, 1
  add ebx, [curtoffset]

  mov eax, 0
  mov al, [ebx+1]
  AND al, 240
  Shr eax, 2
  mov ecx, [dosscreencolors+eax]
  mov [temp], ecx

  PUSH ecx
  PUSH dword [textdchandle]
  CALL [SetTextColor]

  PUSH dword [temp]
  PUSH dword [textdchandle]
  CALL [SetBkColor]

  Push dword 1
  Push dword clearbkchar
  Push dword [writey]
  Push dword [writex]
  PUSH dword [textdchandle]
  CALL [TextOutA]  

  mov al, [ebx+1]
  AND al, 15
  Shl eax, 2
  mov ecx, [dosscreencolors+eax]
  PUSH ecx
  PUSH dword [textdchandle]
  CALL [SetTextColor]

  Push dword 1
  Push ebx
  Push dword [writey]
  Push dword [writex]
  PUSH dword [textdchandle]
  CALL [TextOutA]  

  inc dword [curtx]
  CMP dword [curtx], 80
  JNGE getcharloopx

  inc dword [curty]
CMP dword [curty], 25
JNGE getcharloopy

RET
