summaryrefslogtreecommitdiffstats
path: root/matchblox/engine/C_MatchBloxEngine.cpp
blob: 52ffde93a187f0e53847dc147871760fb4dce260 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include <GL/glut.h>

#include <time.h>
#include <string>
#include <iostream>

#include "C_MatchBloxEngine.h"

#include "C_3DObject.h"
#include "C_Environment.h"
#include "C_Hand.h"
#include "C_Block.h"
#include "C_Box.h"
#include "C_Log.h"
#include "bitmap.h"

C_MatchBloxEngine::C_MatchBloxEngine(const char *f_strModelPath,
                                     const char *f_strLogFile)
{
  //create logger
  
  
  //Load models
  if (LoadModels(f_strModelPath))
  {
    //set state to initialised;
    m_State = ES_INITIALISED;
  }
  else
  {
    m_State = ES_ERROR;
  }

  //initialise a random seed
  srand ( time(NULL) );

  //init vars
  m_CurrentBox = BS_SMALL;

  //debug
  m_pBox[0]->SetPos(0.0, -5.0, -15.0);
  m_pBox[0]->SetRot(45.0, 0.0, 0.0);
  m_pBox[0]->RandomizeTiles();

  m_pBlock[0]->SetPos(-6.0, 1.0, -10.0);
  m_pBlock[0]->SetRot(90.0, 0.0, 0.0);
  m_pBlock[1]->SetPos(-2.0, 1.0, -10.0);
  m_pBlock[1]->SetRot(90.0, 0.0, 0.0);
  m_pBlock[2]->SetPos(2.0, 1.0, -10.0);
  m_pBlock[2]->SetRot(90.0, 0.0, 0.0);
  m_pBlock[3]->SetPos(6.0, 1.0, -10.0);
  m_pBlock[3]->SetRot(90.0, 0.0, 0.0);
}

C_MatchBloxEngine::~C_MatchBloxEngine()
{
  //destroy logger
  
  //delete models
  DeleteModels();
}

GameResult C_MatchBloxEngine::GameStep(void)
{
  //process message queue
  return GR_ERROR;
}

void C_MatchBloxEngine::Render_Basics(unsigned int f_uiElapsedTime)
{
  //render the environment
  m_pEnvMap->Render();

  m_pBox[(intptr_t)m_CurrentBox]->Render();
  for (int i=0; i<4; i++)
    m_pBlock[i]->Render(f_uiElapsedTime);
//  m_pHand->Render(f_uiElapsedTime);
}

void C_MatchBloxEngine::Render(unsigned int f_uiElapsedTime)
{
  switch (m_State)
  {
  case ES_INITIALISED:
    Render_Basics(f_uiElapsedTime);
  break;

  case ES_ERROR:
    //render a red cube
    glPushMatrix();
    glPushAttrib(GL_ENABLE_BIT);
    glDisable(GL_LIGHTING);
    glColor3d(1.0, 0.0, 0.0);
    glTranslated(0.0, 0.0, -5.0);
    glutSolidCube(5.0);
    glPopAttrib();
    glPopMatrix();
  break;

  case ES_GET_READY:
    Render_Basics(f_uiElapsedTime);
    //render some GET READY text
  break;

  case ES_PLAYING_GRAB_BLOCK:
    Render_Basics(f_uiElapsedTime);
  break;

  case ES_PLAYING_PUT_BLOCK:
    Render_Basics(f_uiElapsedTime);
  break;

  case ES_PAUSED:
    Render_Basics(f_uiElapsedTime);
    //render menu??
  break;

  case ES_FINISHED:
    //render results...
  break;
  }

  //glPushMatrix();

  //double l_dSeconds = (double)f_uiElapsedTime/1000.0;

  //glRotated(l_dSeconds * 5.0, 0.0, 1.0, 0.0);
  ////glRotated(l_dSeconds * 10, 0.0, 0.0, 1.0);

  //m_pEnvMap->Render();

  //glPopMatrix();

  //m_pBox[0]->Render();

  //for(int i=0; i<4; i++)
  //{
  //   glPushMatrix();
  //   m_pBlock[i]->Render(f_uiElapsedTime);
  //   glPopMatrix();
  //}

}

bool C_MatchBloxEngine::NewGame(int f_iUserID, int f_iGameId, BoxSize f_BS)
{
  if(m_State == ES_INITIALISED)
  {
    //log new game

    //prepare a fresh box
    m_CurrentBox = f_BS;
    m_pBox[(int)m_CurrentBox]->RandomizeTiles();

    //set state to GET READY
    m_State = ES_GET_READY; 
  
    //init object positions
    m_pBox[(int)m_CurrentBox]->SetPos(0.0, -6.0, -15.0);
    return true;
  }

  return false;
}

bool C_MatchBloxEngine::StartGame()
{
  return false;
}

bool C_MatchBloxEngine::Pause()
{
  //only pause when playing
  if (m_State == ES_PLAYING_GRAB_BLOCK ||
      m_State == ES_PLAYING_PUT_BLOCK)
  {
    //save current state
    m_SavedState = m_State;

    //probably do something with a time variable

    //set current state to paused
    m_State = ES_PAUSED;

    return true;    
  }

  return false;
}

bool C_MatchBloxEngine::Resume()
{
  if (m_State == ES_PAUSED)
  {
    //restore previous state
    m_State = m_SavedState;

    //restore timers
    //

    return true;
  }
  return false;
}

bool C_MatchBloxEngine::Abort()
{
  //abort when not in error or init state
  if (m_State != ES_ERROR && m_State != ES_INITIALISED)
  {
    //set state to initialised
    m_State = ES_INITIALISED;

    //..

    return true;
  }
  return false;
}

bool C_MatchBloxEngine::LoadModels(const char* f_strModelDir)
{
  MatProps_t   l_Mat;
  std::string l_BaseName = f_strModelDir;

  //create the environment mapped cube
  m_pEnvMap = new C_Environment("envmaps/terrain_", 50.0);

  //load the bitmaps for the textures
  LoadTexture((l_BaseName + "/wood1.bmp").c_str(), m_uiWood1Tex);
  LoadTexture((l_BaseName + "/wood2.bmp").c_str(), m_uiWood2Tex);
  LoadTexture((l_BaseName + "/wood3.bmp").c_str(), m_uiWood3Tex);

  //load the block models
  //red squares
  l_Mat.setAmb(1.0, 0.0, 0.0, 1.0);
  l_Mat.setDif(1.0, 0.0, 0.0, 1.0);
  m_pBlock[BT_SQUARE] = new C_Block((l_BaseName + "/square.obj").c_str(),
                                    m_uiWood1Tex, l_Mat);
  if (!m_pBlock[BT_SQUARE]->Initialized()) return false;

  //yellow cricles
  l_Mat.setAmb(0.0, 1.0, 1.0, 1.0);
  l_Mat.setDif(0.0, 1.0, 1.0, 1.0);
  m_pBlock[BT_CIRCLE] = new C_Block((l_BaseName + "/circle.obj").c_str(),
                                    m_uiWood1Tex, l_Mat);
  if (!m_pBlock[BT_CIRCLE]->Initialized()) return false;

  //green triangles
  l_Mat.setAmb(0.0, 1.0, 0.0, 1.0);
  l_Mat.setDif(0.0, 1.0, 0.0, 1.0);
  m_pBlock[BT_TRIANGLE] = new C_Block((l_BaseName + "/triangle.obj").c_str(),
                                    m_uiWood1Tex, l_Mat);
  if (!m_pBlock[BT_TRIANGLE]->Initialized()) return false;

  //blue crosses
  l_Mat.setAmb(0.0, 0.0, 1.0, 1.0);
  l_Mat.setDif(0.0, 0.0, 1.0, 1.0);
  m_pBlock[BT_CROSS] = new C_Block((l_BaseName + "/cross.obj").c_str(),
                                    m_uiWood1Tex, l_Mat);
  if (!m_pBlock[BT_CROSS]->Initialized()) return false;

  
  //load the hand???
  
  
  //Load the box tiles
  l_Mat.setAmb(1.0, 1.0, 1.0, 1.0);
  l_Mat.setDif(1.0, 1.0, 1.0, 1.0);
  
  m_pTiles[BT_SQUARE]   = new C_3DObject((l_BaseName + "/tile_square.obj").c_str(),
                                         m_uiWood3Tex, l_Mat);
  if (!m_pTiles[BT_SQUARE]->Initialized()) return false;

  m_pTiles[BT_CIRCLE]   = new C_3DObject((l_BaseName + "/tile_circle.obj").c_str(),
                                         m_uiWood3Tex, l_Mat);
  if (!m_pTiles[BT_CIRCLE]->Initialized()) return false;

  m_pTiles[BT_TRIANGLE] = new C_3DObject((l_BaseName + "/tile_triangle.obj").c_str(),
                                         m_uiWood3Tex, l_Mat);
  if (!m_pTiles[BT_TRIANGLE]->Initialized()) return false;

  m_pTiles[BT_CROSS]   = new C_3DObject((l_BaseName + "/tile_cross.obj").c_str(),
                                         m_uiWood3Tex, l_Mat);
  if (!m_pTiles[BT_CROSS]->Initialized()) return false;

  m_pTiles[4]          = new C_3DObject((l_BaseName + "/tile_no_hole.obj").c_str(),
                                         m_uiWood3Tex, l_Mat);
  if (!m_pTiles[4]->Initialized()) return false;

  
  //Load the box models
  m_pBox[0] = new C_Box((l_BaseName + "/box_small.obj").c_str(),
                        m_uiWood2Tex, l_Mat, 2, 2, m_pTiles);
  if (!m_pBox[0]->Initialized()) return false;

  m_pBox[1] = new C_Box((l_BaseName + "/box_med.obj").c_str(),
                        m_uiWood2Tex, l_Mat, 4, 2, m_pTiles);
  if (!m_pBox[1]->Initialized()) return false;

  m_pBox[2] = new C_Box((l_BaseName + "/box_large.obj").c_str(),
                        m_uiWood2Tex, l_Mat, 4, 4, m_pTiles);
  if (!m_pBox[2]->Initialized()) return false;

  
  return true;
}

void C_MatchBloxEngine::DeleteModels()
{
  //delete objects
  delete m_pEnvMap;
  delete m_pBlock[0];
  delete m_pBlock[1];
  delete m_pBlock[2];
  delete m_pBlock[3];
  //delete m_pHand; 
  delete m_pBox[0];
  delete m_pBox[1];
  delete m_pBox[2];
  delete m_pTiles[0];
  delete m_pTiles[1];
  delete m_pTiles[2];
  delete m_pTiles[3];
  delete m_pTiles[4];

  //delete textures
  glDeleteTextures(1, &m_uiWood1Tex);
  glDeleteTextures(1, &m_uiWood2Tex);
  glDeleteTextures(1, &m_uiWood3Tex);
}

void C_MatchBloxEngine::LoadTexture(const char* f_BmpName, GLuint &f_uiTexHandle)
{
  BitmapStruct l_Bmp;

  l_Bmp = BitmapLoad((char*)f_BmpName);
  f_uiTexHandle = (GLuint)l_Bmp.m_iImageId;
  
  glBindTexture(GL_TEXTURE_2D, f_uiTexHandle);

  //set the texture paramaters
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}