0

This program is copy & paste ready!

INTRODUCTION
As the description of my topic says, I can almost taste the fruits of my labor. Journeying up, down, and around the Internet, I have searched, read, and studied various documents and tutorials to finally understand vectors enough to implement them into a game.

As most of you know, games will maintain the existence of entities (or game objects) and will render them to the screen. For example, Mario is standing on the game floor with 2 enemy mushrooms nearby. To the game, Mario, the floor, and the 2 enemy mushrooms are all entities. They will usually have, at minimum, an ID of some sort, an image or sprite corresponding to the type of object they are (environment, player, enemy, etc.), and a position.

The game will maintain the existence and state of these objects, and during every cycle through the game loop, will loop through the vector and draw these objects to the screen.

I finally understand how this is done, and have created a console-based simulation of what a game WOULD do in this situation.

It is not complete, and this is where I need your help.

Here is my code:

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

class GameObject
{
    private:
        int                id;
        int                X;
        int                Y;
        bool            alive;

    public:
        GameObject(int id);        // Constructor
        ~GameObject();             // Destructor
        void PrintInfo();         // Print all attributes
};

GameObject::GameObject(int init_id)
{
    id = init_id;
    X = rand() % 800; // <--- not really random
    Y = rand() % 600; // <--- not really random
    alive = true;
}

GameObject::~GameObject()
{
}

void GameObject::PrintInfo()
{
    std::cout << "   ID: " << id << "\t|\t" 
              << "Position: (" << X << "," << Y << ")" << "\t|\t" 
              << "Alive?: " << alive << std::endl;
}


int main()
{
    //    Program Purpose:
    //        Simulate the maintaining and drawing of game entities/objects
    //        in a graphical game. Entity positions will be maintained and
    //        this program will output (in text) what WOULD have been drawn
    //        if this was a working game with a real game loop.

    std::vector<GameObject> myGameObjects;  // Create a vector of "GameObject" objects
    std::vector<GameObject>::iterator it;    // Create vector iterator
    GameObject *newGO = NULL;    // What exactly do I call this? A pointer to the newest object?
    
    // Create and fill vector with 20 GameObjects
    for (int index = 0; index < 20; index++)
    {
        newGO = new GameObject(index);        // Create new GameObject
        myGameObjects.push_back(*newGO);    // Put new GameObject into vector
        delete newGO;
    }

    // SIMULATE FIRST SCREEN RENDER
    std::cout << "Game: I would have drawn to the screen these entities: " << std::endl;
    
    for (it = myGameObjects.begin(); it != myGameObjects.end(); ++it)
    {
        it->PrintInfo();
    }

    // Create a few more custom GameObjects
    newGO = new GameObject(500);
    myGameObjects.push_back(*newGO);
    delete newGO; 

    newGO = new GameObject(1000);
    myGameObjects.push_back(*newGO);
    delete newGO;

    // Pretend something happened in the game and destroy GameObject with ID 5, 10, 15
    //
    // CODE MISSING
    //


    // SIMULATE SECOND SCREEN RENDER
    std::cout << std::endl << std::endl;    // Add some white space for clarity
    std::cout << "Game: I would have drawn to the screen these entities: " << std::endl;
    
    for (it = myGameObjects.begin(); it != myGameObjects.end(); ++it)
    {
        it->PrintInfo();
    }


    // Delete ALL objects
    for (it = myGameObjects.begin(); it != myGameObjects.end(); ++it)
    {
        // Delete all objects before closing program
        // to ensure proper memory management
        //
        //    CODE MISSING
        //
    }



    system("pause");
    return 0;
}

For quick reference:
The lines I need some clarification/help on are:

  • Line 50
  • Line 77
  • Line 93

Current output for those interested to see:
[img]http://img525.imageshack.us/img525/3476/outputf.png[/img]


I would benefit more greatly from the help if you provided a description of what you did and how it works.
I really can't thank you enough for all of the help.
-BlackPhoenix

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Lerner
0

Either:
1) change line 49 to: std::vector<GameObject*> myGameObjects; if you want an array of pointers to GameObjects so you can use newGO as declared on line 50.
OR
2) add a public mutator function to set the id member of a GameObject calling something cute like setID(). change line 50 to: GameObject newGO;. replace line 55 with: newGO.setID(i); so you don't have to diddle with GameObject pointers, inluding not needed to delete them.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.