Pixel question
picnic

Hello

I'm having fun with Allegro and 'D' (thanks SiegeLord!) but have a question about pixel plotting.

Dumping a 2D array to the screen pixel-by-pixel works as expected but only if the window is square, eg 640 x 640 rather than say 800x600.

Having read the wiki about primitive pixel plotting I imagine this is something to do with drawing being related to the pixel centres being offset 0.5 'pixel units' (sorry I'm struggling to put this into words clearly!), although it could be that integers are being supplied to the function (although the pattern of missing pixels in the output doesn't seem to indicate so).

I can fix the problem by adding 0.5 to the co-ords supplied to al_draw_pixel() so it will render correctly regardless of window proportions - see below - but I'd like to know if this fix will work in all cases (proportions/resolution/hardware) or whether there's a better way to ensure that when I need pixel 100, 100 plotted that is what happens.

```  foreach(y; 0..MapSize)
{
foreach(x; 0..MapSize)
{
......blah blah
al_draw_pixel(x+0.5, y+0.5, c);
```

Speed isn't a major concern as the plotting only happens during map generation.

This is on Linux/Allegro 5/DMD with a Radeon HD6400

Chris Katko

If a pixel center is at 0.5 (0.5, 1.5, 2.5, ...), shouldn't you subtract 0.5 from all coordinates? Otherwise, you'll be off-by-one and missing the first row/column of the screen.

Someone correct me if I'm wrong, I don't have Allegro 5 up at the moment.

LennyLen

If a pixel center is at 0.5 (0.5, 1.5, 2.5, ...), shouldn't you subtract 0.5 from all coordinates? Otherwise, you'll be off-by-one and missing the first row/column of the screen.

Nope, you add 0.5. Under the traditional approach, the top-left pixel is (0, 0), but with the new co-ordinate system, it's (0.5, 0.5).

Edgar Reynaldo

In A5 you want to center on .5 and draw outward from there using the thickness parameter. See https://www.allegro.cc/manual/5/primitives.html for details.

Chris Katko
LennyLen said:

Nope, you add 0.5. Under the traditional approach, the top-left pixel is (0, 0), but with the new co-ordinate system, it's (0.5, 0.5).

Yeah, you're right. I had a brainfart because I was thinking 1,1 was the first pixel, being moved up-and-to-the-left.

picnic

Thanks for the replies but I don't understand why the pixels aren't properly positioned when the window is anything other than square.

I read that wiki page which is why I suspected adding 0.5 might fix things BUT that page makes no mention of al_draw_pixel(), only the other primitive drawing routines (line, ellipse etc).

Just to recap my original question, will adding 0.5 to each co-ordinate passed to al_draw_pixel() fix things whatever the resolution/window proportions/display hardware?

Oh 'ang on, https://www.allegro.cc/manual/5/al_draw_pixel mentions al_put_pixel() - I didn't know about that routine, maybe it does what I need.

Testing...

EDIT: Yeah that works regardless of window proportions but it is horribly slow - no matter I like looking at a black screen during map generation

Edgar Reynaldo

The pixels are centered on 0.5,0.5, so yes.

Mark Oates

You should use al_put_pixel(), which is in pixel coordinates of a bitmap target.

If you're using al_draw_pixel() then you aren't drawing pixel, but rather a 1x1 square, which could be affected by transforms, rotations, scaling, projection, etc. With al_draw_pixel(), you're working with screen coordinates, rather that bitmap's pixel coordinates.

picnic

Thanks Mark, that's clear.