加载位图图像向一定大小 [英] loading a bitmap image to a certain size

查看:344
本文介绍了加载位图图像向一定大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着用快板位图加载到一定的大小。

Im trying to load a bitmap to a certain size using allegro.

al_crate_bitmap(X,Y) - 创建位图以一定的尺寸
al_load_bitmap(文件名) - 加载我需要的形象,但其原来的尺寸

al_crate_bitmap(x,y) - creates a bitmap to a certain size al_load_bitmap(filename) - loads the image i need, but to its original size.

我需要一个位图装载到一​​个大小我设置。任何想法?

I need to load a bitmap to a size I set. Any ideas?

谢谢,
桑尼

推荐答案

关键是 al_draw_scaled_bitmap()。就这样,你可以在任何新的大小块复制任何来源的位图到目标位图。所以,你可能实际上并不需要创建一个新的位图。您可以随时使用该功能在不同的大小来绘制位图。快板5硬件加速,所以这些类型的操作往往是免费。

The key is al_draw_scaled_bitmap(). With that, you can blit any source bitmap onto the target bitmap at any new size. So you may not actually need to create a new bitmap. You can always use that function to draw the bitmap at a different size. Allegro 5 is hardware accelerated, so these types of operations are often "free."

要直接回答这个问题:

ALLEGRO_BITMAP *load_bitmap_at_size(const char *filename, int w, int h)
{
  ALLEGRO_BITMAP *resized_bmp, *loaded_bmp, *prev_target;

  // 1. create a temporary bitmap of size we want
  resized_bmp = al_create_bitmap(w, h);
  if (!resized_bmp) return NULL;

  // 2. load the bitmap at the original size
  loaded_bmp = al_load_bitmap(filename);
  if (!loaded_bmp)
  {
     al_destroy_bitmap(resized_bmp);
     return NULL;
  }

  // 3. set the target bitmap to the resized bmp
  prev_target = al_get_target_bitmap();
  al_set_target_bitmap(resized_bmp);

  // 4. copy the loaded bitmap to the resized bmp
  al_draw_scaled_bitmap(loaded_bmp,
     0, 0,                                // source origin
     al_get_bitmap_width(loaded_bmp),     // source width
     al_get_bitmap_height(loaded_bmp),    // source height
     0, 0,                                // target origin
     w, h,                                // target dimensions
     0                                    // flags
  );

  // 5. restore the previous target and clean up
  al_set_target_bitmap(prev_target);
  al_destroy_loaded_bmp(loaded_bmp);

  return resized_bmp;      
}

我的评论在code的基本步骤。请记住,快板5有一个隐含的目标,通常是在显示器的背面缓冲区。然而,正如上述code所示,你可以,如果你需要做的位图位图操作目标等位图。

I've commented the basic steps in the code. Keep in mind that Allegro 5 has an implicit target that is usually the back buffer of the display. However, as the above code illustrates, you can target other bitmaps if you need to do bitmap to bitmap operations.

另一种方法,移动目标位图的功能外:

Alternate way, moving the target bitmap outside the function:

void load_bitmap_onto_target(const char *filename)
{
  ALLEGRO_BITMAP *loaded_bmp;
  const int w = al_get_bitmap_width(al_get_target_bitmap());   
  const int h = al_get_bitmap_height(al_get_target_bitmap());

  // 1. load the bitmap at the original size
  loaded_bmp = al_load_bitmap(filename);
  if (!loaded_bmp) return;

  // 2. copy the loaded bitmap to the resized bmp
  al_draw_scaled_bitmap(loaded_bmp,
     0, 0,                                // source origin
     al_get_bitmap_width(loaded_bmp),     // source width
     al_get_bitmap_height(loaded_bmp),    // source height
     0, 0,                                // target origin
     w, h,                                // target dimensions
     0                                    // flags
  );

  // 3. cleanup
  al_destroy_bitmap(loaded_bmp);
}

ALLEGRO_BITMAP *my_bmp = al_create_bitmap(1000,1000);
al_set_target_bitmap(my_bmp);
load_bitmap_onto_target("test.png");
// perhaps restore the target bitmap to the back buffer, or continue
// to modify the my_bmp with more drawing operations.

这篇关于加载位图图像向一定大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆