examples/memory_sdl/main.cpp

This is a variant on the "memory/" example program, modified to demonstrate how to use GLICT with other rendering APIs such as SDL.

Being that the author of this code is a beginner SDL programmer, he asks you to forgive him on the newbie code, and for the lack of comments. Those who need this code will probably understand it.

Bug:
Palette is not correctly set when in 8bit mode.
00001 /*
00002     GLICT - Graphics Library Interface Creation Toolkit
00003     Copyright (C) 2006 OBJECT Networks
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public
00016     License along with this library; if not, write to the Free
00017     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00032 // GLICTMemory_SDL
00033 //  simple game made with GLICT
00034 //  to compile you'll need SDL
00035 
00036 // SDL includes
00037 // we use sdl because it's cross platform compatible
00038 #include <SDL/SDL.h>
00039 
00040 
00041 
00042 // GLICT includes
00043 // following includes should, in your project, actually look a
00044 // bit different
00045 // this project, as it is, should be placed inside the GLICT's folder so
00046 // its includes is relative
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 // some nice strings for the cards... ;)
00056 char cardtitles[16][16] = { "Gecko", "Smygflik", "mips", "the fike", "Pekay", "Yorick", "tliff", "SimOne"};
00057 // and some colors for the cards
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 // other game related vars
00061 char matrix[16]; // matrix specifies where is each card
00062 char taken[16]; // taken specifies if we've used a card in generation, so we dont generate same pair again
00063 char solved[16]; // solved specifies if a piece has been solved
00064 char flipd[2]; // which cards are currently flipped
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; // glut's window identifier
00075 
00076 
00077 
00078 
00079 // this happens when msgbox is dismissed; serves for memory cleanup
00080 void OnDismissSuccess (glictPos* relmousepos, glictContainer* caller) {
00081     delete caller;
00082 }
00083 
00084 
00085 // CardOnClick is executed upon click on a card with mouse
00086 // we'll identify which card is used by pointer difference
00087 void CardOnClick(glictPos* relmousepos, glictContainer* callerclass) {
00088     glictButton *button = dynamic_cast<glictButton*>(callerclass);
00089     int cardclicked = button - cards;
00090     //char c[256];
00091     //sprintf(c, "%d", cardclicked);
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 // game related, board generation function
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 // MainWidgets initializes the widgets on main screen
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     // set desktop's width and height to what we got
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 // the main function initializes everything
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         //printf("'Best' video mode: %d\n", video_bpp = SDL_GetVideoInfo()->vfmt->BitsPerPixel);
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)); // magneta is transparent
00269 
00270     sysfont->SetFontParam(sysfontpic);
00271     sysfont->SetRenderFunc(SDLFontDraw);
00272     sysfont->SetSizeFunc(SDLFontSize);
00273 
00274 
00275         //SDL_SetColors(sysfontpic, colors, 0, sizeof(colors) / sizeof(SDL_Color));
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         // blanks the screen..
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                                         // blanks the screen..
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 }

SourceForge.net Logo
generated with doxygen 1.5.3 on Mon Oct 29 18:09:26 2007