Jump to content
  • Advertisement
Sign in to follow this  

Creating Main() into a new .cpp

This topic is 4099 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) {
int main(int argc, char *argv[]) {

	//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)

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

	renderSystems = mRoot->getAvailableRenderers();
	r_it = renderSystems->begin();

	//Load Plugins
#if defined(_DEBUG)

	//Initialize Resources
		"resource", "FileSystem", "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");
	viewPort = mWindow->addViewport(mCamera);


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

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

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




	//Scene Cleanup Section
	delete mRoot;
	return 0;


Share this post

Link to post
Share on other sites
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.



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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!