#include #include #include #include #include #include #include #include #define SCREEN_WIDTH 24 #define SCREEN_HEIGHT 20 #define BYTES_PER_PIXEL 2 #include "game_sprites.h" int dump_screen=0; int clear_framebuffer(char *buffer,char color,char value) { int i=0; while(i=xsize) { offset+=((SCREEN_WIDTH-xsize)*BYTES_PER_PIXEL); xx=0; yy++; } } return 0; } int put_text_xy(char *text,int x,int y,int color, char *framebuffer) { int offset,i; offset=(y*SCREEN_WIDTH+x)*BYTES_PER_PIXEL; i=0; while(i7,colors[color&7]); old_color=color; } temp_string[0]=framebuffer[((i+j*SCREEN_WIDTH)*BYTES_PER_PIXEL)+1]; if (temp_string[0]==219) { if (!block_mode){ block_mode=1; size+=to_buffer(out_buffer,"\016"); } temp_string[0]='0'; } else { if (block_mode) { block_mode=0; size+=to_buffer(out_buffer,"\017"); } } size+=to_buffer(out_buffer,temp_string); } size+=to_buffer(out_buffer,"\n"); } if (size>40*44*10) { printf("\a\a\a\n"); exit(10); } write(1,out_buffer,size); if (dump_screen) { int fd; fd=creat("screen",0x666); write(fd,out_buffer,size); close(fd); dump_screen=0; } // fdatasync(1); return 0; } int get_timer() { static int initialized=0; static int startseconds,startusecs; struct timeval our_time; int temp_time; if (!initialized) { gettimeofday(&our_time,NULL); startseconds=time(NULL); startusecs=our_time.tv_usec; initialized=1; } gettimeofday(&our_time,NULL); temp_time=(time(NULL)-startseconds)*1000; temp_time+=(our_time.tv_usec/1000); return temp_time; } void clear_keyboard_buffer() { int ch; while(read(0,&ch,1)==1); } void pause_a_while(int msecs) { int ch,our_time; our_time=get_timer(); while ( read(0,&ch,1)!=1) { usleep(1); if ( (get_timer()-our_time) > msecs ) break; } clear_keyboard_buffer(); } int get_char() { char ch; int result; result=read(0,&ch,1); if (result<1) return 0; else return ch; } int get_a_char() { char ch,ch1,ch2,ch3; ch=get_char(); if (ch==27) { ch1=get_char(); ch2=get_char(); ch3=get_char(); if ((ch1==0) && (ch2==0)) return 'q'; if ((ch1==91) && (ch2==66)) return 'm'; if ((ch1==91) && (ch2==65)) return 'i'; if ((ch1==91) && (ch2==67)) return 'k'; if ((ch1==91) && (ch2==68)) return 'j'; if ((ch1==91) && (ch2==91)) { if (ch3==65) return 'h'; } { FILE *fff; fff=fopen("bob","w"); fprintf(fff,"%i %i %i\n",ch1,ch2,ch3); fclose(fff); } // } } return ch; } void null() { } void put_attention_block(char *framebuffer,int color) { int i,j; for(i=5;i<12;i++) for(j=0;j<40;j++) put_text_xy("*",j,i,color,framebuffer); } int verify_quit(char *framebuffer) { int quit_bar=0; char ch; put_attention_block(framebuffer,14); put_text_xy(" QUIT? Are you sure? ",9,7,12,framebuffer); put_text_xy(" NO!",9,8,12,framebuffer); put_text_xy(" YES!",9,9,12,framebuffer); quit_loop: if (quit_bar==0) { put_text_xy(" --> NO! ",9,8,13,framebuffer); put_text_xy(" YES ",9,9,5,framebuffer); } else { put_text_xy(" NO ",9,8,5,framebuffer); put_text_xy(" --> YES! ",9,9,13,framebuffer); } dump_to_screen(framebuffer); while ((ch=get_a_char())==0) usleep(5000); if ((ch=='i') || (ch=='j') || (ch=='k') || (ch=='m')) quit_bar=!quit_bar; if (ch=='y') quit_bar=1; if (ch=='n') quit_bar=0; if (ch=='\n') { return quit_bar; } goto quit_loop; return 0; } int help(char *framebuffer) { clear_framebuffer(framebuffer,0,' '); put_text_xy("TOM BOMBEM",10,0,15,framebuffer); put_text_xy("by",14,1,7,framebuffer); put_text_xy("Vince Weaver",9,2,15,framebuffer); put_text_xy("Key Bindings:",0,4,15,framebuffer); put_text_xy("UP or 'i' : Move menu up",2,6,7,framebuffer); put_text_xy("DOWN or 'm' : Move menu down",2,7,7,framebuffer); put_text_xy("ENTER : Selects current option",2,8,7,framebuffer); put_text_xy("RIGHT or 'k' : Move ship right",2,10,7,framebuffer); put_text_xy("LEFT or 'j' : Move ship left",2,11,7,framebuffer); put_text_xy("SPACEBAR : Shoots",2,12,7,framebuffer); put_text_xy("F1 or 'h' : Displays help",2,14,7,framebuffer); put_text_xy("ESC or 'q' : Quits",2,15,7,framebuffer); put_text_xy("'p' : Pauses",2,16,7,framebuffer); put_text_xy("'s' : Toggles sound",2,17,7,framebuffer); dump_to_screen(framebuffer); while(get_char()==0) usleep(30); return 0; } int map_ch; void skip_line(FILE *fff) { map_ch=fgetc(fff); while((map_ch!='\n') && (map_ch!=-1)) map_ch=fgetc(fff); } int get_int(FILE *fff) { int result=0; map_ch=fgetc(fff); while(map_ch!=-1) { while ((map_ch<=' ')&&(map_ch!=-1)) map_ch=fgetc(fff); if (map_ch==';') skip_line(fff); if ((map_ch>='0') && (map_ch<='9')) { while ((map_ch>='0') && (map_ch<='9')) { result=result*10; result+=map_ch-'0'; map_ch=fgetc(fff); } return result; } } return -1; } void panic_oom(char *string) { printf("Out of Memory: %s\n",string); exit(1); } int new_game(char *framebuffer) { int quit=0,game_paused=0,sound=1,done_waiting=0; int ch; int time_spent,oldusec=0,oldsec=0; struct timeval timing_info; struct tom_struct { int x,y,xadd,yadd; int roomx,roomy,roomz; } tom; struct enemies_struct { int x,y,xadd,yadd; char type; } enemies[2]; int total_enemies=0; int x,y,z,i,j,k,tempx,tempy; int roomx,roomy,roomz,offset; char *current_room; FILE *level_file; struct map_type { int xsize,ysize,zsize; int roomx,roomy; char **rooms; } map; tom.xadd=0; tom.yadd=0; /* Load Level */ level_file=fopen("level3.map","r"); if (level_file==NULL) { printf("File not found!\n"); exit(1); } /* Get header info */ map.xsize=get_int(level_file); map.ysize=get_int(level_file); map.zsize=get_int(level_file); map.roomx=get_int(level_file); map.roomy=get_int(level_file); printf("Found map of %i,%i,%i\n",map.xsize,map.ysize,map.zsize); printf("Roomsize of %ix%i\n",map.roomx,map.roomy); map.rooms=calloc(map.xsize*map.ysize*map.zsize,sizeof(char *)); if (map.rooms==NULL) panic_oom("map.rooms"); for(i=0;imap.xsize) || (roomy>map.ysize) || (roomz>map.zsize)) { printf("Room horribly out of bounds!\n"); exit(1); } offset=roomx+(roomy*map.xsize)+(roomz*map.xsize*map.ysize); printf("Offset=%i\n",offset); current_room=map.rooms[offset]; x=0;y=0; for(i=0;i=map.roomx) { while(map_ch!='\n') map_ch=fgetc(level_file); map_ch=fgetc(level_file); // printf("SKIP LINE, ch=%i\n",map_ch); y++; x=0; } } } clear_keyboard_buffer(); clear_framebuffer(framebuffer,0x0,' '); for(i=0;i0) tom.xadd=-1; else { tom.xadd=0; tom.x=(map.roomx-1); tom.roomx--; offset=tom.roomx+(tom.roomy*map.xsize)+(tom.roomz*map.xsize*map.ysize); // printf("Tom starting in room offset %i\n",offset); current_room=map.rooms[offset]; } break; case 'k': if (tom.x0) tom.yadd=-1; else { tom.yadd=0; tom.y=(map.roomy-1); tom.roomy--; offset=tom.roomx+(tom.roomy*map.xsize)+(tom.roomz*map.xsize*map.ysize); // printf("Tom starting in room offset %i\n",offset); current_room=map.rooms[offset]; } break; case 'm': if (tom.y