Thread: Vector Construction problem..

  1. #1
    Registered User SSJMetroid's Avatar
    Join Date
    Dec 2005
    Posts
    12

    Vector Construction problem..

    Ok so here's my problem:

    I'm making a little game project that's like a clone of Breakout, which I'm sure everyone has played before, and I encapsulated the game variables in its own class called "Game" that looks something like this...

    Code:
    //game.h
    //Game class header file
    #ifndef GAME_H
    #define GAME_H
    
    #include <vector>
    
    #include "player.h"
    #include "brick.h"
    #include "ball.h"
    
    using namespace std;
    
    class Game
    {
    public:
              Game(int player_x, int player_y, unsigned int player_w, unsigned int player_h, int player_lives,
                       int ball_x, int ball_y, unsigned int ball_w, unsigned int ball_h, double ball_XSpeed, double ball_YSpeed,
                       int brick_x, int brick_y, unsigned int brick_w, unsigned int brick_h, int brick_hp,
                       int level_width, int level_height);
              
              void Reset();
              void Play();
              
              Player player1;
              Ball ball;
              vector<vector<Brick> > bricks;
    };
    
    #endif
    Not the most efficient code on the planet, I KNOW. . -.-

    But! I'm having trouble creating the constructor of this Game class. As you can see, the "bricks" variable is a 2-dimensional vector. What I'm having trouble doing is creating a constructor that assigns two of the parameters "int level_width, int level_height" as the width and height dimensions of the vector.

    I hope I explained that fully. O_o

    Thanks for the help, and sorry in advance about all my constructor parameters - I want to keep it that way for certain reasons though. >.<

  2. #2
    Registered User SSJMetroid's Avatar
    Join Date
    Dec 2005
    Posts
    12
    Bah, this board is so slow with replys I had to reply to my own problem..

    I got a constructor working and I improved the "clenliness" of my Game class. It looks like this:

    Code:
    Game::Game(unsigned int player_w, unsigned int player_h, int player_lives,
                   unsigned int ball_w, unsigned int ball_h,
                   int brick_x, int brick_y, unsigned int brick_w, unsigned int brick_h, int brick_hp,
                   int rows, int columns):
          player1(0,0,player_w, player_h, player_lives),
          ball(0,0,ball_w, ball_h,0,0)
    {
          bricks.reserve(rows);
          for(int y = 0; y < rows; y++)
          {
                  bricks[y].reserve(columns);
                  for(int x = 0; x < columns; x++)
                  {
                          bricks[y][x].SetWidth(brick_w);
                          bricks[y][x].SetHeight(brick_h);
                          bricks[y][x].SetHP(brick_hp);
                          bricks[y][x].SetX( brick_x + (brick_w * x) );
                          bricks[y][x].SetY( brick_y + (brick_h * y) );
                  }
          }
          
          Reset();
    }
    I compiled it and everything is running great. ^^

    Although it would still help if other people showed me an example of theirs.

    Edit: notice the time of my posts, it took me 4 freaking hours to do that. T_T

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
                          bricks[y][x].SetWidth(brick_w);
                          bricks[y][x].SetHeight(brick_h);
                          bricks[y][x].SetHP(brick_hp);
                          bricks[y][x].SetX( brick_x + (brick_w * x) );
                          bricks[y][x].SetY( brick_y + (brick_h * y) );
    Does every brick really need to know it's own width, height, and position in the grid?

  4. #4
    Registered User SSJMetroid's Avatar
    Join Date
    Dec 2005
    Posts
    12
    Woah you're right.. Thanks for pointing that pitfall out to me!

    Edit: Dude holy crap that was useful.. my code for the Brick class looked like this:

    Code:
    //brick.h
    //Brick class header file
    #ifndef BRICK_H
    #define BRICK_H
    
    #include "object.h"
    
    class Brick : public Object
    {
    public:
                Brick(int x, int y, unsigned int w, unsigned int h, int new_hp);
                void SetHP(int num);
                int GetHP();
    protected:
                
    private:
                int hp;
    };
    
    #endif
    Hauhahahah, all I had to do was NOT inherit the abstract "Object" class.
    Last edited by SSJMetroid; 10-09-2006 at 08:23 PM.

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
     bricks.reserve(rows);
          for(int y = 0; y < rows; y++)
          {
                  bricks[y].reserve(columns);
                  for(int x = 0; x < columns; x++)
                  {
                          bricks[y][x].SetWidth(brick_w);
                          bricks[y][x].SetHeight(brick_h);
                          bricks[y][x].SetHP(brick_hp);
                          bricks[y][x].SetX( brick_x + (brick_w * x) );
                          bricks[y][x].SetY( brick_y + (brick_h * y) );
                  }
          }
    Calling reserve does not make those indices valid. It may work, but it is not standard (try using the at() member function instead, and you'll likely get an out_of_range exception). Instead of reserve(), use resize()
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Registered User SSJMetroid's Avatar
    Join Date
    Dec 2005
    Posts
    12
    Thank you for the reply dude.

    I'll look into using resize() instead and see if anything is different..

    Edit: Ok I tried using resize(), so the constructor looks like this now...

    Code:
    Game::Game(unsigned int player_w, unsigned int player_h, int player_lives,
                   unsigned int ball_w, unsigned int ball_h,
                   int brick_x, int brick_y, int brick_hp,
                   int rows, int columns):
          player1(0,0,player_w, player_h, player_lives),
          ball(0,0,ball_w, ball_h,0,0)
    {
          bricks.resize(rows);
          for(int y = 0; y < rows; y++)
          {
                  bricks[y].resize(columns);
                  for(int x = 0; x < columns; x++)
                  {
                          bricks[y][x].SetHP(brick_hp);
                  }
          }
          
          Reset();
    }
    But now the compiler is pumping out an error message saying like "no matching function for call to `Brick::Brick()' "... I'm using Dev-C++ for this project. Is it just because my syntax is wrong? When I used Reserve() it worked just fine... =/
    Last edited by SSJMetroid; 10-09-2006 at 08:54 PM.

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Hmm... I would still use reserve(). resize() is an expensive operation. And even more so if done repeatedly inside a loop. The only thing you need is to then push_back() to insert a new element.

    Anyways... the error you are getting now is because you probably haven't defined a default constructor. resize() creates an element automagically. For it to work there must be a default constructor.

    However, I suggest you go back to reserve(). Then on your class Brick class create a default constructor. Then inside those loops simply start the loop with a bricks.push_back().

    We can help you further if you provide us with the Brick class definition

    EDIT: This thread would be better on the C++ board
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Registered User SSJMetroid's Avatar
    Join Date
    Dec 2005
    Posts
    12
    I agree with you, I really should have posted on the regular C++ board. heheh..

    But yeah, I'll just stick with reserve(). It seems better and it works in my constructor, so I'm lazy and I don't want to change much code.

    Thanks for your input, here's all the Brick class is now...

    Code:
    //brick.h
    //Brick class header file
    #ifndef BRICK_H
    #define BRICK_H
    
    class Brick
    {
    public:
                Brick(int new_hp);
                void SetHP(int num);
                int GetHP();
    protected:
                
    private:
                int hp;
    };
    
    #endif

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    <<on behalf of Mario F., I believe this would go here>>
    Quote Originally Posted by SSJMetroid
    I agree with you, I really should have posted on the regular C++ board. heheh..
    M'kay. <<moved>>
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User SSJMetroid's Avatar
    Join Date
    Dec 2005
    Posts
    12
    Thanks much.

  11. #11
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    If this is a breakout clone you should already know how many bricks are on the board. If this is true then a simple dynamic array would work just fine. To remove a brick from the array you would need to take the ball x coord and ball y coord and divide it by the brick width and brick height. Then you find the offset by doing row*gridwidth+column.

    Code:
    void CGame::FindBrickOffset(int x,int y)
    {
      int row=y/m_iBrickHeight;
      int col=x/m_iBrickWidth;
    
      return row*m_iBoardWidth+col;
    }
    Set the brick offset to 0 and then in your render loop if the current offset's value is 0...you don't draw anything. Since you are drawing from memory when you change the memory value it is immediately reflected on the screen.
    To do borders you don't draw anything on the edges of the bricks.

  12. #12
    Registered User SSJMetroid's Avatar
    Join Date
    Dec 2005
    Posts
    12
    Hey Bubba, thanks for the reply!

    I'll start by saying: I never even thought of using a dynamic array.. mostly because I've never used them before. I'll have to look more into that juicy goodness, hahah. It's these kinds of things that self-taught C++ noobies like me seem to miss..

    If it's more efficient, then I should definitly use it, but I was making this project so I could get used to vectors specifically. XD I do appreciate the input though, thanks!

    Perhaps I'll do something easy after I finish this to practice dynamic arrays..

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You can do the same thing with a vector, just make it a one-dimensional vector and use the same technique.

  14. #14
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    I've yet to encounter a situation where a dynamic array
    i.e.
    Code:
    SomeClass* array = new SomeClass[size];
    is a better design choice than a vector. All you gain from the dynamic array are memory leak and exception safety problems.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM