home *** CD-ROM | disk | FTP | other *** search
/ Loadstar 229 / 229.d81 / t.sprites < prev    next >
Text File  |  2022-08-26  |  9KB  |  349 lines

  1. u
  2.     S P R I T E S   A R E   F U N
  3.            By Dave Moorman
  4.  
  5.  
  6.     This article is for the Beginner
  7. C-64 programmer who is ready to
  8. become an Intermediate. You have
  9. BASIC down, and want to get into some
  10. of the cool stuff our sweet little
  11. computer can do. Like sprites.
  12.  
  13.     "Sprite" is not the legal term.
  14. Atari, Apple or TI trademarked the
  15. term before Commodore could get
  16. around to it. The official term is
  17. "moveable objects." Whatever. But
  18. when it comes to sprites, no one does
  19. it better than a C-64.
  20.  
  21.     Sprites are small bitmap images
  22. which can appear on top of (or
  23. behind) screen characters without
  24. messing up the screen data. They are
  25. part of the hardware functions of the
  26. VIC II -- Video Interface Chip. The
  27. VIC II was created in 1981 to be the
  28. Mother of All 8-bit Video Devices,
  29. and became one of the reasons the
  30. Commodore 64 was designed and on the
  31. market in just three months.
  32.  
  33.     The VIC II has a ton of features:
  34.         40-column text screen
  35.         320x200 pixel High Resolution
  36.             Bitmap Graphics
  37.         160x200 (double-wide) pixel
  38.             Multi-color Bitmap
  39.             Graphics
  40.         16 colors -- with up to four
  41.             colors in each character
  42.             cell
  43.         Raster Interrupts for split
  44.             Screens
  45.         Light Pen capable
  46.         8 Sprites
  47.  
  48.     Being 1981 technology, the VIC II
  49. has limitations. But remember, it is
  50. a 4 Megahertz co-processing chip
  51. which must share memory with the 1
  52. Megahertz 6510 micro-processor. What
  53. it accomplishes is pure magic.
  54.  
  55.     Like sprites. If you have ever
  56. read what a programmer has to do to
  57. get moving objects on a VGA screen,
  58. you will certainly appreciate how
  59. easy sprites are to program. However,
  60. the programming is hardware based,
  61. involving many of the 47 registers of
  62. the VIC II chip. (A register is a
  63. memory location inside the C-64 which
  64. controls a co-processor such as the
  65. VIC II. When you POKE53281,0 to get a
  66. black background, you have just put 0
  67. into one of the VIC II registers.
  68.  
  69.     Sprites require quite a bit of
  70. set-up. An ML module such as
  71. JoySprite greatly helps in
  72. controlling sprites -- and even adds
  73. a number of features through clever
  74. Machine Language programming.
  75.  
  76.     Here we will look at sprites
  77. themselves -- and use some of the
  78. registers to make, display, and move
  79. sprites around the screen. The
  80. problem for programmers is that in
  81. their rush to get the C-64 out the
  82. door, Commodore included BASIC 2.0,
  83. which was actually designed for the
  84. PET computer some five years before.
  85. No BASIC commands were added for
  86. sprites (or sound).
  87.  
  88.     However, BASIC 2.0 does have PEEK
  89. and POKE, which allow use to directly
  90. access memory and registers. Which we
  91. will now do.
  92.  
  93.     A sprite must have an image -- a
  94. 24x21 pixel bitmap. In high
  95. resolution mode, the On pixels can be
  96. any one color. The Off pixels are
  97. transparent, showing whatever is on
  98. the screen behind the sprite. So our
  99. first step is to create a sprite. A
  100. [simple] sprite, that is. You can do
  101. all this in immediate mode, or write
  102. the code as program lines and RUN
  103. your program. We will just show the
  104. code.
  105.  
  106.  FOR X=0TO63:POKE832+X,255:NEXT
  107.  
  108.     This FOR-NEXT loop POKEs 255's
  109. into memory locations 832 to 895. In
  110. binary digits, 255 = 11111111. With a
  111. bitmap, every bit (BInary digiT) that
  112. is on or 1 becomes a pixel on the
  113. screen. So here we have created a
  114. block of on pixels.
  115.  
  116.     Now a sprite must know what image
  117. to display. Sprite images are
  118. numbered 0 to 255, and point to a
  119. memory location divided by 64. That
  120. means that the sprite image at 832 is
  121. image number 832/64 or 13. (We have 3
  122. easy to use locations for sprites at
  123. 832, 896, and 960 -- numbered 13, 14,
  124. and 15 respectively. See "Build a
  125. Boot" for move about making room for
  126. more sprites.)
  127.  
  128.     The sprites themselves are also
  129. numbered -- 0 through 7. So we will
  130. put image 13 in sprite 0.
  131.  
  132.  POKE2040,13
  133.  
  134.     The sprite image pointer bytes
  135. begin 1016 bytes after the beginning
  136. of the text screen. For the default
  137. text screen, that means 2040-2047
  138. hold the sprite image numbers for
  139. sprites 0-7.
  140.  
  141.     So far, so good. Now we have to
  142. turn on the sprite. The sprite enable
  143. register (53269) uses bit-logic to
  144. tell which sprites are on or off.
  145.  
  146.  Bit:   7 6 5 4 3 2 1 0
  147.  M:     0 0 0 0 0 0 0 0
  148.  
  149.  To turn on Sprite 0, we must set bit
  150. 0. (Do you see a pattern here?) That
  151. would be:
  152.  
  153.  Bit:    7   6   5  4  3  2  1  0
  154.  M:      0   0   0  0  0  0  0  1
  155.  Values:128 64  32 16  8  4  2  1
  156.  
  157.     If you add up the Values of the
  158. set bits, you will have the decimal
  159. value to POKE into 53269. In this
  160. case it is easy: 1.
  161.  
  162.     But where is the sprite? We have
  163. not positioned it on the screen yet.
  164. Each sprite has an X and Y location,
  165. with the values in the following
  166. memory locations:
  167.  
  168.       53248   Sprite0 X
  169.       53249   Sprite0 Y
  170.       53250   Sprite1 X
  171.       53251   Sprite1 Y
  172.       53252   Sprite2 X
  173.       53253   Sprite2 Y
  174.       53254   Sprite3 X
  175.       53255   Sprite3 Y
  176.       53256   Sprite4 X
  177.       53257   Sprite4 Y
  178.       53258   Sprite5 X
  179.       53259   Sprite5 Y
  180.       53260   Sprite6 X
  181.       53261   Sprite6 Y
  182.       53262   Sprite7 X
  183.       53263   Sprite7 Y
  184.  
  185.  You don't have to remember every
  186. number. The X-coordinate POKEd with
  187.  
  188.  X- POKE 53248 + SP# * 2, location
  189.  Y- POKE 53249 + SP# * 2, location
  190.  
  191.  For our purposes, we will put the
  192. sprite at 100x100:
  193.  
  194.  POKE 53248,100:POKE 53249,100
  195.  
  196.     And there it is! OK, it is just a
  197. block of color. But you did it
  198. yourself, right!
  199.  
  200.     We have one little problem with
  201. the X-coordinate. The screen is 320
  202. pixels across. Moreover, sprites can
  203. hide behind the borders, so the first
  204. full position on the screen is 24.
  205. Try it!
  206.  
  207.     Now try POKE 53248,320. It does
  208. not work. A byte can only hold a
  209. value of 0 through 255. And we need
  210. at least 344 to put the sprite behind
  211. the right border. What we need is
  212. just one more bit (binary digit).
  213. Then we can use values 0-511.
  214.  
  215.     The designers didn't have room on
  216. the chip for eight more registers, so
  217. they had to use a bit-logic byte for
  218. the high bit of the X-coordinate --
  219. 53264. This works just like the
  220. sprite enable byte. For Sprite 0,
  221. this is not too hard. You have to
  222. divide the X-coordinate into low and
  223. high bytes.
  224.  
  225.     When I am programming and need
  226. LO/HI divisions, I write a couple of
  227. DEFined FuNctions:
  228.  
  229.  10 DEF FNL(X)=X-FNH(X)*256
  230.  20 DEF FNH(X)=INT(X/256)
  231.  
  232.     Here is a clunky way to do it in
  233. immediate mode:
  234.  
  235.  X = 300
  236.  XH = INT(X/256)
  237.  XL = X-XH*256
  238.  POKE 53248,XL
  239.  POKE 53264,XH
  240.  
  241.     Since we are working with Sprite
  242. 0, we are only dealing with bit 0 in
  243. 53264. When you are working with more
  244. sprites, use JoySprite -- which does
  245. all the grunt work!
  246.  
  247.     Now for the color. The color
  248. registers begin at 53287. So, POKE
  249. 53287+Sprite#,Color will do the
  250. trick. For Sprite 0, just POKE
  251. 53287,7 to turn your sprite yellow.
  252.  
  253.     Multi-color (another bit-logic
  254. byte at 53276) will turn any or all
  255. sprites to multi-color mode. To do
  256. this, two bits are used for each
  257. pixel, which is then twice as wide as
  258. a high resolution pixel. The two bits
  259. give each sprite a possibility of
  260. three colors (and transparent).
  261.  
  262.      00 - Transparent
  263.      01 - Color 1   53285
  264.      10 - Color 2   53287+Sprite#
  265.      11 - Color 3   53286
  266.  
  267.     All sprites share 01 and 11, with
  268. the color registers as shown above.
  269. Only Color 2 (01) is peculiar to the
  270. particular sprite.
  271.  
  272.     To see multi-color at work, do
  273. this:
  274.  
  275.  FOR X=0TO63:POKE832+X,16+8+2+1:NEXT
  276.  
  277.  This will create three sets of four
  278. vertical bars -- 00, 01, 10, and 11
  279. (in that order). You can then POKE
  280. the registers with various color
  281. codes.
  282.  
  283.     Sprites are not all that big --
  284. 24x21 pixels on a 320x200 screen. So
  285. we can double the width and height of
  286. a sprite, again with two bit-logic
  287. bytes:
  288.  
  289.     X-expand 53277
  290.     Y-expand 53271
  291.  
  292.  Since we are working with Sprite 0,
  293. POKEing a 1 into these registers will
  294. affect our image. Note: the
  295. resolution is not improved, becoming
  296. rather blocky.
  297.  
  298.     Sprites can appear on top of
  299. screen pixels or behind, depending on
  300. the Priority register. This, too, is
  301. a bit-logic byte -- at 53275.
  302.  
  303.     The last pair of sprite registers
  304. report collisions. When a sprite
  305. touches any other sprite, or any
  306. screen pixel, the event is recorded
  307. in one of two bit-logic bytes:
  308.  
  309.     Sprite to Sprite Collision 53278
  310.