Jump to content
  • Advertisement
Sign in to follow this  
CrimsonGT

Creating Main() into a new .cpp

This topic is 3922 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im not new to programming, but I am to c++. I am writing a very basic Ogre application and the tutorial I followed has the main() loop right in with everything else. I am trying to figure out how to seperate it so I can have like a main.cpp and then ogre.cpp file. My reason is that I am extremely OCD and have to keep everything as organized as possible. Im still fairly new to what *should* go into headers and what should go into source files. I am assuming this code should be in a source file, and I know I can't include a source file in another except through inheritance. So should I put all of this into a header file with the exception of the #if defined(WIN32) through the #endif? Here is my code...
//Include all of files needed for the Project
#include "wo_Globals.h"

#if defined(WIN32)
#include "windows.h"

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
#else
int main(int argc, char *argv[]) {
#endif

	//Initialize Ogre Variables
	Ogre::Root *mRoot;
	Ogre::RenderWindow *mWindow;
	Ogre::SceneManager *sceneMgr;
	Ogre::Camera *mCamera;
	Ogre::Viewport *viewPort;

	mRoot = new Root("", "");

	//Load Render System
#if defined(_DEBUG)
	mRoot->loadPlugin("RenderSystem_GL_d");
#else
	mRoot->loadPlugin("RenderSystem_GL");
#endif

	RenderSystemList *renderSystems = NULL;
	RenderSystemList::iterator r_it;

	renderSystems = mRoot->getAvailableRenderers();
	r_it = renderSystems->begin();
	mRoot->setRenderSystem(*r_it);
	mRoot->initialise(false);

	//Load Plugins
#if defined(_DEBUG)
	mRoot->loadPlugin("Plugin_CgProgramManager_d");
	mRoot->loadPlugin("Plugin_OctreeSceneManager_d");
#else
	mRoot->loadPlugin("Plugin_CgProgramManager");
	mRoot->loadPlugin("Plugin_OctreeSceneManager");
#endif

	//Initialize Resources
	ResourceGroupManager::getSingleton().addResourceLocation(
		"resource", "FileSystem", "General");

	ResourceGroupManager::getSingleton().initialiseResourceGroup("General");

	//Hardcoded Defaults - Temp
	NameValuePairList opts;
	opts["resolution"] = "1024x768";
	opts["fullscreen"] = "false";
	opts["vsync"] = "false";

	//Create the actual Window
	mWindow = mRoot->createRenderWindow("Warlords", 1024, 768, false, &opts);

	//Create the Scene Manager, Camera, and Viewport
	sceneMgr = mRoot->createSceneManager(ST_GENERIC);
	mCamera = sceneMgr->createCamera("PlayerCam");
	mCamera->setNearClipDistance(5);
	viewPort = mWindow->addViewport(mCamera);
	viewPort->setBackgroundColour(ColourValue(0,0,0));

	mCamera->setAspectRatio((Real)1.333333);

	//For Input Handler
	unsigned long hWnd;
	mWindow->getCustomAttribute("WINDOW", &hWnd);

	//Setup the Inputer Handler
	Simulation *mSim = new Simulation();
	InputHandler *mHandler = new InputHandler(mSim, hWnd);
	mSim->requestStateChange(SIMULATION);

	//While Game State is not SHUTDOWN, Continue to Render
	while (mSim->getCurrentState() != SHUTDOWN) {

		mHandler->capture();

		WindowEventUtilities::messagePump();

		mRoot->renderOneFrame();
	}

	//Scene Cleanup Section
	delete mRoot;
	return 0;

}

Share this post


Link to post
Share on other sites
Advertisement
Hello CrimsonGT,

I'd read the following article for a discussion on organizing code between source and header files: Organizing Code Files in C and C++

I'm not sure I understand your suggestion to move most of this code into a header. As you've correctly assumed that (typically) code belongs in source file(s), given most of your example is the code (function body) it shouldn't be moved into a header.

Cheers,

Tom

Share this post


Link to post
Share on other sites
Basically I am trying to figure out how to JUST pull the Main() loop out of that code, and place it into a different source file, but have everything still work the same.

Share this post


Link to post
Share on other sites
A wrote a bunch of stuff up, but decided it was unnecessary as the link given by TomH would have covered everything I said and likely more thoroughly. So:

1)Write a function that takes as arguments all of the variables that you will use. You will probably want to pass these arguments by reference. Just write the function body for now.

2)Write a prototype for that function.

2)Read that link repeatedly until you understand it and how it applies to your situation.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!