-#include <dirent.h>
+/*
+ * zeRace 0.2, a funny retro racing game
+ * http://royale.zerezo.com/zerace/
+ *
+ * Copyright (C) 2004 Antoine Jacquet <royale@zerezo.com>
+ *
+ * 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.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <stdlib.h>
+#include <time.h>
#include <signal.h>
+#include <math.h>
+#include <string.h>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_net.h>
#include <SDL_mixer.h>
-#include <math.h>
+#include <SDL_rotozoom.h>
#include "sdl.h"
-#define MAXLINELENGTH 1000
-#define VERSION "0.1"
+/* configuration constants */
+#define COEFF 1
+#define DELAY 7
+#define MAXLINELENGTH 80
+#define VERSION "0.2"
#define WIDTH 1024
#define HEIGHT 768
-/* global variables */
-
-struct track
+/* tracklist : double chained list */
+struct _tracklist
{
char *name;
char *title;
char *full;
char *function;
int x,y,a;
- struct track *prev;
- struct track *next;
+ int best_time;
+ char *best_pseudo;
+ struct _tracklist *prev;
+ struct _tracklist *next;
} *tracklist=NULL;
-int fullscreen=0;
-int sound=0;
-char pseudo[MAXLINELENGTH]="anonymous";
-char url[MAXLINELENGTH]="";
+/* user setup */
+struct _config
+{
+ char pseudo[MAXLINELENGTH];
+ char url[MAXLINELENGTH];
+ int fullscreen;
+ int sound;
+ int tire;
+ SDLKey up;
+ SDLKey down;
+ SDLKey left;
+ SDLKey right;
+ char color;
+} config = {"anonymous","",0,0,1,SDLK_UP,SDLK_DOWN,SDLK_LEFT,SDLK_RIGHT,6};
+
+/* display and all directions for the car */
SDL_Surface *screen;
SDL_Surface *cars[256];
+
+/* read the user configuration file */
+void zeRace_read_config()
+{
+ FILE *fic;
+ if ((fic=fopen("zeRace.cfg","rt"))==NULL)
+ {
+ fprintf(stderr,"can't open config file \"zeRace.cfg\"\n");
+ return;
+ }
+ fread(&config,sizeof(struct _config),1,fic);
+ fclose(fic);
+}
+
+
+/* save the user configuration file */
+void zeRace_save_config()
+{
+ FILE *fic;
+ if ((fic=fopen("zeRace.cfg","wt"))==NULL)
+ {
+ fprintf(stderr,"can't create config file \"zeRace.cfg\"\n");
+ return;
+ }
+ fwrite(&config,sizeof(struct _config),1,fic);
+ fclose(fic);
+}
+
+
+/* exit the game and clean */
void zeRace_exit()
{
printf("quit\n");
- if (sound) Mix_CloseAudio();
+ if (config.sound) Mix_CloseAudio();
SDLNet_Quit();
SDL_Quit();
- /*
- save_config();
- zeRace_send_ghosts();
- close_sdl();
- */
+ zeRace_save_config();
exit(0);
}
-void zeRace_read_config()
+/* get available local tracks */
+void zeRace_get_tracks()
{
FILE *fic;
char line[MAXLINELENGTH];
- char keyword[MAXLINELENGTH];
- char value[MAXLINELENGTH];
- int configlines=0;
+ struct _tracklist *tmp=NULL,*first=NULL;
+
+ if ((fic=fopen("tracks/list.txt","rt"))==NULL)
+ {
+ fprintf(stderr,"can't open track list\n");
+ zeRace_exit();
+ }
+ while (!feof(fic))
+ {
+ tmp=(struct _tracklist *)malloc(sizeof(struct _tracklist));
+ if (first==NULL) first=tmp;
+ fgets(line,MAXLINELENGTH,fic);
+ tmp->name=(char *)malloc(strlen(line)+1);
+ strcpy(tmp->name,line);
+ tmp->name[strlen(tmp->name)-1]='\0';
+ fgets(line,MAXLINELENGTH,fic);
+ tmp->title=(char *)malloc(strlen(line)+1);
+ strcpy(tmp->title,line);
+ fgets(line,MAXLINELENGTH,fic);
+ tmp->author=(char *)malloc(strlen(line)+1);
+ strcpy(tmp->author,line);
+ fgets(line,MAXLINELENGTH,fic);
+ tmp->version=(char *)malloc(strlen(line)+1);
+ strcpy(tmp->version,line);
+ fgets(line,MAXLINELENGTH,fic);
+ tmp->x=atoi(line);
+ fgets(line,MAXLINELENGTH,fic);
+ tmp->y=atoi(line);
+ fgets(line,MAXLINELENGTH,fic);
+ tmp->a=atoi(line);
+ fgets(line,MAXLINELENGTH,fic);
+ tmp->best_time=atoi(line);
+ fgets(line,MAXLINELENGTH,fic);
+ tmp->best_pseudo=(char *)malloc(strlen(line)+1);
+ strcpy(tmp->best_pseudo,line);
+ tmp->full=(char *)malloc(strlen(line)+20);
+ sprintf(tmp->full,"tracks/%s.png",tmp->name);
+ tmp->function=(char *)malloc(strlen(line)+30);
+ sprintf(tmp->function,"tracks/%s_function.png",tmp->name);
+ tmp->prev=tracklist;
+ if (tmp->prev) tmp->prev->next=tmp;
+ tracklist=tmp;
+ /* skip one line */
+ fgets(line,MAXLINELENGTH,fic);
+ }
+ fclose(fic);
+ if (!tmp) { fprintf(stderr,"no circuits found !\n"); zeRace_exit(); }
+ while (tmp->prev) tmp=tmp->prev;
+ tmp->prev=tracklist;
+ tracklist->next=tmp;
+}
+
+
+/* check for a newer version online to warn the user */
+void zeRace_check_version()
+{
+ IPaddress ip;
+ TCPsocket tcpsock;
+ char *request=
+ "GET /zerace/version.php HTTP/1.0\n"
+ "Host: royale.zerezo.com\n"
+ "User-Agent: zeRace " VERSION "\n"
+ "\n";
+ char response[1024],*tmp,*version;
+ int len,result;
- if ((fic=fopen("zeRace.cfg","rt"))==NULL)
+ printf("checking version... ");
+ fflush(stdout);
+
+ if(SDLNet_ResolveHost(&ip,"royale.zerezo.com",80)==-1)
{
- fprintf(stderr,"can't open config file \"zeRace.cfg\"\n");
- exit(1);
+ fprintf(stderr,"SDLNet_ResolveHost: %s\n",SDLNet_GetError());
+ return;
+ }
+
+ tcpsock=SDLNet_TCP_Open(&ip);
+ if(!tcpsock)
+ {
+ fprintf(stderr,"SDLNet_TCP_Open: %s\n",SDLNet_GetError());
+ return;
}
- while (fgets(line,MAXLINELENGTH,fic))
+ len=strlen(request);
+ result=SDLNet_TCP_Send(tcpsock,request,len);
+ if(result<len)
+ fprintf(stderr,"SDLNet_TCP_Send: %s\n",SDLNet_GetError());
+ else
+ printf("done\n");
+
+ len=SDLNet_TCP_Recv(tcpsock,response,1024);
+ if (len<0)
+ fprintf(stderr,"SDLNet_TCP_Recv: %s\n",SDLNet_GetError());
+ else
{
- configlines++;
- if (*line!=';' && *line!='#' && *line!='/' && *line!='-' && *line!='\n')
+ tmp=response;
+ while (*tmp++!='\0') if (strncmp(tmp,"version=",8)==0)
{
- if ((sscanf(line,"%s : %s\n",(char *)&keyword,(char *)&value))!=2) { fprintf(stderr,"error in config file : each line must have the format \"keyword : value\" (line %d)\n",configlines); exit(1); }
-
- if (strcmp("pseudo",keyword)==0)
- strcpy(pseudo,value);
- else
-
- if (strcmp("url",keyword)==0)
- strcpy(url,value);
- else
-
- if (strcmp("fullscreen",keyword)==0)
- {
- if (strcmp("no",value)==0) fullscreen=0;
- else if (strcmp("yes",value)==0) fullscreen=1;
- else { fprintf(stderr,"unknown value for \"fullscreen\" option, must be \"yes\" or \"no\"\n"); exit(1); }
- }
- else
-
- if (strcmp("sound",keyword)==0)
- {
- if (strcmp("no",value)==0) sound=0;
- else if (strcmp("yes",value)==0) sound=1;
- else { fprintf(stderr,"unknown value for \"sound\" option, must be \"yes\" or \"no\"\n"); exit(1); }
- }
-
- else { fprintf(stderr,"error in config file : \"%s\" is an unknown keyword (line %d)\n",keyword,configlines); exit(1); }
+ version=tmp+8;
+ while (*tmp!='\n') tmp++;
+ *tmp='\0';
+ if (strcmp(version,VERSION)>0) printf("new version available !\nhttp://royale.zerezo.com/zerace/\n");
}
}
+
+ SDLNet_TCP_Close(tcpsock);
}
-void zeRace_get_tracks()
+/* get remote list of tracks */
+void zeRace_update_tracks()
{
- struct dirent **namelist;
- int i;
- unsigned char ext[5];
+ IPaddress ip;
+ TCPsocket tcpsock;
+ char *request=
+ "GET /zerace/tracks.php HTTP/1.0\n"
+ "Host: royale.zerezo.com\n"
+ "User-Agent: zeRace " VERSION "\n"
+ "\n";
+ char response[10240],*tmp;
+ int len,result;
FILE *fic;
- char *shortname=NULL;
- char line[MAXLINELENGTH];
- int configlines;
- struct track *tmp=NULL,*first=NULL;
- DIR *dirp;
- struct dirent *dp;
+
+ printf("checking version and updating tracks... ");
+ fflush(stdout);
- if ((dirp=opendir("tracks"))==NULL) { fprintf(stderr,"can't open \"tracks\" directory or no tracks\n"); return; }
+ if(SDLNet_ResolveHost(&ip,"royale.zerezo.com",80)==-1)
+ {
+ fprintf(stderr,"SDLNet_ResolveHost: %s\n",SDLNet_GetError());
+ return;
+ }
- while (dp=readdir(dirp)) if (strlen(dp->d_name)>4)
+ tcpsock=SDLNet_TCP_Open(&ip);
+ if(!tcpsock)
+ {
+ fprintf(stderr,"SDLNet_TCP_Open: %s\n",SDLNet_GetError());
+ return;
+ }
+
+ len=strlen(request);
+ result=SDLNet_TCP_Send(tcpsock,request,len);
+ if(result<len)
+ fprintf(stderr,"SDLNet_TCP_Send: %s\n",SDLNet_GetError());
+ else
{
- shortname=(char *)realloc(shortname,strlen(dp->d_name)-3);
- strncpy(shortname,dp->d_name,strlen(dp->d_name)-4);
- shortname[strlen(dp->d_name)-4]='\0';
- for (i=0;i<5;i++) ext[i]=tolower(dp->d_name[strlen(dp->d_name)-4+i]);
- if (strcmp(".txt",ext)==0)
+ if ((fic=fopen("tracks/list.txt","wt"))==NULL)
{
- tmp=(struct track *)malloc(sizeof(struct track));
- if (first==NULL) first=tmp;
- tmp->name=(char *)malloc(strlen(shortname)+1);
- strcpy(tmp->name,shortname);
- tmp->title="";
- tmp->author="";
- tmp->version="";
- tmp->x=10;
- tmp->y=10;
- tmp->a=0;
- configlines=0;
- sprintf(line,"tracks/%s.txt",shortname);
- if ((fic=fopen(line,"rt"))==NULL)
- {
- fprintf(stderr,"can't open track file \"%s\"\n",line);
- zeRace_exit();
- }
- while (fgets(line,MAXLINELENGTH,fic))
- {
- configlines++;
- if (*line!=';' && *line!='#' && *line!='/' && *line!='-' && *line!='\n')
- {
- for (i=0;i<strlen(line)-2;i++) if (line[i]==' ' && line[i+1]==':' && line[i+2]==' ') break;
- if (line[i]!=' ' || line[i+1]!=':' || line[i+2]!=' ')
- {
- fprintf(stderr,"error in track file \"%s\" : each line must have the format \"keyword : value\" (line %d)\n",shortname,configlines);
- exit(1);
- }
- line[i]='\0';
-
- if (strcmp("title",line)==0)
- {
- tmp->title=(char *)malloc(strlen(line+i+3)+1);
- strcpy(tmp->title,line+i+3);
- }
- else
-
- if (strcmp("author",line)==0)
- {
- tmp->author=(char *)malloc(strlen(line+i+3)+1);
- strcpy(tmp->author,line+i+3);
- }
- else
-
- if (strcmp("x",line)==0)
- tmp->x=atoi(line+i+3);
- else
-
- if (strcmp("y",line)==0)
- tmp->y=atoi(line+i+3);
- else
-
- if (strcmp("a",line)==0)
- tmp->a=atoi(line+i+3);
- else
-
- if (strcmp("version",line)==0)
- {
- tmp->version=(char *)malloc(strlen(line+i+3)+1);
- strcpy(tmp->version,line+i+3);
- }
-
- else { fprintf(stderr,"error in track file \"%s\" : \"%s\" is an unknown keyword (line %d)\n",shortname,line,configlines); exit(1); }
- }
- }
- fclose(fic);
- tmp->full=(char *)malloc(strlen(line)+20);
- sprintf(tmp->full,"tracks/%s.png",shortname);
- tmp->function=(char *)malloc(strlen(line)+30);
- sprintf(tmp->function,"tracks/%s_function.png",shortname);
- tmp->prev=tracklist;
- if (tmp->prev) tmp->prev->next=tmp;
- tracklist=tmp;
+ fprintf(stderr,"can't create track list\n");
+ zeRace_exit();
}
+ len=SDLNet_TCP_Recv(tcpsock,response,10240);
+ tmp=response;
+ while (*tmp!='\n' || *(tmp+1)!='\r') tmp++;
+ tmp+=3;
+ fwrite(tmp,1,len+response-tmp,fic);
+ fclose(fic);
+ printf("done\n");
}
- if (!tmp) { fprintf(stderr,"no circuits found !\n"); zeRace_exit(); }
- while (tmp->prev) tmp=tmp->prev;
- tmp->prev=tracklist;
- tracklist->next=tmp;
+
+ SDLNet_TCP_Close(tcpsock);
}
-void zeRace_init()
+/* load the car sprite and rotate it for every angles */
+void zeRace_generate_cars()
{
+ int i;
SDL_Surface *car;
- int i,flags;
+ char temp[20]="sprites/carX.png";
+ temp[11]='A'+config.color;
+ /* load the car sprite */
+ car=IMG_Load(temp);
+ /* and rotate it for all available angles */
+ for (i=0;i<256;i++)
+ {
+ float x,y;
+ float tcos,tsin;
+ tcos=cos(2*M_PI*i/256);
+ tsin=sin(2*M_PI*i/256);
+ for (x=0;x<cars[i]->w;x++) for (y=0;y<cars[i]->h;y++)
+ {
+ int x2,y2;
+ x2=(x-cars[i]->w/2.0)*tcos+(y-cars[i]->h/2.0)*tsin+car->w/2.0;
+ y2=(x-cars[i]->w/2.0)*tsin-(y-cars[i]->h/2.0)*tcos+car->h/2.0;
+ if (x2>0 && x2<car->w && y2>0 && y2<car->h)
+ putpixel(cars[i],x,y,getpixel(car,x2,y2));
+ }
+ }
+ SDL_FreeSurface(car);
+}
+
+
+/* initialize the game */
+void zeRace_init()
+{
+ int flags,i;
+ /* do a clean exit in case of emergency */
signal(SIGINT,zeRace_exit);
signal(SIGTERM,zeRace_exit);
+
+ /* read the user configuration file */
zeRace_read_config();
- //zeRace_check_version();
+
+ /* check for a newer available version */
+ zeRace_check_version();
+
+ /* update the list of tracks */
+ zeRace_update_tracks();
+
+ /* get the list of local tracks */
zeRace_get_tracks();
- //zeRace_get_ghosts();
srand(time(NULL));
}
flags=SDL_HWSURFACE|SDL_ANYFORMAT;
- if (fullscreen) flags|=SDL_FULLSCREEN;
+ if (config.fullscreen) flags|=SDL_FULLSCREEN;
if ((screen=SDL_SetVideoMode(WIDTH,HEIGHT,32,flags))==NULL)
{
SDL_WM_SetCaption("zeRace " VERSION,"zeRace " VERSION);
SDL_ShowCursor(SDL_DISABLE);
- if (sound) if (Mix_OpenAudio(44100,MIX_DEFAULT_FORMAT,2,512)<0)
+ if (config.sound) if (Mix_OpenAudio(44100,MIX_DEFAULT_FORMAT,2,512)<0)
{
fprintf(stderr,"Mix_OpenAudio error\n");
zeRace_exit();
}
- car=IMG_Load("sprites/car.png");
- for (i=0;i<256;i++)
+ /* allocate memory for car sprites */
+ for (i=0;i<256;i++) if ((cars[i]=SDL_CreateRGBSurface(SDL_SWSURFACE,30,30,32,0x000000ff,0x0000ff00,0x00ff0000,0xff000000))==NULL)
{
- float x,y;
- float tcos,tsin;
- cars[i]=SDL_CreateRGBSurface(SDL_SWSURFACE,car->h*2,car->h*2,32,0x000000ff,0x0000ff00,0x00ff0000,0xff000000);
- if(cars[i]==NULL)
- {
- fprintf(stderr,"CreateRGBSurface failed: %s\n",SDL_GetError());
- exit(1);
- }
- tcos=cos(2*M_PI*i/256);
- tsin=sin(2*M_PI*i/256);
- for (x=0;x<cars[i]->w;x++) for (y=0;y<cars[i]->h;y++)
- {
- int x2,y2;
- Uint32 col;
- x2=(x-cars[i]->w/2.0)*tcos+(y-cars[i]->h/2.0)*tsin+car->w/2.0;
- y2=(x-cars[i]->w/2.0)*tsin-(y-cars[i]->h/2.0)*tcos+car->h/2.0;
- if (x2>0 && x2<car->w && y2>0 && y2<car->h)
- putpixel(cars[i],x,y,getpixel(car,x2,y2));
- }
- }
+ fprintf(stderr,"CreateRGBSurface failed: %s\n",SDL_GetError());
+ zeRace_exit();
+ };
+ zeRace_generate_cars();
}
+/* send the best time for this race to the web server */
void zeRace_send_time(float x,float y,float speed,float angle,int btime,char *bkeys)
{
IPaddress ip;
TCPsocket tcpsock;
char *temp;
char *msg1=
- "POST /zerace/time.php HTTP/1.1\n"
+ "POST /zerace/time.php HTTP/1.0\n"
"Host: royale.zerezo.com\n"
"User-Agent: zeRace " VERSION "\n"
"Content-Type: application/x-www-form-urlencoded\n"
return;
}
- temp=(char *)malloc(strlen(msg1)+strlen(pseudo)+strlen(msg2)+strlen(url)+strlen(msg3)+strlen(tracklist->name)+strlen(msg4)+10+strlen(msg5)+10+strlen(msg6)+10+strlen(msg7)+10+strlen(msg8)+10+strlen(msg9)+strlen(bkeys)+100);
- sprintf(temp,"%s%s%s%s%s%s%s%d%s%f%s%f%s%f%s%f%s%s\n",msg1,pseudo,msg2,url,msg3,tracklist->name,msg4,btime,msg5,x,msg6,y,msg7,speed,msg8,angle,msg9,bkeys);
+ temp=(char *)malloc(strlen(msg1)+strlen(config.pseudo)+strlen(msg2)+strlen(config.url)+strlen(msg3)+strlen(tracklist->name)+strlen(msg4)+10+strlen(msg5)+10+strlen(msg6)+10+strlen(msg7)+10+strlen(msg8)+10+strlen(msg9)+strlen(bkeys)+100);
+ sprintf(temp,"%s%s%s%s%s%s%s%d%s%f%s%f%s%f%s%f%s%s\n",msg1,config.pseudo,msg2,config.url,msg3,tracklist->name,msg4,btime,msg5,x,msg6,y,msg7,speed,msg8,angle,msg9,bkeys);
len=strlen(temp);
result=SDLNet_TCP_Send(tcpsock,temp,len);
if(result<len)
fprintf(stderr,"SDLNet_TCP_Send: %s\n", SDLNet_GetError());
else
- printf(" done\n");
+ printf("done\n");
SDLNet_TCP_Close(tcpsock);
}
+/* print a time for a lap */
+void print_time(int x,int y,int time)
+{
+ char temp[20];
+ int q,r;
+ time*=DELAY;
+ q=(time/1000);
+ r=(time%1000);
+ if (q>100) return;
+ temp[0]=q/10+'0';
+ temp[1]=q%10+'0';
+ temp[2]='"';
+ temp[3]=r/100+'0';
+ temp[4]=r%100/10+'0';
+ temp[5]=r%10+'0';
+ temp[6]='\0';
+ print(screen,x,y,temp);
+}
+
+
+/* launch a new race */
void zeRace_launch()
{
SDL_Surface *cir,*fun;
float x,y,angle,speed;
float sx,sy,sangle,sspeed;
float bx,by,bangle,bspeed;
- int c,r,v,b;
- int temp;
+ int c,r,g,b;
char keys[10000];
char bkeys[10000];
- char text[10];
- Mix_Music *light,*engine,*crash,*slide;
+ Mix_Music *light,*engine,*crash,*slide;
int lastsound_time=-999,alltime=0,lastsound=0;
cir=IMG_Load(tracklist->full);
lastcheck=0;
time=0;
- if (sound) if (!(light=Mix_LoadMUS("sounds/light.wav")) || !(engine=Mix_LoadMUS("sounds/engine.wav")) || !(crash=Mix_LoadMUS("sounds/crash.wav")) || !(slide=Mix_LoadMUS("sounds/slide.wav")))
+ if (config.sound) if (!(light=Mix_LoadMUS("sounds/light.wav")) || !(engine=Mix_LoadMUS("sounds/engine.wav")) || !(crash=Mix_LoadMUS("sounds/crash.wav")) || !(slide=Mix_LoadMUS("sounds/slide.wav")))
{
fprintf(stderr,"Mix_LoadMUS error\n");
zeRace_exit();
}
+ /* startup countdown */
for (i=4;i>=-1;i--)
{
char startup[15]="sprites/?.png";
SDL_BlitSurface(temp,NULL,screen,&pos);
SDL_FreeSurface(temp);
}
- if (sound) if (i!=4) Mix_PlayMusic(light,1);
+ if (config.sound) if (i!=4) Mix_PlayMusic(light,1);
SDL_Flip(screen);
if (i!=-1) SDL_Delay(1000);
}
+ /* main loop */
for (;;)
- {
+ {
+ /* clear the old position */
size.w=cars[0]->w;
size.h=cars[0]->h;
pos.x=x;
pos.y=y;
SDL_BlitSurface(cir,&pos,screen,&pos);
+ /* save the old position and compute the new one */
ox=x;
oy=y;
speed*=0.995;
x=x-cos(angle)*speed;
y=y-sin(angle)*speed;
+ /* collision with the border of the screen */
if (x<0 || x>screen->w-cars[0]->w || y<0 || y>screen->h-cars[0]->h)
{
x=ox;
speed=0;
}
+ /* display the car at the new position and update display */
pos.x=x;
pos.y=y;
i=(unsigned char)(256*angle/2.0/M_PI)%256;
SDL_UpdateRect(screen,ox,oy,cars[i]->w,cars[i]->h);
SDL_UpdateRect(screen,x,y,cars[i]->w,cars[i]->h);
- if (kl) angle-=0.01;
- if (kr) angle+=0.01;
- if (ku) speed+=0.01*2;
- if (kd) speed-=0.01;
+ /* accelerate, brake and turn depending on the pressed keys */
+ if (kl) { if (speed<0) angle+=0.01*(255-b)/255*COEFF; else angle-=0.01*(255-b)/255*COEFF; }
+ if (kr) { if (speed<0) angle-=0.01*(255-b)/255*COEFF; else angle+=0.01*(255-b)/255*COEFF; }
+ if (ku) speed+=0.01*2*COEFF;
+ if (kd) speed-=0.01*COEFF;
+ /* play engine sound if no sound is currently playing */
if (lastsound_time+100<alltime)
{
lastsound=0;
lastsound_time=alltime;
- if (sound) Mix_PlayMusic(engine,1);
+ if (config.sound) Mix_PlayMusic(engine,1);
}
- if (kd && speed>0.5 || speed>2.0 && !ku)
+ /* if the car is fast or braking, it slides */
+ if ((kd && speed>0.5) || (speed>2.0 && !ku))
{
-
+ /* if the only sound is the engine, play the slide sound */
if (lastsound_time+100<alltime || lastsound<1)
{
lastsound=1;
lastsound_time=alltime;
- if (sound) Mix_PlayMusic(slide,1)==-1;
+ if (config.sound) Mix_PlayMusic(slide,1)==-1;
}
- putpixel(cir,x+cars[i]->w/2+cos(angle)*cars[i]->w/3-sin(angle)*2,y+cars[i]->h/2+sin(angle)*cars[i]->h/3+cos(angle)*2,0);
- putpixel(cir,x+cars[i]->w/2+cos(angle)*cars[i]->w/3+sin(angle)*5,y+cars[i]->h/2+sin(angle)*cars[i]->h/3-cos(angle)*5,0);
- if (kd)
+ /* display tires slide */
+ if (config.tire)
{
- putpixel(cir,x+cars[i]->w/2+cos(angle)*cars[i]->w/3-sin(angle)*3,y+cars[i]->h/2+sin(angle)*cars[i]->h/3+cos(angle)*3,0);
- putpixel(cir,x+cars[i]->w/2+cos(angle)*cars[i]->w/3+sin(angle)*4,y+cars[i]->h/2+sin(angle)*cars[i]->h/3-cos(angle)*4,0);
+ putpixel(cir,x+cars[i]->w/2+cos(angle)*cars[i]->w/3-sin(angle)*2,y+cars[i]->h/2+sin(angle)*cars[i]->h/3+cos(angle)*2,0);
+ putpixel(cir,x+cars[i]->w/2+cos(angle)*cars[i]->w/3+sin(angle)*5,y+cars[i]->h/2+sin(angle)*cars[i]->h/3-cos(angle)*5,0);
+ /* if we are braking the slide is larger */
+ if (kd)
+ {
+ putpixel(cir,x+cars[i]->w/2+cos(angle)*cars[i]->w/3-sin(angle)*3,y+cars[i]->h/2+sin(angle)*cars[i]->h/3+cos(angle)*3,0);
+ putpixel(cir,x+cars[i]->w/2+cos(angle)*cars[i]->w/3+sin(angle)*4,y+cars[i]->h/2+sin(angle)*cars[i]->h/3-cos(angle)*4,0);
+ }
}
}
-
+
+ /* get the pixel color under the center of car in the function map */
c=getpixel(fun,x+cars[i]->w/2,y+cars[i]->h/2);
- r=c&0x000000ff;
- v=(c&0x0000ff00)>>8;
-
- if (v==0)
+ /* red layer (checkpoints) */
+ r=(c )&0xff;
+ /* green layer (road quality) */
+ g=(c>>8 )&0xff;
+ /* blue layer (unused) */
+ b=(c>>16)&0xff;
+
+ /* if it is a wall we move back to the last position */
+ if (g==0)
{
x=ox;
y=oy;
+ /* play the crash sound */
if (lastsound_time+100<alltime || lastsound<2)
{
lastsound=2;
lastsound_time=alltime;
- if (sound) Mix_PlayMusic(crash,1)==-1;
+ if (config.sound) Mix_PlayMusic(crash,1)==-1;
}
}
- speed-=speed*(255-v)/1000;
+ /* update the speed depending on the road quality */
+ speed-=speed*(255-g)/1000;
+ /* if we are on the next checkpoint, validate it (no missing allowed) */
if (r/8==lastcheck+1) lastcheck++;
+ /* if we validate all and start over, we complete a turn */
if (r/8==0 && lastcheck==31)
{
- printf("time = %d\"%d\n",time*5/1000,time*5%1000);
+ printf("time = %d\"%d\n",time*DELAY/1000,time*DELAY%1000);
+ print(screen,0,0,"Last lap : ");
+ print_time(110,0,time);
+ SDL_UpdateRect(screen,0,0,170,19);
+ /* if it is the first turn of the best turn, save it */
if (btime==-1 || time<btime)
{
btime=time;
keys[time]='\0';
memcpy(bkeys,keys,btime);
}
+ /* reset turn variables */
lastcheck=0;
time=0;
sx=x;
sspeed=speed;
}
+ /* look for user interaction */
while (SDL_PollEvent(&event))
{
switch (event.type)
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
- case SDLK_ESCAPE: // escape
+ case SDLK_ESCAPE:
+ /* free memory */
Mix_FreeMusic(light);
Mix_FreeMusic(engine);
Mix_FreeMusic(crash);
Mix_FreeMusic(slide);
+ /* if the best time is small enought to save all keys, send it */
if (btime<10000) zeRace_send_time(bx,by,bspeed,bangle,btime,bkeys);
return;
- case SDLK_UP: //up
- ku=1;
- break;
- case SDLK_DOWN: //down
- kd=1;
- break;
- case SDLK_LEFT: //left
- kl=1;
- break;
- case SDLK_RIGHT: //right
- kr=1;
+ default:
+ i=event.key.keysym.sym;
+ if (i==config.up) ku=1;
+ if (i==config.down) kd=1;
+ if (i==config.left) kl=1;
+ if (i==config.right) kr=1;
break;
}
break;
case SDL_KEYUP:
- switch (event.key.keysym.sym)
- {
- case SDLK_UP: //up
- ku=0;
- break;
- case SDLK_DOWN: //down
- kd=0;
- break;
- case SDLK_LEFT: //left
- kl=0;
- break;
- case SDLK_RIGHT: //right
- kr=0;
- break;
- }
+ i=event.key.keysym.sym;
+ if (i==config.up) ku=0;
+ if (i==config.down) kd=0;
+ if (i==config.left) kl=0;
+ if (i==config.right) kr=0;
break;
}
}
- SDL_Delay(5);
+ /* let the system breath */
+ SDL_Delay(DELAY);
+ /* save pressed keys to validate best time */
if (time<btime) keys[time]=(ku<<3 | kd<<2 | kl<<1 | kr)+'A';
+ /* game time */
time++;
alltime++;
}
}
+/* display a random splash screen at startup */
void zeRace_splash()
{
SDL_Surface *splash;
SDL_Rect pos;
- int i;
char temp[20]="splashs/0.jpg";
SDL_FillRect(screen,NULL,0x000000);
}
-void zeRace_menu()
+/* menu loop to select track */
+void zeRace_select_track()
{
SDL_Event event;
print(screen,WIDTH/2-strlen(tracklist->title)*5,5*HEIGHT/6-20,tracklist->title);
print(screen,WIDTH/2-(strlen(tracklist->author)+strlen("Author : "))*5,5*HEIGHT/6+0,"Author : ");
print(screen,WIDTH/2-(strlen(tracklist->author)-strlen("Author : "))*5,5*HEIGHT/6+0,tracklist->author);
- print(screen,WIDTH/2-(strlen(tracklist->version)+strlen("Version : "))*5,5*HEIGHT/6+20,"Version : ");
- print(screen,WIDTH/2-(strlen(tracklist->version)-strlen("Version : "))*5,5*HEIGHT/6+20,tracklist->version);
+ print(screen,WIDTH/2-( strlen("Version : ")+strlen(tracklist->version))*5,5*HEIGHT/6+20,"Version : ");
+ print(screen,WIDTH/2-(-strlen("Version : ")+strlen(tracklist->version))*5,5*HEIGHT/6+20,tracklist->version);
+ print(screen,WIDTH/2-( strlen("Best time : ")+6+strlen(" by ")+strlen(tracklist->best_pseudo))*5,5*HEIGHT/6+40,"Best time : ");
+ print_time (WIDTH/2-(-strlen("Best time : ")+6+strlen(" by ")+strlen(tracklist->best_pseudo))*5,5*HEIGHT/6+40,tracklist->best_time);
+ print(screen,WIDTH/2-(-strlen("Best time : ")-6+strlen(" by ")+strlen(tracklist->best_pseudo))*5,5*HEIGHT/6+40," by ");
+ print(screen,WIDTH/2-(-strlen("Best time : ")-6-strlen(" by ")+strlen(tracklist->best_pseudo))*5,5*HEIGHT/6+40,tracklist->best_pseudo);
full=IMG_Load(tracklist->full);
preview=(SDL_Surface *)zoomSurface(full,0.5,0.5,1);
SDL_FreeSurface(full);
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
- case SDLK_ESCAPE: // escape
- zeRace_exit();
- break;
- case SDLK_RETURN: //enter
- case SDLK_SPACE: //space
+ case SDLK_ESCAPE:
+ return;
+ case SDLK_RETURN:
+ case SDLK_SPACE:
zeRace_launch();
update();
break;
- case SDLK_LEFT: //left
+ case SDLK_LEFT:
tracklist=tracklist->next;
update();
break;
- case SDLK_RIGHT: //right
+ case SDLK_RIGHT:
tracklist=tracklist->prev;
update();
break;
+ default:
+ break;
+ }
+ break;
+ }
+ }
+ SDL_Delay(10);
+ }
+}
+
+
+/* configuration screen */
+void zeRace_config()
+{
+ SDL_Event event;
+ int active=0;
+ #define CONFIG_OPTIONS 11
+
+ void update()
+ {
+ SDL_Rect pos;
+ SDL_FillRect(screen,NULL,0x000000);
+ print(screen,20,HEIGHT/(CONFIG_OPTIONS+4)*(3+active),">");
+ print(screen,WIDTH/2-24*5,HEIGHT/(CONFIG_OPTIONS+4),"* Configuration screen *");
+ print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*3,"Pseudo : ");
+ print(screen,40+10*strlen("Pseudo : "),HEIGHT/(CONFIG_OPTIONS+4)*3,config.pseudo);
+ print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*4,"Url : ");
+ print(screen,40+10*strlen("Url : "),HEIGHT/(CONFIG_OPTIONS+4)*4,config.url);
+ print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*5,"Fullscreen : ");
+ print(screen,40+10*strlen("Fullscreen : "),HEIGHT/(CONFIG_OPTIONS+4)*5,config.fullscreen?"Yes":"No");
+ print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*6,"Sound : ");
+ print(screen,40+10*strlen("Sound : "),HEIGHT/(CONFIG_OPTIONS+4)*6,config.sound?"Yes":"No");
+ print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*7,"Tire : ");
+ print(screen,40+10*strlen("Tire : "),HEIGHT/(CONFIG_OPTIONS+4)*7,config.tire?"Yes":"No");
+ print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*8,"Accelerate key : ");
+ print(screen,40+10*strlen("Accelerate key : "),HEIGHT/(CONFIG_OPTIONS+4)*8,config.up?SDL_GetKeyName(config.up):"<press key>");
+ print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*9,"Brake key : ");
+ print(screen,40+10*strlen("Brake key : "),HEIGHT/(CONFIG_OPTIONS+4)*9,config.down?SDL_GetKeyName(config.down):"<press key>");
+ print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*10,"Turn left key : ");
+ print(screen,40+10*strlen("Turn left key : "),HEIGHT/(CONFIG_OPTIONS+4)*10,config.left?SDL_GetKeyName(config.left):"<press key>");
+ print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*11,"Turn right key : ");
+ print(screen,40+10*strlen("Turn right key : "),HEIGHT/(CONFIG_OPTIONS+4)*11,config.right?SDL_GetKeyName(config.right):"<press key>");
+ print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*12,"Color : ");
+ pos.x=123;
+ pos.y=HEIGHT/(CONFIG_OPTIONS+4)*12-7;
+ SDL_BlitSurface(cars[0],NULL,screen,&pos);
+ print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*(CONFIG_OPTIONS+2),"Back to main menu");
+ SDL_Flip(screen);
+ }
+
+ int read_key()
+ {
+ for (;;)
+ {
+ while (SDL_PollEvent(&event)) switch (event.type)
+ {
+ case SDL_KEYDOWN:
+ return event.key.keysym.sym;
+ }
+ SDL_Delay(10);
+ }
+ }
+
+ SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,SDL_DEFAULT_REPEAT_INTERVAL);
+ update();
+ for (;;)
+ {
+ while (SDL_PollEvent(&event))
+ {
+ switch (event.type)
+ {
+ case SDL_QUIT:
+ zeRace_exit();
+ break;
+ case SDL_KEYDOWN:
+ switch (event.key.keysym.sym)
+ {
+ case SDLK_ESCAPE:
+ SDL_EnableKeyRepeat(0,0);
+ return;
+ case SDLK_RETURN:
+ case SDLK_SPACE:
+ case SDLK_LEFT:
+ case SDLK_RIGHT:
+ switch (active)
+ {
+ case 0: readstring(screen,40+10*strlen("Pseudo : "),HEIGHT/(CONFIG_OPTIONS+4)*3,config.pseudo,MAXLINELENGTH); break;
+ case 1: readstring(screen,40+10*strlen("Url : "),HEIGHT/(CONFIG_OPTIONS+4)*4,config.url,MAXLINELENGTH); break;;
+ case 2: config.fullscreen=!config.fullscreen; break;
+ case 3: config.sound=!config.sound; break;
+ case 4: config.tire=!config.tire; break;
+ case 5: config.up=0; update(); config.up=read_key(); break;
+ case 6: config.down=0; update(); config.down=read_key(); break;
+ case 7: config.left=0; update(); config.left=read_key(); break;
+ case 8: config.right=0; update(); config.right=read_key(); break;
+ case 9:
+ if (event.key.keysym.sym==SDLK_LEFT) config.color--; else config.color++;
+ if (config.color<0) config.color=11;
+ if (config.color>11) config.color=0;
+ zeRace_generate_cars();
+ break;
+ case 10:
+ SDL_EnableKeyRepeat(0,0);
+ return;
+ }
+ update();
+ break;
+ case SDLK_UP:
+ active--; if (active<0) active=CONFIG_OPTIONS-1;
+ update();
+ break;
+ case SDLK_DOWN:
+ active++; if (active>CONFIG_OPTIONS-1) active=0;
+ update();
+ break;
+ default:
+ break;
+ }
+ break;
+ }
+ }
+ SDL_Delay(10);
+ }
+}
+
+
+/* main menu */
+void zeRace_menu()
+{
+ SDL_Event event;
+ int active=0;
+ SDL_Surface *logo;
+ #define MENU_OPTIONS 3
+
+ void update()
+ {
+ SDL_Rect pos;
+ SDL_FillRect(screen,NULL,0x000000);
+ pos.x=WIDTH/2-logo->w/2;
+ pos.y=HEIGHT/6-logo->h/2;
+ SDL_BlitSurface(logo,NULL,screen,&pos);
+ print(screen,650,HEIGHT/(MENU_OPTIONS+4)*2,"version " VERSION);
+ print(screen,420,HEIGHT/(MENU_OPTIONS+4)*(3+active),">");
+ print(screen,440,HEIGHT/(MENU_OPTIONS+4)*3,"Local game");
+ /* print(screen,440,HEIGHT/(MENU_OPTIONS+4)*4,"Network game"); */
+ print(screen,440,HEIGHT/(MENU_OPTIONS+4)*4,"Configuration");
+ print(screen,440,HEIGHT/(MENU_OPTIONS+4)*5,"Exit game");
+ SDL_Flip(screen);
+ }
+
+ logo=IMG_Load("sprites/logo.jpg");
+ SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,SDL_DEFAULT_REPEAT_INTERVAL);
+ update();
+ for (;;)
+ {
+ while (SDL_PollEvent(&event))
+ {
+ switch (event.type)
+ {
+ case SDL_QUIT:
+ zeRace_exit();
+ break;
+ case SDL_KEYDOWN:
+ switch (event.key.keysym.sym)
+ {
+ case SDLK_ESCAPE:
+ SDL_EnableKeyRepeat(0,0);
+ return;
+ case SDLK_RETURN:
+ case SDLK_SPACE:
+ case SDLK_LEFT:
+ case SDLK_RIGHT:
+ switch (active)
+ {
+ case 0: zeRace_select_track(); break;
+ case 1: zeRace_config(); break;
+ case 2: return;
+ }
+ update();
+ break;
+ case SDLK_UP:
+ active--; if (active<0) active=MENU_OPTIONS-1;
+ update();
+ break;
+ case SDLK_DOWN:
+ active++; if (active>MENU_OPTIONS-1) active=0;
+ update();
+ break;
+ default:
+ break;
}
break;
}
}
+/* main program */
int main(int argc,char *argv[])
{
zeRace_init();