|
|
@ -483,6 +483,13 @@ |
|
|
|
(return (path piece > is-primary-piece))) |
|
|
|
(return false)) |
|
|
|
|
|
|
|
|
|
|
|
(var g-current-puzzle (* puzzle-data) null) |
|
|
|
|
|
|
|
(defun game-board-load-next-puzzle () |
|
|
|
(set g-current-puzzle (addr (at (mod (rand) g-num-puzzles) g-puzzle-list))) |
|
|
|
(game-board-load (path g-current-puzzle > board))) |
|
|
|
|
|
|
|
;; |
|
|
|
;; UI (immediate-mode) |
|
|
|
;; |
|
|
@ -522,6 +529,51 @@ |
|
|
|
(return (and (path in-state > was-clicked) |
|
|
|
(is-within-aabb (path in-state > pointer-position) position size)))) |
|
|
|
|
|
|
|
(defstruct-local font-glyph |
|
|
|
symbol char |
|
|
|
position SDL_Rect) |
|
|
|
(var g-font-atlas ([] font-glyph) |
|
|
|
(array |
|
|
|
(array '1' (array 3 13 88 130)) |
|
|
|
(array '2' (array 108 13 97 129)) |
|
|
|
(array '3' (array 221 13 92 124)) |
|
|
|
(array '4' (array 329 21 79 125)) |
|
|
|
(array '5' (array 425 15 79 125)) |
|
|
|
(array '6' (array 513 16 82 118)) |
|
|
|
(array '7' (array 601 18 91 111)) |
|
|
|
(array '8' (array 692 13 92 115)) |
|
|
|
(array '9' (array 788 13 88 126)) |
|
|
|
(array '0' (array 894 17 83 116)) |
|
|
|
(array '/' (array 981 14 80 139)))) |
|
|
|
|
|
|
|
(defun-local pick-font-glyph-from-character (symbol char &return (* font-glyph)) |
|
|
|
(var i int 0) |
|
|
|
(while (< i (array-size g-font-atlas)) |
|
|
|
(when (= symbol (field (at i g-font-atlas) symbol)) |
|
|
|
(return (addr (at i g-font-atlas)))) |
|
|
|
(incr i)) |
|
|
|
(return null)) |
|
|
|
|
|
|
|
(defun-local draw-string (renderer (* SDL_Renderer) font-texture (* SDL_Texture) |
|
|
|
text (* (const char)) position vec2) |
|
|
|
(var current-position vec2 position) |
|
|
|
(var current-char (* (const char)) text) |
|
|
|
(while (deref current-char) |
|
|
|
(var glyph (* font-glyph) (pick-font-glyph-from-character (deref current-char))) |
|
|
|
(unless glyph ;; Missing character |
|
|
|
(incr current-char) |
|
|
|
(continue)) |
|
|
|
|
|
|
|
(var dest-rect SDL_Rect (path glyph > position)) |
|
|
|
(var src-rect SDL_Rect (path glyph > position)) |
|
|
|
(set (field dest-rect x) (to-float (vec-x current-position))) |
|
|
|
(set (field dest-rect y) (to-float (vec-y current-position))) |
|
|
|
(unless (= 0 (SDL_RenderCopy renderer font-texture |
|
|
|
(addr src-rect) (addr dest-rect))) |
|
|
|
(sdl-print-error)) |
|
|
|
(set (vec-x current-position) (+ (vec-x current-position) (field src-rect w))) |
|
|
|
(incr current-char))) |
|
|
|
|
|
|
|
;; |
|
|
|
;; Helpers |
|
|
|
;; |
|
|
@ -599,7 +651,7 @@ |
|
|
|
(var delta-time float (/ frame-diff-ticks |
|
|
|
(type-cast performance-num-ticks-per-second float))) |
|
|
|
|
|
|
|
(printf "%s at %f seconds\n" label delta-time)) |
|
|
|
(printf "--- %s at %f seconds\n" label delta-time)) |
|
|
|
|
|
|
|
;; Factor 0 to 1 |
|
|
|
(defun-local vec2-interpolate (factor float from vec2 to vec2 &return vec2) |
|
|
@ -683,8 +735,8 @@ Rush Hour database from Michael Fogleman.\n\n") |
|
|
|
|
|
|
|
;; (unless (bunzip-decompress puzzle-database-filename) |
|
|
|
;; (return 1)) |
|
|
|
;; (sdl-print-time-delta start-load-ticks "Database loaded") |
|
|
|
(unless (read-puzzles) (return 1)) |
|
|
|
(sdl-print-time-delta start-load-ticks "Puzzles loaded") |
|
|
|
|
|
|
|
(var background-texture (* SDL_Texture) (sdl-texture-from-bmp (in-data-dir "Board.bmp") |
|
|
|
renderer)) |
|
|
@ -731,11 +783,19 @@ Rush Hour database from Michael Fogleman.\n\n") |
|
|
|
(in-data-dir "Undo_Button.bmp") renderer)) |
|
|
|
(unless undo-button-texture (return 1)) |
|
|
|
|
|
|
|
(var font-texture (* SDL_Texture) |
|
|
|
(sdl-texture-from-bmp |
|
|
|
(in-data-dir "Font.bmp") renderer)) |
|
|
|
(unless font-texture (return 1)) |
|
|
|
|
|
|
|
(sdl-print-time-delta start-load-ticks "Textures loaded") |
|
|
|
|
|
|
|
;; (game-board-load "IBBxooIooLDDJAALooJoKEEMFFKooMGGHHHM") |
|
|
|
(srand (type-cast (SDL_GetPerformanceCounter) int)) |
|
|
|
(game-board-load (field (at (mod (rand) g-num-puzzles) g-puzzle-list) board)) |
|
|
|
|
|
|
|
;; TODO NEXT: display turn count |
|
|
|
;; (game-board-load "IBBxooIooLDDJAALooJoKEEMFFKooMGGHHHM") |
|
|
|
(game-board-load-next-puzzle) |
|
|
|
|
|
|
|
;; |
|
|
|
;; Game loop |
|
|
|
;; |
|
|
@ -747,6 +807,7 @@ Rush Hour database from Michael Fogleman.\n\n") |
|
|
|
(var is-day-mode bool false) |
|
|
|
(var selected-piece (* board-piece) null) |
|
|
|
(var selection-start-position vec2 (array 0)) |
|
|
|
(var current-move-count int 0) |
|
|
|
|
|
|
|
(var in-state input-state (array 0)) |
|
|
|
|
|
|
@ -839,6 +900,9 @@ Rush Hour database from Michael Fogleman.\n\n") |
|
|
|
(unless (is-within-grid new-position) |
|
|
|
(set exit-reason "Piece movement constraints failed to keep piece on board") |
|
|
|
(break)) |
|
|
|
(when (or (!= 0 (vec-x delta-grid-movement)) |
|
|
|
(!= 0 (vec-y delta-grid-movement))) |
|
|
|
(incr current-move-count)) |
|
|
|
(set (path selected-piece > grid-position) new-position) |
|
|
|
;; Remove the moving position difference from changing grid position, b/c moving is relative |
|
|
|
(set (vec-x (path selected-piece > moving-position)) |
|
|
@ -861,13 +925,8 @@ Rush Hour database from Michael Fogleman.\n\n") |
|
|
|
(when (do-button (addr in-state) (array 0.f 0.f) (array 166.f 166.f) renderer theme-button-texture) |
|
|
|
(set is-day-mode (not is-day-mode))) |
|
|
|
(when (do-button (addr in-state) (array 300.f 0.f) (array 166.f 166.f) renderer undo-button-texture) |
|
|
|
(game-board-load (field (at (mod (rand) g-num-puzzles) g-puzzle-list) board)))) |
|
|
|
|
|
|
|
(when (is-in-win-state) |
|
|
|
(var dest-rect SDL_Rect (array 60 140 960 500)) |
|
|
|
(unless (= 0 (SDL_RenderCopy renderer win-texture null (addr dest-rect))) |
|
|
|
(sdl-print-error) |
|
|
|
(set exit-reason "Render error"))) |
|
|
|
(set current-move-count 0) |
|
|
|
(game-board-load-next-puzzle))) |
|
|
|
|
|
|
|
;; Pointer position |
|
|
|
;; (scope ;; TODO REMOVE |
|
|
@ -911,9 +970,9 @@ Rush Hour database from Michael Fogleman.\n\n") |
|
|
|
(/ delta-time 0.1f) |
|
|
|
(path piece > moving-position) (array 0.f 0.f))) |
|
|
|
;; Make sure it's not always approaching smaller and smaller values |
|
|
|
(when (< (fabs (vec-x ease-position)) 0.01f) |
|
|
|
(when (< (fabs (vec-x ease-position)) 0.001f) |
|
|
|
(set (vec-x ease-position) 0.f)) |
|
|
|
(when (< (fabs (vec-y ease-position)) 0.01f) |
|
|
|
(when (< (fabs (vec-y ease-position)) 0.001f) |
|
|
|
(set (vec-y ease-position) 0.f)) |
|
|
|
(set (path piece > moving-position) ease-position))) |
|
|
|
|
|
|
@ -943,6 +1002,20 @@ Rush Hour database from Michael Fogleman.\n\n") |
|
|
|
(sdl-print-error) |
|
|
|
(set exit-reason "Render error"))) |
|
|
|
|
|
|
|
;; Turn count |
|
|
|
(var turns-buffer ([] 8 char) (array 0)) |
|
|
|
(var num-printed int |
|
|
|
(snprintf turns-buffer (array-size turns-buffer) |
|
|
|
"%d/%d" current-move-count (? g-current-puzzle (path g-current-puzzle > num-moves) 0))) |
|
|
|
(set (at num-printed turns-buffer) 0) |
|
|
|
(draw-string renderer font-texture turns-buffer (array 600.f 10.f)) |
|
|
|
|
|
|
|
(when (is-in-win-state) |
|
|
|
(var dest-rect SDL_Rect (array 60 140 960 500)) |
|
|
|
(unless (= 0 (SDL_RenderCopy renderer win-texture null (addr dest-rect))) |
|
|
|
(sdl-print-error) |
|
|
|
(set exit-reason "Render error"))) |
|
|
|
|
|
|
|
(SDL_RenderPresent renderer) |
|
|
|
|
|
|
|
(var current-counter-ticks Uint64 (SDL_GetPerformanceCounter)) |
|
|
@ -962,7 +1035,7 @@ Rush Hour database from Michael Fogleman.\n\n") |
|
|
|
|
|
|
|
(scope ;; Frame time |
|
|
|
(var num-timings int (array-size recent-n-perf-counts)) |
|
|
|
(printf "Recent %d frame timings:\n" num-timings) |
|
|
|
(printf "Recent %d frame timings (fixed sleep of %d ms):\n" num-timings todo-arbitrary-delay-ms) |
|
|
|
(var i int 0) |
|
|
|
(while (< i num-timings) |
|
|
|
(var delta-time float (/ (at i recent-n-perf-counts) |
|
|
@ -990,6 +1063,8 @@ Rush Hour database from Michael Fogleman.\n\n") |
|
|
|
(set theme-button-texture null) |
|
|
|
(SDL_DestroyTexture undo-button-texture) |
|
|
|
(set undo-button-texture null) |
|
|
|
(SDL_DestroyTexture font-texture) |
|
|
|
(set undo-button-texture null) |
|
|
|
|
|
|
|
(SDL_DestroyRenderer renderer) |
|
|
|
|
|
|
|