Skip to main content
GameDev.net gamedev.net
🔒 Locked

Basic GDI help

Started by Super Llama Oct 7, 2008 at 9:39 PM 9 replies 1.6k views
Original Post
Super Llama
Super Llama
This probably sounds like an incredibly noobish post... I'm using C++ and GDI (wingdi.h). I'm trying to draw bitmaps onto the window, but I can't figure out how to load and display them, let alone do ones with alpha. I understand how to make a custom bitmap structure and draw pixels to a CompatibleHDC, then blit them, but I don't understand how an hDC can have transparent pixels in it... there's no function for this. Could someone explain basic alpha blitting to me, as well as how to load bitmaps from a file? Thanks :)
It's a sofa! It's a camel! No! It's Super Llama!
gan
gan
Quote:

as well as how to load bitmaps from a file?
Thanks :)

LoadBitmap
Quote:


Could someone explain basic alpha blitting to me

alpha blitting? something basis will be Alpha Blending : Alpha Blending a Bitmap ? Else you might need to google it .

[Edited by - gan on October 8, 2008 12:08:20 AM]
clashie
clashie
Take a look at LoadImage()

This will explain transparency better than I can in this post.
http://www.winprog.org/tutorial/transparency.html

Also, take a look at this for your alpha blending needs.
http://www.gamedev.net/reference/articles/article817.asp



DIB's are where the fun begins.
Aardvajk
Aardvajk
Something else you might like to consider is using GDI+ with native C++.

It's incredibly easy to integrate with normal GDI, is supported without any additional installs on XP or higher and supports alphablending from the word go, as well as lots of other funky stuff. You're using basically the same interface as you do in C# for 2D drawing.

There's plenty of information in the Platform SDK docs.
george135
george135
Quote:
Original post by clashie
This will explain transparency better than I can in this post.
http://www.winprog.org/tutorial/transparency.html


No, it's completely outdated.
Transparency is now done in 2 lines of code.

Super Llama
Super Llama
Okay, I wasn't aware of any way to use GDI+ with Native C++. What header files are required? I REALLY don't want to use any .NET things, I got addicted to Namespaces and junk when programming VB.NET and I'm trying to clean it up with some good old Native C++ with basic WinAPI.

I'm using wingdi.h, is there a different H for GDI+? Because I've worked with GDI+ before and found out how easy it is, as long as its not .NET or anything.

And the LoadBitmap() function only loads from the EXE file, which I can't seem to embed bitmaps into anyway =P

I think GDI+ supports PNG, right? Oh, and I did google my problem but I couldn't find anything relevant, so I asked here.

Please respond, my new questions are:
1) How to reference GDI+? Header filename?
2) Does GDI+ support PNG? I love PNG :D
3) If GDI+ is .NET, is there a good native alternative?

thanks for your time [smile]
It's a sofa! It's a camel! No! It's Super Llama!
Aardvajk
Aardvajk
Quote:
Original post by Super Llama
1) How to reference GDI+? Header filename?


See the link I posted above to the CodeProject article. Ignore all the references to MFC in that article - MFC is not required to make this work.

The header itself is but there are a couple of trivial lines you need at program start and termination and you need to link against gdiplus.lib as well.

Quote:
Original post by Super Llama
2) Does GDI+ support PNG? I love PNG :D


Yes, the image loading methods support PNG.

Quote:
Original post by Super Llama
3) If GDI+ is .NET, is there a good native alternative?


I don't believe GDI+ to be a part of .NET. However, it is irrelevant as GDI+ can be used in an entirely unmanaged, native C++ application. You just include a header and link to a DLL like any other Win32 API section.

This is an example of how pleasant it is to use compared to Win32 GDI functions. Note this is an example and I don't propose you should actually load images inside a WM_PAINT handler [smile].

// still need to BeginPaint() and EndPaint() in response to WM_PAINT, but once you have the DC...using namespace Gdiplus;void OnPaint(HDC Dc){	Graphics Gr(Dc);	Bitmap B(L"C:\\Graphics\\block.png");		Gr.DrawImage(&B,0,0,24,24); // if the image contains alpha, it just works :)}


No resource leaks, no storing and restoring old objects into devices, no more CreateCompatibleDC(), no more checking for GDI leaks with TaskManager...

Sorry if I sound like a bit of a fanboy, but GDI+ in a native C++ application is just such bliss to work with after years of grappling with GDI. [smile]

HTH

Quote:
Original post by george135
No, it's completely outdated.
Transparency is now done in 2 lines of code.


To be fair to Win32 API, there is also a method AlphaBlend that supports alphablending without GDI+ which I guess is what george135 is referring to above, so GDI+ is not a better option for the alphablending, just for all the other millions of reasons.
Super Llama
Super Llama
Yay! Thank you, that's exactly what I was looking for [smile]

EDIT: Uh oh, I'm a noob again and the line didn't draw... I must be calling BeginPaint and EndPaint wrong...

EDIT: Nevermind, I just sepecificed an invalid color for the pen :)

It works great :D

Are the BeginPaint() and EndPaint() functions necessary? In what context?

[Edited by - Super Llama on October 8, 2008 9:12:07 PM]
It's a sofa! It's a camel! No! It's Super Llama!
clashie
clashie
Quote:
Original post by EasilyConfused
To be fair to Win32 API, there is also a method AlphaBlend that supports alphablending without GDI+ which I guess is what george135 is referring to above, so GDI+ is not a better option for the alphablending, just for all the other millions of reasons.


I bet it's SLOWWWWW though. :)

I've just been using a DIB for all my stuff, and it's been nice. It's a little confusing initially, but it's pretty simple. I do all my drawing by setting the bits, and it seems to be very fast. Transparency is simple since you just don't draw the pixel if you come across a certain color, and alpha blending is easy as well.
Aardvajk
Aardvajk
Quote:
Original post by Super Llama
Are the BeginPaint() and EndPaint() functions necessary? In what context?


Yes, you have to use these in response to a WM_PAINT to get the DC and fill out a PAINTSTRUCT rather than GetDC() which you might use elsewhere.

I don't have sample code at the moment as am at work, but I can post an example tonight.
Super Llama
Super Llama
Ah, so the PAINTSTRUCT is an OUT variable. I've been using GetDC and then putting the DC INTO the Paintstruct and then sending it to the function XD

Thanks for clearing that up :)
It's a sofa! It's a camel! No! It's Super Llama!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.