I need some help setting up and modifying a 32-bit bitmap in Windows[solved]

Started by
4 comments, last by Amr0 14 years, 1 month ago
I have absolutely no experience with bitmaps under Windows. The last time I did anything with bitmaps, I was using 8-bit bitmaps on Mac OS 7. Things are just a tiny bit different. I've looked through MSDN and have done plenty of Google searches, but most of what I am finding is strictly 8-bit code examples. I'm making a software rasterizer. Currently, I'm cheating and setting pixels via an orthographic projection and GL_POINTS. Obviously not optimal. Laughable, really. How do I set up a 32-bit bitmap and set individual pixels? I found some code that sort of works, using CreateDIBSection, but for some reason the bitmap is always displayed in blue. I have a choice of black, white and shades of blue. Here is the set up code I'm using:

	ZeroMemory(&RGB32BitsBITMAPINFO,sizeof(BITMAPINFO));
	RGB32BitsBITMAPINFO.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	RGB32BitsBITMAPINFO.bmiHeader.biWidth = 800;
	RGB32BitsBITMAPINFO.bmiHeader.biHeight = -800;
	RGB32BitsBITMAPINFO.bmiHeader.biPlanes = 1;
	RGB32BitsBITMAPINFO.bmiHeader.biBitCount = 32;
	RGB32BitsBITMAPINFO.bmiHeader.biCompression = BI_RGB;

	offscreenDC = CreateCompatibleDC(NULL);
	offscreen = CreateDIBSection(offscreenDC,(BITMAPINFO *)&RGB32BitsBITMAPINFO,DIB_RGB_COLORS,(void **)&ptPixels,NULL,0);
	if(!offscreen)
		return(false);
	SelectObject(offscreenDC,offscreen);


To set pixels, I'm doing: ptPixels[offset] = (long)pixel_color; Where: offset = ((y * height) + x) * 4); No matter the value of pixel_color, I get black, white or shades of blue. What am I doing wrong? Does anyone have a link to a tutorial or some code I could see? [Edited by - maspeir on March 3, 2010 12:40:57 PM]

No, I am not a professional programmer. I'm just a hobbyist having fun...

Advertisement
Here is a neat little class that you can use to "render" to a windows bitmap. It comes with a sample application that uses it to continually generate some image and display it to a window. The class also supports saving the image to file. If you have any questions about it, feel free to ask.
http://my.opera.com/adelamro/blog/gdi-memory-bitmap Sorry I'm linking to the blog post since hot linking is not allowed on my.opera.
Thank you. I'll take a look at that, but I solved it on my own. I was writing this code at about 1 AM. Tired minds don't write very good code.

Anyway, this works:

buffer = &ptPixels[(y * 800) + x];...snip...offset = ((tv * 256) + tu) * 4; // Texture accesstr = (unsigned char)tex->imageData[offset];tg = (unsigned char)tex->imageData[offset + 1];tb = (unsigned char)tex->imageData[offset + 2];*buffer = (tr << 16) | (tg << 8) | tb;...snip...


[edit]

Thank you again for that link! Your code will provide me with good reference material.

No, I am not a professional programmer. I'm just a hobbyist having fun...

Quote:but I solved it on my own

Surprisingly, I find that.... disappointing >_<'
I wonder if it's just me or it's a global thing..

[EDIT]Ah, I feel much better now :D
Quote:Original post by Amr0
Quote:but I solved it on my own

Surprisingly, I find that.... disappointing >_<'


Don't be! Your code will provide me with quite a bit of direction. I didn't want someone else to solve this for me, but I couldn't figure it out last night. As is typical with me, I'll make a post asking for help, and then while waiting for a reply, solve the problem myself. For some reason, merely asking the question gives me the answer.

No, I am not a professional programmer. I'm just a hobbyist having fun...

Don't worry about it. I'm just being... umm.. bored and lonely I guess.

This topic is closed to new replies.

Advertisement