;;-*-Lisp-*-
(in-package goal)

;; name: font.gc
;; name in dgo: font
;; dgos: GAME

(deftype draw-string-result (uint64)
  ((length float :offset 0)
   (b float :offset 32))
  )

;; DECOMP BEGINS

(defmethod-mips2c "(method 9 font-work)" 9 font-work)

(def-mips2c draw-string-asm (function string dma-buffer font-context draw-string-result))

;; WARN: Return type mismatch uint vs draw-string-result.
;; ERROR: Failed load: (set! a2-4 (l.wu (+ a2-3 2224))) at op 19
(defun draw-string ((arg0 string) (arg1 dma-buffer) (arg2 font-context))
  (local-vars (v0-2 uint))
  (#when PC_PORT
    ;; debug font scaling.
    (unless (logtest? (-> arg2 flags) (font-flags large pc-hack))
      (*! (-> arg2 scale) (get-debug-font-scale-factor))))
  (let ((v1-1 (the int (* 128.0 (-> arg2 alpha)))))
    (-> arg2 origin quad)
    (dotimes (a0-2 45)
      (dotimes (a1-1 4)
        (set! (-> *font-work* color-table a0-2 color a1-1 a) v1-1)
        )
      )
    (set! (-> *font-work* color-shadow w) v1-1)
    )
  (if (< (the-as uint (dma-buffer-free arg1)) (the-as uint (* (length arg0) 32)))
      (set! v0-2 (the-as uint (-> arg2 origin quad)))
      (set! v0-2 (the-as uint (draw-string-asm arg0 arg1 arg2)))
      )
  (dotimes (v1-7 45)
    (dotimes (a0-9 4)
      (set! (-> *font-work* color-table v1-7 color a0-9 a) #x80)
      )
    )
  (set! (-> *font-work* color-shadow w) 128)
  (the-as draw-string-result v0-2)
  )

; (def-mips2c get-string-length (function string font-context draw-string-result))
(def-mips2c get-string-length-asm (function string font-context draw-string-result))

(#when PC_PORT
;; og:preserve-this get-string-length is an asm function, but for PC port we use a trampoline in order to inject hacks more easily
(defun get-string-length ((text string) (ctx font-context))
  "return the length of a string, in gs framebuffer units"
  (let ((orig-scale (-> ctx scale))
        (result (begin (#when PC_PORT ;; debug font scaling.
                                      (unless (logtest? (-> ctx flags) (font-flags large pc-hack))
                                        (*! (-> ctx scale) (get-debug-font-scale-factor))))
                       (get-string-length-asm text ctx))))
    (set! (-> ctx scale) orig-scale)
    result)
  )

)

(defun draw-string-xy ((arg0 string) (arg1 dma-buffer) (arg2 int) (arg3 int) (arg4 font-color) (arg5 font-flags))
  (let ((s4-0 (new 'stack 'font-context *font-default-matrix* arg2 arg3 0.0 arg4 arg5)))
    (set-context! *font-work* s4-0)
    (draw-string arg0 arg1 s4-0)
    )
  )

;; WARN: Return type mismatch float vs none.
(defun draw-string-adv ((arg0 string) (arg1 dma-buffer) (arg2 font-context))
  (set-context! *font-work* arg2)
  (let ((v1-2 (draw-string arg0 arg1 arg2)))
    (+! (-> arg2 origin x) (-> v1-2 length))
    )
  (none)
  )
