Browse Source

Moved several macros into Cakelisp

* Any macros which belonged in cakelisp's CHelpers instead of gamelib
are now there
* Reformatted some files
windows-imgui
Macoy Madson 9 months ago
parent
commit
c9972339f2
  1. 86
      src/Math.cake
  2. 155
      src/Ogre.cake
  3. 65
      src/OgreInitialize.cake

86
src/Math.cake

@ -9,7 +9,7 @@
(set-cakelisp-option cakelisp-src-dir "Dependencies/cakelisp/src")
(import &comptime-only "Dependencies.cake")
(import &comptime-only "Dependencies.cake" "CHelpers.cake")
(c-import "<stdio.h>")
@ -18,7 +18,7 @@
;; TODO: Remove once importing Math.cake no longer changes all compile commands
(add-build-config-label "HandmadeMath")
(c-define-local HANDMADE_MATH_IMPLEMENTATION)
(c-preprocessor-define HANDMADE_MATH_IMPLEMENTATION)
(c-import &with-decls "HandmadeMath.h"
;; Must also include in source file so HANDMADE_MATH_IMPLEMENTATION is output here
&with-defs "HandmadeMath.h")
@ -232,88 +232,6 @@
(def-type-alias quaternion hmm_quaternion)
;;
;; Macros/helpers (TODO move)
;;
(defgenerator c-define-local (define-name symbol)
(var define-statement (const ([] CStatementOperation))
(array
(array Keyword "#define" -1)
(array Expression null 1)
;; Bit of a hack
(array Keyword "\n" -1)))
(return (CStatementOutput environment context tokens startTokenIndex
define-statement (array-size define-statement)
output)))
;; When encountering references of (alias), output C function invocation underlyingFuncName()
;; Note that this circumvents the reference system in order to reduce compile-time cost of using
;; aliased functions. This will probably have to be fixed eventually
(defgenerator output-aliased-c-function-invocation (&optional &rest arguments any)
(var invocation-name (& (const std::string)) (field (at (+ 1 startTokenIndex) tokens) contents))
;; TODO Hack: If I was referenced directly, don't do anything, because it's only for dependencies
(when (= 0 (call-on compare invocation-name
"output-aliased-c-function-invocation"))
(return true))
(get-or-create-comptime-var c-function-aliases (<> std::unordered_map std::string std::string))
(def-type-alias FunctionAliasMap (<> std::unordered_map std::string std::string))
(var alias-func-pair (in FunctionAliasMap iterator)
(call-on-ptr find c-function-aliases invocation-name))
(unless (!= alias-func-pair (call-on-ptr end c-function-aliases))
(ErrorAtToken (at (+ 1 startTokenIndex) tokens)
"unknown function alias. This is likely a code error, as it should never have " \
"gotten this far")
(return false))
(var underlying-func-name (& (const std::string)) (path alias-func-pair > second))
;; (Logf "found %s, outputting %s\n" (call-on c_str invocation-name) (call-on c_str underlying-func-name))
(if arguments
(scope
(var invocation-statement (const ([] CStatementOperation))
(array
(array KeywordNoSpace (call-on c_str underlying-func-name) -1)
(array OpenParen null -1)
(array ExpressionList null 1)
(array CloseParen null -1)
(array SmartEndStatement null -1)))
(return (CStatementOutput environment context tokens startTokenIndex
invocation-statement (array-size invocation-statement)
output)))
(scope
(var invocation-statement (const ([] CStatementOperation))
(array
(array KeywordNoSpace (call-on c_str underlying-func-name) -1)
(array OpenParen null -1)
(array CloseParen null -1)
(array SmartEndStatement null -1)))
(return (CStatementOutput environment context tokens startTokenIndex
invocation-statement (array-size invocation-statement)
output)))))
;; When encountering references of (alias), output C function invocation underlyingFuncName()
;; output-aliased-c-function-invocation actually does the work
(defgenerator def-c-function-alias (alias (ref symbol) underlying-func-name (ref symbol))
;; TODO Hack: Invoke this to create a dependency on it, so by the time we make the
;; alias, we can set the generators table to it
(output-aliased-c-function-invocation)
(get-or-create-comptime-var c-function-aliases (<> std::unordered_map std::string std::string))
(set (at (field alias contents) (deref c-function-aliases)) (field underlying-func-name contents))
;; (Logf "aliasing %s to %s\n" (call-on c_str (field underlying-func-name contents))
;; (call-on c_str (field alias contents)))
;; Upen encountering an invocation of our alias, run the aliased function output
;; In case the function already has references, resolve them now. Future invocations will be
;; handled immediately (because it'll be in the generators list)
(var evaluated-success bool
(registerEvaluateGenerator environment (call-on c_str (field alias contents))
(at "output-aliased-c-function-invocation" (field environment generators))))
(return evaluated-success))
;;
;; Testing
;;

155
src/Ogre.cake

@ -67,9 +67,9 @@
;; Also notice the HBU_STATIC flag; since the HBU_WRITE_ONLY
;; bit would prohibit us from reading the data for importing.
(var mesh-v1 Ogre::v1::MeshPtr
(call-on load (call (in Ogre v1 MeshManager getSingleton))
name Ogre::ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME
Ogre::v1::HardwareBuffer::HBU_STATIC Ogre::v1::HardwareBuffer::HBU_STATIC))
(call-on load (call (in Ogre v1 MeshManager getSingleton))
name Ogre::ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME
Ogre::v1::HardwareBuffer::HBU_STATIC Ogre::v1::HardwareBuffer::HBU_STATIC))
(var half-position bool true)
(var half-UVs bool true)
@ -79,10 +79,10 @@
(var mesh-name-v2 (const Ogre::String) (+ name (Ogre::String " Imported")))
;; Import the v1 mesh to v2
(var v2Mesh Ogre::MeshPtr
(call-on createByImportingV1 (call (in Ogre MeshManager getSingleton))
mesh-name-v2 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME
(call-on get mesh-v1)
half-position half-UVs use-Q-tangents))
(call-on createByImportingV1 (call (in Ogre MeshManager getSingleton))
mesh-name-v2 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME
(call-on get mesh-v1)
half-position half-UVs use-Q-tangents))
;; We don't need the v1 mesh. Free CPU memory, get it out of the GPU.
;; Leave it loaded if you want to use athene with v1 Entity.
@ -104,8 +104,8 @@
(var scene-manager (* (in Ogre SceneManager)) (ogre-get-scene-manager))
(var v2Mesh Ogre::MeshPtr
(call-on load (call (in Ogre MeshManager getSingleton))
name Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME))
(call-on load (call (in Ogre MeshManager getSingleton))
name Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME))
;; Create an Item with the model we just imported.
;; Notice we use the name of the imported model. We could also use the overload
@ -137,11 +137,11 @@
animation-name (* (const char))
&return OgreAnimationHandle)
(var skeleton (* (in Ogre SkeletonInstance))
(call-on-ptr getSkeletonInstance (field mesh mesh-item)))
(call-on-ptr getSkeletonInstance (field mesh mesh-item)))
(call-on-ptr addAnimationsFromSkeleton skeleton skeleton-name
(in Ogre ResourceGroupManager AUTODETECT_RESOURCE_GROUP_NAME))
(var animation (* (in Ogre SkeletonAnimation))
(call-on-ptr getAnimation skeleton animation-name))
(call-on-ptr getAnimation skeleton animation-name))
(call-on-ptr setEnabled animation true)
(return animation))
@ -179,7 +179,7 @@
(defun ogre-create-particle-system (&return particle-system-handle)
(var scene-manager (* (in Ogre SceneManager)) (ogre-get-scene-manager))
(var particle-system (* (in Ogre ParticleSystem))
(call-on-ptr createParticleSystem scene-manager "TestParticles"))
(call-on-ptr createParticleSystem scene-manager "TestParticles"))
;; Not so good because it requires accessing things in ParticleFX, which is a problem when dynamically linking
;; Use StringInterface::setParameter to set things up without their types
;; (scope ;; Set up particle system
@ -214,31 +214,12 @@
;; TODO Fragile: we don't have guarantees this is the right attachment
;; TODO: Why doesn't this work?
(var particle-system (* (in Ogre ParticleSystem))
(path particle-handle > particle-system))
(path particle-handle > particle-system))
(when (> (call-on-ptr getNumEmitters particle-system) 0)
(var particle-emitter (* (in Ogre ParticleEmitter))
(call-on-ptr getEmitter particle-system 0))
(call-on-ptr getEmitter particle-system 0))
(call-on-ptr setPosition particle-emitter (array x y z))))
(defmacro var-cast-to (var-name symbol type any expression-to-cast any)
(tokenize-push output
(var (token-splice var-name) (token-splice type)
(type-cast (token-splice expression-to-cast)
(token-splice type))))
(return true))
;; e.g. (negate 1) outputs (-1)
(defgenerator negate (to-negate (index any))
(var negate-statement (const ([] CStatementOperation))
(array
(array OpenParen null -1)
(array Keyword "-" -1)
(array Expression null 1)
(array CloseParen null -1)))
(return (CStatementOutput environment context tokens startTokenIndex
negate-statement (array-size negate-statement)
output)))
;;
;; Building
;;
@ -267,29 +248,29 @@ Note that you can also build Ogre manually. This can be useful if you are portin
(Log "Ogre: Building via CMake and Make\n")
(var ogre-dependencies-dir-output (* (const char))
"Dependencies/ogre-next-deps/build/ogredeps")
"Dependencies/ogre-next-deps/build/ogredeps")
(unless (fileExists ogre-dependencies-dir-output) ;; Build dependencies
(var ogre-dependencies-dir (* (const char)) "Dependencies/ogre-next-deps/build")
(makeDirectory ogre-dependencies-dir)
(run-process-sequential-or
("cmake"
"-G" "Ninja" ".."
:in-directory ogre-dependencies-dir)
(build-ogre-on-failure "failed at Ogre dependencies. This tool requires CMake.")
(return false))
("cmake"
"-G" "Ninja" ".."
:in-directory ogre-dependencies-dir)
(build-ogre-on-failure "failed at Ogre dependencies. This tool requires CMake.")
(return false))
(run-process-sequential-or
("ninja"
:in-directory ogre-dependencies-dir)
(build-ogre-on-failure "failed at Ogre dependencies Ninja build. This tool requires Ninja.")
(return false))
("ninja"
:in-directory ogre-dependencies-dir)
(build-ogre-on-failure "failed at Ogre dependencies Ninja build. This tool requires Ninja.")
(return false))
(run-process-sequential-or
("ninja" "install"
:in-directory ogre-dependencies-dir)
(build-ogre-on-failure "failed at Ogre dependencies Ninja install. This tool requires Ninja.")
(return false))
("ninja" "install"
:in-directory ogre-dependencies-dir)
(build-ogre-on-failure "failed at Ogre dependencies Ninja install. This tool requires Ninja.")
(return false))
(unless (fileExists ogre-dependencies-dir-output)
(Logf "error: Expected Ogre dependencies to be in %s, but didn't find them\n"
@ -298,13 +279,13 @@ Note that you can also build Ogre manually. This can be useful if you are portin
(var absolute-softlink-file ([] MAX_PATH_LENGTH char) (array 0))
(scope (var absolute-softlink-path (* (const char))
(makeAbsolutePath_Allocated null "Dependencies/ogre-next"))
(makeAbsolutePath_Allocated null "Dependencies/ogre-next"))
(PrintfBuffer absolute-softlink-file "%s/Dependencies" absolute-softlink-path)
(free (type-cast absolute-softlink-path (* void))))
(unless (fileExists absolute-softlink-file)
;; TODO: Use symlink() instead
(var absolute-dependencies-path (* (const char))
(makeAbsolutePath_Allocated null ogre-dependencies-dir-output))
(makeAbsolutePath_Allocated null ogre-dependencies-dir-output))
;; Note that this needs to be absolute because nested Dependencies folders can cause recursive
;; symlinks, which can happen unless the path is unambiguous
(Logf "Please run the following commands, then re-run the current command
@ -326,51 +307,51 @@ ln -s %s %s\n"
(scope ;; Debug
(run-process-sequential-or
("cmake"
"-D" "OGRE_USE_BOOST=0"
"-D" "OGRE_CONFIG_THREAD_PROVIDER=0"
"-D" "OGRE_CONFIG_THREADS=0"
"-D" "OGRE_BUILD_COMPONENT_SCENE_FORMAT=1"
"-D" "OGRE_BUILD_SAMPLES2=1"
"-D" "OGRE_BUILD_TESTS=1"
;; NOTE: You must not use quotes for this command. It needs to be
;; in one argument. This is because the shell interprets quotes a
;; certain way, which exec() does not
"-D" "CMAKE_BUILD_TYPE=Debug"
"-G" "Ninja"
"../.."
:in-directory ogre-debug-output-dir)
(build-ogre-on-failure "failed at Ogre cmake. This tool requires CMake.")
(return false))
("cmake"
"-D" "OGRE_USE_BOOST=0"
"-D" "OGRE_CONFIG_THREAD_PROVIDER=0"
"-D" "OGRE_CONFIG_THREADS=0"
"-D" "OGRE_BUILD_COMPONENT_SCENE_FORMAT=1"
"-D" "OGRE_BUILD_SAMPLES2=1"
"-D" "OGRE_BUILD_TESTS=1"
;; NOTE: You must not use quotes for this command. It needs to be
;; in one argument. This is because the shell interprets quotes a
;; certain way, which exec() does not
"-D" "CMAKE_BUILD_TYPE=Debug"
"-G" "Ninja"
"../.."
:in-directory ogre-debug-output-dir)
(build-ogre-on-failure "failed at Ogre cmake. This tool requires CMake.")
(return false))
(run-process-sequential-or
("ninja" "--verbose"
:in-directory ogre-debug-output-dir)
(build-ogre-on-failure "failed at Ogre ninja. This tool requires Ninja.")
(return false)))
("ninja" "--verbose"
:in-directory ogre-debug-output-dir)
(build-ogre-on-failure "failed at Ogre ninja. This tool requires Ninja.")
(return false)))
(scope ;; Release
(run-process-sequential-or
("cmake"
"-D" "OGRE_USE_BOOST=0"
"-D" "OGRE_CONFIG_THREAD_PROVIDER=0"
"-D" "OGRE_CONFIG_THREADS=0"
"-D" "OGRE_BUILD_COMPONENT_SCENE_FORMAT=1"
"-D" "OGRE_BUILD_SAMPLES2=1"
"-D" "OGRE_BUILD_TESTS=1"
"-D" "OGRE_DEBUG_MODE=0"
"-D" "CMAKE_BUILD_TYPE=Release"
"-G" "Ninja"
"../.."
:in-directory ogre-release-output-dir)
(build-ogre-on-failure "failed at Ogre cmake. This tool requires CMake.")
(return false))
("cmake"
"-D" "OGRE_USE_BOOST=0"
"-D" "OGRE_CONFIG_THREAD_PROVIDER=0"
"-D" "OGRE_CONFIG_THREADS=0"
"-D" "OGRE_BUILD_COMPONENT_SCENE_FORMAT=1"
"-D" "OGRE_BUILD_SAMPLES2=1"
"-D" "OGRE_BUILD_TESTS=1"
"-D" "OGRE_DEBUG_MODE=0"
"-D" "CMAKE_BUILD_TYPE=Release"
"-G" "Ninja"
"../.."
:in-directory ogre-release-output-dir)
(build-ogre-on-failure "failed at Ogre cmake. This tool requires CMake.")
(return false))
(run-process-sequential-or
("ninja"
:in-directory ogre-release-output-dir)
(build-ogre-on-failure "failed at Ogre ninja. This tool requires Ninja.")
(return false)))
("ninja"
:in-directory ogre-release-output-dir)
(build-ogre-on-failure "failed at Ogre ninja. This tool requires Ninja.")
(return false)))
;; One final to check to ensure everything's good to go
(unless (and (fileExists "Dependencies/ogre-next/build/Debug/lib/libOgreMain_d.so")

65
src/OgreInitialize.cake

@ -1,7 +1,7 @@
;; Most of this file comes from the Ogre 2 basic project. It's customized now. Once I get more
;; familiar with Ogre, I'll probably try to move this file into Ogre.cake or something, which
;; currently acts as my "custom code"/integration file
(import &comptime-only "CHelpers.cake")
(import &comptime-only "CHelpers.cake" "CppHelpers.cake")
(c-import
"OgreArchiveManager.h"
@ -54,7 +54,7 @@
(call-on load config (+ resourcePath "resources2.cfg"))
(var rootHlmsFolder (in Ogre String)
(+ resourcePath (call-on getSetting config "DoNotUseAsResource" "Hlms" "")))
(+ resourcePath (call-on getSetting config "DoNotUseAsResource" "Hlms" "")))
(cond ((call-on empty rootHlmsFolder)
(set rootHlmsFolder "./"))
@ -78,14 +78,14 @@
;; Get the path to all the subdirectories used by HlmsUnlit
(call (in Ogre HlmsUnlit getDefaultPaths) mainFolderPath libraryFoldersPaths)
(var archiveUnlit (* (in Ogre Archive))
(call-on load archiveManager (+ rootHlmsFolder mainFolderPath) "FileSystem" true))
(call-on load archiveManager (+ rootHlmsFolder mainFolderPath) "FileSystem" true))
(var archiveUnlitLibraryFolders (in Ogre ArchiveVec))
(set libraryFolderPathIt (call-on begin libraryFoldersPaths))
(set libraryFolderPathEn (call-on end libraryFoldersPaths))
(while (!= libraryFolderPathIt libraryFolderPathEn)
(var archiveLibrary (* (in Ogre Archive))
(call-on load archiveManager
(+ rootHlmsFolder (deref libraryFolderPathIt)) "FileSystem" true))
(call-on load archiveManager
(+ rootHlmsFolder (deref libraryFolderPathIt)) "FileSystem" true))
(call-on push_back archiveUnlitLibraryFolders archiveLibrary)
(incr libraryFolderPathIt))
@ -98,8 +98,8 @@
;; Do the same for HlmsPbs:
(call (in Ogre HlmsPbs getDefaultPaths) mainFolderPath libraryFoldersPaths)
(var archivePbs (* (in Ogre Archive))
(call-on load archiveManager
(+ rootHlmsFolder mainFolderPath) "FileSystem" true))
(call-on load archiveManager
(+ rootHlmsFolder mainFolderPath) "FileSystem" true))
;; Get the library archive(s)
(var archivePbsLibraryFolders (in Ogre ArchiveVec))
@ -107,8 +107,8 @@
(set libraryFolderPathEn (call-on end libraryFoldersPaths))
(while (!= libraryFolderPathIt libraryFolderPathEn)
(var archiveLibrary (* (in Ogre Archive))
(call-on load archiveManager
(+ rootHlmsFolder (deref libraryFolderPathIt)) "FileSystem" true))
(call-on load archiveManager
(+ rootHlmsFolder (deref libraryFolderPathIt)) "FileSystem" true))
(call-on push_back archivePbsLibraryFolders archiveLibrary)
(incr libraryFolderPathIt))
@ -118,7 +118,7 @@
(call-on-ptr registerHlms (call-on getHlmsManager (call (in Ogre Root getSingleton))) hlmsPbs))
(var renderSystem (* (in Ogre RenderSystem))
(call-on-ptr getRenderSystem (call (in Ogre Root getSingletonPtr))))
(call-on-ptr getRenderSystem (call (in Ogre Root getSingletonPtr))))
(when (= (call-on-ptr getName renderSystem) "Direct3D11 Rendering Subsystem")
;; Set lower limits 512kb instead of the default 4MB per Hlms in D3D 11.0
;; and below to avoid saturating AMD's discard limit (8MB) or
@ -145,8 +145,8 @@
;; #endif
(set g-ogre-root (ogre-new (call (in Ogre Root) (+ pluginsFolder pluginsFile)
(+ writeAccessFolder "ogre.cfg")
(+ writeAccessFolder "Ogre.log"))))
(+ writeAccessFolder "ogre.cfg")
(+ writeAccessFolder "Ogre.log"))))
;; This allows the user to configure the graphics. It's damn annoying during dev though
;; TODO: Make this return false and quit the app
@ -155,7 +155,7 @@
;; Initialize Root
(var renderSystem (* (in Ogre RenderSystem))
(call-on-ptr getRenderSystemByName g-ogre-root "OpenGL 3+ Rendering Subsystem"))
(call-on-ptr getRenderSystemByName g-ogre-root "OpenGL 3+ Rendering Subsystem"))
(unless renderSystem
(printf "Render system not found!\n")
(return false))
@ -167,8 +167,8 @@
(call-on-ptr setConfigOption renderSystem "sRGB Gamma Conversion" "Yes")
(call-on-ptr setRenderSystem g-ogre-root renderSystem)
(set g-ogre-window (call-on-ptr initialise g-ogre-root
(not useCurrentWindow) ;; autoCreateWindow
"GameLib"))
(not useCurrentWindow) ;; autoCreateWindow
"GameLib"))
;; Use the already existing window
;; See http:;;wiki.ogre3d.org/Using+SDL+Input (that actually didn't work 100%)
@ -181,8 +181,8 @@
(var winWidth int 1920)
(var winHeight int 1080)
(set g-ogre-window (call-on-ptr createRenderWindow g-ogre-root "GameLib" winWidth winHeight
false ;; Fullscreen
(addr windowSettings)))
false ;; Fullscreen
(addr windowSettings)))
;; renderWindow->setVisible(true);
)
@ -191,7 +191,7 @@
;; Create SceneManager
(var numThreads (const size_t) 1u)
(set g-scene-manager (call-on-ptr createSceneManager g-ogre-root
(in Ogre ST_GENERIC) numThreads "SceneManager"))
(in Ogre ST_GENERIC) numThreads "SceneManager"))
;; Create & setup camera
(var camera (* (in Ogre Camera)) (call-on-ptr createCamera g-scene-manager "Main Camera"))
@ -206,7 +206,7 @@
;; Setup a basic compositor with a blue clear colour
(var compositorManager (* (in Ogre CompositorManager2))
(call-on-ptr getCompositorManager2 g-ogre-root))
(call-on-ptr getCompositorManager2 g-ogre-root))
(var workspaceName (const (in Ogre String)) "Main Workspace")
;; const IdString definitionNameId = workspaceName;
;; TODO: This needs initializer parameters
@ -217,7 +217,7 @@
g-scene-manager (call-on-ptr getTexture g-ogre-window) camera workspaceName true)
(var resource-group-manager (& (in Ogre ResourceGroupManager))
(call (in Ogre ResourceGroupManager getSingleton)))
(call (in Ogre ResourceGroupManager getSingleton)))
(call-on addResourceLocation resource-group-manager
"data/Models" "FileSystem" "Models")
(call-on addResourceLocation resource-group-manager
@ -241,14 +241,14 @@
(call-on loadResourceGroup resource-group-manager "Materials"))
;; (call (in Ogre WindowEventUtilities addWindowEventListener)
;; g-ogre-window (addr g_myWindowEventListener))
;; g-ogre-window (addr g_myWindowEventListener))
(set g-graphics-intialized true)
(return true))
(defun ogre-shutdown-internal ()
;; (call (in Ogre WindowEventUtilities removeWindowEventListener)
;; g-ogre-window (addr g_myWindowEventListener))
;; g-ogre-window (addr g_myWindowEventListener))
(OGRE_DELETE g-ogre-root)
(set g-ogre-root null))
@ -267,27 +267,6 @@
(return false))
(return true))
;; Given
;; (var-construct name type constructor-arguments)
;; Output
;; type name(constructor-arguments);
(defgenerator var-construct (name (index symbol) type (index any)
&rest constructor-arguments (index any))
;; TODO: Make version of this that uses the absolute token index rather than re-getting the args
;; Right now, we're not really getting the benefits of using generator arguments
(var constructor-var-statement (const ([] CStatementOperation))
(array
(array TypeNoArray null 2) ;; type
(array Keyword " " -1)
(array Expression null 1) ;; name
(array OpenParen null -1)
(array ExpressionList null 3) ;; constructor-arguments
(array CloseParen null -1)
(array SmartEndStatement null -1)))
(return (CStatementOutput environment context tokens startTokenIndex
constructor-var-statement (array-size constructor-var-statement)
output)))
;;
;; Building
;;

Loading…
Cancel
Save