00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00027
00028
00029
00030
00031
00032
00033
00034 #include <GL/glut.h>
00035
00036
00037
00038
00039
00040
00041 #include <GLICT/container.h>
00042 #include <GLICT/button.h>
00043 #include <GLICT/globals.h>
00044 #include <GLICT/window.h>
00045 #include <GLICT/panel.h>
00046 #include <GLICT/messagebox.h>
00047 #include <GLICT/fonts.h>
00048 #include "glut-helper.h"
00049
00050 char cardtitles[16][16] = { "Gecko", "Smygflik", "mips", "the fike", "Pekay", "Yorick", "tliff", "SimOne"};
00051
00052 float cardcolor[16][3] = { { 1., 0., 0.}, { 0., 0., .9 }, {.8, .8, 0.}, {.7, 0., .7},
00053 {0., .85, 0.}, { .55, 0., 0. }, {0., 75., .75}, {.5, 1., .5}};
00054
00055 char matrix[16];
00056 char taken[16];
00057 char solved[16];
00058 char flipd[2];
00059
00060
00061 int totalsolved = 0, totalopens = 0;
00062
00063 glictContainer desktop;
00064 glictWindow window;
00065 glictButton cards[16];
00066 glictPanel pnlSolveds;
00067 glictMessageBox* msgSuccess;
00068 int windowhandle;
00069
00070
00071 void display() {
00072
00073
00074
00075 glClearColor(0.0,0.0,0.0,0.0);
00076 glClear(GL_COLOR_BUFFER_BIT);
00077
00078
00079
00080
00081
00082 glEnable(GL_STENCIL_TEST);
00083 glDisable(GL_DEPTH_TEST);
00084
00085
00086 glMatrixMode(GL_MODELVIEW);
00087
00088
00089
00090 glLoadIdentity();
00091
00092
00093 desktop.Paint();
00094
00095
00096
00097
00098
00099
00100
00101
00102 glDisable(GL_STENCIL_TEST);
00103
00104
00105 glutSwapBuffers();
00106 }
00107
00108
00109 void reshape(int x, int y) {
00110 glMatrixMode(GL_PROJECTION);
00111 glLoadIdentity();
00112 glViewport(0,0,x,y);
00113 gluOrtho2D(0,x,0,y);
00114
00115
00116
00117
00118 glRotatef(180., 1., 0., 0.);
00119 glTranslatef(0,-y,0.0);
00120
00121 glMatrixMode(GL_MODELVIEW);
00122
00123
00124
00125 desktop.SetWidth(x);
00126 desktop.SetHeight(y);
00127
00128 window.SetWidth(96*4);
00129 window.SetHeight(96*4);
00130
00131 window.SetPos(x/2-96*2, y/2-96*2);
00132
00133
00134 glutPostRedisplay();
00135 }
00136
00137
00138 void mouse(int button, int shift, int mousex, int mousey) {
00139 glictPos pos;
00140 pos.x = mousex; pos.y = mousey;
00141
00142 if (shift==GLUT_DOWN) desktop.CastEvent(GLICT_MOUSEDOWN, &pos, 0);
00143 if (shift==GLUT_UP) desktop.CastEvent(GLICT_MOUSEUP, &pos, 0);
00144 glutPostRedisplay();
00145 }
00146
00147
00148
00149
00150 void OnDismissSuccess (glictPos* relmousepos, glictContainer* caller) {
00151 delete caller;
00152 }
00153
00154
00155
00156
00157 void CardOnClick(glictPos* relmousepos, glictContainer* callerclass) {
00158 glictButton *button = dynamic_cast<glictButton*>(callerclass);
00159 int cardclicked = button - cards;
00160
00161
00162
00163
00164 glutSetWindowTitle("GLICTMemory");
00165
00166
00167 if (flipd[1]!=-1 ) {
00168
00169 if (!solved[flipd[0]]) {
00170 char tmp[16];
00171 sprintf(tmp, "%d", flipd[0]); cards[flipd[0]].SetCaption(tmp); cards[flipd[0]].SetBGColor(0.5, 0.5, 0.5, 1.);
00172 sprintf(tmp, "%d", flipd[1]); cards[flipd[1]].SetCaption(tmp); cards[flipd[1]].SetBGColor(0.5, 0.5, 0.5, 1.);
00173
00174 }
00175 flipd[0]=-1;
00176 flipd[1]=-1;
00177
00178 }
00179
00180 if (solved[cardclicked]) {
00181 glutSetWindowTitle("GLICTMemory - Card's been solved already.");
00182 return;
00183 }
00184
00185 if (flipd[0]!=-1 && cardclicked==flipd[0]) {
00186 return;
00187 }
00188
00189
00190 button->SetCaption(cardtitles[matrix[cardclicked]]);
00191 button->SetBGColor(cardcolor[matrix[cardclicked]][0], cardcolor[matrix[cardclicked]][1], cardcolor[matrix[cardclicked]][2], 1.);
00192 if (flipd[0]==-1) flipd[0]=cardclicked; else {
00193 flipd[1]=cardclicked;
00194
00195
00196 totalopens++;
00197 if (matrix[flipd[0]] == matrix[flipd[1]]) {
00198 glutSetWindowTitle("GLICTMemory - Correct!");
00199 solved[flipd[0]] = 1;
00200 solved[flipd[1]] = 1;
00201 totalsolved++;
00202
00203 if (totalsolved == 8) {
00204 msgSuccess = new glictMessageBox;
00205 msgSuccess->SetOnDismiss(OnDismissSuccess);
00206 msgSuccess->SetCaption("Congrats!");
00207 msgSuccess->SetMessage("You've solved the game!");
00208 msgSuccess->SetPos(32,32);
00209 window.AddObject(msgSuccess);
00210
00211 }
00212 } else {
00213 glutSetWindowTitle("GLICTMemory - Wrong!");
00214 }
00215 }
00216
00217
00218 char solveds[256];
00219 sprintf(solveds, "Solved: %d\nOpens: %d", totalsolved, totalopens);
00220 pnlSolveds.SetCaption(solveds);
00221 glutPostRedisplay();
00222 }
00223
00224
00225
00226 void generate() {
00227 bool gen;
00228 int x,y;
00229 srand(time(NULL));
00230 for (int i=0;i<16;i++) taken[i]=false;
00231 for (int i=0;i<8;i++) {
00232 gen=false;
00233 while (!gen) {
00234 x = (int)(rand() / (float)RAND_MAX * (float)16);
00235 if (!taken[x]) {taken[x]=true; matrix[x]=i; gen=true;}
00236 }
00237
00238 gen=false;
00239 while (!gen) {
00240 x = (int)(rand() / (float)RAND_MAX * (float)16);
00241 if (!taken[x]) {taken[x]=true; matrix[x]=i; gen=true;}
00242
00243 }
00244 }
00245 flipd[0]=-1;
00246 flipd[1]=-1;
00247 }
00248
00249 void MainWidgets() {
00250 generate();
00251 window.SetCaption("Board");
00252 desktop.AddObject(&window);
00253
00254 for (int i=0; i<16;i++) {
00255
00256 cards[i].SetPos((i % 4) * 96, (i / 4) * 96);
00257 cards[i].SetWidth(96);
00258 cards[i].SetHeight(96);
00259 cards[i].SetBGColor(0.5, 0.5, 0.5, 1.);
00260
00261 char tmp[3];
00262 sprintf(tmp,"%d", i);
00263 cards[i].SetCaption(tmp);
00264 cards[i].SetOnClick(CardOnClick);
00265
00266 window.AddObject(&cards[i]);
00267 }
00268
00269 desktop.AddObject(&pnlSolveds);
00270 pnlSolveds.SetCaption("Solved: 0\nOpens: 0");
00271 pnlSolveds.SetBGColor(0.,0.,0.,1.);
00272 pnlSolveds.SetWidth(100);
00273 }
00274
00275
00276
00277
00278 int main(int argc, char** argv) {
00279
00280 glutInit(&argc, argv);
00281 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_STENCIL);
00282 glutInitWindowSize (640, 480);
00283 glutInitWindowPosition (0, 0);
00284
00285 windowhandle = glutCreateWindow ("GLICTMemory");
00286
00287 MainWidgets();
00288
00289 glictFont* sysfont = glictCreateFont("system");
00290 sysfont->SetFontParam(GLUT_STROKE_MONO_ROMAN);
00291 sysfont->SetRenderFunc(glutxStrokeString);
00292 sysfont->SetSizeFunc(glutxStrokeSize);
00293
00294
00295
00296 glutDisplayFunc(display);
00297 glutReshapeFunc(reshape);
00298 glutMouseFunc(mouse);
00299
00300
00301 glutSetWindow(windowhandle);
00302 glutShowWindow();
00303
00304
00305 glutMainLoop();
00306 return 0;
00307 }