@ -9,22 +9,85 @@
( import "SDL.cake" )
( c-import "<stdio.h>"
"SDL.h" "SDL_syswm.h" "SDL_timer.h" )
"SDL.h" "SDL_syswm.h" "SDL_timer.h" "SDL_render.h" )
( var g-window-width int 1080 )
( var g-window-height int 2340 )
;; TODO: Paths are relative to gamelib, including when running, unless we change our working dir
( 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 ) )
( defun-local sdl-intialize-2d-renderer ( renderer-out ( * ( * SDL_Renderer ) ) window ( * SDL_Window )
&return bool )
( var num-render-drivers int ( SDL_GetNumRenderDrivers ) )
( unless num-render-drivers
( return false ) )
( var i int 0 )
( while ( < i num-render-drivers )
( var driver-info SDL_RendererInfo ( array 0 ) )
( unless ( = 0 ( SDL_GetRenderDriverInfo i ( addr driver-info ) ) )
( return false ) )
( printf " Renderer [%d]: %s\n
\tHardware accelerated: %s\n
\tRender to texture: %s\n
\tMax texture width: %d\n
\tMax texture height: %d\n
\n "
i ( field driver-info name )
( ? ( bit-and ( field driver-info flags ) SDL_RENDERER_ACCELERATED ) "yes" "no" )
( ? ( bit-and ( field driver-info flags ) SDL_RENDERER_TARGETTEXTURE ) "yes" "no" )
( field driver-info max_texture_width )
( field driver-info max_texture_height ) )
( incr i ) )
( var macoy-beast-driver ( const int ) 0 )
( var selected-renderer int macoy-beast-driver )
( printf "Using renderer %d\n" selected-renderer )
( set ( deref renderer-out ) ( SDL_CreateRenderer window selected-renderer SDL_RENDERER_ACCELERATED ) )
( unless ( deref renderer-out )
( sdl-print-error )
( return false ) )
( return true ) )
( defun main ( &return int )
( printf " Kitty Gridlock\n
Created by Macoy Madson <macoy@macoy.me>.\n
https://macoy.me/code/macoy/kitty-gridlock\n
Copyright ( c ) 2021 Macoy Madson.\n
Licensed under GPL-3.0-or-later.\n " )
Licensed under GPL-3.0-or-later.\n
Rush Hour database from Michael Fogleman.\n\n " )
;;
;; Initialization
;;
( var window ( * SDL_Window ) null )
( unless ( sdl-initialize-for-2d ( addr window ) "Kitty Gridlock"
g-window-width g-window-height ) ( return 1 ) )
g-window-width g-window-height )
( return 1 ) )
( var renderer ( * SDL_Renderer ) null )
( unless ( sdl-intialize-2d-renderer ( addr renderer ) window ) ( return 1 ) )
( var background-surface ( * SDL_Surface ) ( SDL_LoadBMP
( in-data-dir "Board.bmp" ) ) )
( unless background-surface
( printf "Failed to load surface. Is the data directory in the right location?\n" )
( sdl-print-error )
( return 1 ) )
( var background-texture ( * SDL_Texture )
( SDL_CreateTextureFromSurface renderer background-surface ) )
( unless background-texture ( sdl-print-error ) ( return 1 ) )
;; No need to hold on to surface after texture has been created
( SDL_FreeSurface background-surface )
;;
;; Game loop
;;
( var exit-reason ( * ( const char ) ) null )
( while ( not exit-reason )
( var event SDL_Event )
@ -34,8 +97,16 @@ Licensed under GPL-3.0-or-later.\n")
( var currentKeyStates ( * ( const Uint8 ) ) ( SDL_GetKeyboardState null ) )
( when ( at SDL_SCANCODE_ESCAPE currentKeyStates )
( set exit-reason "Escape pressed" ) )
( SDL_UpdateWindowSurface window ) )
( SDL_RenderClear renderer )
( unless ( = 0 ( SDL_RenderCopy renderer background-texture null null ) )
( sdl-print-error )
( return 1 ) )
( SDL_RenderPresent renderer ) )
;;
;; Shutdown
;;
( when exit-reason
( printf "Exiting. Reason: %s\n" exit-reason ) )
@ -43,3 +114,26 @@ Licensed under GPL-3.0-or-later.\n")
( return 0 ) )
( module-use-sdl-build-options )
( defgenerator define-constant ( define-name symbol value any )
( var define-statement ( const ( [] CStatementOperation ) )
( array
( array Keyword "#define" -1 )
( array Expression null 1 )
( array Keyword " " -1 )
( array Expression null 2 )
( array Keyword "\n" -1 ) ) )
( return ( CStatementOutput environment context tokens startTokenIndex
define-statement ( array-size define-statement )
output ) ) )
;; Necessary to create e.g. in C PREFIX "_my_thing"
( defgenerator static-string-combine ( string-A any string-B any )
( var statement ( const ( [] CStatementOperation ) )
( array
( array Expression null 1 )
( array Keyword " " -1 )
( array Expression null 2 ) ) )
( return ( CStatementOutput environment context tokens startTokenIndex
statement ( array-size statement )
output ) ) )