Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / [SOLVED] Get Pixel of Image

Author
Message
Rocco Sette
6
Years of Service
User Offline
Joined: 2nd Aug 2017
Location:
Posted: 14th Aug 2018 17:45
Hi,

I'm trying to detect if a click on the screen match with the earth on a map, and not the see.

The see have an uniform color, someone have an idea ?

I get the sprite under GetPointerX and GetPointerY but he detect the transparence... Because i separate water sprite and earth sprite...

Thank's

The author of this post has marked a post as an answer.

Go to answer

smallg
Valued Member
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location: steam
Posted: 14th Aug 2018 17:56
probably convert the image to a memblock and then return the alpha value of the point clicked - this is pixel perfect collision

or if your land doesn't change you could create a bunch of custom collision shapes for your land to match the shape - see the shapes with SetPhysicsDebugOn() - this method is likely the faster method in real time but must be set up in advance
life's one big game
spec= 4ghz, 16gb ram, AMD R9 2700 gpu
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 15th Aug 2018 05:47
Returns the color of a pixel
fubar
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 15th Aug 2018 14:33
Here's my pixel-perfect system, I even wrote a document explaining the method I used.

https://forum.thegamecreators.com/thread/219444
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Bengismo
6
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 15th Aug 2018 15:42 Edited at: 15th Aug 2018 15:45
I posted an example of doing this months ago but cant find the thread.

The example tells you whether your looking at the sea or a country and it tells you which country. It does this by looking up the pixel colour in an image memblock. I actually intentionally used a separate image to encode which country is which but the alpha in the main image could be used if wanted but its actually very difficult to find good paint programs where you can set an alpha value accurately so a separate image is a good idea.

Attachments

Login to view attachments
Rocco Sette
6
Years of Service
User Offline
Joined: 2nd Aug 2017
Location:
Posted: 15th Aug 2018 20:59
It works this of solution of fubarpk, thank's !
Rocco Sette
6
Years of Service
User Offline
Joined: 2nd Aug 2017
Location:
Posted: 31st Jan 2019 12:49 Edited at: 31st Jan 2019 12:50
Hi again !

I use that for get pixel of image :



But loading is soooo long...

We have a way to get RVB pixel more speed ?
puzzler2018
User Banned
Posted: 31st Jan 2019 13:01 Edited at: 31st Jan 2019 13:02
Because we got pick pixel inside two for loops and the pick pixel has creatememblockfromimage function. This will be causing that

If I get chance tonight I'll tweak to just have 1 global creatememblockfromimage before it enters the two for loops and then will be faster

Unless others are about before I get to it
Rocco Sette
6
Years of Service
User Offline
Joined: 2nd Aug 2017
Location:
Posted: 31st Jan 2019 13:17
OK i waiting, thank's
Rocco Sette
6
Years of Service
User Offline
Joined: 2nd Aug 2017
Location:
Posted: 31st Jan 2019 13:21 Edited at: 31st Jan 2019 13:34
I precise that i want loop on "image", not on actuel sprite show on the screen.
puzzler2018
User Banned
Posted: 31st Jan 2019 13:36
Something like this


fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 31st Jan 2019 14:40
Just saw this good solution puzzler
I normally refrain from globals as they use space on the stack (well they do in c etc not sure how AppGameKit handles the stackspace)
but passing the variables is quite an easy change to this code. not sure if passing a variable by reference in AppGameKit is that much
better i imagine its like pointers which is also an improvement. If you have a look at KrazyGrandPrix i know the codes big you
wont find globals but lots of passing by reference. This is one of my new challenges no more globals lol well im a try anyway.

For those that dont understand what passing by reference is thats when you pass a variable to a function that you can change
in the function. This allows you to change multiple parametes instead of being stuck with the normal function method of
returning a value and setting that value to want for example add c=add(10,5) would return 15 to the value c but passing a,b,c as a
reference li do something(a,b,c) you could change the values of a, b and c all in the function so something



fubar
Rocco Sette
6
Years of Service
User Offline
Joined: 2nd Aug 2017
Location:
Posted: 31st Jan 2019 15:07
@puzzler2018 : But i'm trying to get color RGB of each pixel, your offset is too big, no ?

@fubarpk : Can you give an example of passing by reference ?
puzzler2018
User Banned
Posted: 31st Jan 2019 15:20 Edited at: 31st Jan 2019 15:21
Doing it the way I did will stop recreating of image memblocks causing memory lags each frame.

The picture is just loaded once into a memblock and then the offset works out the x and y of every pixel from the main image memblock of which dispose at the end abyway

Promise you won't get any better than this
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 31st Jan 2019 15:28 Edited at: 31st Jan 2019 15:31
Scrolling text example passing by reference

its late 2am here but here is a quick example I made from previous code you may find useful
sorry its the credits from KrazyGrandPrix and uses kenney font but you should be able to change that
but could use any font just may have to change the sprite sizes and in the createtextimage the get image
command to match anyfont and size. also may find in the scroller routine if you go bigger font you
may wish to change this if textSpr[num].x <-20 then textSpr[num].x=GetWindowWidth() +280

hope you find the code useful

I also find constants are very handy ie you can create a sprite as a set number for example
#constant mysprite 1 ,createSprite(mysprite,0) this can make your code much more readable and allot
better than a serious of numbers and one line to change to update all of your code if you need to
fubar
Rocco Sette
6
Years of Service
User Offline
Joined: 2nd Aug 2017
Location:
Posted: 31st Jan 2019 15:32 Edited at: 31st Jan 2019 15:33
This :


Is the only thing that i don't understand...

Why 12 ? Why 4 ?
Rocco Sette
6
Years of Service
User Offline
Joined: 2nd Aug 2017
Location:
Posted: 31st Jan 2019 15:41
Oh :o
4 for RGB + Alpha and 12 for the metadata ?
Golelorn
7
Years of Service
User Offline
Joined: 20th Nov 2016
Location:
Posted: 31st Jan 2019 15:42
https://www.appgamekit.com/documentation/Reference/Memblock/CreateImageFromMemblock.htm

According to the documentation 4 is the byte size, and 12 is the offset.
Rocco Sette
6
Years of Service
User Offline
Joined: 2nd Aug 2017
Location:
Posted: 31st Jan 2019 15:44
Yes i understand now.

Thank's a lot, i understand more memblock now
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 31st Jan 2019 15:46
puzzlers code with passing by reference


fubar
puzzler2018
User Banned
Posted: 31st Jan 2019 18:05 Edited at: 31st Jan 2019 18:06
Thanks Fubarpk - just minor tweak here and there, sorry if been pedantic


Rocco Sette
6
Years of Service
User Offline
Joined: 2nd Aug 2017
Location:
Posted: 31st Jan 2019 20:22 Edited at: 31st Jan 2019 20:24


Error is at line " r = getMemblockbyte(mem, offset) // tweaked " (See attached file

Attachments

Login to view attachments
puzzler2018
User Banned
Posted: 31st Jan 2019 20:29
This post has been marked by the post author as the answer.
Try with the For loops with width-1 and height-1

Like this



This is the working one that I have just tested and creates the map.txt as requested too
puzzler2018
User Banned
Posted: 31st Jan 2019 20:38
Wow didnt realise a function can return type data

Learn something new everyday

fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 31st Jan 2019 20:56
Quote: "Wow didnt realise a function can return type data"

Yep

Sorry if sounded naggy, wasn't particularly aimed at anyone just thought it was a good chance to speak about referencing
in native c the ability to use pointers and the clean code it creates are the two main reasons for its speed. Im not even sure
the workings behind if AppGameKit use it the same but speed tests would make me think so

fubar
puzzler2018
User Banned
Posted: 31st Jan 2019 20:59
no no its perfectly good programming - im tired of seeing Global variables in my apps too, im sure better way to manage it.

so, good work to advsing how to do things a different way
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 31st Jan 2019 21:10 Edited at: 31st Jan 2019 21:23
Your welcome.

Ive only just learned that agk can do this, im sure like with code like your minecraft
the huge global arrays of types could get a performance boost with programming
techniques like this. unfortunately you cant pass by reference a single integer,string
or float it has to be a made json type.

My tests on this with dbpro verses gdk there was a minor speed improvement directly
converting the code over but replacing globals with pointer arithmetic made a big boost
to speed. Not so sure AppGameKit does do it quite aswell but there was speed comparisons
on one of the forum posts
fubar
chafari
Valued Member
17
Years of Service
User Offline
Joined: 2nd May 2006
Location: Canary Islands
Posted: 31st Jan 2019 21:26
@Bengismo ...That's very nice ...I will keep this
I'm not a grumpy grandpa
Rocco Sette
6
Years of Service
User Offline
Joined: 2nd Aug 2017
Location:
Posted: 31st Jan 2019 22:08
Thank's to puzzler2018 and fubarpk, it work's perfectly now !

Just load to write file for save pixel in txt file is long, but it work's x)

Thank's !
puzzler2018
User Banned
Posted: 31st Jan 2019 22:14
Your welcome.

Your writing the RGB as their values and not as their bytes. Hope thats what you trying to acheive further

R=255
G=255
B=0

then the file writes with the code as

255,255,0

If thats correct to what you want then our time is achieved here

Rocco Sette
6
Years of Service
User Offline
Joined: 2nd Aug 2017
Location:
Posted: 31st Jan 2019 22:20 Edited at: 31st Jan 2019 22:20
puzzler2018
User Banned
Posted: 31st Jan 2019 22:22
perfect - leave you with happy programming
puzzler2018
User Banned
Posted: 31st Jan 2019 22:24
But then not sure your logic, is there a reason why you would want to write an image out and then back in.

im sure you have your reasons
puzzler2018
User Banned
Posted: 31st Jan 2019 22:27
What are you trying to wanting to achieve here - writing something and then back in, in some form doesnt make sense - i thought you maybe working on compression/encryption system lol

- -
Rocco Sette
6
Years of Service
User Offline
Joined: 2nd Aug 2017
Location:
Posted: 31st Jan 2019 22:36 Edited at: 31st Jan 2019 22:36
I use it with offset map, i load a map and move around like on google eath.

If i'm too much on left, left come to right, up to down, down to up etc.
puzzler2018
User Banned
Posted: 31st Jan 2019 22:40
spot on - ill leave you to it..

Talking on google maps - i was thinking of a 3D heightmap on a google map snap.

So a live 3D with height creation on a google map - realtime

Sounds interesting to bring back real life - wonder what be like to see our home town in 3D in AppGameKit - or maybe go to extreme in going further afield

Such good fun I think

real time driving in real world UK
Golelorn
7
Years of Service
User Offline
Joined: 20th Nov 2016
Location:
Posted: 2nd Feb 2019 01:28 Edited at: 2nd Feb 2019 01:33
fubarpk, I was testing out passing arrays as reference through functions, and discovered, that when a global array is passed it changes the array to local. So any changes made to that array do not effect the global array. Threw me for a huge loop! But sounds like I need to start doing this. I changed some of my functions, and got rid of a few globals. I quickly realized I am really abusing globals when there is no need.

edit: just realized the function probably needs to return the array to change the global array like
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 2nd Feb 2019 05:08 Edited at: 2nd Feb 2019 05:11
Oh I was brought up oldskool that globals were really bad, not as much an issue nowadays
but they do chew resources and effect performance. Especially when you have arrays the larger
the worse effect on performance. Im glad you have learnt about them

the difference between c and agk is they both similar and apart from syntax pointers are the
main difference and pointer arithmetic is one of the hardest things to get your head around in
native c. But when you do its amazing the difference. Unfortunately we cant use pointers like
you can in c but passing by reference is dam close
fubar
Rick Nasher
6
Years of Service
User Offline
Joined: 25th Jul 2017
Location: Amsterdam
Posted: 3rd Feb 2019 10:14 Edited at: 3rd Feb 2019 10:58
@puzzler2018
Quote: "spot on - ill leave you to it..

Talking on google maps - i was thinking of a 3D heightmap on a google map snap.

So a live 3D with height creation on a google map - realtime

Sounds interesting to bring back real life - wonder what be like to see our home town in 3D in AppGameKit - or maybe go to extreme in going further afield

Such good fun I think

real time driving in real world UK"


Very, very cool idea. Did something similar in Blitz3d, but then in the Netherlands and flying in helicopter.
Driving is so much more fun though, as the Netherlands are basically very flat. Tend to get boring quickly (from airial point of view).
Rocco Sette
6
Years of Service
User Offline
Joined: 2nd Aug 2017
Location:
Posted: 4th Feb 2019 09:55
Optimization of writing / reading :



All work's fine

Before it taking 10 seconds, now is instantanly

Login to post a reply

Server time is: 2024-04-18 16:15:17
Your offset time is: 2024-04-18 16:15:17