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.

Newcomers AppGameKit Corner / Sprite memory usage

Author
Message
vitp4145
8
Years of Service
User Offline
Joined: 22nd Aug 2016
Location:
Posted: 21st Oct 2017 20:24
Hello everybody,

This may seem like a silly question, but as I am unsure, here goes.

I am working on a project to develop a card game, using a standard 52-card deck of playing cards. As far as I can see, I have two ways to go:

1) Load the 52 cards as individual images, then assign them to a 52-sprite card deck.

2) Load one image containing all 52 cards, and then create 52 sprites as animated sprites, each created using the one image containing all cards.

Using the 2nd option works well enough from a programming point of view, but which one would result in smaller memory usage? The single image containing all 52 cards is 433KB, the single card images are 72.1KB each.

I am unsure of wether the created sprites in option 2) actually use the image or just point to it. If they just point to it, then I am assuming option 2) will produce the smaller memory usage.

Any replies that help clear my understanding of this matter will be greatly appreciated.
puzzler2018
User Banned
Posted: 21st Oct 2017 20:29 Edited at: 21st Oct 2017 20:31
The same- your still loading 52 cards of the same size - they all need to be loaded - which ever method you choose for best programming techniques is your choice

I feel anyway

D
vitp4145
8
Years of Service
User Offline
Joined: 22nd Aug 2016
Location:
Posted: 21st Oct 2017 20:45 Edited at: 21st Oct 2017 20:46
"The same- your still loading 52 cards of the same size - they all need to be loaded"

If I load 52 seperate images at 72KB per image that is initially much more than a single image (containing 52 cards) at 433KB.

So, to make sure I understand this correctly, is each sprite then allocated a memory size equal to the image it uses in it's creation?

52 sprites using a single image of 433KB would then take up 52x433KB? (22,516KB) + 433KB (LoadImage()x1) = 22,949KB?

52 sprites using 52 images at 72KB per image would then take up 52X72KB? (3,744KB) + 3,744KB (LoadImage()x52) = 7,448KB?
puzzler2018
User Banned
Posted: 21st Oct 2017 20:53
Thats correct - each individual card (png file) need to be loaded into memory, consuming the amount of sizes your saying

Unless you want to something really fancy and that is create a blank card and load it once- then use the fonts to add your numbers as your app flows

Just an idea
puzzler2018
User Banned
Posted: 21st Oct 2017 20:58 Edited at: 21st Oct 2017 20:58
Forget that - fonts have licensing issues

Im sorry, but yes you will have to load them all into memory- its not that much surely
vitp4145
8
Years of Service
User Offline
Joined: 22nd Aug 2016
Location:
Posted: 21st Oct 2017 21:03
"Unless you want to something really fancy and that is create a blank card and load it once- then use the fonts to add your numbers as your app flows

Just an idea"

That is a very interesting idea ... I hadn't thought of that approach. A blank card, an appropriately labelled font and a suit/face image would make the memory requirement go down. All I would have to do is figure out how to make the seperate sprites move around the screen in sync/unison when I move the cards on the screen. It is a shame there is no BuildSprite() command to include multiple images in a sprite
puzzler2018
User Banned
Posted: 21st Oct 2017 21:09
You can use render and getimage commands to grab the much smaller fonts into memory - to the build them as a sprite

Can move sprites with setspriteposition to mvoe them along with the blank card sprite (may need to use sprite depth function)

Have fun and happy learning x
puzzler2018
User Banned
Posted: 21st Oct 2017 21:10
Im sure other guys on here too - may have some other interesting ideas - so please dont take this on board. Maybe others may like to have some imput
puzzler2018
User Banned
Posted: 21st Oct 2017 21:16
If you like - I can put something together tomorrow to get you started if you need
vitp4145
8
Years of Service
User Offline
Joined: 22nd Aug 2016
Location:
Posted: 21st Oct 2017 21:31
Thank you for your offer, it is much appreciated.

Figuring the best way to learn amy language is to use it to produce something, I have started this app to see if I can produce a card game. I am comfortable with using either option 1) or 2) (from a programming point of view, as the cards - along with their properties - are all in a record array. That said, the ability to position and move multiple sprites to give the illusion they are just one sprite will come in very handy for all manner of projects, so any help will be gratefully received.
puzzler2018
User Banned
Posted: 21st Oct 2017 21:36 Edited at: 21st Oct 2017 21:37
Your welcome - if need any support - just post what your needing assistance with - we will all try and help where ever possible

I shall quickly knock somethiing up tomorrow to get you started here.

For the 2nd option - you will need to learn TYPEs - and that is each card as a position of x and y to be kept remembered

any way - we all shall help your way forth

Welcome to AppGameKit and the forum
Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 22nd Oct 2017 04:03
vitp4145,

option #2 is more efficient from a performance and memory point of view.

PlayBASIC To HTML5/WEB - Convert PlayBASIC To Machine Code
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 22nd Oct 2017 10:14
Checkout the LoadSubImage() command. It will load a subimage from an atlas which can be created by utilities like Texture Packer or this utility created by the community
puzzler2018
User Banned
Posted: 22nd Oct 2017 15:57


D
vitp4145
8
Years of Service
User Offline
Joined: 22nd Aug 2016
Location:
Posted: 22nd Oct 2017 16:04
"option #2 is more efficient from a performance and memory point of view."

Can someone please explain why this should be so?

If, as has been suggested, each sprite has its own image of the original image loaded, wouldn't option 2 take up more memory?
puzzler2018
User Banned
Posted: 22nd Oct 2017 16:13
if suit=2 then suitname[2] = createtext("Spades") // ♠
if suit=1 then suitname[1] = createtext("Diamonds") // ♦
if suit=3 then suitname[3] = CreateText("Hearts") // ♥
if suit=4 then suitname[4] = CreateText("Clubs") // ♣

notice the characters at the end - these can be put in createtext too- will look lovely in the centre of the cards

Option 2 - your creating a simple box image (nothing much really for memory, then adding text for each card name and the above symbols .

It really isnt going to take a lot of memory my good fellow-

vitp4145
8
Years of Service
User Offline
Joined: 22nd Aug 2016
Location:
Posted: 22nd Oct 2017 16:30
Thanks for the code puzzler2018, it is much appreciated.

I have created two projects, one which loads 52 seperate cards and assigns them to a 52-sprite array and the other which loads a spritesheet image and then creates 52 sprites in an array using that image (which are then set to be animated sprites, each one with its own unique frame number).

Running each in turn and using Task Manager toi reference memory usage, loading 52 seperate card images runs with 18.4MB memory usage, while the one that uses one single image that is then used by the 52 sprite array runs with 41.7MB memory usage. Neither amount is huge, I admit, but it looks like loading each card as a seperate image is going to make the smallest foorprint.
puzzler2018
User Banned
Posted: 22nd Oct 2017 16:42
Cool

This version I did runs at 40mb

D
vitp4145
8
Years of Service
User Offline
Joined: 22nd Aug 2016
Location:
Posted: 22nd Oct 2017 16:46
I am not a computer guru by any means. Running the twoiprojects a few times and checking on the memory usage in Task Manager, it now shows each ruinning at about 40-41 MB apiece. Ah well, it will be good for the learning curve in AG2 to develop the app with both approaches in tandem. It should only impact the way in which each card in the array is accessed, code wise, and not really cause the rest of the code to have to be altered.
puzzler2018
User Banned
Posted: 22nd Oct 2017 16:50


Enjoy.

I certainly enjoyed creating it
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 22nd Oct 2017 18:06
I think you're getting stuck in the trap I often find myself in. I learned to be memory and CPU efficient in a business development environment.
My advice would be to make the game in the easiest way possible, and if you hit problems, then you can think about more efficiency.
This may seem like you will be refactoring some of your code which is time-consuming, but in most cases, this is still faster. otherwise you'll get hung up on the most efficient way to do everything.
I wrote a card game with all cards loaded, and it ran just fine. I would suggest loading the images, but only having enough sprites for the visible cards. Texture the visible cards with the relevant images.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
vitp4145
8
Years of Service
User Offline
Joined: 22nd Aug 2016
Location:
Posted: 22nd Oct 2017 18:12
Thank you for your advice BatVink. I am comfortable with creating and deleting sprites as required, so I will give this advice a go and see how it works.
Conjured Entertainment
AGK Developer
19
Years of Service
User Offline
Joined: 12th Sep 2005
Location: Nirvana
Posted: 22nd Oct 2017 21:52 Edited at: 22nd Oct 2017 22:05
Quote: "if suit=2 then suitname[2] = createtext("Spades") // ♠
if suit=1 then suitname[1] = createtext("Diamonds") // ♦
if suit=3 then suitname[3] = CreateText("Hearts") // ♥
if suit=4 then suitname[4] = CreateText("Clubs") // ♣

notice the characters at the end - these can be put in createtext too- will look lovely in the centre of the cards

Option 2 - your creating a simple box image (nothing much really for memory, then adding text for each card name and the above symbols .

It really isnt going to take a lot of memory my good fellow-"


That is kind of what I did for my video poker.

I had just 4 images for the cards, one for each suit, then I used text to make it the number or A,K,Q,J

It takes more code to assign the right number and suit to the number of card dealt (1-52) but it did not take many images at all.

Just 4 images for the faces and then I used different Gray tone patterns for the backs and let the user chose the back colors with RGB slides.

I gave the user a choice of 12 patterns, 1 of my logo already colored, 1 of AppGameKit logo already colored, and 10 gray tones that they could color.

Only 16 images for the whole deck with back options, and I used separate ones instead of an atlas.

It would have been better to have used an atlas and subimages I suppose, but it isn't necessary with so few images.

Here is what they look like in the folder...



Quote: "
I think you're getting stuck in the trap I often find myself in. I learned to be memory and CPU efficient in a business development environment.
My advice would be to make the game in the easiest way possible, and if you hit problems, then you can think about more efficiency.
This may seem like you will be refactoring some of your code which is time-consuming, but in most cases, this is still faster. otherwise you'll get hung up on the most efficient way to do everything.
I wrote a card game with all cards loaded, and it ran just fine. I would suggest loading the images, but only having enough sprites for the visible cards. Texture the visible cards with the relevant images."

Yeah, I was overly concerned with file sizes out of old habits.

David Gervais has a nice deck of card images that he gave to the community and hooked me up with some too, but I already had this other method in mind, so I did not use the 52 card atlas sets.

You can get a lot nicer look when not getting hung up about keeping things compact, but for the HTML5 build I would probably still try to keep the file sizes down, but that is just me.

Coding things my way since 1981 -- Currently using AppGameKit V2 Tier 1

Attachments

Login to view attachments
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 22nd Oct 2017 22:04
Just looked up my code from...2012!
I had 52 cards (attached, courtesy of David Gervais who contributed many resources to this forum).
I loaded all the images and when I wanted to display a card, I passed in a reference to it (array element), the value, and whether it should be back or front facing.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt

Attachments

Login to view attachments
Conjured Entertainment
AGK Developer
19
Years of Service
User Offline
Joined: 12th Sep 2005
Location: Nirvana
Posted: 22nd Oct 2017 22:06 Edited at: 23rd Oct 2017 01:14
lol

I was editing my post about David's cards as you were typing about it.



You are right though, looking in my folders, his card set was separate images and not an atlas. (for some reason I thought they were an atlas)

Here are the blanks he provided for using the method of manually adding the text... (as you can see, his blanks were far better looking than mine )

EDIT
I just realized that your ZIP is a different set of cards than the one I have.

I am sure that David will not mind if we share these as he shared with us, so courtesy of David Gervais, here is the other set of his cards that I dug out of my folders... (see attached ZIP)

Coding things my way since 1981 -- Currently using AppGameKit V2 Tier 1

Attachments

Login to view attachments

Login to post a reply

Server time is: 2024-11-24 12:38:08
Your offset time is: 2024-11-24 12:38:08