You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

892 lines
20 KiB

13 years ago
/***************************************************************************
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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h> /*include datei die das Komandozeilen handling vereinfacht*/
#include <time.h>
#include <math.h>
#include <sys/io.h>
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
#include "SDL_mixer.h"
/* 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 blub[1000];
int enp = FALSE;
int inp = FALSE;
signed int score[3] ={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;
13 years ago
Mix_Music *buzz_sound = NULL;
13 years ago
/** \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;
13 years ago
SDL_Joystick *joystick;
13 years ago
/** \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;
13 years ago
system("./set_led 0 0 0 0");
13 years ago
SDL_JoystickClose(joystick);
13 years ago
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[33m sdljpd:\033[m So long, and thanks for all the fish <;))>< \n");
/* 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);
}
}
13 years ago
void get_buz (void)
13 years ago
{
13 years ago
int i,n;
SDL_JoystickUpdate ();
13 years ago
if(lockbz == FALSE )
{
13 years ago
for ( i=0; i < SDL_JoystickNumButtons ( joystick ); ++i )
{
n = SDL_JoystickGetButton ( joystick, i );
if ( n != 0 ) {
13 years ago
printf ( "found you pressed button %i\n", i );
if(i == 0)
{
plselect = 0;
13 years ago
lockbz = TRUE;
system("./set_led 1 0 0 0");
13 years ago
Mix_PlayMusic(buzz_sound, 0);
}
if(i == 5)
{
plselect = 1;
lockbz = TRUE;
system("./set_led 0 1 0 0");
13 years ago
Mix_PlayMusic(buzz_sound, 0);
}
13 years ago
if(i == 10)
{
plselect = 2;
lockbz = TRUE;
system("./set_led 0 0 1 0");
13 years ago
Mix_PlayMusic(buzz_sound, 0);
}
}
}
13 years ago
}
13 years ago
}
13 years ago
void draw_sdl (void)
{
int countx =0;
int county =0;
13 years ago
int i=0;
13 years ago
/* 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);
13 years ago
get_buz();
13 years ago
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);
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);
13 years ago
for (i=0; i<=100; i++)
buf[i] = 0;
13 years ago
for (i=0; i<strlen(myblock[xpos][ypos].frage); i+=45)
13 years ago
{
drect.y = drect.y + 23;
13 years ago
strncpy(buf,myblock[xpos][ypos].frage+i, 45);
13 years ago
SDL_FreeSurface(text);
text = TTF_RenderText_Blended(font, buf , color);
SDL_BlitSurface(text,NULL,surface,&drect);
}
drect.y = drect.y - 23;
13 years ago
drect.x = drect.x - 23;
drect.y = drect.y - 23;
}
if(inp == 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, "Frage:" , color);
SDL_BlitSurface(text,NULL,surface,&drect);
13 years ago
for (i=0; i<=100; i++)
buf[i] = 0;
for (i=0; i<strlen(myblock[xpos][ypos].antwort); i+=45)
{
drect.y = drect.y + 23;
13 years ago
strncpy(buf,myblock[xpos][ypos].antwort+i,45);
13 years ago
SDL_FreeSurface(text);
text = TTF_RenderText_Blended(font, buf , color);
SDL_BlitSurface(text,NULL,surface,&drect);
}
13 years ago
drect.y = drect.y - 23;
drect.x = drect.x - 23;
drect.y = drect.y - 23;
}
}
/*Player gefuddel */
drect.y = (ypos*100) + 100;
drect.x = (xpos*200) + 65;
if(enp == FALSE && inp == FALSE){
SDL_BlitSurface(afr,NULL,surface, &drect);
}
drect.w = nfield -> h;
drect.h = nfield -> w;
drect.y = 650;
for(countx=0;countx<=2;countx++)
{
drect.x = (countx*365) + 50;
SDL_BlitSurface(nfield, NULL, surface, &drect );
}
drect.x = (plselect*365) + 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 = (365) + 65;
SDL_BlitSurface(text,NULL,surface,&drect);
SDL_FreeSurface(text);
text = TTF_RenderText_Blended(font, pl3,color);
drect.x = (2*365) + 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 = (365) + 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*365) + 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,"Jessy");
strcpy(pl2,"Micha");
strcpy(pl3,"Pierre");
13 years ago
strcpy(xpfp,"level.txt");
while (( option = getopt(argc, argv, "hf:a:b:c:")) >= 0)
switch (option)
{
case 'h': {
printf("Usage: %s [Optionen] \n Optionen: \n -h print this help Text \n-f <file> 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;
}
printf("\n \033[36m sdljpd -- jeopady on steroids \n Copyright (c) 2005 Frederick Bullik <Frederick.Bullik@gmx.de>\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 */
13 years ago
if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0 )
13 years ago
{
fprintf( stderr, "SDL initialization failed: %s\n",
13 years ago
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();
13 years ago
buzz_sound = Mix_LoadMUS("buzzer.wav");
13 years ago
if ( !videoInfo )
{
fprintf( stderr, "Video query failed: %s\n",
SDL_GetError( ) );
Quit( 1 );
}
13 years ago
printf ( "%i joysticks found: ", SDL_NumJoysticks () );
joystick = SDL_JoystickOpen (0);
if(joystick == NULL) {
13 years ago
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 0");
13 years ago
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;
13 years ago
/* Player left */
13 years ago
case SDLK_a:
if(plselect >0){
plselect --;}
enp = FALSE;
inp = FALSE;
break;
13 years ago
/* Player right */
13 years ago
case SDLK_d:
if(plselect <2){
plselect ++;}
enp = FALSE;
inp = FALSE;
break;
13 years ago
/* Score Add 100 */
13 years ago
case SDLK_w:
score[plselect] = score[plselect] + 100;
enp = FALSE;
inp = FALSE;
break;
13 years ago
/* Score Subtract 100 */
13 years ago
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 0");
13 years ago
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;
13 years ago
/* Right Answer */
13 years ago
case SDLK_INSERT:
score[plselect]=score[plselect]+pkt[ypos];
solved[xpos][ypos] = (plselect + 1);
lockbz = TRUE;
enp = FALSE;
inp = TRUE;
break;
13 years ago
/* Wrong Answer */
13 years ago
case SDLK_DELETE:
score[plselect]=score[plselect]-pkt[ypos];
lockbz = FALSE;
system("./set_led 1 1 1 0");
13 years ago
inp = FALSE;
enp = TRUE;
break;
13 years ago
/* Mark solved */
13 years ago
case SDLK_PAGEUP:
solved[xpos][ypos] = (plselect + 1);
break;
13 years ago
/* Mark unsolved */
13 years ago
case SDLK_PAGEDOWN:
solved[xpos][ypos] = 0;
13 years ago
/* Lock Buzzers */
13 years ago
case SDLK_l:
lockbz = TRUE;
13 years ago
system("./set_led 0 0 0 0");
13 years ago
break;
13 years ago
/* Unlock Buzzers */
13 years ago
case SDLK_u:
lockbz = FALSE;
system("./set_led 1 1 1 0");
13 years ago
break;
13 years ago
/* Reset playing field */
13 years ago
case SDLK_r:
13 years ago
/* lockbz = FALSE;
system("./set_led 1 1 1 0"); */
13 years ago
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 );
}