2 * zeRace 0.6, a funny retro racing game
3 * http://royale.zerezo.com/zerace/
5 * Copyright (C) 2004 Antoine Jacquet <royale@zerezo.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <SDL_image.h>
30 #include <SDL_mixer.h>
31 #include <SDL_rotozoom.h>
34 #include "tracklist.h"
38 /* configuration constants */
43 #define MAXRECORDKEYS 9999
46 /* some usefull colors */
47 #define C_BLACK SDL_MapRGB(screen->format,0,0,0)
48 #define C_WHITE SDL_MapRGB(screen->format,255,255,255)
49 #define C_RED SDL_MapRGB(screen->format,255,0,0)
50 #define C_ORANGE SDL_MapRGB(screen->format,255,200,0)
51 #define C_YELLOW SDL_MapRGB(screen->format,255,255,100)
52 #define T_BLACK SDL_MapRGB(cir->format,0,0,0)
55 struct _tracklist *tracklist;
60 char pseudo[MAXLINELENGTH];
61 char url[MAXLINELENGTH];
72 } config = {"anonymous","",0,0,1,SDLK_UP,SDLK_DOWN,SDLK_LEFT,SDLK_RIGHT,6,SDLK_b,0};
74 /* full script for a lap */
77 float x,y,angle,speed;
78 char keys[MAXRECORDKEYS];
82 /* display and all directions for the car */
84 SDL_Surface *cars[NB_CARS][256];
87 UDPsocket udpsock=NULL;
92 /* read the user configuration file */
93 void zeRace_read_config()
96 if ((fic=fopen("zeRace.cfg","rb"))==NULL)
98 fprintf(stderr,"can't open config file \"zeRace.cfg\"\n");
101 fread(&config,sizeof(struct _config),1,fic);
106 /* save the user configuration file */
107 void zeRace_save_config()
110 if ((fic=fopen("zeRace.cfg","wb"))==NULL)
112 fprintf(stderr,"can't create config file \"zeRace.cfg\"\n");
115 fwrite(&config,sizeof(struct _config),1,fic);
120 /* exit the game and clean */
124 if (config.sound) Mix_CloseAudio();
127 zeRace_save_config();
132 /* check for a newer version online to warn the user */
133 void zeRace_check_version()
138 "GET /zerace/version.php HTTP/1.0\n"
139 "Host: royale.zerezo.com\n"
140 "User-Agent: zeRace " VERSION "\n"
142 char response[1024],*tmp,*version;
145 printf("checking version... ");
148 if (SDLNet_ResolveHost(&ip,"royale.zerezo.com",80)==-1)
150 fprintf(stderr,"SDLNet_ResolveHost: %s\n",SDLNet_GetError());
154 tcpsock=SDLNet_TCP_Open(&ip);
157 fprintf(stderr,"SDLNet_TCP_Open: %s\n",SDLNet_GetError());
162 result=SDLNet_TCP_Send(tcpsock,request,len);
164 fprintf(stderr,"SDLNet_TCP_Send: %s\n",SDLNet_GetError());
168 len=SDLNet_TCP_Recv(tcpsock,response,1024);
170 fprintf(stderr,"SDLNet_TCP_Recv: %s\n",SDLNet_GetError());
174 while (*tmp++!='\0') if (strncmp(tmp,"version=",8)==0)
177 while (*tmp!='\n') tmp++;
179 if (strcmp(version,VERSION)>0) printf("new version available !\nhttp://royale.zerezo.com/zerace/\n");
183 SDLNet_TCP_Close(tcpsock);
187 /* get remote list of tracks */
188 void zeRace_update_tracks()
193 "GET /zerace/tracks.php HTTP/1.0\n"
194 "Host: royale.zerezo.com\n"
195 "User-Agent: zeRace " VERSION "\n"
197 char response[10240],*tmp;
201 printf("checking version and updating tracks... ");
204 if (SDLNet_ResolveHost(&ip,"royale.zerezo.com",80)==-1)
206 fprintf(stderr,"SDLNet_ResolveHost: %s\n",SDLNet_GetError());
210 tcpsock=SDLNet_TCP_Open(&ip);
213 fprintf(stderr,"SDLNet_TCP_Open: %s\n",SDLNet_GetError());
218 result=SDLNet_TCP_Send(tcpsock,request,len);
220 fprintf(stderr,"SDLNet_TCP_Send: %s\n",SDLNet_GetError());
223 if ((fic=fopen("tracks/list.txt","wt"))==NULL)
225 fprintf(stderr,"can't create track list\n");
228 len=SDLNet_TCP_Recv(tcpsock,response,10240);
230 while (*tmp!='\n' || *(tmp+1)!='\r') tmp++;
232 fwrite(tmp,1,len+response-tmp,fic);
237 SDLNet_TCP_Close(tcpsock);
241 /* download the file if it is missing */
242 void zeRace_download_file(char *file)
247 char response[10240],*tmp;
252 if (stat(file,&buf)<0)
254 printf("downloading file \"%s\" : ",file);
257 if (SDLNet_ResolveHost(&ip,"royale.zerezo.com",80)==-1)
259 fprintf(stderr,"SDLNet_ResolveHost: %s\n",SDLNet_GetError());
263 tcpsock=SDLNet_TCP_Open(&ip);
266 fprintf(stderr,"SDLNet_TCP_Open: %s\n",SDLNet_GetError());
271 "GET /zerace/%s HTTP/1.0\n"
272 "Host: royale.zerezo.com\n"
273 "User-Agent: zeRace " VERSION "\n"
276 result=SDLNet_TCP_Send(tcpsock,request,len);
278 fprintf(stderr,"SDLNet_TCP_Send: %s\n",SDLNet_GetError());
281 if ((fic=fopen(file,"wb"))==NULL)
283 fprintf(stderr,"can't create \"%s\" file\n",file);
286 len=SDLNet_TCP_Recv(tcpsock,response,10240);
288 while (*tmp!='\n' || *(tmp+1)!='\r') tmp++;
290 fwrite(tmp,1,len+response-tmp,fic);
291 while ((len=SDLNet_TCP_Recv(tcpsock,response,10240))) fwrite(response,1,len,fic);
296 SDLNet_TCP_Close(tcpsock);
301 /* load the car sprite and rotate it for every angles */
302 void zeRace_generate_cars()
306 char temp[20]="sprites/carX.png";
307 for (i=0;i<NB_CARS;i++)
310 /* load the car sprite */
312 /* and rotate it for all available angles */
317 if ((cars[i][j]=SDL_CreateRGBSurface(SDL_SWSURFACE,30,30,32,RMASK,GMASK,BMASK,AMASK))==NULL)
319 fprintf(stderr,"CreateRGBSurface failed: %s\n",SDL_GetError());
322 tcos=cos(2*M_PI*j/256);
323 tsin=sin(2*M_PI*j/256);
324 for (x=0;x<cars[i][j]->w;x++) for (y=0;y<cars[i][j]->h;y++)
327 x2=(x-cars[i][j]->w/2.0)*tcos+(y-cars[i][j]->h/2.0)*tsin+car->w/2.0;
328 y2=(x-cars[i][j]->w/2.0)*tsin-(y-cars[i][j]->h/2.0)*tcos+car->h/2.0;
329 if (x2>0 && x2<car->w && y2>0 && y2<car->h)
330 putpixel(cars[i][j],x,y,getpixel(car,x2,y2));
333 SDL_FreeSurface(car);
338 /* initialize the game */
342 struct _tracklist *loopcheck;
344 /* do a clean exit in case of emergency */
345 signal(SIGINT,zeRace_exit);
346 signal(SIGTERM,zeRace_exit);
349 if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)<0)
351 fprintf(stderr,"could not initialize SDL : %s\n",SDL_GetError());
356 /* initialize SDLNet */
357 if (SDLNet_Init()==-1)
359 fprintf(stderr,"could not initialize SDLNet : %s\n",SDLNet_GetError());
363 /* read the user configuration file */
364 zeRace_read_config();
366 /* check for a newer available version */
367 zeRace_check_version();
369 /* update the list of tracks */
370 zeRace_update_tracks();
372 /* get the list of local tracks */
373 if (!zeRace_get_tracks(&tracklist)) zeRace_exit();
375 /* download missing files */
379 zeRace_download_file(tracklist->full);
380 zeRace_download_file(tracklist->function);
381 tracklist=tracklist->next;
382 } while (tracklist!=loopcheck);
386 packet=SDLNet_AllocPacket(1024);
389 fprintf(stderr,"SDLNet_AllocPacket: %s\n",SDLNet_GetError());
393 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,SDL_DEFAULT_REPEAT_INTERVAL);
395 flags=SDL_HWSURFACE|SDL_ANYFORMAT;
396 if (config.fullscreen) flags|=SDL_FULLSCREEN;
398 if ((screen=SDL_SetVideoMode(WIDTH,HEIGHT,32,flags))==NULL)
400 fprintf(stderr,"could not create a surface : %s\n",SDL_GetError());
404 SDL_WM_SetIcon(SDL_LoadBMP("icon.bmp"),NULL);
405 SDL_WM_SetCaption("zeRace " VERSION,"zeRace " VERSION);
406 SDL_ShowCursor(SDL_DISABLE);
408 if (config.sound) if (Mix_OpenAudio(44100,MIX_DEFAULT_FORMAT,2,512)<0)
410 fprintf(stderr,"Mix_OpenAudio error\n");
414 /* pre-calculate car sprites */
415 zeRace_generate_cars();
419 /* send the best time for this race to the web server */
420 void zeRace_send_time(struct _record *record)
426 "POST /zerace/time.php HTTP/1.0\n"
427 "Host: royale.zerezo.com\n"
428 "User-Agent: zeRace " VERSION "\n"
429 "Content-Type: application/x-www-form-urlencoded\n"
430 "Content-Length: 99999\n"
434 char *msg3="&track=";
435 char *msg4="&btime=";
438 char *msg7="&speed=";
439 char *msg8="&angle=";
440 char *msg9="&bkeys=";
443 /* if the best time is small enought to save all keys, send it */
444 if (record->time>=MAXRECORDKEYS) return;
446 printf("sending time... ");
449 if (SDLNet_ResolveHost(&ip,"royale.zerezo.com",80)==-1)
451 fprintf(stderr,"SDLNet_ResolveHost: %s\n",SDLNet_GetError());
455 tcpsock=SDLNet_TCP_Open(&ip);
458 fprintf(stderr,"SDLNet_TCP_Open: %s\n",SDLNet_GetError());
462 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(record->keys)+100);
463 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,record->time,msg5,record->x,msg6,record->y,msg7,record->speed,msg8,record->angle,msg9,record->keys);
466 result=SDLNet_TCP_Send(tcpsock,temp,len);
468 fprintf(stderr,"SDLNet_TCP_Send: %s\n", SDLNet_GetError());
472 SDLNet_TCP_Close(tcpsock);
476 /* print a time for a lap */
477 void print_time(int x,int y,int time)
489 temp[4]=r%100/10+'0';
492 print(screen,x,y,temp);
497 void lights(int x,int y,int r,Uint32 pixel)
499 putpixel(screen,x,y,pixel);
502 putpixel(screen,x-1,y,pixel);
503 putpixel(screen,x+1,y,pixel);
504 putpixel(screen,x,y-1,pixel);
505 putpixel(screen,x,y+1,pixel);
509 putpixel(screen,x-2,y,pixel);
510 putpixel(screen,x+2,y,pixel);
511 putpixel(screen,x,y-2,pixel);
512 putpixel(screen,x,y+2,pixel);
513 putpixel(screen,x-1,y-1,pixel);
514 putpixel(screen,x-1,y+1,pixel);
515 putpixel(screen,x+1,y-1,pixel);
516 putpixel(screen,x+1,y+1,pixel);
521 /* launch a new race */
522 void zeRace_launch(int alltime,int go)
524 SDL_Surface *cir,*fun,*hilight;
527 int ku=0,kd=0,kl=0,kr=0,i;
529 struct _record current,best;
530 Mix_Music *light,*engine,*crash,*slide;
531 int lastsound_time=-999,lastsound=0;
535 struct _car oldnetpos[MAX_CLIENTS],newnetpos[MAX_CLIENTS];
541 SDL_FreeSurface(cir);
542 SDL_FreeSurface(fun);
545 Mix_FreeMusic(light);
546 Mix_FreeMusic(engine);
547 Mix_FreeMusic(crash);
548 Mix_FreeMusic(slide);
553 cir=IMG_Load(tracklist->full);
554 /* dark the track if it is night */
557 for (pos.x=0;pos.x<screen->w;pos.x++)
558 for (pos.y=0;pos.y<screen->h;pos.y++)
562 c=getpixel(cir,pos.x,pos.y);
563 SDL_GetRGB(c,cir->format,&r,&g,&b);
567 putpixel(cir,pos.x,pos.y,SDL_MapRGB(cir->format,r,g,b));
570 fun=IMG_Load(tracklist->function);
571 hilight=IMG_Load("sprites/light.png");
573 current.speed=car.speed=0;
574 current.angle=car.angle=tracklist->a*2*M_PI/360;
575 current.x=car.ox=car.x=tracklist->x;
576 current.y=car.oy=car.y=tracklist->y;
582 best.time=MAXRECORDKEYS;
584 memset(oldnetpos,0,MAX_CLIENTS*sizeof(struct _car));
585 memset(newnetpos,0,MAX_CLIENTS*sizeof(struct _car));
587 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")))
589 fprintf(stderr,"Mix_LoadMUS error\n");
593 /* startup countdown */
596 char startup[15]="sprites/?.png";
601 SDL_BlitSurface(cir,NULL,screen,NULL);
602 SDL_BlitSurface(cars[config.color][(unsigned char)(256*car.angle/2.0/M_PI)%256],NULL,screen,&pos);
605 temp=IMG_Load(startup);
606 pos.x=screen->w/2-temp->w/2;
607 pos.y=screen->h/2-temp->h/2;
608 SDL_BlitSurface(temp,NULL,screen,&pos);
609 SDL_FreeSurface(temp);
611 if (config.sound) if (i!=4) Mix_PlayMusic(light,1);
614 if (i!=-1) SDL_Delay(1000);
620 /* look for user interaction */
621 while (SDL_PollEvent(&event))
629 switch (event.key.keysym.sym)
634 print(screen,WIDTH/2-strlen("Disconnecting !")*5,HEIGHT/2-10,"Disconnecting !");
635 strcpy(packet->data,"disconnect");
636 packet->len=strlen(packet->data)+1;
637 SDLNet_UDP_Send(udpsock,-1,packet);
640 zeRace_send_time(&best);
644 i=event.key.keysym.sym;
645 if (i==config.up) ku=1;
646 if (i==config.down) kd=1;
647 if (i==config.left) kl=1;
648 if (i==config.right) kr=1;
651 /* display the boss screen */
653 boss=IMG_Load("sprites/boss.png");
654 SDL_BlitSurface(boss,NULL,screen,NULL);
655 SDL_FreeSurface(boss);
657 /* and wait until the user press another key */
658 for (;;) if (SDL_PollEvent(&event)) { if (event.type==SDL_KEYDOWN) break; } else SDL_Delay(10);
659 SDL_BlitSurface(cir,NULL,screen,NULL);
666 i=event.key.keysym.sym;
667 if (i==config.up) ku=0;
668 if (i==config.down) kd=0;
669 if (i==config.left) kl=0;
670 if (i==config.right) kr=0;
675 /* save pressed keys to validate best time */
676 if (current.time<MAXRECORDKEYS) current.keys[current.time]=(ku<<3 | kd<<2 | kl<<1 | kr)+'A';
677 current.keys[current.time+1]='\0';
678 /* and to send to server if needed */
681 net.keys[net.time]=(ku<<3 | kd<<2 | kl<<1 | kr)+'A';
682 net.keys[net.time+1]='\0';
686 move_car(&car,(ku<<3 | kd<<2 | kl<<1 | kr),fun);
689 /* if we are in network mode */
693 while (SDLNet_UDP_Recv(udpsock,packet)) if (strcmp(packet->data,"positions")==0)
695 int servertime,clienttime,nb;
696 servertime=SDLNet_Read32(packet->data+strlen("positions")+1);
697 clienttime=SDLNet_Read32(packet->data+strlen("positions")+1+4);
698 nb=SDLNet_Read16(packet->data+strlen("positions")+1+4+4);
699 if (clienttime>lastack)
701 memcpy(net.keys,net.keys+clienttime-lastack,net.time+1);
702 net.time-=clienttime-lastack;
703 if (clienttime>servertime+5) delay+=DELAY;
704 if (clienttime<servertime-5) delay-=DELAY;
705 if (delay<0) delay=0;
706 for (i=0;i<MAX_CLIENTS;i++) newnetpos[i].w=0;
709 newnetpos[i].w=newnetpos[i].h=30;
710 newnetpos[i].x=SDLNet_Read16(packet->data+strlen("positions")+1+4+4+2+i*14);
711 newnetpos[i].y=SDLNet_Read16(packet->data+strlen("positions")+1+4+4+2+i*14+2);
712 newnetpos[i].angle=(float)SDLNet_Read16(packet->data+strlen("positions")+1+4+4+2+i*14+2+2)/1000;
713 newnetpos[i].color=SDLNet_Read16(packet->data+strlen("positions")+1+4+4+2+i*14+2+2+2);
714 newnetpos[i].lights_brake=SDLNet_Read16(packet->data+strlen("positions")+1+4+4+2+i*14+2+2+2+2);
715 newnetpos[i].lights_backwards=SDLNet_Read16(packet->data+strlen("positions")+1+4+4+2+i*14+2+2+2+2+2);
716 newnetpos[i].lights_warning=SDLNet_Read16(packet->data+strlen("positions")+1+4+4+2+i*14+2+2+2+2+2+2);
721 else if (strcmp(packet->data,"collision")==0)
725 lastack=SDLNet_Read32(packet->data+strlen("collision")+1);
726 car.x=(float)SDLNet_Read32(packet->data+strlen("collision")+1+4)/65536-100;
727 car.y=(float)SDLNet_Read32(packet->data+strlen("collision")+1+4+4)/65536-100;
728 car.speed=(float)SDLNet_Read32(packet->data+strlen("collision")+1+4+4+4)/65536-100;
729 car.angle=(float)SDLNet_Read32(packet->data+strlen("collision")+1+4+4+4+4)/65536-100;
731 else /* end of this network race */
733 zeRace_send_time(&best);
737 if (strlen(net.keys)!=0)
742 SDLNet_Write32(lastack,tmp);
744 SDLNet_Write32(car.x,tmp);
746 SDLNet_Write32(car.y,tmp);
748 strcpy(tmp,net.keys);
750 packet->len=(void *)tmp-(void *)packet->data+10;
751 if (net.time%network_speed==0) if (!SDLNet_UDP_Send(udpsock,-1,packet))
753 fprintf(stderr,"SDLNet_UDP_Send: %s\n",SDLNet_GetError());
759 /* clear the old network position */
760 if (udpsock) for (i=0;i<MAX_CLIENTS;i++) if (oldnetpos[i].w)
762 pos.x=oldnetpos[i].x-car.w/2;
763 pos.y=oldnetpos[i].y-car.h/2;
766 SDL_BlitSurface(cir,&pos,screen,&pos);
769 /* clear the old position */
770 pos.x=car.ox-car.w/2;
771 pos.y=car.oy-car.h/2;
774 SDL_BlitSurface(cir,&pos,screen,&pos);
776 /* clear the lights */
779 pos.x=car.ox-cos(car.angle)*l-sin(car.angle)*o;
780 pos.y=car.oy-sin(car.angle)*l+cos(car.angle)*o;
785 SDL_BlitSurface(cir,&pos,screen,&pos);
786 pos.x=car.ox-cos(car.angle)*l+sin(car.angle)*o;
787 pos.y=car.oy-sin(car.angle)*l-cos(car.angle)*o;
792 SDL_BlitSurface(cir,&pos,screen,&pos);
795 /* display the network car at the new position */
796 if (udpsock) for (i=0;i<MAX_CLIENTS;i++) if (newnetpos[i].w)
798 pos.x=newnetpos[i].x-car.w/2;
799 pos.y=newnetpos[i].y-car.h/2;
802 SDL_BlitSurface(cars[newnetpos[i].color][(unsigned char)(256*newnetpos[i].angle/2.0/M_PI)%256],NULL,screen,&pos);
804 /* if the car is braking, display red lights */
805 if (newnetpos[i].lights_brake)
807 lights(newnetpos[i].x+cos(newnetpos[i].angle)*car.w/3-sin(newnetpos[i].angle)*4,newnetpos[i].y+sin(newnetpos[i].angle)*car.h/3+cos(newnetpos[i].angle)*4,3,C_RED);
808 lights(newnetpos[i].x+cos(newnetpos[i].angle)*car.w/3+sin(newnetpos[i].angle)*4,newnetpos[i].y+sin(newnetpos[i].angle)*car.h/3-cos(newnetpos[i].angle)*4,3,C_RED);
811 /* if the car is going backwards, display white lights */
812 if (newnetpos[i].lights_backwards)
814 lights(newnetpos[i].x+cos(newnetpos[i].angle)*car.w/3-sin(newnetpos[i].angle)*4,newnetpos[i].y+sin(newnetpos[i].angle)*car.h/3+cos(newnetpos[i].angle)*4,3,C_WHITE);
815 lights(newnetpos[i].x+cos(newnetpos[i].angle)*car.w/3+sin(newnetpos[i].angle)*4,newnetpos[i].y+sin(newnetpos[i].angle)*car.h/3-cos(newnetpos[i].angle)*4,3,C_WHITE);
818 /* if the car is stopped, then warning */
819 if (newnetpos[i].lights_warning && alltime/75%2)
821 lights(newnetpos[i].x-cos(newnetpos[i].angle)*car.w/3-sin(newnetpos[i].angle)*5,newnetpos[i].y-sin(newnetpos[i].angle)*car.h/3+cos(newnetpos[i].angle)*5,2,C_ORANGE);
822 lights(newnetpos[i].x-cos(newnetpos[i].angle)*car.w/3+sin(newnetpos[i].angle)*5,newnetpos[i].y-sin(newnetpos[i].angle)*car.h/3-cos(newnetpos[i].angle)*5,2,C_ORANGE);
823 lights(newnetpos[i].x+cos(newnetpos[i].angle)*car.w/3-sin(newnetpos[i].angle)*5,newnetpos[i].y+sin(newnetpos[i].angle)*car.h/3+cos(newnetpos[i].angle)*5,2,C_ORANGE);
824 lights(newnetpos[i].x+cos(newnetpos[i].angle)*car.w/3+sin(newnetpos[i].angle)*5,newnetpos[i].y+sin(newnetpos[i].angle)*car.h/3-cos(newnetpos[i].angle)*5,2,C_ORANGE);
828 /* display the car at the new position */
833 SDL_BlitSurface(cars[config.color][(unsigned char)(256*car.angle/2.0/M_PI)%256],NULL,screen,&pos);
835 /* if the car is braking, display red lights */
836 if (car.lights_brake)
838 lights(car.x+cos(car.angle)*car.w/3-sin(car.angle)*4,car.y+sin(car.angle)*car.h/3+cos(car.angle)*4,3,C_RED);
839 lights(car.x+cos(car.angle)*car.w/3+sin(car.angle)*4,car.y+sin(car.angle)*car.h/3-cos(car.angle)*4,3,C_RED);
842 /* if the car is going backwards, display white lights */
843 if (car.lights_backwards)
845 lights(car.x+cos(car.angle)*car.w/3-sin(car.angle)*4,car.y+sin(car.angle)*car.h/3+cos(car.angle)*4,3,C_WHITE);
846 lights(car.x+cos(car.angle)*car.w/3+sin(car.angle)*4,car.y+sin(car.angle)*car.h/3-cos(car.angle)*4,3,C_WHITE);
849 /* if the car is stopped, then warning */
850 if (car.lights_warning && alltime/75%2)
852 lights(car.x-cos(car.angle)*car.w/3-sin(car.angle)*5,car.y-sin(car.angle)*car.h/3+cos(car.angle)*5,2,C_ORANGE);
853 lights(car.x-cos(car.angle)*car.w/3+sin(car.angle)*5,car.y-sin(car.angle)*car.h/3-cos(car.angle)*5,2,C_ORANGE);
854 lights(car.x+cos(car.angle)*car.w/3-sin(car.angle)*5,car.y+sin(car.angle)*car.h/3+cos(car.angle)*5,2,C_ORANGE);
855 lights(car.x+cos(car.angle)*car.w/3+sin(car.angle)*5,car.y+sin(car.angle)*car.h/3-cos(car.angle)*5,2,C_ORANGE);
858 /* display the lights */
861 lights(car.x+cos(car.angle)*car.w/3-sin(car.angle)*3,car.y+sin(car.angle)*car.h/3+cos(car.angle)*4,2,C_RED);
862 lights(car.x+cos(car.angle)*car.w/3+sin(car.angle)*3,car.y+sin(car.angle)*car.h/3-cos(car.angle)*4,2,C_RED);
863 lights(car.x-cos(car.angle)*car.w/3-sin(car.angle)*4,car.y-sin(car.angle)*car.h/3+cos(car.angle)*4,3,C_YELLOW);
864 lights(car.x-cos(car.angle)*car.w/3+sin(car.angle)*4,car.y-sin(car.angle)*car.h/3-cos(car.angle)*4,3,C_YELLOW);
865 pos.x=car.x-cos(car.angle)*l-sin(car.angle)*o;
866 pos.y=car.y-sin(car.angle)*l+cos(car.angle)*o;
871 SDL_BlitSurface(hilight,NULL,screen,&pos);
872 pos.x=car.x-cos(car.angle)*l+sin(car.angle)*o;
873 pos.y=car.y-sin(car.angle)*l-cos(car.angle)*o;
878 SDL_BlitSurface(hilight,NULL,screen,&pos);
884 for (i=0;i<MAX_CLIENTS;i++)
886 if (oldnetpos[i].w) SDL_UpdateRect(screen,oldnetpos[i].x-car.w/2,oldnetpos[i].y-car.h/2,car.w,car.h);
887 if (newnetpos[i].w) SDL_UpdateRect(screen,newnetpos[i].x-car.w/2,newnetpos[i].y-car.h/2,car.w,car.h);
889 memcpy(oldnetpos,newnetpos,MAX_CLIENTS*sizeof(struct _car));
891 SDL_UpdateRect(screen,car.ox-car.w/2,car.oy-car.h/2,car.w,car.h);
892 SDL_UpdateRect(screen,car.x-car.w/2,car.y-car.h/2,car.w,car.h);
894 /* update the lights by night */
897 pos.x=car.x-cos(car.angle)*l-sin(car.angle)*15;
898 pos.y=car.y-sin(car.angle)*l+cos(car.angle)*15;
903 if (pos.x<0) pos.x=0;
904 if (pos.y<0) pos.y=0;
905 if (pos.x+pos.w>screen->w) pos.w=screen->w-pos.x;
906 if (pos.y+pos.h>screen->h) pos.h=screen->h-pos.y;
907 SDL_UpdateRect(screen,pos.x,pos.y,pos.w,pos.h);
908 pos.x=car.x-cos(car.angle)*l+sin(car.angle)*15;
909 pos.y=car.y-sin(car.angle)*l-cos(car.angle)*15;
914 if (pos.x<0) pos.x=0;
915 if (pos.y<0) pos.y=0;
916 if (pos.x+pos.w>screen->w) pos.w=screen->w-pos.x;
917 if (pos.y+pos.h>screen->h) pos.h=screen->h-pos.y;
918 SDL_UpdateRect(screen,pos.x,pos.y,pos.w,pos.h);
921 memcpy(oldnetpos,newnetpos,MAX_CLIENTS*sizeof(struct _car));
923 /* play engine sound if no sound is currently playing */
924 if (lastsound_time+100<alltime)
927 lastsound_time=alltime;
928 if (config.sound) Mix_PlayMusic(engine,1);
931 /* if the car is fast or braking, it slides */
932 if ((kd && car.speed>0.5) || (car.speed>2.0 && !ku))
934 /* if the only sound is the engine, play the slide sound */
935 if (lastsound_time+100<alltime || lastsound<1)
938 lastsound_time=alltime;
939 if (config.sound) Mix_PlayMusic(slide,1)==-1;
942 /* display tires slide */
945 putpixel(cir,car.x+cos(car.angle)*car.w/3-sin(car.angle)*4,car.y+sin(car.angle)*car.h/3+cos(car.angle)*4,T_BLACK);
946 putpixel(cir,car.x+cos(car.angle)*car.w/3+sin(car.angle)*4,car.y+sin(car.angle)*car.h/3-cos(car.angle)*4,T_BLACK);
947 /* if we are braking the slide is larger */
950 putpixel(cir,car.x+cos(car.angle)*car.w/3-sin(car.angle)*3,car.y+sin(car.angle)*car.h/3+cos(car.angle)*3,T_BLACK);
951 putpixel(cir,car.x+cos(car.angle)*car.w/3+sin(car.angle)*3,car.y+sin(car.angle)*car.h/3-cos(car.angle)*3,T_BLACK);
957 if (car.crashflag && (lastsound_time+100<alltime || lastsound<2))
960 lastsound_time=alltime;
961 if (config.sound) Mix_PlayMusic(crash,1)==-1;
967 if (udpsock && net.time>MAX_LAG)
969 print(screen,WIDTH/2-strlen("Timeout !")*5,HEIGHT/2-10,"Timeout !");
971 zeRace_send_time(&best);
979 /* if we completed a lap */
981 printf("time = %d\"%d\n",current.time*DELAY/1000,current.time*DELAY%1000);
982 print(screen,0,0,"Last lap : ");
983 print_time(110,0,current.time);
984 SDL_UpdateRect(screen,0,0,200,19);
985 /* if it is the first turn of the best turn, save it */
986 if (best.time==-1 || current.time<best.time)
987 memcpy(&best,¤t,sizeof(struct _record));
988 /* reset turn variables */
992 current.angle=car.angle;
993 current.speed=car.speed;
996 /* if we completed an incomplete lap */
998 print(screen,0,0,"Last lap : CANCELED ");
999 SDL_UpdateRect(screen,0,0,200,19);
1000 /* reset turn variables */
1004 /* if we miss a checkpoint */
1006 print(screen,0,0,"Checkpoint missed ! ");
1007 SDL_UpdateRect(screen,0,0,200,19);
1009 /* if we validate a missed checkpoint */
1011 print(screen,0,0,"Checkpoint missed OK");
1012 SDL_UpdateRect(screen,0,0,200,19);
1019 /* let the system breath */
1025 /* display a random splash screen at startup */
1026 void zeRace_splash()
1028 SDL_Surface *splash;
1030 char temp[20]="splashs/0.jpg";
1032 SDL_FillRect(screen,NULL,C_BLACK);
1033 temp[8]=rand()%5+'1';
1034 splash=IMG_Load(temp);
1035 pos.x=screen->w/2-splash->w/2-1;
1037 pos.y=screen->h/2-splash->h/2-1;
1039 SDL_FillRect(screen,&pos,C_WHITE);
1040 pos.x=screen->w/2-splash->w/2;
1041 pos.y=screen->h/2-splash->h/2;
1042 SDL_BlitSurface(splash,NULL,screen,&pos);
1043 print(screen,screen->w/2-strlen("zeRace " VERSION)*5,screen->h/2-splash->h/2-20,"zeRace " VERSION);
1044 SDL_FreeSurface(splash);
1050 /* menu loop to select track */
1057 SDL_Surface *full,*preview;
1059 SDL_FillRect(screen,NULL,C_BLACK);
1060 print(screen,WIDTH/2-28*5,HEIGHT/6,"* Please choose your race *");
1061 print(screen,WIDTH/2-strlen(tracklist->title)*5,5*HEIGHT/6-20,tracklist->title);
1062 print(screen,WIDTH/2-(strlen(tracklist->author)+strlen("Author : "))*5,5*HEIGHT/6+0,"Author : ");
1063 print(screen,WIDTH/2-(strlen(tracklist->author)-strlen("Author : "))*5,5*HEIGHT/6+0,tracklist->author);
1064 print(screen,WIDTH/2-( strlen("Version : ")+strlen(tracklist->version))*5,5*HEIGHT/6+20,"Version : ");
1065 print(screen,WIDTH/2-(-strlen("Version : ")+strlen(tracklist->version))*5,5*HEIGHT/6+20,tracklist->version);
1066 print(screen,WIDTH/2-( strlen("Best time : ")+6+strlen(" by ")+strlen(tracklist->best_pseudo))*5,5*HEIGHT/6+40,"Best time : ");
1067 print_time (WIDTH/2-(-strlen("Best time : ")+6+strlen(" by ")+strlen(tracklist->best_pseudo))*5,5*HEIGHT/6+40,tracklist->best_time);
1068 print(screen,WIDTH/2-(-strlen("Best time : ")-6+strlen(" by ")+strlen(tracklist->best_pseudo))*5,5*HEIGHT/6+40," by ");
1069 print(screen,WIDTH/2-(-strlen("Best time : ")-6-strlen(" by ")+strlen(tracklist->best_pseudo))*5,5*HEIGHT/6+40,tracklist->best_pseudo);
1070 full=IMG_Load(tracklist->full);
1071 preview=(SDL_Surface *)zoomSurface(full,0.5,0.5,1);
1072 SDL_FreeSurface(full);
1073 pos.x=WIDTH/2-preview->w/2-1;
1075 pos.y=screen->h/2-preview->h/2-1;
1077 SDL_FillRect(screen,&pos,C_WHITE);
1078 pos.x=WIDTH/2-preview->w/2;
1079 pos.y=screen->h/2-preview->h/2;
1080 SDL_BlitSurface(preview,NULL,screen,&pos);
1081 SDL_FreeSurface(preview);
1088 while (SDL_PollEvent(&event))
1096 switch (event.key.keysym.sym)
1106 tracklist=tracklist->next;
1110 tracklist=tracklist->prev;
1125 void zeRace_top10(char *buf)
1129 SDL_FillRect(screen,NULL,C_BLACK);
1130 nb=SDLNet_Read16(buf);
1132 print(screen,WIDTH/2-16*5,HEIGHT/14,"* Race results *");
1135 print(screen,150,(i+3)*HEIGHT/14,buf);
1137 tmp=SDLNet_Read16(buf);
1140 pos.y=(i+3)*HEIGHT/14-8;
1141 SDL_BlitSurface(cars[tmp][0],NULL,screen,&pos);
1148 /* connect to a server */
1149 void zeRace_connect(char *host,int port)
1153 udpsock=SDLNet_UDP_Open(0);
1156 fprintf(stderr,"SDLNet_UDP_Open: %s\n",SDLNet_GetError());
1159 SDLNet_ResolveHost(&packet->address,host,port);
1161 strcpy(tmp,"connect");
1163 strcpy(tmp,config.pseudo);
1165 SDLNet_Write16(config.color,tmp);
1167 packet->len=(void *)tmp-(void *)packet->data;
1168 SDLNet_UDP_Send(udpsock,-1,packet);
1170 while (SDLNet_UDP_Recv(udpsock,packet) || lag<MAX_LAG)
1173 if (strcmp(tmp,"track")==0)
1175 struct _tracklist *loopcheck=tracklist;
1180 printf("server asked for track : %s\n",tmp);
1181 do if (strcmp(tracklist->name,tmp)==0) break; else tracklist=tracklist->next; while (tracklist!=loopcheck);
1182 if (strcmp(tracklist->name,tmp)!=0)
1184 fprintf(stderr,"unknown track : %s\n",tmp);
1188 time=SDLNet_Read32(tmp);
1190 network_speed=SDLNet_Read32(tmp);
1191 zeRace_launch(time,go);
1192 if (strcmp(packet->data,"finish")==0) zeRace_top10(packet->data+strlen(packet->data)+1);
1198 SDLNet_UDP_Close(udpsock);
1204 void zeRace_network()
1208 char server[MAXLINELENGTH]="localhost";
1210 #define NETWORK_OPTIONS 4
1214 SDL_FillRect(screen,NULL,C_BLACK);
1215 print(screen,380,HEIGHT/(NETWORK_OPTIONS+4)*(3+active),">");
1216 print(screen,WIDTH/2-18*5,HEIGHT/(NETWORK_OPTIONS+4),"* Network screen *");
1217 print(screen,400,HEIGHT/(NETWORK_OPTIONS+4)*3,"Server : ");
1218 print(screen,400+10*strlen("Server : "),HEIGHT/(NETWORK_OPTIONS+4)*3,server);
1219 print(screen,400,HEIGHT/(NETWORK_OPTIONS+4)*4,"Port : ");
1220 print(screen,400+10*strlen("Port : "),HEIGHT/(NETWORK_OPTIONS+4)*4,port);
1221 print(screen,400,HEIGHT/(NETWORK_OPTIONS+4)*(NETWORK_OPTIONS+1),"Connect");
1222 print(screen,400,HEIGHT/(NETWORK_OPTIONS+4)*(NETWORK_OPTIONS+2),"Back to main menu");
1229 while (SDL_PollEvent(&event))
1237 switch (event.key.keysym.sym)
1247 case 0: readstring(screen,400+10*strlen("Server : "),HEIGHT/(NETWORK_OPTIONS+4)*3,server,MAXLINELENGTH); break;
1248 case 1: readstring(screen,400+10*strlen("Port : "),HEIGHT/(NETWORK_OPTIONS+4)*4,port,5); break;;
1250 zeRace_connect(server,atoi(port));
1258 active--; if (active<0) active=NETWORK_OPTIONS-1;
1262 active++; if (active>NETWORK_OPTIONS-1) active=0;
1277 void zeRace_internet()
1282 "GET /zerace/servers.php HTTP/1.0\n"
1283 "Host: royale.zerezo.com\n"
1284 "User-Agent: zeRace " VERSION "\n"
1286 char response[10240],*tmp;
1290 char name[MAXLINELENGTH];
1296 #define INTERNET_OPTIONS 11
1298 printf("dowloading list of servers... ");
1301 if (SDLNet_ResolveHost(&ip,"royale.zerezo.com",80)==-1)
1303 fprintf(stderr,"SDLNet_ResolveHost: %s\n",SDLNet_GetError());
1307 tcpsock=SDLNet_TCP_Open(&ip);
1310 fprintf(stderr,"SDLNet_TCP_Open: %s\n",SDLNet_GetError());
1314 len=strlen(request);
1315 result=SDLNet_TCP_Send(tcpsock,request,len);
1317 fprintf(stderr,"SDLNet_TCP_Send: %s\n",SDLNet_GetError());
1320 len=SDLNet_TCP_Recv(tcpsock,response,10240);
1322 for (tmp=response;tmp<response+10240;tmp++) if (*tmp=='\n') *tmp='\0';
1324 while (*tmp!='\0' || *(tmp+1)!='\r') tmp++;
1328 strcpy(servers[i].name,tmp);
1330 strcpy(servers[i].ip,tmp);
1332 strcpy(servers[i].port,tmp);
1338 SDLNet_TCP_Close(tcpsock);
1343 SDL_FillRect(screen,NULL,C_BLACK);
1344 print(screen,380,HEIGHT/(INTERNET_OPTIONS+4)*(3+active),">");
1345 print(screen,WIDTH/2-19*5,HEIGHT/(INTERNET_OPTIONS+4),"* Internet screen *");
1347 print(screen,400,HEIGHT/(INTERNET_OPTIONS+4)*(i+3),servers[i].name);
1348 print(screen,400,HEIGHT/(INTERNET_OPTIONS+4)*(INTERNET_OPTIONS+2),"Back to main menu");
1355 while (SDL_PollEvent(&event))
1363 switch (event.key.keysym.sym)
1371 if (active==INTERNET_OPTIONS-1)
1374 zeRace_connect(servers[active].ip,atoi(servers[active].port));
1378 active--; if (active<0) active=INTERNET_OPTIONS-1;
1382 active++; if (active>INTERNET_OPTIONS-1) active=0;
1396 /* configuration screen */
1397 void zeRace_config()
1401 #define CONFIG_OPTIONS 13
1406 SDL_FillRect(screen,NULL,C_BLACK);
1407 print(screen,20,HEIGHT/(CONFIG_OPTIONS+4)*(3+active),">");
1408 print(screen,WIDTH/2-24*5,HEIGHT/(CONFIG_OPTIONS+4),"* Configuration screen *");
1409 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*3,"Pseudo : ");
1410 print(screen,40+10*strlen("Pseudo : "),HEIGHT/(CONFIG_OPTIONS+4)*3,config.pseudo);
1411 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*4,"Url : ");
1412 print(screen,40+10*strlen("Url : "),HEIGHT/(CONFIG_OPTIONS+4)*4,config.url);
1413 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*5,"Fullscreen : ");
1414 print(screen,40+10*strlen("Fullscreen : "),HEIGHT/(CONFIG_OPTIONS+4)*5,config.fullscreen?"Yes":"No");
1415 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*6,"Sound : ");
1416 print(screen,40+10*strlen("Sound : "),HEIGHT/(CONFIG_OPTIONS+4)*6,config.sound?"Yes":"No");
1417 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*7,"Tire : ");
1418 print(screen,40+10*strlen("Tire : "),HEIGHT/(CONFIG_OPTIONS+4)*7,config.tire?"Yes":"No");
1419 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*8,"Accelerate key : ");
1420 print(screen,40+10*strlen("Accelerate key : "),HEIGHT/(CONFIG_OPTIONS+4)*8,config.up?SDL_GetKeyName(config.up):"<press key>");
1421 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*9,"Brake key : ");
1422 print(screen,40+10*strlen("Brake key : "),HEIGHT/(CONFIG_OPTIONS+4)*9,config.down?SDL_GetKeyName(config.down):"<press key>");
1423 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*10,"Turn left key : ");
1424 print(screen,40+10*strlen("Turn left key : "),HEIGHT/(CONFIG_OPTIONS+4)*10,config.left?SDL_GetKeyName(config.left):"<press key>");
1425 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*11,"Turn right key : ");
1426 print(screen,40+10*strlen("Turn right key : "),HEIGHT/(CONFIG_OPTIONS+4)*11,config.right?SDL_GetKeyName(config.right):"<press key>");
1427 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*12,"Color : ");
1429 pos.y=HEIGHT/(CONFIG_OPTIONS+4)*12-7;
1430 SDL_BlitSurface(cars[config.color][0],NULL,screen,&pos);
1431 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*13,"Boss key : ");
1432 print(screen,40+10*strlen("Boss key : "),HEIGHT/(CONFIG_OPTIONS+4)*13,config.boss?SDL_GetKeyName(config.boss):"<press key>");
1433 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*14,"By night : ");
1434 print(screen,40+10*strlen("By night : "),HEIGHT/(CONFIG_OPTIONS+4)*14,config.bynight?"Yes":"No");
1435 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*(CONFIG_OPTIONS+2),"Back to main menu");
1443 while (SDL_PollEvent(&event)) switch (event.type)
1446 return event.key.keysym.sym;
1455 while (SDL_PollEvent(&event))
1463 switch (event.key.keysym.sym)
1473 case 0: readstring(screen,40+10*strlen("Pseudo : "),HEIGHT/(CONFIG_OPTIONS+4)*3,config.pseudo,MAXLINELENGTH); break;
1474 case 1: readstring(screen,40+10*strlen("Url : "),HEIGHT/(CONFIG_OPTIONS+4)*4,config.url,MAXLINELENGTH); break;;
1475 case 2: config.fullscreen=!config.fullscreen; break;
1476 case 3: config.sound=!config.sound; break;
1477 case 4: config.tire=!config.tire; break;
1478 case 5: config.up=0; update(); config.up=read_key(); break;
1479 case 6: config.down=0; update(); config.down=read_key(); break;
1480 case 7: config.left=0; update(); config.left=read_key(); break;
1481 case 8: config.right=0; update(); config.right=read_key(); break;
1483 if (event.key.keysym.sym==SDLK_LEFT) config.color--; else config.color++;
1484 if (config.color<0) config.color=NB_CARS-1;
1485 if (config.color>NB_CARS-1) config.color=0;
1487 case 10: config.boss=0; update(); config.boss=read_key(); break;
1488 case 11: config.bynight=!config.bynight; break;
1495 active--; if (active<0) active=CONFIG_OPTIONS-1;
1499 active++; if (active>CONFIG_OPTIONS-1) active=0;
1519 #define MENU_OPTIONS 5
1524 SDL_FillRect(screen,NULL,C_BLACK);
1525 pos.x=WIDTH/2-logo->w/2;
1526 pos.y=HEIGHT/6-logo->h/2;
1527 SDL_BlitSurface(logo,NULL,screen,&pos);
1528 print(screen,650,HEIGHT/6+logo->h/3,"version " VERSION);
1529 print(screen,420,HEIGHT/(MENU_OPTIONS+4)*(3+active),">");
1530 print(screen,440,HEIGHT/(MENU_OPTIONS+4)*3,"Local game");
1531 print(screen,440,HEIGHT/(MENU_OPTIONS+4)*4,"Network game");
1532 print(screen,440,HEIGHT/(MENU_OPTIONS+4)*5,"Internet game");
1533 print(screen,440,HEIGHT/(MENU_OPTIONS+4)*6,"Configuration");
1534 print(screen,440,HEIGHT/(MENU_OPTIONS+4)*7,"Exit game");
1538 logo=IMG_Load("sprites/logo.jpg");
1542 while (SDL_PollEvent(&event))
1550 switch (event.key.keysym.sym)
1560 case 0: zeRace_local(); break;
1561 case 1: zeRace_network(); break;
1562 case 2: zeRace_internet(); break;
1563 case 3: zeRace_config(); break;
1569 active--; if (active<0) active=MENU_OPTIONS-1;
1573 active++; if (active>MENU_OPTIONS-1) active=0;
1588 int main(int argc,char *argv[])