Browse Source

Added immediate-mode buttons

Undo is not yet hooked up yet
master
Macoy Madson 2 years ago
parent
commit
753f843639
  1. BIN
      assets/Theme_Button.xcf
  2. BIN
      assets/Undo_Button.xcf
  3. BIN
      data/Theme_Button.bmp
  4. BIN
      data/Undo_Button.bmp
  5. 87
      src/Main.cake

BIN
assets/Theme_Button.xcf

Binary file not shown.

BIN
assets/Undo_Button.xcf

Binary file not shown.

BIN
data/Theme_Button.bmp

Binary file not shown.

After

Width:  |  Height:  |  Size: 54 KiB

BIN
data/Undo_Button.bmp

Binary file not shown.

After

Width:  |  Height:  |  Size: 54 KiB

87
src/Main.cake

@ -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)

Loading…
Cancel
Save