DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

A Simple Rectangle Class For Any Application

10.27.2008
| 6819 views |
  • submit to reddit
            www.vasilisagames.com

     This is part of Alven Sprittle, a sprite animator coming soon
    from Vasilisa. However, this file does not exist under the same
    license. It has no license. You may use it at your leisure in any
    project. Polite nods to the original author are, however, most
    welcome, and I'd like to know about your project if you use the
    code. :)

     This simple class stores the x, y, width, and height of a virtual
    square which can be used for location purposes. It also provides a
    check to see if these attributes are located under the mouse. Private
    variables are not a good idea for classes that mainly act as "glue"
    for related data, so it's all public.

      The functions all use the scaler to shrink or expand the borders of 
    the rectangle( virtually ). This is nice for quickly making borders, 
    stretching images, and so forth, all without changing the actual values.
    It's easy enough to add your own methods if your like, but I'm leaving
    it as it was used in Alven.

     I designed this class to be useful for other applications. Please 
    remember that if you #include it in multiple files, you'll need to
    seperate the implementation from the definition ( if you don't know
    what that means, you shouldn't be working with multiple files yet! )

/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

  SizeRectangle class
	Author:   Joseph Austin ( jyo@vasilisagames.com )
	Created:  2008

   This code is public domain.


 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#ifndef VASILISA_SIZE_RECTANGLE_H
#define VASILISA_SIZE_RECTANGLE_H

class SizeRectangle
{
public:
    // The data
    int scaler;                 // Used to shrink / grow the rect (virtually)
    int x, y, width, height;    // Actual rectangle data

    // Constructor: All arguments are optional, but can be used for speedy
    //      construction. Scaler starts at 0.
    SizeRectangle( const int pos_x = 0, const int pos_y = 0,
                   const int pos_width = 0, const int pos_height = 0 )
    {
        setLoc( pos_x, pos_y );
        setSize( pos_width, pos_height );
        scaler = 0;
    }

    // setLoc: Mutates m_x, m_y
    void setLoc( const int pos_x, const int pos_y )
    {
        x = pos_x;
        y = pos_y;
    }

    // setSize: mutates m_width, m_height
    void setSize( const int pos_width, const int pos_height )
    {
        width = pos_width;
        height = pos_height;

        // Most libs don't like negatives here, such as Allegro, which
        // I used. This "flips" the rectangle if its boundaries extend
        // in the wrong direction. It is still essentially the same after
        // a flip, but it won't anger your computer.

        if( width < 0 ) {
            width *= -1;
            x -= width;
        }
        if( height < 0 ) {
            height *= -1;
            y -= height;
        }
        if( width == 0 )
            width = 1;
        if( height == 0 )
            height = 1;
    }

    // Special Accessors: these consider the 'scaled' variable.
    int getScaledX()      const       { return x - scaler; }
    int getScaledY()      const       { return y - scaler; }
    int getScaledWidth()  const       { return width  + ( scaler * 2 ); }
    int getScaledHeight() const       { return height + ( scaler * 2 ); }

    // isUnderMouse: Determines if the mouse is under the coordinates
    //      surrounded by this rectangle. This uses the scaler, so be
    //      sure that you want that considered.
    bool isUnderMouse() const
    {
        // The commented code is for allegro. It will serve as an example
        // in your own library, or, if you #include <allegro> with the SDK
        // installed, it ought to work. Of course, they are making drastic
        // changes to its API, so you may have to edit it anyway.

        //return ( mouse_x >= getScaledX() && mouse_y >= getScaledY() &&
        //         mouse_x <  getScaledX() + getScaledWidth() &&
        //         mouse_y <  getScaledY() + getScaledHeight() );

        return false;    // So it will compile, at least
    }
};

#endif