00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00032
00033
00034
00035
00036
00037
00038 #include <SDL/SDL.h>
00039
00040
00041
00042
00043
00044
00045
00046
00047 #include <GLICT/container.h>
00048 #include <GLICT/button.h>
00049 #include <GLICT/globals.h>
00050 #include <GLICT/window.h>
00051 #include <GLICT/panel.h>
00052 #include <GLICT/messagebox.h>
00053 #include <GLICT/fonts.h>
00054 #include "sdlfont.h"
00055
00056 char cardtitles[16][16] = { "Gecko", "Smygflik", "mips", "the fike", "Pekay", "Yorick", "tliff", "SimOne"};
00057
00058 float cardcolor[16][3] = { { 1., 0., 0.}, { 0., 0., .9 }, {.8, .8, 0.}, {.7, 0., .7},
00059 {0., .85, 0.}, { .55, 0., 0. }, {0., 75., .75}, {.5, 1., .5}};
00060
00061 char matrix[16];
00062 char taken[16];
00063 char solved[16];
00064 char flipd[2];
00065
00066
00067 int totalsolved = 0, totalopens = 0;
00068
00069 glictContainer desktop;
00070 glictWindow window;
00071 glictButton cards[16];
00072 glictPanel pnlSolveds;
00073 glictMessageBox* msgSuccess;
00074 int windowhandle;
00075
00076
00077
00078
00079
00080 void OnDismissSuccess (glictPos* relmousepos, glictContainer* caller) {
00081 delete caller;
00082 }
00083
00084
00085
00086
00087 void CardOnClick(glictPos* relmousepos, glictContainer* callerclass) {
00088 glictButton *button = dynamic_cast<glictButton*>(callerclass);
00089 int cardclicked = button - cards;
00090
00091
00092
00093
00094 SDL_WM_SetCaption("GLICTMemory", "");
00095
00096
00097 if (flipd[1]!=-1 ) {
00098
00099 if (!solved[flipd[0]]) {
00100 char tmp[16];
00101 sprintf(tmp, "%d", flipd[0]); cards[flipd[0]].SetCaption(tmp); cards[flipd[0]].SetBGColor(0.5, 0.5, 0.5, 1.);
00102 sprintf(tmp, "%d", flipd[1]); cards[flipd[1]].SetCaption(tmp); cards[flipd[1]].SetBGColor(0.5, 0.5, 0.5, 1.);
00103
00104 }
00105 flipd[0]=-1;
00106 flipd[1]=-1;
00107
00108 }
00109
00110 if (solved[cardclicked]) {
00111 SDL_WM_SetCaption("GLICTMemory - Card's been solved already.", "");
00112 return;
00113 }
00114
00115 if (flipd[0]!=-1 && cardclicked==flipd[0]) {
00116 return;
00117 }
00118
00119
00120 button->SetCaption(cardtitles[matrix[cardclicked]]);
00121 button->SetBGColor(cardcolor[matrix[cardclicked]][0], cardcolor[matrix[cardclicked]][1], cardcolor[matrix[cardclicked]][2], 1.);
00122 if (flipd[0]==-1) flipd[0]=cardclicked; else {
00123 flipd[1]=cardclicked;
00124
00125
00126 totalopens++;
00127 if (matrix[flipd[0]] == matrix[flipd[1]]) {
00128 SDL_WM_SetCaption("GLICTMemory - Correct!", "");
00129 solved[flipd[0]] = 1;
00130 solved[flipd[1]] = 1;
00131 totalsolved++;
00132
00133 if (totalsolved == 8) {
00134 msgSuccess = new glictMessageBox;
00135 msgSuccess->SetOnDismiss(OnDismissSuccess);
00136 msgSuccess->SetCaption("Congrats!");
00137 msgSuccess->SetMessage("You've solved the game!");
00138 msgSuccess->SetPos(32,32);
00139 window.AddObject(msgSuccess);
00140
00141 }
00142 } else {
00143 SDL_WM_SetCaption("GLICTMemory - Wrong!", "");
00144 }
00145 }
00146
00147
00148 char solveds[256];
00149 sprintf(solveds, "Solved: %d\nOpens: %d", totalsolved, totalopens);
00150 pnlSolveds.SetCaption(solveds);
00151
00152 }
00153
00154
00155
00156 void generate() {
00157 bool gen;
00158 int x,y;
00159 srand(time(NULL));
00160 for (int i=0;i<16;i++) taken[i]=false;
00161 for (int i=0;i<8;i++) {
00162 gen=false;
00163 while (!gen) {
00164 x = (int)(rand() / (float)RAND_MAX * (float)16);
00165 if (!taken[x]) {taken[x]=true; matrix[x]=i; gen=true;}
00166 }
00167
00168 gen=false;
00169 while (!gen) {
00170 x = (int)(rand() / (float)RAND_MAX * (float)16);
00171 if (!taken[x]) {taken[x]=true; matrix[x]=i; gen=true;}
00172
00173 }
00174 }
00175 flipd[0]=-1;
00176 flipd[1]=-1;
00177 }
00178
00179 void MainWidgets() {
00180 generate();
00181 window.SetCaption("Board");
00182 desktop.AddObject(&window);
00183
00184 for (int i=0; i<16;i++) {
00185
00186 cards[i].SetPos((i % 4) * 96, (i / 4) * 96);
00187 cards[i].SetWidth(96);
00188 cards[i].SetHeight(96);
00189 cards[i].SetBGColor(0.5, 0.5, 0.5, 1.);
00190
00191 char tmp[3];
00192 sprintf(tmp,"%d", i);
00193 cards[i].SetCaption(tmp);
00194 cards[i].SetOnClick(CardOnClick);
00195
00196 window.AddObject(&cards[i]);
00197 }
00198
00199 desktop.AddObject(&pnlSolveds);
00200 pnlSolveds.SetCaption("Solved: 0\nOpens: 0");
00201 pnlSolveds.SetBGColor(0.,0.,0.,1.);
00202 pnlSolveds.SetWidth(100);
00203 }
00204
00205 void reshape(int x, int y) {
00206
00207 desktop.SetWidth(x);
00208 desktop.SetHeight(y);
00209
00210 window.SetWidth(96*4);
00211 window.SetHeight(96*4);
00212
00213 window.SetPos(x/2-96*2, y/2-96*2);
00214 }
00215
00216 SDL_Surface * screen;
00217 SDL_Surface * sysfontpic;
00218
00219 void SDLRectDraw(float left, float right, float top, float bottom, glictColor &col) {
00220 static const SDL_VideoInfo* vi = SDL_GetVideoInfo();
00221 int color = SDL_MapRGB(vi->vfmt, (int)(col.r * 255), (int)(col.g * 255), (int)(col.b * 255));
00222 SDL_Rect rect = {left, top, right-left, bottom-top};
00223 SDL_FillRect(screen, &rect, color);
00224 }
00225
00226
00227
00228 int main(int argc, char** argv) {
00229
00230 int videoflags = SDL_SWSURFACE | SDL_ANYFORMAT| SDL_DOUBLEBUF | SDL_RESIZABLE | SDL_SRCALPHA ;
00231 int width = 640;
00232 int height = 480;
00233 int video_bpp = 8;
00234
00235
00236 if(SDL_Init(SDL_INIT_VIDEO) < 0) {
00237 fprintf(stderr,"Couldn't initialize SDL: %s\n", SDL_GetError());
00238 exit(1);
00239 }
00240
00241
00242
00243
00244 screen = SDL_SetVideoMode(width, height, video_bpp, videoflags);
00245
00246 if (!screen){
00247 fprintf(stderr, "Could not set %dx%d video mode: %s\n", width, height, SDL_GetError());
00248 exit(1);
00249 }
00250
00251 printf("Set 640x480 at %d bits-per-pixel mode\n",
00252 screen->format->BitsPerPixel);
00253
00254 if (screen->format->BitsPerPixel==8) {
00255
00256 SDL_Color colors[256];
00257 for (int i = 0 ; i < 256; i++) {
00258 colors[i].r = i; colors[i].g = i; colors[i].b = i;
00259 }
00260
00261 SDL_SetColors(screen, colors, 0, sizeof(colors) / sizeof(SDL_Color));
00262 }
00263 SDL_WM_SetCaption("GLICTMemory", "");
00264
00265
00266 glictFont* sysfont = glictCreateFont("system");
00267 sysfontpic = SDL_LoadBMP("font.bmp");
00268 SDL_SetColorKey(sysfontpic, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(SDL_GetVideoInfo()->vfmt, 0xFF, 0, 0xFF));
00269
00270 sysfont->SetFontParam(sysfontpic);
00271 sysfont->SetRenderFunc(SDLFontDraw);
00272 sysfont->SetSizeFunc(SDLFontSize);
00273
00274
00275
00276
00277 MainWidgets();
00278
00279
00280 glictGlobals.w = width;
00281 glictGlobals.h = height;
00282 desktop.SetWidth(width);
00283 desktop.SetHeight(height);
00284
00285 reshape(width,height);
00286
00287 glictGlobals.paintrectCallback = SDLRectDraw;
00288 glictGlobals.enableGlTranslate = false;
00289 bool running = true;
00290 SDL_Event event;
00291
00292
00293 glictColor c = {0,0,0,1};
00294 SDLRectDraw(0,640,0,480,c);
00295
00296 desktop.Paint();
00297 SDL_Flip(screen);
00298
00299 while(running){
00300 while(SDL_PollEvent(&event)){
00301 switch (event.type){
00302 case SDL_KEYDOWN:
00303
00304 break;
00305 case SDL_MOUSEBUTTONUP:
00306 case SDL_MOUSEBUTTONDOWN:{
00307 glictPos p = {event.button.x, event.button.y};
00308 desktop.CastEvent(event.type == SDL_MOUSEBUTTONUP ? GLICT_MOUSEUP : GLICT_MOUSEDOWN, &p,0);
00309
00310
00311 glictColor c = {0,0,0,1};
00312 SDLRectDraw(0,640,0,480,c);
00313
00314 desktop.Paint();
00315 SDL_Flip(screen);
00316
00317 break;
00318 }
00319 case SDL_QUIT:
00320 running = false;
00321 break;
00322 default:
00323 break;
00324 }
00325 }
00326 }
00327
00328
00329 SDL_FreeSurface(screen);
00330 SDL_FreeSurface(sysfontpic);
00331 glictDeleteFont("system");
00332 SDL_Quit();
00333
00334 return 0;
00335 }