Browse Source

Tween to position, reading from database

master
Macoy Madson 3 years ago
parent
commit
6cd7376579
  1. 5
      .gitignore
  2. 6
      Build.sh
  3. 73
      src/Decompression.cake
  4. 49
      src/Main.cake

5
.gitignore

@ -68,4 +68,7 @@ decompression-test
*.kra-autosave.kra
.stfolder
.stversions
*.bmp~
*.bmp~
# Generated from database
data/puzzles.txt

6
Build.sh

@ -6,8 +6,10 @@
cd Dependencies/gamelib && ./Build_FromKitty.sh || exit $?
CAKELISP=./Dependencies/cakelisp/bin/cakelisp
# After relocation, this will be able to be simply .
KITTY_DIR=../..
$CAKELISP --execute $KITTY_DIR/src/Main.cake || exit $?
# $CAKELISP --execute $KITTY_DIR/src/Decompression.cake || exit $?
$CAKELISP --execute $KITTY_DIR/src/Decompression.cake || exit $?
$CAKELISP --execute $KITTY_DIR/src/Main.cake || exit $?

73
src/Decompression.cake

@ -11,7 +11,13 @@
(var-global g-bunzip-debug bool false)
(defun bunzip-decompress (bz2-file (* (const char)) &return bool)
(def-function-signature-global bunzip-decompress-callback (buffer (* char) num-bytes-this-read int
user-data (* void) &return bool))
(defun bunzip-decompress (bz2-file (* (const char))
receive-data-callback bunzip-decompress-callback
user-data (* void)
&return bool)
(var file-descriptor int (open bz2-file O_RDONLY))
(when (= -1 file-descriptor)
(printf "Failed to load file %s\n" bz2-file)
@ -42,10 +48,18 @@
(close file-descriptor)
(return false))
(var num-bytes-this-read int 0)
(when (> return-code 0)
(set num-bytes-this-read return-code)
;; Set null terminator
(set (at return-code output) 0)
(set num-bytes-read (+ num-bytes-read return-code)))
(set (at num-bytes-this-read output) 0)
(set num-bytes-read (+ num-bytes-read num-bytes-this-read)))
(when receive-data-callback
(unless (receive-data-callback output num-bytes-this-read user-data)
(free header-data)
(close file-descriptor)
(return false)))
;; (printf "Output: %d bytes\n%s[end]\n" return-code output)
(when (= return-code RETVAL_LAST_BLOCK)
@ -60,10 +74,61 @@
(comptime-cond
('Kitty-Main)
(true ;; Standalone test
(var g-known-num-puzzles (const int) 2577412)
(var g-num-puzzles-to-read (const int) 100)
;; Note that this doesn't change the distribution of puzzles
(var g-puzzle-skip-count (const int) (/ g-known-num-puzzles g-num-puzzles-to-read))
(defstruct-local puzzle-line-buffer
buffer (* char)
buffer-size int
buffer-write (* char)
num-puzzles-read int
file-out (* FILE))
(defun-local process-puzzle-data (buffer (* char) num-bytes-this-read int
user-data (* void) &return bool)
(var work-in-progress-line (* puzzle-line-buffer) (type-cast user-data (* puzzle-line-buffer)))
(var current-char (* char) buffer)
(while (< (- current-char buffer) num-bytes-this-read)
(cond
((= '\n' (deref current-char))
(set (deref (path work-in-progress-line > buffer-write)) 0)
(when (= 0 (mod (path work-in-progress-line > num-puzzles-read) g-puzzle-skip-count))
(printf "%s\n" (path work-in-progress-line > buffer))
(when (path work-in-progress-line > file-out)
(fprintf (path work-in-progress-line > file-out) "%s\n"
(path work-in-progress-line > buffer))))
(set (path work-in-progress-line > buffer-write)
(path work-in-progress-line > buffer))
(incr (path work-in-progress-line > num-puzzles-read)))
(true
(set (deref (path work-in-progress-line > buffer-write)) (deref current-char))
(incr (path work-in-progress-line > buffer-write))
(when (>= (- (path work-in-progress-line > buffer-write)
(path work-in-progress-line > buffer))
(- (path work-in-progress-line > buffer-size) 1))
(printf "Error: didn't encounter newline before end of buffer\n")
(return false))))
(incr current-char))
(return true))
(defun main (&return int)
(var bz2-file (* (const char)) "assets/rush.txt.bz2")
(unless (bunzip-decompress bz2-file)
;; For when the read buffer straddles a single puzzle
;; 46 chars max per puzzle, plus newline, plus room for null terminator for easy printing
(var work-in-progress-buffer ([] 48 char) (array 0))
(var file-out (* FILE) (fopen "data/puzzles.txt" "w"))
(unless file-out
(printf "Could not open output file\n")
(return 1))
(var work-in-progress-line puzzle-line-buffer
(array work-in-progress-buffer (array-size work-in-progress-buffer)
work-in-progress-buffer 0 file-out))
(unless (bunzip-decompress bz2-file process-puzzle-data (addr work-in-progress-line))
(fclose file-out)
(return 1))
(fclose file-out)
(return 0))
(set-cakelisp-option executable-output "../../decompression-test")))

49
src/Main.cake

@ -553,7 +553,7 @@
(unless texture (sdl-print-error))
(return texture))
(defun-local print-time-delta (start-num-perf-ticks Uint64 label (* (const char)))
(defun-local sdl-print-time-delta (start-num-perf-ticks Uint64 label (* (const char)))
(var performance-num-ticks-per-second (const Uint64) (SDL_GetPerformanceFrequency))
(var current-counter-ticks Uint64 (SDL_GetPerformanceCounter))
@ -563,6 +563,18 @@
(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)
(return (array
(interpolate-range (vec-x from) (vec-x to)
0.f 1.f factor)
(interpolate-range (vec-y from) (vec-y to)
0.f 1.f factor))))
(defun-local vec2-is-zero (vec vec2 &return bool)
(return (and (= 0.f (vec-x vec))
(= 0.f (vec-y vec)))))
;;
;; Main
;;
@ -629,11 +641,11 @@ Rush Hour database from Michael Fogleman.\n\n")
(SDL_RenderPresent renderer)
(SDL_DestroyTexture loading-texture))
(print-time-delta start-load-ticks "Loading screen displayed")
(sdl-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")
;; (sdl-print-time-delta start-load-ticks "Database loaded")
(var background-texture (* SDL_Texture) (sdl-texture-from-bmp (in-data-dir "Board.bmp")
renderer))
@ -680,14 +692,15 @@ Rush Hour database from Michael Fogleman.\n\n")
(in-data-dir "Undo_Button.bmp") renderer))
(unless undo-button-texture (return 1))
(print-time-delta start-load-ticks "Textures loaded")
(sdl-print-time-delta start-load-ticks "Textures loaded")
;; (game-board-load "60 IBBxooIooLDDJAALooJoKEEMFFKooMGGHHHM 2332")
(game-board-load "07 BBBoooooFxooAAFGooEooGooEDDDoooooooo 478")
;;
;; Game loop
;;
(var last-frame-perf-count Uint64 (* 0.016f performance-num-ticks-per-second))
(var delta-time float 0.016f) ;; Made up but reasonable frame time
(var last-frame-perf-count Uint64 (* delta-time performance-num-ticks-per-second))
(var recent-n-perf-counts ([] 10 Uint64) (array 0))
(var recent-n-perf-counts-write-head int 0)
@ -697,7 +710,7 @@ Rush Hour database from Michael Fogleman.\n\n")
(var in-state input-state (array 0))
(print-time-delta start-load-ticks "Game loop starting")
(sdl-print-time-delta start-load-ticks "Game loop starting")
(var exit-reason (* (const char)) null)
(while (not exit-reason)
@ -787,6 +800,13 @@ Rush Hour database from Michael Fogleman.\n\n")
(set exit-reason "Piece movement constraints failed to keep piece on board")
(break))
(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))
(- (vec-x (path selected-piece > moving-position))
(* g-game-board-cell-size-px (vec-x delta-grid-movement))))
(set (vec-y (path selected-piece > moving-position))
(- (vec-y (path selected-piece > moving-position))
(* g-game-board-cell-size-px (vec-y delta-grid-movement))))
(game-board-sync-occupied-state))
(set selected-piece nullptr)))
@ -846,7 +866,16 @@ Rush Hour database from Michael Fogleman.\n\n")
(set (path piece > moving-position)
(constrain-piece-moving-position piece mouse-delta-pos)))
;; Reset positions on release
(set (path piece > moving-position) (array 0)))
(when (not (vec2-is-zero (path piece > moving-position)))
(var ease-position vec2 (vec2-interpolate
(/ 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)
(set (vec-x ease-position) 0.f))
(when (< (fabs (vec-y ease-position)) 0.01f)
(set (vec-y ease-position) 0.f))
(set (path piece > moving-position) ease-position)))
(var piece-position vec2 (game-piece-position-to-screen-position piece))
(var piece-size vec2 (game-piece-get-screen-size piece))
@ -886,8 +915,8 @@ Rush Hour database from Michael Fogleman.\n\n")
(when (>= recent-n-perf-counts-write-head (array-size recent-n-perf-counts))
(set recent-n-perf-counts-write-head 0))
(var delta-time float (/ frame-diff-ticks
(type-cast performance-num-ticks-per-second float)))
(set delta-time (/ frame-diff-ticks
(type-cast performance-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))
@ -917,6 +946,8 @@ Rush Hour database from Michael Fogleman.\n\n")
(set win-texture null)
(SDL_DestroyTexture theme-button-texture)
(set theme-button-texture null)
(SDL_DestroyTexture undo-button-texture)
(set undo-button-texture null)
(SDL_DestroyRenderer renderer)

Loading…
Cancel
Save