]> 4ch.mooo.com Git - 16.git/blob - 16/PCGPE10/TUT1.TXT
separated out row and column rendering into functions
[16.git] / 16 / PCGPE10 / TUT1.TXT
1                    ÕÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͸\r
2                    ³         W E L C O M E         ³\r
3                    ³  To the VGA Trainer Program   ³ ³\r
4                    ³              By               ³ ³\r
5                    ³      DENTHOR of ASPHYXIA      ³ ³ ³\r
6                    ÔÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ; ³ ³\r
7                      ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ ³\r
8                        ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ\r
9 \r
10                            --==[ PART 1 ]==--\r
11 \r
12 \r
13 \r
14 þ Introduction\r
15 \r
16 \r
17 \r
18 Hi there! This is Denthor of ASPHYXIA, AKA Grant Smith. This training\r
19 program is aimed at all those budding young demo coders out there. I am\r
20 assuming that the reader is fairly young, has a bit of basic Std. 6 math\r
21 under his belt, has done a bit of programming before, probably in BASIC,\r
22 and wants to learn how to write a demo all of his/her own.\r
23 \r
24 This I what I am going to do. I am going to describe how certain routines\r
25 work, and even give you working source code on how you do it. The source\r
26 code will assume that you have a VGA card that can handle the\r
27 320x200x256 mode. I will also assume that you have Turbo Pascal 6.0 or\r
28 above (this is because some of the code will be in Assembly language,\r
29 and Turbo Pascal 6.0 makes this incredibly easy to use). By the end of\r
30 the first "run" of sections, you will be able to code some cool demo\r
31 stuff all by yourself. The info you need, I will provide to you, but it\r
32 will be you who decides on the most spectacular way to use it.\r
33 \r
34 Why not download some of our demos and see what I'm trying to head you\r
35 towards.\r
36 \r
37 I will be posting one part a week on the Mailbox BBS. I have the first\r
38 "run" of sections worked out, but if you want me to also do sections on\r
39 other areas of coding, leave a message to Grant Smith in private E-Mail,\r
40 or start a conversation here in this conference. I will do a bit of\r
41 moderating of a sort, and point out things that have been done wrong.\r
42 \r
43 In this, the first part, I will show you how you are supposed to set up\r
44 your Pascal program, how to get into 320x200x256 graphics mode without a\r
45 BGI file, and various methods of putpixels and a clearscreen utility.\r
46 \r
47 NOTE : I drop source code all through my explanations. You needn't try\r
48        to grab all of it from all over the place, at the end of each part I\r
49        add a little program that uses all the new routines that we have\r
50        learned. If you do not fully understand a section, leave me\r
51        private mail telling me what you don't understand or asking how I\r
52        got something etc, and I will try to make myself clearer. One\r
53        last thing : When you spot a mistake I have made in one of my\r
54        parts, leave me mail and I will correct it post-haste.\r
55 \r
56 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
57 þ Disclaimer\r
58 \r
59 \r
60 \r
61 Hi again, sorry that I have to add this, but here goes. All source code\r
62 obtained from this series of instruction programs is used at your own\r
63 risk. Denthor and the ASPHYXIA demo team hold no responsibility for any\r
64 loss or damage suffered by anyone through the use of this code. Look\r
65 guys, the code I'm going to give you has been used by us before in\r
66 Demos, Applications etc, and we have never had any compliants of machine\r
67 damage, but if something does go wrong with your computer, don't blame\r
68 us. Sorry, but that's the way it is.\r
69 \r
70 \r
71 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
72 þ The MCGA mode and how you get into it in Pascal without a BGI\r
73 \r
74 \r
75 Lets face it. BGI's are next to worthless for demo coding. It is\r
76 difficult to find something that is slower then the BGI units for doing\r
77 graphics. Another thing is, they wern't really meant for 256 color\r
78 screens anyhow. You have to obtain a specific external 256VGA BGI to get\r
79 into it in Pascal, and it just doesn't make the grade.\r
80 \r
81 So the question remains, how do we get into MCGA 320x200x256 mode in\r
82 Pascal without a BGI? The answer is simple : Assembly language.\r
83 Obviously assembly language has loads of functions to handle the VGA\r
84 card, and this is just one of them. If you look in Norton Gides to\r
85 Assembly Language, it says this ...\r
86 \r
87 ____________________________________________________________________\r
88 INT 10h,  00h (0)        Set Video Mode\r
89 \r
90     Sets the video mode.\r
91 \r
92        On entry:      AH         00h\r
93                       AL         Video mode\r
94 \r
95        Returns:       None\r
96 \r
97        Registers destroyed:      AX, SP, BP, SI, DI\r
98 ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\r
99 \r
100 This is all well and good, but what does it mean? It means that if you\r
101 plug in the video mode into AL and call interrupt 10h, SHAZAM! you are\r
102 in the mode of your choice. Now, the MCGA video mode is mode 13h, and\r
103 here is how we do it in Pascal.\r
104 \r
105 Procedure SetMCGA;\r
106 BEGIN\r
107   asm\r
108         mov     ax,0013h\r
109         int     10h\r
110   end;\r
111 END;\r
112 \r
113 There you have it! One call to that procedure, and BANG you are in\r
114 320x200x256 mode. We can't actually do anything in it yet, so to go back\r
115 to text mode, you make the video mode equal to 03h, as seen below :\r
116 \r
117 Procedure SetText;\r
118 BEGIN\r
119   asm\r
120         mov     ax,0003h\r
121         int     10h\r
122   end;\r
123 END;\r
124 \r
125 \r
126 BANG! We are back in text mode! Now, cry all your enquiring minds, what\r
127 use is this? We can get into the mode, but how do we actually SHOW\r
128 something on the screen? For that, you must move onto the next section\r
129 ....\r
130 \r
131 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
132 þ  Clearing the screen to a specific color\r
133 \r
134 Now that we are in MCGA mode, how do we clear the screen. The answer is\r
135 simple : you must just remember that the base adress of the screen is\r
136 $a000. From $a000, the next 64000 bytes are what is actually displayed on\r
137 the screen (Note : 320 * 200 = 64000). So to clear the screen, you just use\r
138 the fillchar command (a basic Pascal command) like so :\r
139 \r
140       FillChar (Mem [$a000:0],64000,Col);\r
141 \r
142 What the mem command passes the Segment base and the Offset of a part of\r
143 memory : in this case the screen base is the Segment, and we are starting\r
144 at the top of the screen; Offset 0. The 64000 is the size of the screen\r
145 (see above), and Col is a value between 0 and 255, which represents the\r
146 color you want to clear the screen to.\r
147 \r
148 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
149 þ  Putting a pixel on the screen (two different methoods)\r
150 \r
151 If you look in Norton Guides about putting a pixel onto the screen, you\r
152 will see this  :\r
153 \r
154 \r
155 ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\r
156     Writes a pixel dot of a specified color at a specified screen\r
157     coordinate.\r
158 \r
159     On entry:      AH         0Ch\r
160                    AL         Pixel color\r
161                    CX         Horizontal position of pixel\r
162                    DX         Vertical position of pixel\r
163                    BH         Display page number (graphics modes with more\r
164                               than 1 page)\r
165 \r
166     Returns:       None\r
167 \r
168     Registers destroyed:      AX, SP, BP, SI, DI\r
169 ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\r
170 \r
171 As seen from our SetMCGA example, you would write this by doing the following:\r
172 \r
173 Procedure INTPutpixel (X,Y : Integer; Col : Byte);\r
174 BEGIN\r
175   asm\r
176      mov        ah,0Ch\r
177      mov        al,[col]\r
178      mov        cx,[x]\r
179      mov        dx,[y]\r
180      mov        bx,[1]\r
181      int        10h\r
182   end;\r
183 END;\r
184 \r
185 The X would be the X-Coordinate, the Y would be the Y-Coordinate, and the Col\r
186 would be the color of the pixel to place. Note that MCGA has 256 colors,\r
187 numbered 0 to 255. The startoff pallette is pretty grotty, and I will show\r
188 you how to alter it in my next lesson, but for now you will have to hunt for\r
189 colors that fit in for what you want to do. Luckily, a byte is 0 to 255, so\r
190 that is what we pass to the col variable. Have a look at the following.\r
191 \r
192     CGA = 4 colours.\r
193     4x4 = 16\r
194     EGA = 16 colors.\r
195     16x16 = 256\r
196     VGA = 256 colors.\r
197     Therefore an EGA is a CGA squared, and a VGA is an EGA squared ;-)\r
198 \r
199 Anyway, back to reality. Even though the abouve procedure is written in\r
200 assembly language, it is slooow. Why? I hear your enquiring minds cry. The\r
201 reason is simple : It uses interrupts (It calls INT 10h). Interrupts are\r
202 sloooow ... which is okay for getting into MCGA mode, but not for trying\r
203 to put down a pixel lickety-split. So, why not try the following ...\r
204 \r
205 Procedure MEMPutpixel (X,Y : Integer; Col : Byte);\r
206 BEGIN\r
207   Mem [VGA:X+(Y*320)]:=Col;\r
208 END;\r
209 \r
210 \r
211 The Mem command, as we have seen above, allows you to point at a certain\r
212 point in memory ... the starting point is $a000, the base of the VGA's\r
213 memory, and then we specify how far into this base memory we start.\r
214 Think of the monitor this way. It starts in the top left hand corner at\r
215 0. As you increase the number, you start to move across the screen to your\r
216 right, until you reach 320. At 320, you have gone all the way across the\r
217 screen and come back out the left side, one pixel down. This carries on\r
218 until you reach 63999, at the bottom right hand side of the screen. This\r
219 is how we get the equation X+(Y*320). For every increased Y, we must\r
220 increment the number by 320. Once we are at the beginning of the Y line\r
221 we want, we add our X by how far out we want to be. This gives us the\r
222 exact point in memory that we want to be at, and then we set it equal to\r
223 the pixel value we want.\r
224 \r
225 The MEM methood of putpixel is much faster, and it is shown in the sample\r
226 program at the end of this lesson. The ASPHYXIA team uses neither putpixel;\r
227 we use a DMA-Straight-To-Screen-Kill-Yer-Momma-With-An-Axe type putipixel\r
228 which is FAST. We will give it out, but only to those of you who show us\r
229 you are serious about coding. If you do do anything, upload it to me,\r
230 I will be very interested to see it. Remember : If you do glean anything\r
231 from these training sessions, give us a mention in your demos and UPLOAD\r
232 YOUR DEMO TO US!\r
233 \r
234 Well, after this is the sample program; have fun with it, UNDERSTAND it,\r
235 and next week I will start on fun with the pallette.\r
236 \r
237 See you all later,\r
238     - Denthor\r
239 \r
240 \r
241 ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\r
242 ³ TUTPROG1.PAS ³\r
243 ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ\r
244 \r
245 \r
246 {$X+}   (* This is a handy little trick to know. If you put this at the top\r
247            of your program, you do not have to set a variable when calling\r
248            a function, i.e. you may just say 'READKEY' instead of\r
249            'CH:=READKEY'                                                *)\r
250 \r
251 USES Crt;           (* This has a few nice functions in it, such as the\r
252                        READKEY command.                                 *)\r
253 \r
254 CONST VGA = $a000;  (* This sets the constant VGA to the segment of the\r
255                        VGA screen.                                      *)\r
256 \r
257 {ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ}\r
258 Procedure SetMCGA;  { This procedure gets you into 320x200x256 mode. }\r
259 BEGIN\r
260   asm\r
261      mov        ax,0013h\r
262      int        10h\r
263   end;\r
264 END;\r
265 \r
266 \r
267 {ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ}\r
268 Procedure SetText;  { This procedure returns you to text mode.  }\r
269 BEGIN\r
270   asm\r
271      mov        ax,0003h\r
272      int        10h\r
273   end;\r
274 END;\r
275 \r
276 \r
277 {ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ}\r
278 Procedure Cls (Col : Byte);\r
279    { This clears the screen to the specified color }\r
280 BEGIN\r
281   Fillchar (Mem [$a000:0],64000,col);\r
282 END;\r
283 \r
284 \r
285 {ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ}\r
286 Procedure INTPutpixel (X,Y : Integer; Col : Byte);\r
287    { This puts a pixel on the screen using interrupts. }\r
288 BEGIN\r
289   asm\r
290      mov        ah,0Ch\r
291      mov        al,[col]\r
292      mov        cx,[x]\r
293      mov        dx,[y]\r
294      mov        bx,[1]\r
295      int        10h\r
296   end;\r
297 END;\r
298 \r
299 \r
300 {ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ}\r
301 Procedure TestINTPutpixel;\r
302    { This tests out the speed of the INTPutpixel procedure. }\r
303 VAR loop1,loop2 : Integer;\r
304 BEGIN\r
305   For loop1:=0 to 319 do\r
306     For loop2:=0 to 199 do\r
307       INTPutpixel (loop1,loop2,Random (256));\r
308   Readkey;\r
309   Cls (0);\r
310 END;\r
311 \r
312 \r
313 \r
314 {ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ}\r
315 Procedure MEMPutpixel (X,Y : Integer; Col : Byte);\r
316   { This puts a pixel on the screen by writing directly to memory. }\r
317 BEGIN\r
318   Mem [VGA:X+(Y*320)]:=Col;\r
319 END;\r
320 \r
321 \r
322 {ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ}\r
323 Procedure TestMEMPutpixel;\r
324    { This tests out the speed of the MEMPutpixel procedure. }\r
325 VAR loop1,loop2 : Integer;\r
326 BEGIN\r
327   For loop1:=0 to 319 do\r
328     For loop2:=0 to 199 do\r
329       MEMPutpixel (loop1,loop2,Random (256));\r
330   Readkey;\r
331   Cls (0);\r
332 END;\r
333 \r
334 \r
335 \r
336 {ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ}\r
337 BEGIN    (* Of the main program *)\r
338   ClrScr;               { This clears the text Screen (CRT unit) }\r
339   Writeln ('What will happen is that I will clear the screen twice. After');\r
340   Writeln ('each clear screen you will have to hit a key. I will then fill');\r
341   Writeln ('the screen twice with randomlly colored pixels using two different');\r
342   Writeln ('methoods, after each of which you will have to hit a key. I will');\r
343   Writeln ('then return you to text mode.');\r
344   Writeln; Writeln;\r
345   Write ('Hit any kay to continue ...');\r
346   Readkey;\r
347 \r
348   SetMCGA;\r
349   CLS (32);\r
350   Readkey;\r
351   CLS (90);\r
352   Readkey;\r
353   TestINTPutpixel;\r
354   TestMEMPutpixel;\r
355   SetText;\r
356 \r
357   Writeln ('All done. This concludes the first sample program in the ASPHYXIA');\r
358   Writeln ('Training series. You may reach DENTHOR under the name of GRANT');\r
359   Writeln ('SMITH on the MailBox BBS, or leave a message to ASPHYXIA on the');\r
360   Writeln ('ASPHYXIA BBS. Get the numbers from Roblist, or write to :');\r
361   Writeln ('             Grant Smith');\r
362   Writeln ('             P.O. Box 270');\r
363   Writeln ('             Kloof');\r
364   Writeln ('             3640');\r
365   Writeln ('I hope to hear from you soon!');\r
366   Writeln; Writeln;\r
367   Write   ('Hit any key to exit ...');\r
368   Readkey;\r
369 END.     (* Of the main program *)