count number of transparent pixels in sprite

New releases and general discussions.

Post » Fri Jul 31, 2009 12:27 pm

need number of pixels that are not transparent as a value. with this i can create is totally covered event by using another 1 pixel sprite in a container to scan across the top-left to bottom right of main sprite when it overlaps both the main sprite and other sprite 1 is added to variable. when it reaches the end if untransparent pixels of sprite match variable it means its completely covered.
B
2
S
2
G
4
Posts: 259
Reputation: 1,968

Post » Fri Jul 31, 2009 12:52 pm

You can do this already, but its very slow.
Image manipulator copy from sprite
For x = 1 to sprite.width
for y =1 to sprite.height
System: ImageManipulator GetAlpha(LoopIndex("x"), LoopIndex("y")) Equal to 0
>sprite add 1 to sprite.privatevariable

Then if you want not transparent just use greater than 0, but beware of semi transparencies.
Image Image
B
161
S
48
G
91
Posts: 7,359
Reputation: 67,273

Post » Fri Jul 31, 2009 3:44 pm

What on earth do you need this for? It seems a very obscure case to spend programming time on.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,610

Post » Fri Jul 31, 2009 4:45 pm

I did something similar with my terrain generator, but that was an app not a game.
It might sound like a good idea, but per pixel operations will always be way to slow for regular events.
I mean its doable, for something like on the start or load of a layout, if you want to create or save some data for use later.
Image Image
B
161
S
48
G
91
Posts: 7,359
Reputation: 67,273

Post » Sat Aug 01, 2009 1:40 am

quote. what on earth would you do with it

quote. can make totally covered event

there are many things you can do with completely covered events. especially for a.i. its been asked b4

at newt. yeah you could set it up for your frames at startup and use value later. transparent or not transparent dont matter because if you got one you can get the other

for speed on regular event i would go with setting it up to run when specify conditions are met probably with trigger once. normally you wouldn't want to scan px by px just like you wouldnt want while condition to run always
B
2
S
2
G
4
Posts: 259
Reputation: 1,968

Post » Sat Aug 01, 2009 4:07 am

Occlusion query?
B
3
S
2
G
4
Posts: 1,445
Reputation: 4,665

Post » Sat Aug 01, 2009 4:08 pm

How would it help with a 'completely covered' event? It doesn't tell you where the transparent pixels are, just how many there are...
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,610

Post » Sat Aug 01, 2009 4:23 pm

with a event that uses a 1px scanner sprite that scans each pixel of sprite checking for overlaps and counting them if amount overlapped = to number of px not transparent it is covered. you can get untransparent number by subtracting the width and height minus transparent or vice versa for getting transparent either value will do.

by transparent i mean opacity or alpha = 0
B
2
S
2
G
4
Posts: 259
Reputation: 1,968

Post » Sat Aug 01, 2009 4:53 pm

at newt. have you tried

Image manipulator copy from sprite
For x = 1 to sprite.width
for y =1 to sprite.height
System: ImageManipulator GetAlpha(LoopIndex("x"), LoopIndex("y")) Equal to 0
>sprite add 1 to sprite.privatevariable

in new version. if you do and it works can you post cap. i dont want to report if its not bug. i tried on 2x2 sprite half transparent other color and variable goes to 4.
B
2
S
2
G
4
Posts: 259
Reputation: 1,968

Post » Sat Aug 01, 2009 6:05 pm

Yeah I'm getting some weird results, but I suspect its image editor related rather than imagmanipulator, but that's just another reason to use the function sparingly. Again per pixel operations are always going to be quirky especially when dealing with semi transparencies, not to mention other users hardware.
Image Image
B
161
S
48
G
91
Posts: 7,359
Reputation: 67,273

Next

Return to Construct Classic Discussion

Who is online

Users browsing this forum: No registered users and 1 guest