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 2: Calling out for some best programming pratices in AGK.

Author
Message
ThrOtherJoJo
12
Years of Service
User Offline
Joined: 24th Mar 2012
Location: California
Posted: 5th Dec 2012 00:37 Edited at: 5th Dec 2012 00:40
I have a simple app which only has three screens but I have about 20 sprites that I'm using as buttons saved out as .png files with transparency turned on.
I also have 5 virtual buttons that uses images with the alpha set to 255.

The background are created at 720x1280 at 96 dpi.
Now all this app does is play sound bites in which I load all thirteen mp3 files in memory with each mp3 file as an object.

The problem is after I start the program and navigate throughout the screens and pressing the virtual button to play the files; about 2 minutes in, the program slows down.

When I click on the button it takes a second to respond, whereas when I first started it was very responsive.

What I did to not keep things in memory was whenever I would leave a screen I would delete the sprites from that screen and create the new sprites for the screen I wanted to go to. And then when going back, delete the sprites from that screen and then create the sprites again for the new screen.

It seems that you have to do it this way in Tier 2. Whenever I tried to create all the sprites in the Begin function they wouldn't show up when I hit the loop unless I created the sprite again.

But anyway, what are your thoughts. Its a very simple program with no animation. I'm starting this way to get myself acclimated with agk before starting bigger projects.

But do you think I should create the virtual buttons when I need them instead of adding them in the Beginning and then deactivating them and turning off the visiblity when I don't need them?
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 5th Dec 2012 04:30
It would help to see your code.

If, as has been seen elsewhere, you are inadvertently creating the same sprite repeatedly (without deleting), or loading images repeatedly, or any of a number of other practices that log up memory, then we need to see what you are doing to tell.

I don't load music in my app, but I do create (and delete) lots of sprites each round, many with transparency. My app does not get slower as it goes. I do use virtual buttons (invisible ones) in front of sprites for my menus, options, etc.. And it doesn't seem to cause issues creating and deleting as needed.

Cheers,
Ancient Lady
AGK Community Tester
ThrOtherJoJo
12
Years of Service
User Offline
Joined: 24th Mar 2012
Location: California
Posted: 5th Dec 2012 07:11 Edited at: 5th Dec 2012 07:41
Here is some code



How is the DeleteSprite and the DeleteImage functions suppose to work? After the pass I check those variable it doesn't seem like the image has been released.
XanthorXIII
AGK Gold Backer
13
Years of Service
User Offline
Joined: 13th May 2011
Location:
Posted: 5th Dec 2012 15:26
A couple of observations. I see Sync called multiple times in Begin. I believe that method is just for setup and not for displaying. The loop is where you want to call sync and only once. You also would probably benefit from creating some functions to handle your button, sprite and whatever else creation and then return the id or in value to your main. Cleaning up your code would give you the biggest benefit.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 5th Dec 2012 15:52
The code is clearly not your complete code. But I get a feel for what you are doing.

I suspect your usage of the virtual buttons is fine. I don't use them the same way.

I am assuming that GraphicImages() is loading all your images (other than the start up ones). And that PlayIsm is playing the music somehow and I wonder what the rndNum does.

In MainScreen(), since only one of the button events is going to have occurred each cycle, you probably want to do a return in each of your if statements. This prevents checks that aren't necessary. Or turn all but the first 'if' into 'else if'. (Return is probably faster.)

The DeleteSprite and DeleteImage functions don't change the variables passed in at all. They just use the value to do the function indicated.

For sprites/images that are always loaded, I use fixed ids when I create them instead of letting AppGameKit generate them (like for buttons and backgrounds and things).

It's also a good idea to create functions to do the deletes that check to see if the passed id is greater than zero and that the sprite/image exists before deleting it. And always use those functions to do the deletes. This way you make sure that you don't run into an error if the code happens to try to do a double delete.

I don't see anything that looks like it would cause slowdowns as time goes by (but it might be in some other bit of code). But my suggestions for the MainScreen function might help. If your other functions do the same kind of button checking, apply the same fix to them. In general, in any one function, if you are checking for multiple states and only one is possible, make them 'if .. else if ..' or do returns, if nothing else is done after the checks.

Cheers,
Ancient Lady
AGK Community Tester
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 5th Dec 2012 15:52
Also, what version of AppGameKit are you using?

Cheers,
Ancient Lady
AGK Community Tester
bjadams
AGK Backer
16
Years of Service
User Offline
Joined: 29th Mar 2008
Location:
Posted: 5th Dec 2012 16:00
XanthorXIII he's calling Sync in BEGIN just to display a startup logo or splash screen
ThrOtherJoJo
12
Years of Service
User Offline
Joined: 24th Mar 2012
Location: California
Posted: 5th Dec 2012 20:49 Edited at: 5th Dec 2012 20:52
Quote: "The code is clearly not your complete code. But I get a feel for what you are doing."

Correct. I didn't post it because the other two function operate pretty much the same way. And the random function just picks a random number to know which music file to call.

I will try what you suggested, but the main culprit I found out was the background images. When I took those out I didn't notice any slow response time when I click buttons after the program ran a bit.

My 3 background images totalled to 1MB each and where 720x1280 in size. They were png files and I forgot to turn off the transparency. I have changed them to jpegs and I'm only going to load one background image and place the other images on top of that.

That takes the size of the image done to 170KB.

And the version I'm using is 1.07.


Quote: "XanthorXIII he's calling Sync in BEGIN just to display a startup logo or splash screen"


That's correct. I'm creating a small animated loading screen.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 5th Dec 2012 21:39
Quote: "They were png files and I forgot to turn off the transparency."

Yup, turning off transparency can make a difference. But you shouldn't need to change them from png to jpeg. Unless the file is physically smaller.

Quote: "And the random function just picks a random number to know which music file to call."


Then I assume PickRandomNumber() only returns a valid number.

Quote: "And the version I'm using is 1.07."

Which? The current and most stable is v1076.

Good luck with the project and Happy Programming!

Cheers,
Ancient Lady
AGK Community Tester

Login to post a reply

Server time is: 2024-11-24 03:45:30
Your offset time is: 2024-11-24 03:45:30