Remove lag in AS3 tiling program - arrays

QUERY
I am creating a program that has a lot of tiles in the environment that are all movie clips. The player can move around in this environment. How can I hide the tiles that are off the screen to decrease lag?
The tiles are all in a 2D array that is 20 horizontal units by 10 vertical units.
Let me know if you have any suggestions!
MORE INFO
I have a Tile class for the tile, so I can add functions for removal within this. I'm just unsure how to go about it.
-Olin

Sounds like you're looking for how to do some more fine grained memory management by recollecting memory from tiles no longer on screen, in Flash or any other language that results in bytecode that is then run in a virtual machine that handles the low level memory management and garbage collection, your control is limited with regard to reclaiming memory. Your best bet in these instances is to use object pools to dynamically allocate the number of objects you need then retain them and simply mark them as unused when they're ready to be recycled.
Read up on object pooling in AS3 here:
http://help.adobe.com/en_US/as3/mobile/WS948100b6829bd5a6-19cd3c2412513c24bce-8000.html
or on working with the garbage collector here:
http://help.adobe.com/en_US/as3/mobile/WS4bebcd66a74275c3-576ba64d124318d7189-7ffc.html
All part of the top level (conserving memory):
http://help.adobe.com/en_US/as3/mobile/WS4bebcd66a74275c333637c44124318c9bf9-8000.html
even though this is all in the mobile dirctory I'm willing to bet the information is just as pertinent on the desktop.

Related

Huge array of libSDL textures

I am developing an app that presents the user with a potentially very large user-generated image gallery, 10 or so images at the time.
The app is to be implemented in C using libSDL and 2D textures for accelerated rendering.
The overall gist of it in pseudocode is:
while cycle < MAX_CYCLES
while i < MAX_STEPS
show a gallery of 10 image thumbnails
while (poll events)
if event == user has pushed next
break
i++
scramble image galleries using a genetic algorithm
cycle++
I could load every image from disk at initialization time, creating all the required textures, so image presentation is fast. But of course this would be slow and potentially allocate a huge array of textures.
I will scale down the images for presentation, so this could mitigate the problem, but the total size of the collection depends on user preference. Surely I can cap the maximum value, but it cannot be small.
I was thinking about unloading every unused image at every step of every cycle, using SDL_FreeSurface and SDL_DestroyTexture. This would mean reloading the data from disk, recreating the surface and recreating the texture each time. Is this a viable approach?
Also I understand that SDL textures are stored in GPU memory, so the amount of available memory on the card should be my main concern. Am I right?
In summary, is there a recommended method to deal with this type of situation?
I would keep always 3 slides in memory.
Prev - Current - Next
While presenting the current slide, preload the next slide and unload the slide no (Current - 2).
Also I understand that SDL textures are stored in GPU memory, so the amount of available memory on the card should be my main concern. Am I right?
Not quite, if the GPU (Driver) seems it necessary, it will outsource unused texture data to RAM.
For Example, if you're presenting 10 Images and thus have 30 Images present in memory, then for 2K (with alpha) (1920 x 1080 x 4) you will need approx. 250 MB.
As long as you don't run on an embedded system (or very old, outdated system), this shouldn't be a big concern.

replacing CALayer arrays with CAMetalLayer arrays

I have a painting app which at any given time interactively shows content from an array of 200 or so CALayers via an UIImageView. I get reasonable performance, but I'm wondering if there could be any performance benefits with using CAMetalLayers instead. In particular, I'm curious if I could benefit from blitting textures directly to each CAMetalLayer, and would there be any hardware considerations with stacking/displaying so many CAMetalLayers at once.
Are there any gotchas I should consider before implementing, and should I continue using an UIImageView (or other) to host these newly Metal-backed sublayers? Any thoughts would be appreciated.
That’s not going to work. You should be keeping track of your stroke’s data. For example an array of points would be a single stroke and then you should have an array of those strokes. It could be only points (x, y) or more probably also containing color, size and other variables. You should know what do you need to describe your stroke.
Then use that to draw (stamp at those locations). When you want to undo, just start drawing from the beginning all the strokes in the array until n-1, n-2, etc...

Big SCNGeometry SceneKit for iOS

I am working on a cocoa/iOS projet.
I have a common swift class which manage a Scenekit scene.
I want to draw a big terrain (about 5000x5000 points).
I have 2 triangles per 4 points. I have created a scngeometry object for the whole terrain (is it a good thing ?)
I decided to store those points in a 6-Float structure (x,y,z and r,g,b). I tried to create an empty array or to allocate a big array at the begining : i got the same issue.
I work with Int datatype for indices array.
The project works fine on Cocoa but i get memory errors on iOS. I think this is because of the need to have a big and contigous array for vertex.
I tried to create several chunks of geometry objects but scene kit does not like if we erase a previous buffer.
What is the best practice in this case ?
Is there a way to store vertex on the mass storage instead of memory arrays/buffers ?
Thanks
So...twice as many terrain points as there are pixels on a shiny new 5K display? That's a huge amount of memory to be using at once on iOS. And you won't be able to see that resolution on an iOS device.
So how about:
Break your 25 million pixel terrain into smaller tiles, each in its own SCNNode. Loop through the tiles, create one SCNNode, throw away the 6-Float array for that tile and move to the next.
Use SCNLevelOfDetail to produce much simpler versions of those nodes, for display when they're very far away.
Do the construction work on OS X. Archive your scene (NSSecureCoding). Bundle that scene into the iOS app.
Consider using reference nodes in your main SCNScene, and archive each tile as a separate SCNScene file.
Hopefully you're already using triangle strips, not triangles, to build your geometry.

Tracking moving Objects using classes

This is a Theoretical question about the use of classes, something which I haven't yet had too much experience. I just want some opinions about whether this would be the best way to go about programming for my task.
The goal of my project is to track objects that move across the frame of the camera, I have been able to detect the objects and store their centre point for each frame (note that this point is only stored until the next frame is read and new points are stored).
I am thinking of using a class store the previous positions of the objects as well as an ID of some kind so that I can differentiate the objects from each other.
Would this be over complicating things? or would it be better to simply store the history points into a basic array of points?
For some more insight, I will be tracking people from an overhead counter, so realistically there shouldn't be more than 15 objects present in the frame at one time. The images below show the detected heads and their centre points (these are the objects being tracked).

Tile based game theory

I'm looking for articles on tile based games, like the old ultima 6&7, or even puzzle pirates. Specifically:
How they keep track of objects on the map. Objects such as other characters, or trees, or things the character can move.
AI behind the characters. How the game handles character behavior for
characters on the map that are off screen. Especially with very large maps and numerous characters.
I remember checking out Amit's Game Development page back when I wrote some games. He has a great sub-section on tiles that has most of what you want.
You could look through back issues of Game Developer magazine to see if something addresses what you're asking in detail.
For (1) the easiest way of dealing with a tile-based map where each tile can contain multiple objects is to just have a big multidimensional array of structs representing each tile. The struct contains a pointer to the head of a linked list representing all the objects in that tile. This is very memory efficient and lets you quickly find everything in a certain tile while also enumerating them along some other axis (eg, owner, allocation arena, etc).
RogueBasin is devoted to Rogue-like games (e.g. Rogue, NetHack, ). All of those games were based on a simple square grid. The site has an extensive section on developing games like that: http://roguebasin.roguelikedevelopment.org/index.php?title=Articles
You will find both suggestions and code there which could be used to build a game like you describe. After all, the only real difference between Rogue/Larn/NetHack/etc. and Diablo or the Ultima series is using simple text characters to depict the map and gameplay vs. isometric sprites.
In particular you will find information about calculating the area illuminated by a torch or lantern the user is carrying, data structures for storing maps, algorithms for automatic generation of maps, and lots of notes for how different games which have already been written chose to address these problems.
Check out Gamasutra. They have loads of articles for all kinds of game development.
The map would be an array of values. It could be divided into discrete parts. Only parts in range of the player would be loaded and the objects & npc in these parts active.
Since the old hardware had very limited memory and cpu, those games would only be able to load and process parts of the maps.

Resources