/*************************************************************************** sdljpd.c - Jeopardy on steroids ------------------- begin : So 15 Mai 2005 copyright : (C) 2004 by Frederick Bullik email : Frederick.Bullik@web.de ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ #include #include #include #include /*include datei die das Komandozeilen handling vereinfacht*/ #include #include #include #include #include #include #include /* Define our booleans */ #define TRUE 1 #define FALSE 0 #define RENDER_MODE 2 //0=solid 1=shaded 2=blended /* screen width, height, and bit depth */ /** \brief Die Bildschirmbreite fuer den Fenstermodus */ #define SCREEN_WIDTH 1024 /** \brief Die Bildschirmhohe fuer den Fenstermodus */ #define SCREEN_HEIGHT 768 /** \brief Farbtiefe des Bildschirmes */ #define SCREEN_BPP 24 /* make prototype of getopt because of "implicit declaration" */ int getopt(int argc, char * const argv[], const char *optstring); extern char *optarg; extern int optind, opterr, optopt; char xpfp[1000]; char buf[10000]; char pl1[1000]; char pl2[1000]; char pl3[1000]; char pl4[1000]; char blub[1000]; int enp = FALSE; int inp = FALSE; signed int score[4] ={0,0,0,0}; int scorebuf; int plselect = 0; int lockbz = FALSE; int xpos = 0, ypos = 0; int solved[5][5] = {{0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0}}; int pkt[5] = {100,200,300,400,500}; int artype; int countxa = 0; int countya = 0; int countxf = 0; int countyf = 0; int countxt = 0; FILE *file; typedef struct s_block{ char frage[10000]; char antwort[1000]; } t_block; typedef struct { char katname[1000]; } t_kat; t_block myblock[5][5]; t_kat mykat[5]; Mix_Music *music = NULL; Mix_Music *buzz_sound = NULL; /** \brief This is our SDL surface * * Unser Haupt SDL Surface */ SDL_Surface *surface; /*Display Surface*/ SDL_Surface *kachel; /*hintergrund fuer die Fragefelder*/ SDL_Surface *bgrnd; /*makriert blockierte Felder */ SDL_Surface *nfield; /*Score, Namensfekder der Spieler*/ SDL_Surface *plac; /*kenzeichnung acktiver Spierler*/ SDL_Surface *afr; /*kenzeichnung des aktives fragen feldes */ SDL_Surface *text; SDL_Surface *panel; SDL_Rect drect; SDL_Color color={255,125,0}; TTF_Font *font; SDL_Joystick *joystick; /** \brief Hier Raeumen wir nach Programablauf auf. * * \param returnCode Der Zurueckzugebende Return Code * \return Gibt den Uebergebenen Code aus */ /* function to release/destroy our resources and restoring the old desktop */ void Quit( int returnCode ) { Mix_HaltMusic(); Mix_FreeMusic(music); music = NULL; system("./set_led 0 0 0 0"); SDL_JoystickClose(joystick); SDL_FreeSurface(panel); SDL_FreeSurface(text); SDL_FreeSurface(afr); SDL_FreeSurface(plac); SDL_FreeSurface(nfield); SDL_FreeSurface(kachel); SDL_FreeSurface(bgrnd); SDL_FreeSurface(surface); TTF_CloseFont(font); TTF_Quit(); /* clean up the window */ SDL_Quit( ); /*fclose(file);*/ printf("\033[33mPunktestand:\033[m\n%s: %i\n%s: %i\n%s: %i\n%s: %i\n", pl1, score[0], pl2, score[1], pl3, score[2], pl4, score[3]); /* and exit appropriately */ exit( returnCode ); } void musicDone(void){ Mix_HaltMusic(); Mix_FreeMusic(music); music = NULL; music = Mix_LoadMUS("bgmusic-loop.wav"); Mix_PlayMusic(music, -1); } void music_init(void){ if(music == NULL) { music = Mix_LoadMUS("bgmusic-intro.wav"); Mix_PlayMusic(music, 0); Mix_HookMusicFinished(musicDone); } } void filefill(void) { if(artype == 1) { strcpy(mykat[countxt].katname,blub); countxt ++; blub[0] = '\0'; } if(artype == 2) { strcpy(myblock[countxf][countyf].frage,blub); countyf ++; blub[0] = '\0'; if(countyf >4) { countyf = 0; countxf ++; } } if(artype == 3) { strcpy(myblock[countxa][countya].antwort,blub); countya ++; blub[0] = '\0'; if(countya >4) { countya = 0; countxa ++; } } } void filefoo (void) { char ch[100]; char ach; file = fopen(xpfp,"r"); if(file == NULL) { fprintf(stderr, "Folgende Datei konnte nicht geladen werden:%s\n",xpfp ); } while (fscanf(file,"%s",ch) ) { if (strcmp(ch,"Topic:") == 0){ filefill(); artype = 1; continue; } if (strcmp(ch,"Frage:")== 0){ filefill(); artype = 2; continue; } if (strcmp(ch,"Antwort:") == 0){ filefill(); artype = 3; continue; } strcat(blub, ch); strcat(blub, " "); if (fscanf(file,"%c",&ach) == EOF){ filefill(); break; } } fclose(file); } void loadtex( void ) { kachel = IMG_Load("kachel100.jpg"); if (kachel == NULL) { fprintf(stderr, "Das Bild konnte nicht geladen werden:%s\n", SDL_GetError()); exit(-1); } bgrnd = IMG_Load("bgrnd100.jpg"); if (bgrnd == NULL) { fprintf(stderr, "Das Bild konnte nicht geladen werden:%s\n", SDL_GetError()); exit(-1); } nfield = IMG_Load("nfield.jpg"); if (nfield == NULL) { fprintf(stderr, "Das Bild konnte nicht geladen werden:%s\n", SDL_GetError()); exit(-1); } plac = IMG_Load("sp.jpg"); if (plac == NULL) { fprintf(stderr, "Das Bild konnte nicht geladen werden:%s\n", SDL_GetError()); exit(-1); } afr = IMG_Load("aq.jpg"); if (afr == NULL) { fprintf(stderr, "Das Bild konnte nicht geladen werden:%s\n", SDL_GetError()); exit(-1); } panel = IMG_Load("panel1.jpg"); if (panel == NULL) { fprintf(stderr, "Das Bild konnte nicht geladen werden:%s\n", SDL_GetError()); exit(-1); } } void get_buz (void) { int i,n; SDL_JoystickUpdate (); if(lockbz == FALSE ) { for ( i=0; i < SDL_JoystickNumButtons ( joystick ); ++i ) { n = SDL_JoystickGetButton ( joystick, i ); if ( n != 0 ) { if(i == 0) { plselect = 0; lockbz = TRUE; system("./set_led 1 0 0 0"); Mix_PlayMusic(buzz_sound, 0); } if(i == 5) { plselect = 1; lockbz = TRUE; system("./set_led 0 1 0 0"); Mix_PlayMusic(buzz_sound, 0); } if(i == 10) { plselect = 2; lockbz = TRUE; system("./set_led 0 0 1 0"); Mix_PlayMusic(buzz_sound, 0); } if(i == 15) { plselect = 3; lockbz = TRUE; system("./set_led 0 0 0 1"); Mix_PlayMusic(buzz_sound, 0); } } } } } void draw_sdl (void) { int countx =0; int county =0; int i, k, n=0; /* Spielfeld geschlumpfe*/ drect.w = kachel -> w; drect.h = kachel -> h; SDL_SetAlpha(bgrnd, SDL_SRCALPHA | SDL_RLEACCEL, 128); SDL_SetAlpha(afr, SDL_SRCALPHA | SDL_RLEACCEL, 128); SDL_SetAlpha(plac, SDL_SRCALPHA | SDL_RLEACCEL, 128); get_buz(); SDL_FillRect(surface, NULL, 0); for(county=0;county<=4;county++) { drect.y = (county*100) + 100; for(countx=0;countx<=4;countx++) { drect.x = (countx*200) + 65; SDL_BlitSurface(kachel, NULL, surface, &drect ); if(solved[countx][county] == 0){ SDL_FreeSurface(text); sprintf(buf,"%i",pkt[county]); /*if(pkt[county]==100){buf = "100";} if(pkt[county]==200){buf = "200";} if(pkt[county]==300){buf = "300";} f(pkt[county]==400){buf = "400";} if(pkt[county]==500){buf = "500";}*/ text = TTF_RenderText_Blended(font, buf,color); drect.x = drect.x + 23; SDL_BlitSurface(text,NULL,surface,&drect); } if(solved[countx][county] >=1){ SDL_BlitSurface(bgrnd, NULL, surface, &drect ); if(solved[countx][county] == 1) strcpy(buf,pl1); if(solved[countx][county] == 2) strcpy(buf,pl2); if(solved[countx][county] == 3) strcpy(buf,pl3); if(solved[countx][county] == 4) strcpy(buf,pl4); drect.x = (countx*200) + 65; drect.y = drect.y + 23; SDL_FreeSurface(text); text = TTF_RenderText_Blended(font, buf , color); drect.w = text -> w; drect.h = text -> h; SDL_BlitSurface(text,NULL,surface,&drect); drect.y = drect.y - 23; } } for(countx=0;countx<=5;countx++) { drect.x = (countx*200) + 65; drect.y = 45; strcpy(buf,mykat[countx].katname); SDL_FreeSurface(text); text = TTF_RenderText_Blended(font, buf , color); SDL_BlitSurface(text,NULL,surface,&drect); } /* The Code for the Panle Drawing goes here */ if(enp == TRUE){ drect.w = panel -> w; drect.h = panel -> h; drect.x = 185; drect.y = 100; SDL_BlitSurface(panel, NULL, surface, &drect ); drect.x = drect.x + 23; drect.y = drect.y + 23; SDL_FreeSurface(text); text = TTF_RenderText_Blended(font, "Antwort:" , color); SDL_BlitSurface(text,NULL,surface,&drect); i = 0; while (i w; drect.h = panel -> h; drect.x = 185; drect.y = 100; SDL_BlitSurface(panel, NULL, surface, &drect); drect.x = drect.x + 23; drect.y = drect.y + 23; SDL_FreeSurface(text); text = TTF_RenderText_Blended(font, "Frage:" , color); SDL_BlitSurface(text,NULL,surface,&drect); i = 0; while (i h; drect.h = nfield -> w; drect.y = 650; for(countx=0;countx<=3;countx++) { drect.x = (countx*250) + 50; SDL_BlitSurface(nfield, NULL, surface, &drect ); } drect.x = (plselect*250) + 50; SDL_BlitSurface(plac,NULL,surface,&drect); drect.y = drect.y + 23; SDL_FreeSurface(text); text = TTF_RenderText_Blended(font, pl1,color); drect.x = 65; SDL_BlitSurface(text,NULL,surface,&drect); SDL_FreeSurface(text); text = TTF_RenderText_Blended(font, pl2,color); drect.x = (250) + 65; SDL_BlitSurface(text,NULL,surface,&drect); SDL_FreeSurface(text); text = TTF_RenderText_Blended(font, pl3,color); drect.x = (2*250) + 65; SDL_BlitSurface(text,NULL,surface,&drect); SDL_FreeSurface(text); text = TTF_RenderText_Blended(font, pl4,color); drect.x = (3*250) + 65; SDL_BlitSurface(text,NULL,surface,&drect); drect.y = drect.y + 30; SDL_FreeSurface(text); scorebuf = score[0]; sprintf(buf,"%i",scorebuf); text = TTF_RenderText_Blended(font,buf,color); drect.x = 65; SDL_BlitSurface(text,NULL,surface,&drect); SDL_FreeSurface(text); scorebuf = score[1]; sprintf(buf,"%i",scorebuf); text = TTF_RenderText_Blended(font,buf,color); drect.x = (250) + 65; SDL_BlitSurface(text,NULL,surface,&drect); SDL_FreeSurface(text); scorebuf = score[2]; sprintf(buf,"%i",scorebuf); text = TTF_RenderText_Blended(font,buf,color); drect.x = (2*250) + 65; SDL_BlitSurface(text,NULL,surface,&drect); SDL_FreeSurface(text); scorebuf = score[3]; sprintf(buf,"%i",scorebuf); text = TTF_RenderText_Blended(font,buf,color); drect.x = (3*250) + 65; SDL_BlitSurface(text,NULL,surface,&drect); SDL_Flip(surface); } int main( int argc, char **argv ) { /** \brief Zählt argc*/ int option; /** \brief Flags zur Übergabe an den Video mode */ int videoFlags; /** \brief Variable der Hauptschleife */ int done = FALSE; /** \brief sammelt Events */ SDL_Event event; /** \brief Display Informationen */ const SDL_VideoInfo *videoInfo; /** \brief Ist unser Fenster Acti? */ int isActive = TRUE; /** \brief String für den Fenstertitel */ char *wmcapt ="SdlJPD " ; int audio_rate = 22050; Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */ int audio_channels = 2; int audio_buffers = 4096; strcpy(pl1,"Spieler1"); strcpy(pl2,"Spieler2"); strcpy(pl3,"Spieler3"); strcpy(pl4,"Spieler4"); strcpy(xpfp,"level.txt"); while (( option = getopt(argc, argv, "hf:a:b:c:d:")) >= 0) switch (option) { case 'h': { printf("Usage: %s [Optionen] \n Optionen: \n -h print this help Text \n-f jeopardy file\n",argv[0]); Quit(0); } break; case 'f': strcpy(xpfp,optarg); break; case 'a': strcpy(pl1,optarg); break; case 'b': strcpy(pl2,optarg); break; case 'c': strcpy(pl3,optarg); break; case 'd': strcpy(pl4,optarg); break; } printf("\n \033[36m sdljpd -- jeopady on steroids \n Copyright (c) 2005 Frederick Bullik \n\n This program is free software; you can redistribute it and/or\n modify it under the terms of the GNU General Public License\n as published by the Free Software Foundation; either version 2\n of the License, or (at your option) any later version.\n\n This program is distributed in the hope that it will be useful,\n but WITHOUT ANY WARRANTY; without even the implied warranty of\n MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n GNU General Public License for more details.\n\n You should have received a copy of the GNU General Public License\n along with this program; if not, write to the Free Software \n Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\033[m \n\n"); printf("========================================================================\n"); filefoo(); if(TTF_Init()==-1) { printf("TTF_Init: %s\n", TTF_GetError()); exit(2); } font=TTF_OpenFont("subfont.ttf", 26); if(!font) { printf("TTF_OpenFont: %s\n", TTF_GetError()); /* handle error*/ } /* sprintf(wmcapt,"glpong - %s vs ", player_a );*/ /* initialize SDL */ if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0 ) { fprintf( stderr, "SDL initialization failed: %s\n", SDL_GetError( ) ); Quit( 1 ); } if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) { printf("Unable to open audio!\n"); exit(1); } Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels); /* Fetch the video info */ videoInfo = SDL_GetVideoInfo( ); music_init(); buzz_sound = Mix_LoadMUS("buzzer.wav"); if ( !videoInfo ) { fprintf( stderr, "Video query failed: %s\n", SDL_GetError( ) ); Quit( 1 ); } printf ( "%i joysticks found: ", SDL_NumJoysticks () ); joystick = SDL_JoystickOpen (0); if(joystick == NULL) { printf("Unable to open joystick!\n"); exit(1); } SDL_JoystickEventState ( SDL_QUERY ); SDL_JoystickUpdate (); printf ( "%s\n", SDL_JoystickName ( 0 ) ); system("./set_led 1 1 1 1"); SDL_WM_SetCaption(wmcapt, NULL); /*SDL_WM_SetIcon(SDL_LoadBMP("glpong.bmp"),NULL);*/ /* the flags to pass to SDL_SetVideoMode */ videoFlags |= SDL_HWPALETTE; /* Store the palette in hardware */ videoFlags |= SDL_DOUBLEBUF; videoFlags |= SDL_RESIZABLE; /* Enable window resizing */ videoFlags |= SDL_HWSURFACE; videoFlags |= SDL_HWACCEL; SDL_ShowCursor( SDL_DISABLE); /* get a SDL surface */ surface = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, videoFlags ); /* Verify there is a surface */ if ( !surface ) { fprintf( stderr, "Video mode set failed: %s\n", SDL_GetError( ) ); Quit( 1 ); } loadtex(); /* wait for events */ while ( !done ) { /* handle the events in the queue */ while ( SDL_PollEvent( &event ) ) { switch( event.type ) { case SDL_ACTIVEEVENT: /* Something's happend with our focus * If we lost focus or we are iconified, we * shouldn't draw the screen */ if ( event.active.gain == 0 ) isActive = FALSE; else isActive = TRUE; break; case SDL_VIDEORESIZE: /* handle resize event */ surface = SDL_SetVideoMode( event.resize.w, event.resize.h, 16, videoFlags ); if ( !surface ) { fprintf( stderr, "Could not get a surface after resize: %s\n", SDL_GetError( ) ); Quit( 1 ); } break; case SDL_KEYDOWN: /* handle key presses */ switch (event.key.keysym.sym){ case SDLK_ESCAPE: Quit( 0 ); break; case SDLK_q: Quit( 0 ); break; case SDLK_F1: /* F1 key was pressed * this toggles fullscreen mode */ SDL_WM_ToggleFullScreen( surface ); break; /* Player left */ case SDLK_a: if(plselect >0){ plselect --;} enp = FALSE; inp = FALSE; break; /* Player right */ case SDLK_d: if(plselect <3){ plselect ++;} enp = FALSE; inp = FALSE; break; /* Score Add 100 */ case SDLK_w: score[plselect] = score[plselect] + 100; enp = FALSE; inp = FALSE; break; /* Score Subtract 100 */ case SDLK_s: score[plselect] = score[plselect] - 100; enp = FALSE; inp = FALSE; break; case SDLK_RETURN: lockbz = FALSE; enp = TRUE; system("./set_led 1 1 1 1"); break; case SDLK_LEFT: if(enp == FALSE){ if(xpos >0){ xpos --; } inp = FALSE; } break; case SDLK_RIGHT: if(enp == FALSE){ if(xpos <4){ xpos ++; } inp = FALSE; } break; case SDLK_UP: if(enp == FALSE){ if(ypos >0){ ypos --; } inp = FALSE; } break; case SDLK_DOWN: if(enp== FALSE) { if(ypos <4){ ypos ++; } inp = FALSE; } break; /* Right Answer */ case SDLK_INSERT: score[plselect]=score[plselect]+pkt[ypos]; solved[xpos][ypos] = (plselect + 1); lockbz = TRUE; system("./set_led 0 0 0 0"); enp = FALSE; inp = TRUE; break; /* Show Answer without changing score */ case SDLK_y: lockbz = TRUE; enp = FALSE; inp = TRUE; break; /* Wrong Answer */ case SDLK_DELETE: score[plselect]=score[plselect]-pkt[ypos]; lockbz = FALSE; system("./set_led 1 1 1 1"); inp = FALSE; enp = TRUE; break; /* Mark solved */ case SDLK_PAGEUP: solved[xpos][ypos] = (plselect + 1); break; /* Mark unsolved */ case SDLK_PAGEDOWN: solved[xpos][ypos] = 0; /* Lock Buzzers */ case SDLK_l: lockbz = TRUE; system("./set_led 0 0 0 0"); break; /* Unlock Buzzers */ case SDLK_u: lockbz = FALSE; system("./set_led 1 1 1 1"); break; /* Reset playing field */ case SDLK_r: lockbz = FALSE; system("./set_led 1 1 1 1"); inp = FALSE; enp = FALSE; break; default: break; } break; case SDL_QUIT: /* handle quit requests */ done = TRUE; break; } break; } draw_sdl(); /*Mix_CloseAudio();*/ } /* clean ourselves up and exit */ Quit( 0 ); /* Should never get here */ return( 0 ); }