Bounce

It being a nice sunny weekend, I decided to fool around with bouncing sprites. Writing a real sprite moving toolkit is, of course, above my pay grade, so the idea was to work with what we already have. For this attempt I used EayPtr's ptrmen_cde. (But it isnt hard to convert it to use QJump's Qptr instead.) In theory, the identical code should work in mode 0/8 on a real (fast?) QL, but this piece here is strictly GD2 on some SMSQ/E emulator.

The method here is pretty trivial: Restore the previous background, work out the next location, save that background, and draw the sprite there. However, this means that the program has rather a lot to do in a short amount of time, so that large sprites or fast moving makes the sprite flash or jerk, and that doesnt look very appealing. Some improvement can be achieved by saving the whole window, and only restoring the necessary part. That saves saving the background for each iteration of the loop. But the difference isnt that noticeable.

If transparency is not required (QL mode 0/8 sprites dont have transparency), ie if using rectangular, opaque sprites, then more efficient methods can be used, particularly if the background is regular or solid. While not particularly efficient, this method isnt too bad for short moves, eg within a program window.

Note: I have only tested these in QPC2/SMSQmulator! They may be pretty crap on slow machines. Since graphics on the QL always was challenged, and no provision made for a graphics card/accelerator, its doubtful that there'll ever be any massively dynamic games or utilities for the brand, although a bespoke GD2 sprite toolkit might work for the high-end emulators, at least.

10 REMark     Init window
11 ch = FOPEN(#0; "con_")
12 wsx% = SCR_XLIM(#ch): wsy% = SCR_YLIM(#ch)
13 OUTLN#ch; wsx%, wsy%, 0, 0
14 :
15 RANDOMISE DATE
16 :
17 REMark     Load some sprite
18 spr = Ball16: REMark Or spr = SprLoad("win1_spr_Sun32_spr")
19 sx% = PEEK_W(spr + 4):          REMark Get size
20 sy% = PEEK_W(spr + 6)
21 xmx% = wsx% - sx%:              REMark Work out limits
22 ymx% = wsy% - sy%
23 cx% = RND(sx% TO xmx% - sx%):   REMark Start somewhere
24 cy% = RND(sy% TO ymx% - sy%)
25 dx% = -1: dy% = 1: s% = 2:      REMark Initial direction and speed
26 lx% = cx%: ly% = cy%:           REMark Last position
27 :
28 adr = WSAIN(#ch; sx%, sy%):     REMark Reserve save area
29 :
30 REMark     Save background and draw sprite
31 WSASV#ch; adr, sx%, sy%, cx%, cy%
32 SPRW#ch; cx%, cy%, spr
33 :
34 REMark     Lets roll!
35 REPeat bounce
36  REMark    Move
37  IF dx% > 0 THEN
38   cx% = cx% + s%
39   IF cx% > xmx%: dx% = -1: NEXT bounce
40  ELSE
41   cx% = cx% - s%
42   IF cx% < 0: dx% = 1: NEXT bounce
43  END IF
44  :
45  IF dy% > 0 THEN
46   cy% = cy% + s%
47   IF cy% > ymx%: dy% = -1: NEXT bounce
48  ELSE
49   cy% = cy% - s%
50   IF cy% < 0: dy% = 1: NEXT bounce
51  END IF
52  :
53  REMark Un-draw and re-draw sprite
54  WSARS#ch; adr\ sx%, sy%, lx%, ly%
55  WSASV#ch; adr, sx%, sy%, cx%, cy%
56  SPRW#ch; cx%, cy%, spr
57  lx% = cx%: ly% = cy%
58  :
59  REMark    User interaction
60  k% = CODE(INKEY$(#ch; 1))
61  SELect ON k%
62   = 27: EXIT bounce
63   = 32: BGET#ch; k%: IF k% = 27: EXIT bounce
64   = 208: REMark Cursor up  => faster
65          s% = s% + 1: IF s% > 9: s% = 9: BEEP 1000, 2
66   = 216: REMark Curso down => slower
67          s% = s% - 1: IF s% = 0: s% = 1: BEEP 1000, 2
68  END SELect
69 END REPeat bounce
70 QUIT
71 :
72 :
73 DEFine FuNction SprLoad(fnm$)
74 LOCal ch, fl, ad
75 ch = FOP_IN(fnm$): IF ch < 0: RETurn ch
76 fl = FLEN(#ch): CLOSE#ch
77 ad = ALCHP(fl)
78 LBYTES fnm$, ad
79 RETurn ad
80 END DEFine SprLoad
81 :
82 :
83 DEFine FuNction Ball16
84 LOCal dad
85 REMark Yellow, semi-transparent ball, GD2 mode 16, alpha, RLE, 22x22
86 :
87 dad = ALCHP(646)
88 POKE_W dad + 0, 528, 224, 22, 22, 0, 0, 0, 12, 0, 448
89 POKE_W dad + 20, 0, 0, 21068, 17713, 0, 528, -1280, 767, -8164, -768
90 POKE_W dad + 40, 540, -7937, -3072, 31, -2304, 31, -2304, 508, 1022, 252
91 POKE_W dad + 60, 1021, 1, 1020, -1536, 252, -512, -2301, -512, 252, -1280
92 POKE_W dad + 80, 259, 254, 1017, 6399, 1023, 0, 1020, 1, 7936, -253
93 POKE_W dad + 100, -488, -997, -488, -253, -256, 31, -512, 255, -256, 3
94 POKE_W dad + 120, -232, -741, -64, -485, -232, -253, -256, 255, -256, 480
95 POKE_W dad + 140, 255, 768, 6399, 7162, -16129, 7167, 6399, 769, 224, -256
96 POKE_W dad + 160, 540, 3, -232, 27, -64, -829, -64, -229, 24, -253
97 POKE_W dad + 180, 256, 7423, 254, 768, 6399, 6912, -16129, -15616, -11777, -9985
98 POKE_W dad + 200, -15361, -16384, 7167, 6146, 768, 1022, 0, 1023, 6144, 7167
99 POKE_W dad + 220, -16384, -15364, -9985, -15616, -16129, 6913, 6147, -1024, 3, -232
100 POKE_W dad + 240, 27, -64, 451, -9986, -9469, -10038, -15424, -229, 24, -253
101 POKE_W dad + 260, -512, -253, -232, 27, -64, 451, -9986, -9472, -9985, -15616
102 POKE_W dad + 280, -16129, 6912, 6398, 1023, 1, 7171, -232, 27, -64, 195
103 POKE_W dad + 300, -808, -61, 192, -229, 792, 769, 7423, 0, -7937, 768
104 POKE_W dad + 320, 6399, 6912, -16129, -15616, -13569, -9985, -15361, -16384, 7167, 6146
105 POKE_W dad + 340, 768, -7937, 0, -1, 1023, 6144, 7167, -16132, -15361, -16129
106 POKE_W dad + 360, 6912, 6399, 769, 255, -512, 31, -253, 24, -229, -1344
107 POKE_W dad + 380, -229, -232, 515, 287, -1024, 280, 1023, 6398, 6912, -30209
108 POKE_W dad + 400, -16130, 7167, 6399, 768, 6395, 0, -769, 1022, 6396, 6912
109 POKE_W dad + 420, 6911, 6398, 768, -774, 1, -1000, -253, -1768, -253, 280
110 POKE_W dad + 440, -777, 0, 8183, 768, 8180, 2, -800, 7421, 770, 7392
111 POKE_W dad + 460, -7, 0, 21068, 17713, 0, 484, -1280, 776, 13912, 28927
112 POKE_W dad + 480, 31747, 28760, 13832, -2560, 322, 31737, 32257, 31554, -1792, 276
113 POKE_W dad + 500, 25845, 32257, 25620, -1024, 276, 28915, 32257, 28692, -768, 100
114 POKE_W dad + 520, -3714, 100, -512, 66, -4226, 834, 8, 31727, 32258, 31496
115 POKE_W dad + 540, 14061, 32257, 13912, -4738, 344, 28909, 32257, 28796, -4738, -132
116 POKE_W dad + 560, -4738, 380, 28909, 32257, 28760, -4738, 344, 14061, 32258, 13832
117 POKE_W dad + 580, 31727, 32259, 31496, 66, -4226, 66, -512, 100, -3714, 100
118 POKE_W dad + 600, -768, 276, 28915, 32257, 28692, -1024, 276, 25845, 32257, 25620
119 POKE_W dad + 620, -1792, 322, 31737, 32257, 31554, -2560, 776, 13912, 28927, 31747
120 POKE_W dad + 640, 28760, 13832, -1280
121 RETurn dad
122 END DEFine Ball16
123 :
124 :
  
Generated with sb2htm on 2019 Mar 31
©pjwitte March 2ol9