summaryrefslogtreecommitdiffstats
path: root/matchblox/engine/C_Box.cpp
blob: 573c78cfef2c965dde8e9b4002fceb691c8465cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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_Max.x - l_BBox.m_Min.x;

  //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(GLint f_iTexLocation)
{
  //
  glPushMatrix();
  
  //apply transformation for box
  TransRotateScale();

  //render the box
  C_3DObject::Render(f_iTexLocation);


  //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(f_iTexLocation);
        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(f_iTexLocation);
        glPopMatrix();
      }
    }

  glPopMatrix();    
}