⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 map_of.hpp

📁 The goal of this project is to explore the idea of point-based radiosity, which is a shooting radio
💻 HPP
字号:
#if !defined(BL_MAP_OF_HPP)
#define BL_MAP_OF_HPP
/*+-------------------------------------------------------------------
  Ben Landon
  CSCI E-235
  Semester Project

  Map template

  These maps are not growable.  Their size is fixed at construction
  time.  This is a 2D grid template, which is useful for all kinds
  of stuff.

  Map-of is stored in row major order in an array representation.

*/

#include <stddef.h>
#include <assert.h>

template<class T> class Map_of
{
private:
    unsigned int m_width;
    unsigned int m_height;
  
protected:
    // Needed to compute platform bitmap/pixmap sizes	
    unsigned int rep_size_in_bytes (void);
    T* m_rep;

public:
    // Construct a Map_of with the specified dimensions
    Map_of (unsigned int width, unsigned int height);

    // Destroy a Map_of and delete the array representation.
    // This does NOT call delete on the items in the
    // array, so if they have to be explicitly destroyed,
    // it is up to the calling code to do this.
    ~Map_of ();

    // Set a value in the Map_of at the cell specified
    // by (x, y).  As is typicall in C++, x and y are 
    // zero based. 
    void set (T value, unsigned int x, unsigned int y);

    // Set the value in a Map_of cell specified by 
    // the given offset.  Offset is y * width + x
    // since the Map_of is stored in row-major order.
    // Client code should NOT depend on the Map_of
    // template using row-major order for all time. 
    void set_offset (T value, unsigned int offset);
    
    // Get the value in a Map_of cell specified by 
    // the coordinates (x, y).
    T get (unsigned int x, unsigned int y); 

    // Get the value in a cell at a given offset.
    T get_offset (unsigned int offset);
   
    // Const versions of get/get_offset.
    const T get (unsigned int x, unsigned int y) const; 
    const T get_offset (unsigned int offset) const;
  
    inline unsigned int width (void) const { return m_width; }
    inline unsigned int height (void) const { return m_height; }
    inline unsigned int bits_per_element (void) const 
    { return (8 * sizeof(T)); }
    
    inline unsigned int xy_to_offset (unsigned int x, unsigned int y ) const 
    {return (y * m_width + x); }

    // Sets the value of all cells in the Map_of to be 
    // the value given by clear_value.
    void clear (T clear_value);
};

template<class T> Map_of<T>::Map_of<T> (unsigned int width, unsigned int height)
    : m_width(width), m_height(height)
{
    unsigned int size = m_width * m_height;
    m_rep = new T[size];
}

template<class T> Map_of<T>::~Map_of<T> ()
{
    if (this->m_rep)
    {
	delete [] m_rep;
    }
    m_rep = NULL;
    m_width = 0;
    m_height = 0;
}

// Needed to compute platform bitmap/pixmap sizes	
template<class T> unsigned int Map_of<T>::rep_size_in_bytes (void)
{
    return (m_width * m_height * sizeof(T));
}

template<class T> void Map_of<T>::set (T value, unsigned int x, unsigned int y)
{
    // These checks on legal values for 
    // x and y should be changed to asserts
    // or throws once I tighten up the clipping
    // rules.  The current problem is what is
    // the right thing if a vertex is right on
    // the clipping boundary.

    if (x < 0)
	return;
    
    if (x >= m_width)
	return;

    if (y < 0)
	return;

    if (y >= m_height)
	return;

  // Map_of is in row major format.
  // Recall that the parameters are x and y, not row and column.

    unsigned int element = m_width * y + x;
    m_rep[element] = value;
}

template<class T> void Map_of<T>::set_offset (T value, unsigned int offset)
{
    unsigned int num_elements = m_width * m_height;

    // Change these checks on legal values of offset to 
    // asserts or throws once clipping is tightened up.
    // See the comments in Map_of<T>::set

    if (offset < 0)
	return;

    if (offset >= num_elements)
	return;

    m_rep[offset] = value;
}

template<class T> T Map_of<T>::get (unsigned int x, unsigned int y)
{
    if ((x < 0) || (x >= m_width))
	return (T)0;
   
    if ((y < 0) || (y >= m_height))
	return (T)0;

    // Map_of is in row major format.
    // The parameters are x and y, not row and column.
    unsigned int element = m_width * y + x;
    return m_rep[element];
}

template<class T> T Map_of<T>::get_offset (unsigned int offset)
{
    unsigned int num_elements = m_width * m_height;

    if (offset < 0 || offset >= num_elements)
	return (T)0;

    return m_rep[offset];
}


template<class T> const T Map_of<T>::get (unsigned int x, unsigned int y) const
{
    if ((x < 0) || (x >= m_width))
	return (T)0;
   
    if ((y < 0) || (y >= m_height))
	return (T)0;

    // Map_of is in row major format.
    // The parameters are x and y, not row and column.
    unsigned int element = m_width * y + x;
    return m_rep[element];
}

template <class T> const T Map_of<T>::get_offset (unsigned int offset) const
{
    unsigned int num_elements = m_width * m_height;

    if (offset < 0 || offset >= num_elements)
	return (T)0;

    return m_rep[offset];

}

template<class T> void Map_of<T>::clear (T clear_value)
{
    for (unsigned int j = 0; j < m_height; j++)
    {
	for (unsigned int i = 0; i < m_width; i++)
	{
	    unsigned int element = m_width * j + i;
	    m_rep[element] = clear_value;
	}
    }
}

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -