Browse Source

Pieces partially moving, fix frame rate

* Pieces can now be moved, though there is a bug with the constrain
code constraining a bit too much
* Added timing code to confirm that the game was running at 4,000hz to
5000hz, which was causing annoying GPU coil whine. I arbitrarily sleep
now to get ~90hz instead. I have a TODO item to actually calculate a
good sleep value
master
Macoy Madson 3 years ago
parent
commit
2fd60ad0f6
  1. 71
      src/Main.cake

71
src/Main.cake

@ -11,7 +11,9 @@
&comptime-only "Macros.cake")
(c-import "<stdio.h>"
"SDL.h" "SDL_syswm.h" "SDL_timer.h" "SDL_render.h")
"SDL.h" "SDL_syswm.h" "SDL_timer.h" "SDL_render.h"
;; For round()
"<math.h>")
;;
;; Constants
@ -20,6 +22,10 @@
(var debug-log-enabled bool true)
;; (var debug-log-enabled bool false)
;; TODO: Actually look at frame rate and adjust accordingly
;; This fixed GPU "coil whine" which I was getting running at 5400hz (completely unnecessary)
(var todo-arbitrary-delay-ms int 10)
(define-constant DATA_DIR "data/")
(defmacro in-data-dir (path-in-data string)
@ -145,6 +151,12 @@
(path piece > is-wall)
(path piece > is-primary-piece)))
(defun-local is-within-grid (coordinate grid-vec2 &return bool)
(return (and (>= (vec-x coordinate) 0)
(>= (vec-y coordinate) 0)
(< (vec-x coordinate) g-game-board-grid-size)
(< (vec-y coordinate) g-game-board-grid-size))))
(defun-local game-board-sync-occupied-state (&return bool)
;; Zero out to make overlap validation easy
(memset g-game-board-spatial-state -1
@ -157,10 +169,7 @@
(var num-cells int (path piece > num-cells))
(scope ;; Validate that piece does not go off board
(when (or (>= (vec-x (path piece > grid-position)) g-game-board-grid-size)
(< (vec-x (path piece > grid-position)) 0)
(>= (vec-y (path piece > grid-position)) g-game-board-grid-size)
(< (vec-y (path piece > grid-position)) 0))
(unless (is-within-grid (path piece > grid-position))
(printf "error: Piece %p origin is off board! Must abort occupied state sync\n" piece)
(return false))
(if (path piece > is-vertical)
@ -333,12 +342,7 @@
(array (/ (type-cast (vec-x board-position) int) g-game-board-cell-size-px)
(/ (type-cast (vec-y board-position) int) g-game-board-cell-size-px)))
(unless
(and
(>= (vec-x grid-position) 0)
(>= (vec-y grid-position) 0)
(< (vec-x grid-position) g-game-board-grid-size)
(< (vec-y grid-position) g-game-board-grid-size))
(unless (is-within-grid grid-position)
(return null))
(var piece-index char
@ -397,10 +401,7 @@
directions i
(var direction (* scan-direction) (addr (at i directions)))
(var scan-position grid-vec2 (path direction > start-position))
(while (and (>= (vec-x scan-position) 0)
(>= (vec-y scan-position) 0)
(< (vec-x scan-position) g-game-board-grid-size)
(< (vec-y scan-position) g-game-board-grid-size))
(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)
@ -578,6 +579,10 @@ Rush Hour database from Michael Fogleman.\n\n")
;;
;; 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)
(var selection-start-position vec2 (array 0))
(var exit-reason (* (const char)) null)
@ -602,9 +607,29 @@ Rush Hour database from Michael Fogleman.\n\n")
(set selected-piece (pick-game-piece-from-screen-position mouse-position))))
(block
(when selected-piece
;; TODO: Selection released; let block finish move and update grid
(ignore))
(set selected-piece nullptr)))
;; Selection released; let block finish move and update grid
(var delta-grid-movement grid-vec2
(array
(type-cast
(round
(/ (vec-x (path selected-piece > moving-position))
g-game-board-cell-size-px)) int)
(type-cast
(round
(/ (vec-y (path selected-piece > moving-position))
g-game-board-cell-size-px)) int)))
(printf "%f %f becomes %d %d\n"
(vec-x (path selected-piece > moving-position))
(vec-y (path selected-piece > moving-position))
(vec-xy delta-grid-movement))
(var new-position grid-vec2
(grid-vec2-add (path selected-piece > grid-position) delta-grid-movement))
(unless (is-within-grid new-position)
(set exit-reason "Piece movement constraints failed to keep piece on board")
(break))
(set (path selected-piece > grid-position) new-position)
(game-board-sync-occupied-state))
(set selected-piece nullptr)))
(SDL_RenderClear renderer)
(unless (= 0 (SDL_RenderCopy renderer background-texture null null))
@ -667,7 +692,15 @@ Rush Hour database from Michael Fogleman.\n\n")
(sdl-print-error)
(set exit-reason "Render error")))
(SDL_RenderPresent renderer))
(SDL_RenderPresent renderer)
(var current-counter-ticks Uint64 (SDL_GetPerformanceCounter))
(var frame-diff-ticks Uint64 (- current-counter-ticks last-frame-perf-count))
(set last-frame-perf-count (SDL_GetPerformanceCounter))
(var delta-time float (/ frame-diff-ticks
(type-cast counter-num-ticks-per-second float)))
;; (printf "%lu %f %fhz\n" frame-diff-ticks delta-time (/ 1.f delta-time))
(SDL_Delay todo-arbitrary-delay-ms))
;;
;; Shutdown

Loading…
Cancel
Save