diff options
Diffstat (limited to 'matchblox/C_Box.cpp')
-rw-r--r-- | matchblox/C_Box.cpp | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/matchblox/C_Box.cpp b/matchblox/C_Box.cpp new file mode 100644 index 0000000..9bde3ff --- /dev/null +++ b/matchblox/C_Box.cpp @@ -0,0 +1,111 @@ +#include <GL/glut.h> +#include <math.h> + +#include "C_Box.h" +#include "typedefs.h" + +C_Box::C_Box(const char *f_strFileName, + GLuint f_uiTexture, + MatProps_t f_Mat, + int f_iTilesX, int f_iTilesY, + C_3DObject *f_pTiles[5]) +: C_3DObject(f_strFileName, f_uiTexture, f_Mat), + m_pTiles(f_pTiles), m_iTilesX(f_iTilesX), + m_iTilesY(f_iTilesY) +{ + m_iNumTiles = m_iTilesX * m_iTilesY; + //create a new tiles layout array + m_iTilesLayout = new int[m_iNumTiles]; + + //calculate the tile size (this should be the same + //for all tile types) + BoundingBox_t l_BBox = m_pTiles[0]->GetBoundingBox(); + m_dTileSize = l_BBox.m_dRight - l_BBox.m_dLeft; + + //calculate the lower left tile position (in xz plane) + m_LowLeftTilePos = Vect3D_t(-m_dTileSize * (double)(m_iTilesX-1)/2.0, + 0.0, + m_dTileSize * (double)(m_iTilesY-1)/2.0); +} + +C_Box::~C_Box() +{ + delete [] m_iTilesLayout; +} + +void C_Box::RandomizeTiles() +{ + int l_iNumTiles = m_iTilesX * m_iTilesY; + + //first set the layout to all tiles with no hole + for (int i=0; i<l_iNumTiles; i++) + m_iTilesLayout[i] = 4; + + //add the four hole tiles + for (int i=0; i<4; i++) + { + //generate a random number in the range + //0 - (l_iNumTiles-1) + int pos = rand() % l_iNumTiles; + + //skip position in which we already inserted a hole + //type tile (type 4) + while (m_iTilesLayout[pos] != 4) + pos = (pos+1) % l_iNumTiles; + + //insert the hole tile in the free spot + m_iTilesLayout[pos] = i; + + //set the position for the hole tile relative to the box its position + int col = pos % m_iTilesX; + int row = pos / m_iTilesX; + + m_pTiles[i]->SetPos(m_LowLeftTilePos.x + (double)col * m_dTileSize, + 0.0, + m_LowLeftTilePos.z - (double)row * m_dTileSize); + } +} + +void C_Box::Render() +{ + // + glPushMatrix(); + + //apply transformation for box + TransRotateScale(); + + //render the box + C_3DObject::Render(); + + + //render the tiles + for(int x=0; x<m_iTilesX; x++) + for(int y=0; y<m_iTilesY; y++) + { + int l_iTileIndex = m_iTilesLayout[x + y*m_iTilesX]; + //check the tile + if (l_iTileIndex >= 0 && l_iTileIndex <= 3) + { + //tile with a hole + glPushMatrix(); + m_pTiles[l_iTileIndex]->TransRotateScale(); + m_pTiles[l_iTileIndex]->Render(); + glPopMatrix(); + } + else + { + //tile without a hole + //set the position + m_pTiles[4]->SetPos(m_LowLeftTilePos.x + (double)x * m_dTileSize, + 0.0, + m_LowLeftTilePos.z - (double)y * m_dTileSize); + glPushMatrix(); + m_pTiles[4]->TransRotateScale(); + m_pTiles[4]->Render(); + glPopMatrix(); + } + } + + glPopMatrix(); +} + |