X-Git-Url: http://royale.zerezo.com/git/?p=zeRace;a=blobdiff_plain;f=zeRace.c;fp=zeRace.c;h=a4f5252267711f23f6c768d5502fd21c07c808b9;hp=6f787e059854f8975a5204079e4157ac3f0ff85d;hb=1be6de5da72b6218e87c20e29782ce6f9d99196e;hpb=ee6ec6d95d78f74973a2ba97077cc94709bb6c61 diff --git a/zeRace.c b/zeRace.c index 6f787e0..a4f5252 100644 --- a/zeRace.c +++ b/zeRace.c @@ -1,5 +1,5 @@ /* - * zeRace 0.4, a funny retro racing game + * zeRace 0.5, a funny retro racing game * http://royale.zerezo.com/zerace/ * * Copyright (C) 2004 Antoine Jacquet @@ -41,6 +41,14 @@ #define WIDTH 1024 #define HEIGHT 768 #define MAXRECORDKEYS 9999 +#define NB_CARS 16 + +/* some usefull colors */ +#define C_BLACK SDL_MapRGB(screen->format,0,0,0) +#define C_WHITE SDL_MapRGB(screen->format,255,255,255) +#define C_RED SDL_MapRGB(screen->format,255,0,0) +#define C_ORANGE SDL_MapRGB(screen->format,255,200,0) +#define C_YELLOW SDL_MapRGB(screen->format,255,255,100) /* tracklist */ struct _tracklist *tracklist; @@ -59,7 +67,8 @@ struct _config SDLKey right; int color; SDLKey boss; -} config = {"anonymous","",0,0,1,SDLK_UP,SDLK_DOWN,SDLK_LEFT,SDLK_RIGHT,6,SDLK_b}; + int bynight; +} config = {"anonymous","",0,0,1,SDLK_UP,SDLK_DOWN,SDLK_LEFT,SDLK_RIGHT,6,SDLK_b,0}; /* full script for a lap */ struct _record @@ -71,7 +80,7 @@ struct _record /* display and all directions for the car */ SDL_Surface *screen; -SDL_Surface *cars[12][256]; +SDL_Surface *cars[NB_CARS][256]; /* network stuff */ UDPsocket udpsock=NULL; @@ -83,7 +92,7 @@ int network_speed=1; void zeRace_read_config() { FILE *fic; - if ((fic=fopen("zeRace.cfg","rt"))==NULL) + if ((fic=fopen("zeRace.cfg","rb"))==NULL) { fprintf(stderr,"can't open config file \"zeRace.cfg\"\n"); return; @@ -97,7 +106,7 @@ void zeRace_read_config() void zeRace_save_config() { FILE *fic; - if ((fic=fopen("zeRace.cfg","wt"))==NULL) + if ((fic=fopen("zeRace.cfg","wb"))==NULL) { fprintf(stderr,"can't create config file \"zeRace.cfg\"\n"); return; @@ -268,7 +277,7 @@ void zeRace_download_file(char *file) fprintf(stderr,"SDLNet_TCP_Send: %s\n",SDLNet_GetError()); else { - if ((fic=fopen(file,"wt"))==NULL) + if ((fic=fopen(file,"wb"))==NULL) { fprintf(stderr,"can't create \"%s\" file\n",file); zeRace_exit(); @@ -294,7 +303,7 @@ void zeRace_generate_cars() int i,j; SDL_Surface *car; char temp[20]="sprites/carX.png"; - for (i=0;i<12;i++) + for (i=0;inext!=loopcheck) + do { zeRace_download_file(tracklist->full); zeRace_download_file(tracklist->function); tracklist=tracklist->next; - } + } while (tracklist!=loopcheck); srand(time(NULL)); @@ -481,10 +490,35 @@ void print_time(int x,int y,int time) } +/* car lights */ +void lights(int x,int y,int r,Uint32 pixel) +{ + putpixel(screen,x,y,pixel); + if (r>1) + { + putpixel(screen,x-1,y,pixel); + putpixel(screen,x+1,y,pixel); + putpixel(screen,x,y-1,pixel); + putpixel(screen,x,y+1,pixel); + } + if (r>2) + { + putpixel(screen,x-2,y,pixel); + putpixel(screen,x+2,y,pixel); + putpixel(screen,x,y-2,pixel); + putpixel(screen,x,y+2,pixel); + putpixel(screen,x-1,y-1,pixel); + putpixel(screen,x-1,y+1,pixel); + putpixel(screen,x+1,y-1,pixel); + putpixel(screen,x+1,y+1,pixel); + } +} + + /* launch a new race */ void zeRace_launch(int alltime,int go) { - SDL_Surface *cir,*fun; + SDL_Surface *cir,*fun,*hilight; SDL_Rect pos; SDL_Event event; int ku=0,kd=0,kl=0,kr=0,i; @@ -496,7 +530,8 @@ void zeRace_launch(int alltime,int go) int lastack=alltime; struct _record net; struct _car oldnetpos[MAX_CLIENTS],newnetpos[MAX_CLIENTS]; - + int l=80,o=15; + /* free memory */ void free_mem() { @@ -513,13 +548,31 @@ void zeRace_launch(int alltime,int go) } cir=IMG_Load(tracklist->full); + /* dark the track if it is night */ + if (config.bynight) + { + for (pos.x=0;pos.xw;pos.x++) + for (pos.y=0;pos.yh;pos.y++) + { + Uint32 c; + Uint8 r,g,b; + c=getpixel(cir,pos.x,pos.y); + SDL_GetRGB(c,cir->format,&r,&g,&b); + r*=0.3; + g*=0.3; + b*=0.3; + putpixel(cir,pos.x,pos.y,SDL_MapRGB(cir->format,r,g,b)); + } + } fun=IMG_Load(tracklist->function); + hilight=IMG_Load("sprites/light.png"); current.speed=car.speed=0; current.angle=car.angle=tracklist->a*2*M_PI/360; current.x=car.ox=car.x=tracklist->x; current.y=car.oy=car.y=tracklist->y; car.lastcheck=0; + car.lapflag=0; car.w=cars[0][0]->w; car.h=cars[0][0]->h; current.time=0; @@ -626,6 +679,9 @@ void zeRace_launch(int alltime,int go) net.keys[net.time+1]='\0'; } + /* move the car */ + move_car(&car,(ku<<3 | kd<<2 | kl<<1 | kr),fun); + delay=DELAY; /* if we are in network mode */ if (udpsock!=NULL) @@ -648,14 +704,27 @@ void zeRace_launch(int alltime,int go) for (i=0;idata+strlen("positions")+1+4+4+2+i*8); - newnetpos[i].y=SDLNet_Read16(packet->data+strlen("positions")+1+4+4+2+i*8+2); - newnetpos[i].angle=(float)SDLNet_Read16(packet->data+strlen("positions")+1+4+4+2+i*8+2+2)/1000; - newnetpos[i].color=SDLNet_Read16(packet->data+strlen("positions")+1+4+4+2+i*8+2+2+2); + newnetpos[i].x=SDLNet_Read16(packet->data+strlen("positions")+1+4+4+2+i*14); + newnetpos[i].y=SDLNet_Read16(packet->data+strlen("positions")+1+4+4+2+i*14+2); + newnetpos[i].angle=(float)SDLNet_Read16(packet->data+strlen("positions")+1+4+4+2+i*14+2+2)/1000; + newnetpos[i].color=SDLNet_Read16(packet->data+strlen("positions")+1+4+4+2+i*14+2+2+2); + newnetpos[i].lights_brake=SDLNet_Read16(packet->data+strlen("positions")+1+4+4+2+i*14+2+2+2+2); + newnetpos[i].lights_backwards=SDLNet_Read16(packet->data+strlen("positions")+1+4+4+2+i*14+2+2+2+2+2); + newnetpos[i].lights_warning=SDLNet_Read16(packet->data+strlen("positions")+1+4+4+2+i*14+2+2+2+2+2+2); } lastack=clienttime; } } + else if (strcmp(packet->data,"collision")==0) + { + net.time=-1; + net.keys[0]='\0'; + lastack=SDLNet_Read32(packet->data+strlen("collision")+1); + car.x=(float)SDLNet_Read32(packet->data+strlen("collision")+1+4)/65536-100; + car.y=(float)SDLNet_Read32(packet->data+strlen("collision")+1+4+4)/65536-100; + car.speed=(float)SDLNet_Read32(packet->data+strlen("collision")+1+4+4+4)/65536-100; + car.angle=(float)SDLNet_Read32(packet->data+strlen("collision")+1+4+4+4+4)/65536-100; + } else /* end of this network race */ { zeRace_send_time(best); @@ -669,6 +738,10 @@ void zeRace_launch(int alltime,int go) tmp+=strlen(tmp)+1; SDLNet_Write32(lastack,tmp); tmp+=4; + SDLNet_Write32(car.x,tmp); + tmp+=4; + SDLNet_Write32(car.y,tmp); + tmp+=4; strcpy(tmp,net.keys); tmp+=strlen(tmp)+1; packet->len=(void *)tmp-(void *)packet->data+10; @@ -697,6 +770,25 @@ void zeRace_launch(int alltime,int go) pos.h=car.h; SDL_BlitSurface(cir,&pos,screen,&pos); + /* clear the lights */ + if (config.bynight) + { + pos.x=car.ox-cos(car.angle)*l-sin(car.angle)*o; + pos.y=car.oy-sin(car.angle)*l+cos(car.angle)*o; + pos.x-=60; + pos.y-=60; + pos.w=120; + pos.h=120; + SDL_BlitSurface(cir,&pos,screen,&pos); + pos.x=car.ox-cos(car.angle)*l+sin(car.angle)*o; + pos.y=car.oy-sin(car.angle)*l-cos(car.angle)*o; + pos.x-=60; + pos.y-=60; + pos.w=120; + pos.h=120; + SDL_BlitSurface(cir,&pos,screen,&pos); + } + /* display the network car at the new position */ if (udpsock) for (i=0;iscreen->w) pos.w=screen->w-pos.x; + if (pos.y+pos.h>screen->h) pos.h=screen->h-pos.y; + SDL_UpdateRect(screen,pos.x,pos.y,pos.w,pos.h); + pos.x=car.x-cos(car.angle)*l+sin(car.angle)*15; + pos.y=car.y-sin(car.angle)*l-cos(car.angle)*15; + pos.x-=60; + pos.y-=60; + pos.w=120; + pos.h=120; + if (pos.x<0) pos.x=0; + if (pos.y<0) pos.y=0; + if (pos.x+pos.w>screen->w) pos.w=screen->w-pos.x; + if (pos.y+pos.h>screen->h) pos.h=screen->h-pos.y; + SDL_UpdateRect(screen,pos.x,pos.y,pos.w,pos.h); + } + memcpy(oldnetpos,newnetpos,MAX_CLIENTS*sizeof(struct _car)); - /* move the car */ - move_car(&car,(ku<<3 | kd<<2 | kl<<1 | kr),fun); - /* play engine sound if no sound is currently playing */ if (lastsound_time+100w/2-splash->w/2-1; pos.w=splash->w+2; pos.y=screen->h/2-splash->h/2-1; pos.h=splash->h+2; - SDL_FillRect(screen,&pos,0xffffff); + SDL_FillRect(screen,&pos,C_WHITE); pos.x=screen->w/2-splash->w/2; pos.y=screen->h/2-splash->h/2; SDL_BlitSurface(splash,NULL,screen,&pos); @@ -844,7 +1053,7 @@ void zeRace_local() { SDL_Surface *full,*preview; SDL_Rect pos; - SDL_FillRect(screen,NULL,0x000000); + SDL_FillRect(screen,NULL,C_BLACK); print(screen,WIDTH/2-28*5,HEIGHT/6,"* Please choose your race *"); print(screen,WIDTH/2-strlen(tracklist->title)*5,5*HEIGHT/6-20,tracklist->title); print(screen,WIDTH/2-(strlen(tracklist->author)+strlen("Author : "))*5,5*HEIGHT/6+0,"Author : "); @@ -862,7 +1071,7 @@ void zeRace_local() pos.w=preview->w+2; pos.y=screen->h/2-preview->h/2-1; pos.h=preview->h+2; - SDL_FillRect(screen,&pos,0xffffff); + SDL_FillRect(screen,&pos,C_WHITE); pos.x=WIDTH/2-preview->w/2; pos.y=screen->h/2-preview->h/2; SDL_BlitSurface(preview,NULL,screen,&pos); @@ -914,7 +1123,7 @@ void zeRace_top10(char *buf) { int i,nb,tmp; SDL_Rect pos; - SDL_FillRect(screen,NULL,0x000000); + SDL_FillRect(screen,NULL,C_BLACK); nb=SDLNet_Read16(buf); buf+=2; print(screen,WIDTH/2-16*5,HEIGHT/14,"* Race results *"); @@ -966,7 +1175,7 @@ void zeRace_connect(char *host,int port) tmp+=strlen(tmp)+1; go=*tmp++; printf("server asked for track : %s\n",tmp); - while (tracklist->next!=loopcheck) if (strcmp(tracklist->name,tmp)==0) break; else tracklist=tracklist->next; + do if (strcmp(tracklist->name,tmp)==0) break; else tracklist=tracklist->next; while (tracklist!=loopcheck); if (strcmp(tracklist->name,tmp)!=0) { fprintf(stderr,"unknown track : %s\n",tmp); @@ -999,7 +1208,7 @@ void zeRace_network() void update() { - SDL_FillRect(screen,NULL,0x000000); + SDL_FillRect(screen,NULL,C_BLACK); print(screen,380,HEIGHT/(NETWORK_OPTIONS+4)*(3+active),">"); print(screen,WIDTH/2-18*5,HEIGHT/(NETWORK_OPTIONS+4),"* Network screen *"); print(screen,400,HEIGHT/(NETWORK_OPTIONS+4)*3,"Server : "); @@ -1128,7 +1337,7 @@ void zeRace_internet() void update() { int i; - SDL_FillRect(screen,NULL,0x000000); + SDL_FillRect(screen,NULL,C_BLACK); print(screen,380,HEIGHT/(INTERNET_OPTIONS+4)*(3+active),">"); print(screen,WIDTH/2-19*5,HEIGHT/(INTERNET_OPTIONS+4),"* Internet screen *"); for (i=0;i<10;i++) @@ -1186,12 +1395,12 @@ void zeRace_config() { SDL_Event event; int active=0; - #define CONFIG_OPTIONS 12 + #define CONFIG_OPTIONS 13 void update() { SDL_Rect pos; - SDL_FillRect(screen,NULL,0x000000); + SDL_FillRect(screen,NULL,C_BLACK); print(screen,20,HEIGHT/(CONFIG_OPTIONS+4)*(3+active),">"); print(screen,WIDTH/2-24*5,HEIGHT/(CONFIG_OPTIONS+4),"* Configuration screen *"); print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*3,"Pseudo : "); @@ -1218,6 +1427,8 @@ void zeRace_config() SDL_BlitSurface(cars[config.color][0],NULL,screen,&pos); print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*13,"Boss key : "); print(screen,40+10*strlen("Boss key : "),HEIGHT/(CONFIG_OPTIONS+4)*13,config.boss?SDL_GetKeyName(config.boss):""); + print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*14,"By night : "); + print(screen,40+10*strlen("By night : "),HEIGHT/(CONFIG_OPTIONS+4)*14,config.bynight?"Yes":"No"); print(screen,40,HEIGHT/(CONFIG_OPTIONS+4)*(CONFIG_OPTIONS+2),"Back to main menu"); SDL_Flip(screen); } @@ -1267,11 +1478,12 @@ void zeRace_config() case 8: config.right=0; update(); config.right=read_key(); break; case 9: if (event.key.keysym.sym==SDLK_LEFT) config.color--; else config.color++; - if (config.color<0) config.color=11; - if (config.color>11) config.color=0; + if (config.color<0) config.color=NB_CARS-1; + if (config.color>NB_CARS-1) config.color=0; break; case 10: config.boss=0; update(); config.boss=read_key(); break; - case 11: + case 11: config.bynight=!config.bynight; break; + case 12: return; } update(); @@ -1306,7 +1518,7 @@ void zeRace_menu() void update() { SDL_Rect pos; - SDL_FillRect(screen,NULL,0x000000); + SDL_FillRect(screen,NULL,C_BLACK); pos.x=WIDTH/2-logo->w/2; pos.y=HEIGHT/6-logo->h/2; SDL_BlitSurface(logo,NULL,screen,&pos);