Best way to draw a pixel

I've been programming in high level interpreted languages for a long time now, and am trying to get into C++. The problem I'm running into now is that I can't figure out how to draw a pixel without downloading this library, or subscribing to this methodology, or shifting that paradigm. Here are examples of methods which have been suggested to me, and I don't want to do:

1. Download png/gif/jpg/etc library, use function draw_image(file), and use an image file that's just 1px of the color you want.
2. Use OpenGL to draw a rectangle 1x1 px large, in the color you want.
3. Download library X that does it for you!

My problem with each of those is that I don't ever learn a damn thing. I used each of those methods in the past (back when I was in school) and got so bored with C++ that I went back to using the higher level languages, because they gave me lower level access to what I wanted to do. (technically, they didn't really give me lower level access, they just made it seem like it). Here's an example of the kind of thing I'm looking for:

Create a new instance of class X which is a canvas (or use function Y to get the drawing area already instantiated as part of a window). The Z attribute points to the memory block where the "pixel data" of this canvas is stored. Use Z[Y(-1)*width + X] to access the (X,Y) pixel in this canvas, and use this format blah_blah to set that pixel equal to blah color.

I understand Windows XP isn't going to give me direct access to the display's memory locations, and that's okay. If I can just create a "canvas" (or whatever is the simplest equivalent object) and associate it with my Window, and then set it up to paint itself whenever my WinProc receives the appropriate command, I'll be good. Bonus points if I can make two of these and swap them out quickly for double buffering.

... But all that is pretty complicated. What it basically comes down to is trying to draw one pixel on the screen, instead of loading up a state machine to create four vertices and then draw a rectangle 1px large. I know it's a big topic, thanks in advance for any help pointing me in the right direction.
Last edited on
The thing is, you normally don't draw a single pixel at the highest level (e.g. in main()). Depending on the kind of application, you draw lines, rectangles, surfaces, polygons, and textures. The first three, which are the ones I know, draw individual pixels by directly accessing the surface they're going to draw to. Basically, like what you describe.
Yes, you do have to go through hoops so draw a single pixel, but it doesn't really matter because in practice you'll only do it once or twice at the lowest level (e.g. in drawRectangle()), but never at the highest level. A finished program could look like this using basically any library:
1
2
3
4
5
6
7
8
9
10
11
int main(){
	Screen theScreen(1024,768,24);        //initialize a 1024x768 screen with a bitdepth of 24
	Image anImage("img.png");             //load an image
	anImage.draw(theScreen,100,100);      //draw the image with the top left corner at 100,100
	Rectangle aRectangle={99,99,0,255,0}; //prepare a green square with a side of 99
	aRectangle.draw(theScreen,1,1);       //draw the rectangle at 1,1
	theScreen.putPixel(0,0,255,0,255);    //draw a magenta pixel at 0,0
	Image anotherImage(theScreen);        //copy the screen to a new image
	anotherImage.write("img.png");        //write the image
	return 0;
}
> The thing is, you normally don't draw a single pixel

Absolutely correct! Most of the time, most programmers don't need to draw a single pixel. In the applications I want to program, though, I do. I'm really not just asking because I don't understand that it's normally bad practice. I understand that I'll take a huge performance hit (not to mention increased development time) if I'm single pixels where I /should/ be using rectangles or bitmaps. At the same time, though, there's the performance hit you take when you're using rectangles and higher level constructs do draw single pixels.

Any node in the right direction would be appreciated.
Well, this is the beginners section, so I just wanted to make sure.

If you're aiming for performance then you'll have to implement putPixel() on line 7 by directly accessing the parameter's pixel data, assuming the library doesn't have it already.
Although unless the library is very badly written, drawing a 1x1 rectangle should only take slightly longer than drawing a pixel.

What are you going to do with single pixels, anyway?
On Windows the simple way is with SetPixelV() (bad : slow) and the right way is with DIBs.
(The fastest one is with DX...)
Stick with the libraries. The days of writing straight to video memory are over...
I don't think I've ever seen a drawing library that didn't let you set a single pixel value. If it lets you draw lines, squares, circles, etc, it also let's you set a single pixel.
Topic archived. No new replies allowed.