⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demo.c

📁 用djgpp里allegro库编写的完整演示程序
💻 C
📖 第 1 页 / 共 3 页
字号:

   scroll_count = 1;
   install_int(scroll_counter, 5);

   while ((c=scroll_count) < 160)
      stretch_blit(data[TITLE_BMP].dat, screen, 0, 0, 320, 128,
		   SCREEN_W/2-c, SCREEN_H/2-c*64/160-32, c*2, c*128/160);

   remove_int(scroll_counter);
   blit(data[TITLE_BMP].dat, screen, 0, 0, SCREEN_W/2-160, SCREEN_H/2-96, 320, 128);

   clear_keybuf();

   scroll_count = 0;

   if (use_retrace_proc)
      retrace_proc = scroll_counter;
   else
      install_int(scroll_counter, 6);

   do {
      /* animate the starfield */
      for (c=0; c<MAX_STARS; c++) {
	 if (star[c].z <= itofix(1)) {
	    x = itofix(random()&0xff);
	    y = itofix(((random()&3)+1)*SCREEN_W);
	    star[c].x = fmul(fcos(x), y);
	    star[c].y = fmul(fsin(x), y);
	    star[c].z = itofix((random() & 0x1f) + 0x20);
	 }

	 x = fdiv(star[c].x, star[c].z);
	 y = fdiv(star[c].y, star[c].z);
	 ix = (int)(x>>16) + SCREEN_W/2;
	 iy = (int)(y>>16) + SCREEN_H/2;
	 putpixel(screen, star[c].ox, star[c].oy, 0);
	 if ((ix >= 0) && (ix < SCREEN_W) && (iy >= 0) && (iy <= SCREEN_H)) {
	    if (getpixel(screen, ix, iy) == 0) {
	       if (c < star_count) {
		  c2 = 7-(int)(star[c].z>>18);
		  putpixel(screen, ix, iy, MID(0, c2, 7));
	       }
	       star[c].ox = ix;
	       star[c].oy = iy;
	    }
	    star[c].z -= 4096;
	 }
	 else
	    star[c].z = 0;
      }

      if (star_count < MAX_STARS) {
	 if (star_count_count++ >= 32) {
	    star_count_count = 0;
	    star_count++;
	 }
      }

      /* wait a bit if we need to */
      do {
      } while (scroll_count <= 0);

      /* and move the text scroller */
      c = use_retrace_proc ? scroll_count*2 : scroll_count;
      scroll_count = 0;
      blit(text_bmp, text_bmp, c, 0, 0, 0, SCREEN_W, 24);
      rectfill(text_bmp, SCREEN_W-c, 0, SCREEN_W, 24, 0);

      while (c > 0) {
	 text_pix += c;
	 textout(text_bmp, data[TITLE_FONT].dat, buf, SCREEN_W-text_pix, 0, -1);
	 if (text_pix >= text_width) {
	    c = text_pix - text_width;
	    text_char++;
	    if (text_char >= data[TITLE_TEXT].size)
	       text_char = 0;
	    buf[0] = ((char *)data[TITLE_TEXT].dat)[text_char];
	    text_pix = 0;
	    text_width = text_length(data[TITLE_FONT].dat, buf);
	 }
	 else
	    c = 0;
      }
      blit(text_bmp, screen, 0, 0, 0, SCREEN_H-24, SCREEN_W, 24);

      poll_joystick();

   } while ((!keypressed()) && (!joy[0].button[0].b) && (!joy[0].button[1].b));

   if (use_retrace_proc)
      retrace_proc = NULL;
   else
      remove_int(scroll_counter);

   fade_out(5);

   while (keypressed())
      if ((readkey() & 0xff) == 27)
	 return FALSE;

   destroy_bitmap(text_bmp);

   return TRUE;
}



void set_gui_colors()
{
   static RGB black = { 0,  0,  0,  0 };
   static RGB grey  = { 48, 48, 48, 0 };
   static RGB white = { 63, 63, 63, 0 };

   set_color(0,   &black);
   set_color(16,  &black);
   set_color(1,   &grey); 
   set_color(255, &white); 

   gui_fg_color = 0;
   gui_bg_color = 1;
}



char *anim_list_getter(int index, int *list_size)
{
   static char *s[] =
   {
      "Double buffered",
      "Page flipping",
      "Synced flips",
      "Triple buffered",
      "Dirty rectangles"
   };

   if (index < 0) {
      *list_size = 5;
      return NULL;
   }

   return s[index];
}



extern DIALOG anim_type_dlg[];


int anim_list_proc(int msg, DIALOG *d, int c)
{
   int sel, ret;

   sel = d->d1;

   ret = d_list_proc(msg, d, c);

   if (sel != d->d1)
      ret |= D_REDRAW;

   return ret;
}



int anim_desc_proc(int msg, DIALOG *d, int c)
{
   static char *double_buffer_desc[] = 
   {
      "Draws onto a memory bitmap,",
      "and then uses a brute-force",
      "blit to copy the entire",
      "image across to the screen.",
      NULL
   };

   static char *page_flip_desc[] = 
   {
      "Uses two pages of video",
      "memory, and flips back and",
      "forth between them. It will",
      "only work if there is enough",
      "video memory to set up dual",
      "pages.",
      NULL
   };

   static char *retrace_flip_desc[] = 
   {
      "This is basically the same",
      "as page flipping, but it uses",
      "the vertical retrace interrupt",
      "simulator instead of retrace",
      "polling. Only works in mode-X,",
      "and not under win95.",
      NULL
   };

   static char *triple_buffer_desc[] = 
   {
      "Uses three pages of video",
      "memory, to avoid wasting time",
      "waiting for retraces. Only",
      "works in mode-X, or with a",
      "VBE 3.0 driver and suitable",
      "SVGA hardware.",
      NULL
   };

   static char *dirty_rectangle_desc[] = 
   {
      "This is similar to double",
      "buffering, but stores a list",
      "of which parts of the screen",
      "have changed, to minimise the",
      "amount of drawing that needs",
      "to be done.",
      NULL
   };

   static char **descs[] =
   {
      double_buffer_desc,
      page_flip_desc,
      retrace_flip_desc,
      triple_buffer_desc,
      dirty_rectangle_desc
   };

   char **p;
   int y;

   if (msg == MSG_DRAW) {
      rectfill(screen, d->x, d->y, d->x+d->w, d->y+d->h, d->bg);
      text_mode(d->bg);

      p = descs[anim_type_dlg[2].d1];
      y = d->y;

      while (*p) {
	 textout(screen, font, *p, d->x, y, d->fg);
	 y += 8;
	 p++;
      } 
   }

   return D_O_K;
}



DIALOG anim_type_dlg[] =
{
   /* (dialog proc)     (x)   (y)   (w)   (h)   (fg)  (bg)  (key) (flags)     (d1)  (d2)  (dp)                 (dp2) (dp3) */
   { d_shadow_box_proc, 0,    0,    280,  150,  0,    1,    0,    0,          0,    0,    NULL,                NULL, NULL  },
   { d_ctext_proc,      140,  8,    1,    1,    0,    1,    0,    0,          0,    0,    "Animation Method",  NULL, NULL  },
   { anim_list_proc,    16,   28,   152,  43,   0,    1,    0,    D_EXIT,     0,    0,    anim_list_getter,    NULL, NULL  },
   { anim_desc_proc,    16,   90,   248,  48,   0,    1,    0,    0,          0,    0,    0,                   NULL, NULL  },
   { d_button_proc,     184,  28,   80,   16,   0,    1,    13,   D_EXIT,     0,    0,    "OK",                NULL, NULL  },
   { d_button_proc,     184,  50,   80,   16,   0,    1,    27,   D_EXIT,     0,    0,    "Cancel",            NULL, NULL  },
   { NULL,              0,    0,    0,    0,    0,    0,    0,    0,          0,    0,    NULL,                NULL, NULL  }
};



int pick_animation_type(int *type, int card, int w, int h)
{
   int ret;

   centre_dialog(anim_type_dlg);

   if (((card == GFX_MODEX) || ((w < 640) && ((w != 320) || (h != 200)))) && 
       (w*h*2 <= 256*1024)) {
      if (windows_version != 0)
	 anim_type_dlg[2].d1 = 1;
      else if (w*h*3 <= 256*1024)
	 anim_type_dlg[2].d1 = 3;
      else
	 anim_type_dlg[2].d1 = 2;
   }
   else if (card == GFX_VBEAF)
      anim_type_dlg[2].d1 = 1;
   else
      anim_type_dlg[2].d1 = 4;

   clear(screen);

   ret = do_dialog(anim_type_dlg, 2);

   *type = anim_type_dlg[2].d1 + 1;

   return (ret == 5) ? -1 : ret;
}



int main(int argc, char *argv[])
{
   int c, w, h, vh;
   char buf[256];

   for (c=1; c<argc; c++) {
      if (stricmp(argv[c], "-cheat") == 0)
	 cheat = TRUE;

      if (stricmp(argv[c], "-turbo") == 0)
	 turbo = TRUE;
   }

   allegro_init();
   install_keyboard();
   install_mouse();
   install_timer();

   if (install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, argv[0]) != 0) {
      printf("\nError initialising sound\n%s\n\n", allegro_error);
      exit(1);
   }

   if (install_joystick(JOY_TYPE_AUTODETECT) != 0) {
      printf("\nError initialising joystick\n%s\n\n", allegro_error);
      exit(1);
   }

   fade_out(4);
   set_gfx_mode(GFX_VGA, 320, 200, 0, 0);

   replace_filename(buf, argv[0], "demo.dat", sizeof(buf));
   data = load_datafile(buf);
   if (!data) {
      allegro_exit();
      printf("Error loading %s\n\n", buf);
      exit(1);
   }

   if ((midi_driver->id == MIDI_AWE32) || (midi_driver->id == MIDI_DIGMID))
      play_midi(data[INTRO_MUSIC].dat, FALSE);
   else
      play_sample(data[INTRO_SPL].dat, 255, 128, 1000, FALSE);

   play_memory_fli(data[INTRO_ANIM].dat, screen, FALSE, NULL);

   c = retrace_count;

   generate_explosions();

   do {
   } while (retrace_count-c < 120);

   fade_out(1);

   clear(screen);
   set_gui_colors();

   if (!gfx_mode_select(&c, &w, &h)) {
      allegro_exit();
      exit(1);
   }

   if (pick_animation_type(&animation_type, c, w, h) < 0) {
      allegro_exit();
      exit(1);
   }

   if (animation_type == PAGE_FLIP) {
      vh = h * 2;
   }
   else if (animation_type == RETRACE_FLIP) {
      vh = h*2;

      if (c == GFX_AUTODETECT)
	 c = GFX_MODEX;

      if (c != GFX_MODEX) {
	 allegro_exit();
	 printf("Error: retrace simulation is only possible in mode-X\n\n");
	 exit(1);
      }
   }
   else  if (animation_type == TRIPLE_BUFFER) {
      vh = h*3;
   }
   else
      vh = 0;

   if (set_gfx_mode(c, w, h, 0, vh) != 0) {
      allegro_exit();
      printf("Error setting graphics mode\n%s\n\n", (w < 640) ? "Try an animation type that requires fewer pages of video memory" : allegro_error);
      exit(1);
   }

   if ((animation_type == PAGE_FLIP) || (animation_type == RETRACE_FLIP) ||
       (animation_type == TRIPLE_BUFFER)) {
      page1 = create_sub_bitmap(screen, 0, 0, SCREEN_W, SCREEN_H);
      page2 = create_sub_bitmap(screen, 0, SCREEN_H, SCREEN_W, SCREEN_H);

      if (animation_type == TRIPLE_BUFFER) {
	 if (!(gfx_capabilities & GFX_CAN_TRIPLE_BUFFER)) {
	    strcpy(buf, gfx_driver->name);
	    allegro_exit();
	    printf("The %s driver does not support triple buffering\n\n", buf);
	    exit(1);
	 }

	 if (gfx_driver->id == GFX_MODEX) {
	    if (windows_version != 0) {
	       set_gui_colors();
	       alert("Warning: mode-X triple buffering", "uses the timer retrace simulator,", "which only works well from DOS", "Ok", NULL, 13, 0);
	    }

	    timer_simulate_retrace(TRUE);
	    use_retrace_proc = TRUE;
	 }

	 page3 = create_sub_bitmap(screen, 0, SCREEN_H*2, SCREEN_W, SCREEN_H);
      }
      else if (gfx_driver->id == GFX_MODEX) {
	 use_retrace_proc = TRUE;

	 if (animation_type == RETRACE_FLIP)
	    timer_simulate_retrace(TRUE);
      }
   }

   LOCK_VARIABLE(game_time);
   LOCK_FUNCTION(game_timer);

   LOCK_VARIABLE(scroll_count);
   LOCK_FUNCTION(scroll_counter);

   LOCK_VARIABLE(score);
   LOCK_VARIABLE(frame_count);
   LOCK_VARIABLE(fps);
   LOCK_FUNCTION(fps_proc);

   text_mode(0);

   s = create_bitmap(SCREEN_W, SCREEN_H);

   stop_sample(data[INTRO_SPL].dat);

   while (title_screen())
      play_game(); 

   allegro_exit();

#ifdef DJGPP
   cputs(data[END_TEXT].dat);
#else
   printf("%s", data[END_TEXT].dat);
#endif

   destroy_bitmap(s);

   if ((animation_type == PAGE_FLIP) || (animation_type == RETRACE_FLIP) ||
       (animation_type == TRIPLE_BUFFER)) {
      destroy_bitmap(page1);
      destroy_bitmap(page2);

      if (animation_type == TRIPLE_BUFFER)
	 destroy_bitmap(page3);
   }

   for (c=0; c<EXPLODE_FRAMES; c++)
      destroy_rle_sprite(explosion[c]);

   unload_datafile(data);
   return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -