Advertisement
paul_nicholls

c64-multiplexer2.h

Jun 27th, 2023
899
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.84 KB | None | 0 0
  1. /// @file
  2. /// A flexible sprite multiplexer routine for 32 sprites.
  3. ///
  4. /// Usage:
  5. /// - Once:
  6. /// - plexInit(screen): Initialize the data structures and set the screen address
  7. /// Each frame:
  8. /// - Set x-pos, y-pos and pointer in PLEX_XPOS[id], PLEX_YPOS[id], PLEX_PTR[id]
  9. /// - set enabled, color and multicolor in PLEX_ENABLED[id], PLEX_COLOR[id], PLEX_MULTICOLOR[id]
  10. /// - plexSort() Sorts the sprites according to y-positions and prepares for showing them. This uses an insertion sort that is quite fast when the relative order of the sprites does not change very much.
  11. /// - plexShowSprite() Shows the next sprite by copying values from PLEX_XXX[] to an actual sprite. Actual sprites are used round-robin. This should be called once for each of the 24 virtual sprites.
  12. /// - plexFreeNextYpos() Returns the Y-position where the next sprite is available to be shown (ie. the next pos where the next sprite is no longer in use showing something else).
  13. /// - plexShowNextYpos() Returns the Y-position of the next sprite to show.
  14. //
  15. /// In practice a good method is to wait until the raster is beyond plexFreeNextYpos() and then call plexShowSprite(). Repeat until all 32 sprites have been shown.
  16. /// TODO: Let the caller specify the number of sprites to use (or add PLEX_ENABLE[PLEX_COUNT])
  17.  
  18. #include <c64.h>
  19.  
  20. /// The number of sprites in the multiplexer
  21. extern const char PLEX_COUNT;
  22.  
  23. /// The sprite enable for each multiplexer sprite; 0 = 0ff, 1 = on
  24. extern char PLEX_ENABLED[PLEX_COUNT];
  25.  
  26. /// The sprite color for each multiplexer sprite
  27. extern char PLEX_COLOR[PLEX_COUNT];
  28.  
  29. /// The sprite multicolor enable for each multiplexer sprite; 0 = hi-res, 1 = multicolor
  30. extern char PLEX_MULTICOLOR[PLEX_COUNT];
  31.  
  32. /// The x-positions of the multiplexer sprites (0x000-0x1ff)
  33. extern unsigned int PLEX_XPOS[PLEX_COUNT];
  34.  
  35. /// The y-positions of the multiplexer sprites.
  36. extern char PLEX_YPOS[PLEX_COUNT];
  37.  
  38. /// The sprite pointers for the multiplexed sprites
  39. extern char PLEX_PTR[PLEX_COUNT];
  40.  
  41. /// The address of the sprite pointers on the current screen (screen+0x3f8).
  42. extern char* volatile PLEX_SCREEN_PTR;
  43.  
  44. /// Indexes of the plex-sprites sorted by sprite y-position. Each call to plexSort() will fix the sorting if changes to the Y-positions have ruined it.
  45. extern char PLEX_SORTED_IDX[PLEX_COUNT];
  46.  
  47. /// Initialize the multiplexer data structures
  48. void plexInit(char* screen);
  49.  
  50. /// Set the address of the current screen used for setting sprite pointers (at screen+0x3f8)
  51. inline void plexSetScreen(char* screen);
  52.  
  53. /// Ensure that the indices in PLEX_SORTED_IDX is sorted based on the y-positions in PLEX_YPOS
  54. /// Assumes that the positions are nearly sorted already (as each sprite just moves a bit)
  55. /// Uses an insertion sort:
  56. /// 1. Moves a marker (m) from the start to end of the array. Every time the marker moves forward all elements before the marker are sorted correctly.
  57. /// 2a. If the next element after the marker is larger that the current element
  58. ///     the marker can be moved forwards (as the sorting is correct).
  59. /// 2b. If the next element after the marker is smaller than the current element:
  60. ///     elements before the marker are shifted right one at a time until encountering one smaller than the current one.
  61. ///      It is then inserted at the spot. Now the marker can move forward.
  62. void plexSort();
  63.  
  64. /// Show the next sprite.
  65. /// plexSort() prepares showing the sprites
  66. void plexShowSprite();
  67.  
  68. /// Get the y-position of the next sprite to show
  69. inline char plexShowNextYpos();
  70.  
  71. /// Prepare for a new frame. Initialize free to zero for all sprites.
  72. inline void plexFreePrepare();
  73.  
  74. /// Get the Y-position where the next sprite to be shown is free to use.
  75. inline char plexFreeNextYpos();
  76.  
  77. /// Update the data structure to reflect that a sprite has been shown. This sprite will be free again after 21 lines.
  78. inline void plexFreeAdd(char ypos);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement