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 / [Tier 1] Display score using sprites. (save some memory)

Author
Message
Cliff Mellangard 3DEGS
Developer
18
Years of Service
User Offline
Joined: 20th Feb 2006
Location: Sweden
Posted: 10th Feb 2013 23:16 Edited at: 10th Feb 2013 23:34
I made this for fun as i used a similar function in dbp by another author.

You can save some precious memory by using this the right way.



The image used.


Edited....
Fixed a small bug

Android 2.3 , ZTE Skate , 480x800 , 800 mhz , Samsung Galaxy Y , 240x320 , 832 mhz , Sony ericson arc 480x854 , 1 ghz
Android 4.0 , Dmtech 3g 9738B , 1024x768 , 9.7 inches , 1.2 ghz

Attachments

Login to view attachments
TrezSoft
AGK Tool Maker
11
Years of Service
User Offline
Joined: 8th Nov 2012
Location: UK
Posted: 10th Feb 2013 23:24
Cliff, Currently I load a small font image with only the numbers and a sub images file. Does your method save more memory that this method ?

Font Attached
All other entries from 32 to 46 and 61 to 127 are set to 0:0:1:1

47:0:0:1:1
48:0:0:47:90
49:47:0:47:90
50:94:0:47:90
51:141:0:47:90
52:188:0:47:90
53:0:90:47:90
54:47:90:47:90
55:94:90:47:90
56:141:90:47:90
57:188:90:47:90
58:0:0:1:1
59:0:0:1:1
60:0:0:1:1

Attachments

Login to view attachments
Cliff Mellangard 3DEGS
Developer
18
Years of Service
User Offline
Joined: 20th Feb 2006
Location: Sweden
Posted: 10th Feb 2013 23:28 Edited at: 10th Feb 2013 23:29
Quote: "Cliff, Currently I load a small font image with only the numbers and a sub images file. Does your method save more memory that this method ?

Font Attached
All other entries from 32 to 46 and 61 to 127 are set to 0:0:1:1 "

I belive yours is as good as mine

I simply skipped the use of an subimages text file

The only thing is that your function could be larger as you grabb an ascii number ?

Your function neads to get the image coords from the text file that could be slower?

Android 2.3 , ZTE Skate , 480x800 , 800 mhz , Samsung Galaxy Y , 240x320 , 832 mhz , Sony ericson arc 480x854 , 1 ghz
Android 4.0 , Dmtech 3g 9738B , 1024x768 , 9.7 inches , 1.2 ghz
TrezSoft
AGK Tool Maker
11
Years of Service
User Offline
Joined: 8th Nov 2012
Location: UK
Posted: 10th Feb 2013 23:35
I went this route because in some cases I need seperators in the numbers formats : , etc. But looking at your solution it may be faster. Ill give it a try when I have time to see what the difference is. Thanks for the snippet
Cliff Mellangard 3DEGS
Developer
18
Years of Service
User Offline
Joined: 20th Feb 2006
Location: Sweden
Posted: 10th Feb 2013 23:38
Quote: "I went this route because in some cases I need seperators in the numbers formats : , etc. But looking at your solution it may be faster. Ill give it a try when I have time to see what the difference is. Thanks for the snippet "

Its preatty easy to add dots or seperators to my function
I only neaded one to display score in integers

Android 2.3 , ZTE Skate , 480x800 , 800 mhz , Samsung Galaxy Y , 240x320 , 832 mhz , Sony ericson arc 480x854 , 1 ghz
Android 4.0 , Dmtech 3g 9738B , 1024x768 , 9.7 inches , 1.2 ghz
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 11th Feb 2013 16:26
Seems like we have similar ideas. Let me know if I can improve the article I wrote with any of your code techniques.

DVader
20
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 11th Feb 2013 16:37
Hmm, how does this save memory from just using a text font? I have used sprites to display text many times in Dark Basic, but haven't bothered yet in AppGameKit because the fonts are handled better in AGK.
Perhaps it may save memory if you planned on using an entire new font for the high score, and don't need text characters. Although I tend to use the same font for name etc as I do for the score display. If you use ascii codes you can go the whole hog and replace the keyboard as well

TrezSoft
AGK Tool Maker
11
Years of Service
User Offline
Joined: 8th Nov 2012
Location: UK
Posted: 11th Feb 2013 16:50
@BatVink , I knew I had read this somewhere just could not remember. So thanks for the great article.

In a lot of projects I have had to use differnt fonts for Timers, Scores etc so it does save a lot of memory and loading time. Also
I found that when using Very Large fonts (to keep them sharp) for scores etc the resulting font image would be over 1024 * 1024 when all the charcters are included. By using either method only the required characters are required in the font image.
DVader
20
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 11th Feb 2013 17:23
But what about name, etc? A high score, is more than a just number, is my point. If you plan on using different fonts for numbers and text, possibly. If you only use one font however, you have actually used more memory with this than a single font anyway. Of course I do see the point, as I have done this myself in DB Pro as I said, but rather for making the game look better, than saving memory.
A way to save memory on audio would be more useful As that is the main culprit as far as I can see for most my apps so far. A midi player Cliff?

Cliff Mellangard 3DEGS
Developer
18
Years of Service
User Offline
Joined: 20th Feb 2006
Location: Sweden
Posted: 11th Feb 2013 17:31
Quote: "Seems like we have similar ideas. Let me know if I can improve the article I wrote with any of your code techniques."

Will do if i find anything to improve
Yours are a bit more advanced and mine is simply for numbers 0-9
Iam soon back to hospital so i dont know when ?
Quote: "Hmm, how does this save memory from just using a text font? I have used sprites to display text many times in Dark Basic, but haven't bothered yet in AppGameKit because the fonts are handled better in AGK.
Perhaps it may save memory if you planned on using an entire new font for the high score, and don't need text characters. Although I tend to use the same font for name etc as I do for the score display. If you use ascii codes you can go the whole hog and replace the keyboard as well "

Its mainly the chance of using a smaller image that consumes memory for score display etc.
a bitmap font can be 320-1024 *2 in size.
And if you only nead the numbers why use memory with the rest
Even if you set the font to be 16x16 so will the memory usage be of the whole image.
Its pretty much as trezsoft states
Quote: "In a lot of projects I have had to use differnt fonts for Timers, Scores etc so it does save a lot of memory and loading time. Also
I found that when using Very Large fonts (to keep them sharp) for scores etc the resulting font image would be over 1024 * 1024 when all the charcters are included. By using either method only the required characters are required in the font image. "


Android 2.3 , ZTE Skate , 480x800 , 800 mhz , Samsung Galaxy Y , 240x320 , 832 mhz , Sony ericson arc 480x854 , 1 ghz
Android 4.0 , Dmtech 3g 9738B , 1024x768 , 9.7 inches , 1.2 ghz
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 11th Feb 2013 17:52
My game is probably a good example of where it is useful. I am creating bingo balls, you only ever need the numbers. The numbers need to be clear at 256x256, which would be a phenomenal use of resources for a full alphabet + numbers + symbols.

AgentSam
12
Years of Service
User Offline
Joined: 14th Mar 2012
Location: Virtual Space
Posted: 12th Feb 2013 04:24 Edited at: 12th Feb 2013 06:55
By looking at the functions in the OP Code Snippet, I think there are a few possible pitfalls to be observed.

function Create_Number_Sprites(id)
* Only reserves room for 6 sprites for digits.

Function int_numbers_display(x,y,score,id)
* Will fail when the "score" parameter contains a value that has more than 6 digits.
* It will also fail to remove previous digits, when the value changes from larger to smaller (eg. a change from 123456 to 123 will still leave the digits 4, 5 and 6 visible).

Of course, I think Cliff intended these as simplified examples, not as complete functions ready for use.

Cheers,
AgentSam
AgentSam
12
Years of Service
User Offline
Joined: 14th Mar 2012
Location: Virtual Space
Posted: 12th Feb 2013 06:40 Edited at: 12th Feb 2013 08:45
Just for kicks, I recreated the digits from the OP as a bitmap font, using a texture atlas that is HALF (128x64) the size of the original (128x128), but contains the same digits at the original resolution - while also including a space, colon, and the period.

It was a fun little test... Here's the code snippet:



And here's the new font bitmap:


The following post contains a package with the full project in ready to compile form (tested with AppGameKit 1076).

By using text objects instead of individual sprites, we gain the following advantages:
* We can add as many, or as few, characters in the bitmap font as we like, and they will be handled automatically with no extra code.
* With a single call to SetTextSize, SetTextSpacing, etc, we can change the appearance of the entire text object.
* Bitmap fonts can be created to utilize either variable width or fixed width letters/digits - with no changes in code; only the font bitmap definition (subimages.txt) needs to change.
* All of the low-level overhead of creating, showing, positioning and hiding individual letters is handled automatically by the text object.

But as there are always exceptions, the method that uses individual sprites would be better in some cases. It would certainly be better when advanced text animation is required -- such as non-linear motion (for example, when digits need to shoot from the sides, rain from the top, or spiral down into view or out of the view, etc.)

Cheers,
AgentSam

EDIT:
As per the documentation page here: SetTextDefaultFontImage, I have noticed that the subimages.txt file does not need to list ALL characters, when only a subset is required. So it could be trimmed down quite a bit, when only the digits are required. (But the project link below contains the untrimmed version.)

Here's the relevant piece of information from the doc page: "The AppGameKit will look for images "32" up to and including "127" in the subimages file and any not found will default to the space character (32). "

Attachments

Login to view attachments
AgentSam
12
Years of Service
User Offline
Joined: 14th Mar 2012
Location: Virtual Space
Posted: 12th Feb 2013 06:40 Edited at: 12th Feb 2013 06:42
Previous project as a downloadable package in the download button.

Attachments

Login to view attachments
Cliff Mellangard 3DEGS
Developer
18
Years of Service
User Offline
Joined: 20th Feb 2006
Location: Sweden
Posted: 12th Feb 2013 10:07
Quote: "By looking at the functions in the OP Code Snippet, I think there are a few possible pitfalls to be observed.

function Create_Number_Sprites(id)
* Only reserves room for 6 sprites for digits.

Function int_numbers_display(x,y,score,id)
* Will fail when the "score" parameter contains a value that has more than 6 digits.
* It will also fail to remove previous digits, when the value changes from larger to smaller (eg. a change from 123456 to 123 will still leave the digits 4, 5 and 6 visible).

Of course, I think Cliff intended these as simplified examples, not as complete functions ready for use."

Its a simpel demostration to show how to use sprites for digits.
Thats why there is not more then 6 digits in the sample.
And to avoid numbers showing so do you simply add an setspritevisible 0 command first in the display function.
As the amount should never get higher then 8 digits in an normal game so is the loop to do this extremely small
Sometimes do i post small snippets to get feedback on how others do it as its fun and learning in many ways.

Android 2.3 , ZTE Skate , 480x800 , 800 mhz , Samsung Galaxy Y , 240x320 , 832 mhz , Sony ericson arc 480x854 , 1 ghz
Android 4.0 , Dmtech 3g 9738B , 1024x768 , 9.7 inches , 1.2 ghz
AgentSam
12
Years of Service
User Offline
Joined: 14th Mar 2012
Location: Virtual Space
Posted: 12th Feb 2013 18:18
Quote: " as its fun and learning in many ways"


As it was in this case aswell.

Cheers,
AgentSam

Login to post a reply

Server time is: 2024-05-07 17:05:28
Your offset time is: 2024-05-07 17:05:28