Page 1 of 1

How the heck do you put the code in order?!

Posted: Sun Sep 13, 2009 12:41 am
by GlitchGuy2
Okay, one problem I have had in all these game engines is organizing my source code and getting all the engine setups into a single function/header.

Say this is my main.cpp

Code: Select all

#include "masterhead.h"

int main()
{
	createAcorn();
	device->drop();
	return 0;
}

#include "functionmain.h"
This is masterhead.h

Code: Select all

#pragma once

#include <irrlicht.h>

// irrlicht's namespaces
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

// Link the library
#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
#endif

#include "prototype.h"
And here's prototype.h

Code: Select all

#pragma once


// CREATE THE ENGINE AND DRIVERS

int createAcorn(void)
{
			IrrlichtDevice *device =
	#ifdef _IRR_OSX_PLATFORM_
					createDevice( video::EDT_OPENGL, dimension2d<s32>(640, 480), 16,
							false, false, false, 0);
	#else
					createDevice( video::EDT_SOFTWARE, dimension2d<s32>(640, 480), 16,
							false, false, false, 0);
	#endif
			if(!device)
					return 1;


	IVideoDriver* driver = device->getVideoDriver();
	ISceneManager* smgr = device->getSceneManager();
	IGUIEnvironment* guienv = device->getGUIEnvironment();
}

/****************************************************************/



// FRAMES PER SECOND return

int returnFPS(void)
{
	driver->getFPS();
}

// Current Software Running return

int returnDriver(void)
{
	driver->getName();
}

Now, please, isn't this the most newbish, unorganized code ever? I can't even compile it without the compiler making the createAcorn() function be considered an integer or something, and because it for that weird reason it won't access the function createAcorn() it says that device is undeclared and stuff like that, because the device is created in the said function.

Also, I call this project Acorn, which will explain why the function is called createAcorn() if anyone doesn't understand that, createAcorn() is the initiation function.

Also, I'm probably gonna get yelled at for using an #include at the bottom of main.cpp

My question I need help with is... How do I organize my source codes to work with the compiler I'm using (Visual C++ 2008 Express)

Posted: Sun Sep 13, 2009 1:50 am
by Jake-GR
dont mean to flame, but it might be a good idea to pick up a couple c++ books. I have currently have 3 i keep for reference.
at first glance of your code, id look up "scope" your createAcorn() function gets run, setting device, driver, etc... but once it hits the end bracket for the function } they are lost (still in memory but unaccessable)

good practices also, dont use "using namespace" in header files, headers are more for just structuring the .cpp file so when another file needs to use the class it knows where to look.

there is a quick fix for it, but it involves the deadly word "global"... which in this example there is no reason to use them.

also look at the layout of the tutorials, break stuff into functions, but leave them in the same file "main.cpp" and when that works, then start moving into other files.

Posted: Sun Sep 13, 2009 1:59 am
by jpoag
Yeah, Jake beat me to the punch, but said pretty much what I was going to say. I think you're confusing a function with a macro expansion (please don't litter your code with macros).

Organization is the least of your problems. As a side note, it is possible to include a file where ever you want in your code. The #include directive literally inserts the file at that point. If you are going to #include files that are just code, then you could name then '.inc' or what ever you want.

Is it good form?

I'm reminded of the old saying: "When all you have is a hammer, everything looks like a nail." Once you figure out what you're doing wrong (and how to make it work) then you can add it to your bag of tricks. Just don't over do it in the future.

Posted: Sun Sep 13, 2009 2:23 am
by GlitchGuy2
Yeah, I have some books on programming, C++ for Dummies and C++ in 10 Minutes.

Thanks for the advice everyone.

@Jake No offense taken