|
|
@ -445,6 +445,45 @@ |
|
|
|
(return (path piece > is-primary-piece))) |
|
|
|
(return false)) |
|
|
|
|
|
|
|
;; |
|
|
|
;; UI (immediate-mode) |
|
|
|
;; |
|
|
|
(defstruct-local input-state |
|
|
|
pointer-position vec2 |
|
|
|
is-pointer-pressed bool |
|
|
|
was-clicked bool) ;; Click on pointer release |
|
|
|
|
|
|
|
(defun-local update-input-state (in-state (* input-state) mouse-position vec2 is-pressed bool) |
|
|
|
(set (path in-state > pointer-position) mouse-position) |
|
|
|
(set (path in-state > was-clicked) (and (path in-state > is-pointer-pressed) |
|
|
|
(not is-pressed))) |
|
|
|
(set (path in-state > is-pointer-pressed) is-pressed)) |
|
|
|
|
|
|
|
(defun-local is-within-aabb (point-to-test vec2 upper-left vec2 size vec2 &return bool) |
|
|
|
(var bottom-right vec2 (vec2-add upper-left size)) |
|
|
|
(return |
|
|
|
(and |
|
|
|
(>= (vec-x point-to-test) (vec-x upper-left)) |
|
|
|
(>= (vec-y point-to-test) (vec-y upper-left)) |
|
|
|
(< (vec-x point-to-test) (vec-x bottom-right)) |
|
|
|
(< (vec-y point-to-test) (vec-y bottom-right))))) |
|
|
|
|
|
|
|
(defun-local do-button (in-state (* input-state) |
|
|
|
position vec2 size vec2 |
|
|
|
renderer (* SDL_Renderer) |
|
|
|
texture (* SDL_Texture) ;; Assumes whole texture drawn |
|
|
|
&return bool) ;; Returns whether clicked |
|
|
|
(when (and renderer texture) |
|
|
|
(var dest-rect SDL_Rect (array (type-cast (vec-x position) int) |
|
|
|
(type-cast (vec-y position) int) |
|
|
|
(type-cast (vec-x size) int) |
|
|
|
(type-cast (vec-y size) int))) |
|
|
|
(unless (= 0 (SDL_RenderCopy renderer texture null (addr dest-rect))) |
|
|
|
(sdl-print-error))) |
|
|
|
|
|
|
|
(return (and (path in-state > was-clicked) |
|
|
|
(is-within-aabb (path in-state > pointer-position) position size)))) |
|
|
|
|
|
|
|
;; |
|
|
|
;; Helpers |
|
|
|
;; |
|
|
@ -592,10 +631,9 @@ Rush Hour database from Michael Fogleman.\n\n") |
|
|
|
|
|
|
|
(print-time-delta start-load-ticks "Loading screen displayed") |
|
|
|
|
|
|
|
(unless (bunzip-decompress puzzle-database-filename) |
|
|
|
(return 1)) |
|
|
|
|
|
|
|
(print-time-delta start-load-ticks "Database loaded") |
|
|
|
;; (unless (bunzip-decompress puzzle-database-filename) |
|
|
|
;; (return 1)) |
|
|
|
;; (print-time-delta start-load-ticks "Database loaded") |
|
|
|
|
|
|
|
(var background-texture (* SDL_Texture) (sdl-texture-from-bmp (in-data-dir "Board.bmp") |
|
|
|
renderer)) |
|
|
@ -633,22 +671,16 @@ Rush Hour database from Michael Fogleman.\n\n") |
|
|
|
(in-data-dir "Win.bmp") renderer)) |
|
|
|
(unless win-texture (return 1)) |
|
|
|
|
|
|
|
(print-time-delta start-load-ticks "Textures loaded") |
|
|
|
(var theme-button-texture (* SDL_Texture) |
|
|
|
(sdl-texture-from-bmp |
|
|
|
(in-data-dir "Theme_Button.bmp") renderer)) |
|
|
|
(unless theme-button-texture (return 1)) |
|
|
|
(var undo-button-texture (* SDL_Texture) |
|
|
|
(sdl-texture-from-bmp |
|
|
|
(in-data-dir "Undo_Button.bmp") renderer)) |
|
|
|
(unless undo-button-texture (return 1)) |
|
|
|
|
|
|
|
(ignore ;; Example of setting pieces manually |
|
|
|
(set (field (at 0 g-game-board-pieces) num-cells) 3) |
|
|
|
(set (vec-x (field (at 0 g-game-board-pieces) grid-position)) 0) |
|
|
|
(set (vec-y (field (at 0 g-game-board-pieces) grid-position)) 3) |
|
|
|
(set (field (at 0 g-game-board-pieces) is-vertical) true) |
|
|
|
(set (field (at 0 g-game-board-pieces) label) 'b') |
|
|
|
|
|
|
|
(set (field (at 1 g-game-board-pieces) num-cells) 3) |
|
|
|
(set (vec-x (field (at 1 g-game-board-pieces) grid-position)) 1) |
|
|
|
(set (vec-y (field (at 1 g-game-board-pieces) grid-position)) 3) |
|
|
|
(set (field (at 1 g-game-board-pieces) is-vertical) false) |
|
|
|
(set (field (at 1 g-game-board-pieces) label) 'c') |
|
|
|
(unless (game-board-sync-occupied-state) |
|
|
|
(return 1))) |
|
|
|
(print-time-delta start-load-ticks "Textures loaded") |
|
|
|
|
|
|
|
;; (game-board-load "60 IBBxooIooLDDJAALooJoKEEMFFKooMGGHHHM 2332") |
|
|
|
(game-board-load "07 BBBoooooFxooAAFGooEooGooEDDDoooooooo 478") |
|
|
@ -663,6 +695,8 @@ Rush Hour database from Michael Fogleman.\n\n") |
|
|
|
(var selected-piece (* board-piece) null) |
|
|
|
(var selection-start-position vec2 (array 0)) |
|
|
|
|
|
|
|
(var in-state input-state (array 0)) |
|
|
|
|
|
|
|
(print-time-delta start-load-ticks "Game loop starting") |
|
|
|
|
|
|
|
(var exit-reason (* (const char)) null) |
|
|
@ -722,6 +756,9 @@ Rush Hour database from Michael Fogleman.\n\n") |
|
|
|
decentered)))) |
|
|
|
(set mouse-position (vec2-multiply mouse-position to-logical-scale))) |
|
|
|
|
|
|
|
(update-input-state (addr in-state) mouse-position |
|
|
|
(bit-and mouse-button-state SDL_BUTTON_LMASK)) |
|
|
|
|
|
|
|
(if (bit-and mouse-button-state SDL_BUTTON_LMASK) |
|
|
|
(block |
|
|
|
(unless selected-piece ;; If holding, allow dragging even when not exactly on piece |
|
|
@ -760,11 +797,17 @@ Rush Hour database from Michael Fogleman.\n\n") |
|
|
|
(sdl-print-error) |
|
|
|
(set exit-reason "Render error")) |
|
|
|
|
|
|
|
(scope ;; UI |
|
|
|
(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) |
|
|
|
(set is-day-mode (not is-day-mode)))) |
|
|
|
|
|
|
|
(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-print-error) |
|
|
|
(set exit-reason "Render error"))) |
|
|
|
|
|
|
|
;; Pointer position |
|
|
|
;; (scope ;; TODO REMOVE |
|
|
@ -872,6 +915,8 @@ Rush Hour database from Michael Fogleman.\n\n") |
|
|
|
(set pieces-texture null) |
|
|
|
(SDL_DestroyTexture win-texture) |
|
|
|
(set win-texture null) |
|
|
|
(SDL_DestroyTexture theme-button-texture) |
|
|
|
(set theme-button-texture null) |
|
|
|
|
|
|
|
(SDL_DestroyRenderer renderer) |
|
|
|
|
|
|
|