| |
mhindsbo
Registered: Dec 2014 Posts: 51 |
Code optimization
I thought I would tap into the creativity here to see if you have optimization suggestions for the following.
For my game I have a list of objects. When an object spawns I set bit 7 in the first byte of the table so I dont spawn it again while it is active (or if it is destroyed). If the object leaves the screen without being destroyed I flip the bit back again.
Each active objects stores the address of its location in the spawn table and uses the following code to 'reactivate' itself if/when it leaves the screen.
lda object_d6,x ; lo byte of address
sta tempz+0 ; location in zero page
lda object_d7,x ; hi byte of address
sta tempz+1
ldy #0
lda (tempz),y
and #%01111111 ; clear bit 7
sta (tempz),y
That is 29 cycles +3 if page boundary is crossed. Any optimization ideas?
I could of course just add another byte in the object table so I dont have to set/clear a bit, but that adds potentially hundreds of bytes ekstra for a given level. |
|
... 13 posts hidden. Click here to view all posts.... |
| |
Copyfault
Registered: Dec 2001 Posts: 478 |
If I get the initial problem right it is not only about those ~30 active objects but about how to cope with information on any object (be it active or not at a specific moment).
Thus, if there will be>$100 objects in total, also the initial code will come short as this would force to use more than 8 bits for the index register. If 9 Bits suffice this should be done with a case distinction, i.e.
ldx #obj_nr
lda upperbitlist,x
bne OBJECTS_0x100
OBJECTS0x00
lda isobjectactive_0x00,x
..etc..
OBJECTS0x100
lda isobjectactive_0x100,x
..etc..
If possible, use the upperbitlist such that it holds the hi_byte for the information fetch in question, i.e.
ldx #obj_nr
lda upperbitlist,x
sta OBJECTS_LD+2
OBJECTS_LD
lda isobjectactive,x
..etc..
(does maybe have to combined with an ORA as most probably there'll be different such tables)
Last but not least: is it mandatory to use bit7 for the "isactive"-information? If not, it might be a bit tighter to use bit0 and set/clear the active-information with an INC/DEC.
Depends on a lot of other things, so these are just loose ideas... |
| |
cadaver
Registered: Feb 2002 Posts: 1160 |
Is the object activation / deactivation a bottleneck even? Depends on the enemy layouts, but I'd imagine only a few objects leaving the screen per frame (and most often, none)
I have typically done it so that when I put an object onto the screen, I zero the object type byte from the spawn data, meaning "no object." Then, if the object leaves the screen without being killed, I store its type back. Therefore no bit manipulation needed. |
| |
mhindsbo
Registered: Dec 2014 Posts: 51 |
You are right. Activation and deactivation is most likely not a bottleneck as only one or two will happen each frame. It just felt that there should be a faster method. From an object handling point og view the original is actually fairly 'elegant'.
And yes the issue is not the subset of active objects, but the total list with more than 256 objects potentially. |
| |
ChristopherJam
Registered: Aug 2004 Posts: 1409 |
I'm a bit confused about the number of objects thing.
Surely even the original question presupposes no more than 256 objects, as their addresses are stored in the object_d6/object_d7 tables, which are just indexed by X?
And I'm not seeing how Copyfault's suggestion gets past that, unless the upperbitlist table is used to dynamically select for each object ID which of two possible objects it might refer to.
In either case, moving past 256 objects while still storing object ID in X is going to need some kind of system for managing a smaller subset of potentially active ones.. |
| |
cadaver
Registered: Feb 2002 Posts: 1160 |
In the original code, the X indexed variables are for currently active (onscreen) objects, of which there are much less than 256. The d6 / d7 variables point into an arbitrarily sized object spawn data. |
| |
Oswald
Registered: Apr 2002 Posts: 5095 |
btw its hard to imagine a c64 game with hundreds (>256) npcs.. :) |
| |
mhindsbo
Registered: Dec 2014 Posts: 51 |
The x indexed is active object data tables (x, y, image, ...) that are sorted etc. for sprite multiplexor.
hundreds of objects is easy ;-) Aviator Arcade has several hundreds of tanks, planes, etc. per level. that get spawned at specific locations on the map. |
| |
mhindsbo
Registered: Dec 2014 Posts: 51 |
Thanks for the input. Discussing here made me restructure my data format.
Each tile row on the map now simply has a start index and an end index of object to spawn. If start and end index is the same number is means no objects to spawn on that row.
I will limit the number of objects for any given level to 265. And use the suggestion to change object type to 0 once it is spawned.
significantly simpler, smaller and faster.
Thanks again for all the input. |
| |
ChristopherJam
Registered: Aug 2004 Posts: 1409 |
Quoting cadaverIn the original code, the X indexed variables are for currently active (onscreen) objects, of which there are much less than 256. The d6 / d7 variables point into an arbitrarily sized object spawn data.
Ah! That makes sense, thank you.
mhindsbo, glad we could help :) |
| |
Copyfault
Registered: Dec 2001 Posts: 478 |
Quoting ChristopherJam[...]
And I'm not seeing how Copyfault's suggestion gets past that, unless the upperbitlist table is used to dynamically select for each object ID which of two possible objects it might refer to.[...]
I did not write out all the details, but you're right, I had some kind of dynamic link list in mind. Ofcourse this does not really widen the length of the index value (X-reg is still in gear) but this rather gives a possibility to choose between "sub"-objects.
For a proper 9-bit-index-value, it should work to use another register (e.g. a zp-reg) as a highbyte for the index-value and use this just like upperbitlist in my former code examples, just without any index! Downside will certainly be the code doubling to choose between the different tables used lateron, but with self-modification of the code this can be worked around I think.
@mindsbo: glad to read that you found a data structure for your need:) Looking forward to seeing what kind of game comes out of it!!! |
Previous - 1 | 2 | 3 - Next |