What's new

DIB Sections - More of my inferiority

BGNG

New member
For whatever reason, I cannot get DIB Sections to work in C++ no matter what I try to do. So... help! What I want to do is the simplest thing possible, it's just that I don't know how to do it and all Google ever gives me is VB tutorials and... not much else.

I want to use a DIB Section in my program which defines a 24-bit RGB image with any desired width and height which I can directly modify using a char pointer. I want to be able to access each pixel with X and Y index and modify the color values from there. I also want to be able to give any desired device context to the program and have it be able to transfer the image to it, such that it can be done with BitBlt.

So basically, I want to do something like the following:

Code:
// Create the DIB Section
char* DIBPixel;
HDC PicMan = MakeDIBSection(Width, Height, DIBPixel);

// Make the pixel (X, Y) magenta
int PixelOffset = (Y - 1) * Width + (X - 1) * 3;
DIBPixel[PixelOffset + 0] = 255; // Blue value
DIBPixel[PixelOffset + 1] = 0;   // Green value
DIBPixel[PixelOffset + 2] = 255; // Red value

// Transfer the image to another DC, in this case a window
BitBlt(wndDC, 0, 0, Width, Height, PicMan, 0, 0, SRCCOPY);

// Clean up the resources used to make the DIB Section
UnmakeDIBSection(PicMan, DIBPixel);
If someone can actually make a wrapper that has code EXACTLY like that, it would be great. Otherwise, whatever help that can be provided will be appreciated.
 
OP
BGNG

BGNG

New member
Actually, that does look to be right about what I want. I have no clue why Google likes you best, but thanks for the link. I'll check it out later.
 
OP
BGNG

BGNG

New member
Yup. Works like a charm. Turns out the reason I couldn't get it to work before was my own stupidity (passing a value by reference instead of by value), but I like this new method better. Thanks for the heads-up, Zenogais.

As expected, DIB Sections utterly blow the GDI API to pieces, so I'm happy.
 
OP
BGNG

BGNG

New member
Great mountains of horse dookie... Visual Basic is FAR slower than I ever thought it was. Powerful for making programs by-line, it uses a large number of cycles to do simple little things.

In one of my testings of this DIB Section business, I made a particle engine that spews randomly-colored pixels up from the middle of the screen like a fountain. I was using the DIB Section stuff in Visual Basic using a DLL I made in C++. I originally put the If statements for restricting pixel access (where the value is less than zero or greater than the width/height) in the VB program, and got about 3000 particles going simultaneously before it was losing frames. Then I stuck the EXACT SAME IF STATEMENTS in the DLL itself in C++, and I was getting well over 10000 working butter-smooth without any frame loss at all.

Methinks my usage of Visual Basic will be kicked down to GUIs and web programming from now on.
 

HyperHacker

Raving Lunatic
Ugh, DIB sections. >_< That scanline crap confuses the hell out of me.

And I'm surprised you got such good speed in VB. You should have seen the first versions of my Gameboy emulator (just enough to load a ROM and execute most of the instructions). In VB it got ~75,000 instructions per second, in C it got ~11,000,000. (A real Gameboy gets ~4,000,000.) The major bottleneck seems to be the inability to choose when you want to process window messages. Doing it every cycle like I was forced to do in VB only got me around 100,000 cycles.
 

ector

Emulator Developer
HyperHacker, on a real gameboy most of the instructions take several cycles, I doubt you'll be getting much more than 1 million instructions per second on a real GB, so your emu is faster than you think (although still pretty slow ;P)
 
OP
BGNG

BGNG

New member
Well, HyperHacker, this IS a thread on DIB Sections. I highly recommend them for simple pixel stuff like emulators. What is it about them that gives you troubles?
 

Top