2 * zeRace 0.3, 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"
37 /* configuration constants */
42 #define MAXRECORDKEYS 9999
45 struct _tracklist *tracklist;
50 char pseudo[MAXLINELENGTH];
51 char url[MAXLINELENGTH];
60 } config = {"anonymous","",0,0,1,SDLK_UP,SDLK_DOWN,SDLK_LEFT,SDLK_RIGHT,6};
62 /* full script for a lap */
65 float x,y,angle,speed;
66 char keys[MAXRECORDKEYS];
70 /* display and all directions for the car */
72 SDL_Surface *cars[12][256];
75 UDPsocket udpsock=NULL;
80 /* read the user configuration file */
81 void zeRace_read_config()
84 if ((fic=fopen("zeRace.cfg","rt"))==NULL)
86 fprintf(stderr,"can't open config file \"zeRace.cfg\"\n");
89 fread(&config,sizeof(struct _config),1,fic);
94 /* save the user configuration file */
95 void zeRace_save_config()
98 if ((fic=fopen("zeRace.cfg","wt"))==NULL)
100 fprintf(stderr,"can't create config file \"zeRace.cfg\"\n");
103 fwrite(&config,sizeof(struct _config),1,fic);
108 /* exit the game and clean */
112 if (config.sound) Mix_CloseAudio();
115 zeRace_save_config();
120 /* check for a newer version online to warn the user */
121 void zeRace_check_version()
126 "GET /zerace/version.php HTTP/1.0\n"
127 "Host: royale.zerezo.com\n"
128 "User-Agent: zeRace " VERSION "\n"
130 char response[1024],*tmp,*version;
133 printf("checking version... ");
136 if(SDLNet_ResolveHost(&ip,"royale.zerezo.com",80)==-1)
138 fprintf(stderr,"SDLNet_ResolveHost: %s\n",SDLNet_GetError());
142 tcpsock=SDLNet_TCP_Open(&ip);
145 fprintf(stderr,"SDLNet_TCP_Open: %s\n",SDLNet_GetError());
150 result=SDLNet_TCP_Send(tcpsock,request,len);
152 fprintf(stderr,"SDLNet_TCP_Send: %s\n",SDLNet_GetError());
156 len=SDLNet_TCP_Recv(tcpsock,response,1024);
158 fprintf(stderr,"SDLNet_TCP_Recv: %s\n",SDLNet_GetError());
162 while (*tmp++!='\0') if (strncmp(tmp,"version=",8)==0)
165 while (*tmp!='\n') tmp++;
167 if (strcmp(version,VERSION)>0) printf("new version available !\nhttp://royale.zerezo.com/zerace/\n");
171 SDLNet_TCP_Close(tcpsock);
175 /* get remote list of tracks */
176 void zeRace_update_tracks()
181 "GET /zerace/tracks.php HTTP/1.0\n"
182 "Host: royale.zerezo.com\n"
183 "User-Agent: zeRace " VERSION "\n"
185 char response[10240],*tmp;
189 printf("checking version and updating tracks... ");
192 if(SDLNet_ResolveHost(&ip,"royale.zerezo.com",80)==-1)
194 fprintf(stderr,"SDLNet_ResolveHost: %s\n",SDLNet_GetError());
198 tcpsock=SDLNet_TCP_Open(&ip);
201 fprintf(stderr,"SDLNet_TCP_Open: %s\n",SDLNet_GetError());
206 result=SDLNet_TCP_Send(tcpsock,request,len);
208 fprintf(stderr,"SDLNet_TCP_Send: %s\n",SDLNet_GetError());
211 if ((fic=fopen("tracks/list.txt","wt"))==NULL)
213 fprintf(stderr,"can't create track list\n");
216 len=SDLNet_TCP_Recv(tcpsock,response,10240);
218 while (*tmp!='\n' || *(tmp+1)!='\r') tmp++;
220 fwrite(tmp,1,len+response-tmp,fic);
225 SDLNet_TCP_Close(tcpsock);
229 /* load the car sprite and rotate it for every angles */
230 void zeRace_generate_cars()
234 char temp[20]="sprites/carX.png";
238 /* load the car sprite */
240 /* and rotate it for all available angles */
245 if ((cars[i][j]=SDL_CreateRGBSurface(SDL_SWSURFACE,30,30,32,0x000000ff,0x0000ff00,0x00ff0000,0xff000000))==NULL)
247 fprintf(stderr,"CreateRGBSurface failed: %s\n",SDL_GetError());
250 tcos=cos(2*M_PI*j/256);
251 tsin=sin(2*M_PI*j/256);
252 for (x=0;x<cars[i][j]->w;x++) for (y=0;y<cars[i][j]->h;y++)
255 x2=(x-cars[i][j]->w/2.0)*tcos+(y-cars[i][j]->h/2.0)*tsin+car->w/2.0;
256 y2=(x-cars[i][j]->w/2.0)*tsin-(y-cars[i][j]->h/2.0)*tcos+car->h/2.0;
257 if (x2>0 && x2<car->w && y2>0 && y2<car->h)
258 putpixel(cars[i][j],x,y,getpixel(car,x2,y2));
261 SDL_FreeSurface(car);
266 /* initialize the game */
271 /* do a clean exit in case of emergency */
272 signal(SIGINT,zeRace_exit);
273 signal(SIGTERM,zeRace_exit);
275 /* read the user configuration file */
276 zeRace_read_config();
278 /* check for a newer available version */
279 zeRace_check_version();
281 /* update the list of tracks */
282 zeRace_update_tracks();
284 /* get the list of local tracks */
285 if (!zeRace_get_tracks(&tracklist)) zeRace_exit();
289 if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)<0)
291 fprintf(stderr,"could not initialize SDL : %s\n",SDL_GetError());
296 if(SDLNet_Init()==-1)
298 fprintf(stderr,"could not initialize SDLNet : %s\n",SDLNet_GetError());
302 packet=SDLNet_AllocPacket(1024);
305 fprintf(stderr,"SDLNet_AllocPacket: %s\n",SDLNet_GetError());
309 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,SDL_DEFAULT_REPEAT_INTERVAL);
311 flags=SDL_HWSURFACE|SDL_ANYFORMAT;
312 if (config.fullscreen) flags|=SDL_FULLSCREEN;
314 if ((screen=SDL_SetVideoMode(WIDTH,HEIGHT,32,flags))==NULL)
316 fprintf(stderr,"could not create a surface : %s\n",SDL_GetError());
320 SDL_WM_SetIcon(SDL_LoadBMP("icon.bmp"),NULL);
321 SDL_WM_SetCaption("zeRace " VERSION,"zeRace " VERSION);
322 SDL_ShowCursor(SDL_DISABLE);
324 if (config.sound) if (Mix_OpenAudio(44100,MIX_DEFAULT_FORMAT,2,512)<0)
326 fprintf(stderr,"Mix_OpenAudio error\n");
330 /* pre-calculate car sprites */
331 zeRace_generate_cars();
335 /* send the best time for this race to the web server */
336 void zeRace_send_time(struct _record record/*float x,float y,float speed,float angle,int btime,char *bkeys*/)
342 "POST /zerace/time.php HTTP/1.0\n"
343 "Host: royale.zerezo.com\n"
344 "User-Agent: zeRace " VERSION "\n"
345 "Content-Type: application/x-www-form-urlencoded\n"
346 "Content-Length: 99999\n"
350 char *msg3="&track=";
351 char *msg4="&btime=";
354 char *msg7="&speed=";
355 char *msg8="&angle=";
356 char *msg9="&bkeys=";
359 printf("sending time... ");
362 if(SDLNet_ResolveHost(&ip,"royale.zerezo.com",80)==-1)
364 fprintf(stderr,"SDLNet_ResolveHost: %s\n",SDLNet_GetError());
368 tcpsock=SDLNet_TCP_Open(&ip);
371 fprintf(stderr,"SDLNet_TCP_Open: %s\n",SDLNet_GetError());
375 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);
376 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);
379 result=SDLNet_TCP_Send(tcpsock,temp,len);
381 fprintf(stderr,"SDLNet_TCP_Send: %s\n", SDLNet_GetError());
385 SDLNet_TCP_Close(tcpsock);
389 /* print a time for a lap */
390 void print_time(int x,int y,int time)
402 temp[4]=r%100/10+'0';
405 print(screen,x,y,temp);
409 /* launch a new race */
410 void zeRace_launch(int alltime,int go)
412 SDL_Surface *cir,*fun;
415 int ku=0,kd=0,kl=0,kr=0,i;
417 struct _record current,best;
418 Mix_Music *light,*engine,*crash,*slide;
419 int lastsound_time=-999,lastsound=0;
423 struct _car oldnetpos[MAX_CLIENTS],newnetpos[MAX_CLIENTS];
425 cir=IMG_Load(tracklist->full);
426 fun=IMG_Load(tracklist->function);
428 current.speed=car.speed=0;
429 current.angle=car.angle=tracklist->a*2*M_PI/360;
430 current.x=car.ox=car.x=tracklist->x;
431 current.y=car.oy=car.y=tracklist->y;
436 best.time=MAXRECORDKEYS;
438 memset(oldnetpos,0,MAX_CLIENTS*sizeof(struct _car));
439 memset(newnetpos,0,MAX_CLIENTS*sizeof(struct _car));
441 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")))
443 fprintf(stderr,"Mix_LoadMUS error\n");
447 /* startup countdown */
450 char startup[15]="sprites/?.png";
455 SDL_BlitSurface(cir,NULL,screen,NULL);
456 SDL_BlitSurface(cars[config.color][(unsigned char)(256*car.angle/2.0/M_PI)%256],NULL,screen,&pos);
459 temp=IMG_Load(startup);
460 pos.x=screen->w/2-temp->w/2;
461 pos.y=screen->h/2-temp->h/2;
462 SDL_BlitSurface(temp,NULL,screen,&pos);
463 SDL_FreeSurface(temp);
465 if (config.sound) if (i!=4) Mix_PlayMusic(light,1);
468 if (i!=-1) SDL_Delay(1000);
474 /* look for user interaction */
475 while (SDL_PollEvent(&event))
483 switch (event.key.keysym.sym)
487 Mix_FreeMusic(light);
488 Mix_FreeMusic(engine);
489 Mix_FreeMusic(crash);
490 Mix_FreeMusic(slide);
491 /* if the best time is small enought to save all keys, send it */
492 if (best.time<MAXRECORDKEYS) zeRace_send_time(best);
495 print(screen,WIDTH/2-strlen("Disconnecting !")*5,HEIGHT/2-10,"Disconnecting !");
496 strcpy(packet->data,"disconnect");
497 packet->len=strlen(packet->data)+1;
498 SDLNet_UDP_Send(udpsock,-1,packet);
503 i=event.key.keysym.sym;
504 if (i==config.up) ku=1;
505 if (i==config.down) kd=1;
506 if (i==config.left) kl=1;
507 if (i==config.right) kr=1;
512 i=event.key.keysym.sym;
513 if (i==config.up) ku=0;
514 if (i==config.down) kd=0;
515 if (i==config.left) kl=0;
516 if (i==config.right) kr=0;
521 /* save pressed keys to validate best time */
522 if (current.time<MAXRECORDKEYS) current.keys[current.time]=(ku<<3 | kd<<2 | kl<<1 | kr)+'A';
523 current.keys[current.time+1]='\0';
524 /* and to send to server if needed */
527 net.keys[net.time]=(ku<<3 | kd<<2 | kl<<1 | kr)+'A';
528 net.keys[net.time+1]='\0';
532 /* if we are in network mode */
536 while (SDLNet_UDP_Recv(udpsock,packet)) if (strcmp(packet->data,"positions")==0)
538 /*struct _car netcar;*/
539 int servertime,clienttime,nb;
540 memcpy(&servertime,packet->data+strlen("positions")+1,sizeof(int));
541 memcpy(&clienttime,packet->data+strlen("positions")+1+sizeof(int),sizeof(int));
542 memcpy(&nb,packet->data+strlen("positions")+1+sizeof(int)+sizeof(int),sizeof(int));
543 if (clienttime>lastack)
545 memcpy(net.keys,net.keys+clienttime-lastack,net.time+1);
546 net.time-=clienttime-lastack;
547 if (clienttime>servertime+5) delay+=DELAY;
548 if (clienttime<servertime-5) delay-=DELAY;
549 if (delay<0) delay=0;
550 /* printf("servertime = %d clienttime = %d lastack = %d nb = %d delay = %d net.time = %d\n",servertime,clienttime,lastack,nb,delay,net.time); */
551 for (i=0;i<MAX_CLIENTS;i++) newnetpos[i].w=0;
552 memcpy(newnetpos,packet->data+strlen("positions")+1+sizeof(int)*3,sizeof(struct _car)*nb);
556 if (strlen(net.keys)!=0)
561 memcpy(tmp,&lastack,sizeof(int));
563 strcpy(tmp,net.keys);
565 packet->len=(void *)tmp-(void *)packet->data+10;
566 if (net.time%network_speed==0) if (!SDLNet_UDP_Send(udpsock,-1,packet))
568 fprintf(stderr,"SDLNet_UDP_Send: %s\n",SDLNet_GetError());
574 /* clear the old network position */
575 if (udpsock) for (i=0;i<MAX_CLIENTS;i++) if (oldnetpos[i].w)
577 pos.x=oldnetpos[i].x-car.w/2;
578 pos.y=oldnetpos[i].y-car.h/2;
581 SDL_BlitSurface(cir,&pos,screen,&pos);
584 /* clear the old position */
585 pos.x=car.ox-car.w/2;
586 pos.y=car.oy-car.h/2;
589 SDL_BlitSurface(cir,&pos,screen,&pos);
591 /* display the network car at the new position */
592 if (udpsock) for (i=0;i<MAX_CLIENTS;i++) if (newnetpos[i].w)
594 pos.x=newnetpos[i].x-car.w/2;
595 pos.y=newnetpos[i].y-car.h/2;
598 SDL_BlitSurface(cars[newnetpos[i].color][(unsigned char)(256*newnetpos[i].angle/2.0/M_PI)%256],NULL,screen,&pos);
601 /* display the car at the new position */
606 SDL_BlitSurface(cars[config.color][(unsigned char)(256*car.angle/2.0/M_PI)%256],NULL,screen,&pos);
611 for (i=0;i<MAX_CLIENTS;i++)
613 if (oldnetpos[i].w) SDL_UpdateRect(screen,oldnetpos[i].x-car.w/2,oldnetpos[i].y-car.h/2,car.w,car.h);
614 if (newnetpos[i].w) SDL_UpdateRect(screen,newnetpos[i].x-car.w/2,newnetpos[i].y-car.h/2,car.w,car.h);
616 memcpy(oldnetpos,newnetpos,MAX_CLIENTS*sizeof(struct _car));
618 SDL_UpdateRect(screen,car.ox-car.w/2,car.oy-car.h/2,car.w,car.h);
619 SDL_UpdateRect(screen,car.x-car.w/2,car.y-car.h/2,car.w,car.h);
621 memcpy(oldnetpos,newnetpos,MAX_CLIENTS*sizeof(struct _car));
624 move_car(&car,(ku<<3 | kd<<2 | kl<<1 | kr),fun);
626 /* play engine sound if no sound is currently playing */
627 if (lastsound_time+100<alltime)
630 lastsound_time=alltime;
631 if (config.sound) Mix_PlayMusic(engine,1);
634 /* if the car is fast or braking, it slides */
635 if ((kd && car.speed>0.5) || (car.speed>2.0 && !ku))
637 /* if the only sound is the engine, play the slide sound */
638 if (lastsound_time+100<alltime || lastsound<1)
641 lastsound_time=alltime;
642 if (config.sound) Mix_PlayMusic(slide,1)==-1;
645 /* display tires slide */
648 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,0);
649 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,0);
650 /* if we are braking the slide is larger */
653 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,0);
654 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,0);
660 if (car.crashflag && (lastsound_time+100<alltime || lastsound<2))
663 lastsound_time=alltime;
664 if (config.sound) Mix_PlayMusic(crash,1)==-1;
667 /* if we completed a lap */
670 printf("time = %d\"%d\n",current.time*DELAY/1000,current.time*DELAY%1000);
671 print(screen,0,0,"Last lap : ");
672 print_time(110,0,current.time);
673 SDL_UpdateRect(screen,0,0,170,19);
674 /* if it is the first turn of the best turn, save it */
675 if (best.time==-1 || current.time<best.time)
676 memcpy(&best,¤t,sizeof(struct _record));
677 /* reset turn variables */
681 current.angle=car.angle;
682 current.speed=car.speed;
685 /* let the system breath */
690 if (udpsock && net.time>MAX_LAG)
692 print(screen,WIDTH/2-strlen("Timeout !")*5,HEIGHT/2-10,"Timeout !");
701 /* display a random splash screen at startup */
706 char temp[20]="splashs/0.jpg";
708 SDL_FillRect(screen,NULL,0x000000);
709 temp[8]=rand()%3+'1';
710 splash=IMG_Load(temp);
711 pos.x=screen->w/2-splash->w/2-1;
713 pos.y=screen->h/2-splash->h/2-1;
715 SDL_FillRect(screen,&pos,0xffffff);
716 pos.x=screen->w/2-splash->w/2;
717 pos.y=screen->h/2-splash->h/2;
718 SDL_BlitSurface(splash,NULL,screen,&pos);
719 print(screen,screen->w/2-strlen("zeRace " VERSION)*5,screen->h/2-splash->h/2-20,"zeRace " VERSION);
720 SDL_FreeSurface(splash);
726 /* menu loop to select track */
733 SDL_Surface *full,*preview;
735 SDL_FillRect(screen,NULL,0x000000);
736 print(screen,WIDTH/2-28*5,HEIGHT/6,"* Please choose your race *");
737 print(screen,WIDTH/2-strlen(tracklist->title)*5,5*HEIGHT/6-20,tracklist->title);
738 print(screen,WIDTH/2-(strlen(tracklist->author)+strlen("Author : "))*5,5*HEIGHT/6+0,"Author : ");
739 print(screen,WIDTH/2-(strlen(tracklist->author)-strlen("Author : "))*5,5*HEIGHT/6+0,tracklist->author);
740 print(screen,WIDTH/2-( strlen("Version : ")+strlen(tracklist->version))*5,5*HEIGHT/6+20,"Version : ");
741 print(screen,WIDTH/2-(-strlen("Version : ")+strlen(tracklist->version))*5,5*HEIGHT/6+20,tracklist->version);
742 print(screen,WIDTH/2-( strlen("Best time : ")+6+strlen(" by ")+strlen(tracklist->best_pseudo))*5,5*HEIGHT/6+40,"Best time : ");
743 print_time (WIDTH/2-(-strlen("Best time : ")+6+strlen(" by ")+strlen(tracklist->best_pseudo))*5,5*HEIGHT/6+40,tracklist->best_time);
744 print(screen,WIDTH/2-(-strlen("Best time : ")-6+strlen(" by ")+strlen(tracklist->best_pseudo))*5,5*HEIGHT/6+40," by ");
745 print(screen,WIDTH/2-(-strlen("Best time : ")-6-strlen(" by ")+strlen(tracklist->best_pseudo))*5,5*HEIGHT/6+40,tracklist->best_pseudo);
746 full=IMG_Load(tracklist->full);
747 preview=(SDL_Surface *)zoomSurface(full,0.5,0.5,1);
748 SDL_FreeSurface(full);
749 pos.x=WIDTH/2-preview->w/2-1;
751 pos.y=screen->h/2-preview->h/2-1;
753 SDL_FillRect(screen,&pos,0xffffff);
754 pos.x=WIDTH/2-preview->w/2;
755 pos.y=screen->h/2-preview->h/2;
756 SDL_BlitSurface(preview,NULL,screen,&pos);
757 SDL_FreeSurface(preview);
764 while (SDL_PollEvent(&event))
772 switch (event.key.keysym.sym)
782 tracklist=tracklist->next;
786 tracklist=tracklist->prev;
801 void zeRace_top10(char *buf)
805 SDL_FillRect(screen,NULL,0x000000);
806 memcpy(&nb,buf,sizeof(int));
808 print(screen,WIDTH/2-16*5,HEIGHT/14,"* Race results *");
811 print(screen,150,(i+3)*HEIGHT/14,buf);
813 memcpy(&tmp,buf,sizeof(int));
816 pos.y=(i+3)*HEIGHT/14-8;
817 SDL_BlitSurface(cars[tmp][0],NULL,screen,&pos);
824 /* connect to a server */
825 void zeRace_connect(char *host,int port)
829 udpsock=SDLNet_UDP_Open(0);
832 fprintf(stderr,"SDLNet_UDP_Open: %s\n",SDLNet_GetError());
835 SDLNet_ResolveHost(&packet->address,host,port);
837 strcpy(tmp,"connect");
839 strcpy(tmp,config.pseudo);
841 memcpy(tmp,&config.color,sizeof(int));
843 packet->len=(void *)tmp-(void *)packet->data;
844 SDLNet_UDP_Send(udpsock,-1,packet);
846 while (SDLNet_UDP_Recv(udpsock,packet) || lag<MAX_LAG)
849 if (strcmp(tmp,"track")==0)
851 struct _tracklist *loopcheck=tracklist;
856 printf("server asked for track : %s\n",tmp);
857 while (tracklist->next!=loopcheck) if (strcmp(tracklist->name,tmp)==0) break; else tracklist=tracklist->next;
858 if (strcmp(tracklist->name,tmp)!=0)
860 fprintf(stderr,"unknown track : %s\n",tmp);
864 memcpy(&time,tmp,sizeof(int));
866 memcpy(&network_speed,tmp,sizeof(int));
867 zeRace_launch(time,go);
868 if (strcmp(packet->data,"finish")==0) zeRace_top10(packet->data+strlen(packet->data)+1);
874 SDLNet_UDP_Close(udpsock);
880 void zeRace_network()
884 char server[MAXLINELENGTH]="localhost";
886 #define NETWORK_OPTIONS 4
890 SDL_FillRect(screen,NULL,0x000000);
891 print(screen,380,HEIGHT/(NETWORK_OPTIONS+4)*(3+active),">");
892 print(screen,WIDTH/2-18*5,HEIGHT/(NETWORK_OPTIONS+4),"* Network screen *");
893 print(screen,400,HEIGHT/(NETWORK_OPTIONS+4)*3,"Server : ");
894 print(screen,400+10*strlen("Server : "),HEIGHT/(NETWORK_OPTIONS+4)*3,server);
895 print(screen,400,HEIGHT/(NETWORK_OPTIONS+4)*4,"Port : ");
896 print(screen,400+10*strlen("Port : "),HEIGHT/(NETWORK_OPTIONS+4)*4,port);
897 print(screen,400,HEIGHT/(NETWORK_OPTIONS+4)*(NETWORK_OPTIONS+1),"Connect");
898 print(screen,400,HEIGHT/(NETWORK_OPTIONS+4)*(NETWORK_OPTIONS+2),"Back to main menu");
905 while (SDL_PollEvent(&event))
913 switch (event.key.keysym.sym)
923 case 0: readstring(screen,400+10*strlen("Server : "),HEIGHT/(NETWORK_OPTIONS+4)*3,server,MAXLINELENGTH); break;
924 case 1: readstring(screen,400+10*strlen("Port : "),HEIGHT/(NETWORK_OPTIONS+4)*4,port,5); break;;
926 zeRace_connect(server,atoi(port));
934 active--; if (active<0) active=NETWORK_OPTIONS-1;
938 active++; if (active>NETWORK_OPTIONS-1) active=0;
953 void zeRace_internet()
958 "GET /zerace/servers.php HTTP/1.0\n"
959 "Host: royale.zerezo.com\n"
960 "User-Agent: zeRace " VERSION "\n"
962 char response[10240],*tmp;
966 char name[MAXLINELENGTH];
972 #define INTERNET_OPTIONS 11
974 printf("dowloading list of servers... ");
977 if(SDLNet_ResolveHost(&ip,"royale.zerezo.com",80)==-1)
979 fprintf(stderr,"SDLNet_ResolveHost: %s\n",SDLNet_GetError());
983 tcpsock=SDLNet_TCP_Open(&ip);
986 fprintf(stderr,"SDLNet_TCP_Open: %s\n",SDLNet_GetError());
991 result=SDLNet_TCP_Send(tcpsock,request,len);
993 fprintf(stderr,"SDLNet_TCP_Send: %s\n",SDLNet_GetError());
996 len=SDLNet_TCP_Recv(tcpsock,response,10240);
998 for (tmp=response;tmp<response+10240;tmp++) if (*tmp=='\n') *tmp='\0';
1000 while (*tmp!='\0' || *(tmp+1)!='\r') tmp++;
1005 strcpy(servers[i].name,tmp);
1007 strcpy(servers[i].ip,tmp);
1009 strcpy(servers[i].port,tmp);
1015 SDLNet_TCP_Close(tcpsock);
1020 SDL_FillRect(screen,NULL,0x000000);
1021 print(screen,380,HEIGHT/(INTERNET_OPTIONS+4)*(3+active),">");
1022 print(screen,WIDTH/2-19*5,HEIGHT/(INTERNET_OPTIONS+4),"* Internet screen *");
1024 print(screen,400,HEIGHT/(INTERNET_OPTIONS+4)*(i+3),servers[i].name);
1025 print(screen,400,HEIGHT/(INTERNET_OPTIONS+4)*(INTERNET_OPTIONS+2),"Back to main menu");
1032 while (SDL_PollEvent(&event))
1040 switch (event.key.keysym.sym)
1048 if (active==INTERNET_OPTIONS-1)
1051 zeRace_connect(servers[active].ip,atoi(servers[active].port));
1055 active--; if (active<0) active=INTERNET_OPTIONS-1;
1059 active++; if (active>INTERNET_OPTIONS-1) active=0;
1073 /* configuration screen */
1074 void zeRace_config()
1078 #define CONFIG_OPTIONS 11
1083 SDL_FillRect(screen,NULL,0x000000);
1084 print(screen,20,HEIGHT/(CONFIG_OPTIONS+4)*(3+active),">");
1085 print(screen,WIDTH/2-24*5,HEIGHT/(CONFIG_OPTIONS+4),"* Configuration screen *");
1086 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*3,"Pseudo : ");
1087 print(screen,40+10*strlen("Pseudo : "),HEIGHT/(CONFIG_OPTIONS+4)*3,config.pseudo);
1088 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*4,"Url : ");
1089 print(screen,40+10*strlen("Url : "),HEIGHT/(CONFIG_OPTIONS+4)*4,config.url);
1090 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*5,"Fullscreen : ");
1091 print(screen,40+10*strlen("Fullscreen : "),HEIGHT/(CONFIG_OPTIONS+4)*5,config.fullscreen?"Yes":"No");
1092 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*6,"Sound : ");
1093 print(screen,40+10*strlen("Sound : "),HEIGHT/(CONFIG_OPTIONS+4)*6,config.sound?"Yes":"No");
1094 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*7,"Tire : ");
1095 print(screen,40+10*strlen("Tire : "),HEIGHT/(CONFIG_OPTIONS+4)*7,config.tire?"Yes":"No");
1096 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*8,"Accelerate key : ");
1097 print(screen,40+10*strlen("Accelerate key : "),HEIGHT/(CONFIG_OPTIONS+4)*8,config.up?SDL_GetKeyName(config.up):"<press key>");
1098 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*9,"Brake key : ");
1099 print(screen,40+10*strlen("Brake key : "),HEIGHT/(CONFIG_OPTIONS+4)*9,config.down?SDL_GetKeyName(config.down):"<press key>");
1100 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*10,"Turn left key : ");
1101 print(screen,40+10*strlen("Turn left key : "),HEIGHT/(CONFIG_OPTIONS+4)*10,config.left?SDL_GetKeyName(config.left):"<press key>");
1102 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*11,"Turn right key : ");
1103 print(screen,40+10*strlen("Turn right key : "),HEIGHT/(CONFIG_OPTIONS+4)*11,config.right?SDL_GetKeyName(config.right):"<press key>");
1104 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*12,"Color : ");
1106 pos.y=HEIGHT/(CONFIG_OPTIONS+4)*12-7;
1107 SDL_BlitSurface(cars[config.color][0],NULL,screen,&pos);
1108 print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*(CONFIG_OPTIONS+2),"Back to main menu");
1116 while (SDL_PollEvent(&event)) switch (event.type)
1119 return event.key.keysym.sym;
1128 while (SDL_PollEvent(&event))
1136 switch (event.key.keysym.sym)
1146 case 0: readstring(screen,40+10*strlen("Pseudo : "),HEIGHT/(CONFIG_OPTIONS+4)*3,config.pseudo,MAXLINELENGTH); break;
1147 case 1: readstring(screen,40+10*strlen("Url : "),HEIGHT/(CONFIG_OPTIONS+4)*4,config.url,MAXLINELENGTH); break;;
1148 case 2: config.fullscreen=!config.fullscreen; break;
1149 case 3: config.sound=!config.sound; break;
1150 case 4: config.tire=!config.tire; break;
1151 case 5: config.up=0; update(); config.up=read_key(); break;
1152 case 6: config.down=0; update(); config.down=read_key(); break;
1153 case 7: config.left=0; update(); config.left=read_key(); break;
1154 case 8: config.right=0; update(); config.right=read_key(); break;
1156 if (event.key.keysym.sym==SDLK_LEFT) config.color--; else config.color++;
1157 if (config.color<0) config.color=11;
1158 if (config.color>11) config.color=0;
1166 active--; if (active<0) active=CONFIG_OPTIONS-1;
1170 active++; if (active>CONFIG_OPTIONS-1) active=0;
1190 #define MENU_OPTIONS 5
1195 SDL_FillRect(screen,NULL,0x000000);
1196 pos.x=WIDTH/2-logo->w/2;
1197 pos.y=HEIGHT/6-logo->h/2;
1198 SDL_BlitSurface(logo,NULL,screen,&pos);
1199 print(screen,650,HEIGHT/6+logo->h/3,"version " VERSION);
1200 print(screen,420,HEIGHT/(MENU_OPTIONS+4)*(3+active),">");
1201 print(screen,440,HEIGHT/(MENU_OPTIONS+4)*3,"Local game");
1202 print(screen,440,HEIGHT/(MENU_OPTIONS+4)*4,"Network game");
1203 print(screen,440,HEIGHT/(MENU_OPTIONS+4)*5,"Internet game");
1204 print(screen,440,HEIGHT/(MENU_OPTIONS+4)*6,"Configuration");
1205 print(screen,440,HEIGHT/(MENU_OPTIONS+4)*7,"Exit game");
1209 logo=IMG_Load("sprites/logo.jpg");
1213 while (SDL_PollEvent(&event))
1221 switch (event.key.keysym.sym)
1231 case 0: zeRace_local(); break;
1232 case 1: zeRace_network(); break;
1233 case 2: zeRace_internet(); break;
1234 case 3: zeRace_config(); break;
1240 active--; if (active<0) active=MENU_OPTIONS-1;
1244 active++; if (active>MENU_OPTIONS-1) active=0;
1259 int main(int argc,char *argv[])