Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Over mouse function

This thread is locked; no one can reply to it. rss feed Print
Over mouse function
00Andre
Member #16,399
June 2016
avatar

Hey

(I'm sorry foy my english)

I have small problem, with function.
I made a function that checks whether the mouse cursor is in the field Bitmap.
eg. I want to function returns true, if mouse cursor is on alpha pixel.

In function check position cursor, if cursor is in the field Bitmap.
it then get pixel with Bitmap (before lock region bitmap and later unlock)

Function is play in ALLEGRO_EVENT_MOUSE_AXES

if I use functions this is delay big.
(without this function project is fast)

RPG Hacker
Member #12,492
January 2011
avatar

I'm not sure if I got exactly what you problem is, but I assume it's that lock bitmap and unlock bitmap slow down your application?

When you use lock bitmap and unlock bitmap on a display bitmap, it's bound to be slow, because the bitmap first has to be read from the GPU into system memory and then wrote back to the GPU. Try creating the bitmap as a system memory bitmap. That way you can always directly access the pixels and no data has to be copied. If you also need to render the bitmap, then maybe you should make two versions of the same bitmap. One in system memory and one in display memory. You could then use the system memory bitmap for collision checking and the display bitmap for rendering.

jmasterx
Member #11,410
October 2009

Mousemove is called A LOT. And locking / Unlocking can be costly if you do it a lot.

Here are a few suggestions:
-Create a bitfield. https://en.wikipedia.org/wiki/Bit_field Only lock your bitmap once at the start, and calculate which pixels are alpha and which ones are not and put it in the bitfield. Then check the mouse position against the bitfield to see if it is a transparent or opaque area.

If that is too complex to understand, an easier version would be to allocate a [w][h] 2D array of char to do it.

The other option is to build simple polygon approximations of your bitmaps and check if the mouse point is inside the simple polygon.

00Andre
Member #16,399
June 2016
avatar

Before creating a bitmap. I set the flag and display options

#SelectExpand
2al_set_new_bitmap_flags(ALLEGRO_CONVERT_BITMAP | ALLEGRO_VIDEO_BITMAP | 3 ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR); 4al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_ANY_WITH_ALPHA); 5al_set_new_display_option(ALLEGRO_VSYNC, 1, ALLEGRO_REQUIRE)

I removed the lock and unlock bitmaps and are smaller delay.

#SelectExpand
1if(mouse.x > width && mouse.x < x+width && mouse.y > height && mouse.y < y+height) 2{ 3 ALLEGRO_COLOR pxg = al_get_pixel(bitmap, abs(mouse.x-x), abs(mouse.y-y)); 4 5 if(pxg.a == 0) // NORMAL 6 { 7 return true; 8 } 9 else if(pxg.a != 0) // ALPHA 10 { 11 return true; 12 } 13 else if(pxg == color) // COLOR // operator== 14 { 15 return true; 16 } 17 else return false; 18}

but are still delays, but smaller

RPG Hacker
Member #12,492
January 2011
avatar

Try replacing ALLEGRO_VIDEO_BITMAP with ALLEGRO_MEMORY_BITMAP.

00Andre
Member #16,399
June 2016
avatar

How swapped for the Allegro BITMAP MEMORY is even worse

RPG Hacker
Member #12,492
January 2011
avatar

And what if you do as suggested in my first post? Create two versions of that bitmap. One with ALLEGRO_VIDEO_BITMAP and one with ALLEGRO_MEMORY_BITMAP. Use the ALLEGRO_VIDEO_BITMAP one for rendering, while you use the ALLEGRO_MEMORY_BITMAP one for your alpha checks.

Mark Oates
Member #1,146
March 2001
avatar

I'm just curious, 1) how many bitmaps are you checking and 2) what are the sizes of these bitmaps?

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

00Andre
Member #16,399
June 2016
avatar

RPG Marker
It works ! and it very quickly . I did like you said :D:D:D:D

I have one questions.

If it is eg . 200 bitmaps is 200 * 2 is equal to 400 bitmaps.
it will not be problems with optimization ?

RPG Hacker
Member #12,492
January 2011
avatar

The game will, of course, use addtional memroy in system RAM for each copy of a bitmap that you use for mouse collision detection, but depending on the number of bitmaps you need mouse detection for, the size of each bitmap and the hardware you are targeting for your game, it hopefully shouldn't bee too much of a problem. Nowadays most machines have at least 4 to 8 GB of system RAM, which don't exceed to quickly.

If this RAM usage is still too much for you, you'll most likely have to try a different solution, like one of the solutions jmasterx suggested or maybe even just simple box collisions (depending on what you need this for in the first place). So yeah, the best solution really always depends on what exactly you're trying to do and for what purpose.

00Andre
Member #16,399
June 2016
avatar

Thank you very much. ;)

does not want create new thread, So here write

i often use al_clone_bitmap in project.
i tried use recording

#SelectExpand
1 ALLEGRO_BITMAP* bt1 = al_load_bitmap("bt.png"); 2 ALLEGRO_BITMAP* bt2 = bt1;

only if drew on bt2 that bt1 also changed.

Why don't use al_clone_bitmap ? because if used clone_bitmap a lot, that is delay
and i tried solve a problem.

Mark Oates
Member #1,146
March 2001
avatar

al_clone_bitmap will create and allocate in memory a whole new bitmap (based on the previous). Any time you allocate something new in memory, it is usually much slower than other things.

But if you do it like you showed:

ALLEGRO_BITMAP* bt1 = al_load_bitmap("bt.png");
ALLEGRO_BITMAP* bt2 = bt1;

Here you are just creating a new pointer. Now you have two variables that point to the same bitmap in memory, so no copying and allocation for a new bitmap has to happen.

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

Go to: