C++ Allegro 位图子类

C++ Allegro Bitmapping Child Classes

本文关键字:子类 位图 Allegro C++      更新时间:2023-10-16

我有两个子类调用同一个父函数。

一个工作正常,但它的第二个调用无法通过al_convert_mask_to_alpha行。

我又一次被迷惑了。

有谁知道为什么会发生这种情况?

下面的函数是它们都调用的函数。上面都有两者的声明。

Monster* the_monster = new Monster("chard", 10, 10, "assets/monstertrans.bmp");
Hero* the_hero = new Hero(1, "Player", 20, 20, "assets/hero.bmp");
the_monster->Display();
the_hero->Display();

void Creature::Display(void)
{
    //creating the bitmap
    al_init_image_addon(); //allegro image addon
    //FORGETTING THIS LINE WAS A SILLY IDEA!!!!
    creature_bit = al_load_bitmap(m_filename.c_str()); 
    al_convert_mask_to_alpha(creature_bit, al_map_rgb(255,0,255));
    al_draw_bitmap(creature_bit, m_xpos, m_ypos, 0);
    if (!creature_bit)
    {
        cout << "creature creation failed!" << endl;
        cout << "Any key to exit" << endl;
        _getch();
    }
}

你的代码组织得不是很好。

  • 初始化附加组件应该只在程序启动时发生一次
  • 加载位图应作为对象构造函数的一部分进行(或紧随其后,仅一次)
  • 绘制位图应该每帧发生一次(与更新生物的 x/y 数据的逻辑分开)

这些都属于不同的方法。

如果creature_bitnull,那么您应该阅读本文,因为它包含您需要了解的有关如何正确加载位图的所有信息。