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 / Extra image functions - 9 Sprite scaling etc.

Author
Message
Scraggle
Moderator
16
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 21st Nov 2016 19:38 Edited at: 13th Mar 2017 19:02
Here's a collection of image and sprite manipulation functions free of charge (none of this silly pay for code in the store nonsense!)

All the parameters are fully explained in the source code below.
I'd welcome any feedback

SetSpriteTiled( spr, w#, h# )

Description: Performs in much the same way as a simple SetSpriteSize()
However, the texture remains the same size and is instead
tiled across the sprite


SetSpriteSize9Slice( sImg, tSpr, tW, tH, cW, cH )

Description: Creates a sprite by resizing an image without affecting the corners
by dividing the image into nine parts, positioning the
corners and then resizing everything else to fit


SetSpriteSize9SliceFromArea( sImg, tSpr, sX, sY, sW, sH, tW, tH, cW, cH )

Description: Creates a sprite by resizing an image without affecting the corners
by dividing the image into nine parts, positioning the
corners and then resizing everything else to fit.
It differs from SetSpriteSize9Slice() in that an area is passed to the function
and the new sprite is captured and resized from that area of the source image
instead of using the complete image.


GetImage9Slice( sImg, tImgs[], cW, cH )

Description: Slices an image into 9 parts and places the image ID's in an array


GetSprite9Slice( sImg, tSpr[], cW, cH ))

Description: Slices an image into 9 parts, creates sprites from them and places
the sprite ID's in an array


BlendImages( iUpper, iLower, ialpha, Width, Height)

Description: This function will take two images of any size (equal or not)
and blend them together according to the values of a third
greyscale alpha map image


CopyMemblockAreaToMemblock( sMb, tMb, x, y, w, h, tX, tY )

Description: Copy a rectangular area from one memblock to another.
The rectangle can be anywhere in the source image
and it can be positione anywhere in the target image.


CopyImageAreaToImage( sImg, tImg, x, y, w, h, tX, tY )

Description: Copies a rectangular area of an image into another image
The rectangle can be anywhere in the source image
and it can be positioned anywhere in the target image.


CopySpriteAreaToSprite( sSpr, tSpr, x, y, w, h, tX, tY )

Description: Copies a rectangular area of a sprite into another sprite
The rectangle can be anywhere in the source sprite
and it can be positione anywhere in the target sprite.


CopySpriteAreaToMemblock( sSpr, tMb, x, y, w, h, tX, tY )

Description: Copies a rectangular area of a sprite into a memblock
The rectangle can be anywhere in the source sprite
and it can be positione anywhere in the target memblock.


CopyImageAreaToMemblock( sImg, tMb, x, y, w, h, tX, tY )

Description: Copies a rectangular area of an image into a memblock
The rectangle can be anywhere in the source image
and it can be positione anywhere in the target memblock.


CopyImageAreaToSprite( sImg, tSpr, x, y, w, h, tX, tY )

Description: Copies a rectangular area of an image into a sprite
The rectangle can be anywhere in the source image
and it can be positione anywhere in the target sprite.


CreateMemblockEmptyImage(w, h)

Description: Creates a new memblock ready to create an image and
populates the header of the memblock with the dimensions.


CreateMemblockMask( sImg, rm, gm, bm )

Description: Creates a mask based on the alpha channel of the source image
but only if the source image colour matches that passed in


CreateMemblockMaskFromArea( sImg, rm, gm, bm, x, y, w, h )

Description: Creates a mask based on the alpha channel of the source image
but only if the source image colour matches that passed in
and then only within the area specified by the passed parameters


ApplyMaskToImage( img, mask, x, y )

Description: Applies a previously created mask to the alpha channel of an image
Both the mask and the image must exist or the function will fail


InvertMemblockalpha( mb )

Description: Inverts the alpha channel of a given memblock


InvertImageAlpha( img )

Description: Inverts the alpha channel of a given image

AGK V2 user - Tier 1 (mostly)
janbo
10
Years of Service
User Offline
Joined: 10th Nov 2008
Location: Germany
Posted: 21st Nov 2016 22:15 Edited at: 21st Nov 2016 22:18
AAAWWW I wanted to do that now I don't have to investigate it myself to create such a nice function set... thank you..
...no realy... I appreciate it ^^

I am missing the myResizeImage() function not ?

Using AGKv2 Tier1
damothegreat
3
Years of Service
User Offline
Joined: 18th Sep 2016
Location:
Posted: 21st Nov 2016 22:32
should be a nice challenge to create a ResizeImage
Using Tier 1 AppGameKit V2
Started coding with AMOS
Anything is possible if put mind to it
Scraggle
Moderator
16
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 21st Nov 2016 22:43
Quote: "I am missing the myResizeImage() function not ?"


You can rename that to ResizeImage().
I wrote it before Paul had included a native ResizeImage() command so I renamed mine until I had tested that the functionality was the same.
It is, so you don't need mine - stick with the native one
AGK V2 user - Tier 1 (mostly)
BatVink
Moderator
16
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 22nd Nov 2016 08:17
Nice set of functions, thanks for sharing

I was about to post my 9-slice function (saw the same post as you about 2.0.22) and have been usurped by a full library of image functionality.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Scraggle
Moderator
16
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 22nd Nov 2016 09:27 Edited at: 22nd Nov 2016 09:31
I've just noticed that there is another function missing.
It's from my maths library (which I have previously shared) but if you don't have it then here are the min/max functions from it:


Let me know if you come across any other missing functions. I always include a lot of files in every project so there might be other functions that get referenced that I haven't included.
AGK V2 user - Tier 1 (mostly)
Mobiius
Valued Member
16
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 22nd Nov 2016 13:10
Nice, thanks.
MikeHart
16
Years of Service
User Offline
Joined: 9th Jun 2003
Location:
Posted: 22nd Nov 2016 16:04
Again, thank you!
-----------------------------------------------------------
Using AGK2 Tier 1
JohnnyMeek
6
Years of Service
User Offline
Joined: 23rd Apr 2013
Location: Slovenia
Posted: 23rd Nov 2016 15:01
The missing MyResizeImage function returns an integer, and the built in ReSizeImage doesn't. It looks like the functionality is different.

Scraggle
Moderator
16
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 23rd Nov 2016 16:00 Edited at: 23rd Nov 2016 16:02
Yes, sorry, you are correct.
Here is myResizeImage function:


Mine creates a new image with the resized dimensions rather than resizing the one passed in.
AGK V2 user - Tier 1 (mostly)
Scraggle
Moderator
16
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 23rd Nov 2016 19:15 Edited at: 23rd Nov 2016 19:17
Examples of some of the functions in action:

BlendImages()


SetSpriteTiled()


SetSpriteSize9Slice()
AGK V2 user - Tier 1 (mostly)

Attachments

Login to view attachments
Rickynzx
7
Years of Service
User Offline
Joined: 19th Dec 2011
Location: Troon, Scotland
Posted: 14th Dec 2016 15:00
hi Scraggle, i have been using your great functions, but i cant seem to get setspritesize9slice to work correctly. i seem to get a messed up image. i dont know if im using the wrong parameters for the function or not. anyone else having any problems with this?
Mobiius
Valued Member
16
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 14th Dec 2016 16:39
@Rickynzx: Screenshots? Messed up image how?
damothegreat
3
Years of Service
User Offline
Joined: 18th Sep 2016
Location:
Posted: 14th Dec 2016 18:47 Edited at: 14th Dec 2016 18:59
Love the examples of the these functions - I'm sure the will become very useful - especially the Sprite9 one for an App I'm doing.

The picture looks unprofessional at moment, but with Sprite9 command, it will look cleaner and super

Could use some of these to also create an image from screen to file - so that in our Apps - we could have a multiple Save Game feature - to allow a list of Saved Game time stamps (together
with a screen shot of the position of the game you at at the time it saved - I think people in the world would like this feature in games


Cheers

Damo
Using Tier 1 AppGameKit V2
Started coding with AMOS
Anything is possible if put mind to it
Scraggle
Moderator
16
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 15th Dec 2016 08:28
Quote: "i cant seem to get setspritesize9slice to work correctly. i seem to get a messed up image. i dont know if im using the wrong parameters for the function or not. anyone else having any problems with this?"


I'd love to to help but with the limited info you've given me I can only guess at what you're doing.
So, I'm going to guess that you've laid a banana in the road and you're sat watching and waiting for a car to smoosh it, at which point the image (you assume) will correctly split into 9 parts and resize.

No?

OK, then maybe you could tell me exactly what it is that you're trying and show the results that you're getting.
AGK V2 user - Tier 1 (mostly)
Mobiius
Valued Member
16
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 15th Dec 2016 10:49
Quote: "So, I'm going to guess that you've laid a banana in the road and you're sat watching and waiting for a car to smoosh it, at which point the image (you assume) will correctly split into 9 parts and resize."

Legend.
Rickynzx
7
Years of Service
User Offline
Joined: 19th Dec 2011
Location: Troon, Scotland
Posted: 15th Dec 2016 12:16 Edited at: 15th Dec 2016 12:47
Sorry, thought i posted some images. Heres the original image and the messed up image.

does the original image have to be a certain size? or does the final image have to be a square as well?

[img][/img]

[img][/img]

Attachments

Login to view attachments
Scraggle
Moderator
16
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 16th Dec 2016 08:15
From the code that you've uploaded I can see exactly what the problem is.

You've placed your banana on a very quiet road with very few cars. You'll be waiting forever before it gets smooshed.
You need to move the banana to a busier road or just keep waiting .....
AGK V2 user - Tier 1 (mostly)
Mobiius
Valued Member
16
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 16th Dec 2016 08:54
Quote: "From the code that you've uploaded I can see exactly what the problem is.

You've placed your banana on a very quiet road with very few cars. You'll be waiting forever before it gets smooshed.
You need to move the banana to a busier road or just keep waiting ....."

Dude, stop!
Rickynzx
7
Years of Service
User Offline
Joined: 19th Dec 2011
Location: Troon, Scotland
Posted: 16th Dec 2016 13:37
code i have been using

Scraggle
Moderator
16
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 16th Dec 2016 14:04 Edited at: 16th Dec 2016 14:05
That won't work.
The second parameter needs to be a valid sprite ID or you need to call the function thusly:
AGK V2 user - Tier 1 (mostly)
Mobiius
Valued Member
16
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 16th Dec 2016 16:29
The banana has been successfully smooshed.
Rickynzx
7
Years of Service
User Offline
Joined: 19th Dec 2011
Location: Troon, Scotland
Posted: 17th Dec 2016 13:38 Edited at: 18th Dec 2016 21:32
Hi Scraggle , ive tried your code, but still get the messed up mage.
I have also tried the following code with no success.






Quote: "The banana has been successfully smooshed."


Mobiius
Valued Member
16
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 19th Dec 2016 14:21
Have you tried...
damothegreat
3
Years of Service
User Offline
Joined: 18th Sep 2016
Location:
Posted: 19th Dec 2016 14:36
I always suggest to attach the images in the media folder here and the full code in the tags

just so we can work better, instead of tooing and throwing with ideas what it might not be

please attach these images and the code you are experiencing the failure with and can resolve it better, cause we would be able to produce the issue and rectify it

Damo
Using Tier 1 AppGameKit V2
Started coding with AMOS
Anything is possible if put mind to it
Rickynzx
7
Years of Service
User Offline
Joined: 19th Dec 2011
Location: Troon, Scotland
Posted: 20th Dec 2016 14:28
still getting the same, could it be something to do with my computer, not a very powerful laptop.
damothegreat
3
Years of Service
User Offline
Joined: 18th Sep 2016
Location:
Posted: 20th Dec 2016 14:37
Give me a few mins - Ill try and reproduce the issue by grabbing @scraggles code and the PIC your using GUITEST.PNG

Damo
Using Tier 1 AppGameKit V2
Started coding with AMOS
Anything is possible if put mind to it
damothegreat
3
Years of Service
User Offline
Joined: 18th Sep 2016
Location:
Posted: 20th Dec 2016 14:46


I get the same style issue - pinky bits all over - maybe a memory issue with the geblocks memory allocation / data put incorrectly



[img]c:\data\error.png[/img]


It wont be laptop - my laptop is top range and still produces similar issue



Using Tier 1 AppGameKit V2
Started coding with AMOS
Anything is possible if put mind to it

Attachments

Login to view attachments
damothegreat
3
Years of Service
User Offline
Joined: 18th Sep 2016
Location:
Posted: 20th Dec 2016 14:58
@Scraggle.....

Looks like it fills out an empty area of color first (the pinky color) with this bit of code in the Sprite9Slice function

tMb = CreateMemblockEmptyImage( tW, tH )
c = MakeColor(200,0,100) /// Where the piny color is coming from
for k = 12 to tW*tH*4+12 step 4
SetMemblockInt(tMb,k,c)
next k

and doesn't look like its filling out the true pixels from the original

Damo
Using Tier 1 AppGameKit V2
Started coding with AMOS
Anything is possible if put mind to it
Scraggle
Moderator
16
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 20th Dec 2016 19:46
It looks like I may have left some debug code in there.
I'll try to take a look at it soon
AGK V2 user - Tier 1 (mostly)
Scraggle
Moderator
16
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 22nd Dec 2016 08:10 Edited at: 22nd Dec 2016 09:15
Solved!
The problem was with the myResizeImage() function. Although my tests worked perfectly that was only because of the size of the screen. When I tried it with a different size I got the same results as you.

Try replacing the SetSpriteSize9Slice() function with this one and let me know how you get on:

AGK V2 user - Tier 1 (mostly)
damothegreat
3
Years of Service
User Offline
Joined: 18th Sep 2016
Location:
Posted: 22nd Dec 2016 10:29
Thanks @Scraggle - that now works for me
Using Tier 1 AppGameKit V2
Started coding with AMOS
Anything is possible if put mind to it
Rickynzx
7
Years of Service
User Offline
Joined: 19th Dec 2011
Location: Troon, Scotland
Posted: 22nd Dec 2016 11:12
Thanks, works for me too. brilliant set of functions.

I have started delving into the memblock functions as a result of this, i thought they were rather confusing and difficult. But once you get your head around them its amazing what can be done with them.
They will certainly improve my games graphics.
Scraggle
Moderator
16
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 22nd Dec 2016 12:47 Edited at: 22nd Dec 2016 12:48
Quote: "Thanks @Scraggle - that now works for me"

Quote: "Thanks, works for me too"

Groovy
AGK V2 user - Tier 1 (mostly)
CumQuaT
AGK Master
9
Years of Service
User Offline
Joined: 28th Apr 2010
Location: Brisbane, Australia
Posted: 22nd Dec 2016 13:17
I admire your dismissal of the pay-for-code system. Rock on, Scraggle, rock on.
Rich Dersheimer
AGK Developer
10
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 31st Dec 2016 01:11
Very cool, thanks for sharing. (BUMP)
GarBenjamin
AGK Developer
2
Years of Service
User Offline
Joined: 30th Nov 2016
Location: USA
Posted: 31st Dec 2016 17:43 Edited at: 31st Dec 2016 17:43
Fantastic! Thanks for sharing these. I am enjoying participating in this community more each day. You are a refreshing bunch.
TI/994a (BASIC) -> C64 (BASIC/PASCAL/ASM/Others) -> Amiga (AMOS/BLITZ/ASM/C/Gamesmith) -> DOS (C/C++/Allegro) -> Windows (C++/C#/Monkey X/GL Basic/Unity/Others)
Blendman
5
Years of Service
User Offline
Joined: 17th Feb 2014
Location: Arkeos
Posted: 28th Feb 2017 08:37
Hi Scraggle !
Thank you a lot.

Some of your functions are really usefull !
I use it inmy tool Age2D, and my buttons are now better and beautiful (with SetSpriteSize9Slice())

Thanks again !
AGK2 tier1 - http://www.dracaena-studio.com
Phaelax
DBPro Master
16
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 28th Feb 2017 14:13
I would imagine the main reason for the 9 slice command is on windows/objects that get resized during runtime. As any fixed-sized objects would be designed with the proper dimensions from the start. That being said, wouldn't it have less impact to build an object made of 9 sprites rather than copying image data? I'm thinking of when resizing a window, that's a lot of image recreation going on during the resize process

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Scraggle
Moderator
16
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 13th Mar 2017 19:07 Edited at: 13th Mar 2017 20:20
Quote: "I would imagine the main reason for the 9 slice command is on windows/objects that get resized during runtime"

Not really.

A better example of why you would use it is if you were creating a GUI (as indeed Blendman has done). You could design a single button in you're graphics app of choice and then create any number of them at any size in AppGameKit whilst still maintaining their correct look.

Quote: "wouldn't it have less impact to build an object made of 9 sprites rather than copying image data?"

Yes, good suggestion. That's why I have just added two new commands to the original post.

GetImage9Slice( sImg, tImgs[], cW, cH )

Description: Slices an image into 9 parts and places the image ID's in an array


GetSprite9Slice( sImg, tSpr[], cW, cH ))

Description: Slices an image into 9 parts, creates sprites from them and places
the sprite ID's in an array


With those, you can split an image into 9 parts and have them stored in an array of image ID's (or Sprite ID's) for future reference.
That way you only need to slice a source image once. You'll need to resize them yourself if you do it this way but it cuts out the cutting out and it's something that I just found a need for.
I created an info window that I wanted to stretch horizontally and then vertically until it was the correct size (different each time) so I create the 9 sprites with GetSprite9Slice() and used tweens to do the resizing. It produces a great effect

You're welcome
AGK V2 user - Tier 1 (mostly)

Login to post a reply

Server time is: 2019-09-19 17:22:44
Your offset time is: 2019-09-19 17:22:44