
-----------------------------------
abcdefghijklmnopqrstuvwxy
Thu Jan 18, 2007 11:10 pm

creating objects at run time?
-----------------------------------
I am making a game called "Master Mind" with C++ && SDL.  For those that don't know, Master Mind has colored tacs that you insert into holes located on the board.  When a user pics up a tac, say a tac colored blue, I need a new blue colored tac to appear where the user picked up the blue tac.  (Therefore, an infinite supply of blue tacs)  

How would one go about accomplishing this?  I'm still new to C++ and I can't seem to get around the Object Oriented barrier.  

Unfortunately I don't know how to copy paste my code because I'm using Emacs and I don't get why it won't let me...  

I have a Tac class.  
Constructor takes a string file (for the image file of the tac to instantiate on the screen); the x, y coordinates of where to put it on the screen; and the width of the image.  

Tac::Tac(std::string file, int x, int y, int w)

Then the show() method draws it on the screen.  

So then in my program if I click on the image and move it away, how do i get a new copy of the object to appear where i dragged it away from?

-----------------------------------
md
Fri Jan 19, 2007 1:46 am

RE:creating objects at run time?
-----------------------------------
SDL is not my area of expertese; but depending on how you're writing things creating a new instance of a Tac and then moving it to where it should end up would do it. That requires some memory management stuff though... I sugest starting with somethign simple (not a game; especially for C++) for learning. 

Say, write a program that sorts a linked list. That will help you learn about new and delete and list traversals (all important for games).

-----------------------------------
abcdefghijklmnopqrstuvwxy
Fri Jan 19, 2007 2:10 am

RE:creating objects at run time?
-----------------------------------
I'm sick of console applications md...  mainly because I don't have a use for any of them.  A game like mastermind provides me the motivation to program.  And i know all about new and delete :D

I wonder if I've been clear about my actual problem.  Basically I just want a new object instantiated when I move an existing one away. 

One way to accomplish it (I think)
is to declare a vector of Tacs.  When one tac is moved, add a new tac to the vector with the coordinates it should appear to... Does that sound feasible?

-----------------------------------
md
Fri Jan 19, 2007 2:16 am

Re: creating objects at run time?
-----------------------------------
Sorry, I didn't know how much you knew :P

Yes, that would work.

-----------------------------------
abcdefghijklmnopqrstuvwxy
Fri Jan 19, 2007 2:20 am

Re: creating objects at run time?
-----------------------------------

class Color_Tac
{
  private:
    SDL_Surface * pSurface;
    SDL_Rect Cords;
  public:
  Color_Tac(std::string file, int x, int y, int w)
    { 
      pSurface = SDL_LoadBMP(file.c_str());
      Cords.x = x;  
      Cords.y = y;
      Cords.w = w;
    }
    ~Color_Tac() 
    {}
  void handleEvents(SDL_Event&, bool&);
  void show(SDL_Surface*);
};

void Color_Tac::handleEvents(SDL_Event& event, bool& clicked_tac)
{
  int mouse_x, mouse_y;
  
  if (event.type == SDL_MOUSEBUTTONDOWN)
  {
    SDL_GetMouseState(&mouse_x, &mouse_y);
     //check if mouse is over Color_Tac
    if (mouse_x = Cords.x && mouse_y = Cords.y)    
    {        
      cerr show(MainWindow.getSurface());
  }

  return SUCCESS;
}

