jak-project/goal_src/jak1/examples/debug-draw-example.gc
Tyler Wilding c162c66118
g/j1: Cleanup all main issues in the formatter and format all of goal_src/jak1 (#3535)
This PR does two main things:
1. Work through the main low-hanging fruit issues in the formatter
keeping it from feeling mature and usable
2. Iterate and prove that point by formatting all of the Jak 1 code
base. **This has removed around 100K lines in total.**
- The decompiler will now format it's results for jak 1 to keep things
from drifting back to where they were. This is controlled by a new
config flag `format_code`.

How am I confident this hasn't broken anything?:
- I compiled the entire project and stored it's `out/jak1/obj` files
separately
- I then recompiled the project after formatting and wrote a script that
md5's each file and compares it (`compare-compilation-outputs.py`
- The results (eventually) were the same:

![Screenshot 2024-05-25
132900](https://github.com/open-goal/jak-project/assets/13153231/015e6f20-8d19-49b7-9951-97fa88ddc6c2)
> This proves that the only difference before and after is non-critical
whitespace for all code/macros that is actually in use.

I'm still aware of improvements that could be made to the formatter, as
well as general optimization of it's performance. But in general these
are for rare or non-critical situations in my opinion and I'll work
through them before doing Jak 2. The vast majority looks great and is
working properly at this point. Those known issues are the following if
you are curious:

![image](https://github.com/open-goal/jak-project/assets/13153231/0edfaba1-6d36-40f5-ab23-0642209867c4)
2024-06-05 22:17:31 -04:00

313 lines
12 KiB
Common Lisp

;;-*-Lisp-*-
(in-package goal)
(require "engine/gfx/sprite/sparticle/sparticle-launcher.gc")
;; This file demonstrates how to use the debug draw.
;; To run this:
#|
(make-group "iso") ;; build the game
(lt) ;; connect to the runtime
(lg) ;; have the runtime load the game engine
(test-play) ;; start the game loop
(ml "goal_src/examples/debug-draw-example.gc") ;; build and load this file.
;; to get control of camera from keyboard
(kill-test-procs)
(launch-wasd-process)
;; you can turn on actor marks (full) or visibilty boxes in the debug menu.
|#
(defun test-function ((iter int))
"This function draws the debug stuff. You can edit this, then reload this file to play with it."
;; val will increase from 0 to 1, then reset back to 0.
(let* ((frame (the float (mod (* 4 iter) 3200)))
(val (/ frame 1600.0)))
(format *stdcon* "~0kval ~f~%" val)
;; orbit the camera around in a circle with radius 5 m.
(let* ((rad (meters 5.0))
(x (* rad (sin (* (degrees 360.0) val))))
(z (* rad (cos (* (degrees 360.0) val))))
(cam-pos (new 'stack 'vector))
(cam-inv-rot (new 'stack 'matrix)))
;; this matrix will look directly at the origin...
(set! (-> cam-pos x) x)
(set! (-> cam-pos z) z)
(set! (-> cam-pos y) (meters 2.))
(forward-down->inv-matrix cam-inv-rot cam-pos (new 'static 'vector :y 1.0))
;; if the camera is here.
(set! (-> cam-pos x) (- 0. x))
(set! (-> cam-pos z) (- 0. z))
(set! (-> cam-pos y) (meters -2.))
;;(debug-set-camera-pos-rot! cam-pos cam-inv-rot)
;;(hack-update-camera cam-pos cam-inv-rot)
;; create some test points
(let ((p0 (new 'static 'vector :x (meters .8) :y (meters .2) :z (meters 2.0)))
(p1 (new 'static 'vector :x (meters .3) :y (meters .3) :z (meters 2.5)))
(p2 (new 'static 'vector :x (meters .5) :y (meters .7) :z (meters 1.5))))
;;(add-debug-point #t (bucket-id debug-draw0) (new 'static 'vector))
(add-debug-x #t (bucket-id debug-draw0) (new 'static 'vector) (new 'static 'rgba :g #x80 :a #x80))
(add-debug-box #t (bucket-id debug-draw0) p0 p2 (new 'static 'rgba :b #x80 :a #x80))
(add-debug-flat-triangle #t (bucket-id debug-draw0) p0 p1 p2 (new 'static 'rgba :r #x80))
(add-debug-text-3d #t (bucket-id debug-draw0) "triangle!" p0 (font-color menu-flag-on) (the vector2h #f))
(add-debug-sphere #t (bucket-id debug-draw0) p2 (meters 0.5) (new 'static 'rgba :r #x80))))
(update-hack)
;; these also work
;;(draw-end-credits (the int frame))
;; (draw-title-credits val)
)
(none))
(defun update-hack ()
(let ((a2-0 (new 'stack-no-clear 'vector)))
(set! (-> a2-0 x) 1.0)
(set! (-> a2-0 y) 0.0)
(set! (-> a2-0 z) 1.0)
(set! (-> a2-0 w) 1.0)
(let ((gp-0 (vector+float*! (new 'stack-no-clear 'vector) (new 'static 'vector) a2-0 0.0)))
(let* ((s5-0 (matrix-local->world #f #f))
(f28-0 (lerp-scale 122.88 245.76 (fabs (-> s5-0 vector 2 y)) 0.0 0.7))
(f30-0 (lerp-scale 2048.0 245.76 (fabs (-> s5-0 vector 2 y)) 0.0 0.7)))
(let ((f26-0 (lerp-scale 0.0 0.1 (-> s5-0 vector 2 y) 0.3 0.7))
(f0-10 (lerp-scale 1.0 0.1 (-> s5-0 vector 2 y) 0.3 0.7)))
; (if (< 0.0 f26-0)
; (send-event *camera* 'part-water-drip f26-0 f0-10)
; )
)
(set! (-> *part-id-table* 37 init-specs 4 initial-valuef) f28-0)
(set! (-> *part-id-table* 37 init-specs 4 random-rangef) f28-0)
(set! (-> *part-id-table* 38 init-specs 4 initial-valuef) f28-0)
(set! (-> *part-id-table* 38 init-specs 4 random-rangef) f28-0)
(set! (-> *part-id-table* 37 init-specs 5 initial-valuef) f30-0)
(set! (-> *part-id-table* 37 init-specs 5 random-rangef) f30-0)
(set! (-> *part-id-table* 38 init-specs 5 initial-valuef) f30-0)
(set! (-> *part-id-table* 38 init-specs 5 random-rangef) f30-0))
(dotimes (i 10)
(launch-particles (-> *part-id-table* 37) gp-0)
(launch-particles (-> *part-id-table* 38) gp-0))))
0
(none))
(defun launch-test-process ()
"Call this to launch a process that draws the debug demo"
(let ((proc (get-process *nk-dead-pool* process 1024)))
(activate proc *active-pool* 'test *kernel-dram-stack*)
(run-next-time-in-process proc (lambda () (let ((iter 0)) (while #t (test-function iter) (suspend) (+! iter 1)))))
proc))
#|
(define *wasd-camera-transform* (new 'global 'transform))
(defun wasd-camera-update ()
(let ((local-trans (new-stack-vector0))
(trans *wasd-camera-transform*)
(fast-mode (cpad-hold? 0 r2))
(pad-idx 0))
;; circle/square move camera relative x (left and right)
(set! (-> local-trans x)
(cond
((cpad-hold? 0 circle)
-1600.0
)
((cpad-hold? 0 square)
1600.0
)
(else
0.0
)
)
)
;; no way to move camera relative y (up/down)
(set! (-> local-trans y) 0.0)
;; in and out movement
(set! (-> local-trans z)
(cond
((cpad-hold? 0 down)
-1600.0
)
((cpad-hold? 0 up)
1600.0
)
(else
0.0
)
)
)
(set! (-> local-trans w) 1.0)
(when fast-mode
(vector-float*! local-trans local-trans 10.)
)
;; rotate this into world frame
(let ((inv-cam-rot (new-stack-vector0))
(cam-rot-mat (new-stack-matrix0)))
;; unused.
(vector-negate! inv-cam-rot (-> trans rot))
;; convert rotation to rotation matrix.
(matrix-rotate-xyz! cam-rot-mat (-> trans rot))
;; and rotate the translation.
(vector-matrix*! local-trans local-trans cam-rot-mat)
)
;; and update the transform
(vector+! (-> trans trans) (-> trans trans) local-trans)
;; don't forget to fix w.
(set! (-> trans trans w) 1.0)
;; global translation
(let ((diff (if fast-mode 10000.0 2000.0)))
(if (cpad-hold? 0 l1)
(set! (-> trans trans y) (+ diff (-> trans trans y)))
)
(if (cpad-hold? 0 r1)
(set! (-> trans trans y) (+ (- diff) (-> trans trans y)))
)
)
;; rotation (don't allow camera roll)
(if (cpad-hold? 0 x)
(set! (-> trans rot x) (+ 200. (-> trans rot x)))
)
(if (cpad-hold? 0 triangle)
(set! (-> trans rot x) (+ -200. (-> trans rot x)))
)
(if (cpad-hold? 0 left)
(set! (-> trans rot y) (+ 300. (-> trans rot y)))
)
(if (cpad-hold? 0 right)
(set! (-> trans rot y) (+ -300. (-> trans rot y)))
)
(set! (-> trans scale x) 1.)
(set! (-> trans scale y) 1.)
(set! (-> trans scale z) 1.)
(set! (-> trans scale w) 1.)
)
)
(defun launch-test-process ()
"Call this to launch a process that draws the debug demo"
(let ((proc (get-process *nk-dead-pool* process 1024)))
(activate proc *active-pool* 'test *kernel-dram-stack*)
(run-next-time-in-process proc (lambda ()
(let ((iter 0))
(while #t
(test-function iter)
(suspend)
(+! iter 1)
)
)
)
)
proc)
)
(defun launch-wasd-process ()
"Launch a process to control the camera with keyboard.
Note that the test process above also controls the camera and should be killed first.
For example, after loading this file, do
(kill-test-procs)
(launch-wasd-process)"
(let ((proc (get-process *nk-dead-pool* process 1024)))
(activate proc *active-pool* 'test *kernel-dram-stack*)
(set! (-> *wasd-camera-transform* trans y) (meters 4.0))
(run-next-time-in-process proc (lambda ()
(let ((iter 0))
(while #t
(wasd-camera-update)
(let ((mat (new-stack-matrix0)))
(transform-matrix-calc! *wasd-camera-transform* mat)
(set! (-> mat data 3) 0.)
(set! (-> mat data 7) 0.)
(set! (-> mat data 11) 0.)
(set! (-> mat data 12) 0.)
(set! (-> mat data 13) 0.)
(set! (-> mat data 14) 0.)
(set! (-> mat data 15) 1.)
;;(matrix-transpose! mat mat)
(hack-update-camera (-> *wasd-camera-transform* trans) mat)
)
(suspend)
(+! iter 1)
)
)
)
)
proc)
)
;; This will spawn a process the first time this file is loaded
;;(define-perm *test-process* process (launch-wasd-process))
(defun kill-test-procs ()
"Kill all processes started by launch-test-process"
(kill-by-name "test" *active-pool*)
)
|#
(set! *display-profile* #t)
; (set! *display-split-boxes* #t)
; (set! *display-level-border* #t)
; (set! *display-split-box-info* #t)
(set! *display-deci-count* #t)
(defun text-randomizer ()
(dotimes (i 10000)
(let ((idx1 (rand-vu-int-count (-> *common-text* length))))
(dotimes (j 20)
(rand-vu))
(let ((idx2 (rand-vu-int-count (-> *common-text* length))))
(let ((temp (-> *common-text* data idx1 text)))
(set! (-> *common-text* data idx1 text) (-> *common-text* data idx2 text))
(set! (-> *common-text* data idx2 text) temp))))))
(defun update-subdivide-settings! ((settings subdivide-settings) (math-cam math-camera) (idx int))
"Change the subdivide settings."
(set! (-> settings meters 0) (meters 20000.0))
(set! (-> settings meters 1) (meters 20.0))
(set! (-> settings meters 2) (meters 20.0))
(set! (-> settings meters 3) (meters 20.0))
(set! (-> settings meters 4) (meters 20.0))
; (set! (-> settings meters 0) (-> settings far idx))
; (set! (-> settings meters 4) (-> settings close idx))
; (let ((f0-3 (* 0.14285715 (- (-> settings meters 0) (-> settings meters 4)))))
; (set! (-> settings meters 3) (+ (-> settings meters 4) (* 0.5 f0-3)))
; (set! (-> settings meters 2) (+ (-> settings meters 3) f0-3))
; (set! (-> settings meters 1) (+ (-> settings meters 2) (* 2.0 f0-3)))
; )
(let ((f0-7 (/ (-> math-cam inv-hmge-scale w) (-> math-cam d))))
(dotimes (v1-5 5)
(set! (-> settings dist v1-5) (* f0-7 (-> settings meters v1-5)))))
(set! (-> *tfrag-work* frag-dists x) (- (-> settings meters 0)))
(set! (-> *tfrag-work* frag-dists y) (- (-> settings meters 1)))
(set! (-> *tfrag-work* frag-dists z) (- (-> settings meters 2)))
(set! (-> *tfrag-work* frag-dists w) (- (-> settings meters 4)))
0
(none))
;;(test-make-target)
(define *debug-load-level* #f)
(defun load-slot-1 ((lev symbol))
(load-state-want-levels (-> *load-state* want 0 name) lev)
(set! *debug-load-level* lev)
(process-new-function process
(lambda ()
(suspend)
(suspend)
(while (!= (-> *level* data 1 status) 'loaded)
(format 0 "waiting...~%")
(suspend))
(load-state-want-display-level *debug-load-level* #t))))