• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
  • entries
    22
  • comments
    30
  • views
    42481

Blitting with per-pixel alpha in Win32

Sign in to follow this  
Followers 0
jwezorek

1838 views

I don't know how long the Win32 API has included an AlphaBlend() function. I mean, it came in whenever Msimg32.lib did but I'm not sure when that was, probably Windows XP era, I guess.

It's always been a pain in the ass to use; easy to use global alpha but per-pixel is a chore. You see a lot of people asking how to use this call at StackOverflow and other sites but basically never see a comprehensive reply. Generally the replies are variants of "It's easy. you just need to pre-multiply the alpha", which is true but unhelpful for two reasons: (1) doing so is a pain in the ass i.e. show me some code, buddy, and more importantly (2) in order to burn the alpha values into the RGB you need to actually have image data that contains an alpha channel but Win32 only natively supports loading BMP's which generally don't.

So on (2), for completeness, I should say I think that it is possible to get Photoshop to spit out a BMP file with alpha information. I haven't tried it but the advanced options when saving a BMP have an option for the format "a8 r8 g8 b8". I always see it grayed out but am guessing that it's possible to do this somehow. Also I think that you can load PNG's using GDI+; I know next to nothing about GDI+ but if that's what you use I'm not sure the solution I propose below is worth it just to get out of having to write the pre-multiply function yourself.

However, the above aside, if you want alpha-blended images in your application, you want to use PNG files ,and if you are writing to Win32 you need to use a 3rd-party library. The two 3rd party graphics libraries that people commonly use in Windows applications for things like loading PNG's and JPEG's are DevIL and FreeImage. I have no experience with DevIL and, frankly, it looks orphaned to me. What I suggest for blitting with semi-transparency in Win32 is using FreeImage, which seems tailor-made for doing this.
So below is an implementation on top of FreeImage demonstrating

  • Loading PNG's (and other formats) as FreeImage data structures from Win32 resources.
  • Converting from FreeImage to HBITMAPs with alpha burned in.
  • Blitting the HBITMAPs with per-pixel alpha.

    Here's the code I use for loading a PNG from a resource to FreeImage's data structures. FimgUtil::MemPtr and FimgUtil::Ptr are defined asboost::shared_ptrboost::shared_ptr
    That is, I'm using smart pointers with custom deleters to call the appropiate FreeImage clean-up code on destruction. If this isn't your style the following code can easily be modified to use raw pointers.

    [source]


    namespace {
    bool GetResourceData(const char* name, BYTE*& ptr, unsigned long& size) {
    HRSRC hrsrc = FindResource(NULL, name, RT_RCDATA);
    if (hrsrc == NULL) {
    return false;
    }
    HGLOBAL handle = LoadResource(NULL, hrsrc);
    if (handle == NULL) {
    return false;
    }
    ptr = static_cast(LockResource(handle));
    size = SizeofResource(NULL, hrsrc);
    return true;
    }
    }

    FimgUtil::MemPtr FimgUtil::GetMemoryPtr(FIMEMORY* fimem) {
    return FimgUtil::MemPtr(fimem, FreeImage_CloseMemory);
    }

    FimgUtil::Ptr FimgUtil::GetBitmapPtr(FIBITMAP* fibmp) {
    return FimgUtil::Ptr(fibmp, FreeImage_Unload);
    }

    FimgUtil::Ptr FimgUtil::LoadFiBitmapFromResource(const char* rsrc_name, FREE_IMAGE_FORMAT format) {
    BYTE* data;
    unsigned long size = 0;
    if (! GetResourceData(rsrc_name, data, size)) {
    return FimgUtil::Ptr ();
    }
    FimgUtil::MemPtr buff = GetMemoryPtr(FreeImage_OpenMemory(data, size));
    if (buff.get() == 0) {
    return FimgUtil::Ptr ();
    }
    if (format == FIF_UNKNOWN) {
    format = FreeImage_GetFileTypeFromMemory(buff.get(), 0);
    if (format == FIF_UNKNOWN) {
    return FimgUtil::Ptr ();
    }
    }
    return GetBitmapPtr(
    FreeImage_LoadFromMemory(format, buff.get(), 0)
    );
    }
    [/source]

    To convert from an FIBITMAP* to a an HBITMAP and rolled together with the above:
    [source lang="cpp"]

    HBITMAP FimgUtil::FiBitmapToWin32Bitmap(const FimgUtil::Ptr & src_ptr, bool premultiply_alpha) {
    if (premultiply_alpha) {
    FreeImage_PreMultiplyWithAlpha( src_ptr.get() );
    }
    HDC hdc_scr = GetDC(NULL);
    FIBITMAP* src = src_ptr.get();
    HBITMAP hbm = CreateDIBitmap( hdc_scr, FreeImage_GetInfoHeader(src),
    CBM_INIT, FreeImage_GetBits(src),
    FreeImage_GetInfo(src),
    DIB_RGB_COLORS);
    ReleaseDC(NULL, hdc_scr);
    return hbm;
    }

    HBITMAP FimgUtil::LoadPngResource( const char* rsrc_name, bool premultiply_alpha) {
    FimgUtil::Ptr fibmp = LoadFiBitmapFromResource( rsrc_name, FIF_PNG );
    return FiBitmapToWin32Bitmap( fibmp, premultiply_alpha);
    }
    [/source]
    and a wrapper for blitting:

    [source]

    void FimgUtil::BlitWithAlpha( HDC dst, int dst_x, int dst_y, int wd, int hgt, HDC src, int src_x, int src_y, float alpha ) {
    BLENDFUNCTION bf;

    ZeroMemory( &bf, sizeof(BLENDFUNCTION) );
    bf.BlendOp = AC_SRC_OVER;
    bf.BlendFlags = 0;
    bf.AlphaFormat = AC_SRC_ALPHA;
    bf.SourceConstantAlpha = static_cast( 255 * alpha );

    AlphaBlend( dst, dst_x, dst_y, wd, hgt, src, src_x, src_y, wd, hgt, bf);
    }
    [/source]

    Source for my FreeImage utility functions is here.
    Source

1
Sign in to follow this  
Followers 0


2 Comments


[quote]Also I think that you can load PNG's using GDI+[/quote]
Yeah it does. It's quite simple too. This is the error-checking-less bones of initialising, loading a BMP, TIFF, PNG, GIF, ICO or (W|E)MF from a resource, and natively drawing it on the screen.

[code]
namespace Gp = Gdiplus;
ULONG_PTR cookie = 0;
Gp::GdiplusStartupInput inp;
Gp::GdiplusStartup(&cookie, &inp, NULL);
HDC hdcDesk = GetDC(NULL);
{
Gp::Bitmap image(hModule, resourceName);
Gp::Graphics grap(hdcDesk);
grap.DrawImage(&bm, 0, 0);
}
ReleaseDC(NULL, hdcDesk);
Gp::GdiplusShutdown(cookie);
[/code]

After you remove the init and shutdown, it's about 5 lines for the important work. It can also provide you with a suitably formatted HBITMAP of the image data, which has per-pixel alpha values which can then be passed to AlphaBlend() as desired.

For C only development, there's currently the IShellImageData ([url="http://msdn.microsoft.com/en-us/library/bb761207.aspx"]http://msdn.microsoft.com/en-us/library/bb761207.aspx[/url]) COM object which is a wrapper around the basic GDI+ operations.

Oh and AlphaBlend's been around since the first version of Win98 :-)
0

Share this comment


Link to comment

Good old Alphablend()... brings back memories of screwing around with Win32 using Delphi 3.. haha. Good times!

0

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now