• Advertisement
Sign in to follow this  

How to setup things...

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

Hi. Im working on my largest project sofar and i have stumbled into a problem... Ill try to describe the best i can. Ill leave out the unrelated things in the code. main.cpp
int main()
{

    while(1)
    {
        Update(); //update stuff (like enemy positions and such)
        Render(); //render stuff 
    }
}

Game.h
class Game
{
   void Update();
}

Game.cpp
void Game::Update()
{
   //update stuff here
   //like enemy pos
}

Enemy.h
class Enemy
{
    int posX;
    int posY;

    void Draw();

}

Enemy.cpp
void Enemy::Draw()
{
    //Draw enemy on posX and posY
}

Draw.h
class Draw()
{
    void Render();
}

Draw.cpp

void Draw::Render()
{
   Enemy e;
   e.Draw();
}

Earlier i have always done the update and renderering process in the same function so i had no problem, just make the Enemy object static and start rendering/updating. Now i want the Render process to be totally independent of pretty much everything, i only want it to draw the stuff on the appropriate places and i want the update process to keep track of where these places are. Now in my game i will be drawing lots of enemies so i have made a class, Enemy with the appropriate members and when i draw it i just make an object and draw it in teh render process while updating it in the update thingie. I couldve just put them both in the rendering like ive used to, but now im going to draw stuff in the hundreds and i dont want to make a seperate function for each, i just want to make 100 Enemy objects and call teh Enemy.Draw(); a hundred times. Now how do i update the posX and posY for the enemies without doing it in the render function? Somehow i have to save the objects so they dont get destroyed when out of scope, and then update it somehow. Im not that fancy on pointer math and stuff cause i hate it, but it should be possible to do this without making a huge set of globals right? I have a nother question aswell. Is this a good way to do things? Tell me if you need more info on my problem. I would very much appreciate any help on this. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by cresty
Earlier i have always done the update and renderering process in the same function so i had no problem, just make the Enemy object static and start rendering/updating.


Er - *the* enemy object? Only one?

Quote:
Now i want the Render process to be totally independent of pretty much everything, i only want it to draw the stuff on the appropriate places and i want the update process to keep track of where these places are.


Good for you. :)

Quote:
Now in my game i will be drawing lots of enemies


:) See how things "evolve"? :)

Quote:
Now how do i update the posX and posY for the enemies without doing it in the render function?


By doing it in the update function.

Quote:
Somehow i have to save the objects so they dont get destroyed when out of scope, and then update it somehow. Im not that fancy on pointer math and stuff cause i hate it, but it should be possible to do this without making a huge set of globals right?


Yes, and there is no reason to use the P-word.

The first thought is to *pass* the Enemy (i.e., as a parameter), *by reference*, to each function. Keep the variables outside. It doesn't make sense that the process "draw an enemy" should create its own enemy to draw. What you want to say is "here's an enemy; draw it for me". Similarly for updating. There's no reason to wrap those processes in classes, either. Classes are *nouns*; they're things. They're not verbs (actions). Updating something is an action. An updater would be a thing, but we don't really need that level of abstraction (yet).

Of course, you already seem to have the idea that Enemies can "draw" themselves. So why can't they "update" themselves, too? [smile] We see that Update() and Render() become unnecessary; just talk to the Enemy (o_O).

For one enemy:


int main()
{
Enemy e;
while(1)
{
e.update();
e.render();
}
}


For several enemies:


typedef std::vector<Enemy> Enemies;

int main()
{
Enemies enemies;
while(1)
{
// You may think it strange to do two separate loops here, but trust me,
// in the long run you will be thankful for really *separating* the
// update and render processes
for (Enemies::iterator it = enemies.begin(); it != enemies.end(); ++it)
{
it->update();
}
for (Enemies::iterator it = enemies.begin(); it != enemies.end(); ++it)
{
it->render();
}
}
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement