2 * zeRace 0.5, 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)
54 struct _tracklist *tracklist;
59 char pseudo[MAXLINELENGTH];
60 char url[MAXLINELENGTH];
71 } config = {"anonymous","",0,0,1,SDLK_UP,SDLK_DOWN,SDLK_LEFT,SDLK_RIGHT,6,SDLK_b,0};
73 /* full script for a lap */
76 float x,y,angle,speed;
77 char keys[MAXRECORDKEYS];
81 /* display and all directions for the car */
83 SDL_Surface *cars[NB_CARS][256];
86 UDPsocket udpsock=NULL;
91 /* read the user configuration file */
92 void zeRace_read_config()
95 if ((fic=fopen("zeRace.cfg","rb"))==NULL)
97 fprintf(stderr,"can't open config file \"zeRace.cfg\"\n");
100 fread(&config,sizeof(struct _config),1,fic);
105 /* save the user configuration file */
106 void zeRace_save_config()
109 if ((fic=fopen("zeRace.cfg","wb"))==NULL)
111 fprintf(stderr,"can't create config file \"zeRace.cfg\"\n");
114 fwrite(&config,sizeof(struct _config),1,fic);
119 /* exit the game and clean */
123 if (config.sound) Mix_CloseAudio();
126 zeRace_save_config();
131 /* check for a newer version online to warn the user */
132 void zeRace_check_version()
137 "GET /zerace/version.php HTTP/1.0\n"
138 "Host: royale.zerezo.com\n"
139 "User-Agent: zeRace " VERSION "\n"
141 char response[1024],*tmp,*version;
144 printf("checking version... ");
147 if (SDLNet_ResolveHost(&ip,"royale.zerezo.com",80)==-1)
149 fprintf(stderr,"SDLNet_ResolveHost: %s\n",SDLNet_GetError());
153 tcpsock=SDLNet_TCP_Open(&ip);
156 fprintf(stderr,"SDLNet_TCP_Open: %s\n",SDLNet_GetError());
161 result=SDLNet_TCP_Send(tcpsock,request,len);
163 fprintf(stderr,"SDLNet_TCP_Send: %s\n",SDLNet_GetError());
167 len=SDLNet_TCP_Recv(tcpsock,response,1024);
169 fprintf(stderr,"SDLNet_TCP_Recv: %s\n",SDLNet_GetError());
173 while (*tmp++!='\0') if (strncmp(tmp,"version=",8)==0)
176 while (*tmp!='\n') tmp++;
178 if (strcmp(version,VERSION)>0) printf("new version available !\nhttp://royale.zerezo.com/zerace/\n");
182 SDLNet_TCP_Close(tcpsock);
186 /* get remote list of tracks */
187 void zeRace_update_tracks()
192 "GET /zerace/tracks.php HTTP/1.0\n"
193 "Host: royale.zerezo.com\n"
194 "User-Agent: zeRace " VERSION "\n"
196 char response[10240],*tmp;
200 printf("checking version and updating tracks... ");
203 if (SDLNet_ResolveHost(&ip,"royale.zerezo.com",80)==-1)
205 fprintf(stderr,"SDLNet_ResolveHost: %s\n",SDLNet_GetError());
209 tcpsock=SDLNet_TCP_Open(&ip);
212 fprintf(stderr,"SDLNet_TCP_Open: %s\n",SDLNet_GetError());
217 result=SDLNet_TCP_Send(tcpsock,request,len);
219 fprintf(stderr,"SDLNet_TCP_Send: %s\n",SDLNet_GetError());
222 if ((fic=fopen("tracks/list.txt","wt"))==NULL)
224 fprintf(stderr,"can't create track list\n");
227 len=SDLNet_TCP_Recv(tcpsock,response,10240);
229 while (*tmp!='\n' || *(tmp+1)!='\r') tmp++;
231 fwrite(tmp,1,len+response-tmp,fic);
236 SDLNet_TCP_Close(tcpsock);
240 /* download the file if it is missing */
241 void zeRace_download_file(char *file)
246 char response[10240],*tmp;
251 if (stat(file,&buf)<0)
253 printf("downloading file \"%s\" : ",file);
256 if (SDLNet_ResolveHost(&ip,"royale.zerezo.com",80)==-1)
258 fprintf(stderr,"SDLNet_ResolveHost: %s\n",SDLNet_GetError());
262 tcpsock=SDLNet_TCP_Open(&ip);
265 fprintf(stderr,"SDLNet_TCP_Open: %s\n",SDLNet_GetError());
270 "GET /zerace/%s HTTP/1.0\n"
271 "Host: royale.zerezo.com\n"
272 "User-Agent: zeRace " VERSION "\n"
275 result=SDLNet_TCP_Send(tcpsock,request,len);
277 fprintf(stderr,"SDLNet_TCP_Send: %s\n",SDLNet_GetError());
280 if ((fic=fopen(file,"wb"))==NULL)
282 fprintf(stderr,"can't create \"%s\" file\n",file);
285 len=SDLNet_TCP_Recv(tcpsock,response,10240);
287 while (*tmp!='\n' || *(tmp+1)!='\r') tmp++;
289 fwrite(tmp,1,len+response-tmp,fic);
290 while ((len=SDLNet_TCP_Recv(tcpsock,response,10240))) fwrite(response,1,len,fic);
295 SDLNet_TCP_Close(tcpsock);
300 /* load the car sprite and rotate it for every angles */
301 void zeRace_generate_cars()
305 char temp[20]="sprites/carX.png";
306 for (i=0;i<NB_CARS;i++)
309 /* load the car sprite */
311 /* and rotate it for all available angles */
316 if ((cars[i][j]=SDL_CreateRGBSurface(SDL_SWSURFACE,30,30,32,RMASK,GMASK,BMASK,AMASK))==NULL)
318 fprintf(stderr,"CreateRGBSurface failed: %s\n",SDL_GetError());
321 tcos=cos(2*M_PI*j/256);
322 tsin=sin(2*M_PI*j/256);
323 for (x=0;x<cars[i][j]->w;x++) for (y=0;y<cars[i][j]->h;y++)
326 x2=(x-cars[i][j]->w/2.0)*tcos+(y-cars[i][j]->h/2.0)*tsin+car->w/2.0;
327 y2=(x-cars[i][j]->w/2.0)*tsin-(y-cars[i][j]->h/2.0)*tcos+car->h/2.0;
328 if (x2>0 && x2<car->w && y2>0 && y2<car->h)
329 putpixel(cars[i][j],x,y,getpixel(car,x2,y2));
332 SDL_FreeSurface(car);
337 /* initialize the game */
341 struct _tracklist *loopcheck;
343 /* do a clean exit in case of emergency */
344 signal(SIGINT,zeRace_exit);
345 signal(SIGTERM,zeRace_exit);
347 /* read the user configuration file */
348 zeRace_read_config();
350 /* check for a newer available version */
351 zeRace_check_version();
353 /* update the list of tracks */
354 zeRace_update_tracks();
356 /* get the list of local tracks */
357 if (!zeRace_get_tracks(&tracklist)) zeRace_exit();
359 /* download missing files */
363 zeRace_download_file(tracklist->full);
364 zeRace_download_file(tracklist->function);
365 tracklist=tracklist->next;
366 } while (tracklist!=loopcheck);
370 if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)<0)
372 fprintf(stderr,"could not initialize SDL : %s\n",SDL_GetError());
377 if (SDLNet_Init()==-1)
379 fprintf(stderr,"could not initialize SDLNet : %s\n",SDLNet_GetError());
383 packet=SDLNet_AllocPacket(1024);
386 fprintf(stderr,"SDLNet_AllocPacket: %s\n",SDLNet_GetError());
390 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,SDL_DEFAULT_REPEAT_INTERVAL);
392 flags=SDL_HWSURFACE|SDL_ANYFORMAT;
393 if (config.fullscreen) flags|=SDL_FULLSCREEN;
395 if ((screen=SDL_SetVideoMode(WIDTH,HEIGHT,32,flags))==NULL)
397 fprintf(stderr,"could not create a surface : %s\n",SDL_GetError());
401 SDL_WM_SetIcon(SDL_LoadBMP("icon.bmp"),NULL);
402 SDL_WM_SetCaption("zeRace " VERSION,"zeRace " VERSION);
403 SDL_ShowCursor(SDL_DISABLE);
405 if (config.sound) if (Mix_OpenAudio(44100,MIX_DEFAULT_FORMAT,2,512)<0)
407 fprintf(stderr,"Mix_OpenAudio error\n");
411 /* pre-calculate car sprites */
412 zeRace_generate_cars();
416 /* send the best time for this race to the web server */
417 void zeRace_send_time(struct _record record)
423 "POST /zerace/time.php HTTP/1.0\n"
424 "Host: royale.zerezo.com\n"
425 "User-Agent: zeRace " VERSION "\n"
426 "Content-Type: application/x-www-form-urlencoded\n"
427 "Content-Length: 99999\n"
431 char *msg3="&track=";
432 char *msg4="&btime=";
435 char *msg7="&speed=";
436 char *msg8="&angle=";
437 char *msg9="&bkeys=";
440 /* if the best time is small enought to save all keys, send it */
441 if (record.time>=MAXRECORDKEYS) return;
443 printf("sending time... ");
446 if (SDLNet_ResolveHost(&ip,"royale.zerezo.com",80)==-1)
448 fprintf(stderr,"SDLNet_ResolveHost: %s\n",SDLNet_GetError());
452 tcpsock=SDLNet_TCP_Open(&ip);
455 fprintf(stderr,"SDLNet_TCP_Open: %s\n",SDLNet_GetError());
459 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);
460 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);
463 result=SDLNet_TCP_Send(tcpsock,temp,len);
465 fprintf(stderr,"SDLNet_TCP_Send: %s\n", SDLNet_GetError());
469 SDLNet_TCP_Close(tcpsock);
473 /* print a time for a lap */
474 void print_time(int x,int y,int time)
486 temp[4]=r%100/10+'0';
489 print(screen,x,y,temp);
494 void lights(int x,int y,int r,Uint32 pixel)
496 putpixel(screen,x,y,pixel);
499 putpixel(screen,x-1,y,pixel);
500 putpixel(screen,x+1,y,pixel);
501 putpixel(screen,x,y-1,pixel);
502 putpixel(screen,x,y+1,pixel);
506 putpixel(screen,x-2,y,pixel);
507 putpixel(screen,x+2,y,pixel);
508 putpixel(screen,x,y-2,pixel);
509 putpixel(screen,x,y+2,pixel);
510 putpixel(screen,x-1,y-1,pixel);
511 putpixel(screen,x-1,y+1,pixel);
512 putpixel(screen,x+1,y-1,pixel);
513 putpixel(screen,x+1,y+1,pixel);
518 /* launch a new race */
519 void zeRace_launch(int alltime,int go)
521 SDL_Surface *cir,*fun,*hilight;
524 int ku=0,kd=0,kl=0,kr=0,i;
526 struct _record current,best;
527 Mix_Music *light,*engine,*crash,*slide;
528 int lastsound_time=-999,lastsound=0;
532 struct _car oldnetpos[MAX_CLIENTS],newnetpos[MAX_CLIENTS];
538 SDL_FreeSurface(cir);
539 SDL_FreeSurface(fun);
542 Mix_FreeMusic(light);
543 Mix_FreeMusic(engine);
544 Mix_FreeMusic(crash);
545 Mix_FreeMusic(slide);
550 cir=IMG_Load(tracklist->full);
551 /* dark the track if it is night */
554 for (pos.x=0;pos.x<screen->w;pos.x++)
555 for (pos.y=0;pos.y<screen->h;pos.y++)
559 c=getpixel(cir,pos.x,pos.y);
560 SDL_GetRGB(c,cir->format,&r,&g,&b);
564 putpixel(cir,pos.x,pos.y,SDL_MapRGB(cir->format,r,g,b));
567 fun=IMG_Load(tracklist->function);
568 hilight=IMG_Load("sprites/light.png");
570 current.speed=car.speed=0;
571 current.angle=car.angle=tracklist->a*2*M_PI/360;
572 current.x=car.ox=car.x=tracklist->x;
573 current.y=car.oy=car.y=tracklist->y;
579 best.time=MAXRECORDKEYS;
581 memset(oldnetpos,0,MAX_CLIENTS*sizeof(struct _car));
582 memset(newnetpos,0,MAX_CLIENTS*sizeof(struct _car));
584 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")))
586 fprintf(stderr,"Mix_LoadMUS error\n");
590 /* startup countdown */
593 char startup[15]="sprites/?.png";
598 SDL_BlitSurface(cir,NULL,screen,NULL);
599 SDL_BlitSurface(cars[config.color][(unsigned char)(256*car.angle/2.0/M_PI)%256],NULL,screen,&pos);
602 temp=IMG_Load(startup);
603 pos.x=screen->w/2-temp->w/2;
604 pos.y=screen->h/2-temp->h/2;
605 SDL_BlitSurface(temp,NULL,screen,&pos);
606 SDL_FreeSurface(temp);
608 if (config.sound) if (i!=4) Mix_PlayMusic(light,1);
611 if (i!=-1) SDL_Delay(1000);
617 /* look for user interaction */
618 while (SDL_PollEvent(&event))
626 switch (event.key.keysym.sym)
631 print(screen,WIDTH/2-strlen("Disconnecting !")*5,HEIGHT/2-10,"Disconnecting !");
632 strcpy(packet->data,"disconnect");
633 packet->len=strlen(packet->data)+1;
634 SDLNet_UDP_Send(udpsock,-1,packet);
637 zeRace_send_time(best);
641 i=event.key.keysym.sym;
642 if (i==config.up) ku=1;
643 if (i==config.down) kd=1;
644 if (i==config.left) kl=1;
645 if (i==config.right) kr=1;
648 /* display the boss screen */
650 boss=IMG_Load("sprites/boss.png");
651 SDL_BlitSurface(boss,NULL,screen,NULL);
652 SDL_FreeSurface(boss);
654 /* and wait until the user press another key */
655 for (;;) if (SDL_PollEvent(&event)) { if (event.type==SDL_KEYDOWN) break; } else SDL_Delay(10);
656 SDL_BlitSurface(cir,NULL,screen,NULL);
663 i=event.key.keysym.sym;
664 if (i==config.up) ku=0;
665 if (i==config.down) kd=0;
666 if (i==config.left) kl=0;
667 if (i==config.right) kr=0;
672 /* save pressed keys to validate best time */
673 if (current.time<MAXRECORDKEYS) current.keys[current.time]=(ku<<3 | kd<<2 | kl<<1 | kr)+'A';
674 current.keys[current.time+1]='\0';
675 /* and to send to server if needed */
678 net.keys[net.time]=(ku<<3 | kd<<2 | kl<<1 | kr)+'A';
679 net.keys[net.time+1]='\0';
683 move_car(&car,(ku<<3 | kd<<2 | kl<<1 | kr),fun);
686 /* if we are in network mode */
690 while (SDLNet_UDP_Recv(udpsock,packet)) if (strcmp(packet->data,"positions")==0)
692 int servertime,clienttime,nb;
693 servertime=SDLNet_Read32(packet->data+strlen("positions")+1);
694 clienttime=SDLNet_Read32(packet->data+strlen("positions")+1+4);
695 nb=SDLNet_Read16(packet->data+strlen("positions")+1+4+4);
696 if (clienttime>lastack)
698 memcpy(net.keys,net.keys+clienttime-lastack,net.time+1);
699 net.time-=clienttime-lastack;
700 if (clienttime>servertime+5) delay+=DELAY;
701 if (clienttime<servertime-5) delay-=DELAY;
702 if (delay<0) delay=0;
703 for (i=0;i<MAX_CLIENTS;i++) newnetpos[i].w=0;
706 newnetpos[i].w=newnetpos[i].h=30;
707 newnetpos[i].x=SDLNet_Read16(packet->data+strlen("positions")+1+4+4+2+i*14);
708 newnetpos[i].y=SDLNet_Read16(packet->data+strlen("positions")+1+4+4+2+i*14+2);
709 newnetpos[i].angle=(float)SDLNet_Read16(packet->data+strlen("positions")+1+4+4+2+i*14+2+2)/1000;
710 newnetpos[i].color=SDLNet_Read16(packet->data+strlen("positions")+1+4+4+2+i*14+2+2+2);
711 newnetpos[i].lights_brake=SDLNet_Read16(packet->data+strlen("positions")+1+4+4+2+i*14+2+2+2+2);
712 newnetpos[i].lights_backwards=SDLNet_Read16(packet->data+strlen("positions")+1+4+4+2+i*14+2+2+2+2+2);
713 newnetpos[i].lights_warning=SDLNet_Read16(packet->data+strlen("positions")+1+4+4+2+i*14+2+2+2+2+2+2);
718 else if (strcmp(packet->data,"collision")==0)
722 lastack=SDLNet_Read32(packet->data+strlen("collision")+1);
723 car.x=(float)SDLNet_Read32(packet->data+strlen("collision")+1+4)/65536-100;
724 car.y=(float)SDLNet_Read32(packet->data+strlen("collision")+1+4+4)/65536-100;
725 car.speed=(float)SDLNet_Read32(packet->data+strlen("collision")+1+4+4+4)/65536-100;
726 car.angle=(float)SDLNet_Read32(packet->data+strlen("collision")+1+4+4+4+4)/65536-100;
728 else /* end of this network race */
730 zeRace_send_time(best);
734 if (strlen(net.keys)!=0)
739 SDLNet_Write32(lastack,tmp);
741 SDLNet_Write32(car.x,tmp);
743 SDLNet_Write32(car.y,tmp);
745 strcpy(tmp,net.keys);
747 packet->len=(void *)tmp-(void *)packet->data+10;
748 if (net.time%network_speed==0) if (!SDLNet_UDP_Send(udpsock,-1,packet))
750 fprintf(stderr,"SDLNet_UDP_Send: %s\n",SDLNet_GetError());
756 /* clear the old network position */
757 if (udpsock) for (i=0;i<MAX_CLIENTS;i++) if (oldnetpos[i].w)
759 pos.x=oldnetpos[i].x-car.w/2;
760 pos.y=oldnetpos[i].y-car.h/2;
763 SDL_BlitSurface(cir,&pos,screen,&pos);
766 /* clear the old position */
767 pos.x=car.ox-car.w/2;
768 pos.y=car.oy-car.h/2;
771 SDL_BlitSurface(cir,&pos,screen,&pos);
773 /* clear the lights */
776 pos.x=car.ox-cos(car.angle)*l-sin(car.angle)*o;
777 pos.y=car.oy-sin(car.angle)*l+cos(car.angle)*o;
782 SDL_BlitSurface(cir,&pos,screen,&pos);
783 pos.x=car.ox-cos(car.angle)*l+sin(car.angle)*o;
784 pos.y=car.oy-sin(car.angle)*l-cos(car.angle)*o;
789 SDL_BlitSurface(cir,&pos,screen,&pos);
792 /* display the network car at the new position */
793 if (udpsock) for (i=0;i<MAX_CLIENTS;i++) if (newnetpos[i].w)
795 pos.x=newnetpos[i].x-car.w/2;
796 pos.y=newnetpos[i].y-car.h/2;
799 SDL_BlitSurface(cars[newnetpos[i].color][(unsigned char)(256*newnetpos[i].angle/2.0/M_PI)%256],NULL,screen,&pos);
801 /* if the car is braking, display red lights */
802 if (newnetpos[i].lights_brake)
804 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);
805 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 /* if the car is going backwards, display white lights */
809 if (newnetpos[i].lights_backwards)
811 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);
812 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 /* if the car is stopped, then warning */
816 if (newnetpos[i].lights_warning && alltime/75%2)
818 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);
819 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);
820 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);
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);
825 /* display the car at the new position */
830 SDL_BlitSurface(cars[config.color][(unsigned char)(256*car.angle/2.0/M_PI)%256],NULL,screen,&pos);
832 /* if the car is braking, display red lights */
833 if (car.lights_brake)
835 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);
836 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 /* if the car is going backwards, display white lights */
840 if (car.lights_backwards)
842 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);
843 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 /* if the car is stopped, then warning */
847 if (car.lights_warning && alltime/75%2)
849 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);
850 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);
851 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);
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);
855 /* display the lights */
858 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);
859 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);
860 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);
861 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);
862 pos.x=car.x-cos(car.angle)*l-sin(car.angle)*o;
863 pos.y=car.y-sin(car.angle)*l+cos(car.angle)*o;
868 SDL_BlitSurface(hilight,NULL,screen,&pos);
869 pos.x=car.x-cos(car.angle)*l+sin(car.angle)*o;
870 pos.y=car.y-sin(car.angle)*l-cos(car.angle)*o;
875 SDL_BlitSurface(hilight,NULL,screen,&pos);
881 for (i=0;i<MAX_CLIENTS;i++)
883 if (oldnetpos[i].w) SDL_UpdateRect(screen,oldnetpos[i].x-car.w/2,oldnetpos[i].y-car.h/2,car.w,car.h);
884 if (newnetpos[i].w) SDL_UpdateRect(screen,newnetpos[i].x-car.w/2,newnetpos[i].y-car.h/2,car.w,car.h);
886 memcpy(oldnetpos,newnetpos,MAX_CLIENTS*sizeof(struct _car));
888 SDL_UpdateRect(screen,car.ox-car.w/2,car.oy-car.h/2,car.w,car.h);
889 SDL_UpdateRect(screen,car.x-car.w/2,car.y-car.h/2,car.w,car.h);
891 /* update the lights by night */
894 pos.x=car.x-cos(car.angle)*l-sin(car.angle)*15;
895 pos.y=car.y-sin(car.angle)*l+cos(car.angle)*15;
900 if (pos.x<0) pos.x=0;
901 if (pos.y<0) pos.y=0;
902 if (pos.x+pos.w>screen->w) pos.w=screen->w-pos.x;
903 if (pos.y+pos.h>screen->h) pos.h=screen->h-pos.y;
904 SDL_UpdateRect(screen,pos.x,pos.y,pos.w,pos.h);
905 pos.x=car.x-cos(car.angle)*l+sin(car.angle)*15;
906 pos.y=car.y-sin(car.angle)*l-cos(car.angle)*15;
911 if (pos.x<0) pos.x=0;
912 if (pos.y<0) pos.y=0;
913 if (pos.x+pos.w>screen->w) pos.w=screen->w-pos.x;
914 if (pos.y+pos.h>screen->h) pos.h=screen->h-pos.y;
915 SDL_UpdateRect(screen,pos.x,pos.y,pos.w,pos.h);
918 memcpy(oldnetpos,newnetpos,MAX_CLIENTS*sizeof(struct _car));
920 /* play engine sound if no sound is currently playing */
921 if (lastsound_time+100<alltime)
924 lastsound_time=alltime;
925 if (config.sound) Mix_PlayMusic(engine,1);
928 /* if the car is fast or braking, it slides */
929 if ((kd && car.speed>0.5) || (car.speed>2.0 && !ku))
931 /* if the only sound is the engine, play the slide sound */
932 if (lastsound_time+100<alltime || lastsound<1)
935 lastsound_time=alltime;
936 if (config.sound) Mix_PlayMusic(slide,1)==-1;
939 /* display tires slide */
942 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,C_BLACK);
943 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,C_BLACK);
944 /* if we are braking the slide is larger */
947 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,C_BLACK);
948 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,C_BLACK);
954 if (car.crashflag && (lastsound_time+100<alltime || lastsound<2))
957 lastsound_time=alltime;
958 if (config.sound) Mix_PlayMusic(crash,1)==-1;
964 if (udpsock && net.time>MAX_LAG)
966 print(screen,WIDTH/2-strlen("Timeout !")*5,HEIGHT/2-10,"Timeout !");
968 zeRace_send_time(best);
976 /* if we completed a lap */
978 printf("time = %d\"%d\n",current.time*DELAY/1000,current.time*DELAY%1000);
979 print(screen,0,0,"Last lap : ");
980 print_time(110,0,current.time);
981 SDL_UpdateRect(screen,0,0,200,19);
982 /* if it is the first turn of the best turn, save it */
983 if (best.time==-1 || current.time<best.time)
984 memcpy(&best,¤t,sizeof(struct _record));
985 /* reset turn variables */
989 current.angle=car.angle;
990 current.speed=car.speed;
993 /* if we completed an incomplete lap */
995 print(screen,0,0,"Last lap : CANCELED ");
996 SDL_UpdateRect(screen,0,0,200,19);
997 /* reset turn variables */
1001 /* if we miss a checkpoint */
1003 print(screen,0,0,"Checkpoint missed ! ");
1004 SDL_UpdateRect(screen,0,0,200,19);
1006 /* if we validate a missed checkpoint */
1008 print(screen,0,0,"Checkpoint missed OK");
1009 SDL_UpdateRect(screen,0,0,200,19);
1016 /* let the system breath */
1022 /* display a random splash screen at startup */
1023 void zeRace_splash()
1025 SDL_Surface *splash;
1027 char temp[20]="splashs/0.jpg";
1029 SDL_FillRect(screen,NULL,C_BLACK);
1030 temp[8]=rand()%5+'1';
1031 splash=IMG_Load(temp);
1032 pos.x=screen->w/2-splash->w/2-1;
1034 pos.y=screen->h/2-splash->h/2-1;
1036 SDL_FillRect(screen,&pos,C_WHITE);
1037 pos.x=screen->w/2-splash->w/2;
1038 pos.y=screen->h/2-splash->h/2;
1039 SDL_BlitSurface(splash,NULL,screen,&pos);
1040 print(screen,screen->w/2-strlen("zeRace " VERSION)*5,screen->h/2-splash->h/2-20,"zeRace " VERSION);
1041 SDL_FreeSurface(splash);
1047 /* menu loop to select track */
1054 SDL_Surface *full,*preview;
1056 SDL_FillRect(screen,NULL,C_BLACK);
1057 print(screen,WIDTH/2-28*5,HEIGHT/6,"* Please choose your race *");
1058 print(screen,WIDTH/2-strlen(tracklist->title)*5,5*HEIGHT/6-20,tracklist->title);
1059 print(screen,WIDTH/2-(strlen(tracklist->author)+strlen("Author : "))*5,5*HEIGHT/6+0,"Author : ");
1060 print(screen,WIDTH/2-(strlen(tracklist->author)-strlen("Author : "))*5,5*HEIGHT/6+0,tracklist->author);
1061 print(screen,WIDTH/2-( strlen("Version : ")+strlen(tracklist->version))*5,5*HEIGHT/6+20,"Version : ");
1062 print(screen,WIDTH/2-(-strlen("Version : ")+strlen(tracklist->version))*5,5*HEIGHT/6+20,tracklist->version);
1063 print(screen,WIDTH/2-( strlen("Best time : ")+6+strlen(" by ")+strlen(tracklist->best_pseudo))*5,5*HEIGHT/6+40,"Best time : ");
1064 print_time (WIDTH/2-(-strlen("Best time : ")+6+strlen(" by ")+strlen(tracklist->best_pseudo))*5,5*HEIGHT/6+40,tracklist->best_time);
1065 print(screen,WIDTH/2-(-strlen("Best time : ")-6+strlen(" by ")+strlen(tracklist->best_pseudo))*5,5*HEIGHT/6+40," by ");
1066 print(screen,WIDTH/2-(-strlen("Best time : ")-6-strlen(" by ")+strlen(tracklist->best_pseudo))*5,5*HEIGHT/6+40,tracklist->best_pseudo);
1067 full=IMG_Load(tracklist->full);
1068 preview=(SDL_Surface *)zoomSurface(full,0.5,0.5,1);
1069 SDL_FreeSurface(full);
1070 pos.x=WIDTH/2-preview->w/2-1;
1072 pos.y=screen->h/2-preview->h/2-1;
1074 SDL_FillRect(screen,&pos,C_WHITE);
1075 pos.x=WIDTH/2-preview->w/2;
1076 pos.y=screen->h/2-preview->h/2;
1077 SDL_BlitSurface(preview,NULL,screen,&pos);
1078 SDL_FreeSurface(preview);
1085 while (SDL_PollEvent(&event))
1093 switch (event.key.keysym.sym)
1103 tracklist=tracklist->next;
1107 tracklist=tracklist->prev;
1122 void zeRace_top10(char *buf)
1126 SDL_FillRect(screen,NULL,C_BLACK);
1127 nb=SDLNet_Read16(buf);
1129 print(screen,WIDTH/2-16*5,HEIGHT/14,"* Race results *");
1132 print(screen,150,(i+3)*HEIGHT/14,buf);
1134 tmp=SDLNet_Read16(buf);
1137 pos.y=(i+3)*HEIGHT/14-8;
1138 SDL_BlitSurface(cars[tmp][0],NULL,screen,&pos);
1145 /* connect to a server */
1146 void zeRace_connect(char *host,int port)
1150 udpsock=SDLNet_UDP_Open(0);
1153 fprintf(stderr,"SDLNet_UDP_Open: %s\n",SDLNet_GetError());
1156 SDLNet_ResolveHost(&packet->address,host,port);
1158 strcpy(tmp,"connect");
1160 strcpy(tmp,config.pseudo);
1162 SDLNet_Write16(config.color,tmp);
1164 packet->len=(void *)tmp-(void *)packet->data;
1165 SDLNet_UDP_Send(udpsock,-1,packet);
1167 while (SDLNet_UDP_Recv(udpsock,packet) || lag<MAX_LAG)
1170 if (strcmp(tmp,"track")==0)
1172 struct _tracklist *loopcheck=tracklist;
1177 printf("server asked for track : %s\n",tmp);
1178 do if (strcmp(tracklist->name,tmp)==0) break; else tracklist=tracklist->next; while (tracklist!=loopcheck);
1179 if (strcmp(tracklist->name,tmp)!=0)
1181 fprintf(stderr,"unknown track : %s\n",tmp);
1185 time=SDLNet_Read32(tmp);
1187 network_speed=SDLNet_Read32(tmp);
1188 zeRace_launch(time,go);
1189 if (strcmp(packet->data,"finish")==0) zeRace_top10(packet->data+strlen(packet->data)+1);
1195 SDLNet_UDP_Close(udpsock);
1201 void zeRace_network()
1205 char server[MAXLINELENGTH]="localhost";
1207 #define NETWORK_OPTIONS 4
1211 SDL_FillRect(screen,NULL,C_BLACK);
1212 print(screen,380,HEIGHT/(NETWORK_OPTIONS+4)*(3+active),">");
1213 print(screen,WIDTH/2-18*5,HEIGHT/(NETWORK_OPTIONS+4),"* Network screen *");
1214 print(screen,400,HEIGHT/(NETWORK_OPTIONS+4)*3,"Server : ");
1215 print(screen,400+10*strlen("Server : "),HEIGHT/(NETWORK_OPTIONS+4)*3,server);
1216 print(screen,400,HEIGHT/(NETWORK_OPTIONS+4)*4,"Port : ");
1217 print(screen,400+10*strlen("Port : "),HEIGHT/(NETWORK_OPTIONS+4)*4,port);
1218 print(screen,400,HEIGHT/(NETWORK_OPTIONS+4)*(NETWORK_OPTIONS+1),"Connect");
1219 print(screen,400,HEIGHT/(NETWORK_OPTIONS+4)*(NETWORK_OPTIONS+2),"Back to main menu");
1226 while (SDL_PollEvent(&event))
1234 switch (event.key.keysym.sym)
1244 case 0: readstring(screen,400+10*strlen("Server : "),HEIGHT/(NETWORK_OPTIONS+4)*3,server,MAXLINELENGTH); break;
1245 case 1: readstring(screen,400+10*strlen("Port : "),HEIGHT/(NETWORK_OPTIONS+4)*4,port,5); break;;
1247 zeRace_connect(server,atoi(port));
1255 active--; if (active<0) active=NETWORK_OPTIONS-1;
1259 active++; if (active>NETWORK_OPTIONS-1) active=0;
1274 void zeRace_internet()
1279 "GET /zerace/servers.php HTTP/1.0\n"
1280 "Host: royale.zerezo.com\n"
1281 "User-Agent: zeRace " VERSION "\n"
1283 char response[10240],*tmp;
1287 char name[MAXLINELENGTH];
1293 #define INTERNET_OPTIONS 11
1295 printf("dowloading list of servers... ");
1298 if (SDLNet_ResolveHost(&ip,"royale.zerezo.com",80)==-1)
1300 fprintf(stderr,"SDLNet_ResolveHost: %s\n",SDLNet_GetError());
1304 tcpsock=SDLNet_TCP_Open(&ip);
1307 fprintf(stderr,"SDLNet_TCP_Open: %s\n",SDLNet_GetError());
1311 len=strlen(request);
1312 result=SDLNet_TCP_Send(tcpsock,request,len);
1314 fprintf(stderr,"SDLNet_TCP_Send: %s\n",SDLNet_GetError());
1317 len=SDLNet_TCP_Recv(tcpsock,response,10240);
1319 for (tmp=response;tmp<response+10240;tmp++) if (*tmp=='\n') *tmp='\0';
1321 while (*tmp!='\0' || *(tmp+1)!='\r') tmp++;
1325 strcpy(servers[i].name,tmp);
1327 strcpy(servers[i].ip,tmp);
1329 strcpy(servers[i].port,tmp);
1335 SDLNet_TCP_Close(tcpsock);
1340 SDL_FillRect(screen,NULL,C_BLACK);
1341 print(screen,380,HEIGHT/(INTERNET_OPTIONS+4)*(3+active),">");
1342 print(screen,WIDTH/2-19*5,HEIGHT/(INTERNET_OPTIONS+4),"* Internet screen *");
1344 print(screen,400,HEIGHT/(INTERNET_OPTIONS+4)*(i+3),servers[i].name);
1345 print(screen,400,HEIGHT/(INTERNET_OPTIONS+4)*(INTERNET_OPTIONS+2),"Back to main menu");
1352 while (SDL_PollEvent(&event))
1360 switch (event.key.keysym.sym)
1368 if (active==INTERNET_OPTIONS-1)
1371 zeRace_connect(servers[active].ip,atoi(servers[active].port));
1375 active--; if (active<0) active=INTERNET_OPTIONS-1;
1379 active++; if (active>INTERNET_OPTIONS-1) active=0;
1393 /* configuration screen */
1394 void zeRace_config()
1398 #define CONFIG_OPTIONS 13
1403 SDL_FillRect(screen,NULL,C_BLACK);
1404 print(screen,20,HEIGHT/(CONFIG_OPTIONS+4)*(3+active),">");
1405 print(screen,WIDTH/2-24*5,HEIGHT/(CONFIG_OPTIONS+4),"* Configuration screen *");
1406 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*3,"Pseudo : ");
1407 print(screen,40+10*strlen("Pseudo : "),HEIGHT/(CONFIG_OPTIONS+4)*3,config.pseudo);
1408 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*4,"Url : ");
1409 print(screen,40+10*strlen("Url : "),HEIGHT/(CONFIG_OPTIONS+4)*4,config.url);
1410 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*5,"Fullscreen : ");
1411 print(screen,40+10*strlen("Fullscreen : "),HEIGHT/(CONFIG_OPTIONS+4)*5,config.fullscreen?"Yes":"No");
1412 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*6,"Sound : ");
1413 print(screen,40+10*strlen("Sound : "),HEIGHT/(CONFIG_OPTIONS+4)*6,config.sound?"Yes":"No");
1414 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*7,"Tire : ");
1415 print(screen,40+10*strlen("Tire : "),HEIGHT/(CONFIG_OPTIONS+4)*7,config.tire?"Yes":"No");
1416 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*8,"Accelerate key : ");
1417 print(screen,40+10*strlen("Accelerate key : "),HEIGHT/(CONFIG_OPTIONS+4)*8,config.up?SDL_GetKeyName(config.up):"<press key>");
1418 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*9,"Brake key : ");
1419 print(screen,40+10*strlen("Brake key : "),HEIGHT/(CONFIG_OPTIONS+4)*9,config.down?SDL_GetKeyName(config.down):"<press key>");
1420 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*10,"Turn left key : ");
1421 print(screen,40+10*strlen("Turn left key : "),HEIGHT/(CONFIG_OPTIONS+4)*10,config.left?SDL_GetKeyName(config.left):"<press key>");
1422 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*11,"Turn right key : ");
1423 print(screen,40+10*strlen("Turn right key : "),HEIGHT/(CONFIG_OPTIONS+4)*11,config.right?SDL_GetKeyName(config.right):"<press key>");
1424 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*12,"Color : ");
1426 pos.y=HEIGHT/(CONFIG_OPTIONS+4)*12-7;
1427 SDL_BlitSurface(cars[config.color][0],NULL,screen,&pos);
1428 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*13,"Boss key : ");
1429 print(screen,40+10*strlen("Boss key : "),HEIGHT/(CONFIG_OPTIONS+4)*13,config.boss?SDL_GetKeyName(config.boss):"<press key>");
1430 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*14,"By night : ");
1431 print(screen,40+10*strlen("By night : "),HEIGHT/(CONFIG_OPTIONS+4)*14,config.bynight?"Yes":"No");
1432 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*(CONFIG_OPTIONS+2),"Back to main menu");
1440 while (SDL_PollEvent(&event)) switch (event.type)
1443 return event.key.keysym.sym;
1452 while (SDL_PollEvent(&event))
1460 switch (event.key.keysym.sym)
1470 case 0: readstring(screen,40+10*strlen("Pseudo : "),HEIGHT/(CONFIG_OPTIONS+4)*3,config.pseudo,MAXLINELENGTH); break;
1471 case 1: readstring(screen,40+10*strlen("Url : "),HEIGHT/(CONFIG_OPTIONS+4)*4,config.url,MAXLINELENGTH); break;;
1472 case 2: config.fullscreen=!config.fullscreen; break;
1473 case 3: config.sound=!config.sound; break;
1474 case 4: config.tire=!config.tire; break;
1475 case 5: config.up=0; update(); config.up=read_key(); break;
1476 case 6: config.down=0; update(); config.down=read_key(); break;
1477 case 7: config.left=0; update(); config.left=read_key(); break;
1478 case 8: config.right=0; update(); config.right=read_key(); break;
1480 if (event.key.keysym.sym==SDLK_LEFT) config.color--; else config.color++;
1481 if (config.color<0) config.color=NB_CARS-1;
1482 if (config.color>NB_CARS-1) config.color=0;
1484 case 10: config.boss=0; update(); config.boss=read_key(); break;
1485 case 11: config.bynight=!config.bynight; break;
1492 active--; if (active<0) active=CONFIG_OPTIONS-1;
1496 active++; if (active>CONFIG_OPTIONS-1) active=0;
1516 #define MENU_OPTIONS 5
1521 SDL_FillRect(screen,NULL,C_BLACK);
1522 pos.x=WIDTH/2-logo->w/2;
1523 pos.y=HEIGHT/6-logo->h/2;
1524 SDL_BlitSurface(logo,NULL,screen,&pos);
1525 print(screen,650,HEIGHT/6+logo->h/3,"version " VERSION);
1526 print(screen,420,HEIGHT/(MENU_OPTIONS+4)*(3+active),">");
1527 print(screen,440,HEIGHT/(MENU_OPTIONS+4)*3,"Local game");
1528 print(screen,440,HEIGHT/(MENU_OPTIONS+4)*4,"Network game");
1529 print(screen,440,HEIGHT/(MENU_OPTIONS+4)*5,"Internet game");
1530 print(screen,440,HEIGHT/(MENU_OPTIONS+4)*6,"Configuration");
1531 print(screen,440,HEIGHT/(MENU_OPTIONS+4)*7,"Exit game");
1535 logo=IMG_Load("sprites/logo.jpg");
1539 while (SDL_PollEvent(&event))
1547 switch (event.key.keysym.sym)
1557 case 0: zeRace_local(); break;
1558 case 1: zeRace_network(); break;
1559 case 2: zeRace_internet(); break;
1560 case 3: zeRace_config(); break;
1566 active--; if (active<0) active=MENU_OPTIONS-1;
1570 active++; if (active>MENU_OPTIONS-1) active=0;
1585 int main(int argc,char *argv[])