Browse Source

Added win state, fix wall constraints

master
Macoy Madson 3 years ago
parent
commit
9bb122d587
  1. BIN
      assets/Win.xcf
  2. BIN
      data/Win.bmp
  3. 64
      src/Main.cake

BIN
assets/Win.xcf

Binary file not shown.

BIN
data/Win.bmp

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 MiB

64
src/Main.cake

@ -38,12 +38,17 @@
(var g-window-height (const int) 2340)
(var g-window-safe-area-margin-px (const int) 40)
(defstruct-local grid-vec2
X int
Y int)
;; Game board constants
;; Note that these assume portrait aspect ratio by basing the size on the smaller dimension
(var g-game-board-outer-size-px (const int) (- g-window-width (* 2 g-window-safe-area-margin-px)))
(var g-game-board-outer-top-left-px (const vec2)
(array g-window-safe-area-margin-px (/ (- g-window-height g-game-board-outer-size-px) 2)))
(var g-game-board-grid-size (const int) 6)
(var g-game-board-win-cell (const grid-vec2) (array 5 2))
;; Use the nearest even pixel for pixel-perfect game board
(var g-game-board-margin-px (const int)
(/ (% g-game-board-outer-size-px g-game-board-grid-size) 2))
@ -54,14 +59,11 @@
(array (+ (vec-x g-game-board-outer-top-left-px) g-game-board-margin-px)
(+ (vec-y g-game-board-outer-top-left-px) g-game-board-margin-px)))
;;
;; Game board state
;;
(defstruct-local grid-vec2
X int
Y int)
(defun-local grid-vec2-add (a grid-vec2 b grid-vec2 &return grid-vec2)
(var result grid-vec2)
(set (vec-x result) (+ (vec-x a) (vec-x b)))
@ -328,7 +330,7 @@
(var piece-bottom-right-px vec2 (game-piece-get-screen-size piece))
(set piece-bottom-right-px (vec2-add piece-top-left-px piece-bottom-right-px))
;; (printf "%f %f -> %f %f vs. (%f %f)\n" (vec-xy piece-top-left-px) (vec-xy piece-bottom-right-px)
;; (vec-xy screen-point-to-test))
;; (vec-xy screen-point-to-test))
(return
(and
(>= (vec-x screen-point-to-test) (vec-x piece-top-left-px))
@ -363,6 +365,9 @@
(defun-local constrain-piece-moving-position (piece (* (const board-piece))
delta-position vec2 &return vec2)
(when (path piece > is-wall)
(return (array 0.f 0.f)))
(var constrained-position vec2 delta-position)
(if (path piece > is-vertical) ;; Constrain on axes
(set (vec-x constrained-position) 0.f)
@ -404,18 +409,19 @@
(while (is-within-grid scan-position)
(var space-index int (at (vec-y scan-position) (vec-x scan-position)
g-game-board-spatial-state))
(when (= -1 space-index)
(if (path direction > is-increasing)
(set max-position (grid-vec2-add max-position (path direction > delta)))
(set min-position (grid-vec2-add min-position (path direction > delta))))
(set scan-position (grid-vec2-add scan-position (path direction > delta))))
;; Anything other than empty space can immediately break us out
(break)))
(if (= -1 space-index)
(block
(if (path direction > is-increasing)
(set max-position (grid-vec2-add max-position (path direction > delta)))
(set min-position (grid-vec2-add min-position (path direction > delta))))
(set scan-position (grid-vec2-add scan-position (path direction > delta))))
;; Anything other than empty space can immediately break us out
(break))))
(var min-delta-px vec2 (array (* g-game-board-cell-size-px (to-float (vec-x min-position)))
(* g-game-board-cell-size-px (to-float (vec-y min-position)))))
(* g-game-board-cell-size-px (to-float (vec-y min-position)))))
(var max-delta-px vec2 (array (* g-game-board-cell-size-px (to-float (vec-x max-position)))
(* g-game-board-cell-size-px (to-float (vec-y max-position)))))
(* g-game-board-cell-size-px (to-float (vec-y max-position)))))
(if (path piece > is-vertical)
(set (vec-y constrained-position)
(clamp-within (vec-y constrained-position) (vec-y min-delta-px) (vec-y max-delta-px)))
@ -424,6 +430,15 @@
(return constrained-position))
(defun-local is-in-win-state (&return bool)
(var win-square-index BoardPieceIndex (at (vec-y g-game-board-win-cell)
(vec-x g-game-board-win-cell)
g-game-board-spatial-state))
(when (!= -1 win-square-index)
(var piece (* board-piece) (addr (at win-square-index g-game-board-pieces)))
(return (path piece > is-primary-piece)))
(return false))
;;
;; Helpers
;;
@ -559,6 +574,17 @@ Rush Hour database from Michael Fogleman.\n\n")
(var wall-a-origin (const vec2) (array 674.f 507.f))
(var wall-b-origin (const vec2) (array 342.f 682.f))
;; Why doesn't this work?
;; (var win-texture (* SDL_Texture)
;; (sdl-texture-from-bmp-color-to-transparent
;; (in-data-dir "Win.bmp")
;; renderer
;; 0xff 0x00 0xd3))
(var win-texture (* SDL_Texture)
(sdl-texture-from-bmp
(in-data-dir "Win.bmp") renderer))
(unless win-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)
@ -575,12 +601,11 @@ Rush Hour database from Michael Fogleman.\n\n")
(return 1)))
;; (game-board-load "60 IBBxooIooLDDJAALooJoKEEMFFKooMGGHHHM 2332")
(game-board-load "05 oooooEooDooEAADooEoBBCCCoooooooooooo 69")
(game-board-load "07 BBBoooooFxooAAFGooEooGooEDDDoooooooo 478")
;;
;; Game loop
;;
(var counter-num-ticks-per-second (const Uint64) (SDL_GetPerformanceFrequency))
(printf "%ld ticks per second\n" counter-num-ticks-per-second)
(var last-frame-perf-count Uint64 (* 0.016f counter-num-ticks-per-second))
(var selected-piece (* board-piece) null)
@ -632,10 +657,17 @@ Rush Hour database from Michael Fogleman.\n\n")
(set selected-piece nullptr)))
(SDL_RenderClear renderer)
(unless (= 0 (SDL_RenderCopy renderer background-texture null null))
(sdl-print-error)
(set exit-reason "Render error"))
(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")))
(scope ;; Draw grid
(var row int 0)
(while (< row g-game-board-grid-size)

Loading…
Cancel
Save