creating an array of rects

This is a discussion on creating an array of rects within the C++ Programming forums, part of the General Programming Boards category; lookng for a bit of help with a bit of my game im creating, i need to create an array ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    28

    creating an array of rects

    lookng for a bit of help with a bit of my game im creating, i need to create an array of rectngles (RECT)'s but i dont know how to im a java programmer just doing a bit of c++.

    this is what i have so far

    Code:
    RECT source[32];
    source[0] = {1, 19, 61, 40};
    am i along the right lines?

    cheers

    andrew

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    Yes, here's a example

    Code:
    #include <windows.h>
    int main ()
    {
         RECT source[32];
         source[0] = {1, 19, 61, 40}; // This is Illegal
         source[0].bottom = 15; //You must assing seperately
         source[0].left = 19; // because you can only use {values} when intitializing
         source[0].right = 2;
         source[0].top = 6;
         RECT source2 = {1, 2, 3, 4}; // This IS legal.
    }

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    28
    cheers thanks

  4. #4
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    you could also do:
    Code:
    RECT source[32] ={{1, 19, 61, 40},{1, 2,3, 4}, ... (32 times)};

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    28
    na that would look terrible and would be to hard to manage, would of been better having doing it like
    Code:
    source[0] = {1, 19, 61, 40}
    but the other way works now and is fine

    cheers anyway

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    They wouldn't all have to be on the same line.

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Split them onto multiple lines:
    Code:
    RECT Rect[] =
    {
    	{ 0,  0,  32, 32 },
    	{ 0,  32, 32, 64 },
    	{ 32, 0,  64, 32 },
    	{ 32, 32, 64, 64 },
    };
    Or perhaps make a RECT-building function (may come in handly later too):
    Code:
    RECT BuildRect(LONG Left, LONG Top, LONG Right, LONG Bottom)
    {
    	RECT Rect;
    
    	Rect.left = Left;
    	Rect.top = Top;
    	Rect.right = Right;
    	Rect.bottom = Bottom;
    
    	return Rect;
    }
    
    RECT Rect[4];
    
    Rect[0] = BuildRect(0,  0,  32, 32);
    Rect[1] = BuildRect(0,  32, 32, 64);
    Rect[2] = BuildRect(32, 0,  64, 32);
    Rect[3] = BuildRect(32, 32, 64, 64);
    Or, to remove the overhead of function calls, define a macro:
    Code:
    #define BUILD_RECT(Rect, Left, Top, Right, Bottom) \
    { \
    	(Rect).left = (Left); \
    	(Rect).top = (Top); \
    	(Rect).right = (Right); \
    	(Rect).bottom = (Bottom); \
    }
    
    RECT Rect[4];
    
    BUILD_RECT(Rect[0], 0,  0,  32, 32);
    BUILD_RECT(Rect[1], 0,  32, 32, 64);
    BUILD_RECT(Rect[2], 32, 0,  64, 32);
    BUILD_RECT(Rect[3], 32, 32, 64, 64);
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    28
    cheers magos, the idea of the macro looks good i might give that a go when i get it workin as it is now

  9. #9
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    don't use a macro. Wrap your RECT in a class. MFC already has one, but you can roll your own if you're not using MFC.
    Code:
    class CRect
    {
    public:
    	CRect(int top, int bottom, int left, int right)
    	{
    		m_rect.left = left;
    		m_rect.top = top; 
    		m_rect.right = right; 
    		m_rect.bottom = bottom;
    	}
    
    	// you can now get added functionality by implementing these methods
    	int Height() const;
    	int Width() const;
    	int Area()const;
    	// implementation left as an exercise for the reader :-)
    
    	// you can also add assignment, copy operators, intersections, anything
    private:
        RECT m_rect;
    };
    in C++ a class should generally be near the top of your list of potential solutions and a macro close to the bottom (in fact, just above "scream obscenities at the code" )
    "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. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 08:30 AM
  3. Creating a menu that reads input via an array?
    By Nalif in forum C Programming
    Replies: 6
    Last Post: 09-29-2006, 09:21 PM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM
  5. Creating an array of object pointers
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2001, 10:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21