Question about refactoring code..

Hey all good people, I have a question.. I took some time learning C++ and now I'm starting to work with graphic engines, namely the Ogre3D.. Now reading lessons and examples is one thing, they usually work out (:)) but jumping head down into a project and trying to do something, is entirely a different thing..

I basically just want to practice by doing this..
I have one big function called startup() in class Game.. This functions contain following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
int Game::startup()
{
	root = new Ogre::Root("plugins_d.cfg");

	root->loadPlugin("RenderSystem_GL_d.dll");
	Ogre::RenderSystem *rs = root->getRenderSystemByName("OpenGL Rendering Subsystem");
	root->setRenderSystem(rs);
	rs->setConfigOption("Full Screen", "No");
	rs->setConfigOption("Video Mode", "800 x 600 @ 32-bit colour");

	....
	return 0;
}


root is a pointer to the new instance of Ogre::Root class which takes 1 argument. root is earlier defined in Game.h with following:

Ogre::Root *root;

Now what I want it to take that block of code (starting with root->loadPlugins, and endind with setting a resolution) to its own function. So the final result should be:

1
2
3
4
5
6
7
8
9
int Game::startup()
{
	root = new Ogre::Root("plugins_d.cfg");

	setupRenderer("RenderSystem_GL_d.dll");

	....
	return 0;
}


This has to be possible, right? I've been playing around but didn't managed to do it.. First thinking is, okay I need to pass pointer root by reference, and string "RenderSystem_GL_d.dll" by value.. So this is what I got so far:

1
2
3
4
5
6
7
8
void Game::setupRenderer(Ogre::Root &root, Ogre::String renderer)
{
	root->loadPlugin(renderer);
	Ogre::RenderSystem *rs = root->getRenderSystemByName("OpenGL Rendering Subsystem");
	root->setRenderSystem(rs);
	rs->setConfigOption("Full Screen", "No");
	rs->setConfigOption("Video Mode", "800 x 600 @ 32-bit colour");
}


Then I get error "did you intend to use '.' instead of -> ?" That's another question that's been bugged me lately, when does one use -> and when does one use . instead? So that's where I am, hopefully someone can help me out!

Best regards,
-eXtremeCpp
You use '->' for struct/class pointer member access and '.' for struct/class member access.
Thanks for that hanst99, will look more into it. Is there any way you can write me a skeleton for that function? I mostly interested in how to pass and take that root the right way..

Thanks!
Just use . instead of ->. I don't know Ogre3D, so I don't know what else is supposed to go in there.
Well, I know you don't know Ogre3D, but can't this be looked from generalized standpoint? Like okay, you have one important "root" variable, that you need to pass by reference, do stuff with it and return to the function from which you've been started.. etc, things like that?

Anyways, i tried to replace -> with . and got following error..
Code:
1
2
3
4
5
6
7
8
void Game::setupRenderer(Ogre::Root &root, Ogre::String renderer)
{
	root.loadPlugin(renderer);
	Ogre::RenderSystem *rs = root.getRenderSystemByName("OpenGL Rendering Subsystem");
	root.setRenderSystem(rs);
	rs->setConfigOption("Full Screen", "No");
	rs->setConfigOption("Video Mode", "800 x 600 @ 32-bit colour");
}


Error:
error C2664: 'Game::setupRenderer' : cannot convert parameter 1 from 'Ogre::Root *' to 'Ogre::Root &'
¿How are you calling at your method?
Also, if root is a member of Game ¿why are you passing it as parameter?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void Game::setupRenderer( Ogre::root* r )
{
	r->loadPlugin("RenderSystem_GL_d.dll");
	Ogre::RenderSystem *rs = r->getRenderSystemByName("OpenGL Rendering Subsystem");
	r->setRenderSystem(rs);
	rs->setConfigOption("Full Screen", "No");
	rs->setConfigOption("Video Mode", "800 x 600 @ 32-bit colour");
}
  
int Game::startup()
{
	root = new Ogre::Root("plugins_d.cfg");

	setupRenderer(root) ;

	....
	return 0;
}
Last edited on
Topic archived. No new replies allowed.