i am creating an game engine but now i have a problem. i want to create new object when user pressed button. but problem is when new object is being rendered the old objects must get rendered too. here is my loop:
GameObject texturetest;
GameObject sword;
Gameobject test[12]; // avaiable objects
int a;
while (e.eisrunning)
{
SDL_RenderClear(e.sr); /// render
if(buttonpressed)
{
test[a].init(true, false,e.sr,true, "Untitled.png"))
a++;
}
// i can render like test[a].render() but i have 2 problem
//1. if a is null then it will give error
//2. lets say we solved 1. problem but still it will render only current test object not the older ones
SDL_RenderPresent(e.sr); /// render ends
//
e.handleevent();
}
Create a vector of objects to render. Outside the game loop.
As you create objects that must be rendered, put them in the vector.
Each time inside the game loop, render everything in the vector.
When you want to add objects to be rendered, add them to the vector.
Because you created the vector outside the game loop, it will persist. It will not be destoyed each time round the loop. Objects you add to it will stay in it.
vector<GameObject> objects_to_render;
while ( game_is_running) // ENTER GAME LOOP
{
if ( want_to_make_a_new_object ) // e.g. by key press
{
GameObject new_object;
objects_to_render.push_back(new_object);
}
for (int i = 0; i < objects_to_render.size() ; i++)
{
render ( objects_to_render[i] );
}
}
Don't use arrays. Arrays are not for beginners.
test[a].init(true, false,e.sr,true, "Untitled.png"))
This looks very bad. This looks like your GameObject has an init function. This is bad bad bad. This is C style. You're using C++. In C++, we use constructors. It should be impossible for your GameObject to be created without having all these values set in them.
The list of reasons to use an array is very small indeed. Sometimes std::array (which is not the same an an array) instead of a std::vector, but as a rule of thumb, don't use arrays. Especially as a beginner. Arrays aren't for beginners.
@Repeater Hello again! I made some changes with my game engine and now it is C! and there is no Gamobject Class but gameobject struct. So how can I do that in c?
I think you misinterpret what they meant, they didn't mean for you to do it in C, but instead to use std::vector instead of pure arrays
so...I am sorry but change it back to C++...unless you want to deal with manual memory management ( I have found it to be...PITA )...
in C++, you can do this instead
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
vector< int > vec;
for( int i = 0; i < 10; ++i ) {
vec.push_back( 10 ); //adds elements to vector
}
//at the end of for, vec has 10 elements
if ( buttonpressed ) {
vec.push_back( 10 );//add another element;
}
//and then iterate through iterate through iterate
/*
example :
for( int i = 0; i < vec.size(); ++i ) {
cout << vec.at( i );
}
this outputs all elements in vec
*/
note : I usually use SFML the only time I have learnt anything about SDL is when I use D and want to know how to graphics in it, so I went for SDL instead of SFML because I thought I want to learn something new, ( I went back to SFML ), so my SDL knowledge is limited and I am pretty sure that e is something you made, right ?
struct object
{
int blah;
double stuff;
char drawme; //does C have a bool yet? I forget.
};
object test[12];
… code to assign object values and such left out here
for(int x = 0; x < 12; x++)
{
if (test[x].drawme)
drawfunction(test[x]);
}
the list version is more trouble, but it isn't fixed at size 12 max either. If you need a bunch of them..
you can also wrap the above if 'object' was already defined and you don't want to change it.
struct wrapper
{
objectwithoutabool *o; //with a pointer, it can refer back to an existing one without copy or other nonsense very easily. it could even refer to several of them in an array, if you wanted to draw them all in chunks, eg a car made of 50 objects all drawn...
char drawme;
};