RPGDXThe center of Indie-RPG gaming
Not logged in. [log in] [register]
 
 
Post new topic Reply to topic Goto page 1, 2  Next 
View previous topic - View next topic  
Author Message
Ninkazu
Demon Hunter


Joined: 08 Aug 2002
Posts: 945
Location: Location:

PostPosted: Thu Apr 22, 2004 9:54 pm    Post subject: allegro alpha blending [quote]

Ok, this is really pissing me off. I wanted to use Allegro for my project, and having an alpha blending capability is essential. The thing is, I can't get my routine to work, and I fear that even when it does work, it'll be dreadfully slow. Any Allegro-elites out here who can help me?

buffer is an extern BITMAP* not shown.

Sprite.h
Code:

class CSprite
{
private:
   BITMAP *bmp;
   BITMAP *back;
   COLOR_MAP trans_table;
   PALETTE pal;
   int width, height;
public:
   CSprite(const char* file, int w, int h);
   ~CSprite();
   void draw(int x, int y, int frame);
   void draw_rot(int x, int y, int angle, int frame);
   void draw_stregth(int x, int y, int cx, int cy, int frame);
   void draw_lit(int x, int y, int alpha, int frame);
};


Sprite.cpp
Code:

CSprite::CSprite(const char* file, int w, int h):width(w),height(h)
{
    char path_temp[256];
    replace_filename(path_temp, "tiles\\", file, sizeof(path_temp));
    bmp = load_bitmap(path_temp, pal);
    if (!bmp)
    {
        set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
        allegro_message("Error loading ");
      allegro_message(file);
      allegro_message("\n");
    }
    back = create_bitmap(width, height);
}

CSprite::~CSprite()
{
   destroy_bitmap(bmp);
   destroy_bitmap(back);
   bmp = NULL;
   back = NULL;
}

void CSprite::draw_lit(int x, int y, int alpha, int frame)
{
   blit(bmp, back, frame*width, 0, 0, 0, width, height);
   create_trans_table(&trans_table, pal, alpha, alpha, alpha, NULL);
   color_map = &trans_table;
   set_trans_blender(0, 0, 0, alpha);
   blit(back, buffer, 0, 0, x, y, width, height);
}
Back to top  
Bjorn
Demon Hunter


Joined: 29 May 2002
Posts: 1425
Location: Germany

PostPosted: Thu Apr 22, 2004 10:14 pm    Post subject: [quote]

A few things to note:

- Palettes and trans_tables are only for 8bpp mode, are you sure you are using that one? Is your palette adequate for displaying the colors caused by transparency?

- You don't want to create a new trans table each time you draw the sprite. You'd normally do this once after initializing your palette.

- set_trans_blender is for true color modes, as opposed to 8bpp paletted mode. Use either the color_map variable with the trans table created using the desired alpha value, or set_trans_blender.

- For transparency drawing mode to be enabled, you need to do use drawing_mode with DRAW_MODE_TRANS. And you'll probably want to put it back on DRAW_MODE_SOLID afterwards. This is needed for both 8bpp and truecolor modes.

- blit is not affected by the drawing mode, for transparent blitting, after setting up the drawing mode and its parameters, use draw_trans_sprite.

- Take a look at the examples exalpha, exblend, exlights, extrans and exxfade for more info on how it works (list from Allegro 4.1.13).

- If you're really concerned with speed and it's too slow, consider looking into the fblend add-on library.

That's it for now. I hope you'll figure it out now. :-)
Back to top  
Ninkazu
Demon Hunter


Joined: 08 Aug 2002
Posts: 945
Location: Location:

PostPosted: Thu Apr 22, 2004 10:29 pm    Post subject: [quote]

I was working from those examples, an I just couldn't understand it.

Fuck it, I'm using OpenGL.
Back to top  
SJ_Zero
Pretty, Pretty Fairy Princess


Joined: 25 Apr 2003
Posts: 13

PostPosted: Fri Apr 23, 2004 12:32 am    Post subject: [quote]

I love it when people give up on a simple C library to use a horrendously complicated one. Do you think that you'll be able to somehow escape having to set up alpha blending by using OpenGL?
Back to top  
LeoDraco
Demon Hunter


Joined: 24 Jun 2003
Posts: 584
Location: Riverside, South Cali

PostPosted: Fri Apr 23, 2004 12:45 am    Post subject: [quote]

SJ_Zero wrote:
I love it when people give up on a simple C library to use a horrendously complicated one. Do you think that you'll be able to somehow escape having to set up alpha blending by using OpenGL?


Dude, most of that is handled easily in GL with a few function calls. Well, at least for trivial 2d blending. What GL are you using, in which this is something terribly difficult?
_________________
"...LeoDraco is a pompus git..." -- Mandrake
Back to top  
Ninkazu
Demon Hunter


Joined: 08 Aug 2002
Posts: 945
Location: Location:

PostPosted: Fri Apr 23, 2004 12:46 am    Post subject: [quote]

glEnable(GL_ALPHA_SRC);

or something as simple as that. Seriously, it's not so much a complicated library to use, so much as a different thinking perspective. I have to figure out how to texture a triangle list with tiles instead of blitting squares. It's something I didn't want to have to face, but it looks like I have to.
Back to top  
Adam
Mage


Joined: 30 Dec 2002
Posts: 416
Location: Australia

PostPosted: Fri Apr 23, 2004 1:05 am    Post subject: [quote]

"have to"
_________________
https://numbatlogic.com
Back to top  
Fling-master
I wanna be a ballerina!


Joined: 12 Jan 2003
Posts: 27
Location: Sunderland Ontario, Canada

PostPosted: Fri Apr 23, 2004 2:04 am    Post subject: [quote]

Ninkazu wrote:
I was working from those examples, an I just couldn't understand it.

Fuck it, I'm using OpenGL.


Fool.

Code:

// 128 is of course the alpha value... so the sprite will be half faded out
set_trans_blender(0, 0, 0, 128);

// Turn translucent drawing on
drawing_mode(DRAW_MODE_TRANS, NULL, 0, 0);

// Draw sprite translucently using above settings for the blender
draw_trans_sprite(screen, sprite, x, y);

// Turn off translucent drawing
drawing_mode(DRAW_MODE_SOLID, NULL, 0, 0);


I would like to thank you Ninkazu. If it weren't for you starting this topic I would have left alpha blending in Allegro alone for a long time. When I was converting syn9's 3d engine to C I tried to get alpha blending to work with the text boxes and well... I obviously didn't put much effort into it as it was really quite easy to get it to work. I just didn't realize that drawing_mode() had to be called heh. Thanks to Bjørn for pointing that out. :)
_________________
RPG-Dev.net
Back to top  
SJ_Zero
Pretty, Pretty Fairy Princess


Joined: 25 Apr 2003
Posts: 13

PostPosted: Fri Apr 23, 2004 5:08 am    Post subject: [quote]

Thanks for missing the point entirely, ninkazu. :P

hmmmm....can't get a trivial function working in like, the easiest to use lib out there.....


I KNOW! Let's switch to a more difficult API! That'll make things easier!

Isn't that a bit like switching to ASM because QB is too tough?
Back to top  
Ninkazu
Demon Hunter


Joined: 08 Aug 2002
Posts: 945
Location: Location:

PostPosted: Fri Apr 23, 2004 11:11 am    Post subject: [quote]

I didn't post this thread to be ridiculed. Please stop it.

Thanks Fling. I'll try that out as soon as I get home.
Back to top  
Happy
JonA's American snack pack


Joined: 03 Aug 2002
Posts: 200

PostPosted: Fri Apr 23, 2004 3:12 pm    Post subject: [quote]

SJ_Zero wrote:
I love it when people give up on a simple C library to use a horrendously complicated one. Do you think that you'll be able to somehow escape having to set up alpha blending by using OpenGL?


SJ_Zero wrote:
Thanks for missing the point entirely, ninkazu. :P

hmmmm....can't get a trivial function working in like, the easiest to use lib out there.....


I KNOW! Let's switch to a more difficult API! That'll make things easier!

Isn't that a bit like switching to ASM because QB is too tough?


If you're jesting, I'd suggest you tone it down a bit.
Back to top  
Bjorn
Demon Hunter


Joined: 29 May 2002
Posts: 1425
Location: Germany

PostPosted: Fri Apr 23, 2004 5:12 pm    Post subject: [quote]

Fling-master wrote:
I just didn't realize that drawing_mode() had to be called heh. Thanks to Bjørn for pointing that out. :)

Ah, at least I helped someone. :-)

Ninkazu, the two main things not making transparency work for you are the use of blit instead of draw_trans_sprite and not setting up the drawing mode. I won't be arguing about a switch to OpenGL though. It will certainly not be simpler, though it will be a bit more consistent and at least it'll use the video card.

Transparency in 3D is still a tricky thing though, because there is the problem of what to put in the Z-buffer. One solution is to put nothing in the Z-buffer and draw stuff from back to front, which is fine until you realise a polygon A can be neither in front nor behind a polygon B. This is not an issue in 2D though, this paragraph was just a little sidetrip I stumbled on when trying OpenGL for the first time and wanting to draw a model transparently. :-)
Back to top  
Ninkazu
Demon Hunter


Joined: 08 Aug 2002
Posts: 945
Location: Location:

PostPosted: Fri Apr 23, 2004 10:50 pm    Post subject: [quote]

Ok, it works, and with two 32*32 tiles fading up and down on top of each other, it runs at 600 fps.

Fullscreen (320*240*16) 32*32 tiles fading up and down, around 80 to 90 fps.

Fullscreen two different 32*32 tiles fading up and down on top of each other, around 50 fps.

Pentium III 996 MHz...

I doubt I'll have fullscreen alpha blending in the future, but I wonder how this would run on other machines...
Back to top  
BigManJones
Scholar


Joined: 22 Mar 2003
Posts: 196

PostPosted: Sat Apr 24, 2004 2:00 am    Post subject: [quote]

Quote:
Fullscreen two different 32*32 tiles fading up and down on top of each other, around 50 fps.


LOL, I can beat that with my software blending routine in java; this demo has 300 alpha blended sprites:

Requires jre 1.4.2

EDIT; with 300 sprites it runs at about 25 fps on my 866 p3, but 1 sprite will go at 100+.

EDIT2: oops I'm a dumbass, I thought you were talking about 1 32x32 tile at 50 fps, not full screen. I can't beat that in software ;P
Back to top  
janus
Mage


Joined: 29 Jun 2002
Posts: 464
Location: Issaquah, WA

PostPosted: Sat Apr 24, 2004 9:19 am    Post subject: [quote]

I find it amusing that basically everyone seems to consider it a bad idea to switch from an arcane, confusing, slow API with minimal support for hardware (Allegro) to an extendable, high-performance, well-documented, extremely portable API designed for hardware (OpenGL). I guess if it's good enough for Carmack and SGI, it's still not good enough for you guys? Yeah, yeah, you want to draw 2D graphics. If OpenGL works for Ninkazu, why is he a 'fool' for using a better library?
Back to top  
Post new topic Reply to topic Page 1 of 2 All times are GMT
Goto page 1, 2  Next 



Display posts from previous:   
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum