00001 #include <stdio.h>
00002 #include <allegro.h>
00003 #include "include/supercsbros.h"
00004
00005 void init_win32()
00006 {
00007 set_window_title(title_string);
00008 initializemusic();
00009 return;
00010 }
00011
00012 void init_freebsd()
00013 {
00014 allegro_message("FreeBSD has not yet been tested; some options will be disabled");
00015 env->audio_enabled = FALSE;
00016 set_window_title(title_string);
00017 return;
00018 }
00019
00020 void init_linux()
00021 {
00022 allegro_message("GNU/Linux has not yet been tested; some options will be disabled");
00023 env->audio_enabled = FALSE;
00024 set_window_title(title_string);
00025 return;
00026 }
00027 void init_unix()
00028 {
00029 env->audio_enabled = FALSE;
00030 set_window_title(title_string);
00031 return;
00032 }
00033 void init_unknown()
00034 {
00035 allegro_message("Your unknown platform has not been tested yet!\nDanger Will Robinson!");
00036 return;
00037 }
00038
00039
00040 char *OSinit(int number)
00041 {
00042 switch (number)
00043 {
00044 case OSTYPE_UNKNOWN: return "Unknown or MS-DOS";
00045 case OSTYPE_WIN3:
00046 case OSTYPE_WIN95:
00047 case OSTYPE_WIN98:
00048 case OSTYPE_WINME:
00049 case OSTYPE_WINNT:
00050 case OSTYPE_WIN2000:
00051 case OSTYPE_WINXP:
00052 init_win32();
00053 break;
00054 case OSTYPE_OS2:
00055
00056 case OSTYPE_WARP:
00057 case OSTYPE_DOSEMU:
00058 case OSTYPE_OPENDOS:
00059 init_unknown();
00060 break;
00061 case OSTYPE_LINUX:
00062 init_linux();
00063 break;
00064 case OSTYPE_FREEBSD:
00065 init_freebsd();
00066 break;
00067 case OSTYPE_QNX:
00068
00069 case OSTYPE_UNIX:
00070 init_unix();
00071 break;
00072 case OSTYPE_BEOS:
00073
00074 case OSTYPE_MACOS:
00075 init_unknown();
00076 break;
00077 }
00078 return "";
00079 }
00080
00081
00082 void ticker()
00083 {
00084 ++ticks;
00085 }
00086
00087 int initializemusic()
00088 {
00089 env->audio_enabled = 1;
00090 env->audio_pan = 128;
00091 env->audio_pitch = 1000;
00092 env->audio_volume = 128;
00093
00094 if (install_sound(DIGI_AUTODETECT, MIDI_NONE, "") != 0)
00095 {
00096 allegro_message("Error initializing sound system!\nCall Operation Ivy!");
00097 return;
00098 }
00099
00100 music = load_sample(music_file);
00101 if (!music) {
00102 allegro_message("Can't play background music...\n");
00103 return;
00104 }
00105
00106 jump_sound = load_sample(jump_file);
00107 if (!jump_sound) {
00108 allegro_message("Can't make jump sound...\n");
00109 return;
00110 }
00111
00112 item_sound = load_sample(item_file);
00113 if (!item_sound){
00114 allegro_message("Can't make item pickup sound...\n");
00115 return;
00116 }
00117 return;
00118 }
00119
00120
00121 void loadsprites(void)
00122 {
00123 int n;
00124
00125
00126 temp = load_bitmap("images/progress.bmp", NULL);
00127 progress = grabframe(temp,130,14,0,0,1,0);
00128 bar = grabframe(temp,6,10,130,2,1,0);
00129 destroy_bitmap(temp);
00130
00131
00132 temp = load_bitmap(player_bitmap, NULL);
00133 for (n=0; n<8; n++)
00134 player_images[n] = grabframe(temp,50,64,0,0,8,n);
00135 destroy_bitmap(temp);
00136
00137 player = malloc(sizeof(SPRITE));
00138 player->x = 80;
00139 player->y = 100;
00140 player->curframe=0;
00141 player->framecount=0;
00142 player->framedelay=6;
00143 player->rightfacing=0;
00144 player->maxframe=7;
00145 player->width=player_images[0]->w;
00146 player->height=player_images[0]->h;
00147 player->collision=0;
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175 }
00176
00177
00178 void initialize()
00179 {
00180 stats = malloc(sizeof(STATS));
00181 env = malloc(sizeof(ENVIRONMENT));
00182 allegro_init();
00183 OSinit(os_type);
00184
00185 install_timer();
00186 install_keyboard();
00187 install_mouse();
00188
00189
00190
00191 set_color_depth(32);
00192 set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0);
00193
00194 if (MapLoad(map_file))
00195 {
00196 allegro_message("Error reading the map file: %s", map_file);
00197 exit(1);
00198 }
00199
00200 buffer = create_bitmap (WIDTH, HEIGHT);
00201 clear(buffer);
00202
00203 loadsprites();
00204
00205
00206 stats->health = 25;
00207 stats->score = 0;
00208 stats->lives = 3;
00209 env->diff_mod = 0.42;
00210 counter= 300;
00211 return;
00212 }
00213
00214
00215 BITMAP *grabframe(BITMAP *source,
00216 int width, int height,
00217 int startx, int starty,
00218 int columns, int frame)
00219 {
00220 BITMAP *temp = create_bitmap(width,height);
00221 int x = startx + (frame % columns) * width;
00222 int y = starty + (frame / columns) * height;
00223 blit(source,temp,x,y,0,0,width,height);
00224 return temp;
00225 }
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253 int collided(int x, int y)
00254 {
00255 BLKSTR *blockdata;
00256 blockdata = MapGetBlock(x/mapblockwidth, y/mapblockheight);
00257 return blockdata->bl;
00258 }
00259
00260 void draw_crosshair()
00261 {
00262
00263 line(buffer,
00264 (player->x - 10 - mapxoff), (player->y - mapyoff),
00265 (player->x + 10 - mapxoff), (player->y - mapyoff), RED);
00266 line(buffer,
00267 (player->x - mapxoff), (player->y - 10 - mapyoff),
00268 (player->x - mapxoff), (player->y + 10 - mapyoff), RED);
00269
00270
00271 line(buffer,
00272 (player->x - 10 - mapxoff), (player->y - mapyoff + player->height),
00273 (player->x + 10 - mapxoff), (player->y - mapyoff + player->height), RED);
00274 line(buffer,
00275 (player->x - mapxoff), (player->y - 10 - mapyoff + player->height),
00276 (player->x - mapxoff), (player->y + 10 - mapyoff + player->height), RED);
00277
00278
00279 line(buffer,
00280 (player->x - 10 - mapxoff + player->width), (player->y - mapyoff),
00281 (player->x + 10 - mapxoff + player->width), (player->y - mapyoff), RED);
00282 line(buffer,
00283 (player->x - mapxoff + player->width), (player->y - 10 - mapyoff),
00284 (player->x - mapxoff + player->width), (player->y + 10 - mapyoff), RED);
00285
00286
00287 line(buffer,
00288 (player->x - 10 - mapxoff + player->width), (player->y - mapyoff + player->height),
00289 (player->x + 10 - mapxoff + player->width), (player->y - mapyoff + player->height), RED);
00290 line(buffer,
00291 (player->x - mapxoff + player->width), (player->y - 10 - mapyoff + player->height),
00292 (player->x - mapxoff + player->width), (player->y + 10 - mapyoff + player->height), RED);
00293 return;
00294 }
00295
00296
00297 void get_cd()
00298 {
00299 if (env->audio_enabled) { play_sample(jump_sound, env->audio_volume, env->audio_pan, env->audio_pitch, FALSE); }
00300 stats->score += 35;
00301 return;
00302 }
00303
00304
00305 void get_jumpdrive()
00306 {
00307 if (env->audio_enabled) { play_sample(jump_sound, env->audio_volume, env->audio_pan, env->audio_pitch, FALSE); }
00308 stats->score += 15;
00309 return;
00310 }
00311
00312
00313 void do_jump()
00314 {
00315 if (env->audio_enabled) { play_sample(jump_sound, env->audio_volume, env->audio_pan, env->audio_pitch, FALSE); }
00316 return;
00317 }
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327 void show_menu()
00328 {
00329 int n;
00330 int s = 0;
00331 int t = 0;
00332 BITMAP *cursor;
00333 cursor = load_bitmap("images/cursor.pcx", NULL);
00334
00335 enable_hardware_cursor();
00336 select_mouse_cursor(MOUSE_CURSOR_ARROW);
00337 show_mouse(screen);
00338
00339 BITMAP *bg;
00340
00341 BITMAP *menu_items_unselected[3];
00342 BITMAP *menu_items_selected[3];
00343
00344 bg = load_bitmap(bg_file, NULL);
00345 if (!bg) {
00346 allegro_message("Error loading %s", bg);
00347 return;
00348 }
00349 blit(bg, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
00350
00351 destroy_bitmap(bg);
00352
00353 menu_items_unselected[0] = load_bitmap("menus/start_unselected.pcx", NULL);
00354 menu_items_unselected[1] = load_bitmap("menus/settings_unselected.pcx", NULL);
00355 menu_items_unselected[2] = load_bitmap("menus/quit_unselected.pcx", NULL);
00356
00357 menu_items_selected[0] = load_bitmap("menus/start_selected.pcx", NULL);
00358 menu_items_selected[1] = load_bitmap("menus/settings_selected.pcx", NULL);
00359 menu_items_selected[2] = load_bitmap("menus/quit_selected.pcx", NULL);
00360
00361
00362
00363 for (n=0; n<3; n++) {
00364 blit(menu_items_unselected[n], screen, 0, 0, 300, (200 + n * 64), SCREEN_W-1, SCREEN_H-1);
00365 }
00366
00367 blit(menu_items_selected[s], screen, 0, 0, 300, (200 + s * 64), SCREEN_W-1, SCREEN_H-1);
00368 while (1)
00369 {
00370 if (mouse_b & 1) {
00371 if ( (mouse_x >= 300) && (mouse_x <= 510) && (mouse_y >= 200) && (mouse_y <= 263))
00372 return;
00373 if ( (mouse_x >= 300) && (mouse_x <= 510) && (mouse_y >= 264) && (mouse_y <= 327))
00374 settings_menu();
00375 if ( (mouse_x >= 300) && (mouse_x <= 510) && (mouse_y >= 328) && (mouse_y <= 392))
00376 {
00377 for (n=0; n<3; n++) {
00378 destroy_bitmap(menu_items_unselected[n]);
00379 destroy_bitmap(menu_items_selected[n]);
00380 }
00381 exit(0);
00382 }
00383 }
00384 }
00385 }
00386
00387 void settings_menu()
00388 {
00389 allegro_message("Pull up settings menu here");
00390 return;
00391 }
00392
00393
00394 void restart_level()
00395 {
00396 if (stats->lives > 0)
00397 {
00398 stats->health=25;
00399 counter=300;
00400 stats->score=0;
00401 player->x = 80;
00402 player->y = 100;
00403 player->collision=0;
00404 jump = JUMPIT;
00405 }
00406 else
00407 {
00408 allegro_message("Deiner gemütlichkeit is kaput! Exiting game...\n");
00409 exit(0);
00410 }
00411 return;
00412 }
00413
00414 void updatestats()
00415 {
00416 int n;
00417 draw_sprite(buffer, progress, 14, 15);
00418
00419 for (n = 0; n < stats->health; n++) {
00420 draw_sprite(buffer, bar, 15+n*5, 17);
00421 }
00422 }
00423
00424 void updatescore()
00425 {
00426 textprintf_ex(buffer, font, 15, 5, WHITE, -1,"Score: %d", stats->score);
00427 textprintf_ex(buffer, font, 105, 5, WHITE, -1, "Lives: %d", stats->lives);
00428 textprintf_ex(buffer, font, 210, 5, WHITE, -1, "Jump: %d", jump);
00429 textprintf_ex(buffer, font, 300, 5, WHITE, -1, "Player->x: %d", player->x);
00430 textprintf_ex(buffer, font, 450, 5, WHITE, -1, "Player->y: %d", player->y);
00431 if (jump < 0) {textprintf_ex(buffer, font, 400, 300, WHITE, -1, "FALLING!");}
00432 if (player->collision) {textprintf_ex(buffer, font, 400, 315, WHITE, -1, "Collision!!");}
00433 return;
00434 }
00435
00436 void updatetime()
00437 {
00438 if (counter > 0)
00439 textprintf_ex(screen, font, (WIDTH-100), 5, WHITE, -1, "Time: %d", counter);
00440 else
00441 {
00442 allegro_message("You've run out of time. We should do someting now...like restart the level!\n");
00443 restart_level();
00444 }
00445 return;
00446 }
00447
00448
00449 void checkinput()
00450 {
00451
00452 if (key[KEY_1])
00453 {
00454 if (env->audio_enabled)
00455 play_sample(music, env->audio_volume, env->audio_pan, env->audio_pitch, TRUE);
00456 }
00457 if (key[KEY_2])
00458 {
00459 if (env->audio_enabled)
00460 stop_sample(music);
00461 }
00462 if (key[KEY_H])
00463 {
00464 stats->health -= (int)(env->diff_mod * 5);
00465 if (stats->health < 0)
00466 {
00467 allegro_message("You die. Your corpse is quickly eaten by a grue.");
00468 stats->lives--;
00469 restart_level();
00470 }
00471 }
00472
00473 if (key[KEY_C])
00474 {
00475 get_cd();
00476 }
00477
00478 if (key[KEY_J])
00479 {
00480 get_jumpdrive();
00481 }
00482
00483 if (key[KEY_RIGHT])
00484 {
00485 player->rightfacing = 1;
00486 player->x+=4;
00487 if (++player->framecount > player->framedelay)
00488 {
00489 player->framecount=0;
00490 if (++player->curframe > player->maxframe)
00491 player->curframe=1;
00492 }
00493 }
00494 else if (key[KEY_LEFT])
00495 {
00496 player->rightfacing = 0;
00497 player->x-=4;
00498 if (++player->framecount > player->framedelay)
00499 {
00500 player->framecount=0;
00501 if (++player->curframe > player->maxframe)
00502 player->curframe=1;
00503 }
00504 }
00505 else player->curframe=0;
00506
00507
00508
00509
00510
00511 if (jump==JUMPIT)
00512 {
00513 if (!collided(player->x + player->width/2,
00514 player->y + player->height+5))
00515 {
00516 jump = 0;
00517 player->collision=0;
00518 }
00519
00520 if (key[KEY_SPACE]) {
00521 do_jump();
00522 jump = 35;
00523 }
00524 }
00525
00526 else
00527 {
00528 player->y -= jump/5;
00529 jump--;
00530 }
00531
00532 if (jump<0)
00533 {
00534
00535 if (collided(player->x + player->width/2,
00536 player->y + player->height) ||
00537 collided(player->x + player->width/2, player->y))
00538 {
00539 player->collision=1;
00540 jump = JUMPIT;
00541 while (collided(player->x + player->width/2,
00542 player->y + player->height))
00543 player->y -= 2;
00544 }
00545 }
00546 return;
00547 }
00548
00549
00550 void updateplayer()
00551 {
00552
00553
00554 if (!player->rightfacing)
00555 {
00556 if (collided(player->x, player->y + player->height) || collided(player->x, player->y+5)) {
00557 player->collision=1;
00558 player->x = player->oldpx;
00559 }
00560 }
00561 else
00562 {
00563 if (collided(player->x + player->width, player->y + player->height) || collided(player->x + player->width, player->y+5)) {
00564 player->collision=1;
00565 player->x = player->oldpx;
00566 }
00567 }
00568
00569 mapxoff = player->x + player->width/2 - WIDTH/2 + 10;
00570 mapyoff = player->y + player->height/2 - HEIGHT/2 + 10;
00571
00572
00573
00574 if (mapxoff < 0) mapxoff = 0;
00575 if (mapxoff > (mapwidth * mapblockwidth - WIDTH))
00576 mapxoff = mapwidth * mapblockwidth - WIDTH;
00577 if (mapyoff < 0)
00578 mapyoff = 0;
00579 if (mapyoff > (mapheight * mapblockheight - HEIGHT))
00580 mapyoff = mapheight * mapblockheight - HEIGHT;
00581
00582
00583 MapDrawBG(buffer, mapxoff, mapyoff, 0, 0, WIDTH-1, HEIGHT-1);
00584
00585
00586 MapDrawFG(buffer, mapxoff, mapyoff, 0, 0, WIDTH-1, HEIGHT-1, 0);
00587
00588
00589 if (player->rightfacing)
00590 draw_sprite(buffer, player_images[player->curframe],
00591 (player->x-mapxoff), (player->y-mapyoff+1));
00592 else
00593 draw_sprite_h_flip(buffer, player_images[player->curframe],
00594 (player->x-mapxoff), (player->y-mapyoff));
00595 return;
00596 }
00597
00598
00599
00600 int main (void)
00601 {
00602 int nticks;
00603 initialize();
00604 show_menu();
00605
00606 LOCK_VARIABLE(ticks);
00607 LOCK_FUNCTION(ticker);
00608 install_int_ex(ticker, MSEC_TO_TIMER( (int)(1000 * env->diff_mod)));
00609
00610 while (!key[KEY_ESC])
00611 {
00612 player->oldpy = player->y;
00613 player->oldpx = player->x;
00614
00615 nticks = ticks;
00616 ticks = 0;
00617
00618 for (; nticks >0; nticks--) {
00619 --counter;
00620 }
00621 player->collision=0;
00622 checkinput();
00623 updateplayer();
00624 draw_crosshair();
00625 updatestats();
00626 updatescore();
00627 updatetime();
00628
00629 vsync();
00630 acquire_screen();
00631 blit(buffer, screen, 0, 0, 0, 0, WIDTH-1, HEIGHT-1);
00632 release_screen();
00633 }
00634
00635 MapFreeMem ();
00636 allegro_exit();
00637
00638 return 0;
00639 }
00640 END_OF_MAIN();
00641