Game Programming and Development Tools

C++ advice needed – fearless

fearless

Member

Posts: 91
From: Romania, Tg Mures
Registered: 11-26-2005
I'm working on my own Irrlicht - Lua binding and I'm having a number of C++ problems that are quite a challenge. I can do this in C# blind folded yet I feel lost in C++.
I'm making a function that will get called from a script file. The function will create objects on the heap. It will allow me to define by scripting the objects that get loaded to the game. The object class will contain pointers to Irrlicht resources (textures, meshes, etc.)
C++ has several containers (std, boost, etc) and in many cases people build their own linked lists.
Which way should I go?

Calin

[Edit]I have to delete my fearless acc. Keep forgetting I stopped using it.
------------------

My projects page:
http://calinnegru.googlepages.com/projects

'As there are plants which will flourish only in mountain soil, so it appears that Mercy will flower only when it grows in the crannies of the rock of Justice; transplanted to the marshlands of mere Humanitarianism, it becomes a man-eating weed, all the more dangerous because it is still called by the same name as the mountain variety.'
The Humanitarian Theory of Punishment
by C. S. Lewis

[This message has been edited by fearless (edited December 14, 2006).]

Jari

Member

Posts: 1471
From: Helsinki, Finland
Registered: 03-11-2005
No need to build anything of your own because like you said there are options. I'd use std's list and some smart pointers from boost if needed. I hope that little piece of info helps. Doesn't C++ something like auto pointer feature these days? I havent taken the time to learn that yet.

------------------
1Jo 2:9-10 He that saith he is in the light, and hateth his brother, is in darkness even until now. 10 He that loveth his brother abideth in the light, and there is none occasion of stumbling in him.
Joh 17:26 And I have declared unto them thy name, and will declare it: that the love wherewith thou hast loved me may be in them, and I in them.

[VoHW] (Help needed) [Blog] - Truedisciple (mp3)

HanClinto

Administrator

Posts: 1828
From: Indiana
Registered: 10-11-2004
quote:
Originally posted by fearless:
[Edit]I have to delete my fearless acc. Keep forgetting I stopped using it.

You could try changing your password to something strange (or possibly just a random jumble of characters that you won't remember but have written down somewhere in case you really need it).

dartsman

Member

Posts: 484
From: Queensland, Australia
Registered: 03-16-2006
quick example of how you'd use the STL list container... don't know if this would compile, I just wrote it up in Notepad, and I haven't coded in C++ for about a week...

///
// Author: dartsman
// Date: 16th
///

#include <iostream>
#include <list>

using namespace std;


std::list<int*> myList;

void addItem(int *item)
{
myList.push_back(item);
}


void clearList()
{
std::list<int*>::iterator iter;

for(iter = myList.being(); iter != myList.end(); ++iter)
{
if ((*iter) != NULL)
{
delete (*iter);
(*iter) = NULL;
}
}
}

void displayList()
{
std::list<int*>::iterator iter;

for(iter = myList.being(); iter != myList.end(); ++iter)
{
if ((*iter) != NULL)
{
cout << (*iter) << endl;
}
}
}

int main()
{
clearList();

int *test1, *test2, *test3;

test1 = new int;
test2 = new int;
test3 = new int;

test1 = 5;
test2 = 325;
test3 = 34;

addItem(test1);
addItem(test2);
addItem(test3);

displayList();

return 0;
}

------------------
www.auran.com

Calin

Member

Posts: 358
From: Moldova
Registered: 12-04-2006
@HanClinto: I had same password for both accounts, I have a new pass to actually get me thinking.

@DartsMan:

Nice example. Problem is the number of objects to be added is unknown until runtime (the data is read from script). So I'm thinking at this version of addItem


std::list<Item*> myList;

void addItem(string name)
{
Item* Temp;
Temp->Name = name;

myList.push_back(Temp);
}

int main()
{
addItem("Jim");
addItem("John");
addItem("Joey");
}


Is this Ok or I'll end up with a single pointer that is being constantly overwritten?


[This message has been edited by Calin (edited December 16, 2006).]

[This message has been edited by Calin (edited December 16, 2006).]

dartsman

Member

Posts: 484
From: Queensland, Australia
Registered: 03-16-2006
You can still use mine at runtime... I don't see what the issue is... I was only passing in some temp variables to show an example of them being used..

Just in whatever you'd load the scripts through, you'd just keep adding those into the list... I think your not getting that you couldn't just cut/paste my example into your script loader (or whatever your doing).

Your example is bad, very bad, Temp has not been defined. In debug it *might* be ok (as Debug will initialise uninitialised variables (set to 0)) but in release, Temp is pointing to some random location.

void addItem(string name)
{
Item *Temp = new Item();
Temp->Name = name;

myList.push_back(Temp);
}

Sure, you could place your code into addItem() to allocate the new memory, that is also a good idea to have the same class allocating the memory as being the one which is destroying the memory too Also for code readability place the * infront of the variable name rather then just after the Data Type.

eg.
int* k, j;
k is a pointer, j isn't.. however it looks like both k and j are pointers
int *k, j;
makes it a lot clearer, as it is right on the k, and not the Data Type

Have a look into Object Factories (basic ones, as advanced ones will go well above what you would really need).

Anywho, I could post up some stuff later on, time to eat some dinner...

------------------
www.auran.com

dartsman

Member

Posts: 484
From: Queensland, Australia
Registered: 03-16-2006
BTW, the number of objects doesn't matter... you could keep adding items into it as it's a linked list...

for (int index = 0; index < 100; ++index)
{
int *temp = new int;
temp = index;
addItem(temp);
}

------------------
www.auran.com

Calin

Member

Posts: 358
From: Moldova
Registered: 12-04-2006
quote:
Originally posted by dartsman:
You can still use mine at runtime... I don't see what the issue is... I was only passing in some temp variables to show an example of them being used..


Sorry, my bad. For some reason I had the impression you're doing something alone the lines:

...

test1 = 5;
test2 = 325;
test3 = 34;

myList.push_back(test1);
myList.push_back(test2);
myList.push_back(test3);


quote:
Originally posted by dartsman:

Your example is bad, very bad, Temp has not been defined. In debug it *might* be ok (as Debug will initialise uninitialised variables (set to 0)) but in release, Temp is pointing to some random location.


Thanks for pointing that out. I haven't done too much coding in C++ and pointers have always been my week spot. I've got it now =].

Your posts help qute a bit Dartsman.

[This message has been edited by Calin (edited December 16, 2006).]

dartsman

Member

Posts: 484
From: Queensland, Australia
Registered: 03-16-2006
quote:
Originally posted by Calin:
Sorry, my bad. For some reason I had the impression you're doing something alone the lines:

...test1 = 5;
test2 = 325;
test3 = 34;
myList.push_back(test1);
myList.push_back(test2);
myList.push_back(test3);


It was... though it's just an example. You would want to extend that into some sort of Object Factory or Object Manager class.

Heres a simple Object Manager example...

class Object
{
public:
Object()
: m_refNum(-1)
{};
~Object() {};

char *Display()
{
char disp[32];
sprintf_s(disp, 32, "Object (%d)", m_refNum);
return disp;
};

void SetReference(int ref) { m_refNum = ref; };

private:
int m_refNum;

// actual data could go here
};

class ObjectManager
{
public:
ObjectManager()
: m_reference(0)
{};
~ObjectManager()
{
ClearList();
};

void addObject(Object *obj)
{
obj->SetReference(m_reference);
myList.push_back(obj);
m_reference++;
}

void clearList()
{
std::list<Object*>::iterator iter;
for(iter = myList.being(); iter != myList.end(); ++iter)
{
if ((*iter) != NULL)
{
delete (*iter);
(*iter) = NULL;
}
}

m_reference = 0;
}

void displayObjects()
{
std::list<Object*>::iterator iter;
for(iter = myList.being(); iter != myList.end(); ++iter)
{
if ((*iter) != NULL)
{
cout << iter->Display() << endl;
}
}
}

private:
int m_reference;

std::list<Object*> myList;
};

So that you could use that and do...

ObjectManager objManager;

for (int index = 0; index < 50; ++index)
{
Object *obj = new Object();
objManager.addObject(obj);
}

objManager.displayObjects();

objManager.clearList();

I believe that'll work, however again, wrote it in Notepad and haven't tried to compile n link it...

------------------
www.auran.com