Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Pixel question

This thread is locked; no one can reply to it. rss feed Print
Pixel question
picnic
Member #14,632
October 2012
avatar

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

Any help or advice welcome :D

Chris Katko
Member #1,881
January 2002
avatar

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.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

LennyLen
Member #5,313
December 2004
avatar

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
Major Reynaldo
May 2007
avatar

Chris Katko
Member #1,881
January 2002
avatar

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.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

picnic
Member #14,632
October 2012
avatar

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 :P

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Mark Oates
Member #1,146
March 2001
avatar

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.

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

picnic
Member #14,632
October 2012
avatar

Thanks Mark, that's clear.

Go to: