Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 creating objects at run time?
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
abcdefghijklmnopqrstuvwxy




PostPosted: Thu Jan 18, 2007 11:10 pm   Post subject: 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?
Sponsor
Sponsor
Sponsor
sponsor
md




PostPosted: Fri Jan 19, 2007 1:46 am   Post subject: 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




PostPosted: Fri Jan 19, 2007 2:10 am   Post subject: 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 Very Happy

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




PostPosted: Fri Jan 19, 2007 2:16 am   Post subject: Re: creating objects at run time?

Sorry, I didn't know how much you knew Razz

Yes, that would work.
abcdefghijklmnopqrstuvwxy




PostPosted: Fri Jan 19, 2007 2:20 am   Post subject: Re: creating objects at run time?

code:

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 + Cords.w) && mouse_x >= Cords.x && mouse_y <= (Cords.y + Cords.w) && mouse_y >= Cords.y)   
    {       
      cerr << "you clicked me, bitch." << endl;
      clicked_tac = true;
      return;
    }
    else if (clicked_tac)
    {
      cerr << "you clicked the screen" << endl;
      Cords.x = (mouse_x - (Cords.w/2));  //move the tac to the new mouse indicated location.
      Cords.y = (mouse_y - (Cords.w/2));
      clicked_tac = false;
    }
  }
  return;
}

void Color_Tac::show(SDL_Surface* pMainWindow)
{
   draw_image(pSurface, pMainWindow, Cords.x, Cords.y); 
   return;
}


That's the class... Now I'm asking any good OOP programmers for ideas on how to make a new instance at run time so as to be able to create infinite amounts of these things while in a game loop...
md




PostPosted: Fri Jan 19, 2007 11:11 am   Post subject: RE:creating objects at run time?

c++:
Color_Tac *t = new Color_Tac("filename", x_pos, y_pos);
abcdefghijklmnopqrstuvwxy




PostPosted: Fri Jan 19, 2007 5:22 pm   Post subject: RE:creating objects at run time?

Say I put that in a loop and did that 40 times... how would i possibly access them all? *t would get confused as to which object it's pointing to won't it?
wtd




PostPosted: Fri Jan 19, 2007 5:32 pm   Post subject: RE:creating objects at run time?

Then you need an aggregate data structure of some sort.
Sponsor
Sponsor
Sponsor
sponsor
abcdefghijklmnopqrstuvwxy




PostPosted: Fri Jan 19, 2007 5:36 pm   Post subject: RE:creating objects at run time?

Indeed, I previously suggested vectors and no one commented on that. I tried to implement it with vectors but I'm having a little problem... I'll tell you what... I'll post the code i have.
abcdefghijklmnopqrstuvwxy




PostPosted: Fri Jan 19, 2007 5:43 pm   Post subject: Re: creating objects at run time?

I know that the code is not organized or good by any means. I wanted to test some things out with SDL so i just started writing a .cpp file and experimenting. Once I figure this last thing out I'll get to programming the actual game.

I commented the offending line. Without that line my ball displays, i click it, and click somewhere else to move it on the screen. Everything works fine. When I add the offending line, the cerr << "you clicked me" and cerr << "you clicked the screen" fill up the console and it ends with an SDL parachute and a segmentation fault. So somehow the mousebuttondown is registering when it shouldn't be in the new objects... But I think it's even deeper than that...

code:

/**
Program designed to move a ball around the screen
**/


#include <iostream>
#include <string>
#include <vector>
#include <SDL/SDL.h>

enum status {SUCCESS, ERROR};

using std::cerr;
using std::endl;

int draw_image(SDL_Surface * image,SDL_Surface * dest, int x, int y)
{
  SDL_Rect cords, fuck;
  cords.x = x;
  cords.y = y;
  SDL_FillRect(dest, NULL, 0x000000); //make the screen black.
  SDL_BlitSurface(image, NULL, dest, &cords);
  SDL_Flip(dest);
  return SUCCESS;
}

class SDL_Window
{
  private:
    SDL_Surface * pMainWindow;
  public:
    SDL_Window();
    ~SDL_Window() { delete pMainWindow; SDL_Quit(); }
    SDL_Surface * getSurface() const { return pMainWindow; }
   
};

SDL_Window::SDL_Window()
{
  if (SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0)
  {
    cerr << "Unable to init SDL: %s\n" << SDL_GetError();
    return;
  }
  pMainWindow = SDL_SetVideoMode(800, 600 ,16, SDL_HWSURFACE|SDL_HWPALETTE|SDL_DOUBLEBUF);
  if (pMainWindow == NULL)
  {
    cerr << "Unable to set %dx%d video resolution: %s\n" << SDL_GetError();
    return;
  }
}

class Color_Tac
{
  private:
    SDL_Surface * pSurface;
    SDL_Rect Cords;
    std::string picFile;
  public:
  Color_Tac(std::string file, int x, int y, int w)
    {
      picFile = file;
      pSurface = SDL_LoadBMP(file.c_str());
      Cords.x = x; 
      Cords.y = y;
      Cords.w = w;
    }
    ~Color_Tac()
    {}
  void handleEvents(SDL_Event&, bool&, std::vector<Color_Tac>& stockpile);
    void show(SDL_Surface*);
};

void Color_Tac::handleEvents(SDL_Event& event, bool& clicked_tac, std::vector<Color_Tac>& stockpile)
{
  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 + Cords.w) && mouse_x >= Cords.x && mouse_y <= (Cords.y + Cords.w) && mouse_y >= Cords.y)   
    {       
      cerr << "you clicked me." << endl;
      clicked_tac = true;
      return;
    }
    else if (clicked_tac == true)
    {
      cerr << "you clicked the screen" << endl;
      Color_Tac ball(picFile,Cords.x, Cords.y, Cords.w);
      stockpile.push_back(ball);  //OFFENDING LINE
      cerr << stockpile.size();
      Cords.x = (mouse_x - (Cords.w/2));
      Cords.y = (mouse_y - (Cords.w/2));
      clicked_tac = false;
    }
  }
  return;
}

void Color_Tac::show(SDL_Surface* pMainWindow)
{
   draw_image(pSurface, pMainWindow, Cords.x, Cords.y); 
   return;
}

 

int main()
{
  SDL_Window MainWindow;
  std::vector<Color_Tac> Ball_StockPile;
  Color_Tac ball("1.bmp",0,0,40);
  Ball_StockPile.push_back(ball);
  cerr << Ball_StockPile.size();
  bool not_done = true, clicked_tac = false;
  while (not_done)
  {
    SDL_Event event;
    while (SDL_PollEvent(&event) == true)
    {
      for (std::vector<Color_Tac>::iterator i(Ball_StockPile.begin()); i < Ball_StockPile.end(); i++)
        i->handleEvents(event, clicked_tac, Ball_StockPile);
      if (event.type == SDL_QUIT) 
        not_done = false;  //to exit program
    }
     for (std::vector<Color_Tac>::iterator i(Ball_StockPile.begin()); i < Ball_StockPile.end(); i++)
       i->show(MainWindow.getSurface());
  }

  return SUCCESS;
}
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: