OSDN Git Service

b131853d9f4f6e5cff5f9abb7ff28e0de34430fe
[proj16/16.git] / 16 / PCGPE10 / SCROLL.TXT
1 \r
2 \r
3 \r
4       SSSSS   CCCCC  RRRRR    OOOOO  LL    LL    IIIIII NN    NN  GGGGG\r
5      SS   SS CC   CC RR  RR  OO   OO LL    LL      II   NNN   NN GG   GG\r
6      SS      CC      RR   RR OO   OO LL    LL      II   NNNN  NN GG\r
7       SSSSS  CC      RR  RR  OO   OO LL    LL      II   NN NN NN GG\r
8           SS CC      RRRRR   OO   OO LL    LL      II   NN  NNNN GG  GGG\r
9      SS   SS CC   CC RR  RR  OO   OO LL    LL      II   NN   NNN GG   GG\r
10       SSSSS   CCCCC  RR   RR  OOOOO  LLLLL LLLLL IIIIII NN    NN  GGGGG\r
11 \r
12                     by Alec Thomas (Kestrel) of FORGE Software Australia\r
13                                           (c9223826@cs.newcastle.edu.au)\r
14 \r
15 \r
16 ------------\r
17 INTRODUCTION\r
18 ------------\r
19 Okay, here it is fans (and air conditioners, open windows...geez I hate that\r
20 joke!), how to do scrolling using either X-mode (and associated variants) and\r
21 standard mode 13h (not hard but I thought I'd put it in anyway :) as well as\r
22 the basics of parallax scrolling...\r
23 \r
24 First things first - X-mode. Throughout this little dissertation, I'm going\r
25 to assume that you know the basics of X-mode (or mode-X or mode-Y or\r
26 whatever you want to call it) such as how to get into it, how to set the\r
27 offset register, etc. and just get on with the scrolling :) I'm not trying\r
28 to teach you X-mode, but SCROLLING!!\r
29 \r
30 One further thing. I'm not saying that the methods I'll explain below are\r
31 the best method of scrolling, I'm just showing how I got it to work myself\r
32 in the hope that someone out there can use it. Anyway, enough of this crap,\r
33 on with the STUFF!!!\r
34 \r
35 (just a little note, when I'm talking about rows, they number from 0-199 and\r
36 the same with columns (except 0-319), etc. unless otherwise stated)\r
37 \r
38 ********************************************************************************\r
39 *                               X-MODE SCROLLING                               *\r
40 ********************************************************************************\r
41 ------------------\r
42 VERTICAL SCROLLING\r
43 ------------------\r
44 Ok, this is the easiest form of scrolling using the VGA hardware...fast and\r
45 clean. The following example assumes you are using 320x200 X-mode with the\r
46 visible page starting at the top of the first page (offset 0).\r
47 \r
48 To scroll what is on the screen up off the top, you simply add 80 (decimal)\r
49 to the screen offset register. This causes the screen to jump up by one\r
50 row. However, it also causes whatever is off the bottom of the screen\r
51 (the next page!) to become visible...not a desireable effect.\r
52 \r
53 Easily fixed however. Draw the image you want to scroll, on the row that\r
54 will scroll on. So, when the screen offset is changed to scroll the screen\r
55 up, the new data is already there for all to see. Beautiful!!!\r
56 \r
57 ----------- Scrolling A (up) --------------\r
58 OFFSET = 0\r
59 WHILE NOT FINISHED DO\r
60   OFFSET = OFFSET + 80\r
61   DRAW TO ROW 200\r
62   SET VGA OFFSET = OFFSET\r
63 END WHILE\r
64 -------------------------------------------\r
65 \r
66 Bzzzzz! Wrong! This works fine, until you have scrolled down to the\r
67 bottom of page 4. Because you're effectively off the bottom of the VGA\r
68 window (starting at segment A000h), you can't write to the rest of the\r
69 VGA memory (if there is any - only SVGA's have more than 256K on board\r
70 memory) and so, you'll be viewing garbage.\r
71 \r
72 No problem. The way around it is to only use two pages!!! "What?" I hear\r
73 you say. In fact, by using only two pages for scrolling, you gain two\r
74 major advantages: page flipping (because you're only using two pages for\r
75 the actual scrolling, you can use the spare two to perform page flipping)\r
76 and infinite scroll regions.\r
77 \r
78 You perform the infinite scrolling in exactly the same way as before, with\r
79 two minor additions: after changing the offset register, you copy the row\r
80 just scrolled on to the row just scrolled off. Also, after you have scrolled\r
81 a full page, you reset the offset to the top of the original page.\r
82 \r
83 ----------- Scrolling B (up) --------------\r
84 OFFSET = 0\r
85 WHILE NOT FINISHED DO\r
86   OFFSET = OFFSET + 80\r
87   IF OFFSET >= (200 * 80) THEN OFFSET = 0\r
88   DRAW TO ROW 200\r
89   SET VGA OFFSET = OFFSET\r
90   DRAW TO ROW -1 (was row 0 before scroll)\r
91 END WHILE\r
92 -------------------------------------------\r
93 \r
94 Ok, so that's how to do vertical scrolling, now on with horizontal scrolling.\r
95 \r
96 \r
97 \r
98 --------------------\r
99 HORIZONTAL SCROLLING\r
100 --------------------\r
101 Horizontal scrolling is essentially the same as vertical scrolling, all\r
102 you do is increment or decrement the VGA offset register by 1 instead of\r
103 80 as with vertical scrolling.\r
104 \r
105 However, horizontal scrolling is complicated by two things\r
106 \r
107   1. Incrementing the offset register by one actually scrolls by FOUR\r
108      pixels (and there are FOUR planes on the VGA, what a coincidence)\r
109 \r
110   2. You can't draw the image off the screen and then scroll it on\r
111      because of the way the VGA wraps to the next row every 80 bytes\r
112      (80 bytes * 4 planes = 320 pixels), if you tried it, you would\r
113      actually be drawing to the other side of the screen (which is\r
114      entirely visible)\r
115 \r
116 I'll solve these problems one at a time.\r
117 \r
118 Firstly, to get the VGA to scroll by only one pixel you use the horizontal\r
119 pixel panning (HPP) register. This register resides at\r
120 \r
121   PORT:     3C0H\r
122   INDEX:    13h\r
123 \r
124 and in real life, you use it like this\r
125 \r
126 ----------------- Pixel Panning ---------------\r
127 IN PORT 3DAH (this clears an internal\r
128   flip-flop of the VGA)\r
129 OUT 13H TO PORT 3C0H\r
130 OUT value TO PORT 3C0H (where "value" is the\r
131   number of pixels to offset)\r
132 -----------------------------------------------\r
133 \r
134 To implement smooth horizontal scrolling, you would do the following:\r
135 \r
136 -------------- Horizontal Scrolling ------------\r
137 FOR X = 0 TO 319 DO\r
138   SET HPP TO ( X MOD 4 )\r
139   SET VGA OFFSET TO ( X/4 )\r
140 END FOR\r
141 ------------------------------------------------\r
142 \r
143 Okay, no problem at all (although I think you might have to fiddle\r
144 around with the HPP a bit to get it right...try different values and\r
145 see what works :).\r
146 \r
147 So, the next problem is with drawing the images off the screen where\r
148 they aren't visible and then scrolling them on!!! As it turns out,\r
149 there's yet ANOTHER register to accomplish this. This one's called the\r
150 offset register (no, not the one I was talking about before, that one\r
151 was actually the "start address" register) and it's at\r
152 \r
153   PORT:     3D4H/3D5H\r
154   OFFSET:   13H\r
155 \r
156 and here's how to use it\r
157 \r
158 -------------- Offset Register ---------------\r
159 OUT 13H TO PORT 3D4H\r
160 OUT value TO PORT 3D5H\r
161 ----------------------------------------------\r
162 \r
163 Now, what my VGA reference says is that this register holds the number\r
164 of bytes (not pixels) difference between the start address of each row.\r
165 So, in X-mode it normally contains the value 80 (as we remember,\r
166 80 bytes * 4 planes = 320 pixels). This register does not affect the\r
167 VISIBLE width of the display, only the difference between addresses on\r
168 each row.\r
169 \r
170 When we scroll horizontally, we need a little bit of extra working space\r
171 so we can draw off the edge of the screen.\r
172 \r
173 Perhaps a little diagram will clarify it. The following picture is of a\r
174 standard X-mode addressing scheme with the OFFSET register set to 80.\r
175 \r
176       ROW    OFFSET\r
177       0         0 ========================\r
178       1        80 [                      ]\r
179       2       160 [                      ]\r
180       ..       .. [       VISIBLE        ]\r
181                   [        SCREEN        ]\r
182                   [                      ]\r
183                   [                      ]\r
184       ..       .. [                      ]\r
185       199   15920 ========================\r
186 \r
187 and the next diagram is of a modified addressing scheme with the OFFSET\r
188 register set to 82 (to give us 4 extra pixels on each side of the screen)\r
189 \r
190 ROW    OFFSET\r
191 0         0 ------========================------\r
192 1        82 |   V [                      ]   V |\r
193 2       164 |   I [                      ]   I |\r
194 ..       .. | N S [      VISIBLE         ] N S |\r
195             | O I [       SCREEN         ] O I |\r
196             | T B [                      ] T B |\r
197             |   L [                      ]   L |\r
198 ..       .. |   E [                      ]   E |\r
199 199   16318 ------========================------\r
200 \r
201 Beautiful!!!\r
202 \r
203 As with vertical scrolling, however, you still have the problem of when\r
204 you reach the bottom of page 4...and it's fixed in the same manner.\r
205 \r
206 I haven't actually managed to get infinite horizontal scrolling working,\r
207 but the method I have just stated will give you a horizontal scrolling\r
208 range of over 200 screens!!!! So if you need more (which is extremely\r
209 unlikely), figure it out yourself.\r
210 \r
211 \r
212 ------------------\r
213 COMBINED SCROLLING\r
214 ------------------\r
215 To do both horizontal and vertical scrolling, all you have to do is combine\r
216 the two methods with a few little extras (it's always the way isn't it).\r
217 \r
218 You have to start off with the original screen on the current page and the\r
219 next page as well. When you scroll horizontally, you have to draw the edge\r
220 that's coming in to the screen to BOTH pages (that means you'll be drawing\r
221 the incoming edge twice, once for each page). You do this so that when you\r
222 have scrolled vertically down through a complete page, you can jump back\r
223 to the first page and it will (hopefully) have an identical copy, and you\r
224 can then continue scrolling again.\r
225 \r
226 I'm sorry about this being so confusing but it's a bit difficult to explain.\r
227 \r
228 \r
229 \r
230 \r
231 \r
232 ********************************************************************************\r
233 *                            STANDARD VGA SCROLLING                            *\r
234 ********************************************************************************\r
235 Without X-mode, there is no easy way to do scrolling using the VGA hardware.\r
236 So basically, you have to resort to redrawing the entire screen for every\r
237 frame. Several popular games (Raptor and Mortal Kombat spring to mind)\r
238 utilise this method with excellent effect, so it is quite effective.\r
239 \r
240 Basically all you do to implement this is redraw the screen every frame\r
241 with a slightly different offset into the "map".\r
242 \r
243 The following bit of pseudo-code will scroll down and to the right\r
244 through the map.\r
245 \r
246 ------------- Standard Scrolling ---------------\r
247 X = 0\r
248 Y = 0\r
249 WHILE NOT FINISHED DO\r
250   DRAW TO SCREEN( 0, 0 ) FROM MAP( X, Y )\r
251   X = X + 1\r
252   Y = Y + 1\r
253 END WHILE\r
254 ------------------------------------------------\r
255 \r
256 \r
257 \r
258 \r
259 \r
260 ********************************************************************************\r
261 *                              PARALLAX SCROLLING                              *\r
262 ********************************************************************************\r
263 Parallax scrolling is when the "world" appears to have different levels\r
264 of perspective. That is, images further away from the viewer move\r
265 proportionately slower than images closer to the screen.\r
266 \r
267 To implement parallax scrolling, you need two or more "maps". You start\r
268 from the most distant map and end with the closest map. When you scroll,\r
269 you offset the map furthest away by the smallest value and the map\r
270 closest to you by the largest value.\r
271 \r
272 The following pseudo-code implements a 3 level parallax scrolling world,\r
273 scrolling (as above) down to the right.\r
274 \r
275 --------------- Parallax Scrolling ------------------\r
276 X = 0\r
277 Y = 0\r
278 WHILE NOT FINISHED DO\r
279   DRAW TO SCREEN( 0, 0 ) USING MAP_FAR AT ( X/4, Y/4 )\r
280   DRAW TO SCREEN( 0, 0 ) USING MAP_MEDIUM AT ( X/2, Y/2 )\r
281   DRAW TO SCREEN( 0, 0 ) USING MAP_NEAR AT ( X, Y )\r
282   X = X + 4\r
283   Y = Y + 4\r
284 END WHILE\r
285 -----------------------------------------------------\r
286 \r
287 Obviously, with parallax scrolling, each successive map shouldn't delete\r
288 the previous map entirely. So you'll have to draw the maps using some\r
289 sort of masking (masking being where you can see through the background\r
290 colour to what was there previously).\r
291 \r
292 \r
293 ********************************************************************************\r
294 *                                  DISCLAIMER                                  *\r
295 ********************************************************************************\r
296 I'm sorry if any of this is confusing, but hey that's half the fun of it -\r
297 figuring out what the hell I'm raving on about :)\r
298 \r
299 So, if you can figure it out, have fun and make games (preferably good ones!)\r
300 \r
301 Later,\r
302       Kestrel => FORGE Software Australia\r