summaryrefslogtreecommitdiffstats
path: root/matchblox/main.cpp
blob: 3e54784bc5d12a972b3da8fc01bed6ff0fe69304 (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
#ifdef G_OS_WIN32
  #define WIN32_LEAN_AND_MEAN 1
  #include <windows.h>
  #define sleep Sleep
#else
  #define FALSE 0
  #define TRUE !FALSE
  #include <unistd.h>
#endif

#include <GL/glew.h>
#include <GL/glut.h>
#include <iostream>

#include <math.h>

#include "typedefs.h"
#include "menu.h"
#include "textfile.h"

#include "C_MatchBloxEngine.h"
#include "wiimote_utils.h"

#define SCREEN_WIDTH  640
#define SCREEN_HEIGHT 480

#define KEY_ESCAPE 27

GameState_t  g_GameState;
C_MatchBloxEngine *g_pEngine;

bool InitWiiMotes()
{
  //connect to the wiimote(s)

  //create an abstract wiimote interface 
#ifdef USE_WIIYOURSELF
  g_GameState.m_pWiimote[0] = new WiiYourselfWiimote();
#endif //USE_WIIYOURSELF

  printf("connecting:\n");
  int retries = 10;
  while(!g_GameState.m_pWiimote[0]->Connect() && retries > 0)
  {
    sleep(100);
    printf(".");
    //retries--;
  }
  
  if (g_GameState.m_pWiimote[0]->IsConnected())
  {
    printf("connected\n");

    g_GameState.m_pWiimote[0]->SetLeds(0x0f);
    
    g_GameState.m_pWiimote[0]->StartRumble();  
    sleep(500);
    g_GameState.m_pWiimote[0]->StopRumble();

    return true;
  }
  else
  {
    return false;
  }
}

bool LoadGreyScaleShader()
{
  GLint l_bStatus;
  char *vs_text = NULL,*fs_text = NULL;
  GLuint  vs = glCreateShader(GL_VERTEX_SHADER),
	        fs = glCreateShader(GL_FRAGMENT_SHADER);
	
	vs_text = textFileRead("shaders\\greyscale.vert");
  if (vs_text == NULL)
  {
    return false;
  }
	fs_text = textFileRead("shaders\\greyscale.frag");
  if (fs_text == NULL)
  {
    free(vs_text);
    return false;
  }

	glShaderSource(vs, 1, (const char**)&vs_text,NULL);
	glShaderSource(fs, 1, (const char**)&fs_text,NULL);
	
	free(vs_text);
  free(fs_text);

	glCompileShader(vs);
//  printShaderInfoLog(vs);
  glGetShaderiv(vs, GL_COMPILE_STATUS, &l_bStatus);
  if (l_bStatus == GL_FALSE)
  {
    return false;
  }

	glCompileShader(fs);
//  printShaderInfoLog(fs);
  glGetShaderiv(fs, GL_COMPILE_STATUS, &l_bStatus);
  if (l_bStatus == GL_FALSE)
  {
    return false;
  }
	
  g_GameState.m_GreyScaleShaderProgram = glCreateProgram();
	glAttachShader(g_GameState.m_GreyScaleShaderProgram,fs);
	glAttachShader(g_GameState.m_GreyScaleShaderProgram,vs);

	glLinkProgram(g_GameState.m_GreyScaleShaderProgram);
//  printProgramInfoLog(g_GameState.m_GreyScaleShaderProgram);
  glGetProgramiv(g_GameState.m_GreyScaleShaderProgram, GL_LINK_STATUS, &l_bStatus);
  if (l_bStatus == GL_FALSE)
  {    
    return false;
  }

  g_GameState.m_GSConvScaleLoc = glGetUniformLocation(g_GameState.m_GreyScaleShaderProgram, "g_ConversionWeights");

  return true;
}

void init_gl(void)
{
  GLfloat l_fLightpos[4] = {0.0, 1.0, 1.0, 0.0};
  glClearColor(1, 0.4, 0.2, 1.0);
  glClearDepth(1000.0);
  glShadeModel(GL_SMOOTH);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
  glLightfv(GL_LIGHT0, GL_POSITION, l_fLightpos);
  glEnable(GL_NORMALIZE);
  glDisable(GL_TEXTURE_2D);
  
  glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
} // init_gl

void idle_func(void)
{
  struct input_payload_wiimote wiimote_payload;
  struct messageq_s message;

  g_GameState.m_pWiimote[0]->FillWiimoteMsgPayload(wiimote_payload, 205.0); // << should be a sensor bar led distance
  
  message.recipient = MESSAGE_MENU | MESSAGE_RENDERER;
  message.sender = MESSAGE_INPUT_WIIMOTE;
  message.payload = &wiimote_payload;
  message.payload_size = sizeof(input_payload_wiimote);
  messageq_send(&message);

  g_pEngine->ProcessMsgs();

  //call engine idle func
  MenuProcessMessage();

  glutPostRedisplay();
}

void render_scene(void)
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  g_pEngine->Render(glutGet(GLUT_ELAPSED_TIME));

  glDisable(GL_LIGHTING);
  glPushMatrix();
  MenuRender();
  glPopMatrix();
  glEnable(GL_LIGHTING);

  glutSwapBuffers();

} // render_scene

void reshape(int w, int h)
{
  //set the new viewport dimension
  glViewport(0, 0, w, h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(90.0, ((GLdouble)h)/((GLdouble)w), 0.5, 100.0);
  glTranslated(0.0, 0.0, -0.5);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

void process_normal_keys(unsigned char key, int x, int y)
{
  struct messageq_s message;
  struct input_payload_keyboard payload;

  message.recipient = MESSAGE_MENU | MESSAGE_RENDERER;
  message.sender = MESSAGE_INPUT_KEYBOARD;

  switch (key)
  {
    case KEY_ESCAPE:
      exit(0);
    break;
    case 'n':
      g_pEngine->Abort();
      g_pEngine->NewGame(0, 0, BS_SMALL);
    break;
    case 'p':
      if (!g_pEngine->Pause())
      {
        g_pEngine->Resume();
      }
      break;
  }

  payload.specialkey = FALSE;
  payload.key = (int)key;
  payload.modifier = glutGetModifiers();
  payload.x = x;
  payload.y = y;

  message.payload = (struct input_payload_keyboard *)&payload;
  message.payload_size = sizeof(payload);
  messageq_send(&message);
} // process_normal_keys



void process_special_keys(int key, int x, int y)
{
  switch (key)
  {
	  // do sumting
    case 0:
    default:
      break;
  }


} // process_special_keys



void process_mouse(int button, int state, int x, int y)
{
  MenuMouseClick(button, state, x, y);
} // process_mouse



void process_passive_mouse(int x, int y)
{
  MenuMouseMove(x, y);
} // process_passive_mouse



int main(int argc, char **argv)
{
  messageq_init();

  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  glutInitWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT);
  glutCreateWindow("MatchBlox");

  glutReshapeFunc(reshape);
  glutDisplayFunc(render_scene);
  glutIdleFunc(idle_func);
  glutKeyboardFunc(process_normal_keys);
  glutSpecialFunc(process_special_keys);
  glutMouseFunc(process_mouse);
  glutPassiveMotionFunc(process_passive_mouse);
  glutSetCursor(GLUT_CURSOR_NONE);  

  init_gl();
  MenuInit(SCREEN_WIDTH, SCREEN_HEIGHT);
  InitWiiMotes();

  GameSettings l_set = {10, 1.1, 2};
  g_pEngine = new C_MatchBloxEngine("models", "", l_set);
  
  //glutFullScreen();

  glutMainLoop();

  return 0;

} // main