GameLib is a collection of libraries for creating applications in Cakelisp.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

288 lines
12 KiB

;; 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 "CHelpers.cake" "CppHelpers.cake")
(c-import
"OgreArchiveManager.h"
"OgreCamera.h"
"OgreConfigFile.h"
"OgreRoot.h"
"OgreWindow.h"
"OgreHlmsManager.h"
"OgreHlmsPbs.h"
"OgreHlmsUnlit.h"
"OgreItem.h"
"OgreMesh.h"
"OgreMesh2.h"
"OgreMeshManager.h"
"OgreMeshManager2.h"
"Compositor/OgreCompositorManager2.h"
"OgreWindowEventUtilities.h")
(forward-declare
(namespace Ogre
(class Root)
(class SceneManager)))
(var-global g-ogre-root (* (in Ogre Root)) null)
(var g-scene-manager (* (in Ogre SceneManager)) null)
(var g-ogre-window (* (in Ogre Window)) null)
(var g-graphics-intialized bool false)
(var-global g-ogre-window-should-quit bool false)
;; TODO: Add advanced class definition
;; class MyWindowEventListener : public Ogre::WindowEventListener
;; {
;; public:
;; virtual void windowClosed(Ogre::Window* window)
;; {
;; g-ogre-window-should-quit = true;
;; }
;; };
;; (var g_myWindowEventListener MyWindowEventListener)
(defun-local registerHlms ()
(var resourcePath (in Ogre String) "data/")
(var config (in Ogre ConfigFile))
(call-on load config (+ resourcePath "resources2.cfg"))
(var rootHlmsFolder (in Ogre String)
(+ resourcePath (call-on getSetting config "DoNotUseAsResource" "Hlms" "")))
(cond ((call-on empty rootHlmsFolder)
(set rootHlmsFolder "./"))
((!= (deref (- (call-on end rootHlmsFolder) 1)) '/')
(set rootHlmsFolder (+ rootHlmsFolder "/"))))
;; At this point rootHlmsFolder should be a valid path to the Hlms data folder
(var hlmsUnlit (* (in Ogre HlmsUnlit)) null)
(var hlmsPbs (* (in Ogre HlmsPbs)) null)
;; For retrieval of the paths to the different folders needed
(var mainFolderPath (in Ogre String))
(var libraryFoldersPaths (in Ogre StringVector))
(var libraryFolderPathIt (in Ogre StringVector const_iterator))
(var libraryFolderPathEn (in Ogre StringVector const_iterator))
(var archiveManager (& (in Ogre ArchiveManager)) (call (in Ogre ArchiveManager getSingleton)))
(scope ;; Create & Register HlmsUnlit
;; 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))
(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 push_back archiveUnlitLibraryFolders archiveLibrary)
(incr libraryFolderPathIt))
;; Create and register the unlit Hlms
(set hlmsUnlit (ogre-new (call (type (in Ogre HlmsUnlit)) archiveUnlit
(addr archiveUnlitLibraryFolders))))
(call-on-ptr registerHlms (call-on getHlmsManager (call (in Ogre Root getSingleton))) hlmsUnlit))
(scope ;; Create & Register HlmsPbs
;; 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))
;; Get the library archive(s)
(var archivePbsLibraryFolders (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 push_back archivePbsLibraryFolders archiveLibrary)
(incr libraryFolderPathIt))
;; Create and register
(set hlmsPbs (ogre-new (call (type (in Ogre HlmsPbs)) archivePbs
(addr archivePbsLibraryFolders))))
(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))))
(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
;; saturate the PCIE bus in some low end machines.
(var supportsNoOverwriteOnTextureBuffers bool)
(call-on-ptr getCustomAttribute renderSystem "MapNoOverwriteOnDynamicBufferSRV"
(addr supportsNoOverwriteOnTextureBuffers))
(unless supportsNoOverwriteOnTextureBuffers
(call-on-ptr setTextureBufferDefaultSize hlmsPbs (* 512 1024))
(call-on-ptr setTextureBufferDefaultSize hlmsUnlit (* 512 1024)))))
(defun ogre-initialize-internal (useCurrentWindow bool &return bool)
(var pluginsFolder (const (in Ogre String)) "./data/")
(var writeAccessFolder (const (in Ogre String)) "./output/")
;; TODO: Support preprocessor?
;; #ifndef OGRE_STATIC_LIB
;; #if OGRE_DEBUG_MODE
(var pluginsFile (* (const char)) "plugins_d.cfg")
;; #else
;; const char* pluginsFile = "plugins.cfg";
;; #endif
;; #endif
(set g-ogre-root (ogre-new (call (in Ogre Root) (+ pluginsFolder pluginsFile)
(+ 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
;; if (!g-ogre-root->showConfigDialog())
;; return false;
;; Initialize Root
(var renderSystem (* (in Ogre RenderSystem))
(call-on-ptr getRenderSystemByName g-ogre-root "OpenGL 3+ Rendering Subsystem"))
(unless renderSystem
(fprintf stderr "Render system not found!\n")
(return false))
;; renderSystem->setConfigOption("Display Frequency", "N/A");
(call-on-ptr setConfigOption renderSystem "Full Screen" "No")
;; renderSystem->setConfigOption("VSync", "Yes");
(call-on-ptr setConfigOption renderSystem "Video Mode" "1920 x 1080")
(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"))
;; Use the already existing window
;; See http:;;wiki.ogre3d.org/Using+SDL+Input (that actually didn't work 100%)
(when useCurrentWindow
(var windowSettings (in Ogre NameValuePairList))
;; TODO: There is a scary comment about using this option in GLXWindow::create(). It may
;; cause problems later. The documentation for this stuff is sorely lacking
(set (at "currentGLDrawable" windowSettings) (call (type (in Ogre String)) "True"))
(set (at "currentGLContext" windowSettings) (call (type (in Ogre String)) "True"))
(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)))
;; renderWindow->setVisible(true);
)
(registerHlms)
;; 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"))
;; Create & setup camera
(var camera (* (in Ogre Camera)) (call-on-ptr createCamera g-scene-manager "Main Camera"))
;; Position it at 500 in Z direction
(call-on-ptr setPosition camera (call (type (in Ogre Vector3)) 0 5 15))
;; Look back along -Z
(call-on-ptr lookAt camera (call (type (in Ogre Vector3)) 0 0 0))
(call-on-ptr setNearClipDistance camera 0.2f)
(call-on-ptr setFarClipDistance camera 1000.0f)
(call-on-ptr setAutoAspectRatio camera true)
;; Setup a basic compositor with a blue clear colour
(var compositorManager (* (in Ogre CompositorManager2))
(call-on-ptr getCompositorManager2 g-ogre-root))
(var workspaceName (const (in Ogre String)) "Main Workspace")
;; const IdString definitionNameId = workspaceName;
;; TODO: This needs initializer parameters
(var-construct backgroundColour (const (in Ogre ColourValue)) 0.0302f 0.03f 0.03f)
(call-on-ptr createBasicWorkspaceDef compositorManager
workspaceName backgroundColour (call (in Ogre IdString)))
(call-on-ptr addWorkspace compositorManager
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-on addResourceLocation resource-group-manager
"data/Models" "FileSystem" "Models")
(call-on addResourceLocation resource-group-manager
"data/Materials/Textures" "FileSystem" "Textures")
(scope ;; Particles
(call-on addResourceLocation resource-group-manager
"data/Particles" "FileSystem" "Particles")
(call-on initialiseResourceGroup resource-group-manager "Particles"
true) ;; changeLocaleTemporarily: See comment above initialiseResourceGroup
(call-on loadResourceGroup resource-group-manager "Particles"))
(scope ;; Materials
;; I had to read https://forums.ogre3d.org/viewtopic.php?f=5&t=94769 before figuring out
;; groups needed to be initialized and loaded in order to work (at least, materials do)
(call-on createResourceGroup resource-group-manager "Materials")
(call-on addResourceLocation resource-group-manager
"data/Materials" "FileSystem" "Materials")
(call-on initialiseResourceGroup resource-group-manager "Materials"
true) ;; changeLocaleTemporarily: See comment above initialiseResourceGroup
(call-on loadResourceGroup resource-group-manager "Materials"))
;; (call (in Ogre WindowEventUtilities addWindowEventListener)
;; 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))
(OGRE_DELETE g-ogre-root)
(set g-ogre-root null))
(defun ogre-get-scene-manager (&return (* (in Ogre SceneManager)))
(return g-scene-manager))
(defgenerator ogre-new (expression-index (index any))
(addStringOutput (field output source) "OGRE_NEW" StringOutMod_SpaceAfter
(addr (at startTokenIndex tokens)))
(var expression-context EvaluatorContext context)
(set (field expression-context scope) EvaluatorScope_ExpressionsOnly)
(unless (= (EvaluateGenerate_Recursive environment expression-context
tokens expression-index output)
0)
(return false))
(return true))
;;
;; Building
;;
(defmacro use-ogre-build-options ()
(tokenize-push output
(add-build-options "-DOGRE_DEBUG_MODE=1"
;; Ignore an annoyingly long error in Ogre's code
"-Wno-gnu-inline-cpp-without-extern")
(add-c-search-directory-module
"Dependencies/ogre-next/OgreMain/include"
"Dependencies/ogre-next/Components/Hlms/Common/include"
"Dependencies/ogre-next/Components/Hlms/Pbs/include"
"Dependencies/ogre-next/Components/Hlms/Unlit/include"
"Dependencies/ogre-next/build/Debug/include"
"Dependencies/ogre-next/Components/Overlay/include"))
(return true))
(use-ogre-build-options)