Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Trying to get pixel color in the lower right corner of display.

This thread is locked; no one can reply to it. rss feed Print
Trying to get pixel color in the lower right corner of display.
nshade
Member #4,372
February 2004

I have a panning picture that is being scaled and "pulled" across the display. The problem is I'm making this game for different resolutions/aspect ratios, I need to to tell the computer to stop scrolling when it gets to the edge of the bitmap.

I can't count how long it's been panning because for widescreen it pans less and for 4:3 it pans more.

The picture is 1240x360 an scrolling from right to left

at 1024x768 it moves the bitmap -1456 pixels (the 1240x360 bitmap is scaled to 2645x768)
at 1280x720 it moves the bitmap -1200 pixels (the 1240x360 bitmap is scaled to 2480x720)
to 1920x1024 it moved the bitmap -1800 pixels (The 1240x360 bitmap is scaled to 3527x1024)

I got these numbers via trial and error and I don't want to make a table for all possible resolutions and I'm not sure how to calculate this manually

As a sneaky way I thought I would solve this problem was put a special color pixel in the lower right corner my bitmap and then test the lower right corner of the display when it comes up. The problem is that al_get_pixel() crashes when the bitmap being read is the display.

Because I can't math well, I don't know how to calculate the offset for any given resolution. (I'm sure there is a way, but I don't know how to begin)

here's a bit of psudocode

#SelectExpand
1scale = al_get_display_height(display) / 360; 2title_bg = al_load_bitmap("res\\title_bg.png"); 3int scroll; 4int amount = -1200; //amount to scroll for 1280x720 5 6for (scroll = 0;scroll > amount; scroll = scroll - 4) 7{ 8al_draw_scaled_bitmap(title_bg, 0, 0, 1240, 360, scroll, 0, (1240 * scale), al_get_display_height(display), 0); 9}

Is there a better way?

========== SUPER EDIT ===============
I just wanted to give some closure on this so I'm not this guy --> https://xkcd.com/979/

I Mathed my way out of it. Here is how you do it if to do the same thing. I also made all the sizes generic so you can scroll any sized bitmap (longer than the display) to any resolution

#SelectExpand
1int scale; 2int scroll; 3int scaled_x; 4int scaled_y; 5int amount_to_scroll; 6 7title_bg = al_load_bitmap("res\\title_bg.png"); 8scale = al_get_display_height(display) / al_get_bitmap_height(title_bg); 9scaled_x = al_get_bitmap_width(title_bg) * scale; 10scaled_y = al_get_bitmap_height(title_bg) * scale; 11amount_to_scroll = -(scaled_x - al_get_display_width(display)); 12 13for (scroll = 0;scroll > amount_to_scrol; scroll = scroll - 4) 14{ 15al_draw_scaled_bitmap(title_bg, 0, 0, al_get_bitmap_width(title_bg), al_get_bitmap_height(title_bg), scroll, 0, scaled_x, al_get_display_height(display), 0); 16}

beoran
Member #12,636
March 2011

The mathematical solution is indeed the best one. Math is far more perfomant than getting a pixel these days, an it makes more sense. Glad to hear you figured it out yourself.

Go to: