Main Page | Class List | Directories | File List | Class Members | File Members

scroller.c

Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <allegro.h>
00003 #include "include/supercsbros.h"
00004 
00005 #define DEBUG 1
00006 
00007 int battle = 0;
00008 
00009 int main (void)
00010 {
00011 
00012   int nticks;
00013   initialize();
00014   main_menu();
00015   loadsprites();
00016 
00017   LOCK_VARIABLE(ticks);
00018   LOCK_FUNCTION(ticker);
00019   install_int_ex(ticker, MSEC_TO_TIMER( (int)(1000 * env->diff_mod)));
00020   env->ticks=0;
00021 
00022   while (!key[KEY_ESC])
00023     {
00024 
00025       clear(buffer);
00026       player->oldpy = player->y; /* XXX: Here be dragons.  We keep track of last  */
00027       player->oldpx = player->x; /*      position for collision detection..kinda. */
00028       nticks = env->ticks;
00029       env->ticks = 0;
00030 
00031       for (; nticks >0; nticks--) {
00032         --env->counter;
00033       }
00034       if (battle == 1)
00035         boss_battle();
00036       player->collision=0;
00037       checkinput();
00038       updateplayer();
00039       updatebullets();
00040       update_enemies();
00041       updateitems();
00042       if (DEBUG) {
00043         draw_crosshair();
00044         updatestats();
00045       }
00046       updatescore();
00047       updatetime();
00048       
00049       vsync();
00050       acquire_screen();
00051       blit(buffer, screen, 0, 0, 0, 0, WIDTH-1, HEIGHT-1);
00052       release_screen();
00053     }
00054 
00055   MapFreeMem();
00056   allegro_exit();
00057   return 0;
00058 }
00059 
00060 
00061 
00062 void initialize()
00063 {
00064   allegro_init();
00065 
00066   install_timer();
00067   install_keyboard();
00068   install_mouse(); 
00069   
00070   set_color_depth(32);
00071   set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0);
00072   buffer = create_bitmap (WIDTH, HEIGHT);
00073   clear(buffer);
00074 
00075   enable_hardware_cursor();
00076   select_mouse_cursor(MOUSE_CURSOR_ALLEGRO);
00077   show_mouse(screen);
00078 
00079   if (MapLoad(map_file))
00080     {
00081       allegro_message("Error reading the map file: %s", map_file);
00082       exit(1);
00083     }
00084   player = malloc(sizeof(SPRITE));
00085   stats  = malloc(sizeof(STATS));
00086   env    = malloc(sizeof(ENVIRONMENT));
00087   if (!player || !stats || !env) {
00088     allegro_message("Error allocating memory for structs...bye.");
00089     exit(1);
00090   }
00091 
00092   stats->health = 25;
00093   stats->score  = 0;
00094   stats->lives  = 3;
00095 
00096   env->counter  = 300;
00097   env->ticks    = 0;
00098   env->diff_mod = 2.00;
00099 
00100   player->x = 0;
00101   player->y = 0;
00102 
00103   OSinit(os_type);
00104   return;
00105 }
00106 
00107 void main_menu()
00108 {
00109   int i = 0;
00110   int s = 3;
00111   
00112   BITMAP *bg;
00113   /* in order: start, settings, quit */
00114   BITMAP *menu_items_unselected[3];
00115   BITMAP *menu_items_selected[3];
00116 
00117   bg = load_bitmap(bg_file, NULL);
00118   if (!bg) {
00119     allegro_message("Error loading %s", bg);
00120     exit(1);
00121   }
00122 
00123   menu_items_unselected[0] = load_bitmap("menus/start_unselected.bmp", NULL);
00124   menu_items_unselected[1] = load_bitmap("menus/settings_unselected.bmp", NULL);
00125   menu_items_unselected[2] = load_bitmap("menus/quit_unselected.bmp", NULL);
00126 
00127 /*   menu_items_selected[0]  = load_bitmap("menus/start_selected.bmp", NULL); */
00128 /*   menu_items_selected[1]  = load_bitmap("menus/settings_selected.bmp", NULL); */
00129 /*   menu_items_selected[2]  = load_bitmap("menus/quit_selected.bmp", NULL); */
00130 
00131 
00132   blit(bg, screen, 0, 0, 0, 0, bg->w, bg->h);
00133   for (i=0; i<3; i++) {
00134     blit(menu_items_unselected[i], screen, 0, 0, 300, (200 + i * 64), SCREEN_W-1, SCREEN_H-1);
00135   }
00136 /*   blit(menu_items_selected[s], screen, 0, 0, 300, (200 + s * 64), SCREEN_W-1, SCREEN_H-1); */
00137   
00138   while (1)
00139     {
00140       mouse_b=0;
00141       if ((mouse_x >= 300) && (mouse_x <= 510) && (mouse_y >= 200) && (mouse_y <= 263)) s = 0;
00142       if ((mouse_x >= 300) && (mouse_x <= 510) && (mouse_y >= 264) && (mouse_y <= 327)) s = 1;
00143       if ((mouse_x >= 300) && (mouse_x <= 510) && (mouse_y >= 328) && (mouse_y <= 392)) s = 2;
00144 
00145       /* set selected item based on bounding rectangle */
00146       if (mouse_b & 1) {
00147         switch (s) {
00148         case 0: break;
00149         case 1: 
00150           blit(bg, screen, 0, 0, 0, 0, bg->w, bg->h);
00151           settings_menu();
00152           blit(bg, screen, 0, 0, 0, 0, bg->w, bg->h);
00153           for (i=0; i<3; i++)
00154             blit(menu_items_unselected[i], screen, 0, 0, 300, (200 + i * 64), SCREEN_W-1, SCREEN_H-1);
00155           break;
00156         case 2: quitgame();
00157         }
00158         if (s == 0) break;
00159       }
00160     }
00161   return;
00162 }
00163 
00164 void quitgame()
00165 {
00166   allegro_exit();
00167   exit(0);
00168 }
00169 
00170 void settings_menu()
00171 {
00172   int i;
00173   int s = 0;
00174 
00175   BITMAP *bg;
00176   //  BITMAP *difficulty_level_on[3];
00177   BITMAP *difficulty_level[3];
00178   BITMAP *on, *off, *done;
00179   BITMAP *difficulty, *audio;
00180 
00181   //  bg = load_bitmap("images/background.bmp", NULL);
00182   //blit(bg, screen, 0, 0, 0, 0, bg->w, bg->h);
00183  
00184   difficulty = load_bitmap("menus/difficulty.bmp", NULL);
00185   audio = load_bitmap("menus/audio.bmp", NULL);
00186   done  = load_bitmap("menus/done_unselected.bmp", NULL);
00187   on    = load_bitmap("menus/on_unselected.bmp", NULL);
00188   off   = load_bitmap("menus/off_unselected.bmp", NULL);
00189  
00190   difficulty_level[0] = load_bitmap("menus/a_unselected.bmp", NULL);
00191   difficulty_level[1] = load_bitmap("menus/b_unselected.bmp", NULL);
00192   difficulty_level[2] = load_bitmap("menus/c_unselected.bmp", NULL);
00193 /*   difficulty_level_on[0]  = load_bitmap("menus/a_selected.bmp", NULL); */
00194 /*   difficulty_level_on[1]  = load_bitmap("menus/b_selected.bmp", NULL); */
00195 /*   difficulty_level_on[2]  = load_bitmap("menus/c_selected.bmp", NULL); */
00196 
00197   blit(difficulty, screen,0, 0, 85, 200, difficulty->w, difficulty->h);
00198   blit(audio, screen, 0, 0, 250, 240, audio->w, audio->h);
00199   blit(done, screen, 0, 0, 270, 280, done->w, done->h);
00200   blit(on, screen, 0, 0, 410, 240, on->w, on->h);
00201   blit(off, screen, 0, 0, 470, 240, off->w, off->h);
00202 
00203   for (i=0; i<3; i++) {
00204     blit(difficulty_level[i], screen, 0, 0, (400 + i * 70), 200, SCREEN_W-1, SCREEN_H-1);
00205   }
00206   
00207   while (1)
00208     {
00209        /* which option are we selecting? */
00210       if ((mouse_x >= 400) && (mouse_x <= 460) && (mouse_y >= 200) && (mouse_y <= 240)) s = 0; /* A Option */
00211       if ((mouse_x >= 470) && (mouse_x <= 530) && (mouse_y >= 200) && (mouse_y <= 240)) s = 1; /* B  "     */
00212       if ((mouse_x >= 540) && (mouse_x <= 600) && (mouse_y >= 200) && (mouse_y <= 240)) s = 2; /* C  "     */
00213       if ((mouse_x >= 410) && (mouse_x <= 460) && (mouse_y >= 241) && (mouse_y <= 280)) s = 3; /* Audio on */
00214       if ((mouse_x >= 470) && (mouse_x <= 530) && (mouse_y >= 241) && (mouse_y <= 280)) s = 4; /* Audio off*/
00215       if ((mouse_x >= 270) && (mouse_x <= 375) && (mouse_y >= 280) && (mouse_y <= 320)) s = 5; /* Done     */
00216 
00217       /* set selected item based on bounding rectangle */
00218       if (mouse_b & 1) {
00219         switch (s) {
00220         case 0: env->diff_mod = 0.42;   break; /* A option */
00221         case 1: env->diff_mod = 0.75;   break; /* B  "     */
00222         case 2: env->diff_mod = 1.00;   break; /* C  "     */
00223         case 3: env->audio_enabled = 1; break; 
00224         case 4: env->audio_enabled = 0; break;
00225         case 5: break;                         /* Just one of those days. */
00226         }
00227         if (s == 5) break;
00228       }
00229     }
00230   //  destroy_bitmap(bg);
00231   destroy_bitmap(difficulty);
00232   destroy_bitmap(audio);
00233   destroy_bitmap(done);
00234   destroy_bitmap(on);
00235   destroy_bitmap(off);
00236   return;
00237 }
00238 
00239 void ticker()
00240 {
00241   ++env->ticks;
00242   return;
00243 }
00244 
00245 void updatetime()
00246 {
00247   if (env->counter > 0)
00248     textprintf_ex(screen, font, (WIDTH-100), 5, WHITE, -1, "Time: %d", env->counter);
00249   else
00250     {
00251       allegro_message("You've run out of time.  We should do someting now...like restart the level!\n");
00252       restart_level();
00253     }
00254   return;
00255 }
00256 
00257 void updatescore()
00258 {
00259   textprintf_ex(buffer, font, 15, 5, WHITE, -1,"Score: %d", stats->score);
00260   textprintf_ex(buffer, font, 105, 5, WHITE, -1, "Lives: %d", stats->lives);
00261   textprintf_ex(buffer, font, 210, 5, WHITE, -1, "Jump: %d", jump);
00262   return;
00263 }
00264 
00265 void updatestats()
00266 {
00267   int n;
00268   draw_sprite(buffer, progress_player, 14, 15);
00269    
00270   for (n = 0; n < stats->health; n++) {
00271     draw_sprite(buffer, bar, 15+n*5, 17);
00272   }
00273   textprintf_ex(buffer, font, 300, 5, WHITE, -1, "Player->x: %d", player->x);
00274   textprintf_ex(buffer, font, 450, 5, WHITE, -1, "Player->y: %d", player->y);
00275   textprintf_ex(buffer, font, 300, 20, WHITE, -1, "Difficulty Modifier: %3f", env->diff_mod);
00276   textprintf_ex(buffer, font, 300, 45, WHITE, -1, "Rightfacing: %d", player->rightfacing);
00277   if (jump < 0) {textprintf_ex(buffer, font, 400, 300, WHITE, -1, "FALLING!");}
00278   if (player->collision) {textprintf_ex(buffer, font, 400, 315, WHITE, -1, "Collision!!");}
00279 }
00280 
00281 void updatebossstats()
00282 {
00283   int n;
00284   draw_sprite(buffer, progress_boss, SCREEN_W - 150, 17);
00285    
00286   for (n = 0; n < bstats->health; n++) {
00287     draw_sprite(buffer, bar, SCREEN_W - 150 +n*5, 17);
00288   }
00289 }
00290 
00291 
00292 BITMAP *grabframe(BITMAP *source, 
00293                   int width, int height, 
00294                   int startx, int starty, 
00295                   int columns, int frame)
00296 {
00297   BITMAP *temp = create_bitmap(width,height);
00298   int x = startx + (frame % columns) * width;
00299   int y = starty + (frame / columns) * height;
00300   blit(source,temp,x,y,0,0,width,height);
00301   return temp;
00302 }
00303 
00304 
00305 void loadsprites()
00306 {
00307   int n;
00308 
00309   // used for health status stevie
00310   temp = load_bitmap("images/progress-player.bmp", NULL);
00311   progress_player = grabframe(temp,130,14,0,0,1,0);
00312   bar = grabframe(temp,6,10,130,2,1,0);
00313   destroy_bitmap(temp);
00314 
00315   //boss health
00316   temp = load_bitmap("images/progress-boss.bmp", NULL);
00317 
00318   progress_boss = grabframe(temp,130,14,0,0,1,0);
00319   bar = grabframe(temp,6,10,130,2,1,0);
00320   destroy_bitmap(temp);
00321   
00322   // Stevie's sprites
00323   temp = load_bitmap(player_bitmap, NULL);
00324   for (n=0; n<8; n++)
00325     player_images[n] = grabframe(temp,50,64,0,0,8,n);
00326   destroy_bitmap(temp);
00327     
00328   player = malloc(sizeof(SPRITE));
00329   player->x = 80;
00330   player->y = 100;
00331   player->curframe=0;
00332   player->framecount=0;
00333   player->framedelay=6;
00334   player->rightfacing=0;
00335   player->maxframe=7;
00336   player->width=player_images[0]->w;
00337   player->height=player_images[0]->h;
00338   player->collision=0;
00339     
00340   temp = load_bitmap(enemy_bitmap1, NULL);
00341   for (n=0; n<8; n++)
00342     mis_1_images[n] = grabframe(temp,50,64,0,0,8,n);
00343   destroy_bitmap(temp);
00344 
00345   for (n=0; n<MAX_ENEMIES; n++) {
00346     enemies[n] = malloc(sizeof(SPRITE));
00347     enemies[n]->alive = 1;
00348     enemies[n]->width = mis_1_images[0]->w;
00349     enemies[n]->height = mis_1_images[0]->h;
00350     enemies[n]->rightfacing = 0;
00351     enemies[n]->xdelay = 4;
00352     enemies[n]->ydelay = 0;
00353     enemies[n]->xcount = 5;
00354     enemies[n]->ycount = 0;
00355     enemies[n]->xspeed = 0;
00356     enemies[n]->yspeed = 0;
00357     enemies[n]->curframe = 0;
00358     enemies[n]->maxframe = 2;
00359     enemies[n]->framecount = 0;
00360     enemies[n]->framedelay = 10;
00361     enemies[n]->animdir = 1;
00362   }
00363   enemies[0]->x = 524;   enemies[0]->y = 287;
00364   enemies[1]->x = 1456;  enemies[1]->y = 127;
00365   enemies[2]->x = 2316;  enemies[2]->y = 286;
00366   enemies[3]->x = 2656;  enemies[3]->y = 286;
00367   enemies[4]->x = 3632;  enemies[4]->y = 158;
00368   enemies[5]->x = 5256;  enemies[5]->y = 446;
00369   enemies[6]->x = 5196;  enemies[6]->y = 254;
00370   enemies[7]->x = 5592;  enemies[7]->y = 254;
00371   enemies[8]->x = 6280;  enemies[8]->y = 319;
00372   enemies[9]->x = 7436;  enemies[9]->y = 190;
00373 
00374 
00375   temp = load_bitmap(boss_bitmap, NULL);
00376   for (n=0; n<8; n++)
00377     boss_images[n] = grabframe(temp,50,64,0,0,8,n);
00378   destroy_bitmap(temp);
00379 
00380   boss = malloc(sizeof(SPRITE));
00381   boss->x = 8890 ;
00382   boss->y = 126 ;
00383   boss->curframe = 0;
00384   boss->alive=1;
00385   boss->framecount = 0;
00386   boss->framedelay = 4;
00387   boss->maxframe=7;
00388   boss->width=boss_images[0]->w;
00389   boss->height=boss_images[0]->h;
00390   boss->collision = 0;
00391   boss->rightfacing=0;
00392 
00393   cd_image = load_bitmap(cd_bitmap, NULL);
00394 
00395   for (n=0; n<5; n++) {
00396     cd[n] = malloc(sizeof(SPRITE));
00397     cd[n]->width = cd_image->w;
00398     cd[n]->height= cd_image->h;
00399     cd[n]->alive=1;
00400   }
00401   
00402   cd[0]->x = 440;   cd[0]->y = 200;
00403   cd[1]->x = 1760;  cd[1]->y = 222;
00404   cd[2]->x = 3340;  cd[2]->y = 200;
00405   cd[3]->x = 4180;  cd[3]->y = 320;
00406   cd[4]->x = 5600;  cd[4]->y = 200;
00407 
00408 
00409   jumpdrive_image = load_bitmap(jumpdrive_bitmap, NULL);
00410   for (n=0; n<6; n++) {
00411     jumpdrive[n] = malloc(sizeof(SPRITE));
00412     jumpdrive[n]->width = jumpdrive_image->w;
00413     jumpdrive[n]->height= jumpdrive_image->h;
00414     jumpdrive[n]->alive=1;
00415   }
00416 
00417   jumpdrive[0]->x = 750;   jumpdrive[0]->y = 180;
00418   jumpdrive[1]->x = 1184;  jumpdrive[1]->y = 382;
00419   jumpdrive[2]->x = 2780;  jumpdrive[2]->y = 382;
00420   jumpdrive[3]->x = 3800;  jumpdrive[3]->y = 60;
00421   jumpdrive[4]->x = 5250;  jumpdrive[4]->y = 400;
00422   
00423 
00424 
00425 
00426   bullet_image = load_bitmap("objects/ball.bmp", NULL);
00427 
00428   for (n=0; n<MAX_BULLETS; n++) {
00429     bullets[n] = malloc(sizeof(SPRITE));
00430     bullets[n]->alive=0;
00431     bullets[n]->x=0;
00432     bullets[n]->y=0;
00433     bullets[n]->xspeed=6;
00434     bullets[n]->yspeed=0;
00435     bullets[n]->xdelay=0;
00436     bullets[n]->ydelay=0;
00437     bullets[n]->xcount=1;
00438     bullets[n]->ycount=0;
00439     bullets[n]->curframe=0;
00440     bullets[n]->maxframe=0;
00441     bullets[n]->animdir=0;
00442     bullets[n]->framedelay=0;
00443     bullets[n]->framecount=0;
00444     bullets[n]->width=bullet_image->w;
00445     bullets[n]->height=bullet_image->h;
00446   }
00447 
00448 }
00449 
00450 int inside(int x,int y,int left,int top,int right,int bottom)
00451 {
00452     if (x > left && x < right && y > top && y < bottom)
00453         return 1;
00454     else
00455         return 0;
00456 }
00457 
00458 void updatebullet(SPRITE *spr)
00459 {
00460     int n,x,y;
00461     int x1,y1,x2,y2;
00462 
00463     updatesprite(spr);
00464 
00465     if (spr->x < mapxoff || spr->x > mapxoff + SCREEN_W)
00466     {
00467         spr->alive = 0;
00468         return;
00469     }
00470 
00471     for (n=0; n<MAX_ENEMIES; n++)
00472     {
00473         if (enemies[n]->alive)
00474         {
00475           x = spr->x + spr->width/2 - mapxoff;
00476           y = spr->y + spr->height/2 - mapyoff;
00477           
00478           x1 = enemies[n]->x - mapxoff;
00479           y1 = enemies[n]->y - mapyoff;
00480           x2 = x1 + enemies[n]->width;
00481           y2 = y1 + enemies[n]->height;
00482           
00483           if (inside(x, y, x1, y1, x2, y2))
00484             {
00485               if (env->audio_enabled == 1)
00486                 play_sample(sounds[HIT], VOLUME, PAN, PITCH, FALSE);
00487               enemies[n]->alive=0;
00488               spr->alive=0;
00489               //startexplosion(spr->x+16, spr->y);
00490               
00491             }
00492       }
00493     }
00494     if (boss->alive) {
00495       x = spr->x + spr->width/2 - mapxoff;
00496       y = spr->y + spr->height/2 - mapyoff;
00497       
00498       x1 = boss->x - mapxoff;
00499       y1 = boss->y - mapyoff;
00500       x2 = x1 + boss->width;
00501       y2 = y1 + boss->height;
00502       
00503       if (inside(x, y, x1, y1, x2, y2))
00504         {
00505           if (env->audio_enabled == 1)
00506             play_sample(sounds[HIT], VOLUME, PAN, PITCH, FALSE);
00507           bstats->health -= 1;
00508         }
00509     }
00510 }
00511 
00512 void updatebullets()
00513 {
00514   int n;
00515   for (n=0; n<MAX_BULLETS; n++) {
00516     if (bullets[n]->alive) {
00517       updatebullet(bullets[n]);
00518       draw_sprite(buffer,bullet_image, bullets[n]->x-mapxoff, bullets[n]->y-mapyoff);
00519     }
00520   }
00521 }
00522 
00523 void updateitems()
00524 {
00525   int n;
00526   for (n=0; n<5; n++) {
00527     if (cd[n]->alive) {
00528       draw_sprite(buffer, cd_image, cd[n]->x-mapxoff, cd[n]->y-mapyoff);
00529     }
00530     if (jumpdrive[n]->alive) {
00531       draw_sprite(buffer, jumpdrive_image, jumpdrive[n]->x-mapxoff, jumpdrive[n]->y-mapyoff);
00532     }
00533   }
00534 }
00535 void loadsounds()
00536 {
00537   int n;
00538     if (install_sound(DIGI_AUTODETECT, MIDI_NONE, "") != 0) 
00539     {
00540         allegro_message("Error initializing sound system");
00541         return;
00542     }
00543     sounds[JUMP] = load_sample("jump.wav");
00544     if (!sounds[JUMP]) 
00545     {
00546         allegro_message("Error reading jump.wav");
00547         return;
00548     }
00549     sounds[HIT] = load_sample("hit.wav");
00550     if (!sounds[HIT]) 
00551     {
00552         allegro_message("Error reading hit.wav");
00553         return;
00554     }
00555     sounds[ITEM] = load_sample("item.wav");
00556     if (!sounds[ITEM]) 
00557     {
00558         allegro_message("Error reading item.wav");
00559         return;
00560     }
00561     sounds[SHOOT] = load_sample("shoot.wav");
00562     if (!sounds[SHOOT]) 
00563     {
00564         allegro_message("Error reading shoot.wav");
00565         return;
00566     }
00567     if (env->audio_enabled == 1)
00568       play_sample(sounds[0], VOLUME, PAN, PITCH, FALSE);
00569   
00570   for (n=0; n < NUM_SOUNDS; n ++)
00571      destroy_sample(sounds[n]);
00572 }
00573 
00574 int collided(int x, int y)
00575 {
00576   BLKSTR *blockdata;
00577   blockdata = MapGetBlock(x/mapblockwidth, y/mapblockheight);
00578   if (blockdata->user1 == 5)
00579     stats->health -= 3;
00580   return blockdata->bl;
00581 }
00582 
00583 void draw_crosshair()
00584 {
00585   // top left
00586   line(buffer, (player->x - 10 - mapxoff), (player->y - mapyoff), (player->x + 10 - mapxoff), (player->y - mapyoff), RED);
00587   line(buffer, (player->x - mapxoff), (player->y - 10 - mapyoff), (player->x - mapxoff), (player->y + 10 - mapyoff), RED);
00588 
00589   // bottom left
00590   line(buffer, (player->x - 10 - mapxoff), (player->y - mapyoff + player->height), (player->x + 10 - mapxoff), (player->y - mapyoff + player->height), RED);
00591   line(buffer, (player->x - mapxoff), (player->y - 10 - mapyoff + player->height), (player->x - mapxoff), (player->y + 10 - mapyoff + player->height), RED);
00592 
00593   // top right
00594   line(buffer, (player->x - 10 - mapxoff + player->width), (player->y - mapyoff), (player->x + 10 - mapxoff + player->width), (player->y - mapyoff), RED);
00595   line(buffer, (player->x - mapxoff + player->width), (player->y - 10 - mapyoff), (player->x - mapxoff + player->width), (player->y + 10 - mapyoff), RED);
00596 
00597   // bottom right
00598   line(buffer, (player->x - 10 - mapxoff + player->width), (player->y - mapyoff + player->height), (player->x + 10 - mapxoff + player->width), (player->y - mapyoff + player->height), RED);
00599   line(buffer, (player->x - mapxoff + player->width), (player->y - 10 - mapyoff + player->height), (player->x - mapxoff + player->width), (player->y + 10 - mapyoff + player->height), RED);
00600   return;
00601 }
00602 
00603 
00604 void restart_level()
00605 {
00606   if (stats->lives > 0)
00607     {
00608       stats->health=25;
00609       env->counter=300;
00610       stats->score=0;
00611       player->x = 80;
00612       player->y = 100;
00613       player->collision=0;
00614       jump = JUMPIT;
00615     }
00616   else
00617     {
00618       allegro_message("Deiner gemütlichkeit is kaput!  Exiting game...\n");
00619       exit(0);
00620     }
00621   return;
00622 }
00623 
00624 void fire()
00625 {
00626   int n;
00627   for (n=0; n<MAX_BULLETS; n++)
00628     {
00629       if (!bullets[n]->alive)
00630         {
00631           if (env->audio_enabled == 1)
00632             play_sample(sounds[SHOOT], VOLUME, PAN, PITCH, FALSE);
00633           bullets[n]->alive++;
00634           bullets[n]->x = player->x;
00635           bullets[n]->y = player->y + player->height/2;
00636           if (player->rightfacing)
00637             bullets[n]->rightfacing=1;
00638           else
00639             bullets[n]->rightfacing=0;
00640           return;
00641         }
00642     }
00643 }
00644 
00645 void updatesprite(SPRITE *spr)
00646 {
00647     if (spr->xcount > spr->xdelay) {
00648       if (spr->rightfacing)
00649         spr->x += spr->xspeed;
00650       else
00651         spr->x -= spr->xspeed;
00652     }
00653     
00654     if (++spr->framecount > spr->framedelay) {
00655       spr->framecount = 0;
00656       if (spr->animdir == -1) {
00657         if (--spr->curframe < 0)
00658           spr->curframe = spr->maxframe;
00659       }
00660       else if (spr->animdir == 1) {
00661         if (++spr->curframe > spr->maxframe)
00662           spr->curframe = 0;
00663       }
00664     }
00665 }
00666 
00667 
00668 void checkinput()
00669 {
00670      
00671  if (key[KEY_1]) {
00672    player->x = 8000;
00673    player->y = 100;
00674   }
00675   if (key[KEY_2]) {
00676     if (battle == 1)
00677       bstats->health = -1;
00678   }
00679   if (key[KEY_S]) {
00680     env->counter = 100000;
00681     remove_timer();
00682   }
00683   if (key[KEY_H]) {
00684     stats->health = -1;
00685   }
00686   if (key[KEY_W]) {
00687     jump = 45;
00688   }
00689   if (key[KEY_LCONTROL]) {
00690     fire();
00691   }
00692   if (key[KEY_RIGHT]) { 
00693     player->rightfacing = 1; 
00694     player->x+=4; 
00695     if (++player->framecount > player->framedelay) {
00696       player->framecount=0;
00697       if (++player->curframe > player->maxframe)
00698         player->curframe=1;
00699     }
00700   }
00701   else if (key[KEY_LEFT]) { 
00702     player->rightfacing = 0; 
00703     player->x-=4; 
00704     if (++player->framecount > player->framedelay) {
00705       player->framecount=0;
00706       if (++player->curframe > player->maxframe)
00707         player->curframe=1;
00708     }
00709   }
00710   else player->curframe=0;
00711   
00712   if (jump==JUMPIT) {
00713     if (!collided(player->x + player->width/2, player->y + player->height+5)) {
00714       jump = 0;
00715       player->collision=0;
00716     }
00717     
00718     if (key[KEY_SPACE]) {
00719       do_jump();
00720       jump = 35;
00721     }
00722   }
00723  
00724   /* this is where we're in the air, either going up or going down */
00725   else
00726     {
00727       if (!(collided(player->x, player->y - jump/5) || collided(player->x + player->width / 2, player->y - jump/5) || collided(player->x + player->width, player->y - jump/5)) ||
00728           collided(player->x, player->y + player->height/2) || collided(player->x + player->width, player->y + player->height/2) || 
00729           collided(player->x, player->height) || collided(player->x + player->width, player->y))
00730         {
00731           player->y -= jump/5;
00732           jump--;
00733         }
00734       else {
00735         jump = 0;
00736         player->collision = 1;
00737       }
00738     }
00739   
00740   if (jump<0)
00741     {
00742       if (collided(player->x + player->width/2, player->y + player->height) || collided(player->x + player->width/2, player->y))
00743         {
00744           player->collision=1;
00745           jump = JUMPIT;
00746           while (collided(player->x + player->width/2,  player->y + player->height))
00747             player->y -= 2;  /* in case we get stuck, push player back up */
00748         }
00749     }
00750   return;
00751 }
00752 
00753 
00754 void update_enemies()
00755 {
00756   int n;
00757   
00758   for (n=0; n<10; n++) {
00759     if (enemies[n]->alive) {
00760            if ((enemies[n]->x - player->x - mapxoff) <= 600)
00761              enemies[n]->xspeed = 4;
00762            updatesprite(enemies[n]);
00763            draw_sprite(buffer, mis_1_images[enemies[n]->curframe],
00764                        enemies[n]->x - mapxoff, enemies[n]->y-mapyoff);
00765     }
00766   }
00767   if (boss->alive) {
00768     draw_sprite(buffer, boss_images[boss->curframe], boss->x-mapxoff, boss->y-mapyoff);
00769   } 
00770 }
00771 
00772 
00773 void updateplayer()
00774 {
00775   int ix, iy, n;
00776   int x1, x2, y1, y2;
00777   
00778   if ((stats->health) < 0 && (battle == 0)) {
00779       allegro_message("You die.  Your corpse is quickly eaten by a grue.");
00780       stats->lives--;
00781       restart_level();
00782   }
00783 
00784   if (player->x >=  8600)
00785     battle = 1;
00786   //check for collided with foreground tiles
00787   if (!player->rightfacing) 
00788     { 
00789       if (collided(player->x, player->y + player->height) || collided(player->x, player->y+5)) {
00790         player->collision=1;
00791         player->x = player->oldpx; 
00792       }
00793     }
00794   else 
00795     { 
00796       if (collided(player->x + player->width, player->y + player->height) || collided(player->x + player->width, player->y+5)) {
00797         player->collision=1;
00798         player->x = player->oldpx; 
00799       }
00800     }   
00801 
00802   //check for item pickup
00803 
00804   for (n=0; n<5; n++) {
00805     if (cd[n]->alive) {
00806       ix = cd[n]->x + cd[n]->width/2 - mapxoff;
00807       iy = cd[n]->y + cd[n]->height/2 - mapyoff;
00808       x1 = player->x - mapxoff;
00809       y1 = player->y - mapyoff;
00810       x2 = x1 + player->width;
00811       y2 = y1 + player->height;
00812       
00813       if (inside(ix, iy, x1, y1, x2, y2)) {
00814         if (env->audio_enabled == 1)
00815           play_sample(sounds[ITEM], VOLUME, PAN, PITCH, FALSE);
00816         cd[n]->alive=0;
00817         stats->score += 50;
00818       }
00819     }
00820     if (jumpdrive[n]->alive) {
00821       ix = jumpdrive[n]->x + jumpdrive[n]->width/2 - mapxoff;
00822       iy = jumpdrive[n]->y + jumpdrive[n]->height/2 - mapyoff;
00823       x1 = player->x - mapxoff;
00824       y1 = player->y - mapyoff;
00825       x2 = x1 + player->width;
00826       y2 = y1 + player->height;
00827       
00828       if (inside(ix, iy, x1, y1, x2, y2)) {
00829         if (env->audio_enabled == 1)
00830           play_sample(sounds[ITEM], VOLUME, PAN, PITCH, FALSE);
00831         jumpdrive[n]->alive=0;
00832         stats->score += 50;
00833       }
00834     }
00835   }
00836 
00837    
00838   //update the map scroll position
00839   mapxoff = player->x + player->width/2 - WIDTH/2 + 10;
00840   mapyoff = player->y + player->height/2 - HEIGHT/2 + 10;
00841   
00842   
00843   //avoid moving beyond the map edge
00844   if (mapxoff < 0) mapxoff = 0;
00845   if (mapxoff > (mapwidth * mapblockwidth - WIDTH))
00846     mapxoff = mapwidth * mapblockwidth - WIDTH;
00847   if (mapyoff < 0) 
00848     mapyoff = 0;
00849   if (mapyoff > (mapheight * mapblockheight - HEIGHT)) 
00850     mapyoff = mapheight * mapblockheight - HEIGHT;
00851   
00852   //draw the background tiles
00853   MapDrawBG(buffer, mapxoff, mapyoff, 0, 0, WIDTH-1, HEIGHT-1);
00854   
00855   //draw foreground tiles
00856   MapDrawFG(buffer, mapxoff, mapyoff, 0, 0, WIDTH-1, HEIGHT-1, 0);
00857   
00858   //draw the player's sprite
00859   if (player->rightfacing) 
00860     draw_sprite(buffer, player_images[player->curframe], 
00861                 (player->x-mapxoff), (player->y-mapyoff+1));
00862   else 
00863     draw_sprite_h_flip(buffer, player_images[player->curframe], 
00864                        (player->x-mapxoff), (player->y-mapyoff));
00865   return;
00866 }
00867 
00868 
00869 void do_jump()
00870 {
00871   if (env->audio_enabled) {  play_sample(sounds[JUMP], VOLUME, PAN, PITCH, FALSE); }
00872   return;
00873 }
00874 
00875 
00876 void end_game()
00877 {
00878   //int is_same_bitmap(preend_image, end_image);
00879 
00880   if ((bstats->health < 0) && (stats->health > 0)) {
00881     allegro_message("Congratulations!  You win!");
00882   }
00883   else if ((stats->health < 0) && (bstats->health > 0)) {
00884     allegro_message("MORE! MORE! MORE!");
00885   }
00886 
00887   main_menu();
00888   return;
00889 }
00890 
00891 void boss_battle()
00892 {
00893   int n;
00894   battle = 1;
00895   remove_timer();
00896 
00897   temp = load_bitmap(boss_bitmap, NULL);
00898   for (n=0; n<8; n++)
00899     boss_images[n] = grabframe(temp,50,64,0,0,8,n);
00900   destroy_bitmap(temp);
00901   
00902   bstats = malloc(sizeof(STATS));
00903   bstats->health=100;
00904   bstats->lives=1;
00905   
00906   while((bstats->health > 0) && (stats->health > 0)) {
00907     player->collision=0;
00908     checkinput();
00909     updateplayer();
00910     updatebullets();
00911     update_enemies();
00912     updateitems();
00913     //draw_crosshair();
00914     updatestats();
00915     updatebossstats();
00916     updatescore();
00917     
00918     vsync();
00919     acquire_screen();
00920     blit(buffer, screen, 0, 0, 0, 0, WIDTH-1, HEIGHT-1);
00921     release_screen();
00922   }
00923   end_game();
00924 }
00925 
00926 void init_win32()
00927 {
00928   set_window_title(title_string);
00929   /* loadsounds(); */
00930   return;
00931 }
00932 void init_linux()
00933 {
00934   allegro_message("GNU/Linux has not yet been tested; some options will be disabled");
00935   env->audio_enabled = FALSE;
00936   set_window_title(title_string);
00937   return;
00938 }
00939 void init_unix()
00940 {
00941   env->audio_enabled = FALSE;
00942   set_window_title(title_string);
00943   return;
00944 }
00945 
00946 
00947 
00948 char *OSinit(int number)
00949 {
00950   switch (number)
00951     {
00952     case OSTYPE_WIN3:    /* fallthrough */
00953     case OSTYPE_WIN95:   /*      |      */
00954     case OSTYPE_WIN98:   /*      |      */
00955     case OSTYPE_WINME:   /*      |      */
00956     case OSTYPE_WINNT:   /*     \|/     */
00957     case OSTYPE_WIN2000: /*      '      */
00958     case OSTYPE_WINXP:
00959       init_win32(); 
00960       break;
00961     case OSTYPE_LINUX:
00962       init_linux();
00963       break;
00964     case OSTYPE_UNIX:
00965       init_unix();
00966       break;
00967     }
00968   return "";
00969 }
00970 END_OF_MAIN();
00971 

Generated on Wed Jul 5 09:05:20 2006 for Super CS Brothers by  doxygen 1.4.4