GameLib is my library for making games
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.
 
 
 
 
 
 

306 lines
9.7 KiB

#include <stdio.h>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Woverloaded-virtual"
#include "OgreArchiveManager.h"
#include "OgreCamera.h"
#include "OgreConfigFile.h"
#include "OgreRoot.h"
#include "OgreWindow.h"
#include "OgreHlmsManager.h"
#include "OgreHlmsPbs.h"
#include "OgreHlmsUnlit.h"
#include "OgreItem.h"
#include "OgreMesh.h"
#include "OgreMesh2.h"
#include "OgreMeshManager.h"
#include "OgreMeshManager2.h"
#include "Compositor/OgreCompositorManager2.h"
#include "OgreWindowEventUtilities.h"
#pragma GCC diagnostic pop
Ogre::Root* g_ogreRoot = nullptr;
static Ogre::SceneManager* g_sceneManager = nullptr;
static Ogre::Window* g_window = nullptr;
bool g_graphicsIntialized = false;
bool g_ogreWindowShouldQuit = false;
class MyWindowEventListener : public Ogre::WindowEventListener
{
public:
virtual void windowClosed(Ogre::Window* window)
{
g_ogreWindowShouldQuit = true;
}
};
MyWindowEventListener g_myWindowEventListener;
static void registerHlms(void)
{
using namespace Ogre;
String resourcePath = "data/";
ConfigFile cf;
cf.load(resourcePath + "resources2.cfg");
String rootHlmsFolder = resourcePath + cf.getSetting("DoNotUseAsResource", "Hlms", "");
if (rootHlmsFolder.empty())
rootHlmsFolder = "./";
else if (*(rootHlmsFolder.end() - 1) != '/')
rootHlmsFolder += "/";
// At this point rootHlmsFolder should be a valid path to the Hlms data folder
HlmsUnlit* hlmsUnlit = 0;
HlmsPbs* hlmsPbs = 0;
// For retrieval of the paths to the different folders needed
String mainFolderPath;
StringVector libraryFoldersPaths;
StringVector::const_iterator libraryFolderPathIt;
StringVector::const_iterator libraryFolderPathEn;
ArchiveManager& archiveManager = ArchiveManager::getSingleton();
{
// Create & Register HlmsUnlit
// Get the path to all the subdirectories used by HlmsUnlit
HlmsUnlit::getDefaultPaths(mainFolderPath, libraryFoldersPaths);
Archive* archiveUnlit =
archiveManager.load(rootHlmsFolder + mainFolderPath, "FileSystem", true);
ArchiveVec archiveUnlitLibraryFolders;
libraryFolderPathIt = libraryFoldersPaths.begin();
libraryFolderPathEn = libraryFoldersPaths.end();
while (libraryFolderPathIt != libraryFolderPathEn)
{
Archive* archiveLibrary =
archiveManager.load(rootHlmsFolder + *libraryFolderPathIt, "FileSystem", true);
archiveUnlitLibraryFolders.push_back(archiveLibrary);
++libraryFolderPathIt;
}
// Create and register the unlit Hlms
hlmsUnlit = OGRE_NEW HlmsUnlit(archiveUnlit, &archiveUnlitLibraryFolders);
Root::getSingleton().getHlmsManager()->registerHlms(hlmsUnlit);
}
{
// Create & Register HlmsPbs
// Do the same for HlmsPbs:
HlmsPbs::getDefaultPaths(mainFolderPath, libraryFoldersPaths);
Archive* archivePbs =
archiveManager.load(rootHlmsFolder + mainFolderPath, "FileSystem", true);
// Get the library archive(s)
ArchiveVec archivePbsLibraryFolders;
libraryFolderPathIt = libraryFoldersPaths.begin();
libraryFolderPathEn = libraryFoldersPaths.end();
while (libraryFolderPathIt != libraryFolderPathEn)
{
Archive* archiveLibrary =
archiveManager.load(rootHlmsFolder + *libraryFolderPathIt, "FileSystem", true);
archivePbsLibraryFolders.push_back(archiveLibrary);
++libraryFolderPathIt;
}
// Create and register
hlmsPbs = OGRE_NEW HlmsPbs(archivePbs, &archivePbsLibraryFolders);
Root::getSingleton().getHlmsManager()->registerHlms(hlmsPbs);
}
RenderSystem* renderSystem = Root::getSingletonPtr()->getRenderSystem();
if (renderSystem->getName() == "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.
bool supportsNoOverwriteOnTextureBuffers;
renderSystem->getCustomAttribute("MapNoOverwriteOnDynamicBufferSRV",
&supportsNoOverwriteOnTextureBuffers);
if (!supportsNoOverwriteOnTextureBuffers)
{
hlmsPbs->setTextureBufferDefaultSize(512 * 1024);
hlmsUnlit->setTextureBufferDefaultSize(512 * 1024);
}
}
}
bool OgreInitialize()
{
using namespace Ogre;
const String pluginsFolder = "./data/";
const String writeAccessFolder = "./output/";
#ifndef OGRE_STATIC_LIB
#if OGRE_DEBUG_MODE
const char* pluginsFile = "plugins_d.cfg";
#else
const char* pluginsFile = "plugins.cfg";
#endif
#endif
g_ogreRoot = OGRE_NEW 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_ogreRoot->showConfigDialog())
// return false;
// Initialize Root
RenderSystem* renderSystem = g_ogreRoot->getRenderSystemByName("OpenGL 3+ Rendering Subsystem");
if (!renderSystem)
{
printf("Render system not found!\n");
return false;
}
// renderSystem->setConfigOption("Display Frequency", "N/A");
renderSystem->setConfigOption("Full Screen", "No");
// renderSystem->setConfigOption("VSync", "Yes");
renderSystem->setConfigOption("Video Mode", "1920 x 1080");
renderSystem->setConfigOption("sRGB Gamma Conversion", "Yes");
g_ogreRoot->setRenderSystem(renderSystem);
g_window = g_ogreRoot->initialise(/*autoCreateWindow=*/true, "GameLib");
registerHlms();
// Create SceneManager
const size_t numThreads = 1u;
g_sceneManager = g_ogreRoot->createSceneManager(ST_GENERIC, numThreads, "SceneManager");
// Create & setup camera
Camera* camera = g_sceneManager->createCamera("Main Camera");
// Position it at 500 in Z direction
camera->setPosition(Vector3(0, 5, 15));
// Look back along -Z
camera->lookAt(Vector3(0, 0, 0));
camera->setNearClipDistance(0.2f);
camera->setFarClipDistance(1000.0f);
camera->setAutoAspectRatio(true);
// Setup a basic compositor with a blue clear colour
CompositorManager2* compositorManager = g_ogreRoot->getCompositorManager2();
const String workspaceName("Main Workspace");
// const IdString definitionNameId = workspaceName;
const ColourValue backgroundColour(0.2f, 0.4f, 0.6f);
compositorManager->createBasicWorkspaceDef(workspaceName, backgroundColour, IdString());
compositorManager->addWorkspace(g_sceneManager, g_window->getTexture(), camera, workspaceName,
true);
Ogre::ResourceGroupManager::getSingleton().addResourceLocation("data/Models", "FileSystem",
"Models");
Ogre::WindowEventUtilities::addWindowEventListener(g_window, &g_myWindowEventListener);
g_graphicsIntialized = true;
return true;
}
bool OgreInitializeSDL()
{
using namespace Ogre;
const String pluginsFolder = "./data/";
const String writeAccessFolder = "./output/";
#ifndef OGRE_STATIC_LIB
#if OGRE_DEBUG_MODE
const char* pluginsFile = "plugins_d.cfg";
#else
const char* pluginsFile = "plugins.cfg";
#endif
#endif
g_ogreRoot = OGRE_NEW 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_ogreRoot->showConfigDialog())
// return false;
// Initialize Root
RenderSystem* renderSystem = g_ogreRoot->getRenderSystemByName("OpenGL 3+ Rendering Subsystem");
if (!renderSystem)
{
printf("Render system not found!\n");
return false;
}
// renderSystem->setConfigOption("Display Frequency", "N/A");
renderSystem->setConfigOption("Full Screen", "No");
// renderSystem->setConfigOption("VSync", "Yes");
renderSystem->setConfigOption("Video Mode", "1920 x 1080");
renderSystem->setConfigOption("sRGB Gamma Conversion", "Yes");
g_ogreRoot->setRenderSystem(renderSystem);
g_window = g_ogreRoot->initialise(/*autoCreateWindow=*/false, "GameLib");
// Use the already existing window
// See http://wiki.ogre3d.org/Using+SDL+Input
{
Ogre::NameValuePairList windowSettings;
windowSettings["currentGLContext"] = String("True");
int winWidth = 1920;
int winHeight = 1080;
g_window =
g_ogreRoot->createRenderWindow("GameLib", winWidth, winHeight, false, &windowSettings);
// renderWindow->setVisible(true);
}
registerHlms();
// Create SceneManager
const size_t numThreads = 1u;
g_sceneManager = g_ogreRoot->createSceneManager(ST_GENERIC, numThreads, "SceneManager");
// Create & setup camera
Camera* camera = g_sceneManager->createCamera("Main Camera");
// Position it at 500 in Z direction
camera->setPosition(Vector3(0, 5, 15));
// Look back along -Z
camera->lookAt(Vector3(0, 0, 0));
camera->setNearClipDistance(0.2f);
camera->setFarClipDistance(1000.0f);
camera->setAutoAspectRatio(true);
// Setup a basic compositor with a blue clear colour
CompositorManager2* compositorManager = g_ogreRoot->getCompositorManager2();
const String workspaceName("Main Workspace");
// const IdString definitionNameId = workspaceName;
const ColourValue backgroundColour(0.2f, 0.4f, 0.6f);
compositorManager->createBasicWorkspaceDef(workspaceName, backgroundColour, IdString());
compositorManager->addWorkspace(g_sceneManager, g_window->getTexture(), camera, workspaceName,
true);
Ogre::ResourceGroupManager::getSingleton().addResourceLocation("data/Models", "FileSystem",
"Models");
Ogre::WindowEventUtilities::addWindowEventListener(g_window, &g_myWindowEventListener);
g_graphicsIntialized = true;
return true;
}
void OgreShutdown()
{
Ogre::WindowEventUtilities::removeWindowEventListener(g_window, &g_myWindowEventListener);
OGRE_DELETE g_ogreRoot;
g_ogreRoot = nullptr;
}
Ogre::SceneManager* ogreGetSceneManager()
{
return g_sceneManager;
}