Jump to content
  • Advertisement
Sign in to follow this  
jtmerchant

std::vectorvoid* For UI?

This topic is 5016 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

I'm making a sort of user interface for my game and to contain all the entities I'm using a std::vector<void*>. To store information, I use the new keyword with whatever I'm dealing with. When I create textures and store them, this obviously gets ugly. Not anything noticeable, but I know its bad to use that much of the free store/stack. If I say something like
int MyFunction()  {
int num1=122;
entities.push_back(((void*)&num1);  //The vector<>
return 1;
}

then the stored information in num1 is gone when I exit that function right?? Is there a better way to do this than using the stack?

Share this post


Link to post
Share on other sites
Advertisement
You could use a class(yes I know I left out the fancy stuff)



class VoidVector{
private:
vector<void *> entities;
public:
void MyFunction();};

void VoidVector::MyFunction(){
int num=122;
entities.push_back(((void*)&num);
return;}

VoidVector VV;
VV.MyFunction();




or if thats too advanced:



int MyFunction(vector<void *> &entities) {
int num1=122;
entities.push_back(((void*)&num1);
return 1;}

vector<void *> voidv;
MyFunction(voidv);




Well I haven't tried either(so I might be way off) but those should work.

Share this post


Link to post
Share on other sites
sounds to me like you're not using the vector properly. why dont you use structs and classes? if your excuse is you havn't learnt about them, then you shouldnt be using a vector :) !

here is what i would do in your situation (i hope i understood it right...)


struct entity_t {
int num1;
int num2;
float pos[3];
};

vector<entity_t> entities;

int myFunction() {
entity_t myNewEntity;
myNewEntity.num1 = 122;
myNewEntity.num2 = 23; //whatever else..
entities.push_back( myNewEntity ); // pushback will make a copy, and the vector isnt storing a pointer so it should be ok
return 1;
}

Share this post


Link to post
Share on other sites

int MyFunction() {
int num1=122;
entities.push_back((void*)&num1); //The vector<>
return 1;
}


Thats what I wrote as an example of something that wouldn't work. And the problem with that is that num1 is local. When you try to access entities[0(or whatever)], the pointer to num1 is messed up because num1 doesn't exist. I was saying that wasn't an option. Hope I didn't confuse you, heh.

Also, I used too many parenthesis in my first post.

EDIT:: BTW, just think of my num1 as your myNewEntity in the example.

[Edited by - jtmerchant on October 24, 2004 12:33:45 AM]

Share this post


Link to post
Share on other sites
is there any particular need for you to store a pointer?
why not store by value, or are you trying to store more than 1 type in the vector?

what my engine does (which is good for larger scale projects) is i have a allocation pool which gets allocated once on startup, i track free's and alloc's and just pass a pointer from the memory pool, its a bit difficult to setup at first, but its probably worth it because its stops memory fragmentation and theres virtual no performance hit on allocs or deallocs, which means you can do them as often as you need, even in the mission critical rendering backends..

here's a very basic implementation to convey the idea


int tracker = 0;
byte *pool = NULL;

void initMem() {
pool = new byte[MAX_POOL_SIZE];
tracker = 0;
}

byte *allocMem( int sizeRequired ) {
byte *r = pool + tracker;
tracker += sizeRequired;
return r;
};

void main() {
initMem();
int *test = static_cast<int*>( allocMem( sizeof(int) ) );
*test = 4;
cout << test << " = " << *test << endl;
}


ofcourse that dosn't demonstrate the difficult task of memory deletion but you should get the picture.'

Share this post


Link to post
Share on other sites
Quote:
Original post by jtmerchant

int MyFunction() {
int num1=122;
entities.push_back((void*)&num1); //The vector<>
return 1;
}


Thats what I wrote as an example of something that wouldn't work. And the problem with that is that num1 is local. When you try to access entities[0(or whatever)], the pointer to num1 is messed up because num1 doesn't exist. I was saying that wasn't an option. Hope I didn't confuse you, heh.

Also, I used too many parenthesis in my first post.

EDIT:: BTW, just think of my num1 as your myNewEntity in the example.


You need to allocate instances on the heap for them to persist through-out any scope, but if you use a vector of void pointers you'll never be able to destroy the instances properly because you've lost type information that is needed to do the destroy.

You really don't need a container of void pointers for this.

I would suggest using something like a composite design pattern.

Share this post


Link to post
Share on other sites
Hmmm... Ok. What I'm doing is basically this:

//#define UIENT_RECT
//#define UIENT_MATRIX
//std::vector<void*> entity;
//std::vector<int> entityID;

void MyFunction() {
//Save Rectangle
RECT* r=new RECT();//Create r and allocate into heap
//...fill r....
entity.push_back((void*)v);//Add r to my vector of entities
entityID.push_back(UIENT_RECT);//Add the description of r to my vector of IDs

//Save Matrix
D3DXMATRIX* m=new D3DXMATRIX();//Create m and allocate into heap
entity.push_back((void*)m);//Add m to my vector of entities
entityID.push_back(UIENT_MATRIX);//Add the description of m tomy vector of IDs
}



I have entityID so that I know what kind of data it is. Now my problem is that I am using the heap. And that I might need to go over the memory limits of the heap. So I know what kind of data it is and how to delete it. I need to know if there's a way to have a vector of different types and not use the heap.

Share this post


Link to post
Share on other sites
Quote:
Original post by jtmerchant
And that I might need to go over the memory limits of the heap. So I know what kind of data it is and how to delete it.


That is highly un-likely to ever happen.

Quote:
Original post by jtmerchant
I need to know if there's a way to have a vector of different types and not use the heap.


The only way that is going to happen is if you write a memory manager that uses pre-allocated chunk of memory that is available through-out the life-time of the program and you write your own or use a pre-existing allocator type for the vector because the default allocator type for STL containers uses the heap aswell.

The closest you'll get to what your asking with-out doing all of the above and not have to explicitly delete the memory and need to not keep type information yourself is to have a vector of boost::any (and storing it by value not a pointer to boost::any) e.g.


#include <boost\any.hpp>

#include <string>
#include <complex>
#include <vector>
#include <iostream>

void add_a_float(std::vector<boost::any>& vec_ref) {

vec_ref.push_back(40.0f);

}

int main() {

using boost::any;
using boost::any_cast;

std::vector<any> vec_of_any;

vec_of_any.push_back(30);
vec_of_any.push_back(std::complex<float>(2.0f, 2.0f));
vec_of_any.push_back(std::string("hello"));

add_a_float(vec_of_any);

std::cout << any_cast<int>(vec_of_any[0]) << '\n';
std::cout << any_cast<std::complex<float> >(vec_of_any[1]) << '\n';
std::cout << any_cast<std::string>(vec_of_any[2]) << '\n';
std::cout << any_cast<float>(vec_of_any[3]) << std::endl;

return 0;
}


But i'm telling this isn't a good way to write custom user interface, you don't need/wont to store arbtrary un-related types. You'll most definitely wont to store a hierarchy of related-types for GUIs/UI this is where OO works really well & naturally.

[Edited by - snk_kid on October 24, 2004 1:24:07 PM]

Share this post


Link to post
Share on other sites
im with snk_kid on this, there's something very wrong with your design if you have to store data in the method you displayed.

you want something like this, but you seem to be trying to hack it rather than using the gifts of the programming language.


class widgetBase {}
class button : public widgetBase {}
class window : public widgetBase {}

std::vector<widgetBase*> myGui;

myGui.push_back( new window() );
myGui.push_back( new button() );

// do stuff

for( std::vector<widgetBase*>::iterator i = myGui.begin();
i != myGui.end(); i++) // might be ++i, can't remember
delete *i;

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!