Browse Source

WIP game board

Macoy Madson 3 years ago
  1. 83


@ -1,22 +1,34 @@
(comptime-define-symbol 'Unix)
;; Until GameLib is relocatable, we will build from it
;; Until GameLib is relocatable, we will build from it; All comptime paths in this file are relative
;; to Dependencies/gamelib!
;; (add-cakelisp-search-directory "Dependencies/gamelib/src")
;; (set-cakelisp-option cakelisp-src-dir "Dependencies/gamelib/Dependencies/cakelisp/src")
;; (add-cakelisp-search-directory "Dependencies/gamelib/Dependencies/cakelisp/runtime")
(add-cakelisp-search-directory "src")
(add-cakelisp-search-directory "src" "Dependencies/cakelisp/runtime")
(import "SDL.cake" "Math.cake")
(import "SDL.cake" "Math.cake"
&comptime-only "Macros.cake")
(c-import "<stdio.h>"
"SDL.h" "SDL_syswm.h" "SDL_timer.h" "SDL_render.h")
;; Constants
(define-constant DATA_DIR "data/")
(defmacro in-data-dir (path-in-data string)
(tokenize-push output
(static-string-combine DATA_DIR (token-splice path-in-data)))
(return true))
;; Made for OnePlus 6T screen
(var g-window-width (const int) 1080)
(var g-window-height (const int) 2340)
(var g-window-safe-area-margin-px (const int) 40)
;; Game board
;; 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)
@ -32,12 +44,60 @@
(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)))
(define-constant DATA_DIR "data/")
;; Game board state
(defmacro in-data-dir (path-in-data string)
(tokenize-push output
(static-string-combine DATA_DIR (token-splice path-in-data)))
(return true))
(defstruct-local grid-vec2
X int
Y int)
(defstruct-local board-piece
grid-position grid-vec2 ;; Always from the top left
moving-position vec2 ;; Floating point for animation, smooth dragging
num-cells int
is-vertical bool
exists bool ;; Empty slots in the array will have this as false
is-wall bool
is-primary-piece bool)
;; Pieces are stored separately, then their positions inform occupied state, which is used to check
;; movement constraints. null = empty cell. Pointers used to easily pick from tap/click
(var g-game-board-occupied-state ([] 6 ([] 6 (* board-piece))) (array 0))
;; Max pieces is determined as follows:
;; 6x6 grid = 36 squares
;; Minimum moveable piece size = 2 squares. Wall size = 1 square; max 2 walls
;; 36 squares / 2 (min piece size) = 13; if two walls, need 14
(var g-game-board-pieces ([] 14 board-piece) (array 0))
(defmacro on-each-existing-board-piece (piece-pointer-name symbol &rest body any)
(var next-piece-index int 0)
(while (< next-piece-index (array-size g-game-board-pieces))
(var (token-splice piece-pointer-name) (* board-piece)
(addr (at next-piece-index g-game-board-pieces)))
(incr next-piece-index) ;; In case expansions include (continue), we've already incremented
(when (path (token-splice piece-pointer-name) > exists)
(token-splice-rest body tokens))))
(return true))
(defun-local print-board-piece (piece (* (const board-piece))))
(defun-local game-board-sync-occupied-state ()
;; Zero out to make overlap validation easy
(memset g-game-board-occupied-state 0 (array-size g-game-board-occupied-state))
(print-board-piece piece)
;; TODO: Determine occupied, error if pointer already set
;; Helpers
(defun-local sdl-intialize-2d-renderer (renderer-out (* (* SDL_Renderer)) window (* SDL_Window)
&return bool)
@ -71,6 +131,10 @@
(return false))
(return true))
;; Main
(defun main (&return int)
(printf "Kitty Gridlock\n\n
Created by Macoy Madson <>.\n
@ -79,6 +143,7 @@ Copyright (c) 2021 Macoy Madson.\n
Licensed under GPL-3.0-or-later.\n
Rush Hour database from Michael Fogleman.\n\n")
;; Useful reference for creating assets
(printf "Window and board dimensions:\n
\twindow width: %d\n
\twindow height: %d\n